blob: 23fecfb7e10097453862a5e62cd1a579eaf5a55c [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>
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020084#define CREATE_TRACE_POINTS
85#include <trace/events/io_uring.h>
86
Jens Axboe2b188cc2019-01-07 10:46:33 -070087#include <uapi/linux/io_uring.h>
88
89#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060090#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
Daniel Xu5277dea2019-09-14 14:23:45 -070092#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060093#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060094
95/*
96 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
97 */
98#define IORING_FILE_TABLE_SHIFT 9
99#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
100#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
101#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200102#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
103 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700104
105struct io_uring {
106 u32 head ____cacheline_aligned_in_smp;
107 u32 tail ____cacheline_aligned_in_smp;
108};
109
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000111 * This data is shared with the application through the mmap at offsets
112 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 *
114 * The offsets to the member fields are published through struct
115 * io_sqring_offsets when calling io_uring_setup.
116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
119 * Head and tail offsets into the ring; the offsets need to be
120 * masked to get valid indices.
121 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * The kernel controls head of the sq ring and the tail of the cq ring,
123 * and the application controls tail of the sq ring and the head of the
124 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 * ring_entries - 1)
130 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 u32 sq_ring_mask, cq_ring_mask;
132 /* Ring sizes (constant, power of 2) */
133 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Number of invalid entries dropped by the kernel due to
136 * invalid index stored in array
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application (i.e. get number of "new events" by comparing to
140 * cached value).
141 *
142 * After a new SQ head value was read by the application this
143 * counter includes all submissions that were dropped reaching
144 * the new SQ head (and possibly more).
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200148 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application.
152 *
153 * The application needs a full memory barrier before checking
154 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
155 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000156 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200158 * Runtime CQ flags
159 *
160 * Written by the application, shouldn't be modified by the
161 * kernel.
162 */
163 u32 cq_flags;
164 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * Number of completion events lost because the queue was full;
166 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800167 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 * the completion queue.
169 *
170 * Written by the kernel, shouldn't be modified by the
171 * application (i.e. get number of "new events" by comparing to
172 * cached value).
173 *
174 * As completion events come in out of order this counter is not
175 * ordered with any other data.
176 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000177 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200178 /*
179 * Ring buffer of completion events.
180 *
181 * The kernel writes completion events fresh every time they are
182 * produced, so the application is allowed to modify pending
183 * entries.
184 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000185 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700186};
187
Jens Axboeedafcce2019-01-09 09:16:05 -0700188struct io_mapped_ubuf {
189 u64 ubuf;
190 size_t len;
191 struct bio_vec *bvec;
192 unsigned int nr_bvecs;
193};
194
Jens Axboe65e19f52019-10-26 07:20:21 -0600195struct fixed_file_table {
196 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700197};
198
Xiaoguang Wang05589552020-03-31 14:05:18 +0800199struct fixed_file_ref_node {
200 struct percpu_ref refs;
201 struct list_head node;
202 struct list_head file_list;
203 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600204 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800205};
206
Jens Axboe05f3fb32019-12-09 11:22:50 -0700207struct fixed_file_data {
208 struct fixed_file_table *table;
209 struct io_ring_ctx *ctx;
210
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700212 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800214 struct list_head ref_list;
215 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216};
217
Jens Axboe5a2e7452020-02-23 16:23:11 -0700218struct io_buffer {
219 struct list_head list;
220 __u64 addr;
221 __s32 len;
222 __u16 bid;
223};
224
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200225struct io_restriction {
226 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
227 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
228 u8 sqe_flags_allowed;
229 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200230 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200231};
232
Jens Axboe534ca6d2020-09-02 13:52:19 -0600233struct io_sq_data {
234 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600235 struct mutex lock;
236
237 /* ctx's that are using this sqd */
238 struct list_head ctx_list;
239 struct list_head ctx_new_list;
240 struct mutex ctx_lock;
241
Jens Axboe534ca6d2020-09-02 13:52:19 -0600242 struct task_struct *thread;
243 struct wait_queue_head wait;
244};
245
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246struct io_ring_ctx {
247 struct {
248 struct percpu_ref refs;
249 } ____cacheline_aligned_in_smp;
250
251 struct {
252 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800253 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700254 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800255 unsigned int cq_overflow_flushed: 1;
256 unsigned int drain_next: 1;
257 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200258 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259
Hristo Venev75b28af2019-08-26 17:23:46 +0000260 /*
261 * Ring buffer of indices into array of io_uring_sqe, which is
262 * mmapped by the application using the IORING_OFF_SQES offset.
263 *
264 * This indirection could e.g. be used to assign fixed
265 * io_uring_sqe entries to operations and only submit them to
266 * the queue when needed.
267 *
268 * The kernel modifies neither the indices array nor the entries
269 * array.
270 */
271 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700272 unsigned cached_sq_head;
273 unsigned sq_entries;
274 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700275 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600276 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700278 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600279
280 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600281 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700282 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283
Jens Axboefcb323c2019-10-24 12:39:47 -0600284 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700285 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286 } ____cacheline_aligned_in_smp;
287
Hristo Venev75b28af2019-08-26 17:23:46 +0000288 struct io_rings *rings;
289
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600291 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600292
293 /*
294 * For SQPOLL usage - we hold a reference to the parent task, so we
295 * have access to the ->files
296 */
297 struct task_struct *sqo_task;
298
299 /* Only used for accounting purposes */
300 struct mm_struct *mm_account;
301
Jens Axboe534ca6d2020-09-02 13:52:19 -0600302 struct io_sq_data *sq_data; /* if using sq thread polling */
303
Jens Axboe90554202020-09-03 12:12:41 -0600304 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600305 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600306 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307
Jens Axboe6b063142019-01-10 22:13:58 -0700308 /*
309 * If used, fixed file set. Writers must ensure that ->refs is dead,
310 * readers must ensure that ->refs is alive as long as the file* is
311 * used. Only updated through io_uring_register(2).
312 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700313 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700314 unsigned nr_user_files;
315
Jens Axboeedafcce2019-01-09 09:16:05 -0700316 /* if used, fixed mapped user buffers */
317 unsigned nr_user_bufs;
318 struct io_mapped_ubuf *user_bufs;
319
Jens Axboe2b188cc2019-01-07 10:46:33 -0700320 struct user_struct *user;
321
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700322 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700323
Jens Axboe0f158b42020-05-14 17:18:39 -0600324 struct completion ref_comp;
325 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700326
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700327 /* if all else fails... */
328 struct io_kiocb *fallback_req;
329
Jens Axboe206aefd2019-11-07 18:27:42 -0700330#if defined(CONFIG_UNIX)
331 struct socket *ring_sock;
332#endif
333
Jens Axboe5a2e7452020-02-23 16:23:11 -0700334 struct idr io_buffer_idr;
335
Jens Axboe071698e2020-01-28 10:04:42 -0700336 struct idr personality_idr;
337
Jens Axboe206aefd2019-11-07 18:27:42 -0700338 struct {
339 unsigned cached_cq_tail;
340 unsigned cq_entries;
341 unsigned cq_mask;
342 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700343 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700344 struct wait_queue_head cq_wait;
345 struct fasync_struct *cq_fasync;
346 struct eventfd_ctx *cq_ev_fd;
347 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700348
349 struct {
350 struct mutex uring_lock;
351 wait_queue_head_t wait;
352 } ____cacheline_aligned_in_smp;
353
354 struct {
355 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700356
Jens Axboedef596e2019-01-09 08:59:42 -0700357 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300358 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700359 * io_uring instances that don't use IORING_SETUP_SQPOLL.
360 * For SQPOLL, only the single threaded io_sq_thread() will
361 * manipulate the list, hence no extra locking is needed there.
362 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300363 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700364 struct hlist_head *cancel_hash;
365 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700366 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600367
368 spinlock_t inflight_lock;
369 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700370 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600371
Jens Axboe4a38aed22020-05-14 17:21:15 -0600372 struct delayed_work file_put_work;
373 struct llist_head file_put_llist;
374
Jens Axboe85faa7b2020-04-09 18:14:00 -0600375 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200376 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700377};
378
Jens Axboe09bb8392019-03-13 12:39:28 -0600379/*
380 * First field must be the file pointer in all the
381 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
382 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700383struct io_poll_iocb {
384 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700385 union {
386 struct wait_queue_head *head;
387 u64 addr;
388 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700389 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600390 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700391 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700392 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700393};
394
Jens Axboeb5dba592019-12-11 14:02:38 -0700395struct io_close {
396 struct file *file;
397 struct file *put_file;
398 int fd;
399};
400
Jens Axboead8a48a2019-11-15 08:49:11 -0700401struct io_timeout_data {
402 struct io_kiocb *req;
403 struct hrtimer timer;
404 struct timespec64 ts;
405 enum hrtimer_mode mode;
406};
407
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700408struct io_accept {
409 struct file *file;
410 struct sockaddr __user *addr;
411 int __user *addr_len;
412 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600413 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700414};
415
416struct io_sync {
417 struct file *file;
418 loff_t len;
419 loff_t off;
420 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700421 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700422};
423
Jens Axboefbf23842019-12-17 18:45:56 -0700424struct io_cancel {
425 struct file *file;
426 u64 addr;
427};
428
Jens Axboeb29472e2019-12-17 18:50:29 -0700429struct io_timeout {
430 struct file *file;
431 u64 addr;
432 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300433 u32 off;
434 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300435 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700436};
437
Jens Axboe9adbd452019-12-20 08:45:55 -0700438struct io_rw {
439 /* NOTE: kiocb has the file as the first member, so don't do it here */
440 struct kiocb kiocb;
441 u64 addr;
442 u64 len;
443};
444
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700445struct io_connect {
446 struct file *file;
447 struct sockaddr __user *addr;
448 int addr_len;
449};
450
Jens Axboee47293f2019-12-20 08:58:21 -0700451struct io_sr_msg {
452 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700453 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300454 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700455 void __user *buf;
456 };
Jens Axboee47293f2019-12-20 08:58:21 -0700457 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700458 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700459 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700460 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700461};
462
Jens Axboe15b71ab2019-12-11 11:20:36 -0700463struct io_open {
464 struct file *file;
465 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700466 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700467 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600468 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700469};
470
Jens Axboe05f3fb32019-12-09 11:22:50 -0700471struct io_files_update {
472 struct file *file;
473 u64 arg;
474 u32 nr_args;
475 u32 offset;
476};
477
Jens Axboe4840e412019-12-25 22:03:45 -0700478struct io_fadvise {
479 struct file *file;
480 u64 offset;
481 u32 len;
482 u32 advice;
483};
484
Jens Axboec1ca7572019-12-25 22:18:28 -0700485struct io_madvise {
486 struct file *file;
487 u64 addr;
488 u32 len;
489 u32 advice;
490};
491
Jens Axboe3e4827b2020-01-08 15:18:09 -0700492struct io_epoll {
493 struct file *file;
494 int epfd;
495 int op;
496 int fd;
497 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498};
499
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300500struct io_splice {
501 struct file *file_out;
502 struct file *file_in;
503 loff_t off_out;
504 loff_t off_in;
505 u64 len;
506 unsigned int flags;
507};
508
Jens Axboeddf0322d2020-02-23 16:41:33 -0700509struct io_provide_buf {
510 struct file *file;
511 __u64 addr;
512 __s32 len;
513 __u32 bgid;
514 __u16 nbufs;
515 __u16 bid;
516};
517
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700518struct io_statx {
519 struct file *file;
520 int dfd;
521 unsigned int mask;
522 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700523 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700524 struct statx __user *buffer;
525};
526
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300527struct io_completion {
528 struct file *file;
529 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300530 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300531};
532
Jens Axboef499a022019-12-02 16:28:46 -0700533struct io_async_connect {
534 struct sockaddr_storage address;
535};
536
Jens Axboe03b12302019-12-02 18:50:25 -0700537struct io_async_msghdr {
538 struct iovec fast_iov[UIO_FASTIOV];
539 struct iovec *iov;
540 struct sockaddr __user *uaddr;
541 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700542 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700543};
544
Jens Axboef67676d2019-12-02 11:03:47 -0700545struct io_async_rw {
546 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600547 const struct iovec *free_iovec;
548 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600549 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600550 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700551};
552
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300553enum {
554 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
555 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
556 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
557 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
558 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700559 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300560
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300561 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562 REQ_F_FAIL_LINK_BIT,
563 REQ_F_INFLIGHT_BIT,
564 REQ_F_CUR_POS_BIT,
565 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300566 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300567 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300569 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700570 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700571 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600572 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800573 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700574
575 /* not a real bit, just to check we're not overflowing the space */
576 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577};
578
579enum {
580 /* ctx owns file */
581 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
582 /* drain existing IO first */
583 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
584 /* linked sqes */
585 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
586 /* doesn't sever on completion < 0 */
587 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
588 /* IOSQE_ASYNC */
589 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700590 /* IOSQE_BUFFER_SELECT */
591 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300592
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300593 /* head of a link */
594 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300595 /* fail rest of links */
596 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
597 /* on inflight list */
598 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
599 /* read/write uses file position */
600 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
601 /* must not punt to workers */
602 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300603 /* has linked timeout */
604 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300605 /* regular file */
606 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300607 /* completion under lock */
608 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300609 /* needs cleanup */
610 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700611 /* already went through poll handler */
612 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700613 /* buffer already selected */
614 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600615 /* doesn't need file table for this request */
616 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800617 /* io_wq_work is initialized */
618 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700619};
620
621struct async_poll {
622 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600623 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300624};
625
Jens Axboe09bb8392019-03-13 12:39:28 -0600626/*
627 * NOTE! Each of the iocb union members has the file pointer
628 * as the first entry in their struct definition. So you can
629 * access the file pointer through any of the sub-structs,
630 * or directly as just 'ki_filp' in this struct.
631 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700633 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600634 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700635 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700636 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700637 struct io_accept accept;
638 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700639 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700640 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700641 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700642 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700643 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700644 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700645 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700646 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700647 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700648 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300649 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700650 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700651 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300652 /* use only after cleaning per-op data, see io_clean_op() */
653 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700654 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700655
Jens Axboee8c2bc12020-08-15 18:44:09 -0700656 /* opcode allocated if it needs to store data for async defer */
657 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700658 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800659 /* polled IO has completed */
660 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700661
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700662 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300663 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700664
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300665 struct io_ring_ctx *ctx;
666 unsigned int flags;
667 refcount_t refs;
668 struct task_struct *task;
669 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700670
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300671 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700672
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300673 /*
674 * 1. used with ctx->iopoll_list with reads/writes
675 * 2. to track reqs with ->files (see io_op_def::file_table)
676 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300677 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600678
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300679 struct percpu_ref *fixed_file_refs;
680 struct callback_head task_work;
681 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
682 struct hlist_node hash_node;
683 struct async_poll *apoll;
684 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700685};
686
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300687struct io_defer_entry {
688 struct list_head list;
689 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300690 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300691};
692
Jens Axboedef596e2019-01-09 08:59:42 -0700693#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700694
Jens Axboe013538b2020-06-22 09:29:15 -0600695struct io_comp_state {
696 unsigned int nr;
697 struct list_head list;
698 struct io_ring_ctx *ctx;
699};
700
Jens Axboe9a56a232019-01-09 09:06:50 -0700701struct io_submit_state {
702 struct blk_plug plug;
703
704 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700705 * io_kiocb alloc cache
706 */
707 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300708 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700709
710 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600711 * Batch completion logic
712 */
713 struct io_comp_state comp;
714
715 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700716 * File reference cache
717 */
718 struct file *file;
719 unsigned int fd;
720 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700721 unsigned int ios_left;
722};
723
Jens Axboed3656342019-12-18 09:50:26 -0700724struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700725 /* needs current->mm setup, does mm access */
726 unsigned needs_mm : 1;
727 /* needs req->file assigned */
728 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600729 /* don't fail if file grab fails */
730 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700731 /* hash wq insertion if file is a regular file */
732 unsigned hash_reg_file : 1;
733 /* unbound wq insertion if file is a non-regular file */
734 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700735 /* opcode is not supported by this kernel */
736 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700737 /* needs file table */
738 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700739 /* needs ->fs */
740 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700741 /* set if opcode supports polled "wait" */
742 unsigned pollin : 1;
743 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700744 /* op supports buffer selection */
745 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700746 /* needs rlimit(RLIMIT_FSIZE) assigned */
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300747 unsigned needs_fsize : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700748 /* must always have async data allocated */
749 unsigned needs_async_data : 1;
750 /* size of async data needed, if any */
751 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700752};
753
Jens Axboe738277a2020-09-03 05:54:56 -0600754static const struct io_op_def io_op_defs[] __read_mostly = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_NOP] = {},
756 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700757 .needs_mm = 1,
758 .needs_file = 1,
759 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700760 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700761 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700762 .needs_async_data = 1,
763 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700764 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300765 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700766 .needs_mm = 1,
767 .needs_file = 1,
768 .hash_reg_file = 1,
769 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700770 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300771 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700772 .needs_async_data = 1,
773 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700774 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300775 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700776 .needs_file = 1,
777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700779 .needs_file = 1,
780 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700781 .pollin = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700782 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700783 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300784 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700785 .needs_file = 1,
786 .hash_reg_file = 1,
787 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700788 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300789 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700790 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700793 .needs_file = 1,
794 .unbound_nonreg_file = 1,
795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_POLL_REMOVE] = {},
797 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700798 .needs_file = 1,
799 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300800 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700801 .needs_mm = 1,
802 .needs_file = 1,
803 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700804 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700805 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700806 .needs_async_data = 1,
807 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .needs_mm = 1,
811 .needs_file = 1,
812 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700813 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700814 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700815 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700816 .needs_async_data = 1,
817 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700818 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300819 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700820 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700821 .needs_async_data = 1,
822 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700823 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300824 [IORING_OP_TIMEOUT_REMOVE] = {},
825 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700826 .needs_mm = 1,
827 .needs_file = 1,
828 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700829 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700830 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700831 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300832 [IORING_OP_ASYNC_CANCEL] = {},
833 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700834 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700835 .needs_async_data = 1,
836 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700837 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300838 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700839 .needs_mm = 1,
840 .needs_file = 1,
841 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700842 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700843 .needs_async_data = 1,
844 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700847 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300848 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700849 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300850 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700851 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700852 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600855 .needs_file = 1,
856 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700857 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700858 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300859 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700860 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700861 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700862 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300863 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700864 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700865 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600866 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700867 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300868 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700869 .needs_mm = 1,
870 .needs_file = 1,
871 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700872 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700873 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700874 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700875 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300876 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700877 .needs_mm = 1,
878 .needs_file = 1,
879 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700880 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300881 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700882 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700883 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300884 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700885 .needs_file = 1,
886 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300887 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700888 .needs_mm = 1,
889 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300890 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700891 .needs_mm = 1,
892 .needs_file = 1,
893 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700894 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700895 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700897 .needs_mm = 1,
898 .needs_file = 1,
899 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700900 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700901 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700902 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300903 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700904 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700905 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700906 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700907 [IORING_OP_EPOLL_CTL] = {
908 .unbound_nonreg_file = 1,
909 .file_table = 1,
910 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300911 [IORING_OP_SPLICE] = {
912 .needs_file = 1,
913 .hash_reg_file = 1,
914 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700915 },
916 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700917 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300918 [IORING_OP_TEE] = {
919 .needs_file = 1,
920 .hash_reg_file = 1,
921 .unbound_nonreg_file = 1,
922 },
Jens Axboed3656342019-12-18 09:50:26 -0700923};
924
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700925enum io_mem_account {
926 ACCT_LOCKED,
927 ACCT_PINNED,
928};
929
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300930static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
931 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700932static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800933static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600934static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700935static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700936static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600937static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700938static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700939static int __io_sqe_files_update(struct io_ring_ctx *ctx,
940 struct io_uring_files_update *ip,
941 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300942static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300943static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700944static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
945 int fd, struct file **out_file, bool fixed);
946static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600947 const struct io_uring_sqe *sqe,
948 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600949static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600950
Jens Axboeb63534c2020-06-04 11:28:00 -0600951static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
952 struct iovec **iovec, struct iov_iter *iter,
953 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600954static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
955 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600956 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700957
958static struct kmem_cache *req_cachep;
959
Jens Axboe738277a2020-09-03 05:54:56 -0600960static const struct file_operations io_uring_fops __read_mostly;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700961
962struct sock *io_uring_get_socket(struct file *file)
963{
964#if defined(CONFIG_UNIX)
965 if (file->f_op == &io_uring_fops) {
966 struct io_ring_ctx *ctx = file->private_data;
967
968 return ctx->ring_sock->sk;
969 }
970#endif
971 return NULL;
972}
973EXPORT_SYMBOL(io_uring_get_socket);
974
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300975static inline void io_clean_op(struct io_kiocb *req)
976{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300977 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
978 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300979 __io_clean_op(req);
980}
981
Jens Axboe4349f302020-07-09 15:07:01 -0600982static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600983{
984 struct mm_struct *mm = current->mm;
985
986 if (mm) {
987 kthread_unuse_mm(mm);
988 mmput(mm);
989 }
990}
991
992static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
993{
994 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300995 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600996 !ctx->sqo_task->mm ||
997 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600998 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600999 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -06001000 }
1001
1002 return 0;
1003}
1004
1005static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1006 struct io_kiocb *req)
1007{
1008 if (!io_op_defs[req->opcode].needs_mm)
1009 return 0;
1010 return __io_sq_thread_acquire_mm(ctx);
1011}
1012
1013static inline void req_set_fail_links(struct io_kiocb *req)
1014{
1015 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1016 req->flags |= REQ_F_FAIL_LINK;
1017}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001018
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001019/*
1020 * Note: must call io_req_init_async() for the first time you
1021 * touch any members of io_wq_work.
1022 */
1023static inline void io_req_init_async(struct io_kiocb *req)
1024{
1025 if (req->flags & REQ_F_WORK_INITIALIZED)
1026 return;
1027
1028 memset(&req->work, 0, sizeof(req->work));
1029 req->flags |= REQ_F_WORK_INITIALIZED;
1030}
1031
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001032static inline bool io_async_submit(struct io_ring_ctx *ctx)
1033{
1034 return ctx->flags & IORING_SETUP_SQPOLL;
1035}
1036
Jens Axboe2b188cc2019-01-07 10:46:33 -07001037static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1038{
1039 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1040
Jens Axboe0f158b42020-05-14 17:18:39 -06001041 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001042}
1043
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001044static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1045{
1046 return !req->timeout.off;
1047}
1048
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1050{
1051 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001052 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001053
1054 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1055 if (!ctx)
1056 return NULL;
1057
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001058 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1059 if (!ctx->fallback_req)
1060 goto err;
1061
Jens Axboe78076bb2019-12-04 19:56:40 -07001062 /*
1063 * Use 5 bits less than the max cq entries, that should give us around
1064 * 32 entries per hash list if totally full and uniformly spread.
1065 */
1066 hash_bits = ilog2(p->cq_entries);
1067 hash_bits -= 5;
1068 if (hash_bits <= 0)
1069 hash_bits = 1;
1070 ctx->cancel_hash_bits = hash_bits;
1071 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1072 GFP_KERNEL);
1073 if (!ctx->cancel_hash)
1074 goto err;
1075 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1076
Roman Gushchin21482892019-05-07 10:01:48 -07001077 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001078 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1079 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001080
1081 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001082 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001083 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001085 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001086 init_completion(&ctx->ref_comp);
1087 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001088 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001089 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001090 mutex_init(&ctx->uring_lock);
1091 init_waitqueue_head(&ctx->wait);
1092 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001093 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001094 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001095 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001096 init_waitqueue_head(&ctx->inflight_wait);
1097 spin_lock_init(&ctx->inflight_lock);
1098 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001099 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1100 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001102err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001103 if (ctx->fallback_req)
1104 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001105 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001106 kfree(ctx);
1107 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001108}
1109
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001110static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001111{
Jens Axboe2bc99302020-07-09 09:43:27 -06001112 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1113 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001114
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001115 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001116 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001117 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001118
Bob Liu9d858b22019-11-13 18:06:25 +08001119 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001120}
1121
Jens Axboede0617e2019-04-06 21:51:27 -06001122static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001123{
Hristo Venev75b28af2019-08-26 17:23:46 +00001124 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125
Pavel Begunkov07910152020-01-17 03:52:46 +03001126 /* order cqe stores with ring update */
1127 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001128
Pavel Begunkov07910152020-01-17 03:52:46 +03001129 if (wq_has_sleeper(&ctx->cq_wait)) {
1130 wake_up_interruptible(&ctx->cq_wait);
1131 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132 }
1133}
1134
Jens Axboe51a4cc12020-08-10 10:55:56 -06001135/*
1136 * Returns true if we need to defer file table putting. This can only happen
1137 * from the error path with REQ_F_COMP_LOCKED set.
1138 */
1139static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001140{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001141 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001142 return false;
1143
1144 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001145
Jens Axboecccf0ee2020-01-27 16:34:48 -07001146 if (req->work.mm) {
1147 mmdrop(req->work.mm);
1148 req->work.mm = NULL;
1149 }
1150 if (req->work.creds) {
1151 put_cred(req->work.creds);
1152 req->work.creds = NULL;
1153 }
Jens Axboeff002b32020-02-07 16:05:21 -07001154 if (req->work.fs) {
1155 struct fs_struct *fs = req->work.fs;
1156
Jens Axboe51a4cc12020-08-10 10:55:56 -06001157 if (req->flags & REQ_F_COMP_LOCKED)
1158 return true;
1159
Jens Axboeff002b32020-02-07 16:05:21 -07001160 spin_lock(&req->work.fs->lock);
1161 if (--fs->users)
1162 fs = NULL;
1163 spin_unlock(&req->work.fs->lock);
1164 if (fs)
1165 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001166 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001167 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001168
1169 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001170}
1171
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001172static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001173{
Jens Axboed3656342019-12-18 09:50:26 -07001174 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001175
Pavel Begunkov16d59802020-07-12 16:16:47 +03001176 io_req_init_async(req);
1177
Jens Axboed3656342019-12-18 09:50:26 -07001178 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001179 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001180 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001181 } else {
1182 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001183 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001184 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001185 if (!req->work.mm && def->needs_mm) {
1186 mmgrab(current->mm);
1187 req->work.mm = current->mm;
1188 }
1189 if (!req->work.creds)
1190 req->work.creds = get_current_cred();
1191 if (!req->work.fs && def->needs_fs) {
1192 spin_lock(&current->fs->lock);
1193 if (!current->fs->in_exec) {
1194 req->work.fs = current->fs;
1195 req->work.fs->users++;
1196 } else {
1197 req->work.flags |= IO_WQ_WORK_CANCEL;
1198 }
1199 spin_unlock(&current->fs->lock);
1200 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001201 if (def->needs_fsize)
1202 req->work.fsize = rlimit(RLIMIT_FSIZE);
1203 else
1204 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001205}
1206
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001207static void io_prep_async_link(struct io_kiocb *req)
1208{
1209 struct io_kiocb *cur;
1210
1211 io_prep_async_work(req);
1212 if (req->flags & REQ_F_LINK_HEAD)
1213 list_for_each_entry(cur, &req->link_list, link_list)
1214 io_prep_async_work(cur);
1215}
1216
Jens Axboe7271ef32020-08-10 09:55:22 -06001217static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001218{
Jackie Liua197f662019-11-08 08:09:12 -07001219 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001220 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001221
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001222 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1223 &req->work, req->flags);
1224 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001225 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001226}
1227
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001228static void io_queue_async_work(struct io_kiocb *req)
1229{
Jens Axboe7271ef32020-08-10 09:55:22 -06001230 struct io_kiocb *link;
1231
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001232 /* init ->work of the whole link before punting */
1233 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001234 link = __io_queue_async_work(req);
1235
1236 if (link)
1237 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001238}
1239
Jens Axboe5262f562019-09-17 12:26:57 -06001240static void io_kill_timeout(struct io_kiocb *req)
1241{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001242 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001243 int ret;
1244
Jens Axboee8c2bc12020-08-15 18:44:09 -07001245 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001246 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001247 atomic_set(&req->ctx->cq_timeouts,
1248 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001249 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001250 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001251 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001252 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001253 }
1254}
1255
Jens Axboef3606e32020-09-22 08:18:24 -06001256static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1257{
1258 struct io_ring_ctx *ctx = req->ctx;
1259
1260 if (!tsk || req->task == tsk)
1261 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001262 if (ctx->flags & IORING_SETUP_SQPOLL) {
1263 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1264 return true;
1265 }
Jens Axboef3606e32020-09-22 08:18:24 -06001266 return false;
1267}
1268
Jens Axboe76e1b642020-09-26 15:05:03 -06001269/*
1270 * Returns true if we found and killed one or more timeouts
1271 */
1272static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001273{
1274 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001275 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001276
1277 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001278 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001279 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001280 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001281 canceled++;
1282 }
Jens Axboef3606e32020-09-22 08:18:24 -06001283 }
Jens Axboe5262f562019-09-17 12:26:57 -06001284 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001285 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001286}
1287
Pavel Begunkov04518942020-05-26 20:34:05 +03001288static void __io_queue_deferred(struct io_ring_ctx *ctx)
1289{
1290 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001291 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1292 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001293 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001294
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001295 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001296 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001297 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001298 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001299 link = __io_queue_async_work(de->req);
1300 if (link) {
1301 __io_queue_linked_timeout(link);
1302 /* drop submission reference */
1303 link->flags |= REQ_F_COMP_LOCKED;
1304 io_put_req(link);
1305 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001306 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001307 } while (!list_empty(&ctx->defer_list));
1308}
1309
Pavel Begunkov360428f2020-05-30 14:54:17 +03001310static void io_flush_timeouts(struct io_ring_ctx *ctx)
1311{
1312 while (!list_empty(&ctx->timeout_list)) {
1313 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001314 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001315
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001316 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001317 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001318 if (req->timeout.target_seq != ctx->cached_cq_tail
1319 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001320 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001321
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001322 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001323 io_kill_timeout(req);
1324 }
1325}
1326
Jens Axboede0617e2019-04-06 21:51:27 -06001327static void io_commit_cqring(struct io_ring_ctx *ctx)
1328{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001329 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001330 __io_commit_cqring(ctx);
1331
Pavel Begunkov04518942020-05-26 20:34:05 +03001332 if (unlikely(!list_empty(&ctx->defer_list)))
1333 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001334}
1335
Jens Axboe90554202020-09-03 12:12:41 -06001336static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1337{
1338 struct io_rings *r = ctx->rings;
1339
1340 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1341}
1342
Jens Axboe2b188cc2019-01-07 10:46:33 -07001343static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1344{
Hristo Venev75b28af2019-08-26 17:23:46 +00001345 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001346 unsigned tail;
1347
1348 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001349 /*
1350 * writes to the cq entry need to come after reading head; the
1351 * control dependency is enough as we're using WRITE_ONCE to
1352 * fill the cq entry
1353 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001354 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001355 return NULL;
1356
1357 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001358 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359}
1360
Jens Axboef2842ab2020-01-08 11:04:00 -07001361static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1362{
Jens Axboef0b493e2020-02-01 21:30:11 -07001363 if (!ctx->cq_ev_fd)
1364 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001365 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1366 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001367 if (!ctx->eventfd_async)
1368 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001369 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001370}
1371
Jens Axboeb41e9852020-02-17 09:52:41 -07001372static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001373{
1374 if (waitqueue_active(&ctx->wait))
1375 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001376 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1377 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001378 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001379 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001380}
1381
Pavel Begunkov46930142020-07-30 18:43:49 +03001382static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1383{
1384 if (list_empty(&ctx->cq_overflow_list)) {
1385 clear_bit(0, &ctx->sq_check_overflow);
1386 clear_bit(0, &ctx->cq_check_overflow);
1387 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1388 }
1389}
1390
Jens Axboee6c8aa92020-09-28 13:10:13 -06001391static inline bool io_match_files(struct io_kiocb *req,
1392 struct files_struct *files)
1393{
1394 if (!files)
1395 return true;
1396 if (req->flags & REQ_F_WORK_INITIALIZED)
1397 return req->work.files == files;
1398 return false;
1399}
1400
Jens Axboec4a2ed72019-11-21 21:01:26 -07001401/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001402static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1403 struct task_struct *tsk,
1404 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001406 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001407 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001408 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001409 unsigned long flags;
1410 LIST_HEAD(list);
1411
1412 if (!force) {
1413 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001414 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001415 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1416 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001417 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001418 }
1419
1420 spin_lock_irqsave(&ctx->completion_lock, flags);
1421
1422 /* if force is set, the ring is going away. always drop after that */
1423 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001424 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001425
Jens Axboec4a2ed72019-11-21 21:01:26 -07001426 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001427 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1428 if (tsk && req->task != tsk)
1429 continue;
1430 if (!io_match_files(req, files))
1431 continue;
1432
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001433 cqe = io_get_cqring(ctx);
1434 if (!cqe && !force)
1435 break;
1436
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001437 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001438 if (cqe) {
1439 WRITE_ONCE(cqe->user_data, req->user_data);
1440 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001441 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001442 } else {
1443 WRITE_ONCE(ctx->rings->cq_overflow,
1444 atomic_inc_return(&ctx->cached_cq_overflow));
1445 }
1446 }
1447
1448 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001449 io_cqring_mark_overflow(ctx);
1450
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001451 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1452 io_cqring_ev_posted(ctx);
1453
1454 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001455 req = list_first_entry(&list, struct io_kiocb, compl.list);
1456 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001457 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001458 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001459
1460 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001461}
1462
Jens Axboebcda7ba2020-02-23 16:42:51 -07001463static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001464{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001465 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001466 struct io_uring_cqe *cqe;
1467
Jens Axboe78e19bb2019-11-06 15:21:34 -07001468 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001469
Jens Axboe2b188cc2019-01-07 10:46:33 -07001470 /*
1471 * If we can't get a cq entry, userspace overflowed the
1472 * submission (by quite a lot). Increment the overflow count in
1473 * the ring.
1474 */
1475 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001476 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001477 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001478 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001479 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001480 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1481 /*
1482 * If we're in ring overflow flush mode, or in task cancel mode,
1483 * then we cannot store the request for later flushing, we need
1484 * to drop it on the floor.
1485 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001486 WRITE_ONCE(ctx->rings->cq_overflow,
1487 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001488 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001489 if (list_empty(&ctx->cq_overflow_list)) {
1490 set_bit(0, &ctx->sq_check_overflow);
1491 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001492 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001493 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001494 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001495 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001496 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001497 refcount_inc(&req->refs);
1498 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499 }
1500}
1501
Jens Axboebcda7ba2020-02-23 16:42:51 -07001502static void io_cqring_fill_event(struct io_kiocb *req, long res)
1503{
1504 __io_cqring_fill_event(req, res, 0);
1505}
1506
Jens Axboee1e16092020-06-22 09:17:17 -06001507static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001508{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001509 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001510 unsigned long flags;
1511
1512 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001513 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001514 io_commit_cqring(ctx);
1515 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1516
Jens Axboe8c838782019-03-12 15:48:16 -06001517 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001518}
1519
Jens Axboe229a7b62020-06-22 10:13:11 -06001520static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001521{
Jens Axboe229a7b62020-06-22 10:13:11 -06001522 struct io_ring_ctx *ctx = cs->ctx;
1523
1524 spin_lock_irq(&ctx->completion_lock);
1525 while (!list_empty(&cs->list)) {
1526 struct io_kiocb *req;
1527
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001528 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1529 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001530 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001531 if (!(req->flags & REQ_F_LINK_HEAD)) {
1532 req->flags |= REQ_F_COMP_LOCKED;
1533 io_put_req(req);
1534 } else {
1535 spin_unlock_irq(&ctx->completion_lock);
1536 io_put_req(req);
1537 spin_lock_irq(&ctx->completion_lock);
1538 }
1539 }
1540 io_commit_cqring(ctx);
1541 spin_unlock_irq(&ctx->completion_lock);
1542
1543 io_cqring_ev_posted(ctx);
1544 cs->nr = 0;
1545}
1546
1547static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1548 struct io_comp_state *cs)
1549{
1550 if (!cs) {
1551 io_cqring_add_event(req, res, cflags);
1552 io_put_req(req);
1553 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001554 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001555 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001556 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001557 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001558 if (++cs->nr >= 32)
1559 io_submit_flush_completions(cs);
1560 }
Jens Axboee1e16092020-06-22 09:17:17 -06001561}
1562
1563static void io_req_complete(struct io_kiocb *req, long res)
1564{
Jens Axboe229a7b62020-06-22 10:13:11 -06001565 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001566}
1567
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001568static inline bool io_is_fallback_req(struct io_kiocb *req)
1569{
1570 return req == (struct io_kiocb *)
1571 ((unsigned long) req->ctx->fallback_req & ~1UL);
1572}
1573
1574static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1575{
1576 struct io_kiocb *req;
1577
1578 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001579 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001580 return req;
1581
1582 return NULL;
1583}
1584
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001585static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1586 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001587{
Jens Axboefd6fab22019-03-14 16:30:06 -06001588 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589 struct io_kiocb *req;
1590
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001591 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001592 size_t sz;
1593 int ret;
1594
1595 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001596 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1597
1598 /*
1599 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1600 * retry single alloc to be on the safe side.
1601 */
1602 if (unlikely(ret <= 0)) {
1603 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1604 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001605 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001606 ret = 1;
1607 }
Jens Axboe2579f912019-01-09 09:10:43 -07001608 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001609 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001610 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001611 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001612 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001613 }
1614
Jens Axboe2579f912019-01-09 09:10:43 -07001615 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001616fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001617 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001618}
1619
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001620static inline void io_put_file(struct io_kiocb *req, struct file *file,
1621 bool fixed)
1622{
1623 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001624 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001625 else
1626 fput(file);
1627}
1628
Jens Axboe51a4cc12020-08-10 10:55:56 -06001629static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001630{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001631 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001632
Jens Axboee8c2bc12020-08-15 18:44:09 -07001633 if (req->async_data)
1634 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001635 if (req->file)
1636 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001637
Jens Axboe51a4cc12020-08-10 10:55:56 -06001638 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001639}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001640
Jens Axboe51a4cc12020-08-10 10:55:56 -06001641static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001642{
Jens Axboe0f212202020-09-13 13:09:39 -06001643 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001644 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001645
Jens Axboe0f212202020-09-13 13:09:39 -06001646 atomic_long_inc(&tctx->req_complete);
1647 if (tctx->in_idle)
1648 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001649 put_task_struct(req->task);
1650
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001651 if (likely(!io_is_fallback_req(req)))
1652 kmem_cache_free(req_cachep, req);
1653 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001654 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1655 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001656}
1657
Jens Axboe51a4cc12020-08-10 10:55:56 -06001658static void io_req_task_file_table_put(struct callback_head *cb)
1659{
1660 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1661 struct fs_struct *fs = req->work.fs;
1662
1663 spin_lock(&req->work.fs->lock);
1664 if (--fs->users)
1665 fs = NULL;
1666 spin_unlock(&req->work.fs->lock);
1667 if (fs)
1668 free_fs_struct(fs);
1669 req->work.fs = NULL;
1670 __io_free_req_finish(req);
1671}
1672
1673static void __io_free_req(struct io_kiocb *req)
1674{
1675 if (!io_dismantle_req(req)) {
1676 __io_free_req_finish(req);
1677 } else {
1678 int ret;
1679
1680 init_task_work(&req->task_work, io_req_task_file_table_put);
1681 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1682 if (unlikely(ret)) {
1683 struct task_struct *tsk;
1684
1685 tsk = io_wq_get_task(req->ctx->io_wq);
1686 task_work_add(tsk, &req->task_work, 0);
1687 }
1688 }
1689}
1690
Jackie Liua197f662019-11-08 08:09:12 -07001691static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001692{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001693 struct io_timeout_data *io = req->async_data;
Jackie Liua197f662019-11-08 08:09:12 -07001694 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001695 int ret;
1696
Jens Axboee8c2bc12020-08-15 18:44:09 -07001697 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001698 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001699 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001700 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001701 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001702 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001703 return true;
1704 }
1705
1706 return false;
1707}
1708
Jens Axboeab0b6452020-06-30 08:43:15 -06001709static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001710{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001711 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001712 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001713
1714 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001715 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001716 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1717 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001718 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001719
1720 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001721 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001722 wake_ev = io_link_cancel_timeout(link);
1723 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001724 return wake_ev;
1725}
1726
1727static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001728{
Jens Axboe2665abf2019-11-05 12:40:47 -07001729 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001730 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001731
Jens Axboeab0b6452020-06-30 08:43:15 -06001732 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1733 unsigned long flags;
1734
1735 spin_lock_irqsave(&ctx->completion_lock, flags);
1736 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001737 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001738 } else {
1739 wake_ev = __io_kill_linked_timeout(req);
1740 }
1741
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001742 if (wake_ev)
1743 io_cqring_ev_posted(ctx);
1744}
1745
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001746static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001747{
1748 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001749
Jens Axboe9e645e112019-05-10 16:07:28 -06001750 /*
1751 * The list should never be empty when we are called here. But could
1752 * potentially happen if the chain is messed up, check to be on the
1753 * safe side.
1754 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001755 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001756 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001757
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001758 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1759 list_del_init(&req->link_list);
1760 if (!list_empty(&nxt->link_list))
1761 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001762 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001763}
1764
1765/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001766 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001767 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001768static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001769{
Jens Axboe2665abf2019-11-05 12:40:47 -07001770 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001771
1772 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001773 struct io_kiocb *link = list_first_entry(&req->link_list,
1774 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001775
Pavel Begunkov44932332019-12-05 16:16:35 +03001776 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001777 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001778
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001779 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001780 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001781 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001782 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001783 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001784
1785 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001786 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001787}
1788
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001789static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001790{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001791 struct io_ring_ctx *ctx = req->ctx;
1792
1793 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1794 unsigned long flags;
1795
1796 spin_lock_irqsave(&ctx->completion_lock, flags);
1797 __io_fail_links(req);
1798 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1799 } else {
1800 __io_fail_links(req);
1801 }
1802
Jens Axboe9e645e112019-05-10 16:07:28 -06001803 io_cqring_ev_posted(ctx);
1804}
1805
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001806static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001807{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001808 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001809 if (req->flags & REQ_F_LINK_TIMEOUT)
1810 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001811
Jens Axboe9e645e112019-05-10 16:07:28 -06001812 /*
1813 * If LINK is set, we have dependent requests in this chain. If we
1814 * didn't fail this request, queue the first one up, moving any other
1815 * dependencies to the next request. In case of failure, fail the rest
1816 * of the chain.
1817 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001818 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1819 return io_req_link_next(req);
1820 io_fail_links(req);
1821 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001822}
Jens Axboe2665abf2019-11-05 12:40:47 -07001823
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001824static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1825{
1826 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1827 return NULL;
1828 return __io_req_find_next(req);
1829}
1830
Jens Axboefd7d6de2020-08-23 11:00:37 -06001831static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1832 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001833{
1834 struct task_struct *tsk = req->task;
1835 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001836 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001837
Jens Axboe6200b0a2020-09-13 14:38:30 -06001838 if (tsk->flags & PF_EXITING)
1839 return -ESRCH;
1840
Jens Axboec2c4c832020-07-01 15:37:11 -06001841 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001842 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1843 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1844 * processing task_work. There's no reliable way to tell if TWA_RESUME
1845 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001846 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001847 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001848 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001849 notify = TWA_SIGNAL;
1850
1851 ret = task_work_add(tsk, cb, notify);
1852 if (!ret)
1853 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001854
Jens Axboec2c4c832020-07-01 15:37:11 -06001855 return ret;
1856}
1857
Jens Axboec40f6372020-06-25 15:39:59 -06001858static void __io_req_task_cancel(struct io_kiocb *req, int error)
1859{
1860 struct io_ring_ctx *ctx = req->ctx;
1861
1862 spin_lock_irq(&ctx->completion_lock);
1863 io_cqring_fill_event(req, error);
1864 io_commit_cqring(ctx);
1865 spin_unlock_irq(&ctx->completion_lock);
1866
1867 io_cqring_ev_posted(ctx);
1868 req_set_fail_links(req);
1869 io_double_put_req(req);
1870}
1871
1872static void io_req_task_cancel(struct callback_head *cb)
1873{
1874 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001875 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001876
1877 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001878 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001879}
1880
1881static void __io_req_task_submit(struct io_kiocb *req)
1882{
1883 struct io_ring_ctx *ctx = req->ctx;
1884
Jens Axboec40f6372020-06-25 15:39:59 -06001885 if (!__io_sq_thread_acquire_mm(ctx)) {
1886 mutex_lock(&ctx->uring_lock);
1887 __io_queue_sqe(req, NULL, NULL);
1888 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001889 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001890 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001891 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001892}
1893
Jens Axboec40f6372020-06-25 15:39:59 -06001894static void io_req_task_submit(struct callback_head *cb)
1895{
1896 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001898
1899 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001900 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001901}
1902
1903static void io_req_task_queue(struct io_kiocb *req)
1904{
Jens Axboec40f6372020-06-25 15:39:59 -06001905 int ret;
1906
1907 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001908 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001909
Jens Axboefd7d6de2020-08-23 11:00:37 -06001910 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001911 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001912 struct task_struct *tsk;
1913
Jens Axboec40f6372020-06-25 15:39:59 -06001914 init_task_work(&req->task_work, io_req_task_cancel);
1915 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001916 task_work_add(tsk, &req->task_work, 0);
1917 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001918 }
Jens Axboec40f6372020-06-25 15:39:59 -06001919}
1920
Pavel Begunkovc3524382020-06-28 12:52:32 +03001921static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001922{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001923 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001924
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001925 if (nxt)
1926 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001927}
1928
Jens Axboe9e645e112019-05-10 16:07:28 -06001929static void io_free_req(struct io_kiocb *req)
1930{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001931 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001932 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001933}
1934
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001935struct req_batch {
1936 void *reqs[IO_IOPOLL_BATCH];
1937 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001938
1939 struct task_struct *task;
1940 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001941};
1942
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001943static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001944{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001945 rb->to_free = 0;
1946 rb->task_refs = 0;
1947 rb->task = NULL;
1948}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001949
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001950static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1951 struct req_batch *rb)
1952{
1953 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1954 percpu_ref_put_many(&ctx->refs, rb->to_free);
1955 rb->to_free = 0;
1956}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001957
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001958static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1959 struct req_batch *rb)
1960{
1961 if (rb->to_free)
1962 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001963 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001964 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001965 put_task_struct_many(rb->task, rb->task_refs);
1966 rb->task = NULL;
1967 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001968}
1969
1970static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1971{
1972 if (unlikely(io_is_fallback_req(req))) {
1973 io_free_req(req);
1974 return;
1975 }
1976 if (req->flags & REQ_F_LINK_HEAD)
1977 io_queue_next(req);
1978
Jens Axboee3bc8e92020-09-24 08:45:57 -06001979 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001980 if (rb->task) {
1981 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001982 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06001983 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001984 rb->task = req->task;
1985 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001986 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001987 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001988
Jens Axboe51a4cc12020-08-10 10:55:56 -06001989 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001990 rb->reqs[rb->to_free++] = req;
1991 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1992 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001993}
1994
Jens Axboeba816ad2019-09-28 11:36:45 -06001995/*
1996 * Drop reference to request, return next in chain (if there is one) if this
1997 * was the last reference to this request.
1998 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001999static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002000{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002001 struct io_kiocb *nxt = NULL;
2002
Jens Axboe2a44f462020-02-25 13:25:41 -07002003 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002004 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002005 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002006 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002007 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002008}
2009
Jens Axboe2b188cc2019-01-07 10:46:33 -07002010static void io_put_req(struct io_kiocb *req)
2011{
Jens Axboedef596e2019-01-09 08:59:42 -07002012 if (refcount_dec_and_test(&req->refs))
2013 io_free_req(req);
2014}
2015
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002016static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002017{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002018 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002019
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002020 /*
2021 * A ref is owned by io-wq in which context we're. So, if that's the
2022 * last one, it's safe to steal next work. False negatives are Ok,
2023 * it just will be re-punted async in io_put_work()
2024 */
2025 if (refcount_read(&req->refs) != 1)
2026 return NULL;
2027
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002028 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002029 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002030}
2031
Jens Axboe978db572019-11-14 22:39:04 -07002032/*
2033 * Must only be used if we don't need to care about links, usually from
2034 * within the completion handling itself.
2035 */
2036static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002037{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002038 /* drop both submit and complete references */
2039 if (refcount_sub_and_test(2, &req->refs))
2040 __io_free_req(req);
2041}
2042
Jens Axboe978db572019-11-14 22:39:04 -07002043static void io_double_put_req(struct io_kiocb *req)
2044{
2045 /* drop both submit and complete references */
2046 if (refcount_sub_and_test(2, &req->refs))
2047 io_free_req(req);
2048}
2049
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002050static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002051{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002052 struct io_rings *rings = ctx->rings;
2053
Jens Axboead3eb2c2019-12-18 17:12:20 -07002054 if (test_bit(0, &ctx->cq_check_overflow)) {
2055 /*
2056 * noflush == true is from the waitqueue handler, just ensure
2057 * we wake up the task, and the next invocation will flush the
2058 * entries. We cannot safely to it from here.
2059 */
2060 if (noflush && !list_empty(&ctx->cq_overflow_list))
2061 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002062
Jens Axboee6c8aa92020-09-28 13:10:13 -06002063 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002064 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002065
Jens Axboea3a0e432019-08-20 11:03:11 -06002066 /* See comment at the top of this file */
2067 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002068 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002069}
2070
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002071static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2072{
2073 struct io_rings *rings = ctx->rings;
2074
2075 /* make sure SQ entry isn't read before tail */
2076 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2077}
2078
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002079static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002080{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002081 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002082
Jens Axboebcda7ba2020-02-23 16:42:51 -07002083 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2084 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002085 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002086 kfree(kbuf);
2087 return cflags;
2088}
2089
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002090static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2091{
2092 struct io_buffer *kbuf;
2093
2094 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2095 return io_put_kbuf(req, kbuf);
2096}
2097
Jens Axboe4c6e2772020-07-01 11:29:10 -06002098static inline bool io_run_task_work(void)
2099{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002100 /*
2101 * Not safe to run on exiting task, and the task_work handling will
2102 * not add work to such a task.
2103 */
2104 if (unlikely(current->flags & PF_EXITING))
2105 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002106 if (current->task_works) {
2107 __set_current_state(TASK_RUNNING);
2108 task_work_run();
2109 return true;
2110 }
2111
2112 return false;
2113}
2114
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002115static void io_iopoll_queue(struct list_head *again)
2116{
2117 struct io_kiocb *req;
2118
2119 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002120 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2121 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002122 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002123 } while (!list_empty(again));
2124}
2125
Jens Axboedef596e2019-01-09 08:59:42 -07002126/*
2127 * Find and free completed poll iocbs
2128 */
2129static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2130 struct list_head *done)
2131{
Jens Axboe8237e042019-12-28 10:48:22 -07002132 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002133 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002134 LIST_HEAD(again);
2135
2136 /* order with ->result store in io_complete_rw_iopoll() */
2137 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002138
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002139 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002140 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002141 int cflags = 0;
2142
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002143 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002144 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002145 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002146 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002147 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002148 continue;
2149 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002150 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002151
Jens Axboebcda7ba2020-02-23 16:42:51 -07002152 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002153 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002154
2155 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002156 (*nr_events)++;
2157
Pavel Begunkovc3524382020-06-28 12:52:32 +03002158 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002159 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002160 }
Jens Axboedef596e2019-01-09 08:59:42 -07002161
Jens Axboe09bb8392019-03-13 12:39:28 -06002162 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002163 if (ctx->flags & IORING_SETUP_SQPOLL)
2164 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002165 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002166
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002167 if (!list_empty(&again))
2168 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002169}
2170
Jens Axboedef596e2019-01-09 08:59:42 -07002171static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2172 long min)
2173{
2174 struct io_kiocb *req, *tmp;
2175 LIST_HEAD(done);
2176 bool spin;
2177 int ret;
2178
2179 /*
2180 * Only spin for completions if we don't have multiple devices hanging
2181 * off our complete list, and we're under the requested amount.
2182 */
2183 spin = !ctx->poll_multi_file && *nr_events < min;
2184
2185 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002186 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002187 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002188
2189 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002190 * Move completed and retryable entries to our local lists.
2191 * If we find a request that requires polling, break out
2192 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002193 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002194 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002195 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002196 continue;
2197 }
2198 if (!list_empty(&done))
2199 break;
2200
2201 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2202 if (ret < 0)
2203 break;
2204
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002205 /* iopoll may have completed current req */
2206 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002207 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002208
Jens Axboedef596e2019-01-09 08:59:42 -07002209 if (ret && spin)
2210 spin = false;
2211 ret = 0;
2212 }
2213
2214 if (!list_empty(&done))
2215 io_iopoll_complete(ctx, nr_events, &done);
2216
2217 return ret;
2218}
2219
2220/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002221 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002222 * non-spinning poll check - we'll still enter the driver poll loop, but only
2223 * as a non-spinning completion check.
2224 */
2225static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2226 long min)
2227{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002228 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002229 int ret;
2230
2231 ret = io_do_iopoll(ctx, nr_events, min);
2232 if (ret < 0)
2233 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002234 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002235 return 0;
2236 }
2237
2238 return 1;
2239}
2240
2241/*
2242 * We can't just wait for polled events to come to us, we have to actively
2243 * find and complete them.
2244 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002245static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002246{
2247 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2248 return;
2249
2250 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002251 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002252 unsigned int nr_events = 0;
2253
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002254 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002255
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002256 /* let it sleep and repeat later if can't complete a request */
2257 if (nr_events == 0)
2258 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002259 /*
2260 * Ensure we allow local-to-the-cpu processing to take place,
2261 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002262 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002263 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002264 if (need_resched()) {
2265 mutex_unlock(&ctx->uring_lock);
2266 cond_resched();
2267 mutex_lock(&ctx->uring_lock);
2268 }
Jens Axboedef596e2019-01-09 08:59:42 -07002269 }
2270 mutex_unlock(&ctx->uring_lock);
2271}
2272
Pavel Begunkov7668b922020-07-07 16:36:21 +03002273static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002274{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002275 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002276 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002277
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002278 /*
2279 * We disallow the app entering submit/complete with polling, but we
2280 * still need to lock the ring to prevent racing with polled issue
2281 * that got punted to a workqueue.
2282 */
2283 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002284 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002285 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002286 * Don't enter poll loop if we already have events pending.
2287 * If we do, we can potentially be spinning for commands that
2288 * already triggered a CQE (eg in error).
2289 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002290 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002291 break;
2292
2293 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002294 * If a submit got punted to a workqueue, we can have the
2295 * application entering polling for a command before it gets
2296 * issued. That app will hold the uring_lock for the duration
2297 * of the poll right here, so we need to take a breather every
2298 * now and then to ensure that the issue has a chance to add
2299 * the poll to the issued list. Otherwise we can spin here
2300 * forever, while the workqueue is stuck trying to acquire the
2301 * very same mutex.
2302 */
2303 if (!(++iters & 7)) {
2304 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002305 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002306 mutex_lock(&ctx->uring_lock);
2307 }
2308
Pavel Begunkov7668b922020-07-07 16:36:21 +03002309 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002310 if (ret <= 0)
2311 break;
2312 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002313 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002314
Jens Axboe500f9fb2019-08-19 12:15:59 -06002315 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002316 return ret;
2317}
2318
Jens Axboe491381ce2019-10-17 09:20:46 -06002319static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002320{
Jens Axboe491381ce2019-10-17 09:20:46 -06002321 /*
2322 * Tell lockdep we inherited freeze protection from submission
2323 * thread.
2324 */
2325 if (req->flags & REQ_F_ISREG) {
2326 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002327
Jens Axboe491381ce2019-10-17 09:20:46 -06002328 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002329 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002330 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002331}
2332
Jens Axboea1d7c392020-06-22 11:09:46 -06002333static void io_complete_rw_common(struct kiocb *kiocb, long res,
2334 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002335{
Jens Axboe9adbd452019-12-20 08:45:55 -07002336 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002337 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002338
Jens Axboe491381ce2019-10-17 09:20:46 -06002339 if (kiocb->ki_flags & IOCB_WRITE)
2340 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002341
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002342 if (res != req->result)
2343 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002344 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002345 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002346 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002347}
2348
Jens Axboeb63534c2020-06-04 11:28:00 -06002349#ifdef CONFIG_BLOCK
2350static bool io_resubmit_prep(struct io_kiocb *req, int error)
2351{
2352 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2353 ssize_t ret = -ECANCELED;
2354 struct iov_iter iter;
2355 int rw;
2356
2357 if (error) {
2358 ret = error;
2359 goto end_req;
2360 }
2361
2362 switch (req->opcode) {
2363 case IORING_OP_READV:
2364 case IORING_OP_READ_FIXED:
2365 case IORING_OP_READ:
2366 rw = READ;
2367 break;
2368 case IORING_OP_WRITEV:
2369 case IORING_OP_WRITE_FIXED:
2370 case IORING_OP_WRITE:
2371 rw = WRITE;
2372 break;
2373 default:
2374 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2375 req->opcode);
2376 goto end_req;
2377 }
2378
Jens Axboee8c2bc12020-08-15 18:44:09 -07002379 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002380 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2381 if (ret < 0)
2382 goto end_req;
2383 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2384 if (!ret)
2385 return true;
2386 kfree(iovec);
2387 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002388 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002389 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002390end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002391 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002392 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002393 return false;
2394}
Jens Axboeb63534c2020-06-04 11:28:00 -06002395#endif
2396
2397static bool io_rw_reissue(struct io_kiocb *req, long res)
2398{
2399#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002400 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002401 int ret;
2402
Jens Axboe355afae2020-09-02 09:30:31 -06002403 if (!S_ISBLK(mode) && !S_ISREG(mode))
2404 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002405 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2406 return false;
2407
Jens Axboefdee9462020-08-27 16:46:24 -06002408 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002409
Jens Axboefdee9462020-08-27 16:46:24 -06002410 if (io_resubmit_prep(req, ret)) {
2411 refcount_inc(&req->refs);
2412 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002413 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002414 }
2415
Jens Axboeb63534c2020-06-04 11:28:00 -06002416#endif
2417 return false;
2418}
2419
Jens Axboea1d7c392020-06-22 11:09:46 -06002420static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2421 struct io_comp_state *cs)
2422{
2423 if (!io_rw_reissue(req, res))
2424 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002425}
2426
2427static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2428{
Jens Axboe9adbd452019-12-20 08:45:55 -07002429 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002430
Jens Axboea1d7c392020-06-22 11:09:46 -06002431 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002432}
2433
Jens Axboedef596e2019-01-09 08:59:42 -07002434static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2435{
Jens Axboe9adbd452019-12-20 08:45:55 -07002436 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002437
Jens Axboe491381ce2019-10-17 09:20:46 -06002438 if (kiocb->ki_flags & IOCB_WRITE)
2439 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002440
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002441 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002442 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002443
2444 WRITE_ONCE(req->result, res);
2445 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002446 smp_wmb();
2447 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002448}
2449
2450/*
2451 * After the iocb has been issued, it's safe to be found on the poll list.
2452 * Adding the kiocb to the list AFTER submission ensures that we don't
2453 * find it from a io_iopoll_getevents() thread before the issuer is done
2454 * accessing the kiocb cookie.
2455 */
2456static void io_iopoll_req_issued(struct io_kiocb *req)
2457{
2458 struct io_ring_ctx *ctx = req->ctx;
2459
2460 /*
2461 * Track whether we have multiple files in our lists. This will impact
2462 * how we do polling eventually, not spinning if we're on potentially
2463 * different devices.
2464 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002465 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002466 ctx->poll_multi_file = false;
2467 } else if (!ctx->poll_multi_file) {
2468 struct io_kiocb *list_req;
2469
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002470 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002471 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002472 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002473 ctx->poll_multi_file = true;
2474 }
2475
2476 /*
2477 * For fast devices, IO may have already completed. If it has, add
2478 * it to the front so we find it first.
2479 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002480 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002481 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002482 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002483 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002484
Jens Axboe534ca6d2020-09-02 13:52:19 -06002485 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2486 wq_has_sleeper(&ctx->sq_data->wait))
2487 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002488}
2489
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002490static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002491{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002492 if (state->has_refs)
2493 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002494 state->file = NULL;
2495}
2496
2497static inline void io_state_file_put(struct io_submit_state *state)
2498{
2499 if (state->file)
2500 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002501}
2502
2503/*
2504 * Get as many references to a file as we have IOs left in this submission,
2505 * assuming most submissions are for one file, or at least that each file
2506 * has more than one submission.
2507 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002508static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002509{
2510 if (!state)
2511 return fget(fd);
2512
2513 if (state->file) {
2514 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002515 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002516 state->ios_left--;
2517 return state->file;
2518 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002519 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002520 }
2521 state->file = fget_many(fd, state->ios_left);
2522 if (!state->file)
2523 return NULL;
2524
2525 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002526 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002527 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002528 return state->file;
2529}
2530
Jens Axboe4503b762020-06-01 10:00:27 -06002531static bool io_bdev_nowait(struct block_device *bdev)
2532{
2533#ifdef CONFIG_BLOCK
2534 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2535#else
2536 return true;
2537#endif
2538}
2539
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540/*
2541 * If we tracked the file through the SCM inflight mechanism, we could support
2542 * any file. For now, just ensure that anything potentially problematic is done
2543 * inline.
2544 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002545static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002546{
2547 umode_t mode = file_inode(file)->i_mode;
2548
Jens Axboe4503b762020-06-01 10:00:27 -06002549 if (S_ISBLK(mode)) {
2550 if (io_bdev_nowait(file->f_inode->i_bdev))
2551 return true;
2552 return false;
2553 }
2554 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002555 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002556 if (S_ISREG(mode)) {
2557 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2558 file->f_op != &io_uring_fops)
2559 return true;
2560 return false;
2561 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002562
Jens Axboec5b85622020-06-09 19:23:05 -06002563 /* any ->read/write should understand O_NONBLOCK */
2564 if (file->f_flags & O_NONBLOCK)
2565 return true;
2566
Jens Axboeaf197f52020-04-28 13:15:06 -06002567 if (!(file->f_mode & FMODE_NOWAIT))
2568 return false;
2569
2570 if (rw == READ)
2571 return file->f_op->read_iter != NULL;
2572
2573 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002574}
2575
Jens Axboe3529d8c2019-12-19 18:24:38 -07002576static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2577 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002578{
Jens Axboedef596e2019-01-09 08:59:42 -07002579 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002580 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002581 unsigned ioprio;
2582 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002583
Jens Axboe491381ce2019-10-17 09:20:46 -06002584 if (S_ISREG(file_inode(req->file)->i_mode))
2585 req->flags |= REQ_F_ISREG;
2586
Jens Axboe2b188cc2019-01-07 10:46:33 -07002587 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002588 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2589 req->flags |= REQ_F_CUR_POS;
2590 kiocb->ki_pos = req->file->f_pos;
2591 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002592 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002593 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2594 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2595 if (unlikely(ret))
2596 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002597
2598 ioprio = READ_ONCE(sqe->ioprio);
2599 if (ioprio) {
2600 ret = ioprio_check_cap(ioprio);
2601 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002602 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002603
2604 kiocb->ki_ioprio = ioprio;
2605 } else
2606 kiocb->ki_ioprio = get_current_ioprio();
2607
Stefan Bühler8449eed2019-04-27 20:34:19 +02002608 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002609 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002610 req->flags |= REQ_F_NOWAIT;
2611
2612 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002613 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002614
Jens Axboedef596e2019-01-09 08:59:42 -07002615 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002616 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2617 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002618 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002619
Jens Axboedef596e2019-01-09 08:59:42 -07002620 kiocb->ki_flags |= IOCB_HIPRI;
2621 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002622 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002623 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002624 if (kiocb->ki_flags & IOCB_HIPRI)
2625 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002626 kiocb->ki_complete = io_complete_rw;
2627 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002628
Jens Axboe3529d8c2019-12-19 18:24:38 -07002629 req->rw.addr = READ_ONCE(sqe->addr);
2630 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002631 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002632 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002633}
2634
2635static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2636{
2637 switch (ret) {
2638 case -EIOCBQUEUED:
2639 break;
2640 case -ERESTARTSYS:
2641 case -ERESTARTNOINTR:
2642 case -ERESTARTNOHAND:
2643 case -ERESTART_RESTARTBLOCK:
2644 /*
2645 * We can't just restart the syscall, since previously
2646 * submitted sqes may already be in progress. Just fail this
2647 * IO with EINTR.
2648 */
2649 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002650 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002651 default:
2652 kiocb->ki_complete(kiocb, ret, 0);
2653 }
2654}
2655
Jens Axboea1d7c392020-06-22 11:09:46 -06002656static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2657 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002658{
Jens Axboeba042912019-12-25 16:33:42 -07002659 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002660 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002661
Jens Axboe227c0c92020-08-13 11:51:40 -06002662 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002663 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002664 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002665 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002666 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002667 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002668 }
2669
Jens Axboeba042912019-12-25 16:33:42 -07002670 if (req->flags & REQ_F_CUR_POS)
2671 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002672 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002673 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002674 else
2675 io_rw_done(kiocb, ret);
2676}
2677
Jens Axboe9adbd452019-12-20 08:45:55 -07002678static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002679 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002680{
Jens Axboe9adbd452019-12-20 08:45:55 -07002681 struct io_ring_ctx *ctx = req->ctx;
2682 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002683 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002684 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002685 size_t offset;
2686 u64 buf_addr;
2687
Jens Axboeedafcce2019-01-09 09:16:05 -07002688 if (unlikely(buf_index >= ctx->nr_user_bufs))
2689 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002690 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2691 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002692 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002693
2694 /* overflow */
2695 if (buf_addr + len < buf_addr)
2696 return -EFAULT;
2697 /* not inside the mapped region */
2698 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2699 return -EFAULT;
2700
2701 /*
2702 * May not be a start of buffer, set size appropriately
2703 * and advance us to the beginning.
2704 */
2705 offset = buf_addr - imu->ubuf;
2706 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002707
2708 if (offset) {
2709 /*
2710 * Don't use iov_iter_advance() here, as it's really slow for
2711 * using the latter parts of a big fixed buffer - it iterates
2712 * over each segment manually. We can cheat a bit here, because
2713 * we know that:
2714 *
2715 * 1) it's a BVEC iter, we set it up
2716 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2717 * first and last bvec
2718 *
2719 * So just find our index, and adjust the iterator afterwards.
2720 * If the offset is within the first bvec (or the whole first
2721 * bvec, just use iov_iter_advance(). This makes it easier
2722 * since we can just skip the first segment, which may not
2723 * be PAGE_SIZE aligned.
2724 */
2725 const struct bio_vec *bvec = imu->bvec;
2726
2727 if (offset <= bvec->bv_len) {
2728 iov_iter_advance(iter, offset);
2729 } else {
2730 unsigned long seg_skip;
2731
2732 /* skip first vec */
2733 offset -= bvec->bv_len;
2734 seg_skip = 1 + (offset >> PAGE_SHIFT);
2735
2736 iter->bvec = bvec + seg_skip;
2737 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002738 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002739 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002740 }
2741 }
2742
Jens Axboe5e559562019-11-13 16:12:46 -07002743 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002744}
2745
Jens Axboebcda7ba2020-02-23 16:42:51 -07002746static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2747{
2748 if (needs_lock)
2749 mutex_unlock(&ctx->uring_lock);
2750}
2751
2752static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2753{
2754 /*
2755 * "Normal" inline submissions always hold the uring_lock, since we
2756 * grab it from the system call. Same is true for the SQPOLL offload.
2757 * The only exception is when we've detached the request and issue it
2758 * from an async worker thread, grab the lock for that case.
2759 */
2760 if (needs_lock)
2761 mutex_lock(&ctx->uring_lock);
2762}
2763
2764static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2765 int bgid, struct io_buffer *kbuf,
2766 bool needs_lock)
2767{
2768 struct io_buffer *head;
2769
2770 if (req->flags & REQ_F_BUFFER_SELECTED)
2771 return kbuf;
2772
2773 io_ring_submit_lock(req->ctx, needs_lock);
2774
2775 lockdep_assert_held(&req->ctx->uring_lock);
2776
2777 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2778 if (head) {
2779 if (!list_empty(&head->list)) {
2780 kbuf = list_last_entry(&head->list, struct io_buffer,
2781 list);
2782 list_del(&kbuf->list);
2783 } else {
2784 kbuf = head;
2785 idr_remove(&req->ctx->io_buffer_idr, bgid);
2786 }
2787 if (*len > kbuf->len)
2788 *len = kbuf->len;
2789 } else {
2790 kbuf = ERR_PTR(-ENOBUFS);
2791 }
2792
2793 io_ring_submit_unlock(req->ctx, needs_lock);
2794
2795 return kbuf;
2796}
2797
Jens Axboe4d954c22020-02-27 07:31:19 -07002798static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2799 bool needs_lock)
2800{
2801 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002802 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002803
2804 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002805 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002806 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2807 if (IS_ERR(kbuf))
2808 return kbuf;
2809 req->rw.addr = (u64) (unsigned long) kbuf;
2810 req->flags |= REQ_F_BUFFER_SELECTED;
2811 return u64_to_user_ptr(kbuf->addr);
2812}
2813
2814#ifdef CONFIG_COMPAT
2815static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2816 bool needs_lock)
2817{
2818 struct compat_iovec __user *uiov;
2819 compat_ssize_t clen;
2820 void __user *buf;
2821 ssize_t len;
2822
2823 uiov = u64_to_user_ptr(req->rw.addr);
2824 if (!access_ok(uiov, sizeof(*uiov)))
2825 return -EFAULT;
2826 if (__get_user(clen, &uiov->iov_len))
2827 return -EFAULT;
2828 if (clen < 0)
2829 return -EINVAL;
2830
2831 len = clen;
2832 buf = io_rw_buffer_select(req, &len, needs_lock);
2833 if (IS_ERR(buf))
2834 return PTR_ERR(buf);
2835 iov[0].iov_base = buf;
2836 iov[0].iov_len = (compat_size_t) len;
2837 return 0;
2838}
2839#endif
2840
2841static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2842 bool needs_lock)
2843{
2844 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2845 void __user *buf;
2846 ssize_t len;
2847
2848 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2849 return -EFAULT;
2850
2851 len = iov[0].iov_len;
2852 if (len < 0)
2853 return -EINVAL;
2854 buf = io_rw_buffer_select(req, &len, needs_lock);
2855 if (IS_ERR(buf))
2856 return PTR_ERR(buf);
2857 iov[0].iov_base = buf;
2858 iov[0].iov_len = len;
2859 return 0;
2860}
2861
2862static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2863 bool needs_lock)
2864{
Jens Axboedddb3e22020-06-04 11:27:01 -06002865 if (req->flags & REQ_F_BUFFER_SELECTED) {
2866 struct io_buffer *kbuf;
2867
2868 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2869 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2870 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002871 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002872 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002873 if (!req->rw.len)
2874 return 0;
2875 else if (req->rw.len > 1)
2876 return -EINVAL;
2877
2878#ifdef CONFIG_COMPAT
2879 if (req->ctx->compat)
2880 return io_compat_import(req, iov, needs_lock);
2881#endif
2882
2883 return __io_iov_buffer_select(req, iov, needs_lock);
2884}
2885
Jens Axboe8452fd02020-08-18 13:58:33 -07002886static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2887 struct iovec **iovec, struct iov_iter *iter,
2888 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002889{
Jens Axboe9adbd452019-12-20 08:45:55 -07002890 void __user *buf = u64_to_user_ptr(req->rw.addr);
2891 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002892 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002893 u8 opcode;
2894
Jens Axboed625c6e2019-12-17 19:53:05 -07002895 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002896 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002897 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002898 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002899 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002900
Jens Axboebcda7ba2020-02-23 16:42:51 -07002901 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002902 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002903 return -EINVAL;
2904
Jens Axboe3a6820f2019-12-22 15:19:35 -07002905 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002906 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002907 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002908 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002909 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002910 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002911 }
2912
Jens Axboe3a6820f2019-12-22 15:19:35 -07002913 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2914 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002915 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002916 }
2917
Jens Axboe4d954c22020-02-27 07:31:19 -07002918 if (req->flags & REQ_F_BUFFER_SELECT) {
2919 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002920 if (!ret) {
2921 ret = (*iovec)->iov_len;
2922 iov_iter_init(iter, rw, *iovec, 1, ret);
2923 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002924 *iovec = NULL;
2925 return ret;
2926 }
2927
Jens Axboe2b188cc2019-01-07 10:46:33 -07002928#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002929 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002930 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2931 iovec, iter);
2932#endif
2933
2934 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2935}
2936
Jens Axboe8452fd02020-08-18 13:58:33 -07002937static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2938 struct iovec **iovec, struct iov_iter *iter,
2939 bool needs_lock)
2940{
Jens Axboee8c2bc12020-08-15 18:44:09 -07002941 struct io_async_rw *iorw = req->async_data;
2942
2943 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07002944 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2945 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07002946 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07002947}
2948
Jens Axboe0fef9482020-08-26 10:36:20 -06002949static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2950{
2951 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2952}
2953
Jens Axboe32960612019-09-23 11:05:34 -06002954/*
2955 * For files that don't have ->read_iter() and ->write_iter(), handle them
2956 * by looping over ->read() or ->write() manually.
2957 */
2958static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2959 struct iov_iter *iter)
2960{
2961 ssize_t ret = 0;
2962
2963 /*
2964 * Don't support polled IO through this interface, and we can't
2965 * support non-blocking either. For the latter, this just causes
2966 * the kiocb to be handled from an async context.
2967 */
2968 if (kiocb->ki_flags & IOCB_HIPRI)
2969 return -EOPNOTSUPP;
2970 if (kiocb->ki_flags & IOCB_NOWAIT)
2971 return -EAGAIN;
2972
2973 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002974 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002975 ssize_t nr;
2976
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002977 if (!iov_iter_is_bvec(iter)) {
2978 iovec = iov_iter_iovec(iter);
2979 } else {
2980 /* fixed buffers import bvec */
2981 iovec.iov_base = kmap(iter->bvec->bv_page)
2982 + iter->iov_offset;
2983 iovec.iov_len = min(iter->count,
2984 iter->bvec->bv_len - iter->iov_offset);
2985 }
2986
Jens Axboe32960612019-09-23 11:05:34 -06002987 if (rw == READ) {
2988 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002989 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002990 } else {
2991 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002992 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002993 }
2994
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002995 if (iov_iter_is_bvec(iter))
2996 kunmap(iter->bvec->bv_page);
2997
Jens Axboe32960612019-09-23 11:05:34 -06002998 if (nr < 0) {
2999 if (!ret)
3000 ret = nr;
3001 break;
3002 }
3003 ret += nr;
3004 if (nr != iovec.iov_len)
3005 break;
3006 iov_iter_advance(iter, nr);
3007 }
3008
3009 return ret;
3010}
3011
Jens Axboeff6165b2020-08-13 09:47:43 -06003012static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3013 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003014{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003015 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003016
Jens Axboeff6165b2020-08-13 09:47:43 -06003017 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003018 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003019 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003020 /* can only be fixed buffers, no need to do anything */
3021 if (iter->type == ITER_BVEC)
3022 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003023 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003024 unsigned iov_off = 0;
3025
3026 rw->iter.iov = rw->fast_iov;
3027 if (iter->iov != fast_iov) {
3028 iov_off = iter->iov - fast_iov;
3029 rw->iter.iov += iov_off;
3030 }
3031 if (rw->fast_iov != fast_iov)
3032 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003033 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003034 } else {
3035 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003036 }
3037}
3038
Jens Axboee8c2bc12020-08-15 18:44:09 -07003039static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003040{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003041 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3042 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3043 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003044}
3045
Jens Axboee8c2bc12020-08-15 18:44:09 -07003046static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003047{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003048 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003049 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003050
Jens Axboee8c2bc12020-08-15 18:44:09 -07003051 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003052}
3053
Jens Axboeff6165b2020-08-13 09:47:43 -06003054static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3055 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003056 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003057{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003058 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003059 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003060 if (!req->async_data) {
3061 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003062 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003063
Jens Axboeff6165b2020-08-13 09:47:43 -06003064 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003065 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003066 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003067}
3068
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003069static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3070 bool force_nonblock)
3071{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003072 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003073 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003074 ssize_t ret;
3075
Jens Axboec183edf2020-09-04 22:36:52 -06003076 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003077 if (unlikely(ret < 0))
3078 return ret;
3079
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003080 iorw->bytes_done = 0;
3081 iorw->free_iovec = iov;
3082 if (iov)
3083 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003084 return 0;
3085}
3086
Jens Axboe3529d8c2019-12-19 18:24:38 -07003087static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3088 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003089{
3090 ssize_t ret;
3091
Jens Axboe3529d8c2019-12-19 18:24:38 -07003092 ret = io_prep_rw(req, sqe, force_nonblock);
3093 if (ret)
3094 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003095
Jens Axboe3529d8c2019-12-19 18:24:38 -07003096 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3097 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003098
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003099 /* either don't need iovec imported or already have it */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003100 if (!req->async_data || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003101 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003102 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003103}
3104
Jens Axboec1dd91d2020-08-03 16:43:59 -06003105/*
3106 * This is our waitqueue callback handler, registered through lock_page_async()
3107 * when we initially tried to do the IO with the iocb armed our waitqueue.
3108 * This gets called when the page is unlocked, and we generally expect that to
3109 * happen when the page IO is completed and the page is now uptodate. This will
3110 * queue a task_work based retry of the operation, attempting to copy the data
3111 * again. If the latter fails because the page was NOT uptodate, then we will
3112 * do a thread based blocking retry of the operation. That's the unexpected
3113 * slow path.
3114 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003115static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3116 int sync, void *arg)
3117{
3118 struct wait_page_queue *wpq;
3119 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003120 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003121 int ret;
3122
3123 wpq = container_of(wait, struct wait_page_queue, wait);
3124
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003125 if (!wake_page_match(wpq, key))
3126 return 0;
3127
Hao Xuc8d317a2020-09-29 20:00:45 +08003128 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003129 list_del_init(&wait->entry);
3130
Pavel Begunkove7375122020-07-12 20:42:04 +03003131 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003132 percpu_ref_get(&req->ctx->refs);
3133
Jens Axboebcf5a062020-05-22 09:24:42 -06003134 /* submit ref gets dropped, acquire a new one */
3135 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003136 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003137 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003138 struct task_struct *tsk;
3139
Jens Axboebcf5a062020-05-22 09:24:42 -06003140 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003141 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003142 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003143 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003144 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003145 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003146 return 1;
3147}
3148
Jens Axboec1dd91d2020-08-03 16:43:59 -06003149/*
3150 * This controls whether a given IO request should be armed for async page
3151 * based retry. If we return false here, the request is handed to the async
3152 * worker threads for retry. If we're doing buffered reads on a regular file,
3153 * we prepare a private wait_page_queue entry and retry the operation. This
3154 * will either succeed because the page is now uptodate and unlocked, or it
3155 * will register a callback when the page is unlocked at IO completion. Through
3156 * that callback, io_uring uses task_work to setup a retry of the operation.
3157 * That retry will attempt the buffered read again. The retry will generally
3158 * succeed, or in rare cases where it fails, we then fall back to using the
3159 * async worker threads for a blocking retry.
3160 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003161static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003162{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003163 struct io_async_rw *rw = req->async_data;
3164 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003165 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003166
3167 /* never retry for NOWAIT, we just complete with -EAGAIN */
3168 if (req->flags & REQ_F_NOWAIT)
3169 return false;
3170
Jens Axboe227c0c92020-08-13 11:51:40 -06003171 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003172 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003173 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003174
Jens Axboebcf5a062020-05-22 09:24:42 -06003175 /*
3176 * just use poll if we can, and don't attempt if the fs doesn't
3177 * support callback based unlocks
3178 */
3179 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3180 return false;
3181
Jens Axboe3b2a4432020-08-16 10:58:43 -07003182 wait->wait.func = io_async_buf_func;
3183 wait->wait.private = req;
3184 wait->wait.flags = 0;
3185 INIT_LIST_HEAD(&wait->wait.entry);
3186 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003187 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003188 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003189 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003190}
3191
3192static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3193{
3194 if (req->file->f_op->read_iter)
3195 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003196 else if (req->file->f_op->read)
3197 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3198 else
3199 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003200}
3201
Jens Axboea1d7c392020-06-22 11:09:46 -06003202static int io_read(struct io_kiocb *req, bool force_nonblock,
3203 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003204{
3205 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003206 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003207 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003208 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003209 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003210 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003211 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003212
Jens Axboee8c2bc12020-08-15 18:44:09 -07003213 if (rw)
3214 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003215
3216 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003217 if (ret < 0)
3218 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003219 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003220 io_size = ret;
3221 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003222 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003223
Jens Axboefd6c2e42019-12-18 12:19:41 -07003224 /* Ensure we clear previously set non-block flag */
3225 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003226 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003227
Pavel Begunkov24c74672020-06-21 13:09:51 +03003228 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003229 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3230 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003231 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003232
Jens Axboe0fef9482020-08-26 10:36:20 -06003233 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003234 if (unlikely(ret))
3235 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003236
Jens Axboe227c0c92020-08-13 11:51:40 -06003237 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003238
Jens Axboe227c0c92020-08-13 11:51:40 -06003239 if (!ret) {
3240 goto done;
3241 } else if (ret == -EIOCBQUEUED) {
3242 ret = 0;
3243 goto out_free;
3244 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003245 /* IOPOLL retry should happen for io-wq threads */
3246 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003247 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003248 /* no retry on NONBLOCK marked file */
3249 if (req->file->f_flags & O_NONBLOCK)
3250 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003251 /* some cases will consume bytes even on error returns */
3252 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003253 ret = 0;
3254 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003255 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003256 /* make sure -ERESTARTSYS -> -EINTR is done */
3257 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003258 }
3259
3260 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003261 if (!iov_iter_count(iter) || !force_nonblock ||
3262 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003263 goto done;
3264
3265 io_size -= ret;
3266copy_iov:
3267 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3268 if (ret2) {
3269 ret = ret2;
3270 goto out_free;
3271 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003272 if (no_async)
3273 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003274 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003275 /* it's copied and will be cleaned with ->io */
3276 iovec = NULL;
3277 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003278 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003279retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003280 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003281 /* if we can retry, do so with the callbacks armed */
3282 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003283 kiocb->ki_flags &= ~IOCB_WAITQ;
3284 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003285 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003286
3287 /*
3288 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3289 * get -EIOCBQUEUED, then we'll get a notification when the desired
3290 * page gets unlocked. We can also get a partial read here, and if we
3291 * do, then just retry at the new offset.
3292 */
3293 ret = io_iter_do_read(req, iter);
3294 if (ret == -EIOCBQUEUED) {
3295 ret = 0;
3296 goto out_free;
3297 } else if (ret > 0 && ret < io_size) {
3298 /* we got some bytes, but not all. retry. */
3299 goto retry;
3300 }
3301done:
3302 kiocb_done(kiocb, ret, cs);
3303 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003304out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003305 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003306 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003307 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003308 return ret;
3309}
3310
Jens Axboe3529d8c2019-12-19 18:24:38 -07003311static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3312 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003313{
3314 ssize_t ret;
3315
Jens Axboe3529d8c2019-12-19 18:24:38 -07003316 ret = io_prep_rw(req, sqe, force_nonblock);
3317 if (ret)
3318 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003319
Jens Axboe3529d8c2019-12-19 18:24:38 -07003320 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3321 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003322
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003323 /* either don't need iovec imported or already have it */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003324 if (!req->async_data || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003325 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003326 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003327}
3328
Jens Axboea1d7c392020-06-22 11:09:46 -06003329static int io_write(struct io_kiocb *req, bool force_nonblock,
3330 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003331{
3332 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003333 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003334 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003335 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003336 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003337 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003338
Jens Axboee8c2bc12020-08-15 18:44:09 -07003339 if (rw)
3340 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003341
3342 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003343 if (ret < 0)
3344 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003345 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003346 io_size = ret;
3347 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003348
Jens Axboefd6c2e42019-12-18 12:19:41 -07003349 /* Ensure we clear previously set non-block flag */
3350 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003351 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003352
Pavel Begunkov24c74672020-06-21 13:09:51 +03003353 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003354 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003355 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003356
Jens Axboe10d59342019-12-09 20:16:22 -07003357 /* file path doesn't support NOWAIT for non-direct_IO */
3358 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3359 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003360 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003361
Jens Axboe0fef9482020-08-26 10:36:20 -06003362 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003363 if (unlikely(ret))
3364 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003365
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003366 /*
3367 * Open-code file_start_write here to grab freeze protection,
3368 * which will be released by another thread in
3369 * io_complete_rw(). Fool lockdep by telling it the lock got
3370 * released so that it doesn't complain about the held lock when
3371 * we return to userspace.
3372 */
3373 if (req->flags & REQ_F_ISREG) {
3374 __sb_start_write(file_inode(req->file)->i_sb,
3375 SB_FREEZE_WRITE, true);
3376 __sb_writers_release(file_inode(req->file)->i_sb,
3377 SB_FREEZE_WRITE);
3378 }
3379 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003380
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003381 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003382 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003383 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003384 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003385 else
3386 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003387
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003388 /*
3389 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3390 * retry them without IOCB_NOWAIT.
3391 */
3392 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3393 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003394 /* no retry on NONBLOCK marked file */
3395 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3396 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003397 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003398 /* IOPOLL retry should happen for io-wq threads */
3399 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3400 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003401done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003402 kiocb_done(kiocb, ret2, cs);
3403 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003404copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003405 /* some cases will consume bytes even on error returns */
3406 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003407 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003408 if (!ret)
3409 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003410 }
Jens Axboe31b51512019-01-18 22:56:34 -07003411out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003412 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003413 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003414 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003415 return ret;
3416}
3417
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003418static int __io_splice_prep(struct io_kiocb *req,
3419 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003420{
3421 struct io_splice* sp = &req->splice;
3422 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3423 int ret;
3424
3425 if (req->flags & REQ_F_NEED_CLEANUP)
3426 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003427 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3428 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003429
3430 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003431 sp->len = READ_ONCE(sqe->len);
3432 sp->flags = READ_ONCE(sqe->splice_flags);
3433
3434 if (unlikely(sp->flags & ~valid_flags))
3435 return -EINVAL;
3436
3437 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3438 (sp->flags & SPLICE_F_FD_IN_FIXED));
3439 if (ret)
3440 return ret;
3441 req->flags |= REQ_F_NEED_CLEANUP;
3442
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003443 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3444 /*
3445 * Splice operation will be punted aync, and here need to
3446 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3447 */
3448 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003449 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003450 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003451
3452 return 0;
3453}
3454
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003455static int io_tee_prep(struct io_kiocb *req,
3456 const struct io_uring_sqe *sqe)
3457{
3458 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3459 return -EINVAL;
3460 return __io_splice_prep(req, sqe);
3461}
3462
3463static int io_tee(struct io_kiocb *req, bool force_nonblock)
3464{
3465 struct io_splice *sp = &req->splice;
3466 struct file *in = sp->file_in;
3467 struct file *out = sp->file_out;
3468 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3469 long ret = 0;
3470
3471 if (force_nonblock)
3472 return -EAGAIN;
3473 if (sp->len)
3474 ret = do_tee(in, out, sp->len, flags);
3475
3476 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3477 req->flags &= ~REQ_F_NEED_CLEANUP;
3478
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003479 if (ret != sp->len)
3480 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003481 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003482 return 0;
3483}
3484
3485static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3486{
3487 struct io_splice* sp = &req->splice;
3488
3489 sp->off_in = READ_ONCE(sqe->splice_off_in);
3490 sp->off_out = READ_ONCE(sqe->off);
3491 return __io_splice_prep(req, sqe);
3492}
3493
Pavel Begunkov014db002020-03-03 21:33:12 +03003494static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003495{
3496 struct io_splice *sp = &req->splice;
3497 struct file *in = sp->file_in;
3498 struct file *out = sp->file_out;
3499 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3500 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003501 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003502
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003503 if (force_nonblock)
3504 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003505
3506 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3507 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003508
Jens Axboe948a7742020-05-17 14:21:38 -06003509 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003510 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003511
3512 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3513 req->flags &= ~REQ_F_NEED_CLEANUP;
3514
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003515 if (ret != sp->len)
3516 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003517 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003518 return 0;
3519}
3520
Jens Axboe2b188cc2019-01-07 10:46:33 -07003521/*
3522 * IORING_OP_NOP just posts a completion event, nothing else.
3523 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003524static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003525{
3526 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003527
Jens Axboedef596e2019-01-09 08:59:42 -07003528 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3529 return -EINVAL;
3530
Jens Axboe229a7b62020-06-22 10:13:11 -06003531 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003532 return 0;
3533}
3534
Jens Axboe3529d8c2019-12-19 18:24:38 -07003535static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003536{
Jens Axboe6b063142019-01-10 22:13:58 -07003537 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003538
Jens Axboe09bb8392019-03-13 12:39:28 -06003539 if (!req->file)
3540 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003541
Jens Axboe6b063142019-01-10 22:13:58 -07003542 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003543 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003544 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003545 return -EINVAL;
3546
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003547 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3548 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3549 return -EINVAL;
3550
3551 req->sync.off = READ_ONCE(sqe->off);
3552 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003553 return 0;
3554}
3555
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003556static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003557{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003558 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003559 int ret;
3560
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003561 /* fsync always requires a blocking context */
3562 if (force_nonblock)
3563 return -EAGAIN;
3564
Jens Axboe9adbd452019-12-20 08:45:55 -07003565 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003566 end > 0 ? end : LLONG_MAX,
3567 req->sync.flags & IORING_FSYNC_DATASYNC);
3568 if (ret < 0)
3569 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003570 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003571 return 0;
3572}
3573
Jens Axboed63d1b52019-12-10 10:38:56 -07003574static int io_fallocate_prep(struct io_kiocb *req,
3575 const struct io_uring_sqe *sqe)
3576{
3577 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3578 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003579 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3580 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003581
3582 req->sync.off = READ_ONCE(sqe->off);
3583 req->sync.len = READ_ONCE(sqe->addr);
3584 req->sync.mode = READ_ONCE(sqe->len);
3585 return 0;
3586}
3587
Pavel Begunkov014db002020-03-03 21:33:12 +03003588static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003589{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003590 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003591
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003592 /* fallocate always requiring blocking context */
3593 if (force_nonblock)
3594 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003595 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3596 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003597 if (ret < 0)
3598 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003599 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003600 return 0;
3601}
3602
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003603static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003604{
Jens Axboef8748882020-01-08 17:47:02 -07003605 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003606 int ret;
3607
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003608 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003609 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003610 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003611 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003612
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003613 /* open.how should be already initialised */
3614 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003615 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003616
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003617 req->open.dfd = READ_ONCE(sqe->fd);
3618 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003619 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003620 if (IS_ERR(req->open.filename)) {
3621 ret = PTR_ERR(req->open.filename);
3622 req->open.filename = NULL;
3623 return ret;
3624 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003625 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003626 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003627 return 0;
3628}
3629
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003630static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3631{
3632 u64 flags, mode;
3633
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003634 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3635 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003636 if (req->flags & REQ_F_NEED_CLEANUP)
3637 return 0;
3638 mode = READ_ONCE(sqe->len);
3639 flags = READ_ONCE(sqe->open_flags);
3640 req->open.how = build_open_how(flags, mode);
3641 return __io_openat_prep(req, sqe);
3642}
3643
Jens Axboecebdb982020-01-08 17:59:24 -07003644static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3645{
3646 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003647 size_t len;
3648 int ret;
3649
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003650 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3651 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003652 if (req->flags & REQ_F_NEED_CLEANUP)
3653 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003654 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3655 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003656 if (len < OPEN_HOW_SIZE_VER0)
3657 return -EINVAL;
3658
3659 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3660 len);
3661 if (ret)
3662 return ret;
3663
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003664 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003665}
3666
Pavel Begunkov014db002020-03-03 21:33:12 +03003667static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003668{
3669 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003670 struct file *file;
3671 int ret;
3672
Jens Axboef86cd202020-01-29 13:46:44 -07003673 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003674 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003675
Jens Axboecebdb982020-01-08 17:59:24 -07003676 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003677 if (ret)
3678 goto err;
3679
Jens Axboe4022e7a2020-03-19 19:23:18 -06003680 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003681 if (ret < 0)
3682 goto err;
3683
3684 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3685 if (IS_ERR(file)) {
3686 put_unused_fd(ret);
3687 ret = PTR_ERR(file);
3688 } else {
3689 fsnotify_open(file);
3690 fd_install(ret, file);
3691 }
3692err:
3693 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003694 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003695 if (ret < 0)
3696 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003697 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003698 return 0;
3699}
3700
Pavel Begunkov014db002020-03-03 21:33:12 +03003701static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003702{
Pavel Begunkov014db002020-03-03 21:33:12 +03003703 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003704}
3705
Jens Axboe067524e2020-03-02 16:32:28 -07003706static int io_remove_buffers_prep(struct io_kiocb *req,
3707 const struct io_uring_sqe *sqe)
3708{
3709 struct io_provide_buf *p = &req->pbuf;
3710 u64 tmp;
3711
3712 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3713 return -EINVAL;
3714
3715 tmp = READ_ONCE(sqe->fd);
3716 if (!tmp || tmp > USHRT_MAX)
3717 return -EINVAL;
3718
3719 memset(p, 0, sizeof(*p));
3720 p->nbufs = tmp;
3721 p->bgid = READ_ONCE(sqe->buf_group);
3722 return 0;
3723}
3724
3725static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3726 int bgid, unsigned nbufs)
3727{
3728 unsigned i = 0;
3729
3730 /* shouldn't happen */
3731 if (!nbufs)
3732 return 0;
3733
3734 /* the head kbuf is the list itself */
3735 while (!list_empty(&buf->list)) {
3736 struct io_buffer *nxt;
3737
3738 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3739 list_del(&nxt->list);
3740 kfree(nxt);
3741 if (++i == nbufs)
3742 return i;
3743 }
3744 i++;
3745 kfree(buf);
3746 idr_remove(&ctx->io_buffer_idr, bgid);
3747
3748 return i;
3749}
3750
Jens Axboe229a7b62020-06-22 10:13:11 -06003751static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3752 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003753{
3754 struct io_provide_buf *p = &req->pbuf;
3755 struct io_ring_ctx *ctx = req->ctx;
3756 struct io_buffer *head;
3757 int ret = 0;
3758
3759 io_ring_submit_lock(ctx, !force_nonblock);
3760
3761 lockdep_assert_held(&ctx->uring_lock);
3762
3763 ret = -ENOENT;
3764 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3765 if (head)
3766 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3767
3768 io_ring_submit_lock(ctx, !force_nonblock);
3769 if (ret < 0)
3770 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003771 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003772 return 0;
3773}
3774
Jens Axboeddf0322d2020-02-23 16:41:33 -07003775static int io_provide_buffers_prep(struct io_kiocb *req,
3776 const struct io_uring_sqe *sqe)
3777{
3778 struct io_provide_buf *p = &req->pbuf;
3779 u64 tmp;
3780
3781 if (sqe->ioprio || sqe->rw_flags)
3782 return -EINVAL;
3783
3784 tmp = READ_ONCE(sqe->fd);
3785 if (!tmp || tmp > USHRT_MAX)
3786 return -E2BIG;
3787 p->nbufs = tmp;
3788 p->addr = READ_ONCE(sqe->addr);
3789 p->len = READ_ONCE(sqe->len);
3790
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003791 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003792 return -EFAULT;
3793
3794 p->bgid = READ_ONCE(sqe->buf_group);
3795 tmp = READ_ONCE(sqe->off);
3796 if (tmp > USHRT_MAX)
3797 return -E2BIG;
3798 p->bid = tmp;
3799 return 0;
3800}
3801
3802static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3803{
3804 struct io_buffer *buf;
3805 u64 addr = pbuf->addr;
3806 int i, bid = pbuf->bid;
3807
3808 for (i = 0; i < pbuf->nbufs; i++) {
3809 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3810 if (!buf)
3811 break;
3812
3813 buf->addr = addr;
3814 buf->len = pbuf->len;
3815 buf->bid = bid;
3816 addr += pbuf->len;
3817 bid++;
3818 if (!*head) {
3819 INIT_LIST_HEAD(&buf->list);
3820 *head = buf;
3821 } else {
3822 list_add_tail(&buf->list, &(*head)->list);
3823 }
3824 }
3825
3826 return i ? i : -ENOMEM;
3827}
3828
Jens Axboe229a7b62020-06-22 10:13:11 -06003829static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3830 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003831{
3832 struct io_provide_buf *p = &req->pbuf;
3833 struct io_ring_ctx *ctx = req->ctx;
3834 struct io_buffer *head, *list;
3835 int ret = 0;
3836
3837 io_ring_submit_lock(ctx, !force_nonblock);
3838
3839 lockdep_assert_held(&ctx->uring_lock);
3840
3841 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3842
3843 ret = io_add_buffers(p, &head);
3844 if (ret < 0)
3845 goto out;
3846
3847 if (!list) {
3848 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3849 GFP_KERNEL);
3850 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003851 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003852 goto out;
3853 }
3854 }
3855out:
3856 io_ring_submit_unlock(ctx, !force_nonblock);
3857 if (ret < 0)
3858 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003859 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003860 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003861}
3862
Jens Axboe3e4827b2020-01-08 15:18:09 -07003863static int io_epoll_ctl_prep(struct io_kiocb *req,
3864 const struct io_uring_sqe *sqe)
3865{
3866#if defined(CONFIG_EPOLL)
3867 if (sqe->ioprio || sqe->buf_index)
3868 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003869 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003870 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003871
3872 req->epoll.epfd = READ_ONCE(sqe->fd);
3873 req->epoll.op = READ_ONCE(sqe->len);
3874 req->epoll.fd = READ_ONCE(sqe->off);
3875
3876 if (ep_op_has_event(req->epoll.op)) {
3877 struct epoll_event __user *ev;
3878
3879 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3880 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3881 return -EFAULT;
3882 }
3883
3884 return 0;
3885#else
3886 return -EOPNOTSUPP;
3887#endif
3888}
3889
Jens Axboe229a7b62020-06-22 10:13:11 -06003890static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3891 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003892{
3893#if defined(CONFIG_EPOLL)
3894 struct io_epoll *ie = &req->epoll;
3895 int ret;
3896
3897 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3898 if (force_nonblock && ret == -EAGAIN)
3899 return -EAGAIN;
3900
3901 if (ret < 0)
3902 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003903 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003904 return 0;
3905#else
3906 return -EOPNOTSUPP;
3907#endif
3908}
3909
Jens Axboec1ca7572019-12-25 22:18:28 -07003910static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3911{
3912#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3913 if (sqe->ioprio || sqe->buf_index || sqe->off)
3914 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003915 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3916 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003917
3918 req->madvise.addr = READ_ONCE(sqe->addr);
3919 req->madvise.len = READ_ONCE(sqe->len);
3920 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3921 return 0;
3922#else
3923 return -EOPNOTSUPP;
3924#endif
3925}
3926
Pavel Begunkov014db002020-03-03 21:33:12 +03003927static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003928{
3929#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3930 struct io_madvise *ma = &req->madvise;
3931 int ret;
3932
3933 if (force_nonblock)
3934 return -EAGAIN;
3935
3936 ret = do_madvise(ma->addr, ma->len, ma->advice);
3937 if (ret < 0)
3938 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003939 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003940 return 0;
3941#else
3942 return -EOPNOTSUPP;
3943#endif
3944}
3945
Jens Axboe4840e412019-12-25 22:03:45 -07003946static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3947{
3948 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3949 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003950 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3951 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003952
3953 req->fadvise.offset = READ_ONCE(sqe->off);
3954 req->fadvise.len = READ_ONCE(sqe->len);
3955 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3956 return 0;
3957}
3958
Pavel Begunkov014db002020-03-03 21:33:12 +03003959static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003960{
3961 struct io_fadvise *fa = &req->fadvise;
3962 int ret;
3963
Jens Axboe3e694262020-02-01 09:22:49 -07003964 if (force_nonblock) {
3965 switch (fa->advice) {
3966 case POSIX_FADV_NORMAL:
3967 case POSIX_FADV_RANDOM:
3968 case POSIX_FADV_SEQUENTIAL:
3969 break;
3970 default:
3971 return -EAGAIN;
3972 }
3973 }
Jens Axboe4840e412019-12-25 22:03:45 -07003974
3975 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3976 if (ret < 0)
3977 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003978 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003979 return 0;
3980}
3981
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003982static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3983{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003984 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003985 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003986 if (sqe->ioprio || sqe->buf_index)
3987 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003988 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003989 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003990
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003991 req->statx.dfd = READ_ONCE(sqe->fd);
3992 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003993 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003994 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3995 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003996
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003997 return 0;
3998}
3999
Pavel Begunkov014db002020-03-03 21:33:12 +03004000static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004001{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004002 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004003 int ret;
4004
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004005 if (force_nonblock) {
4006 /* only need file table for an actual valid fd */
4007 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4008 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004009 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004010 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004011
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004012 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4013 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004014
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004015 if (ret < 0)
4016 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004017 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004018 return 0;
4019}
4020
Jens Axboeb5dba592019-12-11 14:02:38 -07004021static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4022{
4023 /*
4024 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004025 * leave the 'file' in an undeterminate state, and here need to modify
4026 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004027 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004028 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004029 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4030
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004031 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4032 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004033 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4034 sqe->rw_flags || sqe->buf_index)
4035 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004036 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004037 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004038
4039 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004040 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004041 return -EBADF;
4042
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004043 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004044 return 0;
4045}
4046
Jens Axboe229a7b62020-06-22 10:13:11 -06004047static int io_close(struct io_kiocb *req, bool force_nonblock,
4048 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004049{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004050 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004051 int ret;
4052
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004053 /* might be already done during nonblock submission */
4054 if (!close->put_file) {
4055 ret = __close_fd_get_file(close->fd, &close->put_file);
4056 if (ret < 0)
4057 return (ret == -ENOENT) ? -EBADF : ret;
4058 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004059
4060 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004061 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004062 /* was never set, but play safe */
4063 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004064 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004065 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004066 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004067 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004068
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004069 /* No ->flush() or already async, safely close from here */
4070 ret = filp_close(close->put_file, req->work.files);
4071 if (ret < 0)
4072 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004073 fput(close->put_file);
4074 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004075 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004076 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004077}
4078
Jens Axboe3529d8c2019-12-19 18:24:38 -07004079static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004080{
4081 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004082
4083 if (!req->file)
4084 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004085
4086 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4087 return -EINVAL;
4088 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4089 return -EINVAL;
4090
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004091 req->sync.off = READ_ONCE(sqe->off);
4092 req->sync.len = READ_ONCE(sqe->len);
4093 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004094 return 0;
4095}
4096
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004097static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004098{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004099 int ret;
4100
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004101 /* sync_file_range always requires a blocking context */
4102 if (force_nonblock)
4103 return -EAGAIN;
4104
Jens Axboe9adbd452019-12-20 08:45:55 -07004105 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004106 req->sync.flags);
4107 if (ret < 0)
4108 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004109 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004110 return 0;
4111}
4112
YueHaibing469956e2020-03-04 15:53:52 +08004113#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004114static int io_setup_async_msg(struct io_kiocb *req,
4115 struct io_async_msghdr *kmsg)
4116{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004117 struct io_async_msghdr *async_msg = req->async_data;
4118
4119 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004120 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004121 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004122 if (kmsg->iov != kmsg->fast_iov)
4123 kfree(kmsg->iov);
4124 return -ENOMEM;
4125 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004126 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004127 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004128 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004129 return -EAGAIN;
4130}
4131
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004132static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4133 struct io_async_msghdr *iomsg)
4134{
4135 iomsg->iov = iomsg->fast_iov;
4136 iomsg->msg.msg_name = &iomsg->addr;
4137 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4138 req->sr_msg.msg_flags, &iomsg->iov);
4139}
4140
Jens Axboe3529d8c2019-12-19 18:24:38 -07004141static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004142{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004143 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004144 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004145 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004146
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004147 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4148 return -EINVAL;
4149
Jens Axboee47293f2019-12-20 08:58:21 -07004150 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004151 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004152 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004153
Jens Axboed8768362020-02-27 14:17:49 -07004154#ifdef CONFIG_COMPAT
4155 if (req->ctx->compat)
4156 sr->msg_flags |= MSG_CMSG_COMPAT;
4157#endif
4158
Jens Axboee8c2bc12020-08-15 18:44:09 -07004159 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004160 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004161 /* iovec is already imported */
4162 if (req->flags & REQ_F_NEED_CLEANUP)
4163 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004164
Jens Axboee8c2bc12020-08-15 18:44:09 -07004165 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004166 if (!ret)
4167 req->flags |= REQ_F_NEED_CLEANUP;
4168 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004169}
4170
Jens Axboe229a7b62020-06-22 10:13:11 -06004171static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4172 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004173{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004174 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004175 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004176 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004177 int ret;
4178
Jens Axboe03b12302019-12-02 18:50:25 -07004179 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004180 if (unlikely(!sock))
4181 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004182
Jens Axboee8c2bc12020-08-15 18:44:09 -07004183 if (req->async_data) {
4184 kmsg = req->async_data;
4185 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004186 /* if iov is set, it's allocated already */
4187 if (!kmsg->iov)
4188 kmsg->iov = kmsg->fast_iov;
4189 kmsg->msg.msg_iter.iov = kmsg->iov;
4190 } else {
4191 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004192 if (ret)
4193 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004194 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004195 }
4196
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004197 flags = req->sr_msg.msg_flags;
4198 if (flags & MSG_DONTWAIT)
4199 req->flags |= REQ_F_NOWAIT;
4200 else if (force_nonblock)
4201 flags |= MSG_DONTWAIT;
4202
4203 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4204 if (force_nonblock && ret == -EAGAIN)
4205 return io_setup_async_msg(req, kmsg);
4206 if (ret == -ERESTARTSYS)
4207 ret = -EINTR;
4208
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004209 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004210 kfree(kmsg->iov);
4211 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004212 if (ret < 0)
4213 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004214 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004215 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004216}
4217
Jens Axboe229a7b62020-06-22 10:13:11 -06004218static int io_send(struct io_kiocb *req, bool force_nonblock,
4219 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004220{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004221 struct io_sr_msg *sr = &req->sr_msg;
4222 struct msghdr msg;
4223 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004224 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004225 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004226 int ret;
4227
4228 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004229 if (unlikely(!sock))
4230 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004231
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004232 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4233 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004234 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004235
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004236 msg.msg_name = NULL;
4237 msg.msg_control = NULL;
4238 msg.msg_controllen = 0;
4239 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004240
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004241 flags = req->sr_msg.msg_flags;
4242 if (flags & MSG_DONTWAIT)
4243 req->flags |= REQ_F_NOWAIT;
4244 else if (force_nonblock)
4245 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004246
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004247 msg.msg_flags = flags;
4248 ret = sock_sendmsg(sock, &msg);
4249 if (force_nonblock && ret == -EAGAIN)
4250 return -EAGAIN;
4251 if (ret == -ERESTARTSYS)
4252 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004253
Jens Axboe03b12302019-12-02 18:50:25 -07004254 if (ret < 0)
4255 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004256 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004257 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004258}
4259
Pavel Begunkov1400e692020-07-12 20:41:05 +03004260static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4261 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004262{
4263 struct io_sr_msg *sr = &req->sr_msg;
4264 struct iovec __user *uiov;
4265 size_t iov_len;
4266 int ret;
4267
Pavel Begunkov1400e692020-07-12 20:41:05 +03004268 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4269 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004270 if (ret)
4271 return ret;
4272
4273 if (req->flags & REQ_F_BUFFER_SELECT) {
4274 if (iov_len > 1)
4275 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004276 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004277 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004278 sr->len = iomsg->iov[0].iov_len;
4279 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004280 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004281 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004282 } else {
4283 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004284 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004285 if (ret > 0)
4286 ret = 0;
4287 }
4288
4289 return ret;
4290}
4291
4292#ifdef CONFIG_COMPAT
4293static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004294 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004295{
4296 struct compat_msghdr __user *msg_compat;
4297 struct io_sr_msg *sr = &req->sr_msg;
4298 struct compat_iovec __user *uiov;
4299 compat_uptr_t ptr;
4300 compat_size_t len;
4301 int ret;
4302
Pavel Begunkov270a5942020-07-12 20:41:04 +03004303 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004304 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004305 &ptr, &len);
4306 if (ret)
4307 return ret;
4308
4309 uiov = compat_ptr(ptr);
4310 if (req->flags & REQ_F_BUFFER_SELECT) {
4311 compat_ssize_t clen;
4312
4313 if (len > 1)
4314 return -EINVAL;
4315 if (!access_ok(uiov, sizeof(*uiov)))
4316 return -EFAULT;
4317 if (__get_user(clen, &uiov->iov_len))
4318 return -EFAULT;
4319 if (clen < 0)
4320 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004321 sr->len = iomsg->iov[0].iov_len;
4322 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004323 } else {
4324 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004325 &iomsg->iov,
4326 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004327 if (ret < 0)
4328 return ret;
4329 }
4330
4331 return 0;
4332}
Jens Axboe03b12302019-12-02 18:50:25 -07004333#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004334
Pavel Begunkov1400e692020-07-12 20:41:05 +03004335static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4336 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004337{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004338 iomsg->msg.msg_name = &iomsg->addr;
4339 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004340
4341#ifdef CONFIG_COMPAT
4342 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004343 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004344#endif
4345
Pavel Begunkov1400e692020-07-12 20:41:05 +03004346 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004347}
4348
Jens Axboebcda7ba2020-02-23 16:42:51 -07004349static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004350 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004351{
4352 struct io_sr_msg *sr = &req->sr_msg;
4353 struct io_buffer *kbuf;
4354
Jens Axboebcda7ba2020-02-23 16:42:51 -07004355 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4356 if (IS_ERR(kbuf))
4357 return kbuf;
4358
4359 sr->kbuf = kbuf;
4360 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004361 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004362}
4363
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004364static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4365{
4366 return io_put_kbuf(req, req->sr_msg.kbuf);
4367}
4368
Jens Axboe3529d8c2019-12-19 18:24:38 -07004369static int io_recvmsg_prep(struct io_kiocb *req,
4370 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004371{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004372 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004373 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004374 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004375
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004376 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4377 return -EINVAL;
4378
Jens Axboe3529d8c2019-12-19 18:24:38 -07004379 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004380 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004381 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004382 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004383
Jens Axboed8768362020-02-27 14:17:49 -07004384#ifdef CONFIG_COMPAT
4385 if (req->ctx->compat)
4386 sr->msg_flags |= MSG_CMSG_COMPAT;
4387#endif
4388
Jens Axboee8c2bc12020-08-15 18:44:09 -07004389 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004390 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004391 /* iovec is already imported */
4392 if (req->flags & REQ_F_NEED_CLEANUP)
4393 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004394
Jens Axboee8c2bc12020-08-15 18:44:09 -07004395 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004396 if (!ret)
4397 req->flags |= REQ_F_NEED_CLEANUP;
4398 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004399}
4400
Jens Axboe229a7b62020-06-22 10:13:11 -06004401static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4402 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004403{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004404 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004405 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004406 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004407 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004408 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004409
Jens Axboe0fa03c62019-04-19 13:34:07 -06004410 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004411 if (unlikely(!sock))
4412 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004413
Jens Axboee8c2bc12020-08-15 18:44:09 -07004414 if (req->async_data) {
4415 kmsg = req->async_data;
4416 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004417 /* if iov is set, it's allocated already */
4418 if (!kmsg->iov)
4419 kmsg->iov = kmsg->fast_iov;
4420 kmsg->msg.msg_iter.iov = kmsg->iov;
4421 } else {
4422 ret = io_recvmsg_copy_hdr(req, &iomsg);
4423 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004424 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004425 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004426 }
4427
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004428 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004429 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004430 if (IS_ERR(kbuf))
4431 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004432 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4433 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4434 1, req->sr_msg.len);
4435 }
4436
4437 flags = req->sr_msg.msg_flags;
4438 if (flags & MSG_DONTWAIT)
4439 req->flags |= REQ_F_NOWAIT;
4440 else if (force_nonblock)
4441 flags |= MSG_DONTWAIT;
4442
4443 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4444 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004445 if (force_nonblock && ret == -EAGAIN)
4446 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004447 if (ret == -ERESTARTSYS)
4448 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004449
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004450 if (req->flags & REQ_F_BUFFER_SELECTED)
4451 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004452 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004453 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004454 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004455 if (ret < 0)
4456 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004457 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004458 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004459}
4460
Jens Axboe229a7b62020-06-22 10:13:11 -06004461static int io_recv(struct io_kiocb *req, bool force_nonblock,
4462 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004463{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004464 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004465 struct io_sr_msg *sr = &req->sr_msg;
4466 struct msghdr msg;
4467 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004468 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004469 struct iovec iov;
4470 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004471 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004472
Jens Axboefddafac2020-01-04 20:19:44 -07004473 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004474 if (unlikely(!sock))
4475 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004476
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004477 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004478 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004479 if (IS_ERR(kbuf))
4480 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004481 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004482 }
4483
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004484 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004485 if (unlikely(ret))
4486 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004487
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004488 msg.msg_name = NULL;
4489 msg.msg_control = NULL;
4490 msg.msg_controllen = 0;
4491 msg.msg_namelen = 0;
4492 msg.msg_iocb = NULL;
4493 msg.msg_flags = 0;
4494
4495 flags = req->sr_msg.msg_flags;
4496 if (flags & MSG_DONTWAIT)
4497 req->flags |= REQ_F_NOWAIT;
4498 else if (force_nonblock)
4499 flags |= MSG_DONTWAIT;
4500
4501 ret = sock_recvmsg(sock, &msg, flags);
4502 if (force_nonblock && ret == -EAGAIN)
4503 return -EAGAIN;
4504 if (ret == -ERESTARTSYS)
4505 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004506out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004507 if (req->flags & REQ_F_BUFFER_SELECTED)
4508 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004509 if (ret < 0)
4510 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004511 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004512 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004513}
4514
Jens Axboe3529d8c2019-12-19 18:24:38 -07004515static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004516{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004517 struct io_accept *accept = &req->accept;
4518
Jens Axboe17f2fe32019-10-17 14:42:58 -06004519 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4520 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004521 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004522 return -EINVAL;
4523
Jens Axboed55e5f52019-12-11 16:12:15 -07004524 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4525 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004526 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004527 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004528 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004529}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004530
Jens Axboe229a7b62020-06-22 10:13:11 -06004531static int io_accept(struct io_kiocb *req, bool force_nonblock,
4532 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004533{
4534 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004535 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004536 int ret;
4537
Jiufei Xuee697dee2020-06-10 13:41:59 +08004538 if (req->file->f_flags & O_NONBLOCK)
4539 req->flags |= REQ_F_NOWAIT;
4540
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004541 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004542 accept->addr_len, accept->flags,
4543 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004544 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004545 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004546 if (ret < 0) {
4547 if (ret == -ERESTARTSYS)
4548 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004549 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004550 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004551 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004552 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004553}
4554
Jens Axboe3529d8c2019-12-19 18:24:38 -07004555static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004556{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004557 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004558 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004559
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004560 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4561 return -EINVAL;
4562 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4563 return -EINVAL;
4564
Jens Axboe3529d8c2019-12-19 18:24:38 -07004565 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4566 conn->addr_len = READ_ONCE(sqe->addr2);
4567
4568 if (!io)
4569 return 0;
4570
4571 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004572 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004573}
4574
Jens Axboe229a7b62020-06-22 10:13:11 -06004575static int io_connect(struct io_kiocb *req, bool force_nonblock,
4576 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004577{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004578 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004579 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004580 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004581
Jens Axboee8c2bc12020-08-15 18:44:09 -07004582 if (req->async_data) {
4583 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004584 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004585 ret = move_addr_to_kernel(req->connect.addr,
4586 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004587 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004588 if (ret)
4589 goto out;
4590 io = &__io;
4591 }
4592
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004593 file_flags = force_nonblock ? O_NONBLOCK : 0;
4594
Jens Axboee8c2bc12020-08-15 18:44:09 -07004595 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004596 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004597 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004598 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004599 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004600 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004601 ret = -ENOMEM;
4602 goto out;
4603 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004604 io = req->async_data;
4605 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004606 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004607 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004608 if (ret == -ERESTARTSYS)
4609 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004610out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004611 if (ret < 0)
4612 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004613 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004614 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004615}
YueHaibing469956e2020-03-04 15:53:52 +08004616#else /* !CONFIG_NET */
4617static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4618{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004619 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004620}
4621
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004622static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4623 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004624{
YueHaibing469956e2020-03-04 15:53:52 +08004625 return -EOPNOTSUPP;
4626}
4627
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004628static int io_send(struct io_kiocb *req, bool force_nonblock,
4629 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004630{
4631 return -EOPNOTSUPP;
4632}
4633
4634static int io_recvmsg_prep(struct io_kiocb *req,
4635 const struct io_uring_sqe *sqe)
4636{
4637 return -EOPNOTSUPP;
4638}
4639
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004640static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4641 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004642{
4643 return -EOPNOTSUPP;
4644}
4645
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004646static int io_recv(struct io_kiocb *req, bool force_nonblock,
4647 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004648{
4649 return -EOPNOTSUPP;
4650}
4651
4652static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4653{
4654 return -EOPNOTSUPP;
4655}
4656
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004657static int io_accept(struct io_kiocb *req, bool force_nonblock,
4658 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004659{
4660 return -EOPNOTSUPP;
4661}
4662
4663static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4664{
4665 return -EOPNOTSUPP;
4666}
4667
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004668static int io_connect(struct io_kiocb *req, bool force_nonblock,
4669 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004670{
4671 return -EOPNOTSUPP;
4672}
4673#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004674
Jens Axboed7718a92020-02-14 22:23:12 -07004675struct io_poll_table {
4676 struct poll_table_struct pt;
4677 struct io_kiocb *req;
4678 int error;
4679};
4680
Jens Axboed7718a92020-02-14 22:23:12 -07004681static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4682 __poll_t mask, task_work_func_t func)
4683{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004684 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004685 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004686
4687 /* for instances that support it check for an event match first: */
4688 if (mask && !(mask & poll->events))
4689 return 0;
4690
4691 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4692
4693 list_del_init(&poll->wait.entry);
4694
Jens Axboed7718a92020-02-14 22:23:12 -07004695 req->result = mask;
4696 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004697 percpu_ref_get(&req->ctx->refs);
4698
Jens Axboed7718a92020-02-14 22:23:12 -07004699 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004700 * If we using the signalfd wait_queue_head for this wakeup, then
4701 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4702 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4703 * either, as the normal wakeup will suffice.
4704 */
4705 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4706
4707 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004708 * If this fails, then the task is exiting. When a task exits, the
4709 * work gets canceled, so just cancel this request as well instead
4710 * of executing it. We can't safely execute it anyway, as we may not
4711 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004712 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004713 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004714 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004715 struct task_struct *tsk;
4716
Jens Axboee3aabf92020-05-18 11:04:17 -06004717 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004718 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004719 task_work_add(tsk, &req->task_work, 0);
4720 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004721 }
Jens Axboed7718a92020-02-14 22:23:12 -07004722 return 1;
4723}
4724
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004725static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4726 __acquires(&req->ctx->completion_lock)
4727{
4728 struct io_ring_ctx *ctx = req->ctx;
4729
4730 if (!req->result && !READ_ONCE(poll->canceled)) {
4731 struct poll_table_struct pt = { ._key = poll->events };
4732
4733 req->result = vfs_poll(req->file, &pt) & poll->events;
4734 }
4735
4736 spin_lock_irq(&ctx->completion_lock);
4737 if (!req->result && !READ_ONCE(poll->canceled)) {
4738 add_wait_queue(poll->head, &poll->wait);
4739 return true;
4740 }
4741
4742 return false;
4743}
4744
Jens Axboed4e7cd32020-08-15 11:44:50 -07004745static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004746{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004747 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004748 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004749 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004750 return req->apoll->double_poll;
4751}
4752
4753static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4754{
4755 if (req->opcode == IORING_OP_POLL_ADD)
4756 return &req->poll;
4757 return &req->apoll->poll;
4758}
4759
4760static void io_poll_remove_double(struct io_kiocb *req)
4761{
4762 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004763
4764 lockdep_assert_held(&req->ctx->completion_lock);
4765
4766 if (poll && poll->head) {
4767 struct wait_queue_head *head = poll->head;
4768
4769 spin_lock(&head->lock);
4770 list_del_init(&poll->wait.entry);
4771 if (poll->wait.private)
4772 refcount_dec(&req->refs);
4773 poll->head = NULL;
4774 spin_unlock(&head->lock);
4775 }
4776}
4777
4778static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4779{
4780 struct io_ring_ctx *ctx = req->ctx;
4781
Jens Axboed4e7cd32020-08-15 11:44:50 -07004782 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004783 req->poll.done = true;
4784 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4785 io_commit_cqring(ctx);
4786}
4787
4788static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4789{
4790 struct io_ring_ctx *ctx = req->ctx;
4791
4792 if (io_poll_rewait(req, &req->poll)) {
4793 spin_unlock_irq(&ctx->completion_lock);
4794 return;
4795 }
4796
4797 hash_del(&req->hash_node);
4798 io_poll_complete(req, req->result, 0);
4799 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004800 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004801 spin_unlock_irq(&ctx->completion_lock);
4802
4803 io_cqring_ev_posted(ctx);
4804}
4805
4806static void io_poll_task_func(struct callback_head *cb)
4807{
4808 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004809 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004810 struct io_kiocb *nxt = NULL;
4811
4812 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004813 if (nxt)
4814 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004815 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004816}
4817
4818static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4819 int sync, void *key)
4820{
4821 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004822 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004823 __poll_t mask = key_to_poll(key);
4824
4825 /* for instances that support it check for an event match first: */
4826 if (mask && !(mask & poll->events))
4827 return 0;
4828
Jens Axboe8706e042020-09-28 08:38:54 -06004829 list_del_init(&wait->entry);
4830
Jens Axboe807abcb2020-07-17 17:09:27 -06004831 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004832 bool done;
4833
Jens Axboe807abcb2020-07-17 17:09:27 -06004834 spin_lock(&poll->head->lock);
4835 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004836 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004837 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004838 /* make sure double remove sees this as being gone */
4839 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004840 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004841 if (!done)
4842 __io_async_wake(req, poll, mask, io_poll_task_func);
4843 }
4844 refcount_dec(&req->refs);
4845 return 1;
4846}
4847
4848static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4849 wait_queue_func_t wake_func)
4850{
4851 poll->head = NULL;
4852 poll->done = false;
4853 poll->canceled = false;
4854 poll->events = events;
4855 INIT_LIST_HEAD(&poll->wait.entry);
4856 init_waitqueue_func_entry(&poll->wait, wake_func);
4857}
4858
4859static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004860 struct wait_queue_head *head,
4861 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004862{
4863 struct io_kiocb *req = pt->req;
4864
4865 /*
4866 * If poll->head is already set, it's because the file being polled
4867 * uses multiple waitqueues for poll handling (eg one for read, one
4868 * for write). Setup a separate io_poll_iocb if this happens.
4869 */
4870 if (unlikely(poll->head)) {
4871 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004872 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004873 pt->error = -EINVAL;
4874 return;
4875 }
4876 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4877 if (!poll) {
4878 pt->error = -ENOMEM;
4879 return;
4880 }
4881 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4882 refcount_inc(&req->refs);
4883 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004884 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004885 }
4886
4887 pt->error = 0;
4888 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004889
4890 if (poll->events & EPOLLEXCLUSIVE)
4891 add_wait_queue_exclusive(head, &poll->wait);
4892 else
4893 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004894}
4895
4896static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4897 struct poll_table_struct *p)
4898{
4899 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004900 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004901
Jens Axboe807abcb2020-07-17 17:09:27 -06004902 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004903}
4904
Jens Axboed7718a92020-02-14 22:23:12 -07004905static void io_async_task_func(struct callback_head *cb)
4906{
4907 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4908 struct async_poll *apoll = req->apoll;
4909 struct io_ring_ctx *ctx = req->ctx;
4910
4911 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4912
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004913 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004914 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004915 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004916 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004917 }
4918
Jens Axboe31067252020-05-17 17:43:31 -06004919 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004920 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004921 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004922
Jens Axboed4e7cd32020-08-15 11:44:50 -07004923 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004924 spin_unlock_irq(&ctx->completion_lock);
4925
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004926 if (!READ_ONCE(apoll->poll.canceled))
4927 __io_req_task_submit(req);
4928 else
4929 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004930
Jens Axboe6d816e02020-08-11 08:04:14 -06004931 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004932 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004933 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004934}
4935
4936static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4937 void *key)
4938{
4939 struct io_kiocb *req = wait->private;
4940 struct io_poll_iocb *poll = &req->apoll->poll;
4941
4942 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4943 key_to_poll(key));
4944
4945 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4946}
4947
4948static void io_poll_req_insert(struct io_kiocb *req)
4949{
4950 struct io_ring_ctx *ctx = req->ctx;
4951 struct hlist_head *list;
4952
4953 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4954 hlist_add_head(&req->hash_node, list);
4955}
4956
4957static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4958 struct io_poll_iocb *poll,
4959 struct io_poll_table *ipt, __poll_t mask,
4960 wait_queue_func_t wake_func)
4961 __acquires(&ctx->completion_lock)
4962{
4963 struct io_ring_ctx *ctx = req->ctx;
4964 bool cancel = false;
4965
Jens Axboe18bceab2020-05-15 11:56:54 -06004966 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004967 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004968 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004969
4970 ipt->pt._key = mask;
4971 ipt->req = req;
4972 ipt->error = -EINVAL;
4973
Jens Axboed7718a92020-02-14 22:23:12 -07004974 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4975
4976 spin_lock_irq(&ctx->completion_lock);
4977 if (likely(poll->head)) {
4978 spin_lock(&poll->head->lock);
4979 if (unlikely(list_empty(&poll->wait.entry))) {
4980 if (ipt->error)
4981 cancel = true;
4982 ipt->error = 0;
4983 mask = 0;
4984 }
4985 if (mask || ipt->error)
4986 list_del_init(&poll->wait.entry);
4987 else if (cancel)
4988 WRITE_ONCE(poll->canceled, true);
4989 else if (!poll->done) /* actually waiting for an event */
4990 io_poll_req_insert(req);
4991 spin_unlock(&poll->head->lock);
4992 }
4993
4994 return mask;
4995}
4996
4997static bool io_arm_poll_handler(struct io_kiocb *req)
4998{
4999 const struct io_op_def *def = &io_op_defs[req->opcode];
5000 struct io_ring_ctx *ctx = req->ctx;
5001 struct async_poll *apoll;
5002 struct io_poll_table ipt;
5003 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005004 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005005
5006 if (!req->file || !file_can_poll(req->file))
5007 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005008 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005009 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005010 if (def->pollin)
5011 rw = READ;
5012 else if (def->pollout)
5013 rw = WRITE;
5014 else
5015 return false;
5016 /* if we can't nonblock try, then no point in arming a poll handler */
5017 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005018 return false;
5019
5020 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5021 if (unlikely(!apoll))
5022 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005023 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005024
5025 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005026 req->apoll = apoll;
5027 INIT_HLIST_NODE(&req->hash_node);
5028
Nathan Chancellor8755d972020-03-02 16:01:19 -07005029 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005030 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005031 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005032 if (def->pollout)
5033 mask |= POLLOUT | POLLWRNORM;
5034 mask |= POLLERR | POLLPRI;
5035
5036 ipt.pt._qproc = io_async_queue_proc;
5037
5038 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5039 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005040 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005041 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005042 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005043 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005044 kfree(apoll);
5045 return false;
5046 }
5047 spin_unlock_irq(&ctx->completion_lock);
5048 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5049 apoll->poll.events);
5050 return true;
5051}
5052
5053static bool __io_poll_remove_one(struct io_kiocb *req,
5054 struct io_poll_iocb *poll)
5055{
Jens Axboeb41e9852020-02-17 09:52:41 -07005056 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005057
5058 spin_lock(&poll->head->lock);
5059 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005060 if (!list_empty(&poll->wait.entry)) {
5061 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005062 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005063 }
5064 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005065 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005066 return do_complete;
5067}
5068
5069static bool io_poll_remove_one(struct io_kiocb *req)
5070{
5071 bool do_complete;
5072
Jens Axboed4e7cd32020-08-15 11:44:50 -07005073 io_poll_remove_double(req);
5074
Jens Axboed7718a92020-02-14 22:23:12 -07005075 if (req->opcode == IORING_OP_POLL_ADD) {
5076 do_complete = __io_poll_remove_one(req, &req->poll);
5077 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005078 struct async_poll *apoll = req->apoll;
5079
Jens Axboed7718a92020-02-14 22:23:12 -07005080 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005081 do_complete = __io_poll_remove_one(req, &apoll->poll);
5082 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005083 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005084 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005085 kfree(apoll);
5086 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005087 }
5088
Jens Axboeb41e9852020-02-17 09:52:41 -07005089 if (do_complete) {
5090 io_cqring_fill_event(req, -ECANCELED);
5091 io_commit_cqring(req->ctx);
5092 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005093 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005094 io_put_req(req);
5095 }
5096
5097 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005098}
5099
Jens Axboe76e1b642020-09-26 15:05:03 -06005100/*
5101 * Returns true if we found and killed one or more poll requests
5102 */
5103static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005104{
Jens Axboe78076bb2019-12-04 19:56:40 -07005105 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005106 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005107 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005108
5109 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005110 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5111 struct hlist_head *list;
5112
5113 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005114 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5115 if (io_task_match(req, tsk))
5116 posted += io_poll_remove_one(req);
5117 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005118 }
5119 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005120
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005121 if (posted)
5122 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005123
5124 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005125}
5126
Jens Axboe47f46762019-11-09 17:43:02 -07005127static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5128{
Jens Axboe78076bb2019-12-04 19:56:40 -07005129 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005130 struct io_kiocb *req;
5131
Jens Axboe78076bb2019-12-04 19:56:40 -07005132 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5133 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005134 if (sqe_addr != req->user_data)
5135 continue;
5136 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005137 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005138 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005139 }
5140
5141 return -ENOENT;
5142}
5143
Jens Axboe3529d8c2019-12-19 18:24:38 -07005144static int io_poll_remove_prep(struct io_kiocb *req,
5145 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005146{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005147 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5148 return -EINVAL;
5149 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5150 sqe->poll_events)
5151 return -EINVAL;
5152
Jens Axboe0969e782019-12-17 18:40:57 -07005153 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005154 return 0;
5155}
5156
5157/*
5158 * Find a running poll command that matches one specified in sqe->addr,
5159 * and remove it if found.
5160 */
5161static int io_poll_remove(struct io_kiocb *req)
5162{
5163 struct io_ring_ctx *ctx = req->ctx;
5164 u64 addr;
5165 int ret;
5166
Jens Axboe0969e782019-12-17 18:40:57 -07005167 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005168 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005169 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005170 spin_unlock_irq(&ctx->completion_lock);
5171
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005172 if (ret < 0)
5173 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005174 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005175 return 0;
5176}
5177
Jens Axboe221c5eb2019-01-17 09:41:58 -07005178static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5179 void *key)
5180{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005181 struct io_kiocb *req = wait->private;
5182 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005183
Jens Axboed7718a92020-02-14 22:23:12 -07005184 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005185}
5186
Jens Axboe221c5eb2019-01-17 09:41:58 -07005187static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5188 struct poll_table_struct *p)
5189{
5190 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5191
Jens Axboee8c2bc12020-08-15 18:44:09 -07005192 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005193}
5194
Jens Axboe3529d8c2019-12-19 18:24:38 -07005195static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005196{
5197 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005198 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005199
5200 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5201 return -EINVAL;
5202 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5203 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005204 if (!poll->file)
5205 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005206
Jiufei Xue5769a352020-06-17 17:53:55 +08005207 events = READ_ONCE(sqe->poll32_events);
5208#ifdef __BIG_ENDIAN
5209 events = swahw32(events);
5210#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005211 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5212 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005213 return 0;
5214}
5215
Pavel Begunkov014db002020-03-03 21:33:12 +03005216static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005217{
5218 struct io_poll_iocb *poll = &req->poll;
5219 struct io_ring_ctx *ctx = req->ctx;
5220 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005221 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005222
Jens Axboe78076bb2019-12-04 19:56:40 -07005223 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005224 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005225
Jens Axboed7718a92020-02-14 22:23:12 -07005226 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5227 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005228
Jens Axboe8c838782019-03-12 15:48:16 -06005229 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005230 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005231 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005232 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005233 spin_unlock_irq(&ctx->completion_lock);
5234
Jens Axboe8c838782019-03-12 15:48:16 -06005235 if (mask) {
5236 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005237 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005238 }
Jens Axboe8c838782019-03-12 15:48:16 -06005239 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005240}
5241
Jens Axboe5262f562019-09-17 12:26:57 -06005242static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5243{
Jens Axboead8a48a2019-11-15 08:49:11 -07005244 struct io_timeout_data *data = container_of(timer,
5245 struct io_timeout_data, timer);
5246 struct io_kiocb *req = data->req;
5247 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005248 unsigned long flags;
5249
Jens Axboe5262f562019-09-17 12:26:57 -06005250 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005251 atomic_set(&req->ctx->cq_timeouts,
5252 atomic_read(&req->ctx->cq_timeouts) + 1);
5253
zhangyi (F)ef036812019-10-23 15:10:08 +08005254 /*
Jens Axboe11365042019-10-16 09:08:32 -06005255 * We could be racing with timeout deletion. If the list is empty,
5256 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005257 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005258 if (!list_empty(&req->timeout.list))
5259 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005260
Jens Axboe78e19bb2019-11-06 15:21:34 -07005261 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005262 io_commit_cqring(ctx);
5263 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5264
5265 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005266 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005267 io_put_req(req);
5268 return HRTIMER_NORESTART;
5269}
5270
Jens Axboef254ac02020-08-12 17:33:30 -06005271static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005272{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005273 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005274 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005275
Jens Axboef254ac02020-08-12 17:33:30 -06005276 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005277
Jens Axboee8c2bc12020-08-15 18:44:09 -07005278 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005279 if (ret == -1)
5280 return -EALREADY;
5281
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005282 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005283 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005284 io_cqring_fill_event(req, -ECANCELED);
5285 io_put_req(req);
5286 return 0;
5287}
5288
Jens Axboef254ac02020-08-12 17:33:30 -06005289static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5290{
5291 struct io_kiocb *req;
5292 int ret = -ENOENT;
5293
5294 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5295 if (user_data == req->user_data) {
5296 ret = 0;
5297 break;
5298 }
5299 }
5300
5301 if (ret == -ENOENT)
5302 return ret;
5303
5304 return __io_timeout_cancel(req);
5305}
5306
Jens Axboe3529d8c2019-12-19 18:24:38 -07005307static int io_timeout_remove_prep(struct io_kiocb *req,
5308 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005309{
Jens Axboeb29472e2019-12-17 18:50:29 -07005310 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5311 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005312 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5313 return -EINVAL;
5314 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005315 return -EINVAL;
5316
5317 req->timeout.addr = READ_ONCE(sqe->addr);
5318 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5319 if (req->timeout.flags)
5320 return -EINVAL;
5321
Jens Axboeb29472e2019-12-17 18:50:29 -07005322 return 0;
5323}
5324
Jens Axboe11365042019-10-16 09:08:32 -06005325/*
5326 * Remove or update an existing timeout command
5327 */
Jens Axboefc4df992019-12-10 14:38:45 -07005328static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005329{
5330 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005331 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005332
Jens Axboe11365042019-10-16 09:08:32 -06005333 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005334 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005335
Jens Axboe47f46762019-11-09 17:43:02 -07005336 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005337 io_commit_cqring(ctx);
5338 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005339 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005340 if (ret < 0)
5341 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005342 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005343 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005344}
5345
Jens Axboe3529d8c2019-12-19 18:24:38 -07005346static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005347 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005348{
Jens Axboead8a48a2019-11-15 08:49:11 -07005349 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005350 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005351 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005352
Jens Axboead8a48a2019-11-15 08:49:11 -07005353 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005354 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005355 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005356 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005357 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005358 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005359 flags = READ_ONCE(sqe->timeout_flags);
5360 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005361 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005362
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005363 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005364
Jens Axboee8c2bc12020-08-15 18:44:09 -07005365 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005366 return -ENOMEM;
5367
Jens Axboee8c2bc12020-08-15 18:44:09 -07005368 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005369 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005370
5371 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005372 return -EFAULT;
5373
Jens Axboe11365042019-10-16 09:08:32 -06005374 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005375 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005376 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005377 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005378
Jens Axboead8a48a2019-11-15 08:49:11 -07005379 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5380 return 0;
5381}
5382
Jens Axboefc4df992019-12-10 14:38:45 -07005383static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005384{
Jens Axboead8a48a2019-11-15 08:49:11 -07005385 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005386 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005387 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005388 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005389
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005390 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005391
Jens Axboe5262f562019-09-17 12:26:57 -06005392 /*
5393 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005394 * timeout event to be satisfied. If it isn't set, then this is
5395 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005396 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005397 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005398 entry = ctx->timeout_list.prev;
5399 goto add;
5400 }
Jens Axboe5262f562019-09-17 12:26:57 -06005401
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005402 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5403 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005404
5405 /*
5406 * Insertion sort, ensuring the first entry in the list is always
5407 * the one we need first.
5408 */
Jens Axboe5262f562019-09-17 12:26:57 -06005409 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005410 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5411 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005412
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005413 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005414 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005415 /* nxt.seq is behind @tail, otherwise would've been completed */
5416 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005417 break;
5418 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005419add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005420 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005421 data->timer.function = io_timeout_fn;
5422 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005423 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005424 return 0;
5425}
5426
Jens Axboe62755e32019-10-28 21:49:21 -06005427static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005428{
Jens Axboe62755e32019-10-28 21:49:21 -06005429 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005430
Jens Axboe62755e32019-10-28 21:49:21 -06005431 return req->user_data == (unsigned long) data;
5432}
5433
Jens Axboee977d6d2019-11-05 12:39:45 -07005434static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005435{
Jens Axboe62755e32019-10-28 21:49:21 -06005436 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005437 int ret = 0;
5438
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005439 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005440 switch (cancel_ret) {
5441 case IO_WQ_CANCEL_OK:
5442 ret = 0;
5443 break;
5444 case IO_WQ_CANCEL_RUNNING:
5445 ret = -EALREADY;
5446 break;
5447 case IO_WQ_CANCEL_NOTFOUND:
5448 ret = -ENOENT;
5449 break;
5450 }
5451
Jens Axboee977d6d2019-11-05 12:39:45 -07005452 return ret;
5453}
5454
Jens Axboe47f46762019-11-09 17:43:02 -07005455static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5456 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005457 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005458{
5459 unsigned long flags;
5460 int ret;
5461
5462 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5463 if (ret != -ENOENT) {
5464 spin_lock_irqsave(&ctx->completion_lock, flags);
5465 goto done;
5466 }
5467
5468 spin_lock_irqsave(&ctx->completion_lock, flags);
5469 ret = io_timeout_cancel(ctx, sqe_addr);
5470 if (ret != -ENOENT)
5471 goto done;
5472 ret = io_poll_cancel(ctx, sqe_addr);
5473done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005474 if (!ret)
5475 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005476 io_cqring_fill_event(req, ret);
5477 io_commit_cqring(ctx);
5478 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5479 io_cqring_ev_posted(ctx);
5480
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005481 if (ret < 0)
5482 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005483 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005484}
5485
Jens Axboe3529d8c2019-12-19 18:24:38 -07005486static int io_async_cancel_prep(struct io_kiocb *req,
5487 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005488{
Jens Axboefbf23842019-12-17 18:45:56 -07005489 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005490 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005491 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5492 return -EINVAL;
5493 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005494 return -EINVAL;
5495
Jens Axboefbf23842019-12-17 18:45:56 -07005496 req->cancel.addr = READ_ONCE(sqe->addr);
5497 return 0;
5498}
5499
Pavel Begunkov014db002020-03-03 21:33:12 +03005500static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005501{
5502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005503
Pavel Begunkov014db002020-03-03 21:33:12 +03005504 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005505 return 0;
5506}
5507
Jens Axboe05f3fb32019-12-09 11:22:50 -07005508static int io_files_update_prep(struct io_kiocb *req,
5509 const struct io_uring_sqe *sqe)
5510{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005511 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5512 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005513 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5514 return -EINVAL;
5515 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005516 return -EINVAL;
5517
5518 req->files_update.offset = READ_ONCE(sqe->off);
5519 req->files_update.nr_args = READ_ONCE(sqe->len);
5520 if (!req->files_update.nr_args)
5521 return -EINVAL;
5522 req->files_update.arg = READ_ONCE(sqe->addr);
5523 return 0;
5524}
5525
Jens Axboe229a7b62020-06-22 10:13:11 -06005526static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5527 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005528{
5529 struct io_ring_ctx *ctx = req->ctx;
5530 struct io_uring_files_update up;
5531 int ret;
5532
Jens Axboef86cd202020-01-29 13:46:44 -07005533 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005534 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005535
5536 up.offset = req->files_update.offset;
5537 up.fds = req->files_update.arg;
5538
5539 mutex_lock(&ctx->uring_lock);
5540 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5541 mutex_unlock(&ctx->uring_lock);
5542
5543 if (ret < 0)
5544 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005545 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005546 return 0;
5547}
5548
Jens Axboe3529d8c2019-12-19 18:24:38 -07005549static int io_req_defer_prep(struct io_kiocb *req,
5550 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005551{
Jens Axboee7815732019-12-17 19:45:06 -07005552 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005553
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005554 if (!sqe)
5555 return 0;
5556
Jens Axboee8c2bc12020-08-15 18:44:09 -07005557 if (io_alloc_async_data(req))
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005558 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005559 ret = io_prep_work_files(req);
5560 if (unlikely(ret))
5561 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005562
Jens Axboe202700e12020-09-12 13:18:10 -06005563 io_prep_async_work(req);
5564
Jens Axboed625c6e2019-12-17 19:53:05 -07005565 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005566 case IORING_OP_NOP:
5567 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005568 case IORING_OP_READV:
5569 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005570 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005571 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005572 break;
5573 case IORING_OP_WRITEV:
5574 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005575 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005576 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005577 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005578 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005579 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005580 break;
5581 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005582 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005583 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005584 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005585 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005586 break;
5587 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005588 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005589 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005590 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005591 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005592 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005593 break;
5594 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005595 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005596 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005597 break;
Jens Axboef499a022019-12-02 16:28:46 -07005598 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005599 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005600 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005601 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005602 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005603 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005604 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005605 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005606 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005607 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005608 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005609 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005610 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005611 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005612 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005613 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005614 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005615 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005616 case IORING_OP_FALLOCATE:
5617 ret = io_fallocate_prep(req, sqe);
5618 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005619 case IORING_OP_OPENAT:
5620 ret = io_openat_prep(req, sqe);
5621 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005622 case IORING_OP_CLOSE:
5623 ret = io_close_prep(req, sqe);
5624 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005625 case IORING_OP_FILES_UPDATE:
5626 ret = io_files_update_prep(req, sqe);
5627 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005628 case IORING_OP_STATX:
5629 ret = io_statx_prep(req, sqe);
5630 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005631 case IORING_OP_FADVISE:
5632 ret = io_fadvise_prep(req, sqe);
5633 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005634 case IORING_OP_MADVISE:
5635 ret = io_madvise_prep(req, sqe);
5636 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005637 case IORING_OP_OPENAT2:
5638 ret = io_openat2_prep(req, sqe);
5639 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005640 case IORING_OP_EPOLL_CTL:
5641 ret = io_epoll_ctl_prep(req, sqe);
5642 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005643 case IORING_OP_SPLICE:
5644 ret = io_splice_prep(req, sqe);
5645 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005646 case IORING_OP_PROVIDE_BUFFERS:
5647 ret = io_provide_buffers_prep(req, sqe);
5648 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005649 case IORING_OP_REMOVE_BUFFERS:
5650 ret = io_remove_buffers_prep(req, sqe);
5651 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005652 case IORING_OP_TEE:
5653 ret = io_tee_prep(req, sqe);
5654 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005655 default:
Jens Axboee7815732019-12-17 19:45:06 -07005656 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5657 req->opcode);
5658 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005659 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005660 }
5661
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005662 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005663}
5664
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005665static u32 io_get_sequence(struct io_kiocb *req)
5666{
5667 struct io_kiocb *pos;
5668 struct io_ring_ctx *ctx = req->ctx;
5669 u32 total_submitted, nr_reqs = 1;
5670
5671 if (req->flags & REQ_F_LINK_HEAD)
5672 list_for_each_entry(pos, &req->link_list, link_list)
5673 nr_reqs++;
5674
5675 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5676 return total_submitted - nr_reqs;
5677}
5678
Jens Axboe3529d8c2019-12-19 18:24:38 -07005679static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005680{
Jackie Liua197f662019-11-08 08:09:12 -07005681 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005682 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005683 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005684 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005685
Bob Liu9d858b22019-11-13 18:06:25 +08005686 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005687 if (likely(list_empty_careful(&ctx->defer_list) &&
5688 !(req->flags & REQ_F_IO_DRAIN)))
5689 return 0;
5690
5691 seq = io_get_sequence(req);
5692 /* Still a chance to pass the sequence check */
5693 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005694 return 0;
5695
Jens Axboee8c2bc12020-08-15 18:44:09 -07005696 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005697 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005698 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005699 return ret;
5700 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005701 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005702 de = kmalloc(sizeof(*de), GFP_KERNEL);
5703 if (!de)
5704 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005705
Jens Axboede0617e2019-04-06 21:51:27 -06005706 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005707 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005708 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005709 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005710 io_queue_async_work(req);
5711 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005712 }
5713
Jens Axboe915967f2019-11-21 09:01:20 -07005714 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005715 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005716 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005717 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005718 spin_unlock_irq(&ctx->completion_lock);
5719 return -EIOCBQUEUED;
5720}
5721
Jens Axboef573d382020-09-22 10:19:24 -06005722static void io_req_drop_files(struct io_kiocb *req)
5723{
5724 struct io_ring_ctx *ctx = req->ctx;
5725 unsigned long flags;
5726
5727 spin_lock_irqsave(&ctx->inflight_lock, flags);
5728 list_del(&req->inflight_entry);
5729 if (waitqueue_active(&ctx->inflight_wait))
5730 wake_up(&ctx->inflight_wait);
5731 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5732 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005733 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005734 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005735 req->work.files = NULL;
5736}
5737
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005738static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005739{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005740 if (req->flags & REQ_F_BUFFER_SELECTED) {
5741 switch (req->opcode) {
5742 case IORING_OP_READV:
5743 case IORING_OP_READ_FIXED:
5744 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005745 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005746 break;
5747 case IORING_OP_RECVMSG:
5748 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005749 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005750 break;
5751 }
5752 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005753 }
5754
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005755 if (req->flags & REQ_F_NEED_CLEANUP) {
5756 switch (req->opcode) {
5757 case IORING_OP_READV:
5758 case IORING_OP_READ_FIXED:
5759 case IORING_OP_READ:
5760 case IORING_OP_WRITEV:
5761 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005762 case IORING_OP_WRITE: {
5763 struct io_async_rw *io = req->async_data;
5764 if (io->free_iovec)
5765 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005766 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005767 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005768 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005769 case IORING_OP_SENDMSG: {
5770 struct io_async_msghdr *io = req->async_data;
5771 if (io->iov != io->fast_iov)
5772 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005773 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005774 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005775 case IORING_OP_SPLICE:
5776 case IORING_OP_TEE:
5777 io_put_file(req, req->splice.file_in,
5778 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5779 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005780 case IORING_OP_OPENAT:
5781 case IORING_OP_OPENAT2:
5782 if (req->open.filename)
5783 putname(req->open.filename);
5784 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005785 }
5786 req->flags &= ~REQ_F_NEED_CLEANUP;
5787 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005788
Jens Axboef573d382020-09-22 10:19:24 -06005789 if (req->flags & REQ_F_INFLIGHT)
5790 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005791}
5792
Jens Axboe3529d8c2019-12-19 18:24:38 -07005793static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005794 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005795{
Jackie Liua197f662019-11-08 08:09:12 -07005796 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005797 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005798
Jens Axboed625c6e2019-12-17 19:53:05 -07005799 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005800 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005801 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005802 break;
5803 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005804 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005805 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005806 if (sqe) {
5807 ret = io_read_prep(req, sqe, force_nonblock);
5808 if (ret < 0)
5809 break;
5810 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005811 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005812 break;
5813 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005814 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005815 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005816 if (sqe) {
5817 ret = io_write_prep(req, sqe, force_nonblock);
5818 if (ret < 0)
5819 break;
5820 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005821 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005822 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005823 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005824 if (sqe) {
5825 ret = io_prep_fsync(req, sqe);
5826 if (ret < 0)
5827 break;
5828 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005829 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005830 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005831 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005832 if (sqe) {
5833 ret = io_poll_add_prep(req, sqe);
5834 if (ret)
5835 break;
5836 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005837 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005838 break;
5839 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005840 if (sqe) {
5841 ret = io_poll_remove_prep(req, sqe);
5842 if (ret < 0)
5843 break;
5844 }
Jens Axboefc4df992019-12-10 14:38:45 -07005845 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005846 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005847 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005848 if (sqe) {
5849 ret = io_prep_sfr(req, sqe);
5850 if (ret < 0)
5851 break;
5852 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005853 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005854 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005855 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005856 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005857 if (sqe) {
5858 ret = io_sendmsg_prep(req, sqe);
5859 if (ret < 0)
5860 break;
5861 }
Jens Axboefddafac2020-01-04 20:19:44 -07005862 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005863 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005864 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005865 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005866 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005867 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005868 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005869 if (sqe) {
5870 ret = io_recvmsg_prep(req, sqe);
5871 if (ret)
5872 break;
5873 }
Jens Axboefddafac2020-01-04 20:19:44 -07005874 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005875 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005876 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005877 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005878 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005879 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005880 if (sqe) {
5881 ret = io_timeout_prep(req, sqe, false);
5882 if (ret)
5883 break;
5884 }
Jens Axboefc4df992019-12-10 14:38:45 -07005885 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005886 break;
Jens Axboe11365042019-10-16 09:08:32 -06005887 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005888 if (sqe) {
5889 ret = io_timeout_remove_prep(req, sqe);
5890 if (ret)
5891 break;
5892 }
Jens Axboefc4df992019-12-10 14:38:45 -07005893 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005894 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005895 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005896 if (sqe) {
5897 ret = io_accept_prep(req, sqe);
5898 if (ret)
5899 break;
5900 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005901 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005902 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005903 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005904 if (sqe) {
5905 ret = io_connect_prep(req, sqe);
5906 if (ret)
5907 break;
5908 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005909 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005910 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005911 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005912 if (sqe) {
5913 ret = io_async_cancel_prep(req, sqe);
5914 if (ret)
5915 break;
5916 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005917 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005918 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005919 case IORING_OP_FALLOCATE:
5920 if (sqe) {
5921 ret = io_fallocate_prep(req, sqe);
5922 if (ret)
5923 break;
5924 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005925 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005926 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005927 case IORING_OP_OPENAT:
5928 if (sqe) {
5929 ret = io_openat_prep(req, sqe);
5930 if (ret)
5931 break;
5932 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005933 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005934 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005935 case IORING_OP_CLOSE:
5936 if (sqe) {
5937 ret = io_close_prep(req, sqe);
5938 if (ret)
5939 break;
5940 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005941 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005942 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005943 case IORING_OP_FILES_UPDATE:
5944 if (sqe) {
5945 ret = io_files_update_prep(req, sqe);
5946 if (ret)
5947 break;
5948 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005949 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005950 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005951 case IORING_OP_STATX:
5952 if (sqe) {
5953 ret = io_statx_prep(req, sqe);
5954 if (ret)
5955 break;
5956 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005957 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005958 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005959 case IORING_OP_FADVISE:
5960 if (sqe) {
5961 ret = io_fadvise_prep(req, sqe);
5962 if (ret)
5963 break;
5964 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005965 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005966 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005967 case IORING_OP_MADVISE:
5968 if (sqe) {
5969 ret = io_madvise_prep(req, sqe);
5970 if (ret)
5971 break;
5972 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005973 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005974 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005975 case IORING_OP_OPENAT2:
5976 if (sqe) {
5977 ret = io_openat2_prep(req, sqe);
5978 if (ret)
5979 break;
5980 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005981 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005982 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005983 case IORING_OP_EPOLL_CTL:
5984 if (sqe) {
5985 ret = io_epoll_ctl_prep(req, sqe);
5986 if (ret)
5987 break;
5988 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005989 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005990 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005991 case IORING_OP_SPLICE:
5992 if (sqe) {
5993 ret = io_splice_prep(req, sqe);
5994 if (ret < 0)
5995 break;
5996 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005997 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005998 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005999 case IORING_OP_PROVIDE_BUFFERS:
6000 if (sqe) {
6001 ret = io_provide_buffers_prep(req, sqe);
6002 if (ret)
6003 break;
6004 }
Jens Axboe229a7b62020-06-22 10:13:11 -06006005 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006006 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006007 case IORING_OP_REMOVE_BUFFERS:
6008 if (sqe) {
6009 ret = io_remove_buffers_prep(req, sqe);
6010 if (ret)
6011 break;
6012 }
Jens Axboe229a7b62020-06-22 10:13:11 -06006013 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006014 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006015 case IORING_OP_TEE:
6016 if (sqe) {
6017 ret = io_tee_prep(req, sqe);
6018 if (ret < 0)
6019 break;
6020 }
6021 ret = io_tee(req, force_nonblock);
6022 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006023 default:
6024 ret = -EINVAL;
6025 break;
6026 }
6027
6028 if (ret)
6029 return ret;
6030
Jens Axboeb5325762020-05-19 21:20:27 -06006031 /* If the op doesn't have a file, we're not polling for it */
6032 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006033 const bool in_async = io_wq_current_is_worker();
6034
Jens Axboe11ba8202020-01-15 21:51:17 -07006035 /* workqueue context doesn't hold uring_lock, grab it now */
6036 if (in_async)
6037 mutex_lock(&ctx->uring_lock);
6038
Jens Axboe2b188cc2019-01-07 10:46:33 -07006039 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006040
6041 if (in_async)
6042 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006043 }
6044
6045 return 0;
6046}
6047
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006048static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006049{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006050 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006051 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006052 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006053
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006054 timeout = io_prep_linked_timeout(req);
6055 if (timeout)
6056 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006057
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006058 /* if NO_CANCEL is set, we must still run the work */
6059 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6060 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006061 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006062 }
Jens Axboe31b51512019-01-18 22:56:34 -07006063
Jens Axboe561fb042019-10-24 07:25:42 -06006064 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006065 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006066 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006067 /*
6068 * We can get EAGAIN for polled IO even though we're
6069 * forcing a sync submission from here, since we can't
6070 * wait for request slots on the block side.
6071 */
6072 if (ret != -EAGAIN)
6073 break;
6074 cond_resched();
6075 } while (1);
6076 }
Jens Axboe31b51512019-01-18 22:56:34 -07006077
Jens Axboe561fb042019-10-24 07:25:42 -06006078 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006079 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006080 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006081 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006082
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006083 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006084}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006085
Jens Axboe65e19f52019-10-26 07:20:21 -06006086static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6087 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006088{
Jens Axboe65e19f52019-10-26 07:20:21 -06006089 struct fixed_file_table *table;
6090
Jens Axboe05f3fb32019-12-09 11:22:50 -07006091 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006092 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006093}
6094
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006095static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6096 int fd, struct file **out_file, bool fixed)
6097{
6098 struct io_ring_ctx *ctx = req->ctx;
6099 struct file *file;
6100
6101 if (fixed) {
6102 if (unlikely(!ctx->file_data ||
6103 (unsigned) fd >= ctx->nr_user_files))
6104 return -EBADF;
6105 fd = array_index_nospec(fd, ctx->nr_user_files);
6106 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006107 if (file) {
6108 req->fixed_file_refs = ctx->file_data->cur_refs;
6109 percpu_ref_get(req->fixed_file_refs);
6110 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006111 } else {
6112 trace_io_uring_file_get(ctx, fd);
6113 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006114 }
6115
Jens Axboefd2206e2020-06-02 16:40:47 -06006116 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6117 *out_file = file;
6118 return 0;
6119 }
6120 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006121}
6122
Jens Axboe3529d8c2019-12-19 18:24:38 -07006123static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006124 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006125{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006126 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006127
Jens Axboe63ff8222020-05-07 14:56:15 -06006128 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006129 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006130 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006131
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006132 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006133}
6134
Jackie Liua197f662019-11-08 08:09:12 -07006135static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006136{
Jackie Liua197f662019-11-08 08:09:12 -07006137 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006138
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006139 io_req_init_async(req);
6140
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006141 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006142 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006143
Jens Axboe0f212202020-09-13 13:09:39 -06006144 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006145 get_nsproxy(current->nsproxy);
6146 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006147 req->flags |= REQ_F_INFLIGHT;
6148
Jens Axboefcb323c2019-10-24 12:39:47 -06006149 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006150 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006151 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006152 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006153}
6154
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006155static inline int io_prep_work_files(struct io_kiocb *req)
6156{
6157 if (!io_op_defs[req->opcode].file_table)
6158 return 0;
6159 return io_grab_files(req);
6160}
6161
Jens Axboe2665abf2019-11-05 12:40:47 -07006162static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6163{
Jens Axboead8a48a2019-11-15 08:49:11 -07006164 struct io_timeout_data *data = container_of(timer,
6165 struct io_timeout_data, timer);
6166 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006167 struct io_ring_ctx *ctx = req->ctx;
6168 struct io_kiocb *prev = NULL;
6169 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006170
6171 spin_lock_irqsave(&ctx->completion_lock, flags);
6172
6173 /*
6174 * We don't expect the list to be empty, that will only happen if we
6175 * race with the completion of the linked work.
6176 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006177 if (!list_empty(&req->link_list)) {
6178 prev = list_entry(req->link_list.prev, struct io_kiocb,
6179 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006180 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006181 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006182 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6183 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006184 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006185 }
6186
6187 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6188
6189 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006190 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006191 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006192 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006193 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006194 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006195 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006196 return HRTIMER_NORESTART;
6197}
6198
Jens Axboe7271ef32020-08-10 09:55:22 -06006199static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006200{
Jens Axboe76a46e02019-11-10 23:34:16 -07006201 /*
6202 * If the list is now empty, then our linked request finished before
6203 * we got a chance to setup the timer
6204 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006205 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006206 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006207
Jens Axboead8a48a2019-11-15 08:49:11 -07006208 data->timer.function = io_link_timeout_fn;
6209 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6210 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006211 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006212}
6213
6214static void io_queue_linked_timeout(struct io_kiocb *req)
6215{
6216 struct io_ring_ctx *ctx = req->ctx;
6217
6218 spin_lock_irq(&ctx->completion_lock);
6219 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006220 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006221
Jens Axboe2665abf2019-11-05 12:40:47 -07006222 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006223 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006224}
6225
Jens Axboead8a48a2019-11-15 08:49:11 -07006226static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006227{
6228 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006229
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006230 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006231 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006232 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006233 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006234
Pavel Begunkov44932332019-12-05 16:16:35 +03006235 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6236 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006237 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006238 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006239
Jens Axboe76a46e02019-11-10 23:34:16 -07006240 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006241 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006242}
6243
Jens Axboef13fad72020-06-22 09:34:30 -06006244static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6245 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006246{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006247 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006248 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006249 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006250 int ret;
6251
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006252again:
6253 linked_timeout = io_prep_linked_timeout(req);
6254
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006255 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6256 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006257 if (old_creds)
6258 revert_creds(old_creds);
6259 if (old_creds == req->work.creds)
6260 old_creds = NULL; /* restored original creds */
6261 else
6262 old_creds = override_creds(req->work.creds);
6263 }
6264
Jens Axboef13fad72020-06-22 09:34:30 -06006265 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006266
6267 /*
6268 * We async punt it if the file wasn't marked NOWAIT, or if the file
6269 * doesn't support non-blocking read/write attempts
6270 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006271 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006272 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006273punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006274 ret = io_prep_work_files(req);
6275 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006276 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006277 /*
6278 * Queued up for async execution, worker will release
6279 * submit reference when the iocb is actually submitted.
6280 */
6281 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006282 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006283
Pavel Begunkovf063c542020-07-25 14:41:59 +03006284 if (linked_timeout)
6285 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006286 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006287 }
Jens Axboee65ef562019-03-12 10:16:44 -06006288
Pavel Begunkov652532a2020-07-03 22:15:07 +03006289 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006290err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006291 /* un-prep timeout, so it'll be killed as any other linked */
6292 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006293 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006294 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006295 io_req_complete(req, ret);
6296 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006297 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006298
Jens Axboe6c271ce2019-01-10 11:22:30 -07006299 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006300 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006301 if (linked_timeout)
6302 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006303
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006304 if (nxt) {
6305 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006306
6307 if (req->flags & REQ_F_FORCE_ASYNC)
6308 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006309 goto again;
6310 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006311exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006312 if (old_creds)
6313 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314}
6315
Jens Axboef13fad72020-06-22 09:34:30 -06006316static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6317 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006318{
6319 int ret;
6320
Jens Axboe3529d8c2019-12-19 18:24:38 -07006321 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006322 if (ret) {
6323 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006324fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006325 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006326 io_put_req(req);
6327 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006328 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006329 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006330 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006331 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006332 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006333 goto fail_req;
6334 }
6335
Jens Axboece35a472019-12-17 08:04:44 -07006336 /*
6337 * Never try inline submit of IOSQE_ASYNC is set, go straight
6338 * to async execution.
6339 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006340 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006341 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6342 io_queue_async_work(req);
6343 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006344 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006345 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006346}
6347
Jens Axboef13fad72020-06-22 09:34:30 -06006348static inline void io_queue_link_head(struct io_kiocb *req,
6349 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006350{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006351 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006352 io_put_req(req);
6353 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006354 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006355 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006356}
6357
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006358static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006359 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006360{
Jackie Liua197f662019-11-08 08:09:12 -07006361 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006362 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006363
Jens Axboe9e645e112019-05-10 16:07:28 -06006364 /*
6365 * If we already have a head request, queue this one for async
6366 * submittal once the head completes. If we don't have a head but
6367 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6368 * submitted sync once the chain is complete. If none of those
6369 * conditions are true (normal request), then just queue it.
6370 */
6371 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006372 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006373
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006374 /*
6375 * Taking sequential execution of a link, draining both sides
6376 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6377 * requests in the link. So, it drains the head and the
6378 * next after the link request. The last one is done via
6379 * drain_next flag to persist the effect across calls.
6380 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006381 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006382 head->flags |= REQ_F_IO_DRAIN;
6383 ctx->drain_next = 1;
6384 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006385 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006386 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006387 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006388 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006389 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006390 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006391 trace_io_uring_link(ctx, req, head);
6392 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006393
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006394 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006395 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006396 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006397 *link = NULL;
6398 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006399 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006400 if (unlikely(ctx->drain_next)) {
6401 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006402 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006403 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006404 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006405 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006406 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006407
Pavel Begunkov711be032020-01-17 03:57:59 +03006408 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006409 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006410 req->flags |= REQ_F_FAIL_LINK;
6411 *link = req;
6412 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006413 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006414 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006415 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006416
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006417 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006418}
6419
Jens Axboe9a56a232019-01-09 09:06:50 -07006420/*
6421 * Batched submission is done, ensure local IO is flushed out.
6422 */
6423static void io_submit_state_end(struct io_submit_state *state)
6424{
Jens Axboef13fad72020-06-22 09:34:30 -06006425 if (!list_empty(&state->comp.list))
6426 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006427 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006428 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006429 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006430 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006431}
6432
6433/*
6434 * Start submission side cache.
6435 */
6436static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006437 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006438{
6439 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006440 state->comp.nr = 0;
6441 INIT_LIST_HEAD(&state->comp.list);
6442 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006443 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006444 state->file = NULL;
6445 state->ios_left = max_ios;
6446}
6447
Jens Axboe2b188cc2019-01-07 10:46:33 -07006448static void io_commit_sqring(struct io_ring_ctx *ctx)
6449{
Hristo Venev75b28af2019-08-26 17:23:46 +00006450 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006451
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006452 /*
6453 * Ensure any loads from the SQEs are done at this point,
6454 * since once we write the new head, the application could
6455 * write new data to them.
6456 */
6457 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006458}
6459
6460/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006461 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006462 * that is mapped by userspace. This means that care needs to be taken to
6463 * ensure that reads are stable, as we cannot rely on userspace always
6464 * being a good citizen. If members of the sqe are validated and then later
6465 * used, it's important that those reads are done through READ_ONCE() to
6466 * prevent a re-load down the line.
6467 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006468static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006469{
Hristo Venev75b28af2019-08-26 17:23:46 +00006470 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006471 unsigned head;
6472
6473 /*
6474 * The cached sq head (or cq tail) serves two purposes:
6475 *
6476 * 1) allows us to batch the cost of updating the user visible
6477 * head updates.
6478 * 2) allows the kernel side to track the head on its own, even
6479 * though the application is the one updating it.
6480 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006481 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006482 if (likely(head < ctx->sq_entries))
6483 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006484
6485 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006486 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006487 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006488 return NULL;
6489}
6490
6491static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6492{
6493 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006494}
6495
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006496/*
6497 * Check SQE restrictions (opcode and flags).
6498 *
6499 * Returns 'true' if SQE is allowed, 'false' otherwise.
6500 */
6501static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6502 struct io_kiocb *req,
6503 unsigned int sqe_flags)
6504{
6505 if (!ctx->restricted)
6506 return true;
6507
6508 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6509 return false;
6510
6511 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6512 ctx->restrictions.sqe_flags_required)
6513 return false;
6514
6515 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6516 ctx->restrictions.sqe_flags_required))
6517 return false;
6518
6519 return true;
6520}
6521
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006522#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6523 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6524 IOSQE_BUFFER_SELECT)
6525
6526static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6527 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006528 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006529{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006530 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006531 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006532
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006533 req->opcode = READ_ONCE(sqe->opcode);
6534 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006535 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006536 req->file = NULL;
6537 req->ctx = ctx;
6538 req->flags = 0;
6539 /* one is dropped after submission, the other at completion */
6540 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006541 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006542 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006543 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006544 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006545
6546 if (unlikely(req->opcode >= IORING_OP_LAST))
6547 return -EINVAL;
6548
Jens Axboe9d8426a2020-06-16 18:42:49 -06006549 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6550 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006551
6552 sqe_flags = READ_ONCE(sqe->flags);
6553 /* enforce forwards compatibility on users */
6554 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6555 return -EINVAL;
6556
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006557 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6558 return -EACCES;
6559
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006560 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6561 !io_op_defs[req->opcode].buffer_select)
6562 return -EOPNOTSUPP;
6563
6564 id = READ_ONCE(sqe->personality);
6565 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006566 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006567 req->work.creds = idr_find(&ctx->personality_idr, id);
6568 if (unlikely(!req->work.creds))
6569 return -EINVAL;
6570 get_cred(req->work.creds);
6571 }
6572
6573 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006574 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006575
Jens Axboe63ff8222020-05-07 14:56:15 -06006576 if (!io_op_defs[req->opcode].needs_file)
6577 return 0;
6578
6579 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006580}
6581
Jens Axboe0f212202020-09-13 13:09:39 -06006582static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006583{
Jens Axboeac8691c2020-06-01 08:30:41 -06006584 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006585 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006586 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006587
Jens Axboec4a2ed72019-11-21 21:01:26 -07006588 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006589 if (test_bit(0, &ctx->sq_check_overflow)) {
6590 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006591 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006592 return -EBUSY;
6593 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006594
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006595 /* make sure SQ entry isn't read before tail */
6596 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006597
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006598 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6599 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006600
Jens Axboe013538b2020-06-22 09:29:15 -06006601 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006602
6603 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006604 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006605 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006606 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006607
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006608 sqe = io_get_sqe(ctx);
6609 if (unlikely(!sqe)) {
6610 io_consume_sqe(ctx);
6611 break;
6612 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006613 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006614 if (unlikely(!req)) {
6615 if (!submitted)
6616 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006617 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006618 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006619
Jens Axboeac8691c2020-06-01 08:30:41 -06006620 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006621 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006622 /* will complete beyond this point, count as submitted */
6623 submitted++;
6624
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006625 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006626fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006627 io_put_req(req);
6628 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006629 break;
6630 }
6631
Jens Axboe354420f2020-01-08 18:55:15 -07006632 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006633 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006634 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006635 if (err)
6636 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006637 }
6638
Pavel Begunkov9466f432020-01-25 22:34:01 +03006639 if (unlikely(submitted != nr)) {
6640 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6641
6642 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6643 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006644 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006645 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006646 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006647
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006648 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6649 io_commit_sqring(ctx);
6650
Jens Axboe6c271ce2019-01-10 11:22:30 -07006651 return submitted;
6652}
6653
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006654static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6655{
6656 /* Tell userspace we may need a wakeup call */
6657 spin_lock_irq(&ctx->completion_lock);
6658 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6659 spin_unlock_irq(&ctx->completion_lock);
6660}
6661
6662static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6663{
6664 spin_lock_irq(&ctx->completion_lock);
6665 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6666 spin_unlock_irq(&ctx->completion_lock);
6667}
6668
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006669static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6670 int sync, void *key)
6671{
6672 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6673 int ret;
6674
6675 ret = autoremove_wake_function(wqe, mode, sync, key);
6676 if (ret) {
6677 unsigned long flags;
6678
6679 spin_lock_irqsave(&ctx->completion_lock, flags);
6680 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6681 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6682 }
6683 return ret;
6684}
6685
Jens Axboec8d1ba52020-09-14 11:07:26 -06006686enum sq_ret {
6687 SQT_IDLE = 1,
6688 SQT_SPIN = 2,
6689 SQT_DID_WORK = 4,
6690};
6691
6692static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006693 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006694{
6695 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006696 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006697 unsigned int to_submit;
6698 int ret = 0;
6699
6700again:
6701 if (!list_empty(&ctx->iopoll_list)) {
6702 unsigned nr_events = 0;
6703
6704 mutex_lock(&ctx->uring_lock);
6705 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6706 io_do_iopoll(ctx, &nr_events, 0);
6707 mutex_unlock(&ctx->uring_lock);
6708 }
6709
6710 to_submit = io_sqring_entries(ctx);
6711
6712 /*
6713 * If submit got -EBUSY, flag us as needing the application
6714 * to enter the kernel to reap and flush events.
6715 */
6716 if (!to_submit || ret == -EBUSY || need_resched()) {
6717 /*
6718 * Drop cur_mm before scheduling, we can't hold it for
6719 * long periods (or over schedule()). Do this before
6720 * adding ourselves to the waitqueue, as the unuse/drop
6721 * may sleep.
6722 */
6723 io_sq_thread_drop_mm();
6724
6725 /*
6726 * We're polling. If we're within the defined idle
6727 * period, then let us spin without work before going
6728 * to sleep. The exception is if we got EBUSY doing
6729 * more IO, we should wait for the application to
6730 * reap events and wake us up.
6731 */
6732 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6733 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6734 !percpu_ref_is_dying(&ctx->refs)))
6735 return SQT_SPIN;
6736
Jens Axboe534ca6d2020-09-02 13:52:19 -06006737 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006738 TASK_INTERRUPTIBLE);
6739
6740 /*
6741 * While doing polled IO, before going to sleep, we need
6742 * to check if there are new reqs added to iopoll_list,
6743 * it is because reqs may have been punted to io worker
6744 * and will be added to iopoll_list later, hence check
6745 * the iopoll_list again.
6746 */
6747 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6748 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006749 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006750 goto again;
6751 }
6752
Jens Axboec8d1ba52020-09-14 11:07:26 -06006753 to_submit = io_sqring_entries(ctx);
6754 if (!to_submit || ret == -EBUSY)
6755 return SQT_IDLE;
6756 }
6757
Jens Axboe534ca6d2020-09-02 13:52:19 -06006758 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006759 io_ring_clear_wakeup_flag(ctx);
6760
Jens Axboee95eee22020-09-08 09:11:32 -06006761 /* if we're handling multiple rings, cap submit size for fairness */
6762 if (cap_entries && to_submit > 8)
6763 to_submit = 8;
6764
Jens Axboec8d1ba52020-09-14 11:07:26 -06006765 mutex_lock(&ctx->uring_lock);
6766 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6767 ret = io_submit_sqes(ctx, to_submit);
6768 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006769
6770 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6771 wake_up(&ctx->sqo_sq_wait);
6772
Jens Axboec8d1ba52020-09-14 11:07:26 -06006773 return SQT_DID_WORK;
6774}
6775
Jens Axboe69fb2132020-09-14 11:16:23 -06006776static void io_sqd_init_new(struct io_sq_data *sqd)
6777{
6778 struct io_ring_ctx *ctx;
6779
6780 while (!list_empty(&sqd->ctx_new_list)) {
6781 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6782 init_wait(&ctx->sqo_wait_entry);
6783 ctx->sqo_wait_entry.func = io_sq_wake_function;
6784 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6785 complete(&ctx->sq_thread_comp);
6786 }
6787}
6788
Jens Axboe6c271ce2019-01-10 11:22:30 -07006789static int io_sq_thread(void *data)
6790{
Jens Axboe69fb2132020-09-14 11:16:23 -06006791 const struct cred *old_cred = NULL;
6792 struct io_sq_data *sqd = data;
6793 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006794 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006795
Jens Axboec8d1ba52020-09-14 11:07:26 -06006796 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006797 while (!kthread_should_stop()) {
6798 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006799 bool cap_entries;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006800
Jens Axboe69fb2132020-09-14 11:16:23 -06006801 /*
6802 * Any changes to the sqd lists are synchronized through the
6803 * kthread parking. This synchronizes the thread vs users,
6804 * the users are synchronized on the sqd->ctx_lock.
6805 */
6806 if (kthread_should_park())
6807 kthread_parkme();
6808
6809 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6810 io_sqd_init_new(sqd);
6811
Jens Axboee95eee22020-09-08 09:11:32 -06006812 cap_entries = !list_is_singular(&sqd->ctx_list);
6813
Jens Axboe69fb2132020-09-14 11:16:23 -06006814 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6815 if (current->cred != ctx->creds) {
6816 if (old_cred)
6817 revert_creds(old_cred);
6818 old_cred = override_creds(ctx->creds);
6819 }
6820
Jens Axboee95eee22020-09-08 09:11:32 -06006821 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006822
6823 io_sq_thread_drop_mm();
6824 }
6825
6826 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006827 io_run_task_work();
6828 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006829 } else if (ret == SQT_IDLE) {
6830 if (kthread_should_park())
6831 continue;
6832 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6833 io_ring_set_wakeup_flag(ctx);
6834 schedule();
6835 start_jiffies = jiffies;
6836 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6837 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006838 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006839 }
6840
Jens Axboe4c6e2772020-07-01 11:29:10 -06006841 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006842
Jens Axboe69fb2132020-09-14 11:16:23 -06006843 if (old_cred)
6844 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006845
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006846 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006847
Jens Axboe6c271ce2019-01-10 11:22:30 -07006848 return 0;
6849}
6850
Jens Axboebda52162019-09-24 13:47:15 -06006851struct io_wait_queue {
6852 struct wait_queue_entry wq;
6853 struct io_ring_ctx *ctx;
6854 unsigned to_wait;
6855 unsigned nr_timeouts;
6856};
6857
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006858static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006859{
6860 struct io_ring_ctx *ctx = iowq->ctx;
6861
6862 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006863 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006864 * started waiting. For timeouts, we always want to return to userspace,
6865 * regardless of event count.
6866 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006867 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006868 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6869}
6870
6871static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6872 int wake_flags, void *key)
6873{
6874 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6875 wq);
6876
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006877 /* use noflush == true, as we can't safely rely on locking context */
6878 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006879 return -1;
6880
6881 return autoremove_wake_function(curr, mode, wake_flags, key);
6882}
6883
Jens Axboe2b188cc2019-01-07 10:46:33 -07006884/*
6885 * Wait until events become available, if we don't already have some. The
6886 * application must reap them itself, as they reside on the shared cq ring.
6887 */
6888static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6889 const sigset_t __user *sig, size_t sigsz)
6890{
Jens Axboebda52162019-09-24 13:47:15 -06006891 struct io_wait_queue iowq = {
6892 .wq = {
6893 .private = current,
6894 .func = io_wake_function,
6895 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6896 },
6897 .ctx = ctx,
6898 .to_wait = min_events,
6899 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006900 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006901 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006902
Jens Axboeb41e9852020-02-17 09:52:41 -07006903 do {
6904 if (io_cqring_events(ctx, false) >= min_events)
6905 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006906 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006907 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006908 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006909
6910 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006911#ifdef CONFIG_COMPAT
6912 if (in_compat_syscall())
6913 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006914 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006915 else
6916#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006917 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006918
Jens Axboe2b188cc2019-01-07 10:46:33 -07006919 if (ret)
6920 return ret;
6921 }
6922
Jens Axboebda52162019-09-24 13:47:15 -06006923 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006924 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006925 do {
6926 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6927 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006928 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006929 if (io_run_task_work())
6930 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006931 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006932 if (current->jobctl & JOBCTL_TASK_WORK) {
6933 spin_lock_irq(&current->sighand->siglock);
6934 current->jobctl &= ~JOBCTL_TASK_WORK;
6935 recalc_sigpending();
6936 spin_unlock_irq(&current->sighand->siglock);
6937 continue;
6938 }
6939 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006940 break;
6941 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006942 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006943 break;
6944 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006945 } while (1);
6946 finish_wait(&ctx->wait, &iowq.wq);
6947
Jens Axboeb7db41c2020-07-04 08:55:50 -06006948 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006949
Hristo Venev75b28af2019-08-26 17:23:46 +00006950 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006951}
6952
Jens Axboe6b063142019-01-10 22:13:58 -07006953static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6954{
6955#if defined(CONFIG_UNIX)
6956 if (ctx->ring_sock) {
6957 struct sock *sock = ctx->ring_sock->sk;
6958 struct sk_buff *skb;
6959
6960 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6961 kfree_skb(skb);
6962 }
6963#else
6964 int i;
6965
Jens Axboe65e19f52019-10-26 07:20:21 -06006966 for (i = 0; i < ctx->nr_user_files; i++) {
6967 struct file *file;
6968
6969 file = io_file_from_index(ctx, i);
6970 if (file)
6971 fput(file);
6972 }
Jens Axboe6b063142019-01-10 22:13:58 -07006973#endif
6974}
6975
Jens Axboe05f3fb32019-12-09 11:22:50 -07006976static void io_file_ref_kill(struct percpu_ref *ref)
6977{
6978 struct fixed_file_data *data;
6979
6980 data = container_of(ref, struct fixed_file_data, refs);
6981 complete(&data->done);
6982}
6983
Jens Axboe6b063142019-01-10 22:13:58 -07006984static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6985{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006986 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006987 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006988 unsigned nr_tables, i;
6989
Jens Axboe05f3fb32019-12-09 11:22:50 -07006990 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006991 return -ENXIO;
6992
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006993 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006994 if (!list_empty(&data->ref_list))
6995 ref_node = list_first_entry(&data->ref_list,
6996 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006997 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006998 if (ref_node)
6999 percpu_ref_kill(&ref_node->refs);
7000
7001 percpu_ref_kill(&data->refs);
7002
7003 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007004 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07007005 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007006
Jens Axboe6b063142019-01-10 22:13:58 -07007007 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007008 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7009 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007010 kfree(data->table[i].files);
7011 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007012 percpu_ref_exit(&data->refs);
7013 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007014 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007015 ctx->nr_user_files = 0;
7016 return 0;
7017}
7018
Jens Axboe534ca6d2020-09-02 13:52:19 -06007019static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007020{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007021 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007022 /*
7023 * The park is a bit of a work-around, without it we get
7024 * warning spews on shutdown with SQPOLL set and affinity
7025 * set to a single CPU.
7026 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007027 if (sqd->thread) {
7028 kthread_park(sqd->thread);
7029 kthread_stop(sqd->thread);
7030 }
7031
7032 kfree(sqd);
7033 }
7034}
7035
Jens Axboeaa061652020-09-02 14:50:27 -06007036static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7037{
7038 struct io_ring_ctx *ctx_attach;
7039 struct io_sq_data *sqd;
7040 struct fd f;
7041
7042 f = fdget(p->wq_fd);
7043 if (!f.file)
7044 return ERR_PTR(-ENXIO);
7045 if (f.file->f_op != &io_uring_fops) {
7046 fdput(f);
7047 return ERR_PTR(-EINVAL);
7048 }
7049
7050 ctx_attach = f.file->private_data;
7051 sqd = ctx_attach->sq_data;
7052 if (!sqd) {
7053 fdput(f);
7054 return ERR_PTR(-EINVAL);
7055 }
7056
7057 refcount_inc(&sqd->refs);
7058 fdput(f);
7059 return sqd;
7060}
7061
Jens Axboe534ca6d2020-09-02 13:52:19 -06007062static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7063{
7064 struct io_sq_data *sqd;
7065
Jens Axboeaa061652020-09-02 14:50:27 -06007066 if (p->flags & IORING_SETUP_ATTACH_WQ)
7067 return io_attach_sq_data(p);
7068
Jens Axboe534ca6d2020-09-02 13:52:19 -06007069 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7070 if (!sqd)
7071 return ERR_PTR(-ENOMEM);
7072
7073 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007074 INIT_LIST_HEAD(&sqd->ctx_list);
7075 INIT_LIST_HEAD(&sqd->ctx_new_list);
7076 mutex_init(&sqd->ctx_lock);
7077 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007078 init_waitqueue_head(&sqd->wait);
7079 return sqd;
7080}
7081
Jens Axboe69fb2132020-09-14 11:16:23 -06007082static void io_sq_thread_unpark(struct io_sq_data *sqd)
7083 __releases(&sqd->lock)
7084{
7085 if (!sqd->thread)
7086 return;
7087 kthread_unpark(sqd->thread);
7088 mutex_unlock(&sqd->lock);
7089}
7090
7091static void io_sq_thread_park(struct io_sq_data *sqd)
7092 __acquires(&sqd->lock)
7093{
7094 if (!sqd->thread)
7095 return;
7096 mutex_lock(&sqd->lock);
7097 kthread_park(sqd->thread);
7098}
7099
Jens Axboe534ca6d2020-09-02 13:52:19 -06007100static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7101{
7102 struct io_sq_data *sqd = ctx->sq_data;
7103
7104 if (sqd) {
7105 if (sqd->thread) {
7106 /*
7107 * We may arrive here from the error branch in
7108 * io_sq_offload_create() where the kthread is created
7109 * without being waked up, thus wake it up now to make
7110 * sure the wait will complete.
7111 */
7112 wake_up_process(sqd->thread);
7113 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007114
7115 io_sq_thread_park(sqd);
7116 }
7117
7118 mutex_lock(&sqd->ctx_lock);
7119 list_del(&ctx->sqd_list);
7120 mutex_unlock(&sqd->ctx_lock);
7121
7122 if (sqd->thread) {
7123 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7124 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007125 }
7126
7127 io_put_sq_data(sqd);
7128 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007129 }
7130}
7131
Jens Axboe6b063142019-01-10 22:13:58 -07007132static void io_finish_async(struct io_ring_ctx *ctx)
7133{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007134 io_sq_thread_stop(ctx);
7135
Jens Axboe561fb042019-10-24 07:25:42 -06007136 if (ctx->io_wq) {
7137 io_wq_destroy(ctx->io_wq);
7138 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007139 }
7140}
7141
7142#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007143/*
7144 * Ensure the UNIX gc is aware of our file set, so we are certain that
7145 * the io_uring can be safely unregistered on process exit, even if we have
7146 * loops in the file referencing.
7147 */
7148static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7149{
7150 struct sock *sk = ctx->ring_sock->sk;
7151 struct scm_fp_list *fpl;
7152 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007153 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007154
Jens Axboe6b063142019-01-10 22:13:58 -07007155 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7156 if (!fpl)
7157 return -ENOMEM;
7158
7159 skb = alloc_skb(0, GFP_KERNEL);
7160 if (!skb) {
7161 kfree(fpl);
7162 return -ENOMEM;
7163 }
7164
7165 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007166
Jens Axboe08a45172019-10-03 08:11:03 -06007167 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007168 fpl->user = get_uid(ctx->user);
7169 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007170 struct file *file = io_file_from_index(ctx, i + offset);
7171
7172 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007173 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007174 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007175 unix_inflight(fpl->user, fpl->fp[nr_files]);
7176 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007177 }
7178
Jens Axboe08a45172019-10-03 08:11:03 -06007179 if (nr_files) {
7180 fpl->max = SCM_MAX_FD;
7181 fpl->count = nr_files;
7182 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007183 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007184 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7185 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007186
Jens Axboe08a45172019-10-03 08:11:03 -06007187 for (i = 0; i < nr_files; i++)
7188 fput(fpl->fp[i]);
7189 } else {
7190 kfree_skb(skb);
7191 kfree(fpl);
7192 }
Jens Axboe6b063142019-01-10 22:13:58 -07007193
7194 return 0;
7195}
7196
7197/*
7198 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7199 * causes regular reference counting to break down. We rely on the UNIX
7200 * garbage collection to take care of this problem for us.
7201 */
7202static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7203{
7204 unsigned left, total;
7205 int ret = 0;
7206
7207 total = 0;
7208 left = ctx->nr_user_files;
7209 while (left) {
7210 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007211
7212 ret = __io_sqe_files_scm(ctx, this_files, total);
7213 if (ret)
7214 break;
7215 left -= this_files;
7216 total += this_files;
7217 }
7218
7219 if (!ret)
7220 return 0;
7221
7222 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007223 struct file *file = io_file_from_index(ctx, total);
7224
7225 if (file)
7226 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007227 total++;
7228 }
7229
7230 return ret;
7231}
7232#else
7233static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7234{
7235 return 0;
7236}
7237#endif
7238
Jens Axboe65e19f52019-10-26 07:20:21 -06007239static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7240 unsigned nr_files)
7241{
7242 int i;
7243
7244 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007245 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007246 unsigned this_files;
7247
7248 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7249 table->files = kcalloc(this_files, sizeof(struct file *),
7250 GFP_KERNEL);
7251 if (!table->files)
7252 break;
7253 nr_files -= this_files;
7254 }
7255
7256 if (i == nr_tables)
7257 return 0;
7258
7259 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007260 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007261 kfree(table->files);
7262 }
7263 return 1;
7264}
7265
Jens Axboe05f3fb32019-12-09 11:22:50 -07007266static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007267{
7268#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007269 struct sock *sock = ctx->ring_sock->sk;
7270 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7271 struct sk_buff *skb;
7272 int i;
7273
7274 __skb_queue_head_init(&list);
7275
7276 /*
7277 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7278 * remove this entry and rearrange the file array.
7279 */
7280 skb = skb_dequeue(head);
7281 while (skb) {
7282 struct scm_fp_list *fp;
7283
7284 fp = UNIXCB(skb).fp;
7285 for (i = 0; i < fp->count; i++) {
7286 int left;
7287
7288 if (fp->fp[i] != file)
7289 continue;
7290
7291 unix_notinflight(fp->user, fp->fp[i]);
7292 left = fp->count - 1 - i;
7293 if (left) {
7294 memmove(&fp->fp[i], &fp->fp[i + 1],
7295 left * sizeof(struct file *));
7296 }
7297 fp->count--;
7298 if (!fp->count) {
7299 kfree_skb(skb);
7300 skb = NULL;
7301 } else {
7302 __skb_queue_tail(&list, skb);
7303 }
7304 fput(file);
7305 file = NULL;
7306 break;
7307 }
7308
7309 if (!file)
7310 break;
7311
7312 __skb_queue_tail(&list, skb);
7313
7314 skb = skb_dequeue(head);
7315 }
7316
7317 if (skb_peek(&list)) {
7318 spin_lock_irq(&head->lock);
7319 while ((skb = __skb_dequeue(&list)) != NULL)
7320 __skb_queue_tail(head, skb);
7321 spin_unlock_irq(&head->lock);
7322 }
7323#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007324 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007325#endif
7326}
7327
Jens Axboe05f3fb32019-12-09 11:22:50 -07007328struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007329 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007330 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007331};
7332
Jens Axboe4a38aed22020-05-14 17:21:15 -06007333static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007334{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007335 struct fixed_file_data *file_data = ref_node->file_data;
7336 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007337 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007338
7339 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007340 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007341 io_ring_file_put(ctx, pfile->file);
7342 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007343 }
7344
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007345 spin_lock(&file_data->lock);
7346 list_del(&ref_node->node);
7347 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007348
Xiaoguang Wang05589552020-03-31 14:05:18 +08007349 percpu_ref_exit(&ref_node->refs);
7350 kfree(ref_node);
7351 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007352}
7353
Jens Axboe4a38aed22020-05-14 17:21:15 -06007354static void io_file_put_work(struct work_struct *work)
7355{
7356 struct io_ring_ctx *ctx;
7357 struct llist_node *node;
7358
7359 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7360 node = llist_del_all(&ctx->file_put_llist);
7361
7362 while (node) {
7363 struct fixed_file_ref_node *ref_node;
7364 struct llist_node *next = node->next;
7365
7366 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7367 __io_file_put_work(ref_node);
7368 node = next;
7369 }
7370}
7371
Jens Axboe05f3fb32019-12-09 11:22:50 -07007372static void io_file_data_ref_zero(struct percpu_ref *ref)
7373{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007374 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007375 struct io_ring_ctx *ctx;
7376 bool first_add;
7377 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007378
Xiaoguang Wang05589552020-03-31 14:05:18 +08007379 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007380 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007381
Jens Axboe4a38aed22020-05-14 17:21:15 -06007382 if (percpu_ref_is_dying(&ctx->file_data->refs))
7383 delay = 0;
7384
7385 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7386 if (!delay)
7387 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7388 else if (first_add)
7389 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007390}
7391
7392static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7393 struct io_ring_ctx *ctx)
7394{
7395 struct fixed_file_ref_node *ref_node;
7396
7397 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7398 if (!ref_node)
7399 return ERR_PTR(-ENOMEM);
7400
7401 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7402 0, GFP_KERNEL)) {
7403 kfree(ref_node);
7404 return ERR_PTR(-ENOMEM);
7405 }
7406 INIT_LIST_HEAD(&ref_node->node);
7407 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007408 ref_node->file_data = ctx->file_data;
7409 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007410}
7411
7412static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7413{
7414 percpu_ref_exit(&ref_node->refs);
7415 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007416}
7417
7418static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7419 unsigned nr_args)
7420{
7421 __s32 __user *fds = (__s32 __user *) arg;
7422 unsigned nr_tables;
7423 struct file *file;
7424 int fd, ret = 0;
7425 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007426 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007427
7428 if (ctx->file_data)
7429 return -EBUSY;
7430 if (!nr_args)
7431 return -EINVAL;
7432 if (nr_args > IORING_MAX_FIXED_FILES)
7433 return -EMFILE;
7434
7435 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7436 if (!ctx->file_data)
7437 return -ENOMEM;
7438 ctx->file_data->ctx = ctx;
7439 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007440 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007441 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007442
7443 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7444 ctx->file_data->table = kcalloc(nr_tables,
7445 sizeof(struct fixed_file_table),
7446 GFP_KERNEL);
7447 if (!ctx->file_data->table) {
7448 kfree(ctx->file_data);
7449 ctx->file_data = NULL;
7450 return -ENOMEM;
7451 }
7452
Xiaoguang Wang05589552020-03-31 14:05:18 +08007453 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007454 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7455 kfree(ctx->file_data->table);
7456 kfree(ctx->file_data);
7457 ctx->file_data = NULL;
7458 return -ENOMEM;
7459 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007460
7461 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7462 percpu_ref_exit(&ctx->file_data->refs);
7463 kfree(ctx->file_data->table);
7464 kfree(ctx->file_data);
7465 ctx->file_data = NULL;
7466 return -ENOMEM;
7467 }
7468
7469 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7470 struct fixed_file_table *table;
7471 unsigned index;
7472
7473 ret = -EFAULT;
7474 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7475 break;
7476 /* allow sparse sets */
7477 if (fd == -1) {
7478 ret = 0;
7479 continue;
7480 }
7481
7482 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7483 index = i & IORING_FILE_TABLE_MASK;
7484 file = fget(fd);
7485
7486 ret = -EBADF;
7487 if (!file)
7488 break;
7489
7490 /*
7491 * Don't allow io_uring instances to be registered. If UNIX
7492 * isn't enabled, then this causes a reference cycle and this
7493 * instance can never get freed. If UNIX is enabled we'll
7494 * handle it just fine, but there's still no point in allowing
7495 * a ring fd as it doesn't support regular read/write anyway.
7496 */
7497 if (file->f_op == &io_uring_fops) {
7498 fput(file);
7499 break;
7500 }
7501 ret = 0;
7502 table->files[index] = file;
7503 }
7504
7505 if (ret) {
7506 for (i = 0; i < ctx->nr_user_files; i++) {
7507 file = io_file_from_index(ctx, i);
7508 if (file)
7509 fput(file);
7510 }
7511 for (i = 0; i < nr_tables; i++)
7512 kfree(ctx->file_data->table[i].files);
7513
Yang Yingliang667e57d2020-07-10 14:14:20 +00007514 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007515 kfree(ctx->file_data->table);
7516 kfree(ctx->file_data);
7517 ctx->file_data = NULL;
7518 ctx->nr_user_files = 0;
7519 return ret;
7520 }
7521
7522 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007523 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007524 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007525 return ret;
7526 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007527
Xiaoguang Wang05589552020-03-31 14:05:18 +08007528 ref_node = alloc_fixed_file_ref_node(ctx);
7529 if (IS_ERR(ref_node)) {
7530 io_sqe_files_unregister(ctx);
7531 return PTR_ERR(ref_node);
7532 }
7533
7534 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007535 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007536 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007537 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007538 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007539 return ret;
7540}
7541
Jens Axboec3a31e62019-10-03 13:59:56 -06007542static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7543 int index)
7544{
7545#if defined(CONFIG_UNIX)
7546 struct sock *sock = ctx->ring_sock->sk;
7547 struct sk_buff_head *head = &sock->sk_receive_queue;
7548 struct sk_buff *skb;
7549
7550 /*
7551 * See if we can merge this file into an existing skb SCM_RIGHTS
7552 * file set. If there's no room, fall back to allocating a new skb
7553 * and filling it in.
7554 */
7555 spin_lock_irq(&head->lock);
7556 skb = skb_peek(head);
7557 if (skb) {
7558 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7559
7560 if (fpl->count < SCM_MAX_FD) {
7561 __skb_unlink(skb, head);
7562 spin_unlock_irq(&head->lock);
7563 fpl->fp[fpl->count] = get_file(file);
7564 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7565 fpl->count++;
7566 spin_lock_irq(&head->lock);
7567 __skb_queue_head(head, skb);
7568 } else {
7569 skb = NULL;
7570 }
7571 }
7572 spin_unlock_irq(&head->lock);
7573
7574 if (skb) {
7575 fput(file);
7576 return 0;
7577 }
7578
7579 return __io_sqe_files_scm(ctx, 1, index);
7580#else
7581 return 0;
7582#endif
7583}
7584
Hillf Dantona5318d32020-03-23 17:47:15 +08007585static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007586 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007587{
Hillf Dantona5318d32020-03-23 17:47:15 +08007588 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007589 struct percpu_ref *refs = data->cur_refs;
7590 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007591
Jens Axboe05f3fb32019-12-09 11:22:50 -07007592 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007593 if (!pfile)
7594 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007595
Xiaoguang Wang05589552020-03-31 14:05:18 +08007596 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007597 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007598 list_add(&pfile->list, &ref_node->file_list);
7599
Hillf Dantona5318d32020-03-23 17:47:15 +08007600 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007601}
7602
7603static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7604 struct io_uring_files_update *up,
7605 unsigned nr_args)
7606{
7607 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007608 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007609 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007610 __s32 __user *fds;
7611 int fd, i, err;
7612 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007613 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007614
Jens Axboe05f3fb32019-12-09 11:22:50 -07007615 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007616 return -EOVERFLOW;
7617 if (done > ctx->nr_user_files)
7618 return -EINVAL;
7619
Xiaoguang Wang05589552020-03-31 14:05:18 +08007620 ref_node = alloc_fixed_file_ref_node(ctx);
7621 if (IS_ERR(ref_node))
7622 return PTR_ERR(ref_node);
7623
Jens Axboec3a31e62019-10-03 13:59:56 -06007624 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007625 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007626 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007627 struct fixed_file_table *table;
7628 unsigned index;
7629
Jens Axboec3a31e62019-10-03 13:59:56 -06007630 err = 0;
7631 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7632 err = -EFAULT;
7633 break;
7634 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007635 i = array_index_nospec(up->offset, ctx->nr_user_files);
7636 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007637 index = i & IORING_FILE_TABLE_MASK;
7638 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007639 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007640 err = io_queue_file_removal(data, file);
7641 if (err)
7642 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007643 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007644 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007645 }
7646 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007647 file = fget(fd);
7648 if (!file) {
7649 err = -EBADF;
7650 break;
7651 }
7652 /*
7653 * Don't allow io_uring instances to be registered. If
7654 * UNIX isn't enabled, then this causes a reference
7655 * cycle and this instance can never get freed. If UNIX
7656 * is enabled we'll handle it just fine, but there's
7657 * still no point in allowing a ring fd as it doesn't
7658 * support regular read/write anyway.
7659 */
7660 if (file->f_op == &io_uring_fops) {
7661 fput(file);
7662 err = -EBADF;
7663 break;
7664 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007665 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007666 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007667 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007668 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007669 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007670 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007671 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007672 }
7673 nr_args--;
7674 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007675 up->offset++;
7676 }
7677
Xiaoguang Wang05589552020-03-31 14:05:18 +08007678 if (needs_switch) {
7679 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007680 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007681 list_add(&ref_node->node, &data->ref_list);
7682 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007683 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007684 percpu_ref_get(&ctx->file_data->refs);
7685 } else
7686 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007687
7688 return done ? done : err;
7689}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007690
Jens Axboe05f3fb32019-12-09 11:22:50 -07007691static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7692 unsigned nr_args)
7693{
7694 struct io_uring_files_update up;
7695
7696 if (!ctx->file_data)
7697 return -ENXIO;
7698 if (!nr_args)
7699 return -EINVAL;
7700 if (copy_from_user(&up, arg, sizeof(up)))
7701 return -EFAULT;
7702 if (up.resv)
7703 return -EINVAL;
7704
7705 return __io_sqe_files_update(ctx, &up, nr_args);
7706}
Jens Axboec3a31e62019-10-03 13:59:56 -06007707
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007708static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007709{
7710 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7711
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007712 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007713 io_put_req(req);
7714}
7715
Pavel Begunkov24369c22020-01-28 03:15:48 +03007716static int io_init_wq_offload(struct io_ring_ctx *ctx,
7717 struct io_uring_params *p)
7718{
7719 struct io_wq_data data;
7720 struct fd f;
7721 struct io_ring_ctx *ctx_attach;
7722 unsigned int concurrency;
7723 int ret = 0;
7724
7725 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007726 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007727 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007728
7729 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7730 /* Do QD, or 4 * CPUS, whatever is smallest */
7731 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7732
7733 ctx->io_wq = io_wq_create(concurrency, &data);
7734 if (IS_ERR(ctx->io_wq)) {
7735 ret = PTR_ERR(ctx->io_wq);
7736 ctx->io_wq = NULL;
7737 }
7738 return ret;
7739 }
7740
7741 f = fdget(p->wq_fd);
7742 if (!f.file)
7743 return -EBADF;
7744
7745 if (f.file->f_op != &io_uring_fops) {
7746 ret = -EINVAL;
7747 goto out_fput;
7748 }
7749
7750 ctx_attach = f.file->private_data;
7751 /* @io_wq is protected by holding the fd */
7752 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7753 ret = -EINVAL;
7754 goto out_fput;
7755 }
7756
7757 ctx->io_wq = ctx_attach->io_wq;
7758out_fput:
7759 fdput(f);
7760 return ret;
7761}
7762
Jens Axboe0f212202020-09-13 13:09:39 -06007763static int io_uring_alloc_task_context(struct task_struct *task)
7764{
7765 struct io_uring_task *tctx;
7766
7767 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7768 if (unlikely(!tctx))
7769 return -ENOMEM;
7770
7771 xa_init(&tctx->xa);
7772 init_waitqueue_head(&tctx->wait);
7773 tctx->last = NULL;
7774 tctx->in_idle = 0;
7775 atomic_long_set(&tctx->req_issue, 0);
7776 atomic_long_set(&tctx->req_complete, 0);
7777 task->io_uring = tctx;
7778 return 0;
7779}
7780
7781void __io_uring_free(struct task_struct *tsk)
7782{
7783 struct io_uring_task *tctx = tsk->io_uring;
7784
7785 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7786 xa_destroy(&tctx->xa);
7787 kfree(tctx);
7788 tsk->io_uring = NULL;
7789}
7790
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007791static int io_sq_offload_create(struct io_ring_ctx *ctx,
7792 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007793{
7794 int ret;
7795
Jens Axboe6c271ce2019-01-10 11:22:30 -07007796 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007797 struct io_sq_data *sqd;
7798
Jens Axboe3ec482d2019-04-08 10:51:01 -06007799 ret = -EPERM;
7800 if (!capable(CAP_SYS_ADMIN))
7801 goto err;
7802
Jens Axboe534ca6d2020-09-02 13:52:19 -06007803 sqd = io_get_sq_data(p);
7804 if (IS_ERR(sqd)) {
7805 ret = PTR_ERR(sqd);
7806 goto err;
7807 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007808
Jens Axboe534ca6d2020-09-02 13:52:19 -06007809 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007810 io_sq_thread_park(sqd);
7811 mutex_lock(&sqd->ctx_lock);
7812 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7813 mutex_unlock(&sqd->ctx_lock);
7814 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007815
Jens Axboe917257d2019-04-13 09:28:55 -06007816 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7817 if (!ctx->sq_thread_idle)
7818 ctx->sq_thread_idle = HZ;
7819
Jens Axboeaa061652020-09-02 14:50:27 -06007820 if (sqd->thread)
7821 goto done;
7822
Jens Axboe6c271ce2019-01-10 11:22:30 -07007823 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007824 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007825
Jens Axboe917257d2019-04-13 09:28:55 -06007826 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007827 if (cpu >= nr_cpu_ids)
7828 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007829 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007830 goto err;
7831
Jens Axboe69fb2132020-09-14 11:16:23 -06007832 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007833 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007834 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007835 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007836 "io_uring-sq");
7837 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007838 if (IS_ERR(sqd->thread)) {
7839 ret = PTR_ERR(sqd->thread);
7840 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007841 goto err;
7842 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007843 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007844 if (ret)
7845 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007846 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7847 /* Can't have SQ_AFF without SQPOLL */
7848 ret = -EINVAL;
7849 goto err;
7850 }
7851
Jens Axboeaa061652020-09-02 14:50:27 -06007852done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007853 ret = io_init_wq_offload(ctx, p);
7854 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007855 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007856
7857 return 0;
7858err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007859 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007860 return ret;
7861}
7862
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007863static void io_sq_offload_start(struct io_ring_ctx *ctx)
7864{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007865 struct io_sq_data *sqd = ctx->sq_data;
7866
7867 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7868 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007869}
7870
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007871static inline void __io_unaccount_mem(struct user_struct *user,
7872 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007873{
7874 atomic_long_sub(nr_pages, &user->locked_vm);
7875}
7876
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007877static inline int __io_account_mem(struct user_struct *user,
7878 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007879{
7880 unsigned long page_limit, cur_pages, new_pages;
7881
7882 /* Don't allow more pages than we can safely lock */
7883 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7884
7885 do {
7886 cur_pages = atomic_long_read(&user->locked_vm);
7887 new_pages = cur_pages + nr_pages;
7888 if (new_pages > page_limit)
7889 return -ENOMEM;
7890 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7891 new_pages) != cur_pages);
7892
7893 return 0;
7894}
7895
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007896static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7897 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007898{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007899 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007900 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007901
Jens Axboe2aede0e2020-09-14 10:45:53 -06007902 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007903 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007904 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007905 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007906 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007907 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007908}
7909
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007910static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7911 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007912{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007913 int ret;
7914
7915 if (ctx->limit_mem) {
7916 ret = __io_account_mem(ctx->user, nr_pages);
7917 if (ret)
7918 return ret;
7919 }
7920
Jens Axboe2aede0e2020-09-14 10:45:53 -06007921 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007922 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007923 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007924 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007925 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007926 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007927
7928 return 0;
7929}
7930
Jens Axboe2b188cc2019-01-07 10:46:33 -07007931static void io_mem_free(void *ptr)
7932{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007933 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007934
Mark Rutland52e04ef2019-04-30 17:30:21 +01007935 if (!ptr)
7936 return;
7937
7938 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007939 if (put_page_testzero(page))
7940 free_compound_page(page);
7941}
7942
7943static void *io_mem_alloc(size_t size)
7944{
7945 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7946 __GFP_NORETRY;
7947
7948 return (void *) __get_free_pages(gfp_flags, get_order(size));
7949}
7950
Hristo Venev75b28af2019-08-26 17:23:46 +00007951static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7952 size_t *sq_offset)
7953{
7954 struct io_rings *rings;
7955 size_t off, sq_array_size;
7956
7957 off = struct_size(rings, cqes, cq_entries);
7958 if (off == SIZE_MAX)
7959 return SIZE_MAX;
7960
7961#ifdef CONFIG_SMP
7962 off = ALIGN(off, SMP_CACHE_BYTES);
7963 if (off == 0)
7964 return SIZE_MAX;
7965#endif
7966
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007967 if (sq_offset)
7968 *sq_offset = off;
7969
Hristo Venev75b28af2019-08-26 17:23:46 +00007970 sq_array_size = array_size(sizeof(u32), sq_entries);
7971 if (sq_array_size == SIZE_MAX)
7972 return SIZE_MAX;
7973
7974 if (check_add_overflow(off, sq_array_size, &off))
7975 return SIZE_MAX;
7976
Hristo Venev75b28af2019-08-26 17:23:46 +00007977 return off;
7978}
7979
Jens Axboe2b188cc2019-01-07 10:46:33 -07007980static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7981{
Hristo Venev75b28af2019-08-26 17:23:46 +00007982 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007983
Hristo Venev75b28af2019-08-26 17:23:46 +00007984 pages = (size_t)1 << get_order(
7985 rings_size(sq_entries, cq_entries, NULL));
7986 pages += (size_t)1 << get_order(
7987 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007988
Hristo Venev75b28af2019-08-26 17:23:46 +00007989 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007990}
7991
Jens Axboeedafcce2019-01-09 09:16:05 -07007992static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7993{
7994 int i, j;
7995
7996 if (!ctx->user_bufs)
7997 return -ENXIO;
7998
7999 for (i = 0; i < ctx->nr_user_bufs; i++) {
8000 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8001
8002 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008003 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008004
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008005 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008006 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008007 imu->nr_bvecs = 0;
8008 }
8009
8010 kfree(ctx->user_bufs);
8011 ctx->user_bufs = NULL;
8012 ctx->nr_user_bufs = 0;
8013 return 0;
8014}
8015
8016static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8017 void __user *arg, unsigned index)
8018{
8019 struct iovec __user *src;
8020
8021#ifdef CONFIG_COMPAT
8022 if (ctx->compat) {
8023 struct compat_iovec __user *ciovs;
8024 struct compat_iovec ciov;
8025
8026 ciovs = (struct compat_iovec __user *) arg;
8027 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8028 return -EFAULT;
8029
Jens Axboed55e5f52019-12-11 16:12:15 -07008030 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008031 dst->iov_len = ciov.iov_len;
8032 return 0;
8033 }
8034#endif
8035 src = (struct iovec __user *) arg;
8036 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8037 return -EFAULT;
8038 return 0;
8039}
8040
8041static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8042 unsigned nr_args)
8043{
8044 struct vm_area_struct **vmas = NULL;
8045 struct page **pages = NULL;
8046 int i, j, got_pages = 0;
8047 int ret = -EINVAL;
8048
8049 if (ctx->user_bufs)
8050 return -EBUSY;
8051 if (!nr_args || nr_args > UIO_MAXIOV)
8052 return -EINVAL;
8053
8054 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8055 GFP_KERNEL);
8056 if (!ctx->user_bufs)
8057 return -ENOMEM;
8058
8059 for (i = 0; i < nr_args; i++) {
8060 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8061 unsigned long off, start, end, ubuf;
8062 int pret, nr_pages;
8063 struct iovec iov;
8064 size_t size;
8065
8066 ret = io_copy_iov(ctx, &iov, arg, i);
8067 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008068 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008069
8070 /*
8071 * Don't impose further limits on the size and buffer
8072 * constraints here, we'll -EINVAL later when IO is
8073 * submitted if they are wrong.
8074 */
8075 ret = -EFAULT;
8076 if (!iov.iov_base || !iov.iov_len)
8077 goto err;
8078
8079 /* arbitrary limit, but we need something */
8080 if (iov.iov_len > SZ_1G)
8081 goto err;
8082
8083 ubuf = (unsigned long) iov.iov_base;
8084 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8085 start = ubuf >> PAGE_SHIFT;
8086 nr_pages = end - start;
8087
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008088 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008089 if (ret)
8090 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008091
8092 ret = 0;
8093 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008094 kvfree(vmas);
8095 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008096 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008097 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008098 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008099 sizeof(struct vm_area_struct *),
8100 GFP_KERNEL);
8101 if (!pages || !vmas) {
8102 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008103 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008104 goto err;
8105 }
8106 got_pages = nr_pages;
8107 }
8108
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008109 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008110 GFP_KERNEL);
8111 ret = -ENOMEM;
8112 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008113 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008114 goto err;
8115 }
8116
8117 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008118 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008119 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008120 FOLL_WRITE | FOLL_LONGTERM,
8121 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008122 if (pret == nr_pages) {
8123 /* don't support file backed memory */
8124 for (j = 0; j < nr_pages; j++) {
8125 struct vm_area_struct *vma = vmas[j];
8126
8127 if (vma->vm_file &&
8128 !is_file_hugepages(vma->vm_file)) {
8129 ret = -EOPNOTSUPP;
8130 break;
8131 }
8132 }
8133 } else {
8134 ret = pret < 0 ? pret : -EFAULT;
8135 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008136 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008137 if (ret) {
8138 /*
8139 * if we did partial map, or found file backed vmas,
8140 * release any pages we did get
8141 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008142 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008143 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008144 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008145 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008146 goto err;
8147 }
8148
8149 off = ubuf & ~PAGE_MASK;
8150 size = iov.iov_len;
8151 for (j = 0; j < nr_pages; j++) {
8152 size_t vec_len;
8153
8154 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8155 imu->bvec[j].bv_page = pages[j];
8156 imu->bvec[j].bv_len = vec_len;
8157 imu->bvec[j].bv_offset = off;
8158 off = 0;
8159 size -= vec_len;
8160 }
8161 /* store original address for later verification */
8162 imu->ubuf = ubuf;
8163 imu->len = iov.iov_len;
8164 imu->nr_bvecs = nr_pages;
8165
8166 ctx->nr_user_bufs++;
8167 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008168 kvfree(pages);
8169 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008170 return 0;
8171err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008172 kvfree(pages);
8173 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008174 io_sqe_buffer_unregister(ctx);
8175 return ret;
8176}
8177
Jens Axboe9b402842019-04-11 11:45:41 -06008178static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8179{
8180 __s32 __user *fds = arg;
8181 int fd;
8182
8183 if (ctx->cq_ev_fd)
8184 return -EBUSY;
8185
8186 if (copy_from_user(&fd, fds, sizeof(*fds)))
8187 return -EFAULT;
8188
8189 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8190 if (IS_ERR(ctx->cq_ev_fd)) {
8191 int ret = PTR_ERR(ctx->cq_ev_fd);
8192 ctx->cq_ev_fd = NULL;
8193 return ret;
8194 }
8195
8196 return 0;
8197}
8198
8199static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8200{
8201 if (ctx->cq_ev_fd) {
8202 eventfd_ctx_put(ctx->cq_ev_fd);
8203 ctx->cq_ev_fd = NULL;
8204 return 0;
8205 }
8206
8207 return -ENXIO;
8208}
8209
Jens Axboe5a2e7452020-02-23 16:23:11 -07008210static int __io_destroy_buffers(int id, void *p, void *data)
8211{
8212 struct io_ring_ctx *ctx = data;
8213 struct io_buffer *buf = p;
8214
Jens Axboe067524e2020-03-02 16:32:28 -07008215 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008216 return 0;
8217}
8218
8219static void io_destroy_buffers(struct io_ring_ctx *ctx)
8220{
8221 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8222 idr_destroy(&ctx->io_buffer_idr);
8223}
8224
Jens Axboe2b188cc2019-01-07 10:46:33 -07008225static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8226{
Jens Axboe6b063142019-01-10 22:13:58 -07008227 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008228 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008229
8230 if (ctx->sqo_task) {
8231 put_task_struct(ctx->sqo_task);
8232 ctx->sqo_task = NULL;
8233 mmdrop(ctx->mm_account);
8234 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008235 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008236
Jens Axboe6b063142019-01-10 22:13:58 -07008237 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008238 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008239 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008240 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008241
Jens Axboe2b188cc2019-01-07 10:46:33 -07008242#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008243 if (ctx->ring_sock) {
8244 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008245 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008246 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008247#endif
8248
Hristo Venev75b28af2019-08-26 17:23:46 +00008249 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008250 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008251
8252 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008253 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008254 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008255 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008256 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008257 kfree(ctx);
8258}
8259
8260static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8261{
8262 struct io_ring_ctx *ctx = file->private_data;
8263 __poll_t mask = 0;
8264
8265 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008266 /*
8267 * synchronizes with barrier from wq_has_sleeper call in
8268 * io_commit_cqring
8269 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008270 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008271 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008272 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008273 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008274 mask |= EPOLLIN | EPOLLRDNORM;
8275
8276 return mask;
8277}
8278
8279static int io_uring_fasync(int fd, struct file *file, int on)
8280{
8281 struct io_ring_ctx *ctx = file->private_data;
8282
8283 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8284}
8285
Jens Axboe071698e2020-01-28 10:04:42 -07008286static int io_remove_personalities(int id, void *p, void *data)
8287{
8288 struct io_ring_ctx *ctx = data;
8289 const struct cred *cred;
8290
8291 cred = idr_remove(&ctx->personality_idr, id);
8292 if (cred)
8293 put_cred(cred);
8294 return 0;
8295}
8296
Jens Axboe85faa7b2020-04-09 18:14:00 -06008297static void io_ring_exit_work(struct work_struct *work)
8298{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008299 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8300 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008301
Jens Axboe56952e92020-06-17 15:00:04 -06008302 /*
8303 * If we're doing polled IO and end up having requests being
8304 * submitted async (out-of-line), then completions can come in while
8305 * we're waiting for refs to drop. We need to reap these manually,
8306 * as nobody else will be looking for them.
8307 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008308 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008309 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008310 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008311 io_iopoll_try_reap_events(ctx);
8312 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008313 io_ring_ctx_free(ctx);
8314}
8315
Jens Axboe2b188cc2019-01-07 10:46:33 -07008316static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8317{
8318 mutex_lock(&ctx->uring_lock);
8319 percpu_ref_kill(&ctx->refs);
8320 mutex_unlock(&ctx->uring_lock);
8321
Jens Axboef3606e32020-09-22 08:18:24 -06008322 io_kill_timeouts(ctx, NULL);
8323 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008324
8325 if (ctx->io_wq)
8326 io_wq_cancel_all(ctx->io_wq);
8327
Jens Axboe15dff282019-11-13 09:09:23 -07008328 /* if we failed setting up the ctx, we might not have any rings */
8329 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008330 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008331 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008332 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008333
8334 /*
8335 * Do this upfront, so we won't have a grace period where the ring
8336 * is closed but resources aren't reaped yet. This can cause
8337 * spurious failure in setting up a new ring.
8338 */
Jens Axboe760618f2020-07-24 12:53:31 -06008339 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8340 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008341
Jens Axboe85faa7b2020-04-09 18:14:00 -06008342 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008343 /*
8344 * Use system_unbound_wq to avoid spawning tons of event kworkers
8345 * if we're exiting a ton of rings at the same time. It just adds
8346 * noise and overhead, there's no discernable change in runtime
8347 * over using system_wq.
8348 */
8349 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008350}
8351
8352static int io_uring_release(struct inode *inode, struct file *file)
8353{
8354 struct io_ring_ctx *ctx = file->private_data;
8355
8356 file->private_data = NULL;
8357 io_ring_ctx_wait_and_kill(ctx);
8358 return 0;
8359}
8360
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008361static bool io_wq_files_match(struct io_wq_work *work, void *data)
8362{
8363 struct files_struct *files = data;
8364
Jens Axboe0f212202020-09-13 13:09:39 -06008365 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008366}
8367
Jens Axboef254ac02020-08-12 17:33:30 -06008368/*
8369 * Returns true if 'preq' is the link parent of 'req'
8370 */
8371static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8372{
8373 struct io_kiocb *link;
8374
8375 if (!(preq->flags & REQ_F_LINK_HEAD))
8376 return false;
8377
8378 list_for_each_entry(link, &preq->link_list, link_list) {
8379 if (link == req)
8380 return true;
8381 }
8382
8383 return false;
8384}
8385
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008386static bool io_match_link_files(struct io_kiocb *req,
8387 struct files_struct *files)
8388{
8389 struct io_kiocb *link;
8390
8391 if (io_match_files(req, files))
8392 return true;
8393 if (req->flags & REQ_F_LINK_HEAD) {
8394 list_for_each_entry(link, &req->link_list, link_list) {
8395 if (io_match_files(link, files))
8396 return true;
8397 }
8398 }
8399 return false;
8400}
8401
Jens Axboef254ac02020-08-12 17:33:30 -06008402/*
8403 * We're looking to cancel 'req' because it's holding on to our files, but
8404 * 'req' could be a link to another request. See if it is, and cancel that
8405 * parent request if so.
8406 */
8407static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8408{
8409 struct hlist_node *tmp;
8410 struct io_kiocb *preq;
8411 bool found = false;
8412 int i;
8413
8414 spin_lock_irq(&ctx->completion_lock);
8415 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8416 struct hlist_head *list;
8417
8418 list = &ctx->cancel_hash[i];
8419 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8420 found = io_match_link(preq, req);
8421 if (found) {
8422 io_poll_remove_one(preq);
8423 break;
8424 }
8425 }
8426 }
8427 spin_unlock_irq(&ctx->completion_lock);
8428 return found;
8429}
8430
8431static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8432 struct io_kiocb *req)
8433{
8434 struct io_kiocb *preq;
8435 bool found = false;
8436
8437 spin_lock_irq(&ctx->completion_lock);
8438 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8439 found = io_match_link(preq, req);
8440 if (found) {
8441 __io_timeout_cancel(preq);
8442 break;
8443 }
8444 }
8445 spin_unlock_irq(&ctx->completion_lock);
8446 return found;
8447}
8448
Jens Axboeb711d4e2020-08-16 08:23:05 -07008449static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8450{
8451 return io_match_link(container_of(work, struct io_kiocb, work), data);
8452}
8453
8454static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8455{
8456 enum io_wq_cancel cret;
8457
8458 /* cancel this particular work, if it's running */
8459 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8460 if (cret != IO_WQ_CANCEL_NOTFOUND)
8461 return;
8462
8463 /* find links that hold this pending, cancel those */
8464 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8465 if (cret != IO_WQ_CANCEL_NOTFOUND)
8466 return;
8467
8468 /* if we have a poll link holding this pending, cancel that */
8469 if (io_poll_remove_link(ctx, req))
8470 return;
8471
8472 /* final option, timeout link is holding this req pending */
8473 io_timeout_remove_link(ctx, req);
8474}
8475
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008476static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8477 struct files_struct *files)
8478{
8479 struct io_defer_entry *de = NULL;
8480 LIST_HEAD(list);
8481
8482 spin_lock_irq(&ctx->completion_lock);
8483 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008484 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008485 list_cut_position(&list, &ctx->defer_list, &de->list);
8486 break;
8487 }
8488 }
8489 spin_unlock_irq(&ctx->completion_lock);
8490
8491 while (!list_empty(&list)) {
8492 de = list_first_entry(&list, struct io_defer_entry, list);
8493 list_del_init(&de->list);
8494 req_set_fail_links(de->req);
8495 io_put_req(de->req);
8496 io_req_complete(de->req, -ECANCELED);
8497 kfree(de);
8498 }
8499}
8500
Jens Axboe76e1b642020-09-26 15:05:03 -06008501/*
8502 * Returns true if we found and killed one or more files pinning requests
8503 */
8504static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008505 struct files_struct *files)
8506{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008507 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008508 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008509
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008510 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008511 /* cancel all at once, should be faster than doing it one by one*/
8512 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8513
Jens Axboefcb323c2019-10-24 12:39:47 -06008514 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008515 struct io_kiocb *cancel_req = NULL, *req;
8516 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008517
8518 spin_lock_irq(&ctx->inflight_lock);
8519 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008520 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008521 continue;
8522 /* req is being completed, ignore */
8523 if (!refcount_inc_not_zero(&req->refs))
8524 continue;
8525 cancel_req = req;
8526 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008527 }
Jens Axboe768134d2019-11-10 20:30:53 -07008528 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008529 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008530 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008531 spin_unlock_irq(&ctx->inflight_lock);
8532
Jens Axboe768134d2019-11-10 20:30:53 -07008533 /* We need to keep going until we don't find a matching req */
8534 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008535 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008536 /* cancel this request, or head link requests */
8537 io_attempt_cancel(ctx, cancel_req);
8538 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008539 /* cancellations _may_ trigger task work */
8540 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008541 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008542 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008543 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008544
8545 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008546}
8547
Pavel Begunkov801dd572020-06-15 10:33:14 +03008548static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008549{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008550 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8551 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008552
Jens Axboef3606e32020-09-22 08:18:24 -06008553 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008554}
8555
Jens Axboe0f212202020-09-13 13:09:39 -06008556static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8557 struct task_struct *task,
8558 struct files_struct *files)
8559{
8560 bool ret;
8561
8562 ret = io_uring_cancel_files(ctx, files);
8563 if (!files) {
8564 enum io_wq_cancel cret;
8565
8566 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8567 if (cret != IO_WQ_CANCEL_NOTFOUND)
8568 ret = true;
8569
8570 /* SQPOLL thread does its own polling */
8571 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8572 while (!list_empty_careful(&ctx->iopoll_list)) {
8573 io_iopoll_try_reap_events(ctx);
8574 ret = true;
8575 }
8576 }
8577
8578 ret |= io_poll_remove_all(ctx, task);
8579 ret |= io_kill_timeouts(ctx, task);
8580 }
8581
8582 return ret;
8583}
8584
8585/*
8586 * We need to iteratively cancel requests, in case a request has dependent
8587 * hard links. These persist even for failure of cancelations, hence keep
8588 * looping until none are found.
8589 */
8590static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8591 struct files_struct *files)
8592{
8593 struct task_struct *task = current;
8594
Jens Axboe534ca6d2020-09-02 13:52:19 -06008595 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8596 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008597
8598 io_cqring_overflow_flush(ctx, true, task, files);
8599
8600 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8601 io_run_task_work();
8602 cond_resched();
8603 }
8604}
8605
8606/*
8607 * Note that this task has used io_uring. We use it for cancelation purposes.
8608 */
8609static int io_uring_add_task_file(struct file *file)
8610{
8611 if (unlikely(!current->io_uring)) {
8612 int ret;
8613
8614 ret = io_uring_alloc_task_context(current);
8615 if (unlikely(ret))
8616 return ret;
8617 }
8618 if (current->io_uring->last != file) {
8619 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8620 void *old;
8621
8622 rcu_read_lock();
8623 old = xas_load(&xas);
8624 if (old != file) {
8625 get_file(file);
8626 xas_lock(&xas);
8627 xas_store(&xas, file);
8628 xas_unlock(&xas);
8629 }
8630 rcu_read_unlock();
8631 current->io_uring->last = file;
8632 }
8633
8634 return 0;
8635}
8636
8637/*
8638 * Remove this io_uring_file -> task mapping.
8639 */
8640static void io_uring_del_task_file(struct file *file)
8641{
8642 struct io_uring_task *tctx = current->io_uring;
8643 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8644
8645 if (tctx->last == file)
8646 tctx->last = NULL;
8647
8648 xas_lock(&xas);
8649 file = xas_store(&xas, NULL);
8650 xas_unlock(&xas);
8651
8652 if (file)
8653 fput(file);
8654}
8655
8656static void __io_uring_attempt_task_drop(struct file *file)
8657{
8658 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8659 struct file *old;
8660
8661 rcu_read_lock();
8662 old = xas_load(&xas);
8663 rcu_read_unlock();
8664
8665 if (old == file)
8666 io_uring_del_task_file(file);
8667}
8668
8669/*
8670 * Drop task note for this file if we're the only ones that hold it after
8671 * pending fput()
8672 */
8673static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8674{
8675 if (!current->io_uring)
8676 return;
8677 /*
8678 * fput() is pending, will be 2 if the only other ref is our potential
8679 * task file note. If the task is exiting, drop regardless of count.
8680 */
8681 if (!exiting && atomic_long_read(&file->f_count) != 2)
8682 return;
8683
8684 __io_uring_attempt_task_drop(file);
8685}
8686
8687void __io_uring_files_cancel(struct files_struct *files)
8688{
8689 struct io_uring_task *tctx = current->io_uring;
8690 XA_STATE(xas, &tctx->xa, 0);
8691
8692 /* make sure overflow events are dropped */
8693 tctx->in_idle = true;
8694
8695 do {
8696 struct io_ring_ctx *ctx;
8697 struct file *file;
8698
8699 xas_lock(&xas);
8700 file = xas_next_entry(&xas, ULONG_MAX);
8701 xas_unlock(&xas);
8702
8703 if (!file)
8704 break;
8705
8706 ctx = file->private_data;
8707
8708 io_uring_cancel_task_requests(ctx, files);
8709 if (files)
8710 io_uring_del_task_file(file);
8711 } while (1);
8712}
8713
8714static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8715{
8716 return atomic_long_read(&tctx->req_issue) ==
8717 atomic_long_read(&tctx->req_complete);
8718}
8719
8720/*
8721 * Find any io_uring fd that this task has registered or done IO on, and cancel
8722 * requests.
8723 */
8724void __io_uring_task_cancel(void)
8725{
8726 struct io_uring_task *tctx = current->io_uring;
8727 DEFINE_WAIT(wait);
8728 long completions;
8729
8730 /* make sure overflow events are dropped */
8731 tctx->in_idle = true;
8732
8733 while (!io_uring_task_idle(tctx)) {
8734 /* read completions before cancelations */
8735 completions = atomic_long_read(&tctx->req_complete);
8736 __io_uring_files_cancel(NULL);
8737
8738 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8739
8740 /*
8741 * If we've seen completions, retry. This avoids a race where
8742 * a completion comes in before we did prepare_to_wait().
8743 */
8744 if (completions != atomic_long_read(&tctx->req_complete))
8745 continue;
8746 if (io_uring_task_idle(tctx))
8747 break;
8748 schedule();
8749 }
8750
8751 finish_wait(&tctx->wait, &wait);
8752 tctx->in_idle = false;
8753}
8754
Jens Axboefcb323c2019-10-24 12:39:47 -06008755static int io_uring_flush(struct file *file, void *data)
8756{
8757 struct io_ring_ctx *ctx = file->private_data;
8758
Jens Axboe6ab23142020-02-08 20:23:59 -07008759 /*
8760 * If the task is going away, cancel work it may have pending
8761 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008762 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008763 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008764
Jens Axboe0f212202020-09-13 13:09:39 -06008765 io_uring_cancel_task_requests(ctx, data);
8766 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008767 return 0;
8768}
8769
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008770static void *io_uring_validate_mmap_request(struct file *file,
8771 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008772{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008773 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008774 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008775 struct page *page;
8776 void *ptr;
8777
8778 switch (offset) {
8779 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008780 case IORING_OFF_CQ_RING:
8781 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008782 break;
8783 case IORING_OFF_SQES:
8784 ptr = ctx->sq_sqes;
8785 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008786 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008787 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008788 }
8789
8790 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008791 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008792 return ERR_PTR(-EINVAL);
8793
8794 return ptr;
8795}
8796
8797#ifdef CONFIG_MMU
8798
8799static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8800{
8801 size_t sz = vma->vm_end - vma->vm_start;
8802 unsigned long pfn;
8803 void *ptr;
8804
8805 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8806 if (IS_ERR(ptr))
8807 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008808
8809 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8810 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8811}
8812
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008813#else /* !CONFIG_MMU */
8814
8815static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8816{
8817 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8818}
8819
8820static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8821{
8822 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8823}
8824
8825static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8826 unsigned long addr, unsigned long len,
8827 unsigned long pgoff, unsigned long flags)
8828{
8829 void *ptr;
8830
8831 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8832 if (IS_ERR(ptr))
8833 return PTR_ERR(ptr);
8834
8835 return (unsigned long) ptr;
8836}
8837
8838#endif /* !CONFIG_MMU */
8839
Jens Axboe90554202020-09-03 12:12:41 -06008840static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8841{
8842 DEFINE_WAIT(wait);
8843
8844 do {
8845 if (!io_sqring_full(ctx))
8846 break;
8847
8848 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8849
8850 if (!io_sqring_full(ctx))
8851 break;
8852
8853 schedule();
8854 } while (!signal_pending(current));
8855
8856 finish_wait(&ctx->sqo_sq_wait, &wait);
8857}
8858
Jens Axboe2b188cc2019-01-07 10:46:33 -07008859SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8860 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8861 size_t, sigsz)
8862{
8863 struct io_ring_ctx *ctx;
8864 long ret = -EBADF;
8865 int submitted = 0;
8866 struct fd f;
8867
Jens Axboe4c6e2772020-07-01 11:29:10 -06008868 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008869
Jens Axboe90554202020-09-03 12:12:41 -06008870 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8871 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008872 return -EINVAL;
8873
8874 f = fdget(fd);
8875 if (!f.file)
8876 return -EBADF;
8877
8878 ret = -EOPNOTSUPP;
8879 if (f.file->f_op != &io_uring_fops)
8880 goto out_fput;
8881
8882 ret = -ENXIO;
8883 ctx = f.file->private_data;
8884 if (!percpu_ref_tryget(&ctx->refs))
8885 goto out_fput;
8886
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008887 ret = -EBADFD;
8888 if (ctx->flags & IORING_SETUP_R_DISABLED)
8889 goto out;
8890
Jens Axboe6c271ce2019-01-10 11:22:30 -07008891 /*
8892 * For SQ polling, the thread will do all submissions and completions.
8893 * Just return the requested submit count, and wake the thread if
8894 * we were asked to.
8895 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008896 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008897 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008898 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008899 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008900 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008901 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008902 if (flags & IORING_ENTER_SQ_WAIT)
8903 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008904 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008905 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008906 ret = io_uring_add_task_file(f.file);
8907 if (unlikely(ret))
8908 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008909 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008910 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008911 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008912
8913 if (submitted != to_submit)
8914 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008915 }
8916 if (flags & IORING_ENTER_GETEVENTS) {
8917 min_complete = min(min_complete, ctx->cq_entries);
8918
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008919 /*
8920 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8921 * space applications don't need to do io completion events
8922 * polling again, they can rely on io_sq_thread to do polling
8923 * work, which can reduce cpu usage and uring_lock contention.
8924 */
8925 if (ctx->flags & IORING_SETUP_IOPOLL &&
8926 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008927 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008928 } else {
8929 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8930 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008931 }
8932
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008933out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008934 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008935out_fput:
8936 fdput(f);
8937 return submitted ? submitted : ret;
8938}
8939
Tobias Klauserbebdb652020-02-26 18:38:32 +01008940#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008941static int io_uring_show_cred(int id, void *p, void *data)
8942{
8943 const struct cred *cred = p;
8944 struct seq_file *m = data;
8945 struct user_namespace *uns = seq_user_ns(m);
8946 struct group_info *gi;
8947 kernel_cap_t cap;
8948 unsigned __capi;
8949 int g;
8950
8951 seq_printf(m, "%5d\n", id);
8952 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8953 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8954 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8955 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8956 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8957 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8958 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8959 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8960 seq_puts(m, "\n\tGroups:\t");
8961 gi = cred->group_info;
8962 for (g = 0; g < gi->ngroups; g++) {
8963 seq_put_decimal_ull(m, g ? " " : "",
8964 from_kgid_munged(uns, gi->gid[g]));
8965 }
8966 seq_puts(m, "\n\tCapEff:\t");
8967 cap = cred->cap_effective;
8968 CAP_FOR_EACH_U32(__capi)
8969 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8970 seq_putc(m, '\n');
8971 return 0;
8972}
8973
8974static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8975{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008976 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008977 int i;
8978
Jens Axboefad8e0d2020-09-28 08:57:48 -06008979 /*
8980 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8981 * since fdinfo case grabs it in the opposite direction of normal use
8982 * cases. If we fail to get the lock, we just don't iterate any
8983 * structures that could be going away outside the io_uring mutex.
8984 */
8985 has_lock = mutex_trylock(&ctx->uring_lock);
8986
Jens Axboe87ce9552020-01-30 08:25:34 -07008987 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008988 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008989 struct fixed_file_table *table;
8990 struct file *f;
8991
8992 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8993 f = table->files[i & IORING_FILE_TABLE_MASK];
8994 if (f)
8995 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8996 else
8997 seq_printf(m, "%5u: <none>\n", i);
8998 }
8999 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009000 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009001 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9002
9003 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9004 (unsigned int) buf->len);
9005 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009006 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009007 seq_printf(m, "Personalities:\n");
9008 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9009 }
Jens Axboed7718a92020-02-14 22:23:12 -07009010 seq_printf(m, "PollList:\n");
9011 spin_lock_irq(&ctx->completion_lock);
9012 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9013 struct hlist_head *list = &ctx->cancel_hash[i];
9014 struct io_kiocb *req;
9015
9016 hlist_for_each_entry(req, list, hash_node)
9017 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9018 req->task->task_works != NULL);
9019 }
9020 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009021 if (has_lock)
9022 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009023}
9024
9025static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9026{
9027 struct io_ring_ctx *ctx = f->private_data;
9028
9029 if (percpu_ref_tryget(&ctx->refs)) {
9030 __io_uring_show_fdinfo(ctx, m);
9031 percpu_ref_put(&ctx->refs);
9032 }
9033}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009034#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009035
Jens Axboe2b188cc2019-01-07 10:46:33 -07009036static const struct file_operations io_uring_fops = {
9037 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009038 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009039 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009040#ifndef CONFIG_MMU
9041 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9042 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9043#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009044 .poll = io_uring_poll,
9045 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009046#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009047 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009048#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009049};
9050
9051static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9052 struct io_uring_params *p)
9053{
Hristo Venev75b28af2019-08-26 17:23:46 +00009054 struct io_rings *rings;
9055 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009056
Jens Axboebd740482020-08-05 12:58:23 -06009057 /* make sure these are sane, as we already accounted them */
9058 ctx->sq_entries = p->sq_entries;
9059 ctx->cq_entries = p->cq_entries;
9060
Hristo Venev75b28af2019-08-26 17:23:46 +00009061 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9062 if (size == SIZE_MAX)
9063 return -EOVERFLOW;
9064
9065 rings = io_mem_alloc(size);
9066 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009067 return -ENOMEM;
9068
Hristo Venev75b28af2019-08-26 17:23:46 +00009069 ctx->rings = rings;
9070 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9071 rings->sq_ring_mask = p->sq_entries - 1;
9072 rings->cq_ring_mask = p->cq_entries - 1;
9073 rings->sq_ring_entries = p->sq_entries;
9074 rings->cq_ring_entries = p->cq_entries;
9075 ctx->sq_mask = rings->sq_ring_mask;
9076 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009077
9078 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009079 if (size == SIZE_MAX) {
9080 io_mem_free(ctx->rings);
9081 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009082 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009083 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009084
9085 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009086 if (!ctx->sq_sqes) {
9087 io_mem_free(ctx->rings);
9088 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009089 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009090 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009091
Jens Axboe2b188cc2019-01-07 10:46:33 -07009092 return 0;
9093}
9094
9095/*
9096 * Allocate an anonymous fd, this is what constitutes the application
9097 * visible backing of an io_uring instance. The application mmaps this
9098 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9099 * we have to tie this fd to a socket for file garbage collection purposes.
9100 */
9101static int io_uring_get_fd(struct io_ring_ctx *ctx)
9102{
9103 struct file *file;
9104 int ret;
9105
9106#if defined(CONFIG_UNIX)
9107 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9108 &ctx->ring_sock);
9109 if (ret)
9110 return ret;
9111#endif
9112
9113 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9114 if (ret < 0)
9115 goto err;
9116
9117 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9118 O_RDWR | O_CLOEXEC);
9119 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009120err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009121 put_unused_fd(ret);
9122 ret = PTR_ERR(file);
9123 goto err;
9124 }
9125
9126#if defined(CONFIG_UNIX)
9127 ctx->ring_sock->file = file;
9128#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009129 if (unlikely(io_uring_add_task_file(file))) {
9130 file = ERR_PTR(-ENOMEM);
9131 goto err_fd;
9132 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009133 fd_install(ret, file);
9134 return ret;
9135err:
9136#if defined(CONFIG_UNIX)
9137 sock_release(ctx->ring_sock);
9138 ctx->ring_sock = NULL;
9139#endif
9140 return ret;
9141}
9142
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009143static int io_uring_create(unsigned entries, struct io_uring_params *p,
9144 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009145{
9146 struct user_struct *user = NULL;
9147 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009148 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009149 int ret;
9150
Jens Axboe8110c1a2019-12-28 15:39:54 -07009151 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009152 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009153 if (entries > IORING_MAX_ENTRIES) {
9154 if (!(p->flags & IORING_SETUP_CLAMP))
9155 return -EINVAL;
9156 entries = IORING_MAX_ENTRIES;
9157 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009158
9159 /*
9160 * Use twice as many entries for the CQ ring. It's possible for the
9161 * application to drive a higher depth than the size of the SQ ring,
9162 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009163 * some flexibility in overcommitting a bit. If the application has
9164 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9165 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009166 */
9167 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009168 if (p->flags & IORING_SETUP_CQSIZE) {
9169 /*
9170 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9171 * to a power-of-two, if it isn't already. We do NOT impose
9172 * any cq vs sq ring sizing.
9173 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009174 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009175 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009176 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9177 if (!(p->flags & IORING_SETUP_CLAMP))
9178 return -EINVAL;
9179 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9180 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009181 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9182 } else {
9183 p->cq_entries = 2 * p->sq_entries;
9184 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009185
9186 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009187 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009188
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009189 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009190 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009191 ring_pages(p->sq_entries, p->cq_entries));
9192 if (ret) {
9193 free_uid(user);
9194 return ret;
9195 }
9196 }
9197
9198 ctx = io_ring_ctx_alloc(p);
9199 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009200 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009201 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009202 p->cq_entries));
9203 free_uid(user);
9204 return -ENOMEM;
9205 }
9206 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009207 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009208 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009209
Jens Axboe2aede0e2020-09-14 10:45:53 -06009210 ctx->sqo_task = get_task_struct(current);
9211
9212 /*
9213 * This is just grabbed for accounting purposes. When a process exits,
9214 * the mm is exited and dropped before the files, hence we need to hang
9215 * on to this mm purely for the purposes of being able to unaccount
9216 * memory (locked/pinned vm). It's not used for anything else.
9217 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009218 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009219 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009220
Jens Axboef74441e2020-08-05 13:00:44 -06009221 /*
9222 * Account memory _before_ installing the file descriptor. Once
9223 * the descriptor is installed, it can get closed at any time. Also
9224 * do this before hitting the general error path, as ring freeing
9225 * will un-account as well.
9226 */
9227 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9228 ACCT_LOCKED);
9229 ctx->limit_mem = limit_mem;
9230
Jens Axboe2b188cc2019-01-07 10:46:33 -07009231 ret = io_allocate_scq_urings(ctx, p);
9232 if (ret)
9233 goto err;
9234
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009235 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009236 if (ret)
9237 goto err;
9238
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009239 if (!(p->flags & IORING_SETUP_R_DISABLED))
9240 io_sq_offload_start(ctx);
9241
Jens Axboe2b188cc2019-01-07 10:46:33 -07009242 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009243 p->sq_off.head = offsetof(struct io_rings, sq.head);
9244 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9245 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9246 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9247 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9248 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9249 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009250
9251 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009252 p->cq_off.head = offsetof(struct io_rings, cq.head);
9253 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9254 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9255 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9256 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9257 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009258 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009259
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009260 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9261 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009262 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9263 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009264
9265 if (copy_to_user(params, p, sizeof(*p))) {
9266 ret = -EFAULT;
9267 goto err;
9268 }
Jens Axboed1719f72020-07-30 13:43:53 -06009269
9270 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009271 * Install ring fd as the very last thing, so we don't risk someone
9272 * having closed it before we finish setup
9273 */
9274 ret = io_uring_get_fd(ctx);
9275 if (ret < 0)
9276 goto err;
9277
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009278 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009279 return ret;
9280err:
9281 io_ring_ctx_wait_and_kill(ctx);
9282 return ret;
9283}
9284
9285/*
9286 * Sets up an aio uring context, and returns the fd. Applications asks for a
9287 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9288 * params structure passed in.
9289 */
9290static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9291{
9292 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009293 int i;
9294
9295 if (copy_from_user(&p, params, sizeof(p)))
9296 return -EFAULT;
9297 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9298 if (p.resv[i])
9299 return -EINVAL;
9300 }
9301
Jens Axboe6c271ce2019-01-10 11:22:30 -07009302 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009303 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009304 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9305 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009306 return -EINVAL;
9307
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009308 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009309}
9310
9311SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9312 struct io_uring_params __user *, params)
9313{
9314 return io_uring_setup(entries, params);
9315}
9316
Jens Axboe66f4af92020-01-16 15:36:52 -07009317static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9318{
9319 struct io_uring_probe *p;
9320 size_t size;
9321 int i, ret;
9322
9323 size = struct_size(p, ops, nr_args);
9324 if (size == SIZE_MAX)
9325 return -EOVERFLOW;
9326 p = kzalloc(size, GFP_KERNEL);
9327 if (!p)
9328 return -ENOMEM;
9329
9330 ret = -EFAULT;
9331 if (copy_from_user(p, arg, size))
9332 goto out;
9333 ret = -EINVAL;
9334 if (memchr_inv(p, 0, size))
9335 goto out;
9336
9337 p->last_op = IORING_OP_LAST - 1;
9338 if (nr_args > IORING_OP_LAST)
9339 nr_args = IORING_OP_LAST;
9340
9341 for (i = 0; i < nr_args; i++) {
9342 p->ops[i].op = i;
9343 if (!io_op_defs[i].not_supported)
9344 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9345 }
9346 p->ops_len = i;
9347
9348 ret = 0;
9349 if (copy_to_user(arg, p, size))
9350 ret = -EFAULT;
9351out:
9352 kfree(p);
9353 return ret;
9354}
9355
Jens Axboe071698e2020-01-28 10:04:42 -07009356static int io_register_personality(struct io_ring_ctx *ctx)
9357{
9358 const struct cred *creds = get_current_cred();
9359 int id;
9360
9361 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9362 USHRT_MAX, GFP_KERNEL);
9363 if (id < 0)
9364 put_cred(creds);
9365 return id;
9366}
9367
9368static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9369{
9370 const struct cred *old_creds;
9371
9372 old_creds = idr_remove(&ctx->personality_idr, id);
9373 if (old_creds) {
9374 put_cred(old_creds);
9375 return 0;
9376 }
9377
9378 return -EINVAL;
9379}
9380
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009381static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9382 unsigned int nr_args)
9383{
9384 struct io_uring_restriction *res;
9385 size_t size;
9386 int i, ret;
9387
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009388 /* Restrictions allowed only if rings started disabled */
9389 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9390 return -EBADFD;
9391
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009392 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009393 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009394 return -EBUSY;
9395
9396 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9397 return -EINVAL;
9398
9399 size = array_size(nr_args, sizeof(*res));
9400 if (size == SIZE_MAX)
9401 return -EOVERFLOW;
9402
9403 res = memdup_user(arg, size);
9404 if (IS_ERR(res))
9405 return PTR_ERR(res);
9406
9407 ret = 0;
9408
9409 for (i = 0; i < nr_args; i++) {
9410 switch (res[i].opcode) {
9411 case IORING_RESTRICTION_REGISTER_OP:
9412 if (res[i].register_op >= IORING_REGISTER_LAST) {
9413 ret = -EINVAL;
9414 goto out;
9415 }
9416
9417 __set_bit(res[i].register_op,
9418 ctx->restrictions.register_op);
9419 break;
9420 case IORING_RESTRICTION_SQE_OP:
9421 if (res[i].sqe_op >= IORING_OP_LAST) {
9422 ret = -EINVAL;
9423 goto out;
9424 }
9425
9426 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9427 break;
9428 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9429 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9430 break;
9431 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9432 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9433 break;
9434 default:
9435 ret = -EINVAL;
9436 goto out;
9437 }
9438 }
9439
9440out:
9441 /* Reset all restrictions if an error happened */
9442 if (ret != 0)
9443 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9444 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009445 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009446
9447 kfree(res);
9448 return ret;
9449}
9450
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009451static int io_register_enable_rings(struct io_ring_ctx *ctx)
9452{
9453 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9454 return -EBADFD;
9455
9456 if (ctx->restrictions.registered)
9457 ctx->restricted = 1;
9458
9459 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9460
9461 io_sq_offload_start(ctx);
9462
9463 return 0;
9464}
9465
Jens Axboe071698e2020-01-28 10:04:42 -07009466static bool io_register_op_must_quiesce(int op)
9467{
9468 switch (op) {
9469 case IORING_UNREGISTER_FILES:
9470 case IORING_REGISTER_FILES_UPDATE:
9471 case IORING_REGISTER_PROBE:
9472 case IORING_REGISTER_PERSONALITY:
9473 case IORING_UNREGISTER_PERSONALITY:
9474 return false;
9475 default:
9476 return true;
9477 }
9478}
9479
Jens Axboeedafcce2019-01-09 09:16:05 -07009480static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9481 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009482 __releases(ctx->uring_lock)
9483 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009484{
9485 int ret;
9486
Jens Axboe35fa71a2019-04-22 10:23:23 -06009487 /*
9488 * We're inside the ring mutex, if the ref is already dying, then
9489 * someone else killed the ctx or is already going through
9490 * io_uring_register().
9491 */
9492 if (percpu_ref_is_dying(&ctx->refs))
9493 return -ENXIO;
9494
Jens Axboe071698e2020-01-28 10:04:42 -07009495 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009496 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009497
Jens Axboe05f3fb32019-12-09 11:22:50 -07009498 /*
9499 * Drop uring mutex before waiting for references to exit. If
9500 * another thread is currently inside io_uring_enter() it might
9501 * need to grab the uring_lock to make progress. If we hold it
9502 * here across the drain wait, then we can deadlock. It's safe
9503 * to drop the mutex here, since no new references will come in
9504 * after we've killed the percpu ref.
9505 */
9506 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009507 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009508 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009509 if (ret) {
9510 percpu_ref_resurrect(&ctx->refs);
9511 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009512 goto out_quiesce;
9513 }
9514 }
9515
9516 if (ctx->restricted) {
9517 if (opcode >= IORING_REGISTER_LAST) {
9518 ret = -EINVAL;
9519 goto out;
9520 }
9521
9522 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9523 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009524 goto out;
9525 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009526 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009527
9528 switch (opcode) {
9529 case IORING_REGISTER_BUFFERS:
9530 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9531 break;
9532 case IORING_UNREGISTER_BUFFERS:
9533 ret = -EINVAL;
9534 if (arg || nr_args)
9535 break;
9536 ret = io_sqe_buffer_unregister(ctx);
9537 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009538 case IORING_REGISTER_FILES:
9539 ret = io_sqe_files_register(ctx, arg, nr_args);
9540 break;
9541 case IORING_UNREGISTER_FILES:
9542 ret = -EINVAL;
9543 if (arg || nr_args)
9544 break;
9545 ret = io_sqe_files_unregister(ctx);
9546 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009547 case IORING_REGISTER_FILES_UPDATE:
9548 ret = io_sqe_files_update(ctx, arg, nr_args);
9549 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009550 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009551 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009552 ret = -EINVAL;
9553 if (nr_args != 1)
9554 break;
9555 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009556 if (ret)
9557 break;
9558 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9559 ctx->eventfd_async = 1;
9560 else
9561 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009562 break;
9563 case IORING_UNREGISTER_EVENTFD:
9564 ret = -EINVAL;
9565 if (arg || nr_args)
9566 break;
9567 ret = io_eventfd_unregister(ctx);
9568 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009569 case IORING_REGISTER_PROBE:
9570 ret = -EINVAL;
9571 if (!arg || nr_args > 256)
9572 break;
9573 ret = io_probe(ctx, arg, nr_args);
9574 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009575 case IORING_REGISTER_PERSONALITY:
9576 ret = -EINVAL;
9577 if (arg || nr_args)
9578 break;
9579 ret = io_register_personality(ctx);
9580 break;
9581 case IORING_UNREGISTER_PERSONALITY:
9582 ret = -EINVAL;
9583 if (arg)
9584 break;
9585 ret = io_unregister_personality(ctx, nr_args);
9586 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009587 case IORING_REGISTER_ENABLE_RINGS:
9588 ret = -EINVAL;
9589 if (arg || nr_args)
9590 break;
9591 ret = io_register_enable_rings(ctx);
9592 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009593 case IORING_REGISTER_RESTRICTIONS:
9594 ret = io_register_restrictions(ctx, arg, nr_args);
9595 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009596 default:
9597 ret = -EINVAL;
9598 break;
9599 }
9600
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009601out:
Jens Axboe071698e2020-01-28 10:04:42 -07009602 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009603 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009604 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009605out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009606 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009607 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009608 return ret;
9609}
9610
9611SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9612 void __user *, arg, unsigned int, nr_args)
9613{
9614 struct io_ring_ctx *ctx;
9615 long ret = -EBADF;
9616 struct fd f;
9617
9618 f = fdget(fd);
9619 if (!f.file)
9620 return -EBADF;
9621
9622 ret = -EOPNOTSUPP;
9623 if (f.file->f_op != &io_uring_fops)
9624 goto out_fput;
9625
9626 ctx = f.file->private_data;
9627
9628 mutex_lock(&ctx->uring_lock);
9629 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9630 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009631 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9632 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009633out_fput:
9634 fdput(f);
9635 return ret;
9636}
9637
Jens Axboe2b188cc2019-01-07 10:46:33 -07009638static int __init io_uring_init(void)
9639{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009640#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9641 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9642 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9643} while (0)
9644
9645#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9646 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9647 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9648 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9649 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9650 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9651 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9652 BUILD_BUG_SQE_ELEM(8, __u64, off);
9653 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9654 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009655 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009656 BUILD_BUG_SQE_ELEM(24, __u32, len);
9657 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9658 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9659 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9660 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009661 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9662 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009663 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9664 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9665 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9666 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9667 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9668 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9669 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9670 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009671 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009672 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9673 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9674 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009675 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009676
Jens Axboed3656342019-12-18 09:50:26 -07009677 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009678 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009679 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9680 return 0;
9681};
9682__initcall(io_uring_init);