blob: 546bc1fbf48218e4e19e86b5f9b6828501b176ab [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 Axboe6a779382020-09-02 12:21:41 -0600304 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600305 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306
Jens Axboe6b063142019-01-10 22:13:58 -0700307 /*
308 * If used, fixed file set. Writers must ensure that ->refs is dead,
309 * readers must ensure that ->refs is alive as long as the file* is
310 * used. Only updated through io_uring_register(2).
311 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700312 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700313 unsigned nr_user_files;
314
Jens Axboeedafcce2019-01-09 09:16:05 -0700315 /* if used, fixed mapped user buffers */
316 unsigned nr_user_bufs;
317 struct io_mapped_ubuf *user_bufs;
318
Jens Axboe2b188cc2019-01-07 10:46:33 -0700319 struct user_struct *user;
320
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700321 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700322
Jens Axboe0f158b42020-05-14 17:18:39 -0600323 struct completion ref_comp;
324 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700325
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700326 /* if all else fails... */
327 struct io_kiocb *fallback_req;
328
Jens Axboe206aefd2019-11-07 18:27:42 -0700329#if defined(CONFIG_UNIX)
330 struct socket *ring_sock;
331#endif
332
Jens Axboe5a2e7452020-02-23 16:23:11 -0700333 struct idr io_buffer_idr;
334
Jens Axboe071698e2020-01-28 10:04:42 -0700335 struct idr personality_idr;
336
Jens Axboe206aefd2019-11-07 18:27:42 -0700337 struct {
338 unsigned cached_cq_tail;
339 unsigned cq_entries;
340 unsigned cq_mask;
341 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700342 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700343 struct wait_queue_head cq_wait;
344 struct fasync_struct *cq_fasync;
345 struct eventfd_ctx *cq_ev_fd;
346 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700347
348 struct {
349 struct mutex uring_lock;
350 wait_queue_head_t wait;
351 } ____cacheline_aligned_in_smp;
352
353 struct {
354 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700355
Jens Axboedef596e2019-01-09 08:59:42 -0700356 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300357 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700358 * io_uring instances that don't use IORING_SETUP_SQPOLL.
359 * For SQPOLL, only the single threaded io_sq_thread() will
360 * manipulate the list, hence no extra locking is needed there.
361 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300362 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700363 struct hlist_head *cancel_hash;
364 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700365 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600366
367 spinlock_t inflight_lock;
368 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700369 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600370
Jens Axboe4a38aed22020-05-14 17:21:15 -0600371 struct delayed_work file_put_work;
372 struct llist_head file_put_llist;
373
Jens Axboe85faa7b2020-04-09 18:14:00 -0600374 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200375 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376};
377
Jens Axboe09bb8392019-03-13 12:39:28 -0600378/*
379 * First field must be the file pointer in all the
380 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
381 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700382struct io_poll_iocb {
383 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700384 union {
385 struct wait_queue_head *head;
386 u64 addr;
387 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700388 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600389 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700390 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700391 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700392};
393
Jens Axboeb5dba592019-12-11 14:02:38 -0700394struct io_close {
395 struct file *file;
396 struct file *put_file;
397 int fd;
398};
399
Jens Axboead8a48a2019-11-15 08:49:11 -0700400struct io_timeout_data {
401 struct io_kiocb *req;
402 struct hrtimer timer;
403 struct timespec64 ts;
404 enum hrtimer_mode mode;
405};
406
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700407struct io_accept {
408 struct file *file;
409 struct sockaddr __user *addr;
410 int __user *addr_len;
411 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600412 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700413};
414
415struct io_sync {
416 struct file *file;
417 loff_t len;
418 loff_t off;
419 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700420 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700421};
422
Jens Axboefbf23842019-12-17 18:45:56 -0700423struct io_cancel {
424 struct file *file;
425 u64 addr;
426};
427
Jens Axboeb29472e2019-12-17 18:50:29 -0700428struct io_timeout {
429 struct file *file;
430 u64 addr;
431 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300432 u32 off;
433 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300434 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700435};
436
Jens Axboe9adbd452019-12-20 08:45:55 -0700437struct io_rw {
438 /* NOTE: kiocb has the file as the first member, so don't do it here */
439 struct kiocb kiocb;
440 u64 addr;
441 u64 len;
442};
443
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700444struct io_connect {
445 struct file *file;
446 struct sockaddr __user *addr;
447 int addr_len;
448};
449
Jens Axboee47293f2019-12-20 08:58:21 -0700450struct io_sr_msg {
451 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700452 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300453 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700454 void __user *buf;
455 };
Jens Axboee47293f2019-12-20 08:58:21 -0700456 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700457 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700458 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700459 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700460};
461
Jens Axboe15b71ab2019-12-11 11:20:36 -0700462struct io_open {
463 struct file *file;
464 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700465 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700466 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600467 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700468};
469
Jens Axboe05f3fb32019-12-09 11:22:50 -0700470struct io_files_update {
471 struct file *file;
472 u64 arg;
473 u32 nr_args;
474 u32 offset;
475};
476
Jens Axboe4840e412019-12-25 22:03:45 -0700477struct io_fadvise {
478 struct file *file;
479 u64 offset;
480 u32 len;
481 u32 advice;
482};
483
Jens Axboec1ca7572019-12-25 22:18:28 -0700484struct io_madvise {
485 struct file *file;
486 u64 addr;
487 u32 len;
488 u32 advice;
489};
490
Jens Axboe3e4827b2020-01-08 15:18:09 -0700491struct io_epoll {
492 struct file *file;
493 int epfd;
494 int op;
495 int fd;
496 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700497};
498
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300499struct io_splice {
500 struct file *file_out;
501 struct file *file_in;
502 loff_t off_out;
503 loff_t off_in;
504 u64 len;
505 unsigned int flags;
506};
507
Jens Axboeddf0322d2020-02-23 16:41:33 -0700508struct io_provide_buf {
509 struct file *file;
510 __u64 addr;
511 __s32 len;
512 __u32 bgid;
513 __u16 nbufs;
514 __u16 bid;
515};
516
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700517struct io_statx {
518 struct file *file;
519 int dfd;
520 unsigned int mask;
521 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700522 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700523 struct statx __user *buffer;
524};
525
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300526struct io_completion {
527 struct file *file;
528 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300529 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300530};
531
Jens Axboef499a022019-12-02 16:28:46 -0700532struct io_async_connect {
533 struct sockaddr_storage address;
534};
535
Jens Axboe03b12302019-12-02 18:50:25 -0700536struct io_async_msghdr {
537 struct iovec fast_iov[UIO_FASTIOV];
538 struct iovec *iov;
539 struct sockaddr __user *uaddr;
540 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700541 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700542};
543
Jens Axboef67676d2019-12-02 11:03:47 -0700544struct io_async_rw {
545 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600546 const struct iovec *free_iovec;
547 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600548 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600549 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700550};
551
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700552struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700553 union {
554 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700555 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700556 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700557 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700558 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700559};
560
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300561enum {
562 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
563 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
564 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
565 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
566 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700567 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300569 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300570 REQ_F_FAIL_LINK_BIT,
571 REQ_F_INFLIGHT_BIT,
572 REQ_F_CUR_POS_BIT,
573 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300574 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300576 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300577 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700578 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700579 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600580 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800581 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700582
583 /* not a real bit, just to check we're not overflowing the space */
584 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300585};
586
587enum {
588 /* ctx owns file */
589 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
590 /* drain existing IO first */
591 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
592 /* linked sqes */
593 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
594 /* doesn't sever on completion < 0 */
595 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
596 /* IOSQE_ASYNC */
597 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700598 /* IOSQE_BUFFER_SELECT */
599 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300600
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300601 /* head of a link */
602 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300603 /* fail rest of links */
604 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
605 /* on inflight list */
606 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
607 /* read/write uses file position */
608 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
609 /* must not punt to workers */
610 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300611 /* has linked timeout */
612 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300613 /* regular file */
614 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300615 /* completion under lock */
616 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300617 /* needs cleanup */
618 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700619 /* already went through poll handler */
620 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700621 /* buffer already selected */
622 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600623 /* doesn't need file table for this request */
624 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800625 /* io_wq_work is initialized */
626 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700627};
628
629struct async_poll {
630 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600631 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300632};
633
Jens Axboe09bb8392019-03-13 12:39:28 -0600634/*
635 * NOTE! Each of the iocb union members has the file pointer
636 * as the first entry in their struct definition. So you can
637 * access the file pointer through any of the sub-structs,
638 * or directly as just 'ki_filp' in this struct.
639 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700641 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600642 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700643 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700644 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700645 struct io_accept accept;
646 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700647 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700648 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700649 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700650 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700651 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700652 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700653 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700654 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700655 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700656 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300657 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700658 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700659 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300660 /* use only after cleaning per-op data, see io_clean_op() */
661 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700662 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700663
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700664 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700665 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800666 /* polled IO has completed */
667 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700668
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700669 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300670 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700671
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300672 struct io_ring_ctx *ctx;
673 unsigned int flags;
674 refcount_t refs;
675 struct task_struct *task;
676 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700677
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300678 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700679
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300680 /*
681 * 1. used with ctx->iopoll_list with reads/writes
682 * 2. to track reqs with ->files (see io_op_def::file_table)
683 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300684 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600685
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300686 struct percpu_ref *fixed_file_refs;
687 struct callback_head task_work;
688 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
689 struct hlist_node hash_node;
690 struct async_poll *apoll;
691 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700692};
693
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300694struct io_defer_entry {
695 struct list_head list;
696 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300697 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300698};
699
Jens Axboedef596e2019-01-09 08:59:42 -0700700#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700701
Jens Axboe013538b2020-06-22 09:29:15 -0600702struct io_comp_state {
703 unsigned int nr;
704 struct list_head list;
705 struct io_ring_ctx *ctx;
706};
707
Jens Axboe9a56a232019-01-09 09:06:50 -0700708struct io_submit_state {
709 struct blk_plug plug;
710
711 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700712 * io_kiocb alloc cache
713 */
714 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300715 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700716
717 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600718 * Batch completion logic
719 */
720 struct io_comp_state comp;
721
722 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700723 * File reference cache
724 */
725 struct file *file;
726 unsigned int fd;
727 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700728 unsigned int ios_left;
729};
730
Jens Axboed3656342019-12-18 09:50:26 -0700731struct io_op_def {
732 /* needs req->io allocated for deferral/async */
733 unsigned async_ctx : 1;
734 /* needs current->mm setup, does mm access */
735 unsigned needs_mm : 1;
736 /* needs req->file assigned */
737 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600738 /* don't fail if file grab fails */
739 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700740 /* hash wq insertion if file is a regular file */
741 unsigned hash_reg_file : 1;
742 /* unbound wq insertion if file is a non-regular file */
743 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700744 /* opcode is not supported by this kernel */
745 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700746 /* needs file table */
747 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700748 /* needs ->fs */
749 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700750 /* set if opcode supports polled "wait" */
751 unsigned pollin : 1;
752 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700753 /* op supports buffer selection */
754 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300755 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700756};
757
758static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_NOP] = {},
760 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700761 .async_ctx = 1,
762 .needs_mm = 1,
763 .needs_file = 1,
764 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700765 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700766 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700767 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300768 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700769 .async_ctx = 1,
770 .needs_mm = 1,
771 .needs_file = 1,
772 .hash_reg_file = 1,
773 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700774 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300775 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700776 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300777 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .needs_file = 1,
779 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300780 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700781 .needs_file = 1,
782 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700783 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .needs_file = 1,
787 .hash_reg_file = 1,
788 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700789 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300790 .needs_fsize = 1,
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 .async_ctx = 1,
802 .needs_mm = 1,
803 .needs_file = 1,
804 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700805 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700806 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700809 .async_ctx = 1,
810 .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 Axboed3656342019-12-18 09:50:26 -0700816 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300817 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700818 .async_ctx = 1,
819 .needs_mm = 1,
820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_TIMEOUT_REMOVE] = {},
822 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700823 .needs_mm = 1,
824 .needs_file = 1,
825 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700826 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700827 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700828 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300829 [IORING_OP_ASYNC_CANCEL] = {},
830 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700831 .async_ctx = 1,
832 .needs_mm = 1,
833 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700835 .async_ctx = 1,
836 .needs_mm = 1,
837 .needs_file = 1,
838 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700839 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700840 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300841 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700842 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300843 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700846 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700847 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700848 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300849 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600850 .needs_file = 1,
851 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700852 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700855 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700856 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700857 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300858 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700859 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700860 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600861 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700862 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300863 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700864 .needs_mm = 1,
865 .needs_file = 1,
866 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700867 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700868 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700871 .needs_mm = 1,
872 .needs_file = 1,
873 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700874 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300875 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700876 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300877 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700878 .needs_file = 1,
879 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300880 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700881 .needs_mm = 1,
882 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300883 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700884 .needs_mm = 1,
885 .needs_file = 1,
886 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700887 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700888 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300889 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700890 .needs_mm = 1,
891 .needs_file = 1,
892 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700893 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700894 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700895 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700897 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700898 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700899 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700900 [IORING_OP_EPOLL_CTL] = {
901 .unbound_nonreg_file = 1,
902 .file_table = 1,
903 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300904 [IORING_OP_SPLICE] = {
905 .needs_file = 1,
906 .hash_reg_file = 1,
907 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700908 },
909 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700910 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300911 [IORING_OP_TEE] = {
912 .needs_file = 1,
913 .hash_reg_file = 1,
914 .unbound_nonreg_file = 1,
915 },
Jens Axboed3656342019-12-18 09:50:26 -0700916};
917
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700918enum io_mem_account {
919 ACCT_LOCKED,
920 ACCT_PINNED,
921};
922
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300923static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
924 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700925static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800926static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600927static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700928static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700929static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600930static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700931static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700932static int __io_sqe_files_update(struct io_ring_ctx *ctx,
933 struct io_uring_files_update *ip,
934 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300935static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300936static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700937static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
938 int fd, struct file **out_file, bool fixed);
939static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600940 const struct io_uring_sqe *sqe,
941 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600942static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600943
Jens Axboeb63534c2020-06-04 11:28:00 -0600944static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
945 struct iovec **iovec, struct iov_iter *iter,
946 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600947static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
948 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600949 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700950
951static struct kmem_cache *req_cachep;
952
953static const struct file_operations io_uring_fops;
954
955struct sock *io_uring_get_socket(struct file *file)
956{
957#if defined(CONFIG_UNIX)
958 if (file->f_op == &io_uring_fops) {
959 struct io_ring_ctx *ctx = file->private_data;
960
961 return ctx->ring_sock->sk;
962 }
963#endif
964 return NULL;
965}
966EXPORT_SYMBOL(io_uring_get_socket);
967
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300968static inline void io_clean_op(struct io_kiocb *req)
969{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300970 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
971 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300972 __io_clean_op(req);
973}
974
Jens Axboe4349f302020-07-09 15:07:01 -0600975static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600976{
977 struct mm_struct *mm = current->mm;
978
979 if (mm) {
980 kthread_unuse_mm(mm);
981 mmput(mm);
982 }
983}
984
985static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
986{
987 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300988 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600989 !ctx->sqo_task->mm ||
990 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600991 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600992 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600993 }
994
995 return 0;
996}
997
998static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
999 struct io_kiocb *req)
1000{
1001 if (!io_op_defs[req->opcode].needs_mm)
1002 return 0;
1003 return __io_sq_thread_acquire_mm(ctx);
1004}
1005
1006static inline void req_set_fail_links(struct io_kiocb *req)
1007{
1008 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1009 req->flags |= REQ_F_FAIL_LINK;
1010}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001011
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001012/*
1013 * Note: must call io_req_init_async() for the first time you
1014 * touch any members of io_wq_work.
1015 */
1016static inline void io_req_init_async(struct io_kiocb *req)
1017{
1018 if (req->flags & REQ_F_WORK_INITIALIZED)
1019 return;
1020
1021 memset(&req->work, 0, sizeof(req->work));
1022 req->flags |= REQ_F_WORK_INITIALIZED;
1023}
1024
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001025static inline bool io_async_submit(struct io_ring_ctx *ctx)
1026{
1027 return ctx->flags & IORING_SETUP_SQPOLL;
1028}
1029
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1031{
1032 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1033
Jens Axboe0f158b42020-05-14 17:18:39 -06001034 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001035}
1036
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001037static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1038{
1039 return !req->timeout.off;
1040}
1041
Jens Axboe2b188cc2019-01-07 10:46:33 -07001042static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1043{
1044 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001045 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046
1047 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1048 if (!ctx)
1049 return NULL;
1050
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001051 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1052 if (!ctx->fallback_req)
1053 goto err;
1054
Jens Axboe78076bb2019-12-04 19:56:40 -07001055 /*
1056 * Use 5 bits less than the max cq entries, that should give us around
1057 * 32 entries per hash list if totally full and uniformly spread.
1058 */
1059 hash_bits = ilog2(p->cq_entries);
1060 hash_bits -= 5;
1061 if (hash_bits <= 0)
1062 hash_bits = 1;
1063 ctx->cancel_hash_bits = hash_bits;
1064 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1065 GFP_KERNEL);
1066 if (!ctx->cancel_hash)
1067 goto err;
1068 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1069
Roman Gushchin21482892019-05-07 10:01:48 -07001070 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001071 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1072 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073
1074 ctx->flags = p->flags;
Jens Axboe69fb2132020-09-14 11:16:23 -06001075 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001077 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001078 init_completion(&ctx->ref_comp);
1079 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001080 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001081 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082 mutex_init(&ctx->uring_lock);
1083 init_waitqueue_head(&ctx->wait);
1084 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001085 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001086 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001087 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001088 init_waitqueue_head(&ctx->inflight_wait);
1089 spin_lock_init(&ctx->inflight_lock);
1090 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001091 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1092 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001093 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001094err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001095 if (ctx->fallback_req)
1096 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001097 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001098 kfree(ctx);
1099 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100}
1101
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001102static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001103{
Jens Axboe2bc99302020-07-09 09:43:27 -06001104 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1105 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001106
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001107 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001108 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001109 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001110
Bob Liu9d858b22019-11-13 18:06:25 +08001111 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001112}
1113
Jens Axboede0617e2019-04-06 21:51:27 -06001114static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115{
Hristo Venev75b28af2019-08-26 17:23:46 +00001116 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117
Pavel Begunkov07910152020-01-17 03:52:46 +03001118 /* order cqe stores with ring update */
1119 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001120
Pavel Begunkov07910152020-01-17 03:52:46 +03001121 if (wq_has_sleeper(&ctx->cq_wait)) {
1122 wake_up_interruptible(&ctx->cq_wait);
1123 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001124 }
1125}
1126
Jens Axboe51a4cc12020-08-10 10:55:56 -06001127/*
1128 * Returns true if we need to defer file table putting. This can only happen
1129 * from the error path with REQ_F_COMP_LOCKED set.
1130 */
1131static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001132{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001133 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001134 return false;
1135
1136 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001137
Jens Axboecccf0ee2020-01-27 16:34:48 -07001138 if (req->work.mm) {
1139 mmdrop(req->work.mm);
1140 req->work.mm = NULL;
1141 }
1142 if (req->work.creds) {
1143 put_cred(req->work.creds);
1144 req->work.creds = NULL;
1145 }
Jens Axboeff002b32020-02-07 16:05:21 -07001146 if (req->work.fs) {
1147 struct fs_struct *fs = req->work.fs;
1148
Jens Axboe51a4cc12020-08-10 10:55:56 -06001149 if (req->flags & REQ_F_COMP_LOCKED)
1150 return true;
1151
Jens Axboeff002b32020-02-07 16:05:21 -07001152 spin_lock(&req->work.fs->lock);
1153 if (--fs->users)
1154 fs = NULL;
1155 spin_unlock(&req->work.fs->lock);
1156 if (fs)
1157 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001158 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001159 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001160
1161 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001162}
1163
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001164static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001165{
Jens Axboed3656342019-12-18 09:50:26 -07001166 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001167
Pavel Begunkov16d59802020-07-12 16:16:47 +03001168 io_req_init_async(req);
1169
Jens Axboed3656342019-12-18 09:50:26 -07001170 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001171 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001172 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001173 } else {
1174 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001175 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001176 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001177 if (!req->work.mm && def->needs_mm) {
1178 mmgrab(current->mm);
1179 req->work.mm = current->mm;
1180 }
1181 if (!req->work.creds)
1182 req->work.creds = get_current_cred();
1183 if (!req->work.fs && def->needs_fs) {
1184 spin_lock(&current->fs->lock);
1185 if (!current->fs->in_exec) {
1186 req->work.fs = current->fs;
1187 req->work.fs->users++;
1188 } else {
1189 req->work.flags |= IO_WQ_WORK_CANCEL;
1190 }
1191 spin_unlock(&current->fs->lock);
1192 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001193 if (def->needs_fsize)
1194 req->work.fsize = rlimit(RLIMIT_FSIZE);
1195 else
1196 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001197}
1198
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001199static void io_prep_async_link(struct io_kiocb *req)
1200{
1201 struct io_kiocb *cur;
1202
1203 io_prep_async_work(req);
1204 if (req->flags & REQ_F_LINK_HEAD)
1205 list_for_each_entry(cur, &req->link_list, link_list)
1206 io_prep_async_work(cur);
1207}
1208
Jens Axboe7271ef32020-08-10 09:55:22 -06001209static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001210{
Jackie Liua197f662019-11-08 08:09:12 -07001211 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001212 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001213
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001214 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1215 &req->work, req->flags);
1216 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001217 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001218}
1219
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001220static void io_queue_async_work(struct io_kiocb *req)
1221{
Jens Axboe7271ef32020-08-10 09:55:22 -06001222 struct io_kiocb *link;
1223
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001224 /* init ->work of the whole link before punting */
1225 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001226 link = __io_queue_async_work(req);
1227
1228 if (link)
1229 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001230}
1231
Jens Axboe5262f562019-09-17 12:26:57 -06001232static void io_kill_timeout(struct io_kiocb *req)
1233{
1234 int ret;
1235
Jens Axboe2d283902019-12-04 11:08:05 -07001236 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001237 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001238 atomic_set(&req->ctx->cq_timeouts,
1239 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001240 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001241 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001242 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001243 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001244 }
1245}
1246
Jens Axboef3606e32020-09-22 08:18:24 -06001247static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1248{
1249 struct io_ring_ctx *ctx = req->ctx;
1250
1251 if (!tsk || req->task == tsk)
1252 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001253 if (ctx->flags & IORING_SETUP_SQPOLL) {
1254 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1255 return true;
1256 }
Jens Axboef3606e32020-09-22 08:18:24 -06001257 return false;
1258}
1259
Jens Axboe76e1b642020-09-26 15:05:03 -06001260/*
1261 * Returns true if we found and killed one or more timeouts
1262 */
1263static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001264{
1265 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001266 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001267
1268 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001269 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001270 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001271 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001272 canceled++;
1273 }
Jens Axboef3606e32020-09-22 08:18:24 -06001274 }
Jens Axboe5262f562019-09-17 12:26:57 -06001275 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001276 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001277}
1278
Pavel Begunkov04518942020-05-26 20:34:05 +03001279static void __io_queue_deferred(struct io_ring_ctx *ctx)
1280{
1281 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001282 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1283 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001284 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001285
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001286 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001287 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001288 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001289 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001290 link = __io_queue_async_work(de->req);
1291 if (link) {
1292 __io_queue_linked_timeout(link);
1293 /* drop submission reference */
1294 link->flags |= REQ_F_COMP_LOCKED;
1295 io_put_req(link);
1296 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001297 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001298 } while (!list_empty(&ctx->defer_list));
1299}
1300
Pavel Begunkov360428f2020-05-30 14:54:17 +03001301static void io_flush_timeouts(struct io_ring_ctx *ctx)
1302{
1303 while (!list_empty(&ctx->timeout_list)) {
1304 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001305 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001306
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001307 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001308 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001309 if (req->timeout.target_seq != ctx->cached_cq_tail
1310 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001311 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001312
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001313 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001314 io_kill_timeout(req);
1315 }
1316}
1317
Jens Axboede0617e2019-04-06 21:51:27 -06001318static void io_commit_cqring(struct io_ring_ctx *ctx)
1319{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001320 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001321 __io_commit_cqring(ctx);
1322
Pavel Begunkov04518942020-05-26 20:34:05 +03001323 if (unlikely(!list_empty(&ctx->defer_list)))
1324 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001325}
1326
Jens Axboe2b188cc2019-01-07 10:46:33 -07001327static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1328{
Hristo Venev75b28af2019-08-26 17:23:46 +00001329 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330 unsigned tail;
1331
1332 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001333 /*
1334 * writes to the cq entry need to come after reading head; the
1335 * control dependency is enough as we're using WRITE_ONCE to
1336 * fill the cq entry
1337 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001338 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339 return NULL;
1340
1341 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001342 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001343}
1344
Jens Axboef2842ab2020-01-08 11:04:00 -07001345static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1346{
Jens Axboef0b493e2020-02-01 21:30:11 -07001347 if (!ctx->cq_ev_fd)
1348 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001349 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1350 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001351 if (!ctx->eventfd_async)
1352 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001353 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001354}
1355
Jens Axboeb41e9852020-02-17 09:52:41 -07001356static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001357{
1358 if (waitqueue_active(&ctx->wait))
1359 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001360 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1361 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001362 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001363 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001364}
1365
Pavel Begunkov46930142020-07-30 18:43:49 +03001366static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1367{
1368 if (list_empty(&ctx->cq_overflow_list)) {
1369 clear_bit(0, &ctx->sq_check_overflow);
1370 clear_bit(0, &ctx->cq_check_overflow);
1371 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1372 }
1373}
1374
Jens Axboee6c8aa92020-09-28 13:10:13 -06001375static inline bool io_match_files(struct io_kiocb *req,
1376 struct files_struct *files)
1377{
1378 if (!files)
1379 return true;
1380 if (req->flags & REQ_F_WORK_INITIALIZED)
1381 return req->work.files == files;
1382 return false;
1383}
1384
Jens Axboec4a2ed72019-11-21 21:01:26 -07001385/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001386static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1387 struct task_struct *tsk,
1388 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001390 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001391 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001392 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001393 unsigned long flags;
1394 LIST_HEAD(list);
1395
1396 if (!force) {
1397 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001398 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001399 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1400 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001401 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001402 }
1403
1404 spin_lock_irqsave(&ctx->completion_lock, flags);
1405
1406 /* if force is set, the ring is going away. always drop after that */
1407 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001408 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001409
Jens Axboec4a2ed72019-11-21 21:01:26 -07001410 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001411 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1412 if (tsk && req->task != tsk)
1413 continue;
1414 if (!io_match_files(req, files))
1415 continue;
1416
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001417 cqe = io_get_cqring(ctx);
1418 if (!cqe && !force)
1419 break;
1420
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001421 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001422 if (cqe) {
1423 WRITE_ONCE(cqe->user_data, req->user_data);
1424 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001425 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001426 } else {
1427 WRITE_ONCE(ctx->rings->cq_overflow,
1428 atomic_inc_return(&ctx->cached_cq_overflow));
1429 }
1430 }
1431
1432 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001433 io_cqring_mark_overflow(ctx);
1434
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001435 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1436 io_cqring_ev_posted(ctx);
1437
1438 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001439 req = list_first_entry(&list, struct io_kiocb, compl.list);
1440 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001441 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001442 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001443
1444 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001445}
1446
Jens Axboebcda7ba2020-02-23 16:42:51 -07001447static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001448{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001449 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001450 struct io_uring_cqe *cqe;
1451
Jens Axboe78e19bb2019-11-06 15:21:34 -07001452 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001453
Jens Axboe2b188cc2019-01-07 10:46:33 -07001454 /*
1455 * If we can't get a cq entry, userspace overflowed the
1456 * submission (by quite a lot). Increment the overflow count in
1457 * the ring.
1458 */
1459 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001460 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001461 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001462 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001463 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001464 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1465 /*
1466 * If we're in ring overflow flush mode, or in task cancel mode,
1467 * then we cannot store the request for later flushing, we need
1468 * to drop it on the floor.
1469 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001470 WRITE_ONCE(ctx->rings->cq_overflow,
1471 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001472 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001473 if (list_empty(&ctx->cq_overflow_list)) {
1474 set_bit(0, &ctx->sq_check_overflow);
1475 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001476 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001477 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001478 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001479 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001480 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001481 refcount_inc(&req->refs);
1482 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001483 }
1484}
1485
Jens Axboebcda7ba2020-02-23 16:42:51 -07001486static void io_cqring_fill_event(struct io_kiocb *req, long res)
1487{
1488 __io_cqring_fill_event(req, res, 0);
1489}
1490
Jens Axboee1e16092020-06-22 09:17:17 -06001491static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001492{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001493 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001494 unsigned long flags;
1495
1496 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001497 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001498 io_commit_cqring(ctx);
1499 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1500
Jens Axboe8c838782019-03-12 15:48:16 -06001501 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001502}
1503
Jens Axboe229a7b62020-06-22 10:13:11 -06001504static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001505{
Jens Axboe229a7b62020-06-22 10:13:11 -06001506 struct io_ring_ctx *ctx = cs->ctx;
1507
1508 spin_lock_irq(&ctx->completion_lock);
1509 while (!list_empty(&cs->list)) {
1510 struct io_kiocb *req;
1511
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001512 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1513 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001514 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001515 if (!(req->flags & REQ_F_LINK_HEAD)) {
1516 req->flags |= REQ_F_COMP_LOCKED;
1517 io_put_req(req);
1518 } else {
1519 spin_unlock_irq(&ctx->completion_lock);
1520 io_put_req(req);
1521 spin_lock_irq(&ctx->completion_lock);
1522 }
1523 }
1524 io_commit_cqring(ctx);
1525 spin_unlock_irq(&ctx->completion_lock);
1526
1527 io_cqring_ev_posted(ctx);
1528 cs->nr = 0;
1529}
1530
1531static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1532 struct io_comp_state *cs)
1533{
1534 if (!cs) {
1535 io_cqring_add_event(req, res, cflags);
1536 io_put_req(req);
1537 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001538 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001539 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001540 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001541 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001542 if (++cs->nr >= 32)
1543 io_submit_flush_completions(cs);
1544 }
Jens Axboee1e16092020-06-22 09:17:17 -06001545}
1546
1547static void io_req_complete(struct io_kiocb *req, long res)
1548{
Jens Axboe229a7b62020-06-22 10:13:11 -06001549 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001550}
1551
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001552static inline bool io_is_fallback_req(struct io_kiocb *req)
1553{
1554 return req == (struct io_kiocb *)
1555 ((unsigned long) req->ctx->fallback_req & ~1UL);
1556}
1557
1558static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1559{
1560 struct io_kiocb *req;
1561
1562 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001563 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001564 return req;
1565
1566 return NULL;
1567}
1568
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001569static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1570 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001571{
Jens Axboefd6fab22019-03-14 16:30:06 -06001572 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001573 struct io_kiocb *req;
1574
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001575 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001576 size_t sz;
1577 int ret;
1578
1579 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001580 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1581
1582 /*
1583 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1584 * retry single alloc to be on the safe side.
1585 */
1586 if (unlikely(ret <= 0)) {
1587 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1588 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001589 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001590 ret = 1;
1591 }
Jens Axboe2579f912019-01-09 09:10:43 -07001592 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001593 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001594 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001595 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001596 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001597 }
1598
Jens Axboe2579f912019-01-09 09:10:43 -07001599 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001600fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001601 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001602}
1603
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001604static inline void io_put_file(struct io_kiocb *req, struct file *file,
1605 bool fixed)
1606{
1607 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001608 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001609 else
1610 fput(file);
1611}
1612
Jens Axboe51a4cc12020-08-10 10:55:56 -06001613static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001614{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001615 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001616
Jens Axboe5acbbc82020-07-08 15:15:26 -06001617 if (req->io)
1618 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001619 if (req->file)
1620 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001621
Jens Axboe51a4cc12020-08-10 10:55:56 -06001622 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001623}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001624
Jens Axboe51a4cc12020-08-10 10:55:56 -06001625static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001626{
Jens Axboe0f212202020-09-13 13:09:39 -06001627 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001628 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001629
Jens Axboe0f212202020-09-13 13:09:39 -06001630 atomic_long_inc(&tctx->req_complete);
1631 if (tctx->in_idle)
1632 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001633 put_task_struct(req->task);
1634
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001635 if (likely(!io_is_fallback_req(req)))
1636 kmem_cache_free(req_cachep, req);
1637 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001638 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1639 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001640}
1641
Jens Axboe51a4cc12020-08-10 10:55:56 -06001642static void io_req_task_file_table_put(struct callback_head *cb)
1643{
1644 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1645 struct fs_struct *fs = req->work.fs;
1646
1647 spin_lock(&req->work.fs->lock);
1648 if (--fs->users)
1649 fs = NULL;
1650 spin_unlock(&req->work.fs->lock);
1651 if (fs)
1652 free_fs_struct(fs);
1653 req->work.fs = NULL;
1654 __io_free_req_finish(req);
1655}
1656
1657static void __io_free_req(struct io_kiocb *req)
1658{
1659 if (!io_dismantle_req(req)) {
1660 __io_free_req_finish(req);
1661 } else {
1662 int ret;
1663
1664 init_task_work(&req->task_work, io_req_task_file_table_put);
1665 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1666 if (unlikely(ret)) {
1667 struct task_struct *tsk;
1668
1669 tsk = io_wq_get_task(req->ctx->io_wq);
1670 task_work_add(tsk, &req->task_work, 0);
1671 }
1672 }
1673}
1674
Jackie Liua197f662019-11-08 08:09:12 -07001675static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001676{
Jackie Liua197f662019-11-08 08:09:12 -07001677 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001678 int ret;
1679
Jens Axboe2d283902019-12-04 11:08:05 -07001680 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001681 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001682 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001683 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001684 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001685 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001686 return true;
1687 }
1688
1689 return false;
1690}
1691
Jens Axboeab0b6452020-06-30 08:43:15 -06001692static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001693{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001694 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001695 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001696
1697 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001698 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001699 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1700 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001701 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001702
1703 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001704 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001705 wake_ev = io_link_cancel_timeout(link);
1706 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001707 return wake_ev;
1708}
1709
1710static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001711{
Jens Axboe2665abf2019-11-05 12:40:47 -07001712 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001713 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001714
Jens Axboeab0b6452020-06-30 08:43:15 -06001715 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1716 unsigned long flags;
1717
1718 spin_lock_irqsave(&ctx->completion_lock, flags);
1719 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001720 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001721 } else {
1722 wake_ev = __io_kill_linked_timeout(req);
1723 }
1724
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001725 if (wake_ev)
1726 io_cqring_ev_posted(ctx);
1727}
1728
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001729static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001730{
1731 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001732
Jens Axboe9e645e112019-05-10 16:07:28 -06001733 /*
1734 * The list should never be empty when we are called here. But could
1735 * potentially happen if the chain is messed up, check to be on the
1736 * safe side.
1737 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001738 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001739 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001740
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001741 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1742 list_del_init(&req->link_list);
1743 if (!list_empty(&nxt->link_list))
1744 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001745 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001746}
1747
1748/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001749 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001750 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001751static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001752{
Jens Axboe2665abf2019-11-05 12:40:47 -07001753 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001754
1755 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001756 struct io_kiocb *link = list_first_entry(&req->link_list,
1757 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001758
Pavel Begunkov44932332019-12-05 16:16:35 +03001759 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001760 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001761
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001762 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001763 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001764 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001765 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001766 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001767
1768 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001769 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001770}
1771
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001772static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001773{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001774 struct io_ring_ctx *ctx = req->ctx;
1775
1776 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1777 unsigned long flags;
1778
1779 spin_lock_irqsave(&ctx->completion_lock, flags);
1780 __io_fail_links(req);
1781 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1782 } else {
1783 __io_fail_links(req);
1784 }
1785
Jens Axboe9e645e112019-05-10 16:07:28 -06001786 io_cqring_ev_posted(ctx);
1787}
1788
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001789static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001790{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001791 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001792 if (req->flags & REQ_F_LINK_TIMEOUT)
1793 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001794
Jens Axboe9e645e112019-05-10 16:07:28 -06001795 /*
1796 * If LINK is set, we have dependent requests in this chain. If we
1797 * didn't fail this request, queue the first one up, moving any other
1798 * dependencies to the next request. In case of failure, fail the rest
1799 * of the chain.
1800 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001801 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1802 return io_req_link_next(req);
1803 io_fail_links(req);
1804 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001805}
Jens Axboe2665abf2019-11-05 12:40:47 -07001806
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001807static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1808{
1809 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1810 return NULL;
1811 return __io_req_find_next(req);
1812}
1813
Jens Axboefd7d6de2020-08-23 11:00:37 -06001814static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1815 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001816{
1817 struct task_struct *tsk = req->task;
1818 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001819 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001820
Jens Axboe6200b0a2020-09-13 14:38:30 -06001821 if (tsk->flags & PF_EXITING)
1822 return -ESRCH;
1823
Jens Axboec2c4c832020-07-01 15:37:11 -06001824 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001825 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1826 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1827 * processing task_work. There's no reliable way to tell if TWA_RESUME
1828 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001829 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001830 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001831 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001832 notify = TWA_SIGNAL;
1833
1834 ret = task_work_add(tsk, cb, notify);
1835 if (!ret)
1836 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001837
Jens Axboec2c4c832020-07-01 15:37:11 -06001838 return ret;
1839}
1840
Jens Axboec40f6372020-06-25 15:39:59 -06001841static void __io_req_task_cancel(struct io_kiocb *req, int error)
1842{
1843 struct io_ring_ctx *ctx = req->ctx;
1844
1845 spin_lock_irq(&ctx->completion_lock);
1846 io_cqring_fill_event(req, error);
1847 io_commit_cqring(ctx);
1848 spin_unlock_irq(&ctx->completion_lock);
1849
1850 io_cqring_ev_posted(ctx);
1851 req_set_fail_links(req);
1852 io_double_put_req(req);
1853}
1854
1855static void io_req_task_cancel(struct callback_head *cb)
1856{
1857 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001858 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001859
1860 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001861 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001862}
1863
1864static void __io_req_task_submit(struct io_kiocb *req)
1865{
1866 struct io_ring_ctx *ctx = req->ctx;
1867
Jens Axboec40f6372020-06-25 15:39:59 -06001868 if (!__io_sq_thread_acquire_mm(ctx)) {
1869 mutex_lock(&ctx->uring_lock);
1870 __io_queue_sqe(req, NULL, NULL);
1871 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001872 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001873 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001874 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001875}
1876
Jens Axboec40f6372020-06-25 15:39:59 -06001877static void io_req_task_submit(struct callback_head *cb)
1878{
1879 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001880 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001881
1882 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001883 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001884}
1885
1886static void io_req_task_queue(struct io_kiocb *req)
1887{
Jens Axboec40f6372020-06-25 15:39:59 -06001888 int ret;
1889
1890 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001891 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001892
Jens Axboefd7d6de2020-08-23 11:00:37 -06001893 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001894 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001895 struct task_struct *tsk;
1896
Jens Axboec40f6372020-06-25 15:39:59 -06001897 init_task_work(&req->task_work, io_req_task_cancel);
1898 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001899 task_work_add(tsk, &req->task_work, 0);
1900 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001901 }
Jens Axboec40f6372020-06-25 15:39:59 -06001902}
1903
Pavel Begunkovc3524382020-06-28 12:52:32 +03001904static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001905{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001906 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001907
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001908 if (nxt)
1909 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001910}
1911
Jens Axboe9e645e112019-05-10 16:07:28 -06001912static void io_free_req(struct io_kiocb *req)
1913{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001914 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001915 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001916}
1917
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001918struct req_batch {
1919 void *reqs[IO_IOPOLL_BATCH];
1920 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001921
1922 struct task_struct *task;
1923 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001924};
1925
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001926static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001927{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001928 rb->to_free = 0;
1929 rb->task_refs = 0;
1930 rb->task = NULL;
1931}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001932
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001933static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1934 struct req_batch *rb)
1935{
1936 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1937 percpu_ref_put_many(&ctx->refs, rb->to_free);
1938 rb->to_free = 0;
1939}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001940
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001941static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1942 struct req_batch *rb)
1943{
1944 if (rb->to_free)
1945 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001946 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001947 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001948 put_task_struct_many(rb->task, rb->task_refs);
1949 rb->task = NULL;
1950 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001951}
1952
1953static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1954{
1955 if (unlikely(io_is_fallback_req(req))) {
1956 io_free_req(req);
1957 return;
1958 }
1959 if (req->flags & REQ_F_LINK_HEAD)
1960 io_queue_next(req);
1961
Jens Axboee3bc8e92020-09-24 08:45:57 -06001962 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001963 if (rb->task) {
1964 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001965 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06001966 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001967 rb->task = req->task;
1968 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001969 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001970 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001971
Jens Axboe51a4cc12020-08-10 10:55:56 -06001972 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001973 rb->reqs[rb->to_free++] = req;
1974 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1975 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001976}
1977
Jens Axboeba816ad2019-09-28 11:36:45 -06001978/*
1979 * Drop reference to request, return next in chain (if there is one) if this
1980 * was the last reference to this request.
1981 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001982static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001983{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001984 struct io_kiocb *nxt = NULL;
1985
Jens Axboe2a44f462020-02-25 13:25:41 -07001986 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001987 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001988 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001989 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001990 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001991}
1992
Jens Axboe2b188cc2019-01-07 10:46:33 -07001993static void io_put_req(struct io_kiocb *req)
1994{
Jens Axboedef596e2019-01-09 08:59:42 -07001995 if (refcount_dec_and_test(&req->refs))
1996 io_free_req(req);
1997}
1998
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001999static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002000{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002001 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002002
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002003 /*
2004 * A ref is owned by io-wq in which context we're. So, if that's the
2005 * last one, it's safe to steal next work. False negatives are Ok,
2006 * it just will be re-punted async in io_put_work()
2007 */
2008 if (refcount_read(&req->refs) != 1)
2009 return NULL;
2010
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002011 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002012 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002013}
2014
Jens Axboe978db572019-11-14 22:39:04 -07002015/*
2016 * Must only be used if we don't need to care about links, usually from
2017 * within the completion handling itself.
2018 */
2019static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002020{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002021 /* drop both submit and complete references */
2022 if (refcount_sub_and_test(2, &req->refs))
2023 __io_free_req(req);
2024}
2025
Jens Axboe978db572019-11-14 22:39:04 -07002026static void io_double_put_req(struct io_kiocb *req)
2027{
2028 /* drop both submit and complete references */
2029 if (refcount_sub_and_test(2, &req->refs))
2030 io_free_req(req);
2031}
2032
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002033static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002034{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002035 struct io_rings *rings = ctx->rings;
2036
Jens Axboead3eb2c2019-12-18 17:12:20 -07002037 if (test_bit(0, &ctx->cq_check_overflow)) {
2038 /*
2039 * noflush == true is from the waitqueue handler, just ensure
2040 * we wake up the task, and the next invocation will flush the
2041 * entries. We cannot safely to it from here.
2042 */
2043 if (noflush && !list_empty(&ctx->cq_overflow_list))
2044 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002045
Jens Axboee6c8aa92020-09-28 13:10:13 -06002046 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002047 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002048
Jens Axboea3a0e432019-08-20 11:03:11 -06002049 /* See comment at the top of this file */
2050 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002051 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002052}
2053
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002054static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2055{
2056 struct io_rings *rings = ctx->rings;
2057
2058 /* make sure SQ entry isn't read before tail */
2059 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2060}
2061
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002062static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002063{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002064 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002065
Jens Axboebcda7ba2020-02-23 16:42:51 -07002066 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2067 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002068 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002069 kfree(kbuf);
2070 return cflags;
2071}
2072
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002073static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2074{
2075 struct io_buffer *kbuf;
2076
2077 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2078 return io_put_kbuf(req, kbuf);
2079}
2080
Jens Axboe4c6e2772020-07-01 11:29:10 -06002081static inline bool io_run_task_work(void)
2082{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002083 /*
2084 * Not safe to run on exiting task, and the task_work handling will
2085 * not add work to such a task.
2086 */
2087 if (unlikely(current->flags & PF_EXITING))
2088 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002089 if (current->task_works) {
2090 __set_current_state(TASK_RUNNING);
2091 task_work_run();
2092 return true;
2093 }
2094
2095 return false;
2096}
2097
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002098static void io_iopoll_queue(struct list_head *again)
2099{
2100 struct io_kiocb *req;
2101
2102 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002103 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2104 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002105 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002106 } while (!list_empty(again));
2107}
2108
Jens Axboedef596e2019-01-09 08:59:42 -07002109/*
2110 * Find and free completed poll iocbs
2111 */
2112static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2113 struct list_head *done)
2114{
Jens Axboe8237e042019-12-28 10:48:22 -07002115 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002116 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002117 LIST_HEAD(again);
2118
2119 /* order with ->result store in io_complete_rw_iopoll() */
2120 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002121
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002122 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002123 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002124 int cflags = 0;
2125
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002126 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002127 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002128 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002129 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002130 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002131 continue;
2132 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002133 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002134
Jens Axboebcda7ba2020-02-23 16:42:51 -07002135 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002136 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002137
2138 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002139 (*nr_events)++;
2140
Pavel Begunkovc3524382020-06-28 12:52:32 +03002141 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002142 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002143 }
Jens Axboedef596e2019-01-09 08:59:42 -07002144
Jens Axboe09bb8392019-03-13 12:39:28 -06002145 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002146 if (ctx->flags & IORING_SETUP_SQPOLL)
2147 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002148 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002149
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002150 if (!list_empty(&again))
2151 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002152}
2153
Jens Axboedef596e2019-01-09 08:59:42 -07002154static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2155 long min)
2156{
2157 struct io_kiocb *req, *tmp;
2158 LIST_HEAD(done);
2159 bool spin;
2160 int ret;
2161
2162 /*
2163 * Only spin for completions if we don't have multiple devices hanging
2164 * off our complete list, and we're under the requested amount.
2165 */
2166 spin = !ctx->poll_multi_file && *nr_events < min;
2167
2168 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002169 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002170 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002171
2172 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002173 * Move completed and retryable entries to our local lists.
2174 * If we find a request that requires polling, break out
2175 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002176 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002177 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002178 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002179 continue;
2180 }
2181 if (!list_empty(&done))
2182 break;
2183
2184 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2185 if (ret < 0)
2186 break;
2187
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002188 /* iopoll may have completed current req */
2189 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002190 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002191
Jens Axboedef596e2019-01-09 08:59:42 -07002192 if (ret && spin)
2193 spin = false;
2194 ret = 0;
2195 }
2196
2197 if (!list_empty(&done))
2198 io_iopoll_complete(ctx, nr_events, &done);
2199
2200 return ret;
2201}
2202
2203/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002204 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002205 * non-spinning poll check - we'll still enter the driver poll loop, but only
2206 * as a non-spinning completion check.
2207 */
2208static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2209 long min)
2210{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002211 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002212 int ret;
2213
2214 ret = io_do_iopoll(ctx, nr_events, min);
2215 if (ret < 0)
2216 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002217 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002218 return 0;
2219 }
2220
2221 return 1;
2222}
2223
2224/*
2225 * We can't just wait for polled events to come to us, we have to actively
2226 * find and complete them.
2227 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002228static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002229{
2230 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2231 return;
2232
2233 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002234 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002235 unsigned int nr_events = 0;
2236
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002237 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002238
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002239 /* let it sleep and repeat later if can't complete a request */
2240 if (nr_events == 0)
2241 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002242 /*
2243 * Ensure we allow local-to-the-cpu processing to take place,
2244 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002245 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002246 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002247 if (need_resched()) {
2248 mutex_unlock(&ctx->uring_lock);
2249 cond_resched();
2250 mutex_lock(&ctx->uring_lock);
2251 }
Jens Axboedef596e2019-01-09 08:59:42 -07002252 }
2253 mutex_unlock(&ctx->uring_lock);
2254}
2255
Pavel Begunkov7668b922020-07-07 16:36:21 +03002256static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002257{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002258 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002259 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002260
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002261 /*
2262 * We disallow the app entering submit/complete with polling, but we
2263 * still need to lock the ring to prevent racing with polled issue
2264 * that got punted to a workqueue.
2265 */
2266 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002267 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002268 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002269 * Don't enter poll loop if we already have events pending.
2270 * If we do, we can potentially be spinning for commands that
2271 * already triggered a CQE (eg in error).
2272 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002273 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002274 break;
2275
2276 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002277 * If a submit got punted to a workqueue, we can have the
2278 * application entering polling for a command before it gets
2279 * issued. That app will hold the uring_lock for the duration
2280 * of the poll right here, so we need to take a breather every
2281 * now and then to ensure that the issue has a chance to add
2282 * the poll to the issued list. Otherwise we can spin here
2283 * forever, while the workqueue is stuck trying to acquire the
2284 * very same mutex.
2285 */
2286 if (!(++iters & 7)) {
2287 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002288 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002289 mutex_lock(&ctx->uring_lock);
2290 }
2291
Pavel Begunkov7668b922020-07-07 16:36:21 +03002292 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002293 if (ret <= 0)
2294 break;
2295 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002296 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002297
Jens Axboe500f9fb2019-08-19 12:15:59 -06002298 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002299 return ret;
2300}
2301
Jens Axboe491381ce2019-10-17 09:20:46 -06002302static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303{
Jens Axboe491381ce2019-10-17 09:20:46 -06002304 /*
2305 * Tell lockdep we inherited freeze protection from submission
2306 * thread.
2307 */
2308 if (req->flags & REQ_F_ISREG) {
2309 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002310
Jens Axboe491381ce2019-10-17 09:20:46 -06002311 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002313 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002314}
2315
Jens Axboea1d7c392020-06-22 11:09:46 -06002316static void io_complete_rw_common(struct kiocb *kiocb, long res,
2317 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002318{
Jens Axboe9adbd452019-12-20 08:45:55 -07002319 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002320 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321
Jens Axboe491381ce2019-10-17 09:20:46 -06002322 if (kiocb->ki_flags & IOCB_WRITE)
2323 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002325 if (res != req->result)
2326 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002327 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002328 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002329 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002330}
2331
Jens Axboeb63534c2020-06-04 11:28:00 -06002332#ifdef CONFIG_BLOCK
2333static bool io_resubmit_prep(struct io_kiocb *req, int error)
2334{
2335 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2336 ssize_t ret = -ECANCELED;
2337 struct iov_iter iter;
2338 int rw;
2339
2340 if (error) {
2341 ret = error;
2342 goto end_req;
2343 }
2344
2345 switch (req->opcode) {
2346 case IORING_OP_READV:
2347 case IORING_OP_READ_FIXED:
2348 case IORING_OP_READ:
2349 rw = READ;
2350 break;
2351 case IORING_OP_WRITEV:
2352 case IORING_OP_WRITE_FIXED:
2353 case IORING_OP_WRITE:
2354 rw = WRITE;
2355 break;
2356 default:
2357 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2358 req->opcode);
2359 goto end_req;
2360 }
2361
Jens Axboe8f3d7492020-09-14 09:28:14 -06002362 if (!req->io) {
2363 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2364 if (ret < 0)
2365 goto end_req;
2366 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2367 if (!ret)
2368 return true;
2369 kfree(iovec);
2370 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002371 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002372 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002373end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002374 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002375 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002376 return false;
2377}
Jens Axboeb63534c2020-06-04 11:28:00 -06002378#endif
2379
2380static bool io_rw_reissue(struct io_kiocb *req, long res)
2381{
2382#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002383 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002384 int ret;
2385
Jens Axboe355afae2020-09-02 09:30:31 -06002386 if (!S_ISBLK(mode) && !S_ISREG(mode))
2387 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002388 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2389 return false;
2390
Jens Axboefdee9462020-08-27 16:46:24 -06002391 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002392
Jens Axboefdee9462020-08-27 16:46:24 -06002393 if (io_resubmit_prep(req, ret)) {
2394 refcount_inc(&req->refs);
2395 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002396 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002397 }
2398
Jens Axboeb63534c2020-06-04 11:28:00 -06002399#endif
2400 return false;
2401}
2402
Jens Axboea1d7c392020-06-22 11:09:46 -06002403static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2404 struct io_comp_state *cs)
2405{
2406 if (!io_rw_reissue(req, res))
2407 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002408}
2409
2410static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2411{
Jens Axboe9adbd452019-12-20 08:45:55 -07002412 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002413
Jens Axboea1d7c392020-06-22 11:09:46 -06002414 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002415}
2416
Jens Axboedef596e2019-01-09 08:59:42 -07002417static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2418{
Jens Axboe9adbd452019-12-20 08:45:55 -07002419 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002420
Jens Axboe491381ce2019-10-17 09:20:46 -06002421 if (kiocb->ki_flags & IOCB_WRITE)
2422 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002423
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002424 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002425 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002426
2427 WRITE_ONCE(req->result, res);
2428 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002429 smp_wmb();
2430 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002431}
2432
2433/*
2434 * After the iocb has been issued, it's safe to be found on the poll list.
2435 * Adding the kiocb to the list AFTER submission ensures that we don't
2436 * find it from a io_iopoll_getevents() thread before the issuer is done
2437 * accessing the kiocb cookie.
2438 */
2439static void io_iopoll_req_issued(struct io_kiocb *req)
2440{
2441 struct io_ring_ctx *ctx = req->ctx;
2442
2443 /*
2444 * Track whether we have multiple files in our lists. This will impact
2445 * how we do polling eventually, not spinning if we're on potentially
2446 * different devices.
2447 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002448 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002449 ctx->poll_multi_file = false;
2450 } else if (!ctx->poll_multi_file) {
2451 struct io_kiocb *list_req;
2452
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002453 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002454 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002455 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002456 ctx->poll_multi_file = true;
2457 }
2458
2459 /*
2460 * For fast devices, IO may have already completed. If it has, add
2461 * it to the front so we find it first.
2462 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002463 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002464 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002465 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002466 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002467
Jens Axboe534ca6d2020-09-02 13:52:19 -06002468 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2469 wq_has_sleeper(&ctx->sq_data->wait))
2470 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002471}
2472
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002473static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002474{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002475 if (state->has_refs)
2476 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002477 state->file = NULL;
2478}
2479
2480static inline void io_state_file_put(struct io_submit_state *state)
2481{
2482 if (state->file)
2483 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002484}
2485
2486/*
2487 * Get as many references to a file as we have IOs left in this submission,
2488 * assuming most submissions are for one file, or at least that each file
2489 * has more than one submission.
2490 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002491static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002492{
2493 if (!state)
2494 return fget(fd);
2495
2496 if (state->file) {
2497 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002498 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002499 state->ios_left--;
2500 return state->file;
2501 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002502 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002503 }
2504 state->file = fget_many(fd, state->ios_left);
2505 if (!state->file)
2506 return NULL;
2507
2508 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002509 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002510 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002511 return state->file;
2512}
2513
Jens Axboe4503b762020-06-01 10:00:27 -06002514static bool io_bdev_nowait(struct block_device *bdev)
2515{
2516#ifdef CONFIG_BLOCK
2517 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2518#else
2519 return true;
2520#endif
2521}
2522
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523/*
2524 * If we tracked the file through the SCM inflight mechanism, we could support
2525 * any file. For now, just ensure that anything potentially problematic is done
2526 * inline.
2527 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002528static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002529{
2530 umode_t mode = file_inode(file)->i_mode;
2531
Jens Axboe4503b762020-06-01 10:00:27 -06002532 if (S_ISBLK(mode)) {
2533 if (io_bdev_nowait(file->f_inode->i_bdev))
2534 return true;
2535 return false;
2536 }
2537 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002538 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002539 if (S_ISREG(mode)) {
2540 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2541 file->f_op != &io_uring_fops)
2542 return true;
2543 return false;
2544 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002545
Jens Axboec5b85622020-06-09 19:23:05 -06002546 /* any ->read/write should understand O_NONBLOCK */
2547 if (file->f_flags & O_NONBLOCK)
2548 return true;
2549
Jens Axboeaf197f52020-04-28 13:15:06 -06002550 if (!(file->f_mode & FMODE_NOWAIT))
2551 return false;
2552
2553 if (rw == READ)
2554 return file->f_op->read_iter != NULL;
2555
2556 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002557}
2558
Jens Axboe3529d8c2019-12-19 18:24:38 -07002559static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2560 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002561{
Jens Axboedef596e2019-01-09 08:59:42 -07002562 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002563 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002564 unsigned ioprio;
2565 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002566
Jens Axboe491381ce2019-10-17 09:20:46 -06002567 if (S_ISREG(file_inode(req->file)->i_mode))
2568 req->flags |= REQ_F_ISREG;
2569
Jens Axboe2b188cc2019-01-07 10:46:33 -07002570 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002571 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2572 req->flags |= REQ_F_CUR_POS;
2573 kiocb->ki_pos = req->file->f_pos;
2574 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002576 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2577 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2578 if (unlikely(ret))
2579 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002580
2581 ioprio = READ_ONCE(sqe->ioprio);
2582 if (ioprio) {
2583 ret = ioprio_check_cap(ioprio);
2584 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002585 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002586
2587 kiocb->ki_ioprio = ioprio;
2588 } else
2589 kiocb->ki_ioprio = get_current_ioprio();
2590
Stefan Bühler8449eed2019-04-27 20:34:19 +02002591 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002592 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002593 req->flags |= REQ_F_NOWAIT;
2594
2595 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002596 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002597
Jens Axboedef596e2019-01-09 08:59:42 -07002598 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002599 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2600 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002601 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002602
Jens Axboedef596e2019-01-09 08:59:42 -07002603 kiocb->ki_flags |= IOCB_HIPRI;
2604 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002605 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002606 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002607 if (kiocb->ki_flags & IOCB_HIPRI)
2608 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002609 kiocb->ki_complete = io_complete_rw;
2610 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002611
Jens Axboe3529d8c2019-12-19 18:24:38 -07002612 req->rw.addr = READ_ONCE(sqe->addr);
2613 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002614 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002615 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002616}
2617
2618static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2619{
2620 switch (ret) {
2621 case -EIOCBQUEUED:
2622 break;
2623 case -ERESTARTSYS:
2624 case -ERESTARTNOINTR:
2625 case -ERESTARTNOHAND:
2626 case -ERESTART_RESTARTBLOCK:
2627 /*
2628 * We can't just restart the syscall, since previously
2629 * submitted sqes may already be in progress. Just fail this
2630 * IO with EINTR.
2631 */
2632 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002633 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634 default:
2635 kiocb->ki_complete(kiocb, ret, 0);
2636 }
2637}
2638
Jens Axboea1d7c392020-06-22 11:09:46 -06002639static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2640 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002641{
Jens Axboeba042912019-12-25 16:33:42 -07002642 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2643
Jens Axboe227c0c92020-08-13 11:51:40 -06002644 /* add previously done IO, if any */
2645 if (req->io && req->io->rw.bytes_done > 0) {
2646 if (ret < 0)
2647 ret = req->io->rw.bytes_done;
2648 else
2649 ret += req->io->rw.bytes_done;
2650 }
2651
Jens Axboeba042912019-12-25 16:33:42 -07002652 if (req->flags & REQ_F_CUR_POS)
2653 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002654 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002655 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002656 else
2657 io_rw_done(kiocb, ret);
2658}
2659
Jens Axboe9adbd452019-12-20 08:45:55 -07002660static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002661 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002662{
Jens Axboe9adbd452019-12-20 08:45:55 -07002663 struct io_ring_ctx *ctx = req->ctx;
2664 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002665 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002666 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002667 size_t offset;
2668 u64 buf_addr;
2669
2670 /* attempt to use fixed buffers without having provided iovecs */
2671 if (unlikely(!ctx->user_bufs))
2672 return -EFAULT;
2673
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002674 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002675 if (unlikely(buf_index >= ctx->nr_user_bufs))
2676 return -EFAULT;
2677
2678 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2679 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002680 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002681
2682 /* overflow */
2683 if (buf_addr + len < buf_addr)
2684 return -EFAULT;
2685 /* not inside the mapped region */
2686 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2687 return -EFAULT;
2688
2689 /*
2690 * May not be a start of buffer, set size appropriately
2691 * and advance us to the beginning.
2692 */
2693 offset = buf_addr - imu->ubuf;
2694 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002695
2696 if (offset) {
2697 /*
2698 * Don't use iov_iter_advance() here, as it's really slow for
2699 * using the latter parts of a big fixed buffer - it iterates
2700 * over each segment manually. We can cheat a bit here, because
2701 * we know that:
2702 *
2703 * 1) it's a BVEC iter, we set it up
2704 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2705 * first and last bvec
2706 *
2707 * So just find our index, and adjust the iterator afterwards.
2708 * If the offset is within the first bvec (or the whole first
2709 * bvec, just use iov_iter_advance(). This makes it easier
2710 * since we can just skip the first segment, which may not
2711 * be PAGE_SIZE aligned.
2712 */
2713 const struct bio_vec *bvec = imu->bvec;
2714
2715 if (offset <= bvec->bv_len) {
2716 iov_iter_advance(iter, offset);
2717 } else {
2718 unsigned long seg_skip;
2719
2720 /* skip first vec */
2721 offset -= bvec->bv_len;
2722 seg_skip = 1 + (offset >> PAGE_SHIFT);
2723
2724 iter->bvec = bvec + seg_skip;
2725 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002726 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002727 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002728 }
2729 }
2730
Jens Axboe5e559562019-11-13 16:12:46 -07002731 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002732}
2733
Jens Axboebcda7ba2020-02-23 16:42:51 -07002734static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2735{
2736 if (needs_lock)
2737 mutex_unlock(&ctx->uring_lock);
2738}
2739
2740static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2741{
2742 /*
2743 * "Normal" inline submissions always hold the uring_lock, since we
2744 * grab it from the system call. Same is true for the SQPOLL offload.
2745 * The only exception is when we've detached the request and issue it
2746 * from an async worker thread, grab the lock for that case.
2747 */
2748 if (needs_lock)
2749 mutex_lock(&ctx->uring_lock);
2750}
2751
2752static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2753 int bgid, struct io_buffer *kbuf,
2754 bool needs_lock)
2755{
2756 struct io_buffer *head;
2757
2758 if (req->flags & REQ_F_BUFFER_SELECTED)
2759 return kbuf;
2760
2761 io_ring_submit_lock(req->ctx, needs_lock);
2762
2763 lockdep_assert_held(&req->ctx->uring_lock);
2764
2765 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2766 if (head) {
2767 if (!list_empty(&head->list)) {
2768 kbuf = list_last_entry(&head->list, struct io_buffer,
2769 list);
2770 list_del(&kbuf->list);
2771 } else {
2772 kbuf = head;
2773 idr_remove(&req->ctx->io_buffer_idr, bgid);
2774 }
2775 if (*len > kbuf->len)
2776 *len = kbuf->len;
2777 } else {
2778 kbuf = ERR_PTR(-ENOBUFS);
2779 }
2780
2781 io_ring_submit_unlock(req->ctx, needs_lock);
2782
2783 return kbuf;
2784}
2785
Jens Axboe4d954c22020-02-27 07:31:19 -07002786static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2787 bool needs_lock)
2788{
2789 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002790 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002791
2792 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002793 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002794 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2795 if (IS_ERR(kbuf))
2796 return kbuf;
2797 req->rw.addr = (u64) (unsigned long) kbuf;
2798 req->flags |= REQ_F_BUFFER_SELECTED;
2799 return u64_to_user_ptr(kbuf->addr);
2800}
2801
2802#ifdef CONFIG_COMPAT
2803static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2804 bool needs_lock)
2805{
2806 struct compat_iovec __user *uiov;
2807 compat_ssize_t clen;
2808 void __user *buf;
2809 ssize_t len;
2810
2811 uiov = u64_to_user_ptr(req->rw.addr);
2812 if (!access_ok(uiov, sizeof(*uiov)))
2813 return -EFAULT;
2814 if (__get_user(clen, &uiov->iov_len))
2815 return -EFAULT;
2816 if (clen < 0)
2817 return -EINVAL;
2818
2819 len = clen;
2820 buf = io_rw_buffer_select(req, &len, needs_lock);
2821 if (IS_ERR(buf))
2822 return PTR_ERR(buf);
2823 iov[0].iov_base = buf;
2824 iov[0].iov_len = (compat_size_t) len;
2825 return 0;
2826}
2827#endif
2828
2829static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2830 bool needs_lock)
2831{
2832 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2833 void __user *buf;
2834 ssize_t len;
2835
2836 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2837 return -EFAULT;
2838
2839 len = iov[0].iov_len;
2840 if (len < 0)
2841 return -EINVAL;
2842 buf = io_rw_buffer_select(req, &len, needs_lock);
2843 if (IS_ERR(buf))
2844 return PTR_ERR(buf);
2845 iov[0].iov_base = buf;
2846 iov[0].iov_len = len;
2847 return 0;
2848}
2849
2850static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2851 bool needs_lock)
2852{
Jens Axboedddb3e22020-06-04 11:27:01 -06002853 if (req->flags & REQ_F_BUFFER_SELECTED) {
2854 struct io_buffer *kbuf;
2855
2856 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2857 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2858 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002859 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002860 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002861 if (!req->rw.len)
2862 return 0;
2863 else if (req->rw.len > 1)
2864 return -EINVAL;
2865
2866#ifdef CONFIG_COMPAT
2867 if (req->ctx->compat)
2868 return io_compat_import(req, iov, needs_lock);
2869#endif
2870
2871 return __io_iov_buffer_select(req, iov, needs_lock);
2872}
2873
Jens Axboe8452fd02020-08-18 13:58:33 -07002874static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2875 struct iovec **iovec, struct iov_iter *iter,
2876 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002877{
Jens Axboe9adbd452019-12-20 08:45:55 -07002878 void __user *buf = u64_to_user_ptr(req->rw.addr);
2879 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002880 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002881 u8 opcode;
2882
Jens Axboed625c6e2019-12-17 19:53:05 -07002883 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002884 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002885 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002886 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002887 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002888
Jens Axboebcda7ba2020-02-23 16:42:51 -07002889 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002890 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002891 return -EINVAL;
2892
Jens Axboe3a6820f2019-12-22 15:19:35 -07002893 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002894 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002895 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002896 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002897 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002898 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002899 }
2900
Jens Axboe3a6820f2019-12-22 15:19:35 -07002901 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2902 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002903 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002904 }
2905
Jens Axboe4d954c22020-02-27 07:31:19 -07002906 if (req->flags & REQ_F_BUFFER_SELECT) {
2907 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002908 if (!ret) {
2909 ret = (*iovec)->iov_len;
2910 iov_iter_init(iter, rw, *iovec, 1, ret);
2911 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002912 *iovec = NULL;
2913 return ret;
2914 }
2915
Jens Axboe2b188cc2019-01-07 10:46:33 -07002916#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002917 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002918 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2919 iovec, iter);
2920#endif
2921
2922 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2923}
2924
Jens Axboe8452fd02020-08-18 13:58:33 -07002925static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2926 struct iovec **iovec, struct iov_iter *iter,
2927 bool needs_lock)
2928{
2929 if (!req->io)
2930 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2931 *iovec = NULL;
2932 return iov_iter_count(&req->io->rw.iter);
2933}
2934
Jens Axboe0fef9482020-08-26 10:36:20 -06002935static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2936{
2937 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2938}
2939
Jens Axboe32960612019-09-23 11:05:34 -06002940/*
2941 * For files that don't have ->read_iter() and ->write_iter(), handle them
2942 * by looping over ->read() or ->write() manually.
2943 */
2944static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2945 struct iov_iter *iter)
2946{
2947 ssize_t ret = 0;
2948
2949 /*
2950 * Don't support polled IO through this interface, and we can't
2951 * support non-blocking either. For the latter, this just causes
2952 * the kiocb to be handled from an async context.
2953 */
2954 if (kiocb->ki_flags & IOCB_HIPRI)
2955 return -EOPNOTSUPP;
2956 if (kiocb->ki_flags & IOCB_NOWAIT)
2957 return -EAGAIN;
2958
2959 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002960 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002961 ssize_t nr;
2962
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002963 if (!iov_iter_is_bvec(iter)) {
2964 iovec = iov_iter_iovec(iter);
2965 } else {
2966 /* fixed buffers import bvec */
2967 iovec.iov_base = kmap(iter->bvec->bv_page)
2968 + iter->iov_offset;
2969 iovec.iov_len = min(iter->count,
2970 iter->bvec->bv_len - iter->iov_offset);
2971 }
2972
Jens Axboe32960612019-09-23 11:05:34 -06002973 if (rw == READ) {
2974 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002975 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002976 } else {
2977 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002978 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002979 }
2980
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002981 if (iov_iter_is_bvec(iter))
2982 kunmap(iter->bvec->bv_page);
2983
Jens Axboe32960612019-09-23 11:05:34 -06002984 if (nr < 0) {
2985 if (!ret)
2986 ret = nr;
2987 break;
2988 }
2989 ret += nr;
2990 if (nr != iovec.iov_len)
2991 break;
2992 iov_iter_advance(iter, nr);
2993 }
2994
2995 return ret;
2996}
2997
Jens Axboeff6165b2020-08-13 09:47:43 -06002998static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2999 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003000{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003001 struct io_async_rw *rw = &req->io->rw;
3002
Jens Axboeff6165b2020-08-13 09:47:43 -06003003 memcpy(&rw->iter, iter, sizeof(*iter));
3004 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06003005 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003006 /* can only be fixed buffers, no need to do anything */
3007 if (iter->type == ITER_BVEC)
3008 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003009 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003010 unsigned iov_off = 0;
3011
3012 rw->iter.iov = rw->fast_iov;
3013 if (iter->iov != fast_iov) {
3014 iov_off = iter->iov - fast_iov;
3015 rw->iter.iov += iov_off;
3016 }
3017 if (rw->fast_iov != fast_iov)
3018 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003019 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003020 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06003021 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003022 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003023 }
3024}
3025
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003026static inline int __io_alloc_async_ctx(struct io_kiocb *req)
3027{
3028 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
3029 return req->io == NULL;
3030}
3031
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003032static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003033{
Jens Axboed3656342019-12-18 09:50:26 -07003034 if (!io_op_defs[req->opcode].async_ctx)
3035 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003036
3037 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003038}
3039
Jens Axboeff6165b2020-08-13 09:47:43 -06003040static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3041 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003042 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003043{
Jens Axboe227c0c92020-08-13 11:51:40 -06003044 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003045 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003046 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003047 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003048 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003049
Jens Axboeff6165b2020-08-13 09:47:43 -06003050 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003051 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003052 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003053}
3054
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003055static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3056 bool force_nonblock)
3057{
Jens Axboeff6165b2020-08-13 09:47:43 -06003058 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06003059 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003060 ssize_t ret;
3061
Jens Axboec183edf2020-09-04 22:36:52 -06003062 iorw->iter.iov = iov = iorw->fast_iov;
3063 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003064 if (unlikely(ret < 0))
3065 return ret;
3066
Jens Axboec183edf2020-09-04 22:36:52 -06003067 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003068 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003069 return 0;
3070}
3071
Jens Axboe3529d8c2019-12-19 18:24:38 -07003072static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3073 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003074{
3075 ssize_t ret;
3076
Jens Axboe3529d8c2019-12-19 18:24:38 -07003077 ret = io_prep_rw(req, sqe, force_nonblock);
3078 if (ret)
3079 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003080
Jens Axboe3529d8c2019-12-19 18:24:38 -07003081 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3082 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003083
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003084 /* either don't need iovec imported or already have it */
3085 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003086 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003087 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003088}
3089
Jens Axboec1dd91d2020-08-03 16:43:59 -06003090/*
3091 * This is our waitqueue callback handler, registered through lock_page_async()
3092 * when we initially tried to do the IO with the iocb armed our waitqueue.
3093 * This gets called when the page is unlocked, and we generally expect that to
3094 * happen when the page IO is completed and the page is now uptodate. This will
3095 * queue a task_work based retry of the operation, attempting to copy the data
3096 * again. If the latter fails because the page was NOT uptodate, then we will
3097 * do a thread based blocking retry of the operation. That's the unexpected
3098 * slow path.
3099 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003100static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3101 int sync, void *arg)
3102{
3103 struct wait_page_queue *wpq;
3104 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003105 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003106 int ret;
3107
3108 wpq = container_of(wait, struct wait_page_queue, wait);
3109
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003110 if (!wake_page_match(wpq, key))
3111 return 0;
3112
Hao Xuc8d317a2020-09-29 20:00:45 +08003113 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003114 list_del_init(&wait->entry);
3115
Pavel Begunkove7375122020-07-12 20:42:04 +03003116 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003117 percpu_ref_get(&req->ctx->refs);
3118
Jens Axboebcf5a062020-05-22 09:24:42 -06003119 /* submit ref gets dropped, acquire a new one */
3120 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003121 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003122 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003123 struct task_struct *tsk;
3124
Jens Axboebcf5a062020-05-22 09:24:42 -06003125 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003126 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003127 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003128 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003129 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003130 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003131 return 1;
3132}
3133
Jens Axboec1dd91d2020-08-03 16:43:59 -06003134/*
3135 * This controls whether a given IO request should be armed for async page
3136 * based retry. If we return false here, the request is handed to the async
3137 * worker threads for retry. If we're doing buffered reads on a regular file,
3138 * we prepare a private wait_page_queue entry and retry the operation. This
3139 * will either succeed because the page is now uptodate and unlocked, or it
3140 * will register a callback when the page is unlocked at IO completion. Through
3141 * that callback, io_uring uses task_work to setup a retry of the operation.
3142 * That retry will attempt the buffered read again. The retry will generally
3143 * succeed, or in rare cases where it fails, we then fall back to using the
3144 * async worker threads for a blocking retry.
3145 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003146static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003147{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003148 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003149 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003150
3151 /* never retry for NOWAIT, we just complete with -EAGAIN */
3152 if (req->flags & REQ_F_NOWAIT)
3153 return false;
3154
Jens Axboe227c0c92020-08-13 11:51:40 -06003155 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003156 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003157 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003158
Jens Axboebcf5a062020-05-22 09:24:42 -06003159 /*
3160 * just use poll if we can, and don't attempt if the fs doesn't
3161 * support callback based unlocks
3162 */
3163 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3164 return false;
3165
Jens Axboe3b2a4432020-08-16 10:58:43 -07003166 wait->wait.func = io_async_buf_func;
3167 wait->wait.private = req;
3168 wait->wait.flags = 0;
3169 INIT_LIST_HEAD(&wait->wait.entry);
3170 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003171 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003172 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003173 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003174}
3175
3176static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3177{
3178 if (req->file->f_op->read_iter)
3179 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003180 else if (req->file->f_op->read)
3181 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3182 else
3183 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003184}
3185
Jens Axboea1d7c392020-06-22 11:09:46 -06003186static int io_read(struct io_kiocb *req, bool force_nonblock,
3187 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003188{
3189 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003190 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003191 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003192 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003193 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003194 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003195
Jens Axboeff6165b2020-08-13 09:47:43 -06003196 if (req->io)
3197 iter = &req->io->rw.iter;
3198
3199 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003200 if (ret < 0)
3201 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003202 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003203 io_size = ret;
3204 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003205 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003206
Jens Axboefd6c2e42019-12-18 12:19:41 -07003207 /* Ensure we clear previously set non-block flag */
3208 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003209 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003210
Pavel Begunkov24c74672020-06-21 13:09:51 +03003211 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003212 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3213 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003214 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003215
Jens Axboe0fef9482020-08-26 10:36:20 -06003216 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003217 if (unlikely(ret))
3218 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003219
Jens Axboe227c0c92020-08-13 11:51:40 -06003220 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003221
Jens Axboe227c0c92020-08-13 11:51:40 -06003222 if (!ret) {
3223 goto done;
3224 } else if (ret == -EIOCBQUEUED) {
3225 ret = 0;
3226 goto out_free;
3227 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003228 /* IOPOLL retry should happen for io-wq threads */
3229 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003230 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003231 /* no retry on NONBLOCK marked file */
3232 if (req->file->f_flags & O_NONBLOCK)
3233 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003234 /* some cases will consume bytes even on error returns */
3235 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003236 ret = 0;
3237 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003238 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003239 /* make sure -ERESTARTSYS -> -EINTR is done */
3240 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003241 }
3242
3243 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003244 if (!iov_iter_count(iter) || !force_nonblock ||
3245 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003246 goto done;
3247
3248 io_size -= ret;
3249copy_iov:
3250 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3251 if (ret2) {
3252 ret = ret2;
3253 goto out_free;
3254 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003255 if (no_async)
3256 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003257 /* it's copied and will be cleaned with ->io */
3258 iovec = NULL;
3259 /* now use our persistent iterator, if we aren't already */
3260 iter = &req->io->rw.iter;
3261retry:
3262 req->io->rw.bytes_done += ret;
3263 /* if we can retry, do so with the callbacks armed */
3264 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003265 kiocb->ki_flags &= ~IOCB_WAITQ;
3266 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003267 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003268
3269 /*
3270 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3271 * get -EIOCBQUEUED, then we'll get a notification when the desired
3272 * page gets unlocked. We can also get a partial read here, and if we
3273 * do, then just retry at the new offset.
3274 */
3275 ret = io_iter_do_read(req, iter);
3276 if (ret == -EIOCBQUEUED) {
3277 ret = 0;
3278 goto out_free;
3279 } else if (ret > 0 && ret < io_size) {
3280 /* we got some bytes, but not all. retry. */
3281 goto retry;
3282 }
3283done:
3284 kiocb_done(kiocb, ret, cs);
3285 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003286out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003287 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003288 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003289 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003290 return ret;
3291}
3292
Jens Axboe3529d8c2019-12-19 18:24:38 -07003293static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3294 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003295{
3296 ssize_t ret;
3297
Jens Axboe3529d8c2019-12-19 18:24:38 -07003298 ret = io_prep_rw(req, sqe, force_nonblock);
3299 if (ret)
3300 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003301
Jens Axboe3529d8c2019-12-19 18:24:38 -07003302 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3303 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003304
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003305 /* either don't need iovec imported or already have it */
3306 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003307 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003308 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003309}
3310
Jens Axboea1d7c392020-06-22 11:09:46 -06003311static int io_write(struct io_kiocb *req, bool force_nonblock,
3312 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003313{
3314 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003315 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003316 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003317 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003318 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003319
Jens Axboeff6165b2020-08-13 09:47:43 -06003320 if (req->io)
3321 iter = &req->io->rw.iter;
3322
3323 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003324 if (ret < 0)
3325 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003326 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003327 io_size = ret;
3328 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003329
Jens Axboefd6c2e42019-12-18 12:19:41 -07003330 /* Ensure we clear previously set non-block flag */
3331 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003332 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003333
Pavel Begunkov24c74672020-06-21 13:09:51 +03003334 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003335 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003336 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003337
Jens Axboe10d59342019-12-09 20:16:22 -07003338 /* file path doesn't support NOWAIT for non-direct_IO */
3339 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3340 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003341 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003342
Jens Axboe0fef9482020-08-26 10:36:20 -06003343 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003344 if (unlikely(ret))
3345 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003346
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003347 /*
3348 * Open-code file_start_write here to grab freeze protection,
3349 * which will be released by another thread in
3350 * io_complete_rw(). Fool lockdep by telling it the lock got
3351 * released so that it doesn't complain about the held lock when
3352 * we return to userspace.
3353 */
3354 if (req->flags & REQ_F_ISREG) {
3355 __sb_start_write(file_inode(req->file)->i_sb,
3356 SB_FREEZE_WRITE, true);
3357 __sb_writers_release(file_inode(req->file)->i_sb,
3358 SB_FREEZE_WRITE);
3359 }
3360 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003361
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003362 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003363 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003364 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003365 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003366 else
3367 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003368
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003369 /*
3370 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3371 * retry them without IOCB_NOWAIT.
3372 */
3373 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3374 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003375 /* no retry on NONBLOCK marked file */
3376 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3377 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003378 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003379 /* IOPOLL retry should happen for io-wq threads */
3380 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3381 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003382done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003383 kiocb_done(kiocb, ret2, cs);
3384 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003385copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003386 /* some cases will consume bytes even on error returns */
3387 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003388 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003389 if (!ret)
3390 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003391 }
Jens Axboe31b51512019-01-18 22:56:34 -07003392out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003393 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003394 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003395 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003396 return ret;
3397}
3398
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003399static int __io_splice_prep(struct io_kiocb *req,
3400 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003401{
3402 struct io_splice* sp = &req->splice;
3403 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3404 int ret;
3405
3406 if (req->flags & REQ_F_NEED_CLEANUP)
3407 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003408 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3409 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003410
3411 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003412 sp->len = READ_ONCE(sqe->len);
3413 sp->flags = READ_ONCE(sqe->splice_flags);
3414
3415 if (unlikely(sp->flags & ~valid_flags))
3416 return -EINVAL;
3417
3418 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3419 (sp->flags & SPLICE_F_FD_IN_FIXED));
3420 if (ret)
3421 return ret;
3422 req->flags |= REQ_F_NEED_CLEANUP;
3423
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003424 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3425 /*
3426 * Splice operation will be punted aync, and here need to
3427 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3428 */
3429 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003430 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003431 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003432
3433 return 0;
3434}
3435
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003436static int io_tee_prep(struct io_kiocb *req,
3437 const struct io_uring_sqe *sqe)
3438{
3439 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3440 return -EINVAL;
3441 return __io_splice_prep(req, sqe);
3442}
3443
3444static int io_tee(struct io_kiocb *req, bool force_nonblock)
3445{
3446 struct io_splice *sp = &req->splice;
3447 struct file *in = sp->file_in;
3448 struct file *out = sp->file_out;
3449 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3450 long ret = 0;
3451
3452 if (force_nonblock)
3453 return -EAGAIN;
3454 if (sp->len)
3455 ret = do_tee(in, out, sp->len, flags);
3456
3457 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3458 req->flags &= ~REQ_F_NEED_CLEANUP;
3459
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003460 if (ret != sp->len)
3461 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003462 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003463 return 0;
3464}
3465
3466static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3467{
3468 struct io_splice* sp = &req->splice;
3469
3470 sp->off_in = READ_ONCE(sqe->splice_off_in);
3471 sp->off_out = READ_ONCE(sqe->off);
3472 return __io_splice_prep(req, sqe);
3473}
3474
Pavel Begunkov014db002020-03-03 21:33:12 +03003475static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003476{
3477 struct io_splice *sp = &req->splice;
3478 struct file *in = sp->file_in;
3479 struct file *out = sp->file_out;
3480 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3481 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003482 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003483
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003484 if (force_nonblock)
3485 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003486
3487 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3488 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003489
Jens Axboe948a7742020-05-17 14:21:38 -06003490 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003491 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003492
3493 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3494 req->flags &= ~REQ_F_NEED_CLEANUP;
3495
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003496 if (ret != sp->len)
3497 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003498 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003499 return 0;
3500}
3501
Jens Axboe2b188cc2019-01-07 10:46:33 -07003502/*
3503 * IORING_OP_NOP just posts a completion event, nothing else.
3504 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003505static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003506{
3507 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003508
Jens Axboedef596e2019-01-09 08:59:42 -07003509 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3510 return -EINVAL;
3511
Jens Axboe229a7b62020-06-22 10:13:11 -06003512 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003513 return 0;
3514}
3515
Jens Axboe3529d8c2019-12-19 18:24:38 -07003516static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003517{
Jens Axboe6b063142019-01-10 22:13:58 -07003518 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003519
Jens Axboe09bb8392019-03-13 12:39:28 -06003520 if (!req->file)
3521 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003522
Jens Axboe6b063142019-01-10 22:13:58 -07003523 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003524 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003525 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003526 return -EINVAL;
3527
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003528 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3529 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3530 return -EINVAL;
3531
3532 req->sync.off = READ_ONCE(sqe->off);
3533 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003534 return 0;
3535}
3536
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003537static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003538{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003539 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003540 int ret;
3541
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003542 /* fsync always requires a blocking context */
3543 if (force_nonblock)
3544 return -EAGAIN;
3545
Jens Axboe9adbd452019-12-20 08:45:55 -07003546 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003547 end > 0 ? end : LLONG_MAX,
3548 req->sync.flags & IORING_FSYNC_DATASYNC);
3549 if (ret < 0)
3550 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003551 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003552 return 0;
3553}
3554
Jens Axboed63d1b52019-12-10 10:38:56 -07003555static int io_fallocate_prep(struct io_kiocb *req,
3556 const struct io_uring_sqe *sqe)
3557{
3558 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3559 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003560 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3561 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003562
3563 req->sync.off = READ_ONCE(sqe->off);
3564 req->sync.len = READ_ONCE(sqe->addr);
3565 req->sync.mode = READ_ONCE(sqe->len);
3566 return 0;
3567}
3568
Pavel Begunkov014db002020-03-03 21:33:12 +03003569static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003570{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003571 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003572
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003573 /* fallocate always requiring blocking context */
3574 if (force_nonblock)
3575 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003576 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3577 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003578 if (ret < 0)
3579 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003580 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003581 return 0;
3582}
3583
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003584static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003585{
Jens Axboef8748882020-01-08 17:47:02 -07003586 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003587 int ret;
3588
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003589 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003590 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003591 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003592 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003593
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003594 /* open.how should be already initialised */
3595 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003596 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003597
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003598 req->open.dfd = READ_ONCE(sqe->fd);
3599 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003600 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003601 if (IS_ERR(req->open.filename)) {
3602 ret = PTR_ERR(req->open.filename);
3603 req->open.filename = NULL;
3604 return ret;
3605 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003606 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003607 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003608 return 0;
3609}
3610
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003611static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3612{
3613 u64 flags, mode;
3614
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003615 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3616 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003617 if (req->flags & REQ_F_NEED_CLEANUP)
3618 return 0;
3619 mode = READ_ONCE(sqe->len);
3620 flags = READ_ONCE(sqe->open_flags);
3621 req->open.how = build_open_how(flags, mode);
3622 return __io_openat_prep(req, sqe);
3623}
3624
Jens Axboecebdb982020-01-08 17:59:24 -07003625static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3626{
3627 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003628 size_t len;
3629 int ret;
3630
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003631 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3632 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003633 if (req->flags & REQ_F_NEED_CLEANUP)
3634 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003635 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3636 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003637 if (len < OPEN_HOW_SIZE_VER0)
3638 return -EINVAL;
3639
3640 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3641 len);
3642 if (ret)
3643 return ret;
3644
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003645 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003646}
3647
Pavel Begunkov014db002020-03-03 21:33:12 +03003648static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003649{
3650 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003651 struct file *file;
3652 int ret;
3653
Jens Axboef86cd202020-01-29 13:46:44 -07003654 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003655 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003656
Jens Axboecebdb982020-01-08 17:59:24 -07003657 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003658 if (ret)
3659 goto err;
3660
Jens Axboe4022e7a2020-03-19 19:23:18 -06003661 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003662 if (ret < 0)
3663 goto err;
3664
3665 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3666 if (IS_ERR(file)) {
3667 put_unused_fd(ret);
3668 ret = PTR_ERR(file);
3669 } else {
3670 fsnotify_open(file);
3671 fd_install(ret, file);
3672 }
3673err:
3674 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003675 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003676 if (ret < 0)
3677 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003678 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003679 return 0;
3680}
3681
Pavel Begunkov014db002020-03-03 21:33:12 +03003682static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003683{
Pavel Begunkov014db002020-03-03 21:33:12 +03003684 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003685}
3686
Jens Axboe067524e2020-03-02 16:32:28 -07003687static int io_remove_buffers_prep(struct io_kiocb *req,
3688 const struct io_uring_sqe *sqe)
3689{
3690 struct io_provide_buf *p = &req->pbuf;
3691 u64 tmp;
3692
3693 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3694 return -EINVAL;
3695
3696 tmp = READ_ONCE(sqe->fd);
3697 if (!tmp || tmp > USHRT_MAX)
3698 return -EINVAL;
3699
3700 memset(p, 0, sizeof(*p));
3701 p->nbufs = tmp;
3702 p->bgid = READ_ONCE(sqe->buf_group);
3703 return 0;
3704}
3705
3706static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3707 int bgid, unsigned nbufs)
3708{
3709 unsigned i = 0;
3710
3711 /* shouldn't happen */
3712 if (!nbufs)
3713 return 0;
3714
3715 /* the head kbuf is the list itself */
3716 while (!list_empty(&buf->list)) {
3717 struct io_buffer *nxt;
3718
3719 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3720 list_del(&nxt->list);
3721 kfree(nxt);
3722 if (++i == nbufs)
3723 return i;
3724 }
3725 i++;
3726 kfree(buf);
3727 idr_remove(&ctx->io_buffer_idr, bgid);
3728
3729 return i;
3730}
3731
Jens Axboe229a7b62020-06-22 10:13:11 -06003732static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3733 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003734{
3735 struct io_provide_buf *p = &req->pbuf;
3736 struct io_ring_ctx *ctx = req->ctx;
3737 struct io_buffer *head;
3738 int ret = 0;
3739
3740 io_ring_submit_lock(ctx, !force_nonblock);
3741
3742 lockdep_assert_held(&ctx->uring_lock);
3743
3744 ret = -ENOENT;
3745 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3746 if (head)
3747 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3748
3749 io_ring_submit_lock(ctx, !force_nonblock);
3750 if (ret < 0)
3751 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003752 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003753 return 0;
3754}
3755
Jens Axboeddf0322d2020-02-23 16:41:33 -07003756static int io_provide_buffers_prep(struct io_kiocb *req,
3757 const struct io_uring_sqe *sqe)
3758{
3759 struct io_provide_buf *p = &req->pbuf;
3760 u64 tmp;
3761
3762 if (sqe->ioprio || sqe->rw_flags)
3763 return -EINVAL;
3764
3765 tmp = READ_ONCE(sqe->fd);
3766 if (!tmp || tmp > USHRT_MAX)
3767 return -E2BIG;
3768 p->nbufs = tmp;
3769 p->addr = READ_ONCE(sqe->addr);
3770 p->len = READ_ONCE(sqe->len);
3771
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003772 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003773 return -EFAULT;
3774
3775 p->bgid = READ_ONCE(sqe->buf_group);
3776 tmp = READ_ONCE(sqe->off);
3777 if (tmp > USHRT_MAX)
3778 return -E2BIG;
3779 p->bid = tmp;
3780 return 0;
3781}
3782
3783static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3784{
3785 struct io_buffer *buf;
3786 u64 addr = pbuf->addr;
3787 int i, bid = pbuf->bid;
3788
3789 for (i = 0; i < pbuf->nbufs; i++) {
3790 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3791 if (!buf)
3792 break;
3793
3794 buf->addr = addr;
3795 buf->len = pbuf->len;
3796 buf->bid = bid;
3797 addr += pbuf->len;
3798 bid++;
3799 if (!*head) {
3800 INIT_LIST_HEAD(&buf->list);
3801 *head = buf;
3802 } else {
3803 list_add_tail(&buf->list, &(*head)->list);
3804 }
3805 }
3806
3807 return i ? i : -ENOMEM;
3808}
3809
Jens Axboe229a7b62020-06-22 10:13:11 -06003810static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3811 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003812{
3813 struct io_provide_buf *p = &req->pbuf;
3814 struct io_ring_ctx *ctx = req->ctx;
3815 struct io_buffer *head, *list;
3816 int ret = 0;
3817
3818 io_ring_submit_lock(ctx, !force_nonblock);
3819
3820 lockdep_assert_held(&ctx->uring_lock);
3821
3822 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3823
3824 ret = io_add_buffers(p, &head);
3825 if (ret < 0)
3826 goto out;
3827
3828 if (!list) {
3829 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3830 GFP_KERNEL);
3831 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003832 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003833 goto out;
3834 }
3835 }
3836out:
3837 io_ring_submit_unlock(ctx, !force_nonblock);
3838 if (ret < 0)
3839 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003840 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003841 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003842}
3843
Jens Axboe3e4827b2020-01-08 15:18:09 -07003844static int io_epoll_ctl_prep(struct io_kiocb *req,
3845 const struct io_uring_sqe *sqe)
3846{
3847#if defined(CONFIG_EPOLL)
3848 if (sqe->ioprio || sqe->buf_index)
3849 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003850 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003851 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003852
3853 req->epoll.epfd = READ_ONCE(sqe->fd);
3854 req->epoll.op = READ_ONCE(sqe->len);
3855 req->epoll.fd = READ_ONCE(sqe->off);
3856
3857 if (ep_op_has_event(req->epoll.op)) {
3858 struct epoll_event __user *ev;
3859
3860 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3861 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3862 return -EFAULT;
3863 }
3864
3865 return 0;
3866#else
3867 return -EOPNOTSUPP;
3868#endif
3869}
3870
Jens Axboe229a7b62020-06-22 10:13:11 -06003871static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3872 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003873{
3874#if defined(CONFIG_EPOLL)
3875 struct io_epoll *ie = &req->epoll;
3876 int ret;
3877
3878 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3879 if (force_nonblock && ret == -EAGAIN)
3880 return -EAGAIN;
3881
3882 if (ret < 0)
3883 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003884 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003885 return 0;
3886#else
3887 return -EOPNOTSUPP;
3888#endif
3889}
3890
Jens Axboec1ca7572019-12-25 22:18:28 -07003891static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3892{
3893#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3894 if (sqe->ioprio || sqe->buf_index || sqe->off)
3895 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003896 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3897 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003898
3899 req->madvise.addr = READ_ONCE(sqe->addr);
3900 req->madvise.len = READ_ONCE(sqe->len);
3901 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3902 return 0;
3903#else
3904 return -EOPNOTSUPP;
3905#endif
3906}
3907
Pavel Begunkov014db002020-03-03 21:33:12 +03003908static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003909{
3910#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3911 struct io_madvise *ma = &req->madvise;
3912 int ret;
3913
3914 if (force_nonblock)
3915 return -EAGAIN;
3916
3917 ret = do_madvise(ma->addr, ma->len, ma->advice);
3918 if (ret < 0)
3919 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003920 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003921 return 0;
3922#else
3923 return -EOPNOTSUPP;
3924#endif
3925}
3926
Jens Axboe4840e412019-12-25 22:03:45 -07003927static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3928{
3929 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3930 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003931 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3932 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003933
3934 req->fadvise.offset = READ_ONCE(sqe->off);
3935 req->fadvise.len = READ_ONCE(sqe->len);
3936 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3937 return 0;
3938}
3939
Pavel Begunkov014db002020-03-03 21:33:12 +03003940static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003941{
3942 struct io_fadvise *fa = &req->fadvise;
3943 int ret;
3944
Jens Axboe3e694262020-02-01 09:22:49 -07003945 if (force_nonblock) {
3946 switch (fa->advice) {
3947 case POSIX_FADV_NORMAL:
3948 case POSIX_FADV_RANDOM:
3949 case POSIX_FADV_SEQUENTIAL:
3950 break;
3951 default:
3952 return -EAGAIN;
3953 }
3954 }
Jens Axboe4840e412019-12-25 22:03:45 -07003955
3956 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3957 if (ret < 0)
3958 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003959 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003960 return 0;
3961}
3962
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003963static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3964{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003965 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003966 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003967 if (sqe->ioprio || sqe->buf_index)
3968 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003969 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003970 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003971
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003972 req->statx.dfd = READ_ONCE(sqe->fd);
3973 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003974 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003975 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3976 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003977
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003978 return 0;
3979}
3980
Pavel Begunkov014db002020-03-03 21:33:12 +03003981static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003982{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003983 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003984 int ret;
3985
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003986 if (force_nonblock) {
3987 /* only need file table for an actual valid fd */
3988 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3989 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003990 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003991 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003992
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003993 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3994 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003995
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003996 if (ret < 0)
3997 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003998 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003999 return 0;
4000}
4001
Jens Axboeb5dba592019-12-11 14:02:38 -07004002static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4003{
4004 /*
4005 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004006 * leave the 'file' in an undeterminate state, and here need to modify
4007 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004008 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004009 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004010 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4011
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004012 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4013 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004014 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4015 sqe->rw_flags || sqe->buf_index)
4016 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004017 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004018 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004019
4020 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004021 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004022 return -EBADF;
4023
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004024 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004025 return 0;
4026}
4027
Jens Axboe229a7b62020-06-22 10:13:11 -06004028static int io_close(struct io_kiocb *req, bool force_nonblock,
4029 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004030{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004031 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004032 int ret;
4033
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004034 /* might be already done during nonblock submission */
4035 if (!close->put_file) {
4036 ret = __close_fd_get_file(close->fd, &close->put_file);
4037 if (ret < 0)
4038 return (ret == -ENOENT) ? -EBADF : ret;
4039 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004040
4041 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004042 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004043 /* was never set, but play safe */
4044 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004045 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004046 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004047 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004048 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004049
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004050 /* No ->flush() or already async, safely close from here */
4051 ret = filp_close(close->put_file, req->work.files);
4052 if (ret < 0)
4053 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004054 fput(close->put_file);
4055 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004056 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004057 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004058}
4059
Jens Axboe3529d8c2019-12-19 18:24:38 -07004060static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004061{
4062 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004063
4064 if (!req->file)
4065 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004066
4067 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4068 return -EINVAL;
4069 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4070 return -EINVAL;
4071
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004072 req->sync.off = READ_ONCE(sqe->off);
4073 req->sync.len = READ_ONCE(sqe->len);
4074 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004075 return 0;
4076}
4077
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004078static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004079{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004080 int ret;
4081
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004082 /* sync_file_range always requires a blocking context */
4083 if (force_nonblock)
4084 return -EAGAIN;
4085
Jens Axboe9adbd452019-12-20 08:45:55 -07004086 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004087 req->sync.flags);
4088 if (ret < 0)
4089 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004090 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004091 return 0;
4092}
4093
YueHaibing469956e2020-03-04 15:53:52 +08004094#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004095static int io_setup_async_msg(struct io_kiocb *req,
4096 struct io_async_msghdr *kmsg)
4097{
4098 if (req->io)
4099 return -EAGAIN;
4100 if (io_alloc_async_ctx(req)) {
4101 if (kmsg->iov != kmsg->fast_iov)
4102 kfree(kmsg->iov);
4103 return -ENOMEM;
4104 }
4105 req->flags |= REQ_F_NEED_CLEANUP;
4106 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4107 return -EAGAIN;
4108}
4109
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004110static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4111 struct io_async_msghdr *iomsg)
4112{
4113 iomsg->iov = iomsg->fast_iov;
4114 iomsg->msg.msg_name = &iomsg->addr;
4115 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4116 req->sr_msg.msg_flags, &iomsg->iov);
4117}
4118
Jens Axboe3529d8c2019-12-19 18:24:38 -07004119static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004120{
Jens Axboee47293f2019-12-20 08:58:21 -07004121 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004122 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004123 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004124
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004125 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4126 return -EINVAL;
4127
Jens Axboee47293f2019-12-20 08:58:21 -07004128 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004129 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004130 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004131
Jens Axboed8768362020-02-27 14:17:49 -07004132#ifdef CONFIG_COMPAT
4133 if (req->ctx->compat)
4134 sr->msg_flags |= MSG_CMSG_COMPAT;
4135#endif
4136
Jens Axboefddafac2020-01-04 20:19:44 -07004137 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004138 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004139 /* iovec is already imported */
4140 if (req->flags & REQ_F_NEED_CLEANUP)
4141 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004142
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004143 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004144 if (!ret)
4145 req->flags |= REQ_F_NEED_CLEANUP;
4146 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004147}
4148
Jens Axboe229a7b62020-06-22 10:13:11 -06004149static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4150 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004151{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004152 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004153 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004154 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004155 int ret;
4156
Jens Axboe03b12302019-12-02 18:50:25 -07004157 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004158 if (unlikely(!sock))
4159 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004160
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004161 if (req->io) {
4162 kmsg = &req->io->msg;
4163 kmsg->msg.msg_name = &req->io->msg.addr;
4164 /* if iov is set, it's allocated already */
4165 if (!kmsg->iov)
4166 kmsg->iov = kmsg->fast_iov;
4167 kmsg->msg.msg_iter.iov = kmsg->iov;
4168 } else {
4169 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004170 if (ret)
4171 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004172 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004173 }
4174
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004175 flags = req->sr_msg.msg_flags;
4176 if (flags & MSG_DONTWAIT)
4177 req->flags |= REQ_F_NOWAIT;
4178 else if (force_nonblock)
4179 flags |= MSG_DONTWAIT;
4180
4181 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4182 if (force_nonblock && ret == -EAGAIN)
4183 return io_setup_async_msg(req, kmsg);
4184 if (ret == -ERESTARTSYS)
4185 ret = -EINTR;
4186
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004187 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004188 kfree(kmsg->iov);
4189 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004190 if (ret < 0)
4191 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004192 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004193 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004194}
4195
Jens Axboe229a7b62020-06-22 10:13:11 -06004196static int io_send(struct io_kiocb *req, bool force_nonblock,
4197 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004198{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004199 struct io_sr_msg *sr = &req->sr_msg;
4200 struct msghdr msg;
4201 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004202 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004203 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004204 int ret;
4205
4206 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004207 if (unlikely(!sock))
4208 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004209
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004210 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4211 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004212 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004213
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004214 msg.msg_name = NULL;
4215 msg.msg_control = NULL;
4216 msg.msg_controllen = 0;
4217 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004218
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004219 flags = req->sr_msg.msg_flags;
4220 if (flags & MSG_DONTWAIT)
4221 req->flags |= REQ_F_NOWAIT;
4222 else if (force_nonblock)
4223 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004224
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004225 msg.msg_flags = flags;
4226 ret = sock_sendmsg(sock, &msg);
4227 if (force_nonblock && ret == -EAGAIN)
4228 return -EAGAIN;
4229 if (ret == -ERESTARTSYS)
4230 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004231
Jens Axboe03b12302019-12-02 18:50:25 -07004232 if (ret < 0)
4233 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004234 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004235 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004236}
4237
Pavel Begunkov1400e692020-07-12 20:41:05 +03004238static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4239 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004240{
4241 struct io_sr_msg *sr = &req->sr_msg;
4242 struct iovec __user *uiov;
4243 size_t iov_len;
4244 int ret;
4245
Pavel Begunkov1400e692020-07-12 20:41:05 +03004246 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4247 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004248 if (ret)
4249 return ret;
4250
4251 if (req->flags & REQ_F_BUFFER_SELECT) {
4252 if (iov_len > 1)
4253 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004254 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004255 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004256 sr->len = iomsg->iov[0].iov_len;
4257 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004258 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004259 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004260 } else {
4261 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004262 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004263 if (ret > 0)
4264 ret = 0;
4265 }
4266
4267 return ret;
4268}
4269
4270#ifdef CONFIG_COMPAT
4271static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004272 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004273{
4274 struct compat_msghdr __user *msg_compat;
4275 struct io_sr_msg *sr = &req->sr_msg;
4276 struct compat_iovec __user *uiov;
4277 compat_uptr_t ptr;
4278 compat_size_t len;
4279 int ret;
4280
Pavel Begunkov270a5942020-07-12 20:41:04 +03004281 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004282 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004283 &ptr, &len);
4284 if (ret)
4285 return ret;
4286
4287 uiov = compat_ptr(ptr);
4288 if (req->flags & REQ_F_BUFFER_SELECT) {
4289 compat_ssize_t clen;
4290
4291 if (len > 1)
4292 return -EINVAL;
4293 if (!access_ok(uiov, sizeof(*uiov)))
4294 return -EFAULT;
4295 if (__get_user(clen, &uiov->iov_len))
4296 return -EFAULT;
4297 if (clen < 0)
4298 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004299 sr->len = iomsg->iov[0].iov_len;
4300 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004301 } else {
4302 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004303 &iomsg->iov,
4304 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004305 if (ret < 0)
4306 return ret;
4307 }
4308
4309 return 0;
4310}
Jens Axboe03b12302019-12-02 18:50:25 -07004311#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004312
Pavel Begunkov1400e692020-07-12 20:41:05 +03004313static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4314 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004315{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004316 iomsg->msg.msg_name = &iomsg->addr;
4317 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004318
4319#ifdef CONFIG_COMPAT
4320 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004321 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004322#endif
4323
Pavel Begunkov1400e692020-07-12 20:41:05 +03004324 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004325}
4326
Jens Axboebcda7ba2020-02-23 16:42:51 -07004327static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004328 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004329{
4330 struct io_sr_msg *sr = &req->sr_msg;
4331 struct io_buffer *kbuf;
4332
Jens Axboebcda7ba2020-02-23 16:42:51 -07004333 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4334 if (IS_ERR(kbuf))
4335 return kbuf;
4336
4337 sr->kbuf = kbuf;
4338 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004339 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004340}
4341
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004342static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4343{
4344 return io_put_kbuf(req, req->sr_msg.kbuf);
4345}
4346
Jens Axboe3529d8c2019-12-19 18:24:38 -07004347static int io_recvmsg_prep(struct io_kiocb *req,
4348 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004349{
Jens Axboee47293f2019-12-20 08:58:21 -07004350 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004351 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004352 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004353
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004354 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4355 return -EINVAL;
4356
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004358 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004359 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004360 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004361
Jens Axboed8768362020-02-27 14:17:49 -07004362#ifdef CONFIG_COMPAT
4363 if (req->ctx->compat)
4364 sr->msg_flags |= MSG_CMSG_COMPAT;
4365#endif
4366
Jens Axboefddafac2020-01-04 20:19:44 -07004367 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004368 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004369 /* iovec is already imported */
4370 if (req->flags & REQ_F_NEED_CLEANUP)
4371 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004372
Pavel Begunkov1400e692020-07-12 20:41:05 +03004373 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004374 if (!ret)
4375 req->flags |= REQ_F_NEED_CLEANUP;
4376 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004377}
4378
Jens Axboe229a7b62020-06-22 10:13:11 -06004379static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4380 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004381{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004382 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004383 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004384 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004385 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004386 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004387
Jens Axboe0fa03c62019-04-19 13:34:07 -06004388 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004389 if (unlikely(!sock))
4390 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004391
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004392 if (req->io) {
4393 kmsg = &req->io->msg;
4394 kmsg->msg.msg_name = &req->io->msg.addr;
4395 /* if iov is set, it's allocated already */
4396 if (!kmsg->iov)
4397 kmsg->iov = kmsg->fast_iov;
4398 kmsg->msg.msg_iter.iov = kmsg->iov;
4399 } else {
4400 ret = io_recvmsg_copy_hdr(req, &iomsg);
4401 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004402 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004403 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004404 }
4405
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004406 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004407 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004408 if (IS_ERR(kbuf))
4409 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004410 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4411 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4412 1, req->sr_msg.len);
4413 }
4414
4415 flags = req->sr_msg.msg_flags;
4416 if (flags & MSG_DONTWAIT)
4417 req->flags |= REQ_F_NOWAIT;
4418 else if (force_nonblock)
4419 flags |= MSG_DONTWAIT;
4420
4421 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4422 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004423 if (force_nonblock && ret == -EAGAIN)
4424 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004425 if (ret == -ERESTARTSYS)
4426 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004427
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004428 if (req->flags & REQ_F_BUFFER_SELECTED)
4429 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004430 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004431 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004432 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004433 if (ret < 0)
4434 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004435 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004436 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004437}
4438
Jens Axboe229a7b62020-06-22 10:13:11 -06004439static int io_recv(struct io_kiocb *req, bool force_nonblock,
4440 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004441{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004442 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004443 struct io_sr_msg *sr = &req->sr_msg;
4444 struct msghdr msg;
4445 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004446 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004447 struct iovec iov;
4448 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004449 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004450
Jens Axboefddafac2020-01-04 20:19:44 -07004451 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004452 if (unlikely(!sock))
4453 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004454
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004455 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004456 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004457 if (IS_ERR(kbuf))
4458 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004459 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004460 }
4461
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004462 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004463 if (unlikely(ret))
4464 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004465
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004466 msg.msg_name = NULL;
4467 msg.msg_control = NULL;
4468 msg.msg_controllen = 0;
4469 msg.msg_namelen = 0;
4470 msg.msg_iocb = NULL;
4471 msg.msg_flags = 0;
4472
4473 flags = req->sr_msg.msg_flags;
4474 if (flags & MSG_DONTWAIT)
4475 req->flags |= REQ_F_NOWAIT;
4476 else if (force_nonblock)
4477 flags |= MSG_DONTWAIT;
4478
4479 ret = sock_recvmsg(sock, &msg, flags);
4480 if (force_nonblock && ret == -EAGAIN)
4481 return -EAGAIN;
4482 if (ret == -ERESTARTSYS)
4483 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004484out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004485 if (req->flags & REQ_F_BUFFER_SELECTED)
4486 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004487 if (ret < 0)
4488 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004489 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004490 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004491}
4492
Jens Axboe3529d8c2019-12-19 18:24:38 -07004493static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004494{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004495 struct io_accept *accept = &req->accept;
4496
Jens Axboe17f2fe32019-10-17 14:42:58 -06004497 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4498 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004499 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004500 return -EINVAL;
4501
Jens Axboed55e5f52019-12-11 16:12:15 -07004502 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4503 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004504 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004505 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004506 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004507}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004508
Jens Axboe229a7b62020-06-22 10:13:11 -06004509static int io_accept(struct io_kiocb *req, bool force_nonblock,
4510 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004511{
4512 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004513 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004514 int ret;
4515
Jiufei Xuee697dee2020-06-10 13:41:59 +08004516 if (req->file->f_flags & O_NONBLOCK)
4517 req->flags |= REQ_F_NOWAIT;
4518
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004519 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004520 accept->addr_len, accept->flags,
4521 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004522 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004523 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004524 if (ret < 0) {
4525 if (ret == -ERESTARTSYS)
4526 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004527 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004528 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004529 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004530 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004531}
4532
Jens Axboe3529d8c2019-12-19 18:24:38 -07004533static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004534{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004535 struct io_connect *conn = &req->connect;
4536 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004537
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004538 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4539 return -EINVAL;
4540 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4541 return -EINVAL;
4542
Jens Axboe3529d8c2019-12-19 18:24:38 -07004543 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4544 conn->addr_len = READ_ONCE(sqe->addr2);
4545
4546 if (!io)
4547 return 0;
4548
4549 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004550 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004551}
4552
Jens Axboe229a7b62020-06-22 10:13:11 -06004553static int io_connect(struct io_kiocb *req, bool force_nonblock,
4554 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004555{
Jens Axboef499a022019-12-02 16:28:46 -07004556 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004557 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004558 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004559
Jens Axboef499a022019-12-02 16:28:46 -07004560 if (req->io) {
4561 io = req->io;
4562 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004563 ret = move_addr_to_kernel(req->connect.addr,
4564 req->connect.addr_len,
4565 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004566 if (ret)
4567 goto out;
4568 io = &__io;
4569 }
4570
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004571 file_flags = force_nonblock ? O_NONBLOCK : 0;
4572
4573 ret = __sys_connect_file(req->file, &io->connect.address,
4574 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004575 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004576 if (req->io)
4577 return -EAGAIN;
4578 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004579 ret = -ENOMEM;
4580 goto out;
4581 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004582 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004583 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004584 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004585 if (ret == -ERESTARTSYS)
4586 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004587out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004588 if (ret < 0)
4589 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004590 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004591 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004592}
YueHaibing469956e2020-03-04 15:53:52 +08004593#else /* !CONFIG_NET */
4594static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4595{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004596 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004597}
4598
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004599static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4600 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004601{
YueHaibing469956e2020-03-04 15:53:52 +08004602 return -EOPNOTSUPP;
4603}
4604
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004605static int io_send(struct io_kiocb *req, bool force_nonblock,
4606 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004607{
4608 return -EOPNOTSUPP;
4609}
4610
4611static int io_recvmsg_prep(struct io_kiocb *req,
4612 const struct io_uring_sqe *sqe)
4613{
4614 return -EOPNOTSUPP;
4615}
4616
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004617static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4618 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004619{
4620 return -EOPNOTSUPP;
4621}
4622
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004623static int io_recv(struct io_kiocb *req, bool force_nonblock,
4624 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004625{
4626 return -EOPNOTSUPP;
4627}
4628
4629static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4630{
4631 return -EOPNOTSUPP;
4632}
4633
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004634static int io_accept(struct io_kiocb *req, bool force_nonblock,
4635 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004636{
4637 return -EOPNOTSUPP;
4638}
4639
4640static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4641{
4642 return -EOPNOTSUPP;
4643}
4644
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004645static int io_connect(struct io_kiocb *req, bool force_nonblock,
4646 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004647{
4648 return -EOPNOTSUPP;
4649}
4650#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004651
Jens Axboed7718a92020-02-14 22:23:12 -07004652struct io_poll_table {
4653 struct poll_table_struct pt;
4654 struct io_kiocb *req;
4655 int error;
4656};
4657
Jens Axboed7718a92020-02-14 22:23:12 -07004658static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4659 __poll_t mask, task_work_func_t func)
4660{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004661 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004662 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004663
4664 /* for instances that support it check for an event match first: */
4665 if (mask && !(mask & poll->events))
4666 return 0;
4667
4668 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4669
4670 list_del_init(&poll->wait.entry);
4671
Jens Axboed7718a92020-02-14 22:23:12 -07004672 req->result = mask;
4673 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004674 percpu_ref_get(&req->ctx->refs);
4675
Jens Axboed7718a92020-02-14 22:23:12 -07004676 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004677 * If we using the signalfd wait_queue_head for this wakeup, then
4678 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4679 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4680 * either, as the normal wakeup will suffice.
4681 */
4682 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4683
4684 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004685 * If this fails, then the task is exiting. When a task exits, the
4686 * work gets canceled, so just cancel this request as well instead
4687 * of executing it. We can't safely execute it anyway, as we may not
4688 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004689 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004690 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004691 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004692 struct task_struct *tsk;
4693
Jens Axboee3aabf92020-05-18 11:04:17 -06004694 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004695 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004696 task_work_add(tsk, &req->task_work, 0);
4697 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004698 }
Jens Axboed7718a92020-02-14 22:23:12 -07004699 return 1;
4700}
4701
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004702static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4703 __acquires(&req->ctx->completion_lock)
4704{
4705 struct io_ring_ctx *ctx = req->ctx;
4706
4707 if (!req->result && !READ_ONCE(poll->canceled)) {
4708 struct poll_table_struct pt = { ._key = poll->events };
4709
4710 req->result = vfs_poll(req->file, &pt) & poll->events;
4711 }
4712
4713 spin_lock_irq(&ctx->completion_lock);
4714 if (!req->result && !READ_ONCE(poll->canceled)) {
4715 add_wait_queue(poll->head, &poll->wait);
4716 return true;
4717 }
4718
4719 return false;
4720}
4721
Jens Axboed4e7cd32020-08-15 11:44:50 -07004722static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004723{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004724 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4725 if (req->opcode == IORING_OP_POLL_ADD)
4726 return (struct io_poll_iocb *) req->io;
4727 return req->apoll->double_poll;
4728}
4729
4730static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4731{
4732 if (req->opcode == IORING_OP_POLL_ADD)
4733 return &req->poll;
4734 return &req->apoll->poll;
4735}
4736
4737static void io_poll_remove_double(struct io_kiocb *req)
4738{
4739 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004740
4741 lockdep_assert_held(&req->ctx->completion_lock);
4742
4743 if (poll && poll->head) {
4744 struct wait_queue_head *head = poll->head;
4745
4746 spin_lock(&head->lock);
4747 list_del_init(&poll->wait.entry);
4748 if (poll->wait.private)
4749 refcount_dec(&req->refs);
4750 poll->head = NULL;
4751 spin_unlock(&head->lock);
4752 }
4753}
4754
4755static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4756{
4757 struct io_ring_ctx *ctx = req->ctx;
4758
Jens Axboed4e7cd32020-08-15 11:44:50 -07004759 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004760 req->poll.done = true;
4761 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4762 io_commit_cqring(ctx);
4763}
4764
4765static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4766{
4767 struct io_ring_ctx *ctx = req->ctx;
4768
4769 if (io_poll_rewait(req, &req->poll)) {
4770 spin_unlock_irq(&ctx->completion_lock);
4771 return;
4772 }
4773
4774 hash_del(&req->hash_node);
4775 io_poll_complete(req, req->result, 0);
4776 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004777 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004778 spin_unlock_irq(&ctx->completion_lock);
4779
4780 io_cqring_ev_posted(ctx);
4781}
4782
4783static void io_poll_task_func(struct callback_head *cb)
4784{
4785 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004786 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004787 struct io_kiocb *nxt = NULL;
4788
4789 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004790 if (nxt)
4791 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004792 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004793}
4794
4795static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4796 int sync, void *key)
4797{
4798 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004799 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004800 __poll_t mask = key_to_poll(key);
4801
4802 /* for instances that support it check for an event match first: */
4803 if (mask && !(mask & poll->events))
4804 return 0;
4805
Jens Axboe8706e042020-09-28 08:38:54 -06004806 list_del_init(&wait->entry);
4807
Jens Axboe807abcb2020-07-17 17:09:27 -06004808 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004809 bool done;
4810
Jens Axboe807abcb2020-07-17 17:09:27 -06004811 spin_lock(&poll->head->lock);
4812 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004813 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004814 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004815 /* make sure double remove sees this as being gone */
4816 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004817 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004818 if (!done)
4819 __io_async_wake(req, poll, mask, io_poll_task_func);
4820 }
4821 refcount_dec(&req->refs);
4822 return 1;
4823}
4824
4825static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4826 wait_queue_func_t wake_func)
4827{
4828 poll->head = NULL;
4829 poll->done = false;
4830 poll->canceled = false;
4831 poll->events = events;
4832 INIT_LIST_HEAD(&poll->wait.entry);
4833 init_waitqueue_func_entry(&poll->wait, wake_func);
4834}
4835
4836static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004837 struct wait_queue_head *head,
4838 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004839{
4840 struct io_kiocb *req = pt->req;
4841
4842 /*
4843 * If poll->head is already set, it's because the file being polled
4844 * uses multiple waitqueues for poll handling (eg one for read, one
4845 * for write). Setup a separate io_poll_iocb if this happens.
4846 */
4847 if (unlikely(poll->head)) {
4848 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004849 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004850 pt->error = -EINVAL;
4851 return;
4852 }
4853 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4854 if (!poll) {
4855 pt->error = -ENOMEM;
4856 return;
4857 }
4858 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4859 refcount_inc(&req->refs);
4860 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004861 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004862 }
4863
4864 pt->error = 0;
4865 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004866
4867 if (poll->events & EPOLLEXCLUSIVE)
4868 add_wait_queue_exclusive(head, &poll->wait);
4869 else
4870 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004871}
4872
4873static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4874 struct poll_table_struct *p)
4875{
4876 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004877 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004878
Jens Axboe807abcb2020-07-17 17:09:27 -06004879 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004880}
4881
Jens Axboed7718a92020-02-14 22:23:12 -07004882static void io_async_task_func(struct callback_head *cb)
4883{
4884 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4885 struct async_poll *apoll = req->apoll;
4886 struct io_ring_ctx *ctx = req->ctx;
4887
4888 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4889
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004890 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004891 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004892 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004893 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004894 }
4895
Jens Axboe31067252020-05-17 17:43:31 -06004896 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004897 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004898 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004899
Jens Axboed4e7cd32020-08-15 11:44:50 -07004900 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004901 spin_unlock_irq(&ctx->completion_lock);
4902
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004903 if (!READ_ONCE(apoll->poll.canceled))
4904 __io_req_task_submit(req);
4905 else
4906 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004907
Jens Axboe6d816e02020-08-11 08:04:14 -06004908 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004909 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004910 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004911}
4912
4913static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4914 void *key)
4915{
4916 struct io_kiocb *req = wait->private;
4917 struct io_poll_iocb *poll = &req->apoll->poll;
4918
4919 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4920 key_to_poll(key));
4921
4922 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4923}
4924
4925static void io_poll_req_insert(struct io_kiocb *req)
4926{
4927 struct io_ring_ctx *ctx = req->ctx;
4928 struct hlist_head *list;
4929
4930 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4931 hlist_add_head(&req->hash_node, list);
4932}
4933
4934static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4935 struct io_poll_iocb *poll,
4936 struct io_poll_table *ipt, __poll_t mask,
4937 wait_queue_func_t wake_func)
4938 __acquires(&ctx->completion_lock)
4939{
4940 struct io_ring_ctx *ctx = req->ctx;
4941 bool cancel = false;
4942
Jens Axboe18bceab2020-05-15 11:56:54 -06004943 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004944 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004945 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004946
4947 ipt->pt._key = mask;
4948 ipt->req = req;
4949 ipt->error = -EINVAL;
4950
Jens Axboed7718a92020-02-14 22:23:12 -07004951 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4952
4953 spin_lock_irq(&ctx->completion_lock);
4954 if (likely(poll->head)) {
4955 spin_lock(&poll->head->lock);
4956 if (unlikely(list_empty(&poll->wait.entry))) {
4957 if (ipt->error)
4958 cancel = true;
4959 ipt->error = 0;
4960 mask = 0;
4961 }
4962 if (mask || ipt->error)
4963 list_del_init(&poll->wait.entry);
4964 else if (cancel)
4965 WRITE_ONCE(poll->canceled, true);
4966 else if (!poll->done) /* actually waiting for an event */
4967 io_poll_req_insert(req);
4968 spin_unlock(&poll->head->lock);
4969 }
4970
4971 return mask;
4972}
4973
4974static bool io_arm_poll_handler(struct io_kiocb *req)
4975{
4976 const struct io_op_def *def = &io_op_defs[req->opcode];
4977 struct io_ring_ctx *ctx = req->ctx;
4978 struct async_poll *apoll;
4979 struct io_poll_table ipt;
4980 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004981 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004982
4983 if (!req->file || !file_can_poll(req->file))
4984 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004985 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004986 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004987 if (def->pollin)
4988 rw = READ;
4989 else if (def->pollout)
4990 rw = WRITE;
4991 else
4992 return false;
4993 /* if we can't nonblock try, then no point in arming a poll handler */
4994 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004995 return false;
4996
4997 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4998 if (unlikely(!apoll))
4999 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005000 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005001
5002 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005003 req->apoll = apoll;
5004 INIT_HLIST_NODE(&req->hash_node);
5005
Nathan Chancellor8755d972020-03-02 16:01:19 -07005006 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005007 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005008 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005009 if (def->pollout)
5010 mask |= POLLOUT | POLLWRNORM;
5011 mask |= POLLERR | POLLPRI;
5012
5013 ipt.pt._qproc = io_async_queue_proc;
5014
5015 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5016 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005017 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005018 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005019 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005020 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005021 kfree(apoll);
5022 return false;
5023 }
5024 spin_unlock_irq(&ctx->completion_lock);
5025 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5026 apoll->poll.events);
5027 return true;
5028}
5029
5030static bool __io_poll_remove_one(struct io_kiocb *req,
5031 struct io_poll_iocb *poll)
5032{
Jens Axboeb41e9852020-02-17 09:52:41 -07005033 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005034
5035 spin_lock(&poll->head->lock);
5036 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005037 if (!list_empty(&poll->wait.entry)) {
5038 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005039 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005040 }
5041 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005042 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005043 return do_complete;
5044}
5045
5046static bool io_poll_remove_one(struct io_kiocb *req)
5047{
5048 bool do_complete;
5049
Jens Axboed4e7cd32020-08-15 11:44:50 -07005050 io_poll_remove_double(req);
5051
Jens Axboed7718a92020-02-14 22:23:12 -07005052 if (req->opcode == IORING_OP_POLL_ADD) {
5053 do_complete = __io_poll_remove_one(req, &req->poll);
5054 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005055 struct async_poll *apoll = req->apoll;
5056
Jens Axboed7718a92020-02-14 22:23:12 -07005057 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005058 do_complete = __io_poll_remove_one(req, &apoll->poll);
5059 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005060 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005061 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005062 kfree(apoll);
5063 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005064 }
5065
Jens Axboeb41e9852020-02-17 09:52:41 -07005066 if (do_complete) {
5067 io_cqring_fill_event(req, -ECANCELED);
5068 io_commit_cqring(req->ctx);
5069 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005070 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005071 io_put_req(req);
5072 }
5073
5074 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005075}
5076
Jens Axboe76e1b642020-09-26 15:05:03 -06005077/*
5078 * Returns true if we found and killed one or more poll requests
5079 */
5080static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005081{
Jens Axboe78076bb2019-12-04 19:56:40 -07005082 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005083 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005084 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005085
5086 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005087 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5088 struct hlist_head *list;
5089
5090 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005091 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5092 if (io_task_match(req, tsk))
5093 posted += io_poll_remove_one(req);
5094 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005095 }
5096 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005097
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005098 if (posted)
5099 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005100
5101 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005102}
5103
Jens Axboe47f46762019-11-09 17:43:02 -07005104static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5105{
Jens Axboe78076bb2019-12-04 19:56:40 -07005106 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005107 struct io_kiocb *req;
5108
Jens Axboe78076bb2019-12-04 19:56:40 -07005109 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5110 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005111 if (sqe_addr != req->user_data)
5112 continue;
5113 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005114 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005115 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005116 }
5117
5118 return -ENOENT;
5119}
5120
Jens Axboe3529d8c2019-12-19 18:24:38 -07005121static int io_poll_remove_prep(struct io_kiocb *req,
5122 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005123{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005124 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5125 return -EINVAL;
5126 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5127 sqe->poll_events)
5128 return -EINVAL;
5129
Jens Axboe0969e782019-12-17 18:40:57 -07005130 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005131 return 0;
5132}
5133
5134/*
5135 * Find a running poll command that matches one specified in sqe->addr,
5136 * and remove it if found.
5137 */
5138static int io_poll_remove(struct io_kiocb *req)
5139{
5140 struct io_ring_ctx *ctx = req->ctx;
5141 u64 addr;
5142 int ret;
5143
Jens Axboe0969e782019-12-17 18:40:57 -07005144 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005145 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005146 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005147 spin_unlock_irq(&ctx->completion_lock);
5148
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005149 if (ret < 0)
5150 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005151 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005152 return 0;
5153}
5154
Jens Axboe221c5eb2019-01-17 09:41:58 -07005155static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5156 void *key)
5157{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005158 struct io_kiocb *req = wait->private;
5159 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005160
Jens Axboed7718a92020-02-14 22:23:12 -07005161 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005162}
5163
Jens Axboe221c5eb2019-01-17 09:41:58 -07005164static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5165 struct poll_table_struct *p)
5166{
5167 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5168
Jens Axboe807abcb2020-07-17 17:09:27 -06005169 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005170}
5171
Jens Axboe3529d8c2019-12-19 18:24:38 -07005172static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005173{
5174 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005175 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005176
5177 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5178 return -EINVAL;
5179 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5180 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005181 if (!poll->file)
5182 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005183
Jiufei Xue5769a352020-06-17 17:53:55 +08005184 events = READ_ONCE(sqe->poll32_events);
5185#ifdef __BIG_ENDIAN
5186 events = swahw32(events);
5187#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005188 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5189 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005190 return 0;
5191}
5192
Pavel Begunkov014db002020-03-03 21:33:12 +03005193static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005194{
5195 struct io_poll_iocb *poll = &req->poll;
5196 struct io_ring_ctx *ctx = req->ctx;
5197 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005198 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005199
Jens Axboe78076bb2019-12-04 19:56:40 -07005200 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005201 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005202
Jens Axboed7718a92020-02-14 22:23:12 -07005203 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5204 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005205
Jens Axboe8c838782019-03-12 15:48:16 -06005206 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005207 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005208 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005209 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005210 spin_unlock_irq(&ctx->completion_lock);
5211
Jens Axboe8c838782019-03-12 15:48:16 -06005212 if (mask) {
5213 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005214 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005215 }
Jens Axboe8c838782019-03-12 15:48:16 -06005216 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005217}
5218
Jens Axboe5262f562019-09-17 12:26:57 -06005219static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5220{
Jens Axboead8a48a2019-11-15 08:49:11 -07005221 struct io_timeout_data *data = container_of(timer,
5222 struct io_timeout_data, timer);
5223 struct io_kiocb *req = data->req;
5224 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005225 unsigned long flags;
5226
Jens Axboe5262f562019-09-17 12:26:57 -06005227 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005228 atomic_set(&req->ctx->cq_timeouts,
5229 atomic_read(&req->ctx->cq_timeouts) + 1);
5230
zhangyi (F)ef036812019-10-23 15:10:08 +08005231 /*
Jens Axboe11365042019-10-16 09:08:32 -06005232 * We could be racing with timeout deletion. If the list is empty,
5233 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005234 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005235 if (!list_empty(&req->timeout.list))
5236 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005237
Jens Axboe78e19bb2019-11-06 15:21:34 -07005238 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005239 io_commit_cqring(ctx);
5240 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5241
5242 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005243 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005244 io_put_req(req);
5245 return HRTIMER_NORESTART;
5246}
5247
Jens Axboef254ac02020-08-12 17:33:30 -06005248static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005249{
Jens Axboef254ac02020-08-12 17:33:30 -06005250 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005251
Jens Axboef254ac02020-08-12 17:33:30 -06005252 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005253
Jens Axboe2d283902019-12-04 11:08:05 -07005254 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005255 if (ret == -1)
5256 return -EALREADY;
5257
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005258 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005259 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005260 io_cqring_fill_event(req, -ECANCELED);
5261 io_put_req(req);
5262 return 0;
5263}
5264
Jens Axboef254ac02020-08-12 17:33:30 -06005265static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5266{
5267 struct io_kiocb *req;
5268 int ret = -ENOENT;
5269
5270 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5271 if (user_data == req->user_data) {
5272 ret = 0;
5273 break;
5274 }
5275 }
5276
5277 if (ret == -ENOENT)
5278 return ret;
5279
5280 return __io_timeout_cancel(req);
5281}
5282
Jens Axboe3529d8c2019-12-19 18:24:38 -07005283static int io_timeout_remove_prep(struct io_kiocb *req,
5284 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005285{
Jens Axboeb29472e2019-12-17 18:50:29 -07005286 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5287 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005288 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5289 return -EINVAL;
5290 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005291 return -EINVAL;
5292
5293 req->timeout.addr = READ_ONCE(sqe->addr);
5294 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5295 if (req->timeout.flags)
5296 return -EINVAL;
5297
Jens Axboeb29472e2019-12-17 18:50:29 -07005298 return 0;
5299}
5300
Jens Axboe11365042019-10-16 09:08:32 -06005301/*
5302 * Remove or update an existing timeout command
5303 */
Jens Axboefc4df992019-12-10 14:38:45 -07005304static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005305{
5306 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005307 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005308
Jens Axboe11365042019-10-16 09:08:32 -06005309 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005310 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005311
Jens Axboe47f46762019-11-09 17:43:02 -07005312 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005313 io_commit_cqring(ctx);
5314 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005315 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005316 if (ret < 0)
5317 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005318 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005319 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005320}
5321
Jens Axboe3529d8c2019-12-19 18:24:38 -07005322static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005323 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005324{
Jens Axboead8a48a2019-11-15 08:49:11 -07005325 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005326 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005327 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005328
Jens Axboead8a48a2019-11-15 08:49:11 -07005329 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005330 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005331 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005332 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005333 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005334 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005335 flags = READ_ONCE(sqe->timeout_flags);
5336 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005337 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005338
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005339 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005340
Jens Axboe3529d8c2019-12-19 18:24:38 -07005341 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005342 return -ENOMEM;
5343
5344 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005345 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005346
5347 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005348 return -EFAULT;
5349
Jens Axboe11365042019-10-16 09:08:32 -06005350 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005351 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005352 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005353 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005354
Jens Axboead8a48a2019-11-15 08:49:11 -07005355 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5356 return 0;
5357}
5358
Jens Axboefc4df992019-12-10 14:38:45 -07005359static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005360{
Jens Axboead8a48a2019-11-15 08:49:11 -07005361 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005362 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005363 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005364 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005365
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005366 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005367
Jens Axboe5262f562019-09-17 12:26:57 -06005368 /*
5369 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005370 * timeout event to be satisfied. If it isn't set, then this is
5371 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005372 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005373 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005374 entry = ctx->timeout_list.prev;
5375 goto add;
5376 }
Jens Axboe5262f562019-09-17 12:26:57 -06005377
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005378 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5379 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005380
5381 /*
5382 * Insertion sort, ensuring the first entry in the list is always
5383 * the one we need first.
5384 */
Jens Axboe5262f562019-09-17 12:26:57 -06005385 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005386 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5387 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005388
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005389 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005390 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005391 /* nxt.seq is behind @tail, otherwise would've been completed */
5392 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005393 break;
5394 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005395add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005396 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005397 data->timer.function = io_timeout_fn;
5398 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005399 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005400 return 0;
5401}
5402
Jens Axboe62755e32019-10-28 21:49:21 -06005403static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005404{
Jens Axboe62755e32019-10-28 21:49:21 -06005405 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005406
Jens Axboe62755e32019-10-28 21:49:21 -06005407 return req->user_data == (unsigned long) data;
5408}
5409
Jens Axboee977d6d2019-11-05 12:39:45 -07005410static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005411{
Jens Axboe62755e32019-10-28 21:49:21 -06005412 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005413 int ret = 0;
5414
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005415 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005416 switch (cancel_ret) {
5417 case IO_WQ_CANCEL_OK:
5418 ret = 0;
5419 break;
5420 case IO_WQ_CANCEL_RUNNING:
5421 ret = -EALREADY;
5422 break;
5423 case IO_WQ_CANCEL_NOTFOUND:
5424 ret = -ENOENT;
5425 break;
5426 }
5427
Jens Axboee977d6d2019-11-05 12:39:45 -07005428 return ret;
5429}
5430
Jens Axboe47f46762019-11-09 17:43:02 -07005431static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5432 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005433 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005434{
5435 unsigned long flags;
5436 int ret;
5437
5438 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5439 if (ret != -ENOENT) {
5440 spin_lock_irqsave(&ctx->completion_lock, flags);
5441 goto done;
5442 }
5443
5444 spin_lock_irqsave(&ctx->completion_lock, flags);
5445 ret = io_timeout_cancel(ctx, sqe_addr);
5446 if (ret != -ENOENT)
5447 goto done;
5448 ret = io_poll_cancel(ctx, sqe_addr);
5449done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005450 if (!ret)
5451 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005452 io_cqring_fill_event(req, ret);
5453 io_commit_cqring(ctx);
5454 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5455 io_cqring_ev_posted(ctx);
5456
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005457 if (ret < 0)
5458 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005459 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005460}
5461
Jens Axboe3529d8c2019-12-19 18:24:38 -07005462static int io_async_cancel_prep(struct io_kiocb *req,
5463 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005464{
Jens Axboefbf23842019-12-17 18:45:56 -07005465 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005466 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005467 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5468 return -EINVAL;
5469 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005470 return -EINVAL;
5471
Jens Axboefbf23842019-12-17 18:45:56 -07005472 req->cancel.addr = READ_ONCE(sqe->addr);
5473 return 0;
5474}
5475
Pavel Begunkov014db002020-03-03 21:33:12 +03005476static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005477{
5478 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005479
Pavel Begunkov014db002020-03-03 21:33:12 +03005480 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005481 return 0;
5482}
5483
Jens Axboe05f3fb32019-12-09 11:22:50 -07005484static int io_files_update_prep(struct io_kiocb *req,
5485 const struct io_uring_sqe *sqe)
5486{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005487 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5488 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005489 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5490 return -EINVAL;
5491 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005492 return -EINVAL;
5493
5494 req->files_update.offset = READ_ONCE(sqe->off);
5495 req->files_update.nr_args = READ_ONCE(sqe->len);
5496 if (!req->files_update.nr_args)
5497 return -EINVAL;
5498 req->files_update.arg = READ_ONCE(sqe->addr);
5499 return 0;
5500}
5501
Jens Axboe229a7b62020-06-22 10:13:11 -06005502static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5503 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005504{
5505 struct io_ring_ctx *ctx = req->ctx;
5506 struct io_uring_files_update up;
5507 int ret;
5508
Jens Axboef86cd202020-01-29 13:46:44 -07005509 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005510 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005511
5512 up.offset = req->files_update.offset;
5513 up.fds = req->files_update.arg;
5514
5515 mutex_lock(&ctx->uring_lock);
5516 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5517 mutex_unlock(&ctx->uring_lock);
5518
5519 if (ret < 0)
5520 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005521 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005522 return 0;
5523}
5524
Jens Axboe3529d8c2019-12-19 18:24:38 -07005525static int io_req_defer_prep(struct io_kiocb *req,
5526 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005527{
Jens Axboee7815732019-12-17 19:45:06 -07005528 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005529
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005530 if (!sqe)
5531 return 0;
5532
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005533 if (io_alloc_async_ctx(req))
5534 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005535 ret = io_prep_work_files(req);
5536 if (unlikely(ret))
5537 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005538
Jens Axboe202700e12020-09-12 13:18:10 -06005539 io_prep_async_work(req);
5540
Jens Axboed625c6e2019-12-17 19:53:05 -07005541 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005542 case IORING_OP_NOP:
5543 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005544 case IORING_OP_READV:
5545 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005546 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005547 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005548 break;
5549 case IORING_OP_WRITEV:
5550 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005551 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005552 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005553 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005554 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005555 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005556 break;
5557 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005558 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005559 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005560 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005561 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005562 break;
5563 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005564 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005565 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005566 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005567 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005568 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005569 break;
5570 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005571 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005572 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005573 break;
Jens Axboef499a022019-12-02 16:28:46 -07005574 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005575 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005576 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005577 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005578 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005579 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005580 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005581 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005582 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005583 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005584 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005585 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005586 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005587 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005588 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005589 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005590 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005591 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005592 case IORING_OP_FALLOCATE:
5593 ret = io_fallocate_prep(req, sqe);
5594 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005595 case IORING_OP_OPENAT:
5596 ret = io_openat_prep(req, sqe);
5597 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005598 case IORING_OP_CLOSE:
5599 ret = io_close_prep(req, sqe);
5600 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005601 case IORING_OP_FILES_UPDATE:
5602 ret = io_files_update_prep(req, sqe);
5603 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005604 case IORING_OP_STATX:
5605 ret = io_statx_prep(req, sqe);
5606 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005607 case IORING_OP_FADVISE:
5608 ret = io_fadvise_prep(req, sqe);
5609 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005610 case IORING_OP_MADVISE:
5611 ret = io_madvise_prep(req, sqe);
5612 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005613 case IORING_OP_OPENAT2:
5614 ret = io_openat2_prep(req, sqe);
5615 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005616 case IORING_OP_EPOLL_CTL:
5617 ret = io_epoll_ctl_prep(req, sqe);
5618 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005619 case IORING_OP_SPLICE:
5620 ret = io_splice_prep(req, sqe);
5621 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005622 case IORING_OP_PROVIDE_BUFFERS:
5623 ret = io_provide_buffers_prep(req, sqe);
5624 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005625 case IORING_OP_REMOVE_BUFFERS:
5626 ret = io_remove_buffers_prep(req, sqe);
5627 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005628 case IORING_OP_TEE:
5629 ret = io_tee_prep(req, sqe);
5630 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005631 default:
Jens Axboee7815732019-12-17 19:45:06 -07005632 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5633 req->opcode);
5634 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005635 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005636 }
5637
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005638 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005639}
5640
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005641static u32 io_get_sequence(struct io_kiocb *req)
5642{
5643 struct io_kiocb *pos;
5644 struct io_ring_ctx *ctx = req->ctx;
5645 u32 total_submitted, nr_reqs = 1;
5646
5647 if (req->flags & REQ_F_LINK_HEAD)
5648 list_for_each_entry(pos, &req->link_list, link_list)
5649 nr_reqs++;
5650
5651 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5652 return total_submitted - nr_reqs;
5653}
5654
Jens Axboe3529d8c2019-12-19 18:24:38 -07005655static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005656{
Jackie Liua197f662019-11-08 08:09:12 -07005657 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005658 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005659 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005660 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005661
Bob Liu9d858b22019-11-13 18:06:25 +08005662 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005663 if (likely(list_empty_careful(&ctx->defer_list) &&
5664 !(req->flags & REQ_F_IO_DRAIN)))
5665 return 0;
5666
5667 seq = io_get_sequence(req);
5668 /* Still a chance to pass the sequence check */
5669 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005670 return 0;
5671
Pavel Begunkov650b5482020-05-17 14:02:11 +03005672 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005673 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005674 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005675 return ret;
5676 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005677 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005678 de = kmalloc(sizeof(*de), GFP_KERNEL);
5679 if (!de)
5680 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005681
Jens Axboede0617e2019-04-06 21:51:27 -06005682 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005683 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005684 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005685 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005686 io_queue_async_work(req);
5687 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005688 }
5689
Jens Axboe915967f2019-11-21 09:01:20 -07005690 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005691 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005692 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005693 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005694 spin_unlock_irq(&ctx->completion_lock);
5695 return -EIOCBQUEUED;
5696}
5697
Jens Axboef573d382020-09-22 10:19:24 -06005698static void io_req_drop_files(struct io_kiocb *req)
5699{
5700 struct io_ring_ctx *ctx = req->ctx;
5701 unsigned long flags;
5702
5703 spin_lock_irqsave(&ctx->inflight_lock, flags);
5704 list_del(&req->inflight_entry);
5705 if (waitqueue_active(&ctx->inflight_wait))
5706 wake_up(&ctx->inflight_wait);
5707 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5708 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005709 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005710 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005711 req->work.files = NULL;
5712}
5713
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005714static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005715{
5716 struct io_async_ctx *io = req->io;
5717
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005718 if (req->flags & REQ_F_BUFFER_SELECTED) {
5719 switch (req->opcode) {
5720 case IORING_OP_READV:
5721 case IORING_OP_READ_FIXED:
5722 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005723 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005724 break;
5725 case IORING_OP_RECVMSG:
5726 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005727 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005728 break;
5729 }
5730 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005731 }
5732
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005733 if (req->flags & REQ_F_NEED_CLEANUP) {
5734 switch (req->opcode) {
5735 case IORING_OP_READV:
5736 case IORING_OP_READ_FIXED:
5737 case IORING_OP_READ:
5738 case IORING_OP_WRITEV:
5739 case IORING_OP_WRITE_FIXED:
5740 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005741 if (io->rw.free_iovec)
5742 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005743 break;
5744 case IORING_OP_RECVMSG:
5745 case IORING_OP_SENDMSG:
5746 if (io->msg.iov != io->msg.fast_iov)
5747 kfree(io->msg.iov);
5748 break;
5749 case IORING_OP_SPLICE:
5750 case IORING_OP_TEE:
5751 io_put_file(req, req->splice.file_in,
5752 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5753 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005754 case IORING_OP_OPENAT:
5755 case IORING_OP_OPENAT2:
5756 if (req->open.filename)
5757 putname(req->open.filename);
5758 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005759 }
5760 req->flags &= ~REQ_F_NEED_CLEANUP;
5761 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005762
Jens Axboef573d382020-09-22 10:19:24 -06005763 if (req->flags & REQ_F_INFLIGHT)
5764 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005765}
5766
Jens Axboe3529d8c2019-12-19 18:24:38 -07005767static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005768 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005769{
Jackie Liua197f662019-11-08 08:09:12 -07005770 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005771 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005772
Jens Axboed625c6e2019-12-17 19:53:05 -07005773 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005774 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005775 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005776 break;
5777 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005778 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005779 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005780 if (sqe) {
5781 ret = io_read_prep(req, sqe, force_nonblock);
5782 if (ret < 0)
5783 break;
5784 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005785 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005786 break;
5787 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005788 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005789 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005790 if (sqe) {
5791 ret = io_write_prep(req, sqe, force_nonblock);
5792 if (ret < 0)
5793 break;
5794 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005795 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005796 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005797 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005798 if (sqe) {
5799 ret = io_prep_fsync(req, sqe);
5800 if (ret < 0)
5801 break;
5802 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005803 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005804 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005805 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005806 if (sqe) {
5807 ret = io_poll_add_prep(req, sqe);
5808 if (ret)
5809 break;
5810 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005811 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005812 break;
5813 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005814 if (sqe) {
5815 ret = io_poll_remove_prep(req, sqe);
5816 if (ret < 0)
5817 break;
5818 }
Jens Axboefc4df992019-12-10 14:38:45 -07005819 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005820 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005821 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005822 if (sqe) {
5823 ret = io_prep_sfr(req, sqe);
5824 if (ret < 0)
5825 break;
5826 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005827 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005828 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005829 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005830 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005831 if (sqe) {
5832 ret = io_sendmsg_prep(req, sqe);
5833 if (ret < 0)
5834 break;
5835 }
Jens Axboefddafac2020-01-04 20:19:44 -07005836 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005837 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005838 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005839 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005840 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005841 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005842 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005843 if (sqe) {
5844 ret = io_recvmsg_prep(req, sqe);
5845 if (ret)
5846 break;
5847 }
Jens Axboefddafac2020-01-04 20:19:44 -07005848 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005849 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005850 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005851 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005852 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005853 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005854 if (sqe) {
5855 ret = io_timeout_prep(req, sqe, false);
5856 if (ret)
5857 break;
5858 }
Jens Axboefc4df992019-12-10 14:38:45 -07005859 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005860 break;
Jens Axboe11365042019-10-16 09:08:32 -06005861 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005862 if (sqe) {
5863 ret = io_timeout_remove_prep(req, sqe);
5864 if (ret)
5865 break;
5866 }
Jens Axboefc4df992019-12-10 14:38:45 -07005867 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005868 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005869 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005870 if (sqe) {
5871 ret = io_accept_prep(req, sqe);
5872 if (ret)
5873 break;
5874 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005875 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005876 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005877 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005878 if (sqe) {
5879 ret = io_connect_prep(req, sqe);
5880 if (ret)
5881 break;
5882 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005883 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005884 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005885 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005886 if (sqe) {
5887 ret = io_async_cancel_prep(req, sqe);
5888 if (ret)
5889 break;
5890 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005891 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005892 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005893 case IORING_OP_FALLOCATE:
5894 if (sqe) {
5895 ret = io_fallocate_prep(req, sqe);
5896 if (ret)
5897 break;
5898 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005899 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005900 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005901 case IORING_OP_OPENAT:
5902 if (sqe) {
5903 ret = io_openat_prep(req, sqe);
5904 if (ret)
5905 break;
5906 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005907 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005908 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005909 case IORING_OP_CLOSE:
5910 if (sqe) {
5911 ret = io_close_prep(req, sqe);
5912 if (ret)
5913 break;
5914 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005915 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005916 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005917 case IORING_OP_FILES_UPDATE:
5918 if (sqe) {
5919 ret = io_files_update_prep(req, sqe);
5920 if (ret)
5921 break;
5922 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005923 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005924 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005925 case IORING_OP_STATX:
5926 if (sqe) {
5927 ret = io_statx_prep(req, sqe);
5928 if (ret)
5929 break;
5930 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005931 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005932 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005933 case IORING_OP_FADVISE:
5934 if (sqe) {
5935 ret = io_fadvise_prep(req, sqe);
5936 if (ret)
5937 break;
5938 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005939 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005940 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005941 case IORING_OP_MADVISE:
5942 if (sqe) {
5943 ret = io_madvise_prep(req, sqe);
5944 if (ret)
5945 break;
5946 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005947 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005948 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005949 case IORING_OP_OPENAT2:
5950 if (sqe) {
5951 ret = io_openat2_prep(req, sqe);
5952 if (ret)
5953 break;
5954 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005955 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005956 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005957 case IORING_OP_EPOLL_CTL:
5958 if (sqe) {
5959 ret = io_epoll_ctl_prep(req, sqe);
5960 if (ret)
5961 break;
5962 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005963 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005964 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005965 case IORING_OP_SPLICE:
5966 if (sqe) {
5967 ret = io_splice_prep(req, sqe);
5968 if (ret < 0)
5969 break;
5970 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005971 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005972 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005973 case IORING_OP_PROVIDE_BUFFERS:
5974 if (sqe) {
5975 ret = io_provide_buffers_prep(req, sqe);
5976 if (ret)
5977 break;
5978 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005979 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005980 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005981 case IORING_OP_REMOVE_BUFFERS:
5982 if (sqe) {
5983 ret = io_remove_buffers_prep(req, sqe);
5984 if (ret)
5985 break;
5986 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005987 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005988 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005989 case IORING_OP_TEE:
5990 if (sqe) {
5991 ret = io_tee_prep(req, sqe);
5992 if (ret < 0)
5993 break;
5994 }
5995 ret = io_tee(req, force_nonblock);
5996 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005997 default:
5998 ret = -EINVAL;
5999 break;
6000 }
6001
6002 if (ret)
6003 return ret;
6004
Jens Axboeb5325762020-05-19 21:20:27 -06006005 /* If the op doesn't have a file, we're not polling for it */
6006 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006007 const bool in_async = io_wq_current_is_worker();
6008
Jens Axboe11ba8202020-01-15 21:51:17 -07006009 /* workqueue context doesn't hold uring_lock, grab it now */
6010 if (in_async)
6011 mutex_lock(&ctx->uring_lock);
6012
Jens Axboe2b188cc2019-01-07 10:46:33 -07006013 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006014
6015 if (in_async)
6016 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006017 }
6018
6019 return 0;
6020}
6021
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006022static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006023{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006024 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006025 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006026 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006027
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006028 timeout = io_prep_linked_timeout(req);
6029 if (timeout)
6030 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006031
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006032 /* if NO_CANCEL is set, we must still run the work */
6033 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6034 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006035 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006036 }
Jens Axboe31b51512019-01-18 22:56:34 -07006037
Jens Axboe561fb042019-10-24 07:25:42 -06006038 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006039 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006040 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006041 /*
6042 * We can get EAGAIN for polled IO even though we're
6043 * forcing a sync submission from here, since we can't
6044 * wait for request slots on the block side.
6045 */
6046 if (ret != -EAGAIN)
6047 break;
6048 cond_resched();
6049 } while (1);
6050 }
Jens Axboe31b51512019-01-18 22:56:34 -07006051
Jens Axboe561fb042019-10-24 07:25:42 -06006052 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006053 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006054 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006055 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006056
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006057 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006058}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006059
Jens Axboe65e19f52019-10-26 07:20:21 -06006060static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6061 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006062{
Jens Axboe65e19f52019-10-26 07:20:21 -06006063 struct fixed_file_table *table;
6064
Jens Axboe05f3fb32019-12-09 11:22:50 -07006065 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006066 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006067}
6068
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006069static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6070 int fd, struct file **out_file, bool fixed)
6071{
6072 struct io_ring_ctx *ctx = req->ctx;
6073 struct file *file;
6074
6075 if (fixed) {
6076 if (unlikely(!ctx->file_data ||
6077 (unsigned) fd >= ctx->nr_user_files))
6078 return -EBADF;
6079 fd = array_index_nospec(fd, ctx->nr_user_files);
6080 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006081 if (file) {
6082 req->fixed_file_refs = ctx->file_data->cur_refs;
6083 percpu_ref_get(req->fixed_file_refs);
6084 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006085 } else {
6086 trace_io_uring_file_get(ctx, fd);
6087 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006088 }
6089
Jens Axboefd2206e2020-06-02 16:40:47 -06006090 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6091 *out_file = file;
6092 return 0;
6093 }
6094 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006095}
6096
Jens Axboe3529d8c2019-12-19 18:24:38 -07006097static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006098 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006099{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006100 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006101
Jens Axboe63ff8222020-05-07 14:56:15 -06006102 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006103 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006104 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006105
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006106 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006107}
6108
Jackie Liua197f662019-11-08 08:09:12 -07006109static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110{
Jackie Liua197f662019-11-08 08:09:12 -07006111 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006112
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006113 io_req_init_async(req);
6114
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006115 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006116 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006117
Jens Axboe0f212202020-09-13 13:09:39 -06006118 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006119 get_nsproxy(current->nsproxy);
6120 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006121 req->flags |= REQ_F_INFLIGHT;
6122
Jens Axboefcb323c2019-10-24 12:39:47 -06006123 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006124 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006125 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006126 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006127}
6128
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006129static inline int io_prep_work_files(struct io_kiocb *req)
6130{
6131 if (!io_op_defs[req->opcode].file_table)
6132 return 0;
6133 return io_grab_files(req);
6134}
6135
Jens Axboe2665abf2019-11-05 12:40:47 -07006136static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6137{
Jens Axboead8a48a2019-11-15 08:49:11 -07006138 struct io_timeout_data *data = container_of(timer,
6139 struct io_timeout_data, timer);
6140 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006141 struct io_ring_ctx *ctx = req->ctx;
6142 struct io_kiocb *prev = NULL;
6143 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006144
6145 spin_lock_irqsave(&ctx->completion_lock, flags);
6146
6147 /*
6148 * We don't expect the list to be empty, that will only happen if we
6149 * race with the completion of the linked work.
6150 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006151 if (!list_empty(&req->link_list)) {
6152 prev = list_entry(req->link_list.prev, struct io_kiocb,
6153 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006154 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006155 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006156 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6157 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006158 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006159 }
6160
6161 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6162
6163 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006164 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006165 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006166 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006167 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006168 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006169 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006170 return HRTIMER_NORESTART;
6171}
6172
Jens Axboe7271ef32020-08-10 09:55:22 -06006173static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006174{
Jens Axboe76a46e02019-11-10 23:34:16 -07006175 /*
6176 * If the list is now empty, then our linked request finished before
6177 * we got a chance to setup the timer
6178 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006179 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006180 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006181
Jens Axboead8a48a2019-11-15 08:49:11 -07006182 data->timer.function = io_link_timeout_fn;
6183 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6184 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006185 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006186}
6187
6188static void io_queue_linked_timeout(struct io_kiocb *req)
6189{
6190 struct io_ring_ctx *ctx = req->ctx;
6191
6192 spin_lock_irq(&ctx->completion_lock);
6193 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006194 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006195
Jens Axboe2665abf2019-11-05 12:40:47 -07006196 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006197 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006198}
6199
Jens Axboead8a48a2019-11-15 08:49:11 -07006200static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006201{
6202 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006203
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006204 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006205 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006206 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006207 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006208
Pavel Begunkov44932332019-12-05 16:16:35 +03006209 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6210 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006211 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006212 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006213
Jens Axboe76a46e02019-11-10 23:34:16 -07006214 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006215 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006216}
6217
Jens Axboef13fad72020-06-22 09:34:30 -06006218static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6219 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006220{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006221 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006222 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006223 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006224 int ret;
6225
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006226again:
6227 linked_timeout = io_prep_linked_timeout(req);
6228
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006229 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6230 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006231 if (old_creds)
6232 revert_creds(old_creds);
6233 if (old_creds == req->work.creds)
6234 old_creds = NULL; /* restored original creds */
6235 else
6236 old_creds = override_creds(req->work.creds);
6237 }
6238
Jens Axboef13fad72020-06-22 09:34:30 -06006239 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006240
6241 /*
6242 * We async punt it if the file wasn't marked NOWAIT, or if the file
6243 * doesn't support non-blocking read/write attempts
6244 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006245 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006246 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006247punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006248 ret = io_prep_work_files(req);
6249 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006250 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006251 /*
6252 * Queued up for async execution, worker will release
6253 * submit reference when the iocb is actually submitted.
6254 */
6255 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006256 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006257
Pavel Begunkovf063c542020-07-25 14:41:59 +03006258 if (linked_timeout)
6259 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006260 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006261 }
Jens Axboee65ef562019-03-12 10:16:44 -06006262
Pavel Begunkov652532a2020-07-03 22:15:07 +03006263 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006264err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006265 /* un-prep timeout, so it'll be killed as any other linked */
6266 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006267 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006268 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006269 io_req_complete(req, ret);
6270 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006271 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006272
Jens Axboe6c271ce2019-01-10 11:22:30 -07006273 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006274 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006275 if (linked_timeout)
6276 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006277
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006278 if (nxt) {
6279 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006280
6281 if (req->flags & REQ_F_FORCE_ASYNC)
6282 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006283 goto again;
6284 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006285exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006286 if (old_creds)
6287 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006288}
6289
Jens Axboef13fad72020-06-22 09:34:30 -06006290static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6291 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006292{
6293 int ret;
6294
Jens Axboe3529d8c2019-12-19 18:24:38 -07006295 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006296 if (ret) {
6297 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006298fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006299 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006300 io_put_req(req);
6301 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006302 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006303 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006304 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006305 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006306 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006307 goto fail_req;
6308 }
6309
Jens Axboece35a472019-12-17 08:04:44 -07006310 /*
6311 * Never try inline submit of IOSQE_ASYNC is set, go straight
6312 * to async execution.
6313 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006314 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006315 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6316 io_queue_async_work(req);
6317 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006318 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006319 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006320}
6321
Jens Axboef13fad72020-06-22 09:34:30 -06006322static inline void io_queue_link_head(struct io_kiocb *req,
6323 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006324{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006325 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006326 io_put_req(req);
6327 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006328 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006329 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006330}
6331
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006332static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006333 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006334{
Jackie Liua197f662019-11-08 08:09:12 -07006335 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006336 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006337
Jens Axboe9e645e112019-05-10 16:07:28 -06006338 /*
6339 * If we already have a head request, queue this one for async
6340 * submittal once the head completes. If we don't have a head but
6341 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6342 * submitted sync once the chain is complete. If none of those
6343 * conditions are true (normal request), then just queue it.
6344 */
6345 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006346 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006347
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006348 /*
6349 * Taking sequential execution of a link, draining both sides
6350 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6351 * requests in the link. So, it drains the head and the
6352 * next after the link request. The last one is done via
6353 * drain_next flag to persist the effect across calls.
6354 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006355 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006356 head->flags |= REQ_F_IO_DRAIN;
6357 ctx->drain_next = 1;
6358 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006359 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006360 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006361 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006362 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006363 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006364 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006365 trace_io_uring_link(ctx, req, head);
6366 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006367
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006368 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006369 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006370 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006371 *link = NULL;
6372 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006373 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006374 if (unlikely(ctx->drain_next)) {
6375 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006376 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006377 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006378 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006379 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006380 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006381
Pavel Begunkov711be032020-01-17 03:57:59 +03006382 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006383 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006384 req->flags |= REQ_F_FAIL_LINK;
6385 *link = req;
6386 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006387 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006388 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006389 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006390
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006391 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006392}
6393
Jens Axboe9a56a232019-01-09 09:06:50 -07006394/*
6395 * Batched submission is done, ensure local IO is flushed out.
6396 */
6397static void io_submit_state_end(struct io_submit_state *state)
6398{
Jens Axboef13fad72020-06-22 09:34:30 -06006399 if (!list_empty(&state->comp.list))
6400 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006401 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006402 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006403 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006404 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006405}
6406
6407/*
6408 * Start submission side cache.
6409 */
6410static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006411 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006412{
6413 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006414 state->comp.nr = 0;
6415 INIT_LIST_HEAD(&state->comp.list);
6416 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006417 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006418 state->file = NULL;
6419 state->ios_left = max_ios;
6420}
6421
Jens Axboe2b188cc2019-01-07 10:46:33 -07006422static void io_commit_sqring(struct io_ring_ctx *ctx)
6423{
Hristo Venev75b28af2019-08-26 17:23:46 +00006424 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006425
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006426 /*
6427 * Ensure any loads from the SQEs are done at this point,
6428 * since once we write the new head, the application could
6429 * write new data to them.
6430 */
6431 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006432}
6433
6434/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006435 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006436 * that is mapped by userspace. This means that care needs to be taken to
6437 * ensure that reads are stable, as we cannot rely on userspace always
6438 * being a good citizen. If members of the sqe are validated and then later
6439 * used, it's important that those reads are done through READ_ONCE() to
6440 * prevent a re-load down the line.
6441 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006442static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006443{
Hristo Venev75b28af2019-08-26 17:23:46 +00006444 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006445 unsigned head;
6446
6447 /*
6448 * The cached sq head (or cq tail) serves two purposes:
6449 *
6450 * 1) allows us to batch the cost of updating the user visible
6451 * head updates.
6452 * 2) allows the kernel side to track the head on its own, even
6453 * though the application is the one updating it.
6454 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006455 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006456 if (likely(head < ctx->sq_entries))
6457 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006458
6459 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006460 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006461 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006462 return NULL;
6463}
6464
6465static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6466{
6467 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006468}
6469
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006470/*
6471 * Check SQE restrictions (opcode and flags).
6472 *
6473 * Returns 'true' if SQE is allowed, 'false' otherwise.
6474 */
6475static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6476 struct io_kiocb *req,
6477 unsigned int sqe_flags)
6478{
6479 if (!ctx->restricted)
6480 return true;
6481
6482 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6483 return false;
6484
6485 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6486 ctx->restrictions.sqe_flags_required)
6487 return false;
6488
6489 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6490 ctx->restrictions.sqe_flags_required))
6491 return false;
6492
6493 return true;
6494}
6495
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006496#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6497 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6498 IOSQE_BUFFER_SELECT)
6499
6500static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6501 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006502 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006503{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006504 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006505 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006506
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006507 req->opcode = READ_ONCE(sqe->opcode);
6508 req->user_data = READ_ONCE(sqe->user_data);
6509 req->io = NULL;
6510 req->file = NULL;
6511 req->ctx = ctx;
6512 req->flags = 0;
6513 /* one is dropped after submission, the other at completion */
6514 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006515 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006516 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006517 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006518 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006519
6520 if (unlikely(req->opcode >= IORING_OP_LAST))
6521 return -EINVAL;
6522
Jens Axboe9d8426a2020-06-16 18:42:49 -06006523 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6524 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006525
6526 sqe_flags = READ_ONCE(sqe->flags);
6527 /* enforce forwards compatibility on users */
6528 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6529 return -EINVAL;
6530
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006531 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6532 return -EACCES;
6533
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006534 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6535 !io_op_defs[req->opcode].buffer_select)
6536 return -EOPNOTSUPP;
6537
6538 id = READ_ONCE(sqe->personality);
6539 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006540 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006541 req->work.creds = idr_find(&ctx->personality_idr, id);
6542 if (unlikely(!req->work.creds))
6543 return -EINVAL;
6544 get_cred(req->work.creds);
6545 }
6546
6547 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006548 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006549
Jens Axboe63ff8222020-05-07 14:56:15 -06006550 if (!io_op_defs[req->opcode].needs_file)
6551 return 0;
6552
6553 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006554}
6555
Jens Axboe0f212202020-09-13 13:09:39 -06006556static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006557{
Jens Axboeac8691c2020-06-01 08:30:41 -06006558 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006559 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006560 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006561
Jens Axboec4a2ed72019-11-21 21:01:26 -07006562 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006563 if (test_bit(0, &ctx->sq_check_overflow)) {
6564 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006565 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006566 return -EBUSY;
6567 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006568
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006569 /* make sure SQ entry isn't read before tail */
6570 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006571
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006572 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6573 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006574
Jens Axboe013538b2020-06-22 09:29:15 -06006575 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006576
6577 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006578 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006579 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006580 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006581
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006582 sqe = io_get_sqe(ctx);
6583 if (unlikely(!sqe)) {
6584 io_consume_sqe(ctx);
6585 break;
6586 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006587 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006588 if (unlikely(!req)) {
6589 if (!submitted)
6590 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006591 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006592 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006593
Jens Axboeac8691c2020-06-01 08:30:41 -06006594 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006595 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006596 /* will complete beyond this point, count as submitted */
6597 submitted++;
6598
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006599 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006600fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006601 io_put_req(req);
6602 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006603 break;
6604 }
6605
Jens Axboe354420f2020-01-08 18:55:15 -07006606 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006607 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006608 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006609 if (err)
6610 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006611 }
6612
Pavel Begunkov9466f432020-01-25 22:34:01 +03006613 if (unlikely(submitted != nr)) {
6614 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6615
6616 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6617 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006618 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006619 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006620 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006621
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006622 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6623 io_commit_sqring(ctx);
6624
Jens Axboe6c271ce2019-01-10 11:22:30 -07006625 return submitted;
6626}
6627
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006628static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6629{
6630 /* Tell userspace we may need a wakeup call */
6631 spin_lock_irq(&ctx->completion_lock);
6632 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6633 spin_unlock_irq(&ctx->completion_lock);
6634}
6635
6636static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6637{
6638 spin_lock_irq(&ctx->completion_lock);
6639 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6640 spin_unlock_irq(&ctx->completion_lock);
6641}
6642
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006643static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6644 int sync, void *key)
6645{
6646 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6647 int ret;
6648
6649 ret = autoremove_wake_function(wqe, mode, sync, key);
6650 if (ret) {
6651 unsigned long flags;
6652
6653 spin_lock_irqsave(&ctx->completion_lock, flags);
6654 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6655 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6656 }
6657 return ret;
6658}
6659
Jens Axboec8d1ba52020-09-14 11:07:26 -06006660enum sq_ret {
6661 SQT_IDLE = 1,
6662 SQT_SPIN = 2,
6663 SQT_DID_WORK = 4,
6664};
6665
6666static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
6667 unsigned long start_jiffies)
6668{
6669 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006670 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006671 unsigned int to_submit;
6672 int ret = 0;
6673
6674again:
6675 if (!list_empty(&ctx->iopoll_list)) {
6676 unsigned nr_events = 0;
6677
6678 mutex_lock(&ctx->uring_lock);
6679 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6680 io_do_iopoll(ctx, &nr_events, 0);
6681 mutex_unlock(&ctx->uring_lock);
6682 }
6683
6684 to_submit = io_sqring_entries(ctx);
6685
6686 /*
6687 * If submit got -EBUSY, flag us as needing the application
6688 * to enter the kernel to reap and flush events.
6689 */
6690 if (!to_submit || ret == -EBUSY || need_resched()) {
6691 /*
6692 * Drop cur_mm before scheduling, we can't hold it for
6693 * long periods (or over schedule()). Do this before
6694 * adding ourselves to the waitqueue, as the unuse/drop
6695 * may sleep.
6696 */
6697 io_sq_thread_drop_mm();
6698
6699 /*
6700 * We're polling. If we're within the defined idle
6701 * period, then let us spin without work before going
6702 * to sleep. The exception is if we got EBUSY doing
6703 * more IO, we should wait for the application to
6704 * reap events and wake us up.
6705 */
6706 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6707 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6708 !percpu_ref_is_dying(&ctx->refs)))
6709 return SQT_SPIN;
6710
Jens Axboe534ca6d2020-09-02 13:52:19 -06006711 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006712 TASK_INTERRUPTIBLE);
6713
6714 /*
6715 * While doing polled IO, before going to sleep, we need
6716 * to check if there are new reqs added to iopoll_list,
6717 * it is because reqs may have been punted to io worker
6718 * and will be added to iopoll_list later, hence check
6719 * the iopoll_list again.
6720 */
6721 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6722 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006723 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006724 goto again;
6725 }
6726
Jens Axboec8d1ba52020-09-14 11:07:26 -06006727 to_submit = io_sqring_entries(ctx);
6728 if (!to_submit || ret == -EBUSY)
6729 return SQT_IDLE;
6730 }
6731
Jens Axboe534ca6d2020-09-02 13:52:19 -06006732 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006733 io_ring_clear_wakeup_flag(ctx);
6734
6735 mutex_lock(&ctx->uring_lock);
6736 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6737 ret = io_submit_sqes(ctx, to_submit);
6738 mutex_unlock(&ctx->uring_lock);
6739 return SQT_DID_WORK;
6740}
6741
Jens Axboe69fb2132020-09-14 11:16:23 -06006742static void io_sqd_init_new(struct io_sq_data *sqd)
6743{
6744 struct io_ring_ctx *ctx;
6745
6746 while (!list_empty(&sqd->ctx_new_list)) {
6747 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6748 init_wait(&ctx->sqo_wait_entry);
6749 ctx->sqo_wait_entry.func = io_sq_wake_function;
6750 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6751 complete(&ctx->sq_thread_comp);
6752 }
6753}
6754
Jens Axboe6c271ce2019-01-10 11:22:30 -07006755static int io_sq_thread(void *data)
6756{
Jens Axboe69fb2132020-09-14 11:16:23 -06006757 const struct cred *old_cred = NULL;
6758 struct io_sq_data *sqd = data;
6759 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006760 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006761
Jens Axboec8d1ba52020-09-14 11:07:26 -06006762 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006763 while (!kthread_should_stop()) {
6764 enum sq_ret ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006765
Jens Axboe69fb2132020-09-14 11:16:23 -06006766 /*
6767 * Any changes to the sqd lists are synchronized through the
6768 * kthread parking. This synchronizes the thread vs users,
6769 * the users are synchronized on the sqd->ctx_lock.
6770 */
6771 if (kthread_should_park())
6772 kthread_parkme();
6773
6774 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6775 io_sqd_init_new(sqd);
6776
6777 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6778 if (current->cred != ctx->creds) {
6779 if (old_cred)
6780 revert_creds(old_cred);
6781 old_cred = override_creds(ctx->creds);
6782 }
6783
6784 ret |= __io_sq_thread(ctx, start_jiffies);
6785
6786 io_sq_thread_drop_mm();
6787 }
6788
6789 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006790 io_run_task_work();
6791 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006792 } else if (ret == SQT_IDLE) {
6793 if (kthread_should_park())
6794 continue;
6795 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6796 io_ring_set_wakeup_flag(ctx);
6797 schedule();
6798 start_jiffies = jiffies;
6799 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6800 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006801 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006802 }
6803
Jens Axboe4c6e2772020-07-01 11:29:10 -06006804 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006805
Jens Axboe69fb2132020-09-14 11:16:23 -06006806 if (old_cred)
6807 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006808
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006809 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006810
Jens Axboe6c271ce2019-01-10 11:22:30 -07006811 return 0;
6812}
6813
Jens Axboebda52162019-09-24 13:47:15 -06006814struct io_wait_queue {
6815 struct wait_queue_entry wq;
6816 struct io_ring_ctx *ctx;
6817 unsigned to_wait;
6818 unsigned nr_timeouts;
6819};
6820
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006821static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006822{
6823 struct io_ring_ctx *ctx = iowq->ctx;
6824
6825 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006826 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006827 * started waiting. For timeouts, we always want to return to userspace,
6828 * regardless of event count.
6829 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006830 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006831 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6832}
6833
6834static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6835 int wake_flags, void *key)
6836{
6837 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6838 wq);
6839
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006840 /* use noflush == true, as we can't safely rely on locking context */
6841 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006842 return -1;
6843
6844 return autoremove_wake_function(curr, mode, wake_flags, key);
6845}
6846
Jens Axboe2b188cc2019-01-07 10:46:33 -07006847/*
6848 * Wait until events become available, if we don't already have some. The
6849 * application must reap them itself, as they reside on the shared cq ring.
6850 */
6851static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6852 const sigset_t __user *sig, size_t sigsz)
6853{
Jens Axboebda52162019-09-24 13:47:15 -06006854 struct io_wait_queue iowq = {
6855 .wq = {
6856 .private = current,
6857 .func = io_wake_function,
6858 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6859 },
6860 .ctx = ctx,
6861 .to_wait = min_events,
6862 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006863 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006864 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006865
Jens Axboeb41e9852020-02-17 09:52:41 -07006866 do {
6867 if (io_cqring_events(ctx, false) >= min_events)
6868 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006869 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006870 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006871 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006872
6873 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006874#ifdef CONFIG_COMPAT
6875 if (in_compat_syscall())
6876 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006877 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006878 else
6879#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006880 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006881
Jens Axboe2b188cc2019-01-07 10:46:33 -07006882 if (ret)
6883 return ret;
6884 }
6885
Jens Axboebda52162019-09-24 13:47:15 -06006886 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006887 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006888 do {
6889 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6890 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006891 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006892 if (io_run_task_work())
6893 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006894 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006895 if (current->jobctl & JOBCTL_TASK_WORK) {
6896 spin_lock_irq(&current->sighand->siglock);
6897 current->jobctl &= ~JOBCTL_TASK_WORK;
6898 recalc_sigpending();
6899 spin_unlock_irq(&current->sighand->siglock);
6900 continue;
6901 }
6902 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006903 break;
6904 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006905 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006906 break;
6907 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006908 } while (1);
6909 finish_wait(&ctx->wait, &iowq.wq);
6910
Jens Axboeb7db41c2020-07-04 08:55:50 -06006911 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006912
Hristo Venev75b28af2019-08-26 17:23:46 +00006913 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006914}
6915
Jens Axboe6b063142019-01-10 22:13:58 -07006916static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6917{
6918#if defined(CONFIG_UNIX)
6919 if (ctx->ring_sock) {
6920 struct sock *sock = ctx->ring_sock->sk;
6921 struct sk_buff *skb;
6922
6923 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6924 kfree_skb(skb);
6925 }
6926#else
6927 int i;
6928
Jens Axboe65e19f52019-10-26 07:20:21 -06006929 for (i = 0; i < ctx->nr_user_files; i++) {
6930 struct file *file;
6931
6932 file = io_file_from_index(ctx, i);
6933 if (file)
6934 fput(file);
6935 }
Jens Axboe6b063142019-01-10 22:13:58 -07006936#endif
6937}
6938
Jens Axboe05f3fb32019-12-09 11:22:50 -07006939static void io_file_ref_kill(struct percpu_ref *ref)
6940{
6941 struct fixed_file_data *data;
6942
6943 data = container_of(ref, struct fixed_file_data, refs);
6944 complete(&data->done);
6945}
6946
Jens Axboe6b063142019-01-10 22:13:58 -07006947static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6948{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006949 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006950 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006951 unsigned nr_tables, i;
6952
Jens Axboe05f3fb32019-12-09 11:22:50 -07006953 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006954 return -ENXIO;
6955
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006956 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006957 if (!list_empty(&data->ref_list))
6958 ref_node = list_first_entry(&data->ref_list,
6959 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006960 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006961 if (ref_node)
6962 percpu_ref_kill(&ref_node->refs);
6963
6964 percpu_ref_kill(&data->refs);
6965
6966 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006967 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006968 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006969
Jens Axboe6b063142019-01-10 22:13:58 -07006970 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006971 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6972 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006973 kfree(data->table[i].files);
6974 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006975 percpu_ref_exit(&data->refs);
6976 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006977 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006978 ctx->nr_user_files = 0;
6979 return 0;
6980}
6981
Jens Axboe534ca6d2020-09-02 13:52:19 -06006982static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006983{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006984 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006985 /*
6986 * The park is a bit of a work-around, without it we get
6987 * warning spews on shutdown with SQPOLL set and affinity
6988 * set to a single CPU.
6989 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06006990 if (sqd->thread) {
6991 kthread_park(sqd->thread);
6992 kthread_stop(sqd->thread);
6993 }
6994
6995 kfree(sqd);
6996 }
6997}
6998
Jens Axboeaa061652020-09-02 14:50:27 -06006999static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7000{
7001 struct io_ring_ctx *ctx_attach;
7002 struct io_sq_data *sqd;
7003 struct fd f;
7004
7005 f = fdget(p->wq_fd);
7006 if (!f.file)
7007 return ERR_PTR(-ENXIO);
7008 if (f.file->f_op != &io_uring_fops) {
7009 fdput(f);
7010 return ERR_PTR(-EINVAL);
7011 }
7012
7013 ctx_attach = f.file->private_data;
7014 sqd = ctx_attach->sq_data;
7015 if (!sqd) {
7016 fdput(f);
7017 return ERR_PTR(-EINVAL);
7018 }
7019
7020 refcount_inc(&sqd->refs);
7021 fdput(f);
7022 return sqd;
7023}
7024
Jens Axboe534ca6d2020-09-02 13:52:19 -06007025static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7026{
7027 struct io_sq_data *sqd;
7028
Jens Axboeaa061652020-09-02 14:50:27 -06007029 if (p->flags & IORING_SETUP_ATTACH_WQ)
7030 return io_attach_sq_data(p);
7031
Jens Axboe534ca6d2020-09-02 13:52:19 -06007032 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7033 if (!sqd)
7034 return ERR_PTR(-ENOMEM);
7035
7036 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007037 INIT_LIST_HEAD(&sqd->ctx_list);
7038 INIT_LIST_HEAD(&sqd->ctx_new_list);
7039 mutex_init(&sqd->ctx_lock);
7040 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007041 init_waitqueue_head(&sqd->wait);
7042 return sqd;
7043}
7044
Jens Axboe69fb2132020-09-14 11:16:23 -06007045static void io_sq_thread_unpark(struct io_sq_data *sqd)
7046 __releases(&sqd->lock)
7047{
7048 if (!sqd->thread)
7049 return;
7050 kthread_unpark(sqd->thread);
7051 mutex_unlock(&sqd->lock);
7052}
7053
7054static void io_sq_thread_park(struct io_sq_data *sqd)
7055 __acquires(&sqd->lock)
7056{
7057 if (!sqd->thread)
7058 return;
7059 mutex_lock(&sqd->lock);
7060 kthread_park(sqd->thread);
7061}
7062
Jens Axboe534ca6d2020-09-02 13:52:19 -06007063static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7064{
7065 struct io_sq_data *sqd = ctx->sq_data;
7066
7067 if (sqd) {
7068 if (sqd->thread) {
7069 /*
7070 * We may arrive here from the error branch in
7071 * io_sq_offload_create() where the kthread is created
7072 * without being waked up, thus wake it up now to make
7073 * sure the wait will complete.
7074 */
7075 wake_up_process(sqd->thread);
7076 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007077
7078 io_sq_thread_park(sqd);
7079 }
7080
7081 mutex_lock(&sqd->ctx_lock);
7082 list_del(&ctx->sqd_list);
7083 mutex_unlock(&sqd->ctx_lock);
7084
7085 if (sqd->thread) {
7086 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7087 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007088 }
7089
7090 io_put_sq_data(sqd);
7091 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007092 }
7093}
7094
Jens Axboe6b063142019-01-10 22:13:58 -07007095static void io_finish_async(struct io_ring_ctx *ctx)
7096{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007097 io_sq_thread_stop(ctx);
7098
Jens Axboe561fb042019-10-24 07:25:42 -06007099 if (ctx->io_wq) {
7100 io_wq_destroy(ctx->io_wq);
7101 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007102 }
7103}
7104
7105#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007106/*
7107 * Ensure the UNIX gc is aware of our file set, so we are certain that
7108 * the io_uring can be safely unregistered on process exit, even if we have
7109 * loops in the file referencing.
7110 */
7111static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7112{
7113 struct sock *sk = ctx->ring_sock->sk;
7114 struct scm_fp_list *fpl;
7115 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007116 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007117
Jens Axboe6b063142019-01-10 22:13:58 -07007118 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7119 if (!fpl)
7120 return -ENOMEM;
7121
7122 skb = alloc_skb(0, GFP_KERNEL);
7123 if (!skb) {
7124 kfree(fpl);
7125 return -ENOMEM;
7126 }
7127
7128 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007129
Jens Axboe08a45172019-10-03 08:11:03 -06007130 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007131 fpl->user = get_uid(ctx->user);
7132 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007133 struct file *file = io_file_from_index(ctx, i + offset);
7134
7135 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007136 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007137 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007138 unix_inflight(fpl->user, fpl->fp[nr_files]);
7139 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007140 }
7141
Jens Axboe08a45172019-10-03 08:11:03 -06007142 if (nr_files) {
7143 fpl->max = SCM_MAX_FD;
7144 fpl->count = nr_files;
7145 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007146 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007147 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7148 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007149
Jens Axboe08a45172019-10-03 08:11:03 -06007150 for (i = 0; i < nr_files; i++)
7151 fput(fpl->fp[i]);
7152 } else {
7153 kfree_skb(skb);
7154 kfree(fpl);
7155 }
Jens Axboe6b063142019-01-10 22:13:58 -07007156
7157 return 0;
7158}
7159
7160/*
7161 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7162 * causes regular reference counting to break down. We rely on the UNIX
7163 * garbage collection to take care of this problem for us.
7164 */
7165static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7166{
7167 unsigned left, total;
7168 int ret = 0;
7169
7170 total = 0;
7171 left = ctx->nr_user_files;
7172 while (left) {
7173 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007174
7175 ret = __io_sqe_files_scm(ctx, this_files, total);
7176 if (ret)
7177 break;
7178 left -= this_files;
7179 total += this_files;
7180 }
7181
7182 if (!ret)
7183 return 0;
7184
7185 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007186 struct file *file = io_file_from_index(ctx, total);
7187
7188 if (file)
7189 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007190 total++;
7191 }
7192
7193 return ret;
7194}
7195#else
7196static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7197{
7198 return 0;
7199}
7200#endif
7201
Jens Axboe65e19f52019-10-26 07:20:21 -06007202static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7203 unsigned nr_files)
7204{
7205 int i;
7206
7207 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007208 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007209 unsigned this_files;
7210
7211 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7212 table->files = kcalloc(this_files, sizeof(struct file *),
7213 GFP_KERNEL);
7214 if (!table->files)
7215 break;
7216 nr_files -= this_files;
7217 }
7218
7219 if (i == nr_tables)
7220 return 0;
7221
7222 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007223 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007224 kfree(table->files);
7225 }
7226 return 1;
7227}
7228
Jens Axboe05f3fb32019-12-09 11:22:50 -07007229static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007230{
7231#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007232 struct sock *sock = ctx->ring_sock->sk;
7233 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7234 struct sk_buff *skb;
7235 int i;
7236
7237 __skb_queue_head_init(&list);
7238
7239 /*
7240 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7241 * remove this entry and rearrange the file array.
7242 */
7243 skb = skb_dequeue(head);
7244 while (skb) {
7245 struct scm_fp_list *fp;
7246
7247 fp = UNIXCB(skb).fp;
7248 for (i = 0; i < fp->count; i++) {
7249 int left;
7250
7251 if (fp->fp[i] != file)
7252 continue;
7253
7254 unix_notinflight(fp->user, fp->fp[i]);
7255 left = fp->count - 1 - i;
7256 if (left) {
7257 memmove(&fp->fp[i], &fp->fp[i + 1],
7258 left * sizeof(struct file *));
7259 }
7260 fp->count--;
7261 if (!fp->count) {
7262 kfree_skb(skb);
7263 skb = NULL;
7264 } else {
7265 __skb_queue_tail(&list, skb);
7266 }
7267 fput(file);
7268 file = NULL;
7269 break;
7270 }
7271
7272 if (!file)
7273 break;
7274
7275 __skb_queue_tail(&list, skb);
7276
7277 skb = skb_dequeue(head);
7278 }
7279
7280 if (skb_peek(&list)) {
7281 spin_lock_irq(&head->lock);
7282 while ((skb = __skb_dequeue(&list)) != NULL)
7283 __skb_queue_tail(head, skb);
7284 spin_unlock_irq(&head->lock);
7285 }
7286#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007287 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007288#endif
7289}
7290
Jens Axboe05f3fb32019-12-09 11:22:50 -07007291struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007292 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007293 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007294};
7295
Jens Axboe4a38aed22020-05-14 17:21:15 -06007296static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007297{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007298 struct fixed_file_data *file_data = ref_node->file_data;
7299 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007300 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007301
7302 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007303 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007304 io_ring_file_put(ctx, pfile->file);
7305 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007306 }
7307
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007308 spin_lock(&file_data->lock);
7309 list_del(&ref_node->node);
7310 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007311
Xiaoguang Wang05589552020-03-31 14:05:18 +08007312 percpu_ref_exit(&ref_node->refs);
7313 kfree(ref_node);
7314 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007315}
7316
Jens Axboe4a38aed22020-05-14 17:21:15 -06007317static void io_file_put_work(struct work_struct *work)
7318{
7319 struct io_ring_ctx *ctx;
7320 struct llist_node *node;
7321
7322 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7323 node = llist_del_all(&ctx->file_put_llist);
7324
7325 while (node) {
7326 struct fixed_file_ref_node *ref_node;
7327 struct llist_node *next = node->next;
7328
7329 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7330 __io_file_put_work(ref_node);
7331 node = next;
7332 }
7333}
7334
Jens Axboe05f3fb32019-12-09 11:22:50 -07007335static void io_file_data_ref_zero(struct percpu_ref *ref)
7336{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007337 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007338 struct io_ring_ctx *ctx;
7339 bool first_add;
7340 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007341
Xiaoguang Wang05589552020-03-31 14:05:18 +08007342 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007343 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007344
Jens Axboe4a38aed22020-05-14 17:21:15 -06007345 if (percpu_ref_is_dying(&ctx->file_data->refs))
7346 delay = 0;
7347
7348 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7349 if (!delay)
7350 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7351 else if (first_add)
7352 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007353}
7354
7355static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7356 struct io_ring_ctx *ctx)
7357{
7358 struct fixed_file_ref_node *ref_node;
7359
7360 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7361 if (!ref_node)
7362 return ERR_PTR(-ENOMEM);
7363
7364 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7365 0, GFP_KERNEL)) {
7366 kfree(ref_node);
7367 return ERR_PTR(-ENOMEM);
7368 }
7369 INIT_LIST_HEAD(&ref_node->node);
7370 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007371 ref_node->file_data = ctx->file_data;
7372 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007373}
7374
7375static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7376{
7377 percpu_ref_exit(&ref_node->refs);
7378 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007379}
7380
7381static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7382 unsigned nr_args)
7383{
7384 __s32 __user *fds = (__s32 __user *) arg;
7385 unsigned nr_tables;
7386 struct file *file;
7387 int fd, ret = 0;
7388 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007389 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007390
7391 if (ctx->file_data)
7392 return -EBUSY;
7393 if (!nr_args)
7394 return -EINVAL;
7395 if (nr_args > IORING_MAX_FIXED_FILES)
7396 return -EMFILE;
7397
7398 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7399 if (!ctx->file_data)
7400 return -ENOMEM;
7401 ctx->file_data->ctx = ctx;
7402 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007403 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007404 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007405
7406 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7407 ctx->file_data->table = kcalloc(nr_tables,
7408 sizeof(struct fixed_file_table),
7409 GFP_KERNEL);
7410 if (!ctx->file_data->table) {
7411 kfree(ctx->file_data);
7412 ctx->file_data = NULL;
7413 return -ENOMEM;
7414 }
7415
Xiaoguang Wang05589552020-03-31 14:05:18 +08007416 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007417 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7418 kfree(ctx->file_data->table);
7419 kfree(ctx->file_data);
7420 ctx->file_data = NULL;
7421 return -ENOMEM;
7422 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007423
7424 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7425 percpu_ref_exit(&ctx->file_data->refs);
7426 kfree(ctx->file_data->table);
7427 kfree(ctx->file_data);
7428 ctx->file_data = NULL;
7429 return -ENOMEM;
7430 }
7431
7432 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7433 struct fixed_file_table *table;
7434 unsigned index;
7435
7436 ret = -EFAULT;
7437 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7438 break;
7439 /* allow sparse sets */
7440 if (fd == -1) {
7441 ret = 0;
7442 continue;
7443 }
7444
7445 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7446 index = i & IORING_FILE_TABLE_MASK;
7447 file = fget(fd);
7448
7449 ret = -EBADF;
7450 if (!file)
7451 break;
7452
7453 /*
7454 * Don't allow io_uring instances to be registered. If UNIX
7455 * isn't enabled, then this causes a reference cycle and this
7456 * instance can never get freed. If UNIX is enabled we'll
7457 * handle it just fine, but there's still no point in allowing
7458 * a ring fd as it doesn't support regular read/write anyway.
7459 */
7460 if (file->f_op == &io_uring_fops) {
7461 fput(file);
7462 break;
7463 }
7464 ret = 0;
7465 table->files[index] = file;
7466 }
7467
7468 if (ret) {
7469 for (i = 0; i < ctx->nr_user_files; i++) {
7470 file = io_file_from_index(ctx, i);
7471 if (file)
7472 fput(file);
7473 }
7474 for (i = 0; i < nr_tables; i++)
7475 kfree(ctx->file_data->table[i].files);
7476
Yang Yingliang667e57d2020-07-10 14:14:20 +00007477 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007478 kfree(ctx->file_data->table);
7479 kfree(ctx->file_data);
7480 ctx->file_data = NULL;
7481 ctx->nr_user_files = 0;
7482 return ret;
7483 }
7484
7485 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007486 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007487 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007488 return ret;
7489 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007490
Xiaoguang Wang05589552020-03-31 14:05:18 +08007491 ref_node = alloc_fixed_file_ref_node(ctx);
7492 if (IS_ERR(ref_node)) {
7493 io_sqe_files_unregister(ctx);
7494 return PTR_ERR(ref_node);
7495 }
7496
7497 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007498 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007499 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007500 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007501 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007502 return ret;
7503}
7504
Jens Axboec3a31e62019-10-03 13:59:56 -06007505static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7506 int index)
7507{
7508#if defined(CONFIG_UNIX)
7509 struct sock *sock = ctx->ring_sock->sk;
7510 struct sk_buff_head *head = &sock->sk_receive_queue;
7511 struct sk_buff *skb;
7512
7513 /*
7514 * See if we can merge this file into an existing skb SCM_RIGHTS
7515 * file set. If there's no room, fall back to allocating a new skb
7516 * and filling it in.
7517 */
7518 spin_lock_irq(&head->lock);
7519 skb = skb_peek(head);
7520 if (skb) {
7521 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7522
7523 if (fpl->count < SCM_MAX_FD) {
7524 __skb_unlink(skb, head);
7525 spin_unlock_irq(&head->lock);
7526 fpl->fp[fpl->count] = get_file(file);
7527 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7528 fpl->count++;
7529 spin_lock_irq(&head->lock);
7530 __skb_queue_head(head, skb);
7531 } else {
7532 skb = NULL;
7533 }
7534 }
7535 spin_unlock_irq(&head->lock);
7536
7537 if (skb) {
7538 fput(file);
7539 return 0;
7540 }
7541
7542 return __io_sqe_files_scm(ctx, 1, index);
7543#else
7544 return 0;
7545#endif
7546}
7547
Hillf Dantona5318d32020-03-23 17:47:15 +08007548static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007549 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007550{
Hillf Dantona5318d32020-03-23 17:47:15 +08007551 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007552 struct percpu_ref *refs = data->cur_refs;
7553 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007554
Jens Axboe05f3fb32019-12-09 11:22:50 -07007555 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007556 if (!pfile)
7557 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007558
Xiaoguang Wang05589552020-03-31 14:05:18 +08007559 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007560 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007561 list_add(&pfile->list, &ref_node->file_list);
7562
Hillf Dantona5318d32020-03-23 17:47:15 +08007563 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007564}
7565
7566static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7567 struct io_uring_files_update *up,
7568 unsigned nr_args)
7569{
7570 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007571 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007572 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007573 __s32 __user *fds;
7574 int fd, i, err;
7575 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007576 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007577
Jens Axboe05f3fb32019-12-09 11:22:50 -07007578 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007579 return -EOVERFLOW;
7580 if (done > ctx->nr_user_files)
7581 return -EINVAL;
7582
Xiaoguang Wang05589552020-03-31 14:05:18 +08007583 ref_node = alloc_fixed_file_ref_node(ctx);
7584 if (IS_ERR(ref_node))
7585 return PTR_ERR(ref_node);
7586
Jens Axboec3a31e62019-10-03 13:59:56 -06007587 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007588 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007589 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007590 struct fixed_file_table *table;
7591 unsigned index;
7592
Jens Axboec3a31e62019-10-03 13:59:56 -06007593 err = 0;
7594 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7595 err = -EFAULT;
7596 break;
7597 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007598 i = array_index_nospec(up->offset, ctx->nr_user_files);
7599 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007600 index = i & IORING_FILE_TABLE_MASK;
7601 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007602 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007603 err = io_queue_file_removal(data, file);
7604 if (err)
7605 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007606 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007607 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007608 }
7609 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007610 file = fget(fd);
7611 if (!file) {
7612 err = -EBADF;
7613 break;
7614 }
7615 /*
7616 * Don't allow io_uring instances to be registered. If
7617 * UNIX isn't enabled, then this causes a reference
7618 * cycle and this instance can never get freed. If UNIX
7619 * is enabled we'll handle it just fine, but there's
7620 * still no point in allowing a ring fd as it doesn't
7621 * support regular read/write anyway.
7622 */
7623 if (file->f_op == &io_uring_fops) {
7624 fput(file);
7625 err = -EBADF;
7626 break;
7627 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007628 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007629 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007630 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007631 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007632 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007633 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007634 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007635 }
7636 nr_args--;
7637 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007638 up->offset++;
7639 }
7640
Xiaoguang Wang05589552020-03-31 14:05:18 +08007641 if (needs_switch) {
7642 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007643 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007644 list_add(&ref_node->node, &data->ref_list);
7645 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007646 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007647 percpu_ref_get(&ctx->file_data->refs);
7648 } else
7649 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007650
7651 return done ? done : err;
7652}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007653
Jens Axboe05f3fb32019-12-09 11:22:50 -07007654static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7655 unsigned nr_args)
7656{
7657 struct io_uring_files_update up;
7658
7659 if (!ctx->file_data)
7660 return -ENXIO;
7661 if (!nr_args)
7662 return -EINVAL;
7663 if (copy_from_user(&up, arg, sizeof(up)))
7664 return -EFAULT;
7665 if (up.resv)
7666 return -EINVAL;
7667
7668 return __io_sqe_files_update(ctx, &up, nr_args);
7669}
Jens Axboec3a31e62019-10-03 13:59:56 -06007670
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007671static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007672{
7673 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7674
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007675 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007676 io_put_req(req);
7677}
7678
Pavel Begunkov24369c22020-01-28 03:15:48 +03007679static int io_init_wq_offload(struct io_ring_ctx *ctx,
7680 struct io_uring_params *p)
7681{
7682 struct io_wq_data data;
7683 struct fd f;
7684 struct io_ring_ctx *ctx_attach;
7685 unsigned int concurrency;
7686 int ret = 0;
7687
7688 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007689 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007690 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007691
7692 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7693 /* Do QD, or 4 * CPUS, whatever is smallest */
7694 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7695
7696 ctx->io_wq = io_wq_create(concurrency, &data);
7697 if (IS_ERR(ctx->io_wq)) {
7698 ret = PTR_ERR(ctx->io_wq);
7699 ctx->io_wq = NULL;
7700 }
7701 return ret;
7702 }
7703
7704 f = fdget(p->wq_fd);
7705 if (!f.file)
7706 return -EBADF;
7707
7708 if (f.file->f_op != &io_uring_fops) {
7709 ret = -EINVAL;
7710 goto out_fput;
7711 }
7712
7713 ctx_attach = f.file->private_data;
7714 /* @io_wq is protected by holding the fd */
7715 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7716 ret = -EINVAL;
7717 goto out_fput;
7718 }
7719
7720 ctx->io_wq = ctx_attach->io_wq;
7721out_fput:
7722 fdput(f);
7723 return ret;
7724}
7725
Jens Axboe0f212202020-09-13 13:09:39 -06007726static int io_uring_alloc_task_context(struct task_struct *task)
7727{
7728 struct io_uring_task *tctx;
7729
7730 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7731 if (unlikely(!tctx))
7732 return -ENOMEM;
7733
7734 xa_init(&tctx->xa);
7735 init_waitqueue_head(&tctx->wait);
7736 tctx->last = NULL;
7737 tctx->in_idle = 0;
7738 atomic_long_set(&tctx->req_issue, 0);
7739 atomic_long_set(&tctx->req_complete, 0);
7740 task->io_uring = tctx;
7741 return 0;
7742}
7743
7744void __io_uring_free(struct task_struct *tsk)
7745{
7746 struct io_uring_task *tctx = tsk->io_uring;
7747
7748 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7749 xa_destroy(&tctx->xa);
7750 kfree(tctx);
7751 tsk->io_uring = NULL;
7752}
7753
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007754static int io_sq_offload_create(struct io_ring_ctx *ctx,
7755 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007756{
7757 int ret;
7758
Jens Axboe6c271ce2019-01-10 11:22:30 -07007759 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007760 struct io_sq_data *sqd;
7761
Jens Axboe3ec482d2019-04-08 10:51:01 -06007762 ret = -EPERM;
7763 if (!capable(CAP_SYS_ADMIN))
7764 goto err;
7765
Jens Axboe534ca6d2020-09-02 13:52:19 -06007766 sqd = io_get_sq_data(p);
7767 if (IS_ERR(sqd)) {
7768 ret = PTR_ERR(sqd);
7769 goto err;
7770 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007771
Jens Axboe534ca6d2020-09-02 13:52:19 -06007772 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007773 io_sq_thread_park(sqd);
7774 mutex_lock(&sqd->ctx_lock);
7775 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7776 mutex_unlock(&sqd->ctx_lock);
7777 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007778
Jens Axboe917257d2019-04-13 09:28:55 -06007779 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7780 if (!ctx->sq_thread_idle)
7781 ctx->sq_thread_idle = HZ;
7782
Jens Axboeaa061652020-09-02 14:50:27 -06007783 if (sqd->thread)
7784 goto done;
7785
Jens Axboe6c271ce2019-01-10 11:22:30 -07007786 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007787 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007788
Jens Axboe917257d2019-04-13 09:28:55 -06007789 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007790 if (cpu >= nr_cpu_ids)
7791 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007792 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007793 goto err;
7794
Jens Axboe69fb2132020-09-14 11:16:23 -06007795 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007796 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007797 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007798 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007799 "io_uring-sq");
7800 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007801 if (IS_ERR(sqd->thread)) {
7802 ret = PTR_ERR(sqd->thread);
7803 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007804 goto err;
7805 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007806 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007807 if (ret)
7808 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007809 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7810 /* Can't have SQ_AFF without SQPOLL */
7811 ret = -EINVAL;
7812 goto err;
7813 }
7814
Jens Axboeaa061652020-09-02 14:50:27 -06007815done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007816 ret = io_init_wq_offload(ctx, p);
7817 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007818 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007819
7820 return 0;
7821err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007822 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007823 return ret;
7824}
7825
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007826static void io_sq_offload_start(struct io_ring_ctx *ctx)
7827{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007828 struct io_sq_data *sqd = ctx->sq_data;
7829
7830 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7831 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007832}
7833
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007834static inline void __io_unaccount_mem(struct user_struct *user,
7835 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007836{
7837 atomic_long_sub(nr_pages, &user->locked_vm);
7838}
7839
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007840static inline int __io_account_mem(struct user_struct *user,
7841 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007842{
7843 unsigned long page_limit, cur_pages, new_pages;
7844
7845 /* Don't allow more pages than we can safely lock */
7846 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7847
7848 do {
7849 cur_pages = atomic_long_read(&user->locked_vm);
7850 new_pages = cur_pages + nr_pages;
7851 if (new_pages > page_limit)
7852 return -ENOMEM;
7853 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7854 new_pages) != cur_pages);
7855
7856 return 0;
7857}
7858
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007859static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7860 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007861{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007862 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007863 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007864
Jens Axboe2aede0e2020-09-14 10:45:53 -06007865 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007866 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007867 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007868 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007869 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007870 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007871}
7872
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007873static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7874 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007875{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007876 int ret;
7877
7878 if (ctx->limit_mem) {
7879 ret = __io_account_mem(ctx->user, nr_pages);
7880 if (ret)
7881 return ret;
7882 }
7883
Jens Axboe2aede0e2020-09-14 10:45:53 -06007884 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007885 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007886 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007887 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007888 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007889 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007890
7891 return 0;
7892}
7893
Jens Axboe2b188cc2019-01-07 10:46:33 -07007894static void io_mem_free(void *ptr)
7895{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007896 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007897
Mark Rutland52e04ef2019-04-30 17:30:21 +01007898 if (!ptr)
7899 return;
7900
7901 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007902 if (put_page_testzero(page))
7903 free_compound_page(page);
7904}
7905
7906static void *io_mem_alloc(size_t size)
7907{
7908 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7909 __GFP_NORETRY;
7910
7911 return (void *) __get_free_pages(gfp_flags, get_order(size));
7912}
7913
Hristo Venev75b28af2019-08-26 17:23:46 +00007914static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7915 size_t *sq_offset)
7916{
7917 struct io_rings *rings;
7918 size_t off, sq_array_size;
7919
7920 off = struct_size(rings, cqes, cq_entries);
7921 if (off == SIZE_MAX)
7922 return SIZE_MAX;
7923
7924#ifdef CONFIG_SMP
7925 off = ALIGN(off, SMP_CACHE_BYTES);
7926 if (off == 0)
7927 return SIZE_MAX;
7928#endif
7929
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007930 if (sq_offset)
7931 *sq_offset = off;
7932
Hristo Venev75b28af2019-08-26 17:23:46 +00007933 sq_array_size = array_size(sizeof(u32), sq_entries);
7934 if (sq_array_size == SIZE_MAX)
7935 return SIZE_MAX;
7936
7937 if (check_add_overflow(off, sq_array_size, &off))
7938 return SIZE_MAX;
7939
Hristo Venev75b28af2019-08-26 17:23:46 +00007940 return off;
7941}
7942
Jens Axboe2b188cc2019-01-07 10:46:33 -07007943static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7944{
Hristo Venev75b28af2019-08-26 17:23:46 +00007945 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007946
Hristo Venev75b28af2019-08-26 17:23:46 +00007947 pages = (size_t)1 << get_order(
7948 rings_size(sq_entries, cq_entries, NULL));
7949 pages += (size_t)1 << get_order(
7950 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007951
Hristo Venev75b28af2019-08-26 17:23:46 +00007952 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007953}
7954
Jens Axboeedafcce2019-01-09 09:16:05 -07007955static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7956{
7957 int i, j;
7958
7959 if (!ctx->user_bufs)
7960 return -ENXIO;
7961
7962 for (i = 0; i < ctx->nr_user_bufs; i++) {
7963 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7964
7965 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007966 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007967
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007968 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007969 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007970 imu->nr_bvecs = 0;
7971 }
7972
7973 kfree(ctx->user_bufs);
7974 ctx->user_bufs = NULL;
7975 ctx->nr_user_bufs = 0;
7976 return 0;
7977}
7978
7979static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7980 void __user *arg, unsigned index)
7981{
7982 struct iovec __user *src;
7983
7984#ifdef CONFIG_COMPAT
7985 if (ctx->compat) {
7986 struct compat_iovec __user *ciovs;
7987 struct compat_iovec ciov;
7988
7989 ciovs = (struct compat_iovec __user *) arg;
7990 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7991 return -EFAULT;
7992
Jens Axboed55e5f52019-12-11 16:12:15 -07007993 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007994 dst->iov_len = ciov.iov_len;
7995 return 0;
7996 }
7997#endif
7998 src = (struct iovec __user *) arg;
7999 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8000 return -EFAULT;
8001 return 0;
8002}
8003
8004static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8005 unsigned nr_args)
8006{
8007 struct vm_area_struct **vmas = NULL;
8008 struct page **pages = NULL;
8009 int i, j, got_pages = 0;
8010 int ret = -EINVAL;
8011
8012 if (ctx->user_bufs)
8013 return -EBUSY;
8014 if (!nr_args || nr_args > UIO_MAXIOV)
8015 return -EINVAL;
8016
8017 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8018 GFP_KERNEL);
8019 if (!ctx->user_bufs)
8020 return -ENOMEM;
8021
8022 for (i = 0; i < nr_args; i++) {
8023 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8024 unsigned long off, start, end, ubuf;
8025 int pret, nr_pages;
8026 struct iovec iov;
8027 size_t size;
8028
8029 ret = io_copy_iov(ctx, &iov, arg, i);
8030 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008031 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008032
8033 /*
8034 * Don't impose further limits on the size and buffer
8035 * constraints here, we'll -EINVAL later when IO is
8036 * submitted if they are wrong.
8037 */
8038 ret = -EFAULT;
8039 if (!iov.iov_base || !iov.iov_len)
8040 goto err;
8041
8042 /* arbitrary limit, but we need something */
8043 if (iov.iov_len > SZ_1G)
8044 goto err;
8045
8046 ubuf = (unsigned long) iov.iov_base;
8047 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8048 start = ubuf >> PAGE_SHIFT;
8049 nr_pages = end - start;
8050
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008051 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008052 if (ret)
8053 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008054
8055 ret = 0;
8056 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008057 kvfree(vmas);
8058 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008059 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008060 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008061 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008062 sizeof(struct vm_area_struct *),
8063 GFP_KERNEL);
8064 if (!pages || !vmas) {
8065 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008066 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008067 goto err;
8068 }
8069 got_pages = nr_pages;
8070 }
8071
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008072 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008073 GFP_KERNEL);
8074 ret = -ENOMEM;
8075 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008076 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008077 goto err;
8078 }
8079
8080 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008081 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008082 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008083 FOLL_WRITE | FOLL_LONGTERM,
8084 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008085 if (pret == nr_pages) {
8086 /* don't support file backed memory */
8087 for (j = 0; j < nr_pages; j++) {
8088 struct vm_area_struct *vma = vmas[j];
8089
8090 if (vma->vm_file &&
8091 !is_file_hugepages(vma->vm_file)) {
8092 ret = -EOPNOTSUPP;
8093 break;
8094 }
8095 }
8096 } else {
8097 ret = pret < 0 ? pret : -EFAULT;
8098 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008099 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008100 if (ret) {
8101 /*
8102 * if we did partial map, or found file backed vmas,
8103 * release any pages we did get
8104 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008105 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008106 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008107 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008108 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008109 goto err;
8110 }
8111
8112 off = ubuf & ~PAGE_MASK;
8113 size = iov.iov_len;
8114 for (j = 0; j < nr_pages; j++) {
8115 size_t vec_len;
8116
8117 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8118 imu->bvec[j].bv_page = pages[j];
8119 imu->bvec[j].bv_len = vec_len;
8120 imu->bvec[j].bv_offset = off;
8121 off = 0;
8122 size -= vec_len;
8123 }
8124 /* store original address for later verification */
8125 imu->ubuf = ubuf;
8126 imu->len = iov.iov_len;
8127 imu->nr_bvecs = nr_pages;
8128
8129 ctx->nr_user_bufs++;
8130 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008131 kvfree(pages);
8132 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008133 return 0;
8134err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008135 kvfree(pages);
8136 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008137 io_sqe_buffer_unregister(ctx);
8138 return ret;
8139}
8140
Jens Axboe9b402842019-04-11 11:45:41 -06008141static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8142{
8143 __s32 __user *fds = arg;
8144 int fd;
8145
8146 if (ctx->cq_ev_fd)
8147 return -EBUSY;
8148
8149 if (copy_from_user(&fd, fds, sizeof(*fds)))
8150 return -EFAULT;
8151
8152 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8153 if (IS_ERR(ctx->cq_ev_fd)) {
8154 int ret = PTR_ERR(ctx->cq_ev_fd);
8155 ctx->cq_ev_fd = NULL;
8156 return ret;
8157 }
8158
8159 return 0;
8160}
8161
8162static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8163{
8164 if (ctx->cq_ev_fd) {
8165 eventfd_ctx_put(ctx->cq_ev_fd);
8166 ctx->cq_ev_fd = NULL;
8167 return 0;
8168 }
8169
8170 return -ENXIO;
8171}
8172
Jens Axboe5a2e7452020-02-23 16:23:11 -07008173static int __io_destroy_buffers(int id, void *p, void *data)
8174{
8175 struct io_ring_ctx *ctx = data;
8176 struct io_buffer *buf = p;
8177
Jens Axboe067524e2020-03-02 16:32:28 -07008178 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008179 return 0;
8180}
8181
8182static void io_destroy_buffers(struct io_ring_ctx *ctx)
8183{
8184 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8185 idr_destroy(&ctx->io_buffer_idr);
8186}
8187
Jens Axboe2b188cc2019-01-07 10:46:33 -07008188static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8189{
Jens Axboe6b063142019-01-10 22:13:58 -07008190 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008191 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008192
8193 if (ctx->sqo_task) {
8194 put_task_struct(ctx->sqo_task);
8195 ctx->sqo_task = NULL;
8196 mmdrop(ctx->mm_account);
8197 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008198 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008199
Jens Axboe6b063142019-01-10 22:13:58 -07008200 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008201 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008202 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008203 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008204
Jens Axboe2b188cc2019-01-07 10:46:33 -07008205#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008206 if (ctx->ring_sock) {
8207 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008208 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008209 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008210#endif
8211
Hristo Venev75b28af2019-08-26 17:23:46 +00008212 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008213 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008214
8215 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008216 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008217 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008218 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008219 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008220 kfree(ctx);
8221}
8222
8223static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8224{
8225 struct io_ring_ctx *ctx = file->private_data;
8226 __poll_t mask = 0;
8227
8228 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008229 /*
8230 * synchronizes with barrier from wq_has_sleeper call in
8231 * io_commit_cqring
8232 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008233 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00008234 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
8235 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008236 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008237 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008238 mask |= EPOLLIN | EPOLLRDNORM;
8239
8240 return mask;
8241}
8242
8243static int io_uring_fasync(int fd, struct file *file, int on)
8244{
8245 struct io_ring_ctx *ctx = file->private_data;
8246
8247 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8248}
8249
Jens Axboe071698e2020-01-28 10:04:42 -07008250static int io_remove_personalities(int id, void *p, void *data)
8251{
8252 struct io_ring_ctx *ctx = data;
8253 const struct cred *cred;
8254
8255 cred = idr_remove(&ctx->personality_idr, id);
8256 if (cred)
8257 put_cred(cred);
8258 return 0;
8259}
8260
Jens Axboe85faa7b2020-04-09 18:14:00 -06008261static void io_ring_exit_work(struct work_struct *work)
8262{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008263 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8264 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008265
Jens Axboe56952e92020-06-17 15:00:04 -06008266 /*
8267 * If we're doing polled IO and end up having requests being
8268 * submitted async (out-of-line), then completions can come in while
8269 * we're waiting for refs to drop. We need to reap these manually,
8270 * as nobody else will be looking for them.
8271 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008272 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008273 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008274 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008275 io_iopoll_try_reap_events(ctx);
8276 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008277 io_ring_ctx_free(ctx);
8278}
8279
Jens Axboe2b188cc2019-01-07 10:46:33 -07008280static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8281{
8282 mutex_lock(&ctx->uring_lock);
8283 percpu_ref_kill(&ctx->refs);
8284 mutex_unlock(&ctx->uring_lock);
8285
Jens Axboef3606e32020-09-22 08:18:24 -06008286 io_kill_timeouts(ctx, NULL);
8287 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008288
8289 if (ctx->io_wq)
8290 io_wq_cancel_all(ctx->io_wq);
8291
Jens Axboe15dff282019-11-13 09:09:23 -07008292 /* if we failed setting up the ctx, we might not have any rings */
8293 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008294 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008295 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008296 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008297
8298 /*
8299 * Do this upfront, so we won't have a grace period where the ring
8300 * is closed but resources aren't reaped yet. This can cause
8301 * spurious failure in setting up a new ring.
8302 */
Jens Axboe760618f2020-07-24 12:53:31 -06008303 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8304 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008305
Jens Axboe85faa7b2020-04-09 18:14:00 -06008306 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008307 /*
8308 * Use system_unbound_wq to avoid spawning tons of event kworkers
8309 * if we're exiting a ton of rings at the same time. It just adds
8310 * noise and overhead, there's no discernable change in runtime
8311 * over using system_wq.
8312 */
8313 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008314}
8315
8316static int io_uring_release(struct inode *inode, struct file *file)
8317{
8318 struct io_ring_ctx *ctx = file->private_data;
8319
8320 file->private_data = NULL;
8321 io_ring_ctx_wait_and_kill(ctx);
8322 return 0;
8323}
8324
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008325static bool io_wq_files_match(struct io_wq_work *work, void *data)
8326{
8327 struct files_struct *files = data;
8328
Jens Axboe0f212202020-09-13 13:09:39 -06008329 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008330}
8331
Jens Axboef254ac02020-08-12 17:33:30 -06008332/*
8333 * Returns true if 'preq' is the link parent of 'req'
8334 */
8335static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8336{
8337 struct io_kiocb *link;
8338
8339 if (!(preq->flags & REQ_F_LINK_HEAD))
8340 return false;
8341
8342 list_for_each_entry(link, &preq->link_list, link_list) {
8343 if (link == req)
8344 return true;
8345 }
8346
8347 return false;
8348}
8349
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008350static bool io_match_link_files(struct io_kiocb *req,
8351 struct files_struct *files)
8352{
8353 struct io_kiocb *link;
8354
8355 if (io_match_files(req, files))
8356 return true;
8357 if (req->flags & REQ_F_LINK_HEAD) {
8358 list_for_each_entry(link, &req->link_list, link_list) {
8359 if (io_match_files(link, files))
8360 return true;
8361 }
8362 }
8363 return false;
8364}
8365
Jens Axboef254ac02020-08-12 17:33:30 -06008366/*
8367 * We're looking to cancel 'req' because it's holding on to our files, but
8368 * 'req' could be a link to another request. See if it is, and cancel that
8369 * parent request if so.
8370 */
8371static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8372{
8373 struct hlist_node *tmp;
8374 struct io_kiocb *preq;
8375 bool found = false;
8376 int i;
8377
8378 spin_lock_irq(&ctx->completion_lock);
8379 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8380 struct hlist_head *list;
8381
8382 list = &ctx->cancel_hash[i];
8383 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8384 found = io_match_link(preq, req);
8385 if (found) {
8386 io_poll_remove_one(preq);
8387 break;
8388 }
8389 }
8390 }
8391 spin_unlock_irq(&ctx->completion_lock);
8392 return found;
8393}
8394
8395static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8396 struct io_kiocb *req)
8397{
8398 struct io_kiocb *preq;
8399 bool found = false;
8400
8401 spin_lock_irq(&ctx->completion_lock);
8402 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8403 found = io_match_link(preq, req);
8404 if (found) {
8405 __io_timeout_cancel(preq);
8406 break;
8407 }
8408 }
8409 spin_unlock_irq(&ctx->completion_lock);
8410 return found;
8411}
8412
Jens Axboeb711d4e2020-08-16 08:23:05 -07008413static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8414{
8415 return io_match_link(container_of(work, struct io_kiocb, work), data);
8416}
8417
8418static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8419{
8420 enum io_wq_cancel cret;
8421
8422 /* cancel this particular work, if it's running */
8423 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8424 if (cret != IO_WQ_CANCEL_NOTFOUND)
8425 return;
8426
8427 /* find links that hold this pending, cancel those */
8428 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8429 if (cret != IO_WQ_CANCEL_NOTFOUND)
8430 return;
8431
8432 /* if we have a poll link holding this pending, cancel that */
8433 if (io_poll_remove_link(ctx, req))
8434 return;
8435
8436 /* final option, timeout link is holding this req pending */
8437 io_timeout_remove_link(ctx, req);
8438}
8439
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008440static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8441 struct files_struct *files)
8442{
8443 struct io_defer_entry *de = NULL;
8444 LIST_HEAD(list);
8445
8446 spin_lock_irq(&ctx->completion_lock);
8447 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008448 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008449 list_cut_position(&list, &ctx->defer_list, &de->list);
8450 break;
8451 }
8452 }
8453 spin_unlock_irq(&ctx->completion_lock);
8454
8455 while (!list_empty(&list)) {
8456 de = list_first_entry(&list, struct io_defer_entry, list);
8457 list_del_init(&de->list);
8458 req_set_fail_links(de->req);
8459 io_put_req(de->req);
8460 io_req_complete(de->req, -ECANCELED);
8461 kfree(de);
8462 }
8463}
8464
Jens Axboe76e1b642020-09-26 15:05:03 -06008465/*
8466 * Returns true if we found and killed one or more files pinning requests
8467 */
8468static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008469 struct files_struct *files)
8470{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008471 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008472 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008473
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008474 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008475 /* cancel all at once, should be faster than doing it one by one*/
8476 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8477
Jens Axboefcb323c2019-10-24 12:39:47 -06008478 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008479 struct io_kiocb *cancel_req = NULL, *req;
8480 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008481
8482 spin_lock_irq(&ctx->inflight_lock);
8483 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008484 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008485 continue;
8486 /* req is being completed, ignore */
8487 if (!refcount_inc_not_zero(&req->refs))
8488 continue;
8489 cancel_req = req;
8490 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008491 }
Jens Axboe768134d2019-11-10 20:30:53 -07008492 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008493 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008494 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008495 spin_unlock_irq(&ctx->inflight_lock);
8496
Jens Axboe768134d2019-11-10 20:30:53 -07008497 /* We need to keep going until we don't find a matching req */
8498 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008499 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008500 /* cancel this request, or head link requests */
8501 io_attempt_cancel(ctx, cancel_req);
8502 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008503 /* cancellations _may_ trigger task work */
8504 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008505 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008506 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008507 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008508
8509 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008510}
8511
Pavel Begunkov801dd572020-06-15 10:33:14 +03008512static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008513{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008514 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8515 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008516
Jens Axboef3606e32020-09-22 08:18:24 -06008517 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008518}
8519
Jens Axboe0f212202020-09-13 13:09:39 -06008520static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8521 struct task_struct *task,
8522 struct files_struct *files)
8523{
8524 bool ret;
8525
8526 ret = io_uring_cancel_files(ctx, files);
8527 if (!files) {
8528 enum io_wq_cancel cret;
8529
8530 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8531 if (cret != IO_WQ_CANCEL_NOTFOUND)
8532 ret = true;
8533
8534 /* SQPOLL thread does its own polling */
8535 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8536 while (!list_empty_careful(&ctx->iopoll_list)) {
8537 io_iopoll_try_reap_events(ctx);
8538 ret = true;
8539 }
8540 }
8541
8542 ret |= io_poll_remove_all(ctx, task);
8543 ret |= io_kill_timeouts(ctx, task);
8544 }
8545
8546 return ret;
8547}
8548
8549/*
8550 * We need to iteratively cancel requests, in case a request has dependent
8551 * hard links. These persist even for failure of cancelations, hence keep
8552 * looping until none are found.
8553 */
8554static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8555 struct files_struct *files)
8556{
8557 struct task_struct *task = current;
8558
Jens Axboe534ca6d2020-09-02 13:52:19 -06008559 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8560 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008561
8562 io_cqring_overflow_flush(ctx, true, task, files);
8563
8564 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8565 io_run_task_work();
8566 cond_resched();
8567 }
8568}
8569
8570/*
8571 * Note that this task has used io_uring. We use it for cancelation purposes.
8572 */
8573static int io_uring_add_task_file(struct file *file)
8574{
8575 if (unlikely(!current->io_uring)) {
8576 int ret;
8577
8578 ret = io_uring_alloc_task_context(current);
8579 if (unlikely(ret))
8580 return ret;
8581 }
8582 if (current->io_uring->last != file) {
8583 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8584 void *old;
8585
8586 rcu_read_lock();
8587 old = xas_load(&xas);
8588 if (old != file) {
8589 get_file(file);
8590 xas_lock(&xas);
8591 xas_store(&xas, file);
8592 xas_unlock(&xas);
8593 }
8594 rcu_read_unlock();
8595 current->io_uring->last = file;
8596 }
8597
8598 return 0;
8599}
8600
8601/*
8602 * Remove this io_uring_file -> task mapping.
8603 */
8604static void io_uring_del_task_file(struct file *file)
8605{
8606 struct io_uring_task *tctx = current->io_uring;
8607 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8608
8609 if (tctx->last == file)
8610 tctx->last = NULL;
8611
8612 xas_lock(&xas);
8613 file = xas_store(&xas, NULL);
8614 xas_unlock(&xas);
8615
8616 if (file)
8617 fput(file);
8618}
8619
8620static void __io_uring_attempt_task_drop(struct file *file)
8621{
8622 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8623 struct file *old;
8624
8625 rcu_read_lock();
8626 old = xas_load(&xas);
8627 rcu_read_unlock();
8628
8629 if (old == file)
8630 io_uring_del_task_file(file);
8631}
8632
8633/*
8634 * Drop task note for this file if we're the only ones that hold it after
8635 * pending fput()
8636 */
8637static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8638{
8639 if (!current->io_uring)
8640 return;
8641 /*
8642 * fput() is pending, will be 2 if the only other ref is our potential
8643 * task file note. If the task is exiting, drop regardless of count.
8644 */
8645 if (!exiting && atomic_long_read(&file->f_count) != 2)
8646 return;
8647
8648 __io_uring_attempt_task_drop(file);
8649}
8650
8651void __io_uring_files_cancel(struct files_struct *files)
8652{
8653 struct io_uring_task *tctx = current->io_uring;
8654 XA_STATE(xas, &tctx->xa, 0);
8655
8656 /* make sure overflow events are dropped */
8657 tctx->in_idle = true;
8658
8659 do {
8660 struct io_ring_ctx *ctx;
8661 struct file *file;
8662
8663 xas_lock(&xas);
8664 file = xas_next_entry(&xas, ULONG_MAX);
8665 xas_unlock(&xas);
8666
8667 if (!file)
8668 break;
8669
8670 ctx = file->private_data;
8671
8672 io_uring_cancel_task_requests(ctx, files);
8673 if (files)
8674 io_uring_del_task_file(file);
8675 } while (1);
8676}
8677
8678static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8679{
8680 return atomic_long_read(&tctx->req_issue) ==
8681 atomic_long_read(&tctx->req_complete);
8682}
8683
8684/*
8685 * Find any io_uring fd that this task has registered or done IO on, and cancel
8686 * requests.
8687 */
8688void __io_uring_task_cancel(void)
8689{
8690 struct io_uring_task *tctx = current->io_uring;
8691 DEFINE_WAIT(wait);
8692 long completions;
8693
8694 /* make sure overflow events are dropped */
8695 tctx->in_idle = true;
8696
8697 while (!io_uring_task_idle(tctx)) {
8698 /* read completions before cancelations */
8699 completions = atomic_long_read(&tctx->req_complete);
8700 __io_uring_files_cancel(NULL);
8701
8702 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8703
8704 /*
8705 * If we've seen completions, retry. This avoids a race where
8706 * a completion comes in before we did prepare_to_wait().
8707 */
8708 if (completions != atomic_long_read(&tctx->req_complete))
8709 continue;
8710 if (io_uring_task_idle(tctx))
8711 break;
8712 schedule();
8713 }
8714
8715 finish_wait(&tctx->wait, &wait);
8716 tctx->in_idle = false;
8717}
8718
Jens Axboefcb323c2019-10-24 12:39:47 -06008719static int io_uring_flush(struct file *file, void *data)
8720{
8721 struct io_ring_ctx *ctx = file->private_data;
8722
Jens Axboe6ab23142020-02-08 20:23:59 -07008723 /*
8724 * If the task is going away, cancel work it may have pending
8725 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008726 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008727 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008728
Jens Axboe0f212202020-09-13 13:09:39 -06008729 io_uring_cancel_task_requests(ctx, data);
8730 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008731 return 0;
8732}
8733
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008734static void *io_uring_validate_mmap_request(struct file *file,
8735 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008736{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008737 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008738 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008739 struct page *page;
8740 void *ptr;
8741
8742 switch (offset) {
8743 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008744 case IORING_OFF_CQ_RING:
8745 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008746 break;
8747 case IORING_OFF_SQES:
8748 ptr = ctx->sq_sqes;
8749 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008750 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008751 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008752 }
8753
8754 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008755 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008756 return ERR_PTR(-EINVAL);
8757
8758 return ptr;
8759}
8760
8761#ifdef CONFIG_MMU
8762
8763static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8764{
8765 size_t sz = vma->vm_end - vma->vm_start;
8766 unsigned long pfn;
8767 void *ptr;
8768
8769 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8770 if (IS_ERR(ptr))
8771 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008772
8773 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8774 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8775}
8776
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008777#else /* !CONFIG_MMU */
8778
8779static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8780{
8781 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8782}
8783
8784static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8785{
8786 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8787}
8788
8789static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8790 unsigned long addr, unsigned long len,
8791 unsigned long pgoff, unsigned long flags)
8792{
8793 void *ptr;
8794
8795 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8796 if (IS_ERR(ptr))
8797 return PTR_ERR(ptr);
8798
8799 return (unsigned long) ptr;
8800}
8801
8802#endif /* !CONFIG_MMU */
8803
Jens Axboe2b188cc2019-01-07 10:46:33 -07008804SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8805 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8806 size_t, sigsz)
8807{
8808 struct io_ring_ctx *ctx;
8809 long ret = -EBADF;
8810 int submitted = 0;
8811 struct fd f;
8812
Jens Axboe4c6e2772020-07-01 11:29:10 -06008813 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008814
Jens Axboe6c271ce2019-01-10 11:22:30 -07008815 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008816 return -EINVAL;
8817
8818 f = fdget(fd);
8819 if (!f.file)
8820 return -EBADF;
8821
8822 ret = -EOPNOTSUPP;
8823 if (f.file->f_op != &io_uring_fops)
8824 goto out_fput;
8825
8826 ret = -ENXIO;
8827 ctx = f.file->private_data;
8828 if (!percpu_ref_tryget(&ctx->refs))
8829 goto out_fput;
8830
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008831 ret = -EBADFD;
8832 if (ctx->flags & IORING_SETUP_R_DISABLED)
8833 goto out;
8834
Jens Axboe6c271ce2019-01-10 11:22:30 -07008835 /*
8836 * For SQ polling, the thread will do all submissions and completions.
8837 * Just return the requested submit count, and wake the thread if
8838 * we were asked to.
8839 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008840 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008841 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008842 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008843 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008844 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008845 wake_up(&ctx->sq_data->wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008846 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008847 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008848 ret = io_uring_add_task_file(f.file);
8849 if (unlikely(ret))
8850 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008851 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008852 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008853 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008854
8855 if (submitted != to_submit)
8856 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008857 }
8858 if (flags & IORING_ENTER_GETEVENTS) {
8859 min_complete = min(min_complete, ctx->cq_entries);
8860
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008861 /*
8862 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8863 * space applications don't need to do io completion events
8864 * polling again, they can rely on io_sq_thread to do polling
8865 * work, which can reduce cpu usage and uring_lock contention.
8866 */
8867 if (ctx->flags & IORING_SETUP_IOPOLL &&
8868 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008869 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008870 } else {
8871 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8872 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008873 }
8874
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008875out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008876 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008877out_fput:
8878 fdput(f);
8879 return submitted ? submitted : ret;
8880}
8881
Tobias Klauserbebdb652020-02-26 18:38:32 +01008882#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008883static int io_uring_show_cred(int id, void *p, void *data)
8884{
8885 const struct cred *cred = p;
8886 struct seq_file *m = data;
8887 struct user_namespace *uns = seq_user_ns(m);
8888 struct group_info *gi;
8889 kernel_cap_t cap;
8890 unsigned __capi;
8891 int g;
8892
8893 seq_printf(m, "%5d\n", id);
8894 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8895 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8896 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8897 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8898 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8899 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8900 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8901 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8902 seq_puts(m, "\n\tGroups:\t");
8903 gi = cred->group_info;
8904 for (g = 0; g < gi->ngroups; g++) {
8905 seq_put_decimal_ull(m, g ? " " : "",
8906 from_kgid_munged(uns, gi->gid[g]));
8907 }
8908 seq_puts(m, "\n\tCapEff:\t");
8909 cap = cred->cap_effective;
8910 CAP_FOR_EACH_U32(__capi)
8911 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8912 seq_putc(m, '\n');
8913 return 0;
8914}
8915
8916static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8917{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008918 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008919 int i;
8920
Jens Axboefad8e0d2020-09-28 08:57:48 -06008921 /*
8922 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8923 * since fdinfo case grabs it in the opposite direction of normal use
8924 * cases. If we fail to get the lock, we just don't iterate any
8925 * structures that could be going away outside the io_uring mutex.
8926 */
8927 has_lock = mutex_trylock(&ctx->uring_lock);
8928
Jens Axboe87ce9552020-01-30 08:25:34 -07008929 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008930 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008931 struct fixed_file_table *table;
8932 struct file *f;
8933
8934 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8935 f = table->files[i & IORING_FILE_TABLE_MASK];
8936 if (f)
8937 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8938 else
8939 seq_printf(m, "%5u: <none>\n", i);
8940 }
8941 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008942 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008943 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8944
8945 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8946 (unsigned int) buf->len);
8947 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008948 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008949 seq_printf(m, "Personalities:\n");
8950 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8951 }
Jens Axboed7718a92020-02-14 22:23:12 -07008952 seq_printf(m, "PollList:\n");
8953 spin_lock_irq(&ctx->completion_lock);
8954 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8955 struct hlist_head *list = &ctx->cancel_hash[i];
8956 struct io_kiocb *req;
8957
8958 hlist_for_each_entry(req, list, hash_node)
8959 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8960 req->task->task_works != NULL);
8961 }
8962 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008963 if (has_lock)
8964 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008965}
8966
8967static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8968{
8969 struct io_ring_ctx *ctx = f->private_data;
8970
8971 if (percpu_ref_tryget(&ctx->refs)) {
8972 __io_uring_show_fdinfo(ctx, m);
8973 percpu_ref_put(&ctx->refs);
8974 }
8975}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008976#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008977
Jens Axboe2b188cc2019-01-07 10:46:33 -07008978static const struct file_operations io_uring_fops = {
8979 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008980 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008981 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008982#ifndef CONFIG_MMU
8983 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8984 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8985#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008986 .poll = io_uring_poll,
8987 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008988#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008989 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008990#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008991};
8992
8993static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8994 struct io_uring_params *p)
8995{
Hristo Venev75b28af2019-08-26 17:23:46 +00008996 struct io_rings *rings;
8997 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008998
Jens Axboebd740482020-08-05 12:58:23 -06008999 /* make sure these are sane, as we already accounted them */
9000 ctx->sq_entries = p->sq_entries;
9001 ctx->cq_entries = p->cq_entries;
9002
Hristo Venev75b28af2019-08-26 17:23:46 +00009003 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9004 if (size == SIZE_MAX)
9005 return -EOVERFLOW;
9006
9007 rings = io_mem_alloc(size);
9008 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009009 return -ENOMEM;
9010
Hristo Venev75b28af2019-08-26 17:23:46 +00009011 ctx->rings = rings;
9012 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9013 rings->sq_ring_mask = p->sq_entries - 1;
9014 rings->cq_ring_mask = p->cq_entries - 1;
9015 rings->sq_ring_entries = p->sq_entries;
9016 rings->cq_ring_entries = p->cq_entries;
9017 ctx->sq_mask = rings->sq_ring_mask;
9018 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009019
9020 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009021 if (size == SIZE_MAX) {
9022 io_mem_free(ctx->rings);
9023 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009024 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009025 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009026
9027 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009028 if (!ctx->sq_sqes) {
9029 io_mem_free(ctx->rings);
9030 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009031 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009032 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009033
Jens Axboe2b188cc2019-01-07 10:46:33 -07009034 return 0;
9035}
9036
9037/*
9038 * Allocate an anonymous fd, this is what constitutes the application
9039 * visible backing of an io_uring instance. The application mmaps this
9040 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9041 * we have to tie this fd to a socket for file garbage collection purposes.
9042 */
9043static int io_uring_get_fd(struct io_ring_ctx *ctx)
9044{
9045 struct file *file;
9046 int ret;
9047
9048#if defined(CONFIG_UNIX)
9049 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9050 &ctx->ring_sock);
9051 if (ret)
9052 return ret;
9053#endif
9054
9055 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9056 if (ret < 0)
9057 goto err;
9058
9059 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9060 O_RDWR | O_CLOEXEC);
9061 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009062err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009063 put_unused_fd(ret);
9064 ret = PTR_ERR(file);
9065 goto err;
9066 }
9067
9068#if defined(CONFIG_UNIX)
9069 ctx->ring_sock->file = file;
9070#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009071 if (unlikely(io_uring_add_task_file(file))) {
9072 file = ERR_PTR(-ENOMEM);
9073 goto err_fd;
9074 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009075 fd_install(ret, file);
9076 return ret;
9077err:
9078#if defined(CONFIG_UNIX)
9079 sock_release(ctx->ring_sock);
9080 ctx->ring_sock = NULL;
9081#endif
9082 return ret;
9083}
9084
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009085static int io_uring_create(unsigned entries, struct io_uring_params *p,
9086 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009087{
9088 struct user_struct *user = NULL;
9089 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009090 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009091 int ret;
9092
Jens Axboe8110c1a2019-12-28 15:39:54 -07009093 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009094 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009095 if (entries > IORING_MAX_ENTRIES) {
9096 if (!(p->flags & IORING_SETUP_CLAMP))
9097 return -EINVAL;
9098 entries = IORING_MAX_ENTRIES;
9099 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009100
9101 /*
9102 * Use twice as many entries for the CQ ring. It's possible for the
9103 * application to drive a higher depth than the size of the SQ ring,
9104 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009105 * some flexibility in overcommitting a bit. If the application has
9106 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9107 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009108 */
9109 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009110 if (p->flags & IORING_SETUP_CQSIZE) {
9111 /*
9112 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9113 * to a power-of-two, if it isn't already. We do NOT impose
9114 * any cq vs sq ring sizing.
9115 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009116 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009117 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009118 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9119 if (!(p->flags & IORING_SETUP_CLAMP))
9120 return -EINVAL;
9121 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9122 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009123 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9124 } else {
9125 p->cq_entries = 2 * p->sq_entries;
9126 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009127
9128 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009129 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009130
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009131 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009132 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009133 ring_pages(p->sq_entries, p->cq_entries));
9134 if (ret) {
9135 free_uid(user);
9136 return ret;
9137 }
9138 }
9139
9140 ctx = io_ring_ctx_alloc(p);
9141 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009142 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009143 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009144 p->cq_entries));
9145 free_uid(user);
9146 return -ENOMEM;
9147 }
9148 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009149 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009150 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009151
Jens Axboe2aede0e2020-09-14 10:45:53 -06009152 ctx->sqo_task = get_task_struct(current);
9153
9154 /*
9155 * This is just grabbed for accounting purposes. When a process exits,
9156 * the mm is exited and dropped before the files, hence we need to hang
9157 * on to this mm purely for the purposes of being able to unaccount
9158 * memory (locked/pinned vm). It's not used for anything else.
9159 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009160 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009161 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009162
Jens Axboef74441e2020-08-05 13:00:44 -06009163 /*
9164 * Account memory _before_ installing the file descriptor. Once
9165 * the descriptor is installed, it can get closed at any time. Also
9166 * do this before hitting the general error path, as ring freeing
9167 * will un-account as well.
9168 */
9169 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9170 ACCT_LOCKED);
9171 ctx->limit_mem = limit_mem;
9172
Jens Axboe2b188cc2019-01-07 10:46:33 -07009173 ret = io_allocate_scq_urings(ctx, p);
9174 if (ret)
9175 goto err;
9176
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009177 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009178 if (ret)
9179 goto err;
9180
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009181 if (!(p->flags & IORING_SETUP_R_DISABLED))
9182 io_sq_offload_start(ctx);
9183
Jens Axboe2b188cc2019-01-07 10:46:33 -07009184 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009185 p->sq_off.head = offsetof(struct io_rings, sq.head);
9186 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9187 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9188 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9189 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9190 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9191 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009192
9193 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009194 p->cq_off.head = offsetof(struct io_rings, cq.head);
9195 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9196 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9197 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9198 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9199 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009200 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009201
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009202 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9203 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009204 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9205 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009206
9207 if (copy_to_user(params, p, sizeof(*p))) {
9208 ret = -EFAULT;
9209 goto err;
9210 }
Jens Axboed1719f72020-07-30 13:43:53 -06009211
9212 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009213 * Install ring fd as the very last thing, so we don't risk someone
9214 * having closed it before we finish setup
9215 */
9216 ret = io_uring_get_fd(ctx);
9217 if (ret < 0)
9218 goto err;
9219
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009220 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009221 return ret;
9222err:
9223 io_ring_ctx_wait_and_kill(ctx);
9224 return ret;
9225}
9226
9227/*
9228 * Sets up an aio uring context, and returns the fd. Applications asks for a
9229 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9230 * params structure passed in.
9231 */
9232static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9233{
9234 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009235 int i;
9236
9237 if (copy_from_user(&p, params, sizeof(p)))
9238 return -EFAULT;
9239 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9240 if (p.resv[i])
9241 return -EINVAL;
9242 }
9243
Jens Axboe6c271ce2019-01-10 11:22:30 -07009244 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009245 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009246 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9247 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009248 return -EINVAL;
9249
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009250 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009251}
9252
9253SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9254 struct io_uring_params __user *, params)
9255{
9256 return io_uring_setup(entries, params);
9257}
9258
Jens Axboe66f4af92020-01-16 15:36:52 -07009259static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9260{
9261 struct io_uring_probe *p;
9262 size_t size;
9263 int i, ret;
9264
9265 size = struct_size(p, ops, nr_args);
9266 if (size == SIZE_MAX)
9267 return -EOVERFLOW;
9268 p = kzalloc(size, GFP_KERNEL);
9269 if (!p)
9270 return -ENOMEM;
9271
9272 ret = -EFAULT;
9273 if (copy_from_user(p, arg, size))
9274 goto out;
9275 ret = -EINVAL;
9276 if (memchr_inv(p, 0, size))
9277 goto out;
9278
9279 p->last_op = IORING_OP_LAST - 1;
9280 if (nr_args > IORING_OP_LAST)
9281 nr_args = IORING_OP_LAST;
9282
9283 for (i = 0; i < nr_args; i++) {
9284 p->ops[i].op = i;
9285 if (!io_op_defs[i].not_supported)
9286 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9287 }
9288 p->ops_len = i;
9289
9290 ret = 0;
9291 if (copy_to_user(arg, p, size))
9292 ret = -EFAULT;
9293out:
9294 kfree(p);
9295 return ret;
9296}
9297
Jens Axboe071698e2020-01-28 10:04:42 -07009298static int io_register_personality(struct io_ring_ctx *ctx)
9299{
9300 const struct cred *creds = get_current_cred();
9301 int id;
9302
9303 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9304 USHRT_MAX, GFP_KERNEL);
9305 if (id < 0)
9306 put_cred(creds);
9307 return id;
9308}
9309
9310static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9311{
9312 const struct cred *old_creds;
9313
9314 old_creds = idr_remove(&ctx->personality_idr, id);
9315 if (old_creds) {
9316 put_cred(old_creds);
9317 return 0;
9318 }
9319
9320 return -EINVAL;
9321}
9322
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009323static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9324 unsigned int nr_args)
9325{
9326 struct io_uring_restriction *res;
9327 size_t size;
9328 int i, ret;
9329
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009330 /* Restrictions allowed only if rings started disabled */
9331 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9332 return -EBADFD;
9333
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009334 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009335 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009336 return -EBUSY;
9337
9338 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9339 return -EINVAL;
9340
9341 size = array_size(nr_args, sizeof(*res));
9342 if (size == SIZE_MAX)
9343 return -EOVERFLOW;
9344
9345 res = memdup_user(arg, size);
9346 if (IS_ERR(res))
9347 return PTR_ERR(res);
9348
9349 ret = 0;
9350
9351 for (i = 0; i < nr_args; i++) {
9352 switch (res[i].opcode) {
9353 case IORING_RESTRICTION_REGISTER_OP:
9354 if (res[i].register_op >= IORING_REGISTER_LAST) {
9355 ret = -EINVAL;
9356 goto out;
9357 }
9358
9359 __set_bit(res[i].register_op,
9360 ctx->restrictions.register_op);
9361 break;
9362 case IORING_RESTRICTION_SQE_OP:
9363 if (res[i].sqe_op >= IORING_OP_LAST) {
9364 ret = -EINVAL;
9365 goto out;
9366 }
9367
9368 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9369 break;
9370 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9371 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9372 break;
9373 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9374 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9375 break;
9376 default:
9377 ret = -EINVAL;
9378 goto out;
9379 }
9380 }
9381
9382out:
9383 /* Reset all restrictions if an error happened */
9384 if (ret != 0)
9385 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9386 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009387 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009388
9389 kfree(res);
9390 return ret;
9391}
9392
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009393static int io_register_enable_rings(struct io_ring_ctx *ctx)
9394{
9395 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9396 return -EBADFD;
9397
9398 if (ctx->restrictions.registered)
9399 ctx->restricted = 1;
9400
9401 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9402
9403 io_sq_offload_start(ctx);
9404
9405 return 0;
9406}
9407
Jens Axboe071698e2020-01-28 10:04:42 -07009408static bool io_register_op_must_quiesce(int op)
9409{
9410 switch (op) {
9411 case IORING_UNREGISTER_FILES:
9412 case IORING_REGISTER_FILES_UPDATE:
9413 case IORING_REGISTER_PROBE:
9414 case IORING_REGISTER_PERSONALITY:
9415 case IORING_UNREGISTER_PERSONALITY:
9416 return false;
9417 default:
9418 return true;
9419 }
9420}
9421
Jens Axboeedafcce2019-01-09 09:16:05 -07009422static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9423 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009424 __releases(ctx->uring_lock)
9425 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009426{
9427 int ret;
9428
Jens Axboe35fa71a2019-04-22 10:23:23 -06009429 /*
9430 * We're inside the ring mutex, if the ref is already dying, then
9431 * someone else killed the ctx or is already going through
9432 * io_uring_register().
9433 */
9434 if (percpu_ref_is_dying(&ctx->refs))
9435 return -ENXIO;
9436
Jens Axboe071698e2020-01-28 10:04:42 -07009437 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009438 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009439
Jens Axboe05f3fb32019-12-09 11:22:50 -07009440 /*
9441 * Drop uring mutex before waiting for references to exit. If
9442 * another thread is currently inside io_uring_enter() it might
9443 * need to grab the uring_lock to make progress. If we hold it
9444 * here across the drain wait, then we can deadlock. It's safe
9445 * to drop the mutex here, since no new references will come in
9446 * after we've killed the percpu ref.
9447 */
9448 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009449 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009450 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009451 if (ret) {
9452 percpu_ref_resurrect(&ctx->refs);
9453 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009454 goto out_quiesce;
9455 }
9456 }
9457
9458 if (ctx->restricted) {
9459 if (opcode >= IORING_REGISTER_LAST) {
9460 ret = -EINVAL;
9461 goto out;
9462 }
9463
9464 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9465 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009466 goto out;
9467 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009468 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009469
9470 switch (opcode) {
9471 case IORING_REGISTER_BUFFERS:
9472 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9473 break;
9474 case IORING_UNREGISTER_BUFFERS:
9475 ret = -EINVAL;
9476 if (arg || nr_args)
9477 break;
9478 ret = io_sqe_buffer_unregister(ctx);
9479 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009480 case IORING_REGISTER_FILES:
9481 ret = io_sqe_files_register(ctx, arg, nr_args);
9482 break;
9483 case IORING_UNREGISTER_FILES:
9484 ret = -EINVAL;
9485 if (arg || nr_args)
9486 break;
9487 ret = io_sqe_files_unregister(ctx);
9488 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009489 case IORING_REGISTER_FILES_UPDATE:
9490 ret = io_sqe_files_update(ctx, arg, nr_args);
9491 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009492 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009493 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009494 ret = -EINVAL;
9495 if (nr_args != 1)
9496 break;
9497 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009498 if (ret)
9499 break;
9500 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9501 ctx->eventfd_async = 1;
9502 else
9503 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009504 break;
9505 case IORING_UNREGISTER_EVENTFD:
9506 ret = -EINVAL;
9507 if (arg || nr_args)
9508 break;
9509 ret = io_eventfd_unregister(ctx);
9510 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009511 case IORING_REGISTER_PROBE:
9512 ret = -EINVAL;
9513 if (!arg || nr_args > 256)
9514 break;
9515 ret = io_probe(ctx, arg, nr_args);
9516 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009517 case IORING_REGISTER_PERSONALITY:
9518 ret = -EINVAL;
9519 if (arg || nr_args)
9520 break;
9521 ret = io_register_personality(ctx);
9522 break;
9523 case IORING_UNREGISTER_PERSONALITY:
9524 ret = -EINVAL;
9525 if (arg)
9526 break;
9527 ret = io_unregister_personality(ctx, nr_args);
9528 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009529 case IORING_REGISTER_ENABLE_RINGS:
9530 ret = -EINVAL;
9531 if (arg || nr_args)
9532 break;
9533 ret = io_register_enable_rings(ctx);
9534 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009535 case IORING_REGISTER_RESTRICTIONS:
9536 ret = io_register_restrictions(ctx, arg, nr_args);
9537 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009538 default:
9539 ret = -EINVAL;
9540 break;
9541 }
9542
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009543out:
Jens Axboe071698e2020-01-28 10:04:42 -07009544 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009545 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009546 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009547out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009548 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009549 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009550 return ret;
9551}
9552
9553SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9554 void __user *, arg, unsigned int, nr_args)
9555{
9556 struct io_ring_ctx *ctx;
9557 long ret = -EBADF;
9558 struct fd f;
9559
9560 f = fdget(fd);
9561 if (!f.file)
9562 return -EBADF;
9563
9564 ret = -EOPNOTSUPP;
9565 if (f.file->f_op != &io_uring_fops)
9566 goto out_fput;
9567
9568 ctx = f.file->private_data;
9569
9570 mutex_lock(&ctx->uring_lock);
9571 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9572 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009573 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9574 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009575out_fput:
9576 fdput(f);
9577 return ret;
9578}
9579
Jens Axboe2b188cc2019-01-07 10:46:33 -07009580static int __init io_uring_init(void)
9581{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009582#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9583 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9584 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9585} while (0)
9586
9587#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9588 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9589 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9590 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9591 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9592 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9593 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9594 BUILD_BUG_SQE_ELEM(8, __u64, off);
9595 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9596 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009597 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009598 BUILD_BUG_SQE_ELEM(24, __u32, len);
9599 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9600 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9601 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9602 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009603 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9604 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009605 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9606 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9607 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9608 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9609 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9610 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9611 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9612 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009613 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009614 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9615 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9616 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009617 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009618
Jens Axboed3656342019-12-18 09:50:26 -07009619 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009620 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009621 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9622 return 0;
9623};
9624__initcall(io_uring_init);