blob: bc7028d9e6f9248248d7fac3fcc1ec00c6b078da [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020084#define CREATE_TRACE_POINTS
85#include <trace/events/io_uring.h>
86
Jens Axboe2b188cc2019-01-07 10:46:33 -070087#include <uapi/linux/io_uring.h>
88
89#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060090#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
Daniel Xu5277dea2019-09-14 14:23:45 -070092#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060093#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060094
95/*
96 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
97 */
98#define IORING_FILE_TABLE_SHIFT 9
99#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
100#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
101#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200102#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
103 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700104
105struct io_uring {
106 u32 head ____cacheline_aligned_in_smp;
107 u32 tail ____cacheline_aligned_in_smp;
108};
109
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000111 * This data is shared with the application through the mmap at offsets
112 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 *
114 * The offsets to the member fields are published through struct
115 * io_sqring_offsets when calling io_uring_setup.
116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
119 * Head and tail offsets into the ring; the offsets need to be
120 * masked to get valid indices.
121 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * The kernel controls head of the sq ring and the tail of the cq ring,
123 * and the application controls tail of the sq ring and the head of the
124 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 * ring_entries - 1)
130 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 u32 sq_ring_mask, cq_ring_mask;
132 /* Ring sizes (constant, power of 2) */
133 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Number of invalid entries dropped by the kernel due to
136 * invalid index stored in array
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application (i.e. get number of "new events" by comparing to
140 * cached value).
141 *
142 * After a new SQ head value was read by the application this
143 * counter includes all submissions that were dropped reaching
144 * the new SQ head (and possibly more).
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200148 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application.
152 *
153 * The application needs a full memory barrier before checking
154 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
155 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000156 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200158 * Runtime CQ flags
159 *
160 * Written by the application, shouldn't be modified by the
161 * kernel.
162 */
163 u32 cq_flags;
164 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * Number of completion events lost because the queue was full;
166 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800167 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 * the completion queue.
169 *
170 * Written by the kernel, shouldn't be modified by the
171 * application (i.e. get number of "new events" by comparing to
172 * cached value).
173 *
174 * As completion events come in out of order this counter is not
175 * ordered with any other data.
176 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000177 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200178 /*
179 * Ring buffer of completion events.
180 *
181 * The kernel writes completion events fresh every time they are
182 * produced, so the application is allowed to modify pending
183 * entries.
184 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000185 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700186};
187
Jens Axboeedafcce2019-01-09 09:16:05 -0700188struct io_mapped_ubuf {
189 u64 ubuf;
190 size_t len;
191 struct bio_vec *bvec;
192 unsigned int nr_bvecs;
193};
194
Jens Axboe65e19f52019-10-26 07:20:21 -0600195struct fixed_file_table {
196 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700197};
198
Xiaoguang Wang05589552020-03-31 14:05:18 +0800199struct fixed_file_ref_node {
200 struct percpu_ref refs;
201 struct list_head node;
202 struct list_head file_list;
203 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600204 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800205};
206
Jens Axboe05f3fb32019-12-09 11:22:50 -0700207struct fixed_file_data {
208 struct fixed_file_table *table;
209 struct io_ring_ctx *ctx;
210
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700212 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800214 struct list_head ref_list;
215 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216};
217
Jens Axboe5a2e7452020-02-23 16:23:11 -0700218struct io_buffer {
219 struct list_head list;
220 __u64 addr;
221 __s32 len;
222 __u16 bid;
223};
224
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200225struct io_restriction {
226 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
227 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
228 u8 sqe_flags_allowed;
229 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200230 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200231};
232
Jens Axboe534ca6d2020-09-02 13:52:19 -0600233struct io_sq_data {
234 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600235 struct mutex lock;
236
237 /* ctx's that are using this sqd */
238 struct list_head ctx_list;
239 struct list_head ctx_new_list;
240 struct mutex ctx_lock;
241
Jens Axboe534ca6d2020-09-02 13:52:19 -0600242 struct task_struct *thread;
243 struct wait_queue_head wait;
244};
245
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246struct io_ring_ctx {
247 struct {
248 struct percpu_ref refs;
249 } ____cacheline_aligned_in_smp;
250
251 struct {
252 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800253 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700254 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800255 unsigned int cq_overflow_flushed: 1;
256 unsigned int drain_next: 1;
257 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200258 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259
Hristo Venev75b28af2019-08-26 17:23:46 +0000260 /*
261 * Ring buffer of indices into array of io_uring_sqe, which is
262 * mmapped by the application using the IORING_OFF_SQES offset.
263 *
264 * This indirection could e.g. be used to assign fixed
265 * io_uring_sqe entries to operations and only submit them to
266 * the queue when needed.
267 *
268 * The kernel modifies neither the indices array nor the entries
269 * array.
270 */
271 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700272 unsigned cached_sq_head;
273 unsigned sq_entries;
274 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700275 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600276 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700278 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600279
280 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600281 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700282 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283
Jens Axboefcb323c2019-10-24 12:39:47 -0600284 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700285 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286 } ____cacheline_aligned_in_smp;
287
Hristo Venev75b28af2019-08-26 17:23:46 +0000288 struct io_rings *rings;
289
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600291 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600292
293 /*
294 * For SQPOLL usage - we hold a reference to the parent task, so we
295 * have access to the ->files
296 */
297 struct task_struct *sqo_task;
298
299 /* Only used for accounting purposes */
300 struct mm_struct *mm_account;
301
Jens Axboe534ca6d2020-09-02 13:52:19 -0600302 struct io_sq_data *sq_data; /* if using sq thread polling */
303
Jens Axboe90554202020-09-03 12:12:41 -0600304 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600305 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600306 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307
Jens Axboe6b063142019-01-10 22:13:58 -0700308 /*
309 * If used, fixed file set. Writers must ensure that ->refs is dead,
310 * readers must ensure that ->refs is alive as long as the file* is
311 * used. Only updated through io_uring_register(2).
312 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700313 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700314 unsigned nr_user_files;
315
Jens Axboeedafcce2019-01-09 09:16:05 -0700316 /* if used, fixed mapped user buffers */
317 unsigned nr_user_bufs;
318 struct io_mapped_ubuf *user_bufs;
319
Jens Axboe2b188cc2019-01-07 10:46:33 -0700320 struct user_struct *user;
321
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700322 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700323
Jens Axboe0f158b42020-05-14 17:18:39 -0600324 struct completion ref_comp;
325 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700326
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700327 /* if all else fails... */
328 struct io_kiocb *fallback_req;
329
Jens Axboe206aefd2019-11-07 18:27:42 -0700330#if defined(CONFIG_UNIX)
331 struct socket *ring_sock;
332#endif
333
Jens Axboe5a2e7452020-02-23 16:23:11 -0700334 struct idr io_buffer_idr;
335
Jens Axboe071698e2020-01-28 10:04:42 -0700336 struct idr personality_idr;
337
Jens Axboe206aefd2019-11-07 18:27:42 -0700338 struct {
339 unsigned cached_cq_tail;
340 unsigned cq_entries;
341 unsigned cq_mask;
342 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700343 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700344 struct wait_queue_head cq_wait;
345 struct fasync_struct *cq_fasync;
346 struct eventfd_ctx *cq_ev_fd;
347 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700348
349 struct {
350 struct mutex uring_lock;
351 wait_queue_head_t wait;
352 } ____cacheline_aligned_in_smp;
353
354 struct {
355 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700356
Jens Axboedef596e2019-01-09 08:59:42 -0700357 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300358 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700359 * io_uring instances that don't use IORING_SETUP_SQPOLL.
360 * For SQPOLL, only the single threaded io_sq_thread() will
361 * manipulate the list, hence no extra locking is needed there.
362 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300363 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700364 struct hlist_head *cancel_hash;
365 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700366 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600367
368 spinlock_t inflight_lock;
369 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700370 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600371
Jens Axboe4a38aed22020-05-14 17:21:15 -0600372 struct delayed_work file_put_work;
373 struct llist_head file_put_llist;
374
Jens Axboe85faa7b2020-04-09 18:14:00 -0600375 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200376 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700377};
378
Jens Axboe09bb8392019-03-13 12:39:28 -0600379/*
380 * First field must be the file pointer in all the
381 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
382 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700383struct io_poll_iocb {
384 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700385 union {
386 struct wait_queue_head *head;
387 u64 addr;
388 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700389 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600390 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700391 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700392 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700393};
394
Jens Axboeb5dba592019-12-11 14:02:38 -0700395struct io_close {
396 struct file *file;
397 struct file *put_file;
398 int fd;
399};
400
Jens Axboead8a48a2019-11-15 08:49:11 -0700401struct io_timeout_data {
402 struct io_kiocb *req;
403 struct hrtimer timer;
404 struct timespec64 ts;
405 enum hrtimer_mode mode;
406};
407
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700408struct io_accept {
409 struct file *file;
410 struct sockaddr __user *addr;
411 int __user *addr_len;
412 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600413 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700414};
415
416struct io_sync {
417 struct file *file;
418 loff_t len;
419 loff_t off;
420 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700421 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700422};
423
Jens Axboefbf23842019-12-17 18:45:56 -0700424struct io_cancel {
425 struct file *file;
426 u64 addr;
427};
428
Jens Axboeb29472e2019-12-17 18:50:29 -0700429struct io_timeout {
430 struct file *file;
431 u64 addr;
432 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300433 u32 off;
434 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300435 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700436};
437
Jens Axboe9adbd452019-12-20 08:45:55 -0700438struct io_rw {
439 /* NOTE: kiocb has the file as the first member, so don't do it here */
440 struct kiocb kiocb;
441 u64 addr;
442 u64 len;
443};
444
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700445struct io_connect {
446 struct file *file;
447 struct sockaddr __user *addr;
448 int addr_len;
449};
450
Jens Axboee47293f2019-12-20 08:58:21 -0700451struct io_sr_msg {
452 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700453 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300454 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700455 void __user *buf;
456 };
Jens Axboee47293f2019-12-20 08:58:21 -0700457 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700458 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700459 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700460 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700461};
462
Jens Axboe15b71ab2019-12-11 11:20:36 -0700463struct io_open {
464 struct file *file;
465 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700466 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700467 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600468 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700469};
470
Jens Axboe05f3fb32019-12-09 11:22:50 -0700471struct io_files_update {
472 struct file *file;
473 u64 arg;
474 u32 nr_args;
475 u32 offset;
476};
477
Jens Axboe4840e412019-12-25 22:03:45 -0700478struct io_fadvise {
479 struct file *file;
480 u64 offset;
481 u32 len;
482 u32 advice;
483};
484
Jens Axboec1ca7572019-12-25 22:18:28 -0700485struct io_madvise {
486 struct file *file;
487 u64 addr;
488 u32 len;
489 u32 advice;
490};
491
Jens Axboe3e4827b2020-01-08 15:18:09 -0700492struct io_epoll {
493 struct file *file;
494 int epfd;
495 int op;
496 int fd;
497 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498};
499
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300500struct io_splice {
501 struct file *file_out;
502 struct file *file_in;
503 loff_t off_out;
504 loff_t off_in;
505 u64 len;
506 unsigned int flags;
507};
508
Jens Axboeddf0322d2020-02-23 16:41:33 -0700509struct io_provide_buf {
510 struct file *file;
511 __u64 addr;
512 __s32 len;
513 __u32 bgid;
514 __u16 nbufs;
515 __u16 bid;
516};
517
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700518struct io_statx {
519 struct file *file;
520 int dfd;
521 unsigned int mask;
522 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700523 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700524 struct statx __user *buffer;
525};
526
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300527struct io_completion {
528 struct file *file;
529 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300530 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300531};
532
Jens Axboef499a022019-12-02 16:28:46 -0700533struct io_async_connect {
534 struct sockaddr_storage address;
535};
536
Jens Axboe03b12302019-12-02 18:50:25 -0700537struct io_async_msghdr {
538 struct iovec fast_iov[UIO_FASTIOV];
539 struct iovec *iov;
540 struct sockaddr __user *uaddr;
541 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700542 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700543};
544
Jens Axboef67676d2019-12-02 11:03:47 -0700545struct io_async_rw {
546 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600547 const struct iovec *free_iovec;
548 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600549 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600550 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700551};
552
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700553struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700554 union {
555 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700556 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700557 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700558 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700559 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700560};
561
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562enum {
563 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
564 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
565 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
566 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
567 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700568 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300569
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300570 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300571 REQ_F_FAIL_LINK_BIT,
572 REQ_F_INFLIGHT_BIT,
573 REQ_F_CUR_POS_BIT,
574 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300576 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300578 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700579 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700580 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600581 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800582 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700583
584 /* not a real bit, just to check we're not overflowing the space */
585 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300586};
587
588enum {
589 /* ctx owns file */
590 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
591 /* drain existing IO first */
592 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
593 /* linked sqes */
594 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
595 /* doesn't sever on completion < 0 */
596 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
597 /* IOSQE_ASYNC */
598 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700599 /* IOSQE_BUFFER_SELECT */
600 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300601
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300602 /* head of a link */
603 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300604 /* fail rest of links */
605 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
606 /* on inflight list */
607 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
608 /* read/write uses file position */
609 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
610 /* must not punt to workers */
611 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300612 /* has linked timeout */
613 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300614 /* regular file */
615 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300616 /* completion under lock */
617 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300618 /* needs cleanup */
619 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700620 /* already went through poll handler */
621 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700622 /* buffer already selected */
623 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600624 /* doesn't need file table for this request */
625 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800626 /* io_wq_work is initialized */
627 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700628};
629
630struct async_poll {
631 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600632 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300633};
634
Jens Axboe09bb8392019-03-13 12:39:28 -0600635/*
636 * NOTE! Each of the iocb union members has the file pointer
637 * as the first entry in their struct definition. So you can
638 * access the file pointer through any of the sub-structs,
639 * or directly as just 'ki_filp' in this struct.
640 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700642 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600643 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700644 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700645 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700646 struct io_accept accept;
647 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700648 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700649 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700650 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700651 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700652 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700653 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700654 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700655 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700656 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700657 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300658 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700659 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700660 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300661 /* use only after cleaning per-op data, see io_clean_op() */
662 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700663 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700664
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700665 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700666 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800667 /* polled IO has completed */
668 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700669
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700670 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300671 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700672
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300673 struct io_ring_ctx *ctx;
674 unsigned int flags;
675 refcount_t refs;
676 struct task_struct *task;
677 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700678
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300679 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700680
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300681 /*
682 * 1. used with ctx->iopoll_list with reads/writes
683 * 2. to track reqs with ->files (see io_op_def::file_table)
684 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300685 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600686
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300687 struct percpu_ref *fixed_file_refs;
688 struct callback_head task_work;
689 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
690 struct hlist_node hash_node;
691 struct async_poll *apoll;
692 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700693};
694
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300695struct io_defer_entry {
696 struct list_head list;
697 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300698 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300699};
700
Jens Axboedef596e2019-01-09 08:59:42 -0700701#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700702
Jens Axboe013538b2020-06-22 09:29:15 -0600703struct io_comp_state {
704 unsigned int nr;
705 struct list_head list;
706 struct io_ring_ctx *ctx;
707};
708
Jens Axboe9a56a232019-01-09 09:06:50 -0700709struct io_submit_state {
710 struct blk_plug plug;
711
712 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700713 * io_kiocb alloc cache
714 */
715 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300716 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700717
718 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600719 * Batch completion logic
720 */
721 struct io_comp_state comp;
722
723 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700724 * File reference cache
725 */
726 struct file *file;
727 unsigned int fd;
728 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700729 unsigned int ios_left;
730};
731
Jens Axboed3656342019-12-18 09:50:26 -0700732struct io_op_def {
733 /* needs req->io allocated for deferral/async */
734 unsigned async_ctx : 1;
735 /* needs current->mm setup, does mm access */
736 unsigned needs_mm : 1;
737 /* needs req->file assigned */
738 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600739 /* don't fail if file grab fails */
740 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700741 /* hash wq insertion if file is a regular file */
742 unsigned hash_reg_file : 1;
743 /* unbound wq insertion if file is a non-regular file */
744 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700745 /* opcode is not supported by this kernel */
746 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700747 /* needs file table */
748 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700749 /* needs ->fs */
750 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700751 /* set if opcode supports polled "wait" */
752 unsigned pollin : 1;
753 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700754 /* op supports buffer selection */
755 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300756 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700757};
758
Jens Axboe738277a2020-09-03 05:54:56 -0600759static const struct io_op_def io_op_defs[] __read_mostly = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_NOP] = {},
761 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700762 .async_ctx = 1,
763 .needs_mm = 1,
764 .needs_file = 1,
765 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700766 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700767 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .async_ctx = 1,
771 .needs_mm = 1,
772 .needs_file = 1,
773 .hash_reg_file = 1,
774 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700775 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300776 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700779 .needs_file = 1,
780 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300781 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700782 .needs_file = 1,
783 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700784 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700785 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300786 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700787 .needs_file = 1,
788 .hash_reg_file = 1,
789 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700790 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300791 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700792 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300793 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700794 .needs_file = 1,
795 .unbound_nonreg_file = 1,
796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_POLL_REMOVE] = {},
798 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700799 .needs_file = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700802 .async_ctx = 1,
803 .needs_mm = 1,
804 .needs_file = 1,
805 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700806 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700807 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .async_ctx = 1,
811 .needs_mm = 1,
812 .needs_file = 1,
813 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700814 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700815 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700816 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .async_ctx = 1,
820 .needs_mm = 1,
821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_TIMEOUT_REMOVE] = {},
823 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_mm = 1,
825 .needs_file = 1,
826 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700827 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700828 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_ASYNC_CANCEL] = {},
831 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700832 .async_ctx = 1,
833 .needs_mm = 1,
834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .async_ctx = 1,
837 .needs_mm = 1,
838 .needs_file = 1,
839 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700840 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700843 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300844 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700847 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700848 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700849 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300850 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600851 .needs_file = 1,
852 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700853 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700856 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700857 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700858 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300859 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700860 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700861 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600862 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700865 .needs_mm = 1,
866 .needs_file = 1,
867 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700868 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700869 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700872 .needs_mm = 1,
873 .needs_file = 1,
874 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700875 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300876 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700879 .needs_file = 1,
880 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300881 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700882 .needs_mm = 1,
883 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300884 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700885 .needs_mm = 1,
886 .needs_file = 1,
887 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700888 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700889 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300890 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700891 .needs_mm = 1,
892 .needs_file = 1,
893 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700894 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700895 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700898 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700899 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700900 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700901 [IORING_OP_EPOLL_CTL] = {
902 .unbound_nonreg_file = 1,
903 .file_table = 1,
904 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300905 [IORING_OP_SPLICE] = {
906 .needs_file = 1,
907 .hash_reg_file = 1,
908 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700909 },
910 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700911 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300912 [IORING_OP_TEE] = {
913 .needs_file = 1,
914 .hash_reg_file = 1,
915 .unbound_nonreg_file = 1,
916 },
Jens Axboed3656342019-12-18 09:50:26 -0700917};
918
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700919enum io_mem_account {
920 ACCT_LOCKED,
921 ACCT_PINNED,
922};
923
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300924static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
925 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700926static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800927static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600928static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700929static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700930static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600931static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700932static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700933static int __io_sqe_files_update(struct io_ring_ctx *ctx,
934 struct io_uring_files_update *ip,
935 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300936static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300937static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700938static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
939 int fd, struct file **out_file, bool fixed);
940static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600941 const struct io_uring_sqe *sqe,
942 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600943static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600944
Jens Axboeb63534c2020-06-04 11:28:00 -0600945static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
946 struct iovec **iovec, struct iov_iter *iter,
947 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600948static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
949 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600950 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700951
952static struct kmem_cache *req_cachep;
953
Jens Axboe738277a2020-09-03 05:54:56 -0600954static const struct file_operations io_uring_fops __read_mostly;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700955
956struct sock *io_uring_get_socket(struct file *file)
957{
958#if defined(CONFIG_UNIX)
959 if (file->f_op == &io_uring_fops) {
960 struct io_ring_ctx *ctx = file->private_data;
961
962 return ctx->ring_sock->sk;
963 }
964#endif
965 return NULL;
966}
967EXPORT_SYMBOL(io_uring_get_socket);
968
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300969static inline void io_clean_op(struct io_kiocb *req)
970{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300971 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
972 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300973 __io_clean_op(req);
974}
975
Jens Axboe4349f302020-07-09 15:07:01 -0600976static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600977{
978 struct mm_struct *mm = current->mm;
979
980 if (mm) {
981 kthread_unuse_mm(mm);
982 mmput(mm);
983 }
984}
985
986static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
987{
988 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300989 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600990 !ctx->sqo_task->mm ||
991 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600992 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600993 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600994 }
995
996 return 0;
997}
998
999static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1000 struct io_kiocb *req)
1001{
1002 if (!io_op_defs[req->opcode].needs_mm)
1003 return 0;
1004 return __io_sq_thread_acquire_mm(ctx);
1005}
1006
1007static inline void req_set_fail_links(struct io_kiocb *req)
1008{
1009 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1010 req->flags |= REQ_F_FAIL_LINK;
1011}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001012
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001013/*
1014 * Note: must call io_req_init_async() for the first time you
1015 * touch any members of io_wq_work.
1016 */
1017static inline void io_req_init_async(struct io_kiocb *req)
1018{
1019 if (req->flags & REQ_F_WORK_INITIALIZED)
1020 return;
1021
1022 memset(&req->work, 0, sizeof(req->work));
1023 req->flags |= REQ_F_WORK_INITIALIZED;
1024}
1025
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001026static inline bool io_async_submit(struct io_ring_ctx *ctx)
1027{
1028 return ctx->flags & IORING_SETUP_SQPOLL;
1029}
1030
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1032{
1033 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1034
Jens Axboe0f158b42020-05-14 17:18:39 -06001035 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001036}
1037
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001038static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1039{
1040 return !req->timeout.off;
1041}
1042
Jens Axboe2b188cc2019-01-07 10:46:33 -07001043static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1044{
1045 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001046 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047
1048 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1049 if (!ctx)
1050 return NULL;
1051
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001052 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1053 if (!ctx->fallback_req)
1054 goto err;
1055
Jens Axboe78076bb2019-12-04 19:56:40 -07001056 /*
1057 * Use 5 bits less than the max cq entries, that should give us around
1058 * 32 entries per hash list if totally full and uniformly spread.
1059 */
1060 hash_bits = ilog2(p->cq_entries);
1061 hash_bits -= 5;
1062 if (hash_bits <= 0)
1063 hash_bits = 1;
1064 ctx->cancel_hash_bits = hash_bits;
1065 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1066 GFP_KERNEL);
1067 if (!ctx->cancel_hash)
1068 goto err;
1069 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1070
Roman Gushchin21482892019-05-07 10:01:48 -07001071 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001072 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1073 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074
1075 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001076 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001077 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001078 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001079 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001080 init_completion(&ctx->ref_comp);
1081 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001082 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001083 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 mutex_init(&ctx->uring_lock);
1085 init_waitqueue_head(&ctx->wait);
1086 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001087 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001088 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001089 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001090 init_waitqueue_head(&ctx->inflight_wait);
1091 spin_lock_init(&ctx->inflight_lock);
1092 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001093 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1094 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001096err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001097 if (ctx->fallback_req)
1098 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001099 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001100 kfree(ctx);
1101 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102}
1103
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001104static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001105{
Jens Axboe2bc99302020-07-09 09:43:27 -06001106 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1107 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001108
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001109 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001110 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001111 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001112
Bob Liu9d858b22019-11-13 18:06:25 +08001113 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001114}
1115
Jens Axboede0617e2019-04-06 21:51:27 -06001116static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117{
Hristo Venev75b28af2019-08-26 17:23:46 +00001118 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119
Pavel Begunkov07910152020-01-17 03:52:46 +03001120 /* order cqe stores with ring update */
1121 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001122
Pavel Begunkov07910152020-01-17 03:52:46 +03001123 if (wq_has_sleeper(&ctx->cq_wait)) {
1124 wake_up_interruptible(&ctx->cq_wait);
1125 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001126 }
1127}
1128
Jens Axboe51a4cc12020-08-10 10:55:56 -06001129/*
1130 * Returns true if we need to defer file table putting. This can only happen
1131 * from the error path with REQ_F_COMP_LOCKED set.
1132 */
1133static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001134{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001135 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001136 return false;
1137
1138 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001139
Jens Axboecccf0ee2020-01-27 16:34:48 -07001140 if (req->work.mm) {
1141 mmdrop(req->work.mm);
1142 req->work.mm = NULL;
1143 }
1144 if (req->work.creds) {
1145 put_cred(req->work.creds);
1146 req->work.creds = NULL;
1147 }
Jens Axboeff002b32020-02-07 16:05:21 -07001148 if (req->work.fs) {
1149 struct fs_struct *fs = req->work.fs;
1150
Jens Axboe51a4cc12020-08-10 10:55:56 -06001151 if (req->flags & REQ_F_COMP_LOCKED)
1152 return true;
1153
Jens Axboeff002b32020-02-07 16:05:21 -07001154 spin_lock(&req->work.fs->lock);
1155 if (--fs->users)
1156 fs = NULL;
1157 spin_unlock(&req->work.fs->lock);
1158 if (fs)
1159 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001160 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001161 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001162
1163 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001164}
1165
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001166static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001167{
Jens Axboed3656342019-12-18 09:50:26 -07001168 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001169
Pavel Begunkov16d59802020-07-12 16:16:47 +03001170 io_req_init_async(req);
1171
Jens Axboed3656342019-12-18 09:50:26 -07001172 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001173 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001174 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001175 } else {
1176 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001177 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001178 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001179 if (!req->work.mm && def->needs_mm) {
1180 mmgrab(current->mm);
1181 req->work.mm = current->mm;
1182 }
1183 if (!req->work.creds)
1184 req->work.creds = get_current_cred();
1185 if (!req->work.fs && def->needs_fs) {
1186 spin_lock(&current->fs->lock);
1187 if (!current->fs->in_exec) {
1188 req->work.fs = current->fs;
1189 req->work.fs->users++;
1190 } else {
1191 req->work.flags |= IO_WQ_WORK_CANCEL;
1192 }
1193 spin_unlock(&current->fs->lock);
1194 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001195 if (def->needs_fsize)
1196 req->work.fsize = rlimit(RLIMIT_FSIZE);
1197 else
1198 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001199}
1200
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001201static void io_prep_async_link(struct io_kiocb *req)
1202{
1203 struct io_kiocb *cur;
1204
1205 io_prep_async_work(req);
1206 if (req->flags & REQ_F_LINK_HEAD)
1207 list_for_each_entry(cur, &req->link_list, link_list)
1208 io_prep_async_work(cur);
1209}
1210
Jens Axboe7271ef32020-08-10 09:55:22 -06001211static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001212{
Jackie Liua197f662019-11-08 08:09:12 -07001213 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001214 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001215
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001216 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1217 &req->work, req->flags);
1218 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001219 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001220}
1221
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001222static void io_queue_async_work(struct io_kiocb *req)
1223{
Jens Axboe7271ef32020-08-10 09:55:22 -06001224 struct io_kiocb *link;
1225
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001226 /* init ->work of the whole link before punting */
1227 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001228 link = __io_queue_async_work(req);
1229
1230 if (link)
1231 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001232}
1233
Jens Axboe5262f562019-09-17 12:26:57 -06001234static void io_kill_timeout(struct io_kiocb *req)
1235{
1236 int ret;
1237
Jens Axboe2d283902019-12-04 11:08:05 -07001238 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001239 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001240 atomic_set(&req->ctx->cq_timeouts,
1241 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001242 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001243 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001244 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001245 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001246 }
1247}
1248
Jens Axboef3606e32020-09-22 08:18:24 -06001249static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1250{
1251 struct io_ring_ctx *ctx = req->ctx;
1252
1253 if (!tsk || req->task == tsk)
1254 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001255 if (ctx->flags & IORING_SETUP_SQPOLL) {
1256 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1257 return true;
1258 }
Jens Axboef3606e32020-09-22 08:18:24 -06001259 return false;
1260}
1261
Jens Axboe76e1b642020-09-26 15:05:03 -06001262/*
1263 * Returns true if we found and killed one or more timeouts
1264 */
1265static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001266{
1267 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001268 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001269
1270 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001271 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001272 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001273 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001274 canceled++;
1275 }
Jens Axboef3606e32020-09-22 08:18:24 -06001276 }
Jens Axboe5262f562019-09-17 12:26:57 -06001277 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001278 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001279}
1280
Pavel Begunkov04518942020-05-26 20:34:05 +03001281static void __io_queue_deferred(struct io_ring_ctx *ctx)
1282{
1283 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001284 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1285 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001286 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001287
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001288 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001289 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001290 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001291 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001292 link = __io_queue_async_work(de->req);
1293 if (link) {
1294 __io_queue_linked_timeout(link);
1295 /* drop submission reference */
1296 link->flags |= REQ_F_COMP_LOCKED;
1297 io_put_req(link);
1298 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001299 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001300 } while (!list_empty(&ctx->defer_list));
1301}
1302
Pavel Begunkov360428f2020-05-30 14:54:17 +03001303static void io_flush_timeouts(struct io_ring_ctx *ctx)
1304{
1305 while (!list_empty(&ctx->timeout_list)) {
1306 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001307 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001308
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001309 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001310 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001311 if (req->timeout.target_seq != ctx->cached_cq_tail
1312 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001313 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001314
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001315 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001316 io_kill_timeout(req);
1317 }
1318}
1319
Jens Axboede0617e2019-04-06 21:51:27 -06001320static void io_commit_cqring(struct io_ring_ctx *ctx)
1321{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001322 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001323 __io_commit_cqring(ctx);
1324
Pavel Begunkov04518942020-05-26 20:34:05 +03001325 if (unlikely(!list_empty(&ctx->defer_list)))
1326 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001327}
1328
Jens Axboe90554202020-09-03 12:12:41 -06001329static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1330{
1331 struct io_rings *r = ctx->rings;
1332
1333 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1334}
1335
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1337{
Hristo Venev75b28af2019-08-26 17:23:46 +00001338 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339 unsigned tail;
1340
1341 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001342 /*
1343 * writes to the cq entry need to come after reading head; the
1344 * control dependency is enough as we're using WRITE_ONCE to
1345 * fill the cq entry
1346 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001347 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348 return NULL;
1349
1350 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001351 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352}
1353
Jens Axboef2842ab2020-01-08 11:04:00 -07001354static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1355{
Jens Axboef0b493e2020-02-01 21:30:11 -07001356 if (!ctx->cq_ev_fd)
1357 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001358 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1359 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001360 if (!ctx->eventfd_async)
1361 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001362 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001363}
1364
Jens Axboeb41e9852020-02-17 09:52:41 -07001365static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001366{
1367 if (waitqueue_active(&ctx->wait))
1368 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001369 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1370 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001371 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001372 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001373}
1374
Pavel Begunkov46930142020-07-30 18:43:49 +03001375static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1376{
1377 if (list_empty(&ctx->cq_overflow_list)) {
1378 clear_bit(0, &ctx->sq_check_overflow);
1379 clear_bit(0, &ctx->cq_check_overflow);
1380 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1381 }
1382}
1383
Jens Axboee6c8aa92020-09-28 13:10:13 -06001384static inline bool io_match_files(struct io_kiocb *req,
1385 struct files_struct *files)
1386{
1387 if (!files)
1388 return true;
1389 if (req->flags & REQ_F_WORK_INITIALIZED)
1390 return req->work.files == files;
1391 return false;
1392}
1393
Jens Axboec4a2ed72019-11-21 21:01:26 -07001394/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001395static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1396 struct task_struct *tsk,
1397 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001399 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001400 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001401 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001402 unsigned long flags;
1403 LIST_HEAD(list);
1404
1405 if (!force) {
1406 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001407 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001408 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1409 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001410 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001411 }
1412
1413 spin_lock_irqsave(&ctx->completion_lock, flags);
1414
1415 /* if force is set, the ring is going away. always drop after that */
1416 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001417 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001418
Jens Axboec4a2ed72019-11-21 21:01:26 -07001419 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001420 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1421 if (tsk && req->task != tsk)
1422 continue;
1423 if (!io_match_files(req, files))
1424 continue;
1425
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001426 cqe = io_get_cqring(ctx);
1427 if (!cqe && !force)
1428 break;
1429
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001430 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001431 if (cqe) {
1432 WRITE_ONCE(cqe->user_data, req->user_data);
1433 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001434 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001435 } else {
1436 WRITE_ONCE(ctx->rings->cq_overflow,
1437 atomic_inc_return(&ctx->cached_cq_overflow));
1438 }
1439 }
1440
1441 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001442 io_cqring_mark_overflow(ctx);
1443
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001444 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1445 io_cqring_ev_posted(ctx);
1446
1447 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001448 req = list_first_entry(&list, struct io_kiocb, compl.list);
1449 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001450 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001451 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001452
1453 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001454}
1455
Jens Axboebcda7ba2020-02-23 16:42:51 -07001456static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001457{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001459 struct io_uring_cqe *cqe;
1460
Jens Axboe78e19bb2019-11-06 15:21:34 -07001461 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001462
Jens Axboe2b188cc2019-01-07 10:46:33 -07001463 /*
1464 * If we can't get a cq entry, userspace overflowed the
1465 * submission (by quite a lot). Increment the overflow count in
1466 * the ring.
1467 */
1468 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001469 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001470 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001471 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001472 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001473 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1474 /*
1475 * If we're in ring overflow flush mode, or in task cancel mode,
1476 * then we cannot store the request for later flushing, we need
1477 * to drop it on the floor.
1478 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479 WRITE_ONCE(ctx->rings->cq_overflow,
1480 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001481 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001482 if (list_empty(&ctx->cq_overflow_list)) {
1483 set_bit(0, &ctx->sq_check_overflow);
1484 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001485 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001486 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001487 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001488 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001489 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001490 refcount_inc(&req->refs);
1491 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001492 }
1493}
1494
Jens Axboebcda7ba2020-02-23 16:42:51 -07001495static void io_cqring_fill_event(struct io_kiocb *req, long res)
1496{
1497 __io_cqring_fill_event(req, res, 0);
1498}
1499
Jens Axboee1e16092020-06-22 09:17:17 -06001500static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001503 unsigned long flags;
1504
1505 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001506 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001507 io_commit_cqring(ctx);
1508 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1509
Jens Axboe8c838782019-03-12 15:48:16 -06001510 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001511}
1512
Jens Axboe229a7b62020-06-22 10:13:11 -06001513static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001514{
Jens Axboe229a7b62020-06-22 10:13:11 -06001515 struct io_ring_ctx *ctx = cs->ctx;
1516
1517 spin_lock_irq(&ctx->completion_lock);
1518 while (!list_empty(&cs->list)) {
1519 struct io_kiocb *req;
1520
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001521 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1522 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001523 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001524 if (!(req->flags & REQ_F_LINK_HEAD)) {
1525 req->flags |= REQ_F_COMP_LOCKED;
1526 io_put_req(req);
1527 } else {
1528 spin_unlock_irq(&ctx->completion_lock);
1529 io_put_req(req);
1530 spin_lock_irq(&ctx->completion_lock);
1531 }
1532 }
1533 io_commit_cqring(ctx);
1534 spin_unlock_irq(&ctx->completion_lock);
1535
1536 io_cqring_ev_posted(ctx);
1537 cs->nr = 0;
1538}
1539
1540static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1541 struct io_comp_state *cs)
1542{
1543 if (!cs) {
1544 io_cqring_add_event(req, res, cflags);
1545 io_put_req(req);
1546 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001547 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001548 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001549 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001550 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001551 if (++cs->nr >= 32)
1552 io_submit_flush_completions(cs);
1553 }
Jens Axboee1e16092020-06-22 09:17:17 -06001554}
1555
1556static void io_req_complete(struct io_kiocb *req, long res)
1557{
Jens Axboe229a7b62020-06-22 10:13:11 -06001558 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001559}
1560
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001561static inline bool io_is_fallback_req(struct io_kiocb *req)
1562{
1563 return req == (struct io_kiocb *)
1564 ((unsigned long) req->ctx->fallback_req & ~1UL);
1565}
1566
1567static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1568{
1569 struct io_kiocb *req;
1570
1571 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001572 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001573 return req;
1574
1575 return NULL;
1576}
1577
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001578static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1579 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001580{
Jens Axboefd6fab22019-03-14 16:30:06 -06001581 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001582 struct io_kiocb *req;
1583
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001584 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001585 size_t sz;
1586 int ret;
1587
1588 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001589 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1590
1591 /*
1592 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1593 * retry single alloc to be on the safe side.
1594 */
1595 if (unlikely(ret <= 0)) {
1596 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1597 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001598 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001599 ret = 1;
1600 }
Jens Axboe2579f912019-01-09 09:10:43 -07001601 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001602 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001603 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001604 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001605 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606 }
1607
Jens Axboe2579f912019-01-09 09:10:43 -07001608 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001609fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001610 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001611}
1612
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001613static inline void io_put_file(struct io_kiocb *req, struct file *file,
1614 bool fixed)
1615{
1616 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001617 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001618 else
1619 fput(file);
1620}
1621
Jens Axboe51a4cc12020-08-10 10:55:56 -06001622static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001623{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001624 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001625
Jens Axboe5acbbc82020-07-08 15:15:26 -06001626 if (req->io)
1627 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001628 if (req->file)
1629 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001630
Jens Axboe51a4cc12020-08-10 10:55:56 -06001631 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001632}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001633
Jens Axboe51a4cc12020-08-10 10:55:56 -06001634static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001635{
Jens Axboe0f212202020-09-13 13:09:39 -06001636 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001637 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001638
Jens Axboe0f212202020-09-13 13:09:39 -06001639 atomic_long_inc(&tctx->req_complete);
1640 if (tctx->in_idle)
1641 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001642 put_task_struct(req->task);
1643
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001644 if (likely(!io_is_fallback_req(req)))
1645 kmem_cache_free(req_cachep, req);
1646 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001647 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1648 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001649}
1650
Jens Axboe51a4cc12020-08-10 10:55:56 -06001651static void io_req_task_file_table_put(struct callback_head *cb)
1652{
1653 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1654 struct fs_struct *fs = req->work.fs;
1655
1656 spin_lock(&req->work.fs->lock);
1657 if (--fs->users)
1658 fs = NULL;
1659 spin_unlock(&req->work.fs->lock);
1660 if (fs)
1661 free_fs_struct(fs);
1662 req->work.fs = NULL;
1663 __io_free_req_finish(req);
1664}
1665
1666static void __io_free_req(struct io_kiocb *req)
1667{
1668 if (!io_dismantle_req(req)) {
1669 __io_free_req_finish(req);
1670 } else {
1671 int ret;
1672
1673 init_task_work(&req->task_work, io_req_task_file_table_put);
1674 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1675 if (unlikely(ret)) {
1676 struct task_struct *tsk;
1677
1678 tsk = io_wq_get_task(req->ctx->io_wq);
1679 task_work_add(tsk, &req->task_work, 0);
1680 }
1681 }
1682}
1683
Jackie Liua197f662019-11-08 08:09:12 -07001684static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001685{
Jackie Liua197f662019-11-08 08:09:12 -07001686 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001687 int ret;
1688
Jens Axboe2d283902019-12-04 11:08:05 -07001689 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001690 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001691 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001692 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001693 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001694 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001695 return true;
1696 }
1697
1698 return false;
1699}
1700
Jens Axboeab0b6452020-06-30 08:43:15 -06001701static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001702{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001703 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001704 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001705
1706 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001707 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001708 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1709 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001710 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001711
1712 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001713 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001714 wake_ev = io_link_cancel_timeout(link);
1715 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001716 return wake_ev;
1717}
1718
1719static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001720{
Jens Axboe2665abf2019-11-05 12:40:47 -07001721 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001722 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001723
Jens Axboeab0b6452020-06-30 08:43:15 -06001724 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1725 unsigned long flags;
1726
1727 spin_lock_irqsave(&ctx->completion_lock, flags);
1728 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001729 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001730 } else {
1731 wake_ev = __io_kill_linked_timeout(req);
1732 }
1733
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001734 if (wake_ev)
1735 io_cqring_ev_posted(ctx);
1736}
1737
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001738static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001739{
1740 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001741
Jens Axboe9e645e112019-05-10 16:07:28 -06001742 /*
1743 * The list should never be empty when we are called here. But could
1744 * potentially happen if the chain is messed up, check to be on the
1745 * safe side.
1746 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001747 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001748 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001749
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001750 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1751 list_del_init(&req->link_list);
1752 if (!list_empty(&nxt->link_list))
1753 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001754 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001755}
1756
1757/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001758 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001759 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001760static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001761{
Jens Axboe2665abf2019-11-05 12:40:47 -07001762 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001763
1764 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001765 struct io_kiocb *link = list_first_entry(&req->link_list,
1766 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001767
Pavel Begunkov44932332019-12-05 16:16:35 +03001768 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001769 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001770
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001771 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001772 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001773 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001774 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001775 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001776
1777 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001778 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001779}
1780
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001781static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001782{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001783 struct io_ring_ctx *ctx = req->ctx;
1784
1785 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1786 unsigned long flags;
1787
1788 spin_lock_irqsave(&ctx->completion_lock, flags);
1789 __io_fail_links(req);
1790 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1791 } else {
1792 __io_fail_links(req);
1793 }
1794
Jens Axboe9e645e112019-05-10 16:07:28 -06001795 io_cqring_ev_posted(ctx);
1796}
1797
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001798static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001799{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001800 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001801 if (req->flags & REQ_F_LINK_TIMEOUT)
1802 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001803
Jens Axboe9e645e112019-05-10 16:07:28 -06001804 /*
1805 * If LINK is set, we have dependent requests in this chain. If we
1806 * didn't fail this request, queue the first one up, moving any other
1807 * dependencies to the next request. In case of failure, fail the rest
1808 * of the chain.
1809 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001810 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1811 return io_req_link_next(req);
1812 io_fail_links(req);
1813 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001814}
Jens Axboe2665abf2019-11-05 12:40:47 -07001815
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001816static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1817{
1818 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1819 return NULL;
1820 return __io_req_find_next(req);
1821}
1822
Jens Axboefd7d6de2020-08-23 11:00:37 -06001823static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1824 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001825{
1826 struct task_struct *tsk = req->task;
1827 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001828 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001829
Jens Axboe6200b0a2020-09-13 14:38:30 -06001830 if (tsk->flags & PF_EXITING)
1831 return -ESRCH;
1832
Jens Axboec2c4c832020-07-01 15:37:11 -06001833 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001834 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1835 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1836 * processing task_work. There's no reliable way to tell if TWA_RESUME
1837 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001838 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001839 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001840 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001841 notify = TWA_SIGNAL;
1842
1843 ret = task_work_add(tsk, cb, notify);
1844 if (!ret)
1845 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001846
Jens Axboec2c4c832020-07-01 15:37:11 -06001847 return ret;
1848}
1849
Jens Axboec40f6372020-06-25 15:39:59 -06001850static void __io_req_task_cancel(struct io_kiocb *req, int error)
1851{
1852 struct io_ring_ctx *ctx = req->ctx;
1853
1854 spin_lock_irq(&ctx->completion_lock);
1855 io_cqring_fill_event(req, error);
1856 io_commit_cqring(ctx);
1857 spin_unlock_irq(&ctx->completion_lock);
1858
1859 io_cqring_ev_posted(ctx);
1860 req_set_fail_links(req);
1861 io_double_put_req(req);
1862}
1863
1864static void io_req_task_cancel(struct callback_head *cb)
1865{
1866 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001867 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001868
1869 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001870 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001871}
1872
1873static void __io_req_task_submit(struct io_kiocb *req)
1874{
1875 struct io_ring_ctx *ctx = req->ctx;
1876
Jens Axboec40f6372020-06-25 15:39:59 -06001877 if (!__io_sq_thread_acquire_mm(ctx)) {
1878 mutex_lock(&ctx->uring_lock);
1879 __io_queue_sqe(req, NULL, NULL);
1880 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001881 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001882 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001883 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001884}
1885
Jens Axboec40f6372020-06-25 15:39:59 -06001886static void io_req_task_submit(struct callback_head *cb)
1887{
1888 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001889 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001890
1891 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001892 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001893}
1894
1895static void io_req_task_queue(struct io_kiocb *req)
1896{
Jens Axboec40f6372020-06-25 15:39:59 -06001897 int ret;
1898
1899 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001900 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001901
Jens Axboefd7d6de2020-08-23 11:00:37 -06001902 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001903 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001904 struct task_struct *tsk;
1905
Jens Axboec40f6372020-06-25 15:39:59 -06001906 init_task_work(&req->task_work, io_req_task_cancel);
1907 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001908 task_work_add(tsk, &req->task_work, 0);
1909 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001910 }
Jens Axboec40f6372020-06-25 15:39:59 -06001911}
1912
Pavel Begunkovc3524382020-06-28 12:52:32 +03001913static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001914{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001915 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001916
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001917 if (nxt)
1918 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001919}
1920
Jens Axboe9e645e112019-05-10 16:07:28 -06001921static void io_free_req(struct io_kiocb *req)
1922{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001923 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001924 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001925}
1926
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001927struct req_batch {
1928 void *reqs[IO_IOPOLL_BATCH];
1929 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001930
1931 struct task_struct *task;
1932 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001933};
1934
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001935static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001936{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001937 rb->to_free = 0;
1938 rb->task_refs = 0;
1939 rb->task = NULL;
1940}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001941
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001942static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1943 struct req_batch *rb)
1944{
1945 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1946 percpu_ref_put_many(&ctx->refs, rb->to_free);
1947 rb->to_free = 0;
1948}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001949
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001950static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1951 struct req_batch *rb)
1952{
1953 if (rb->to_free)
1954 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001955 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001956 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001957 put_task_struct_many(rb->task, rb->task_refs);
1958 rb->task = NULL;
1959 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001960}
1961
1962static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1963{
1964 if (unlikely(io_is_fallback_req(req))) {
1965 io_free_req(req);
1966 return;
1967 }
1968 if (req->flags & REQ_F_LINK_HEAD)
1969 io_queue_next(req);
1970
Jens Axboee3bc8e92020-09-24 08:45:57 -06001971 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001972 if (rb->task) {
1973 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001974 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06001975 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001976 rb->task = req->task;
1977 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001978 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001979 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001980
Jens Axboe51a4cc12020-08-10 10:55:56 -06001981 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001982 rb->reqs[rb->to_free++] = req;
1983 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1984 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001985}
1986
Jens Axboeba816ad2019-09-28 11:36:45 -06001987/*
1988 * Drop reference to request, return next in chain (if there is one) if this
1989 * was the last reference to this request.
1990 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001991static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001992{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001993 struct io_kiocb *nxt = NULL;
1994
Jens Axboe2a44f462020-02-25 13:25:41 -07001995 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001996 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001997 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001998 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001999 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002000}
2001
Jens Axboe2b188cc2019-01-07 10:46:33 -07002002static void io_put_req(struct io_kiocb *req)
2003{
Jens Axboedef596e2019-01-09 08:59:42 -07002004 if (refcount_dec_and_test(&req->refs))
2005 io_free_req(req);
2006}
2007
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002008static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002009{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002010 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002011
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002012 /*
2013 * A ref is owned by io-wq in which context we're. So, if that's the
2014 * last one, it's safe to steal next work. False negatives are Ok,
2015 * it just will be re-punted async in io_put_work()
2016 */
2017 if (refcount_read(&req->refs) != 1)
2018 return NULL;
2019
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002020 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002021 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002022}
2023
Jens Axboe978db572019-11-14 22:39:04 -07002024/*
2025 * Must only be used if we don't need to care about links, usually from
2026 * within the completion handling itself.
2027 */
2028static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002029{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002030 /* drop both submit and complete references */
2031 if (refcount_sub_and_test(2, &req->refs))
2032 __io_free_req(req);
2033}
2034
Jens Axboe978db572019-11-14 22:39:04 -07002035static void io_double_put_req(struct io_kiocb *req)
2036{
2037 /* drop both submit and complete references */
2038 if (refcount_sub_and_test(2, &req->refs))
2039 io_free_req(req);
2040}
2041
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002042static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002043{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002044 struct io_rings *rings = ctx->rings;
2045
Jens Axboead3eb2c2019-12-18 17:12:20 -07002046 if (test_bit(0, &ctx->cq_check_overflow)) {
2047 /*
2048 * noflush == true is from the waitqueue handler, just ensure
2049 * we wake up the task, and the next invocation will flush the
2050 * entries. We cannot safely to it from here.
2051 */
2052 if (noflush && !list_empty(&ctx->cq_overflow_list))
2053 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002054
Jens Axboee6c8aa92020-09-28 13:10:13 -06002055 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002056 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002057
Jens Axboea3a0e432019-08-20 11:03:11 -06002058 /* See comment at the top of this file */
2059 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002060 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002061}
2062
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002063static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2064{
2065 struct io_rings *rings = ctx->rings;
2066
2067 /* make sure SQ entry isn't read before tail */
2068 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2069}
2070
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002071static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002072{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002073 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002074
Jens Axboebcda7ba2020-02-23 16:42:51 -07002075 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2076 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002077 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002078 kfree(kbuf);
2079 return cflags;
2080}
2081
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002082static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2083{
2084 struct io_buffer *kbuf;
2085
2086 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2087 return io_put_kbuf(req, kbuf);
2088}
2089
Jens Axboe4c6e2772020-07-01 11:29:10 -06002090static inline bool io_run_task_work(void)
2091{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002092 /*
2093 * Not safe to run on exiting task, and the task_work handling will
2094 * not add work to such a task.
2095 */
2096 if (unlikely(current->flags & PF_EXITING))
2097 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002098 if (current->task_works) {
2099 __set_current_state(TASK_RUNNING);
2100 task_work_run();
2101 return true;
2102 }
2103
2104 return false;
2105}
2106
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002107static void io_iopoll_queue(struct list_head *again)
2108{
2109 struct io_kiocb *req;
2110
2111 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002112 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2113 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002114 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002115 } while (!list_empty(again));
2116}
2117
Jens Axboedef596e2019-01-09 08:59:42 -07002118/*
2119 * Find and free completed poll iocbs
2120 */
2121static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2122 struct list_head *done)
2123{
Jens Axboe8237e042019-12-28 10:48:22 -07002124 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002125 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002126 LIST_HEAD(again);
2127
2128 /* order with ->result store in io_complete_rw_iopoll() */
2129 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002130
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002131 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002132 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002133 int cflags = 0;
2134
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002135 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002136 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002137 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002138 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002139 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002140 continue;
2141 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002142 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002143
Jens Axboebcda7ba2020-02-23 16:42:51 -07002144 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002145 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002146
2147 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002148 (*nr_events)++;
2149
Pavel Begunkovc3524382020-06-28 12:52:32 +03002150 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002151 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002152 }
Jens Axboedef596e2019-01-09 08:59:42 -07002153
Jens Axboe09bb8392019-03-13 12:39:28 -06002154 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002155 if (ctx->flags & IORING_SETUP_SQPOLL)
2156 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002157 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002158
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002159 if (!list_empty(&again))
2160 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002161}
2162
Jens Axboedef596e2019-01-09 08:59:42 -07002163static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2164 long min)
2165{
2166 struct io_kiocb *req, *tmp;
2167 LIST_HEAD(done);
2168 bool spin;
2169 int ret;
2170
2171 /*
2172 * Only spin for completions if we don't have multiple devices hanging
2173 * off our complete list, and we're under the requested amount.
2174 */
2175 spin = !ctx->poll_multi_file && *nr_events < min;
2176
2177 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002178 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002179 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002180
2181 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002182 * Move completed and retryable entries to our local lists.
2183 * If we find a request that requires polling, break out
2184 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002185 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002186 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002187 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002188 continue;
2189 }
2190 if (!list_empty(&done))
2191 break;
2192
2193 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2194 if (ret < 0)
2195 break;
2196
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002197 /* iopoll may have completed current req */
2198 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002199 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002200
Jens Axboedef596e2019-01-09 08:59:42 -07002201 if (ret && spin)
2202 spin = false;
2203 ret = 0;
2204 }
2205
2206 if (!list_empty(&done))
2207 io_iopoll_complete(ctx, nr_events, &done);
2208
2209 return ret;
2210}
2211
2212/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002213 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002214 * non-spinning poll check - we'll still enter the driver poll loop, but only
2215 * as a non-spinning completion check.
2216 */
2217static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2218 long min)
2219{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002220 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002221 int ret;
2222
2223 ret = io_do_iopoll(ctx, nr_events, min);
2224 if (ret < 0)
2225 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002226 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002227 return 0;
2228 }
2229
2230 return 1;
2231}
2232
2233/*
2234 * We can't just wait for polled events to come to us, we have to actively
2235 * find and complete them.
2236 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002237static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002238{
2239 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2240 return;
2241
2242 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002243 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002244 unsigned int nr_events = 0;
2245
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002246 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002247
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002248 /* let it sleep and repeat later if can't complete a request */
2249 if (nr_events == 0)
2250 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002251 /*
2252 * Ensure we allow local-to-the-cpu processing to take place,
2253 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002254 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002255 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002256 if (need_resched()) {
2257 mutex_unlock(&ctx->uring_lock);
2258 cond_resched();
2259 mutex_lock(&ctx->uring_lock);
2260 }
Jens Axboedef596e2019-01-09 08:59:42 -07002261 }
2262 mutex_unlock(&ctx->uring_lock);
2263}
2264
Pavel Begunkov7668b922020-07-07 16:36:21 +03002265static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002266{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002267 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002268 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002269
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002270 /*
2271 * We disallow the app entering submit/complete with polling, but we
2272 * still need to lock the ring to prevent racing with polled issue
2273 * that got punted to a workqueue.
2274 */
2275 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002276 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002277 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002278 * Don't enter poll loop if we already have events pending.
2279 * If we do, we can potentially be spinning for commands that
2280 * already triggered a CQE (eg in error).
2281 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002282 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002283 break;
2284
2285 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002286 * If a submit got punted to a workqueue, we can have the
2287 * application entering polling for a command before it gets
2288 * issued. That app will hold the uring_lock for the duration
2289 * of the poll right here, so we need to take a breather every
2290 * now and then to ensure that the issue has a chance to add
2291 * the poll to the issued list. Otherwise we can spin here
2292 * forever, while the workqueue is stuck trying to acquire the
2293 * very same mutex.
2294 */
2295 if (!(++iters & 7)) {
2296 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002297 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002298 mutex_lock(&ctx->uring_lock);
2299 }
2300
Pavel Begunkov7668b922020-07-07 16:36:21 +03002301 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002302 if (ret <= 0)
2303 break;
2304 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002305 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002306
Jens Axboe500f9fb2019-08-19 12:15:59 -06002307 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002308 return ret;
2309}
2310
Jens Axboe491381ce2019-10-17 09:20:46 -06002311static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312{
Jens Axboe491381ce2019-10-17 09:20:46 -06002313 /*
2314 * Tell lockdep we inherited freeze protection from submission
2315 * thread.
2316 */
2317 if (req->flags & REQ_F_ISREG) {
2318 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319
Jens Axboe491381ce2019-10-17 09:20:46 -06002320 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002322 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323}
2324
Jens Axboea1d7c392020-06-22 11:09:46 -06002325static void io_complete_rw_common(struct kiocb *kiocb, long res,
2326 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002327{
Jens Axboe9adbd452019-12-20 08:45:55 -07002328 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002329 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002330
Jens Axboe491381ce2019-10-17 09:20:46 -06002331 if (kiocb->ki_flags & IOCB_WRITE)
2332 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002334 if (res != req->result)
2335 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002336 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002337 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002338 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002339}
2340
Jens Axboeb63534c2020-06-04 11:28:00 -06002341#ifdef CONFIG_BLOCK
2342static bool io_resubmit_prep(struct io_kiocb *req, int error)
2343{
2344 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2345 ssize_t ret = -ECANCELED;
2346 struct iov_iter iter;
2347 int rw;
2348
2349 if (error) {
2350 ret = error;
2351 goto end_req;
2352 }
2353
2354 switch (req->opcode) {
2355 case IORING_OP_READV:
2356 case IORING_OP_READ_FIXED:
2357 case IORING_OP_READ:
2358 rw = READ;
2359 break;
2360 case IORING_OP_WRITEV:
2361 case IORING_OP_WRITE_FIXED:
2362 case IORING_OP_WRITE:
2363 rw = WRITE;
2364 break;
2365 default:
2366 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2367 req->opcode);
2368 goto end_req;
2369 }
2370
Jens Axboe8f3d7492020-09-14 09:28:14 -06002371 if (!req->io) {
2372 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2373 if (ret < 0)
2374 goto end_req;
2375 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2376 if (!ret)
2377 return true;
2378 kfree(iovec);
2379 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002380 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002381 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002382end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002383 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002384 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002385 return false;
2386}
Jens Axboeb63534c2020-06-04 11:28:00 -06002387#endif
2388
2389static bool io_rw_reissue(struct io_kiocb *req, long res)
2390{
2391#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002392 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002393 int ret;
2394
Jens Axboe355afae2020-09-02 09:30:31 -06002395 if (!S_ISBLK(mode) && !S_ISREG(mode))
2396 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002397 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2398 return false;
2399
Jens Axboefdee9462020-08-27 16:46:24 -06002400 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002401
Jens Axboefdee9462020-08-27 16:46:24 -06002402 if (io_resubmit_prep(req, ret)) {
2403 refcount_inc(&req->refs);
2404 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002405 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002406 }
2407
Jens Axboeb63534c2020-06-04 11:28:00 -06002408#endif
2409 return false;
2410}
2411
Jens Axboea1d7c392020-06-22 11:09:46 -06002412static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2413 struct io_comp_state *cs)
2414{
2415 if (!io_rw_reissue(req, res))
2416 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002417}
2418
2419static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2420{
Jens Axboe9adbd452019-12-20 08:45:55 -07002421 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002422
Jens Axboea1d7c392020-06-22 11:09:46 -06002423 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002424}
2425
Jens Axboedef596e2019-01-09 08:59:42 -07002426static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2427{
Jens Axboe9adbd452019-12-20 08:45:55 -07002428 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002429
Jens Axboe491381ce2019-10-17 09:20:46 -06002430 if (kiocb->ki_flags & IOCB_WRITE)
2431 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002432
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002433 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002434 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002435
2436 WRITE_ONCE(req->result, res);
2437 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002438 smp_wmb();
2439 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002440}
2441
2442/*
2443 * After the iocb has been issued, it's safe to be found on the poll list.
2444 * Adding the kiocb to the list AFTER submission ensures that we don't
2445 * find it from a io_iopoll_getevents() thread before the issuer is done
2446 * accessing the kiocb cookie.
2447 */
2448static void io_iopoll_req_issued(struct io_kiocb *req)
2449{
2450 struct io_ring_ctx *ctx = req->ctx;
2451
2452 /*
2453 * Track whether we have multiple files in our lists. This will impact
2454 * how we do polling eventually, not spinning if we're on potentially
2455 * different devices.
2456 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002457 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002458 ctx->poll_multi_file = false;
2459 } else if (!ctx->poll_multi_file) {
2460 struct io_kiocb *list_req;
2461
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002462 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002463 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002464 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002465 ctx->poll_multi_file = true;
2466 }
2467
2468 /*
2469 * For fast devices, IO may have already completed. If it has, add
2470 * it to the front so we find it first.
2471 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002472 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002473 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002474 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002475 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002476
Jens Axboe534ca6d2020-09-02 13:52:19 -06002477 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2478 wq_has_sleeper(&ctx->sq_data->wait))
2479 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002480}
2481
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002482static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002483{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002484 if (state->has_refs)
2485 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002486 state->file = NULL;
2487}
2488
2489static inline void io_state_file_put(struct io_submit_state *state)
2490{
2491 if (state->file)
2492 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002493}
2494
2495/*
2496 * Get as many references to a file as we have IOs left in this submission,
2497 * assuming most submissions are for one file, or at least that each file
2498 * has more than one submission.
2499 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002500static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002501{
2502 if (!state)
2503 return fget(fd);
2504
2505 if (state->file) {
2506 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002507 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002508 state->ios_left--;
2509 return state->file;
2510 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002511 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002512 }
2513 state->file = fget_many(fd, state->ios_left);
2514 if (!state->file)
2515 return NULL;
2516
2517 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002518 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002519 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002520 return state->file;
2521}
2522
Jens Axboe4503b762020-06-01 10:00:27 -06002523static bool io_bdev_nowait(struct block_device *bdev)
2524{
2525#ifdef CONFIG_BLOCK
2526 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2527#else
2528 return true;
2529#endif
2530}
2531
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532/*
2533 * If we tracked the file through the SCM inflight mechanism, we could support
2534 * any file. For now, just ensure that anything potentially problematic is done
2535 * inline.
2536 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002537static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002538{
2539 umode_t mode = file_inode(file)->i_mode;
2540
Jens Axboe4503b762020-06-01 10:00:27 -06002541 if (S_ISBLK(mode)) {
2542 if (io_bdev_nowait(file->f_inode->i_bdev))
2543 return true;
2544 return false;
2545 }
2546 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002548 if (S_ISREG(mode)) {
2549 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2550 file->f_op != &io_uring_fops)
2551 return true;
2552 return false;
2553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554
Jens Axboec5b85622020-06-09 19:23:05 -06002555 /* any ->read/write should understand O_NONBLOCK */
2556 if (file->f_flags & O_NONBLOCK)
2557 return true;
2558
Jens Axboeaf197f52020-04-28 13:15:06 -06002559 if (!(file->f_mode & FMODE_NOWAIT))
2560 return false;
2561
2562 if (rw == READ)
2563 return file->f_op->read_iter != NULL;
2564
2565 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002566}
2567
Jens Axboe3529d8c2019-12-19 18:24:38 -07002568static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2569 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002570{
Jens Axboedef596e2019-01-09 08:59:42 -07002571 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002572 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002573 unsigned ioprio;
2574 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575
Jens Axboe491381ce2019-10-17 09:20:46 -06002576 if (S_ISREG(file_inode(req->file)->i_mode))
2577 req->flags |= REQ_F_ISREG;
2578
Jens Axboe2b188cc2019-01-07 10:46:33 -07002579 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002580 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2581 req->flags |= REQ_F_CUR_POS;
2582 kiocb->ki_pos = req->file->f_pos;
2583 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002585 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2586 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2587 if (unlikely(ret))
2588 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002589
2590 ioprio = READ_ONCE(sqe->ioprio);
2591 if (ioprio) {
2592 ret = ioprio_check_cap(ioprio);
2593 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002594 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002595
2596 kiocb->ki_ioprio = ioprio;
2597 } else
2598 kiocb->ki_ioprio = get_current_ioprio();
2599
Stefan Bühler8449eed2019-04-27 20:34:19 +02002600 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002601 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002602 req->flags |= REQ_F_NOWAIT;
2603
2604 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002606
Jens Axboedef596e2019-01-09 08:59:42 -07002607 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002608 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2609 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002610 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002611
Jens Axboedef596e2019-01-09 08:59:42 -07002612 kiocb->ki_flags |= IOCB_HIPRI;
2613 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002614 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002615 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002616 if (kiocb->ki_flags & IOCB_HIPRI)
2617 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002618 kiocb->ki_complete = io_complete_rw;
2619 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002620
Jens Axboe3529d8c2019-12-19 18:24:38 -07002621 req->rw.addr = READ_ONCE(sqe->addr);
2622 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002623 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002624 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625}
2626
2627static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2628{
2629 switch (ret) {
2630 case -EIOCBQUEUED:
2631 break;
2632 case -ERESTARTSYS:
2633 case -ERESTARTNOINTR:
2634 case -ERESTARTNOHAND:
2635 case -ERESTART_RESTARTBLOCK:
2636 /*
2637 * We can't just restart the syscall, since previously
2638 * submitted sqes may already be in progress. Just fail this
2639 * IO with EINTR.
2640 */
2641 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002642 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643 default:
2644 kiocb->ki_complete(kiocb, ret, 0);
2645 }
2646}
2647
Jens Axboea1d7c392020-06-22 11:09:46 -06002648static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2649 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002650{
Jens Axboeba042912019-12-25 16:33:42 -07002651 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2652
Jens Axboe227c0c92020-08-13 11:51:40 -06002653 /* add previously done IO, if any */
2654 if (req->io && req->io->rw.bytes_done > 0) {
2655 if (ret < 0)
2656 ret = req->io->rw.bytes_done;
2657 else
2658 ret += req->io->rw.bytes_done;
2659 }
2660
Jens Axboeba042912019-12-25 16:33:42 -07002661 if (req->flags & REQ_F_CUR_POS)
2662 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002663 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002664 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002665 else
2666 io_rw_done(kiocb, ret);
2667}
2668
Jens Axboe9adbd452019-12-20 08:45:55 -07002669static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002670 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002671{
Jens Axboe9adbd452019-12-20 08:45:55 -07002672 struct io_ring_ctx *ctx = req->ctx;
2673 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002674 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002675 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002676 size_t offset;
2677 u64 buf_addr;
2678
2679 /* attempt to use fixed buffers without having provided iovecs */
2680 if (unlikely(!ctx->user_bufs))
2681 return -EFAULT;
2682
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002683 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002684 if (unlikely(buf_index >= ctx->nr_user_bufs))
2685 return -EFAULT;
2686
2687 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2688 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002689 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002690
2691 /* overflow */
2692 if (buf_addr + len < buf_addr)
2693 return -EFAULT;
2694 /* not inside the mapped region */
2695 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2696 return -EFAULT;
2697
2698 /*
2699 * May not be a start of buffer, set size appropriately
2700 * and advance us to the beginning.
2701 */
2702 offset = buf_addr - imu->ubuf;
2703 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002704
2705 if (offset) {
2706 /*
2707 * Don't use iov_iter_advance() here, as it's really slow for
2708 * using the latter parts of a big fixed buffer - it iterates
2709 * over each segment manually. We can cheat a bit here, because
2710 * we know that:
2711 *
2712 * 1) it's a BVEC iter, we set it up
2713 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2714 * first and last bvec
2715 *
2716 * So just find our index, and adjust the iterator afterwards.
2717 * If the offset is within the first bvec (or the whole first
2718 * bvec, just use iov_iter_advance(). This makes it easier
2719 * since we can just skip the first segment, which may not
2720 * be PAGE_SIZE aligned.
2721 */
2722 const struct bio_vec *bvec = imu->bvec;
2723
2724 if (offset <= bvec->bv_len) {
2725 iov_iter_advance(iter, offset);
2726 } else {
2727 unsigned long seg_skip;
2728
2729 /* skip first vec */
2730 offset -= bvec->bv_len;
2731 seg_skip = 1 + (offset >> PAGE_SHIFT);
2732
2733 iter->bvec = bvec + seg_skip;
2734 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002735 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002736 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002737 }
2738 }
2739
Jens Axboe5e559562019-11-13 16:12:46 -07002740 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002741}
2742
Jens Axboebcda7ba2020-02-23 16:42:51 -07002743static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2744{
2745 if (needs_lock)
2746 mutex_unlock(&ctx->uring_lock);
2747}
2748
2749static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2750{
2751 /*
2752 * "Normal" inline submissions always hold the uring_lock, since we
2753 * grab it from the system call. Same is true for the SQPOLL offload.
2754 * The only exception is when we've detached the request and issue it
2755 * from an async worker thread, grab the lock for that case.
2756 */
2757 if (needs_lock)
2758 mutex_lock(&ctx->uring_lock);
2759}
2760
2761static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2762 int bgid, struct io_buffer *kbuf,
2763 bool needs_lock)
2764{
2765 struct io_buffer *head;
2766
2767 if (req->flags & REQ_F_BUFFER_SELECTED)
2768 return kbuf;
2769
2770 io_ring_submit_lock(req->ctx, needs_lock);
2771
2772 lockdep_assert_held(&req->ctx->uring_lock);
2773
2774 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2775 if (head) {
2776 if (!list_empty(&head->list)) {
2777 kbuf = list_last_entry(&head->list, struct io_buffer,
2778 list);
2779 list_del(&kbuf->list);
2780 } else {
2781 kbuf = head;
2782 idr_remove(&req->ctx->io_buffer_idr, bgid);
2783 }
2784 if (*len > kbuf->len)
2785 *len = kbuf->len;
2786 } else {
2787 kbuf = ERR_PTR(-ENOBUFS);
2788 }
2789
2790 io_ring_submit_unlock(req->ctx, needs_lock);
2791
2792 return kbuf;
2793}
2794
Jens Axboe4d954c22020-02-27 07:31:19 -07002795static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2796 bool needs_lock)
2797{
2798 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002799 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002800
2801 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002802 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002803 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2804 if (IS_ERR(kbuf))
2805 return kbuf;
2806 req->rw.addr = (u64) (unsigned long) kbuf;
2807 req->flags |= REQ_F_BUFFER_SELECTED;
2808 return u64_to_user_ptr(kbuf->addr);
2809}
2810
2811#ifdef CONFIG_COMPAT
2812static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2813 bool needs_lock)
2814{
2815 struct compat_iovec __user *uiov;
2816 compat_ssize_t clen;
2817 void __user *buf;
2818 ssize_t len;
2819
2820 uiov = u64_to_user_ptr(req->rw.addr);
2821 if (!access_ok(uiov, sizeof(*uiov)))
2822 return -EFAULT;
2823 if (__get_user(clen, &uiov->iov_len))
2824 return -EFAULT;
2825 if (clen < 0)
2826 return -EINVAL;
2827
2828 len = clen;
2829 buf = io_rw_buffer_select(req, &len, needs_lock);
2830 if (IS_ERR(buf))
2831 return PTR_ERR(buf);
2832 iov[0].iov_base = buf;
2833 iov[0].iov_len = (compat_size_t) len;
2834 return 0;
2835}
2836#endif
2837
2838static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2839 bool needs_lock)
2840{
2841 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2842 void __user *buf;
2843 ssize_t len;
2844
2845 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2846 return -EFAULT;
2847
2848 len = iov[0].iov_len;
2849 if (len < 0)
2850 return -EINVAL;
2851 buf = io_rw_buffer_select(req, &len, needs_lock);
2852 if (IS_ERR(buf))
2853 return PTR_ERR(buf);
2854 iov[0].iov_base = buf;
2855 iov[0].iov_len = len;
2856 return 0;
2857}
2858
2859static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2860 bool needs_lock)
2861{
Jens Axboedddb3e22020-06-04 11:27:01 -06002862 if (req->flags & REQ_F_BUFFER_SELECTED) {
2863 struct io_buffer *kbuf;
2864
2865 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2866 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2867 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002868 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002869 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002870 if (!req->rw.len)
2871 return 0;
2872 else if (req->rw.len > 1)
2873 return -EINVAL;
2874
2875#ifdef CONFIG_COMPAT
2876 if (req->ctx->compat)
2877 return io_compat_import(req, iov, needs_lock);
2878#endif
2879
2880 return __io_iov_buffer_select(req, iov, needs_lock);
2881}
2882
Jens Axboe8452fd02020-08-18 13:58:33 -07002883static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2884 struct iovec **iovec, struct iov_iter *iter,
2885 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002886{
Jens Axboe9adbd452019-12-20 08:45:55 -07002887 void __user *buf = u64_to_user_ptr(req->rw.addr);
2888 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002889 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002890 u8 opcode;
2891
Jens Axboed625c6e2019-12-17 19:53:05 -07002892 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002893 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002894 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002895 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002896 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002897
Jens Axboebcda7ba2020-02-23 16:42:51 -07002898 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002899 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002900 return -EINVAL;
2901
Jens Axboe3a6820f2019-12-22 15:19:35 -07002902 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002903 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002904 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002905 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002906 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002907 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002908 }
2909
Jens Axboe3a6820f2019-12-22 15:19:35 -07002910 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2911 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002912 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002913 }
2914
Jens Axboe4d954c22020-02-27 07:31:19 -07002915 if (req->flags & REQ_F_BUFFER_SELECT) {
2916 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002917 if (!ret) {
2918 ret = (*iovec)->iov_len;
2919 iov_iter_init(iter, rw, *iovec, 1, ret);
2920 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002921 *iovec = NULL;
2922 return ret;
2923 }
2924
Jens Axboe2b188cc2019-01-07 10:46:33 -07002925#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002926 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002927 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2928 iovec, iter);
2929#endif
2930
2931 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2932}
2933
Jens Axboe8452fd02020-08-18 13:58:33 -07002934static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2935 struct iovec **iovec, struct iov_iter *iter,
2936 bool needs_lock)
2937{
2938 if (!req->io)
2939 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2940 *iovec = NULL;
2941 return iov_iter_count(&req->io->rw.iter);
2942}
2943
Jens Axboe0fef9482020-08-26 10:36:20 -06002944static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2945{
2946 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2947}
2948
Jens Axboe32960612019-09-23 11:05:34 -06002949/*
2950 * For files that don't have ->read_iter() and ->write_iter(), handle them
2951 * by looping over ->read() or ->write() manually.
2952 */
2953static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2954 struct iov_iter *iter)
2955{
2956 ssize_t ret = 0;
2957
2958 /*
2959 * Don't support polled IO through this interface, and we can't
2960 * support non-blocking either. For the latter, this just causes
2961 * the kiocb to be handled from an async context.
2962 */
2963 if (kiocb->ki_flags & IOCB_HIPRI)
2964 return -EOPNOTSUPP;
2965 if (kiocb->ki_flags & IOCB_NOWAIT)
2966 return -EAGAIN;
2967
2968 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002969 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002970 ssize_t nr;
2971
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002972 if (!iov_iter_is_bvec(iter)) {
2973 iovec = iov_iter_iovec(iter);
2974 } else {
2975 /* fixed buffers import bvec */
2976 iovec.iov_base = kmap(iter->bvec->bv_page)
2977 + iter->iov_offset;
2978 iovec.iov_len = min(iter->count,
2979 iter->bvec->bv_len - iter->iov_offset);
2980 }
2981
Jens Axboe32960612019-09-23 11:05:34 -06002982 if (rw == READ) {
2983 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002984 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002985 } else {
2986 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002987 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002988 }
2989
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002990 if (iov_iter_is_bvec(iter))
2991 kunmap(iter->bvec->bv_page);
2992
Jens Axboe32960612019-09-23 11:05:34 -06002993 if (nr < 0) {
2994 if (!ret)
2995 ret = nr;
2996 break;
2997 }
2998 ret += nr;
2999 if (nr != iovec.iov_len)
3000 break;
3001 iov_iter_advance(iter, nr);
3002 }
3003
3004 return ret;
3005}
3006
Jens Axboeff6165b2020-08-13 09:47:43 -06003007static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3008 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003009{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003010 struct io_async_rw *rw = &req->io->rw;
3011
Jens Axboeff6165b2020-08-13 09:47:43 -06003012 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003013 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003014 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003015 /* can only be fixed buffers, no need to do anything */
3016 if (iter->type == ITER_BVEC)
3017 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003018 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003019 unsigned iov_off = 0;
3020
3021 rw->iter.iov = rw->fast_iov;
3022 if (iter->iov != fast_iov) {
3023 iov_off = iter->iov - fast_iov;
3024 rw->iter.iov += iov_off;
3025 }
3026 if (rw->fast_iov != fast_iov)
3027 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003028 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003029 } else {
3030 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003031 }
3032}
3033
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003034static inline int __io_alloc_async_ctx(struct io_kiocb *req)
3035{
3036 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
3037 return req->io == NULL;
3038}
3039
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003040static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003041{
Jens Axboed3656342019-12-18 09:50:26 -07003042 if (!io_op_defs[req->opcode].async_ctx)
3043 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003044
3045 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003046}
3047
Jens Axboeff6165b2020-08-13 09:47:43 -06003048static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3049 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003050 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003051{
Jens Axboe227c0c92020-08-13 11:51:40 -06003052 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003053 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003054 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003055 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003056 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003057
Jens Axboeff6165b2020-08-13 09:47:43 -06003058 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003059 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003060 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003061}
3062
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003063static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3064 bool force_nonblock)
3065{
Jens Axboeff6165b2020-08-13 09:47:43 -06003066 struct io_async_rw *iorw = &req->io->rw;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003067 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003068 ssize_t ret;
3069
Jens Axboec183edf2020-09-04 22:36:52 -06003070 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003071 if (unlikely(ret < 0))
3072 return ret;
3073
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003074 io_req_map_rw(req, iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003075 return 0;
3076}
3077
Jens Axboe3529d8c2019-12-19 18:24:38 -07003078static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3079 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003080{
3081 ssize_t ret;
3082
Jens Axboe3529d8c2019-12-19 18:24:38 -07003083 ret = io_prep_rw(req, sqe, force_nonblock);
3084 if (ret)
3085 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003086
Jens Axboe3529d8c2019-12-19 18:24:38 -07003087 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3088 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003089
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003090 /* either don't need iovec imported or already have it */
3091 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003092 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003093 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003094}
3095
Jens Axboec1dd91d2020-08-03 16:43:59 -06003096/*
3097 * This is our waitqueue callback handler, registered through lock_page_async()
3098 * when we initially tried to do the IO with the iocb armed our waitqueue.
3099 * This gets called when the page is unlocked, and we generally expect that to
3100 * happen when the page IO is completed and the page is now uptodate. This will
3101 * queue a task_work based retry of the operation, attempting to copy the data
3102 * again. If the latter fails because the page was NOT uptodate, then we will
3103 * do a thread based blocking retry of the operation. That's the unexpected
3104 * slow path.
3105 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003106static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3107 int sync, void *arg)
3108{
3109 struct wait_page_queue *wpq;
3110 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003111 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003112 int ret;
3113
3114 wpq = container_of(wait, struct wait_page_queue, wait);
3115
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003116 if (!wake_page_match(wpq, key))
3117 return 0;
3118
Hao Xuc8d317a2020-09-29 20:00:45 +08003119 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003120 list_del_init(&wait->entry);
3121
Pavel Begunkove7375122020-07-12 20:42:04 +03003122 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003123 percpu_ref_get(&req->ctx->refs);
3124
Jens Axboebcf5a062020-05-22 09:24:42 -06003125 /* submit ref gets dropped, acquire a new one */
3126 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003127 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003128 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003129 struct task_struct *tsk;
3130
Jens Axboebcf5a062020-05-22 09:24:42 -06003131 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003132 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003133 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003134 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003135 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003136 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003137 return 1;
3138}
3139
Jens Axboec1dd91d2020-08-03 16:43:59 -06003140/*
3141 * This controls whether a given IO request should be armed for async page
3142 * based retry. If we return false here, the request is handed to the async
3143 * worker threads for retry. If we're doing buffered reads on a regular file,
3144 * we prepare a private wait_page_queue entry and retry the operation. This
3145 * will either succeed because the page is now uptodate and unlocked, or it
3146 * will register a callback when the page is unlocked at IO completion. Through
3147 * that callback, io_uring uses task_work to setup a retry of the operation.
3148 * That retry will attempt the buffered read again. The retry will generally
3149 * succeed, or in rare cases where it fails, we then fall back to using the
3150 * async worker threads for a blocking retry.
3151 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003152static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003153{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003154 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003155 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003156
3157 /* never retry for NOWAIT, we just complete with -EAGAIN */
3158 if (req->flags & REQ_F_NOWAIT)
3159 return false;
3160
Jens Axboe227c0c92020-08-13 11:51:40 -06003161 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003162 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003163 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003164
Jens Axboebcf5a062020-05-22 09:24:42 -06003165 /*
3166 * just use poll if we can, and don't attempt if the fs doesn't
3167 * support callback based unlocks
3168 */
3169 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3170 return false;
3171
Jens Axboe3b2a4432020-08-16 10:58:43 -07003172 wait->wait.func = io_async_buf_func;
3173 wait->wait.private = req;
3174 wait->wait.flags = 0;
3175 INIT_LIST_HEAD(&wait->wait.entry);
3176 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003177 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003178 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003179 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003180}
3181
3182static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3183{
3184 if (req->file->f_op->read_iter)
3185 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003186 else if (req->file->f_op->read)
3187 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3188 else
3189 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003190}
3191
Jens Axboea1d7c392020-06-22 11:09:46 -06003192static int io_read(struct io_kiocb *req, bool force_nonblock,
3193 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003194{
3195 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003196 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003197 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003198 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003199 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003200 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003201
Jens Axboeff6165b2020-08-13 09:47:43 -06003202 if (req->io)
3203 iter = &req->io->rw.iter;
3204
3205 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003206 if (ret < 0)
3207 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003208 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003209 io_size = ret;
3210 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003211 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003212
Jens Axboefd6c2e42019-12-18 12:19:41 -07003213 /* Ensure we clear previously set non-block flag */
3214 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003215 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003216
Pavel Begunkov24c74672020-06-21 13:09:51 +03003217 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003218 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3219 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003220 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003221
Jens Axboe0fef9482020-08-26 10:36:20 -06003222 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003223 if (unlikely(ret))
3224 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003225
Jens Axboe227c0c92020-08-13 11:51:40 -06003226 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003227
Jens Axboe227c0c92020-08-13 11:51:40 -06003228 if (!ret) {
3229 goto done;
3230 } else if (ret == -EIOCBQUEUED) {
3231 ret = 0;
3232 goto out_free;
3233 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003234 /* IOPOLL retry should happen for io-wq threads */
3235 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003236 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003237 /* no retry on NONBLOCK marked file */
3238 if (req->file->f_flags & O_NONBLOCK)
3239 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003240 /* some cases will consume bytes even on error returns */
3241 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003242 ret = 0;
3243 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003244 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003245 /* make sure -ERESTARTSYS -> -EINTR is done */
3246 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003247 }
3248
3249 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003250 if (!iov_iter_count(iter) || !force_nonblock ||
3251 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003252 goto done;
3253
3254 io_size -= ret;
3255copy_iov:
3256 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3257 if (ret2) {
3258 ret = ret2;
3259 goto out_free;
3260 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003261 if (no_async)
3262 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003263 /* it's copied and will be cleaned with ->io */
3264 iovec = NULL;
3265 /* now use our persistent iterator, if we aren't already */
3266 iter = &req->io->rw.iter;
3267retry:
3268 req->io->rw.bytes_done += ret;
3269 /* if we can retry, do so with the callbacks armed */
3270 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003271 kiocb->ki_flags &= ~IOCB_WAITQ;
3272 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003273 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003274
3275 /*
3276 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3277 * get -EIOCBQUEUED, then we'll get a notification when the desired
3278 * page gets unlocked. We can also get a partial read here, and if we
3279 * do, then just retry at the new offset.
3280 */
3281 ret = io_iter_do_read(req, iter);
3282 if (ret == -EIOCBQUEUED) {
3283 ret = 0;
3284 goto out_free;
3285 } else if (ret > 0 && ret < io_size) {
3286 /* we got some bytes, but not all. retry. */
3287 goto retry;
3288 }
3289done:
3290 kiocb_done(kiocb, ret, cs);
3291 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003292out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003293 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003294 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003295 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003296 return ret;
3297}
3298
Jens Axboe3529d8c2019-12-19 18:24:38 -07003299static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3300 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003301{
3302 ssize_t ret;
3303
Jens Axboe3529d8c2019-12-19 18:24:38 -07003304 ret = io_prep_rw(req, sqe, force_nonblock);
3305 if (ret)
3306 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003307
Jens Axboe3529d8c2019-12-19 18:24:38 -07003308 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3309 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003310
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003311 /* either don't need iovec imported or already have it */
3312 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003313 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003314 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003315}
3316
Jens Axboea1d7c392020-06-22 11:09:46 -06003317static int io_write(struct io_kiocb *req, bool force_nonblock,
3318 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003319{
3320 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003321 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003322 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003323 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003324 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003325
Jens Axboeff6165b2020-08-13 09:47:43 -06003326 if (req->io)
3327 iter = &req->io->rw.iter;
3328
3329 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003330 if (ret < 0)
3331 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003332 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003333 io_size = ret;
3334 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003335
Jens Axboefd6c2e42019-12-18 12:19:41 -07003336 /* Ensure we clear previously set non-block flag */
3337 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003338 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003339
Pavel Begunkov24c74672020-06-21 13:09:51 +03003340 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003341 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003342 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003343
Jens Axboe10d59342019-12-09 20:16:22 -07003344 /* file path doesn't support NOWAIT for non-direct_IO */
3345 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3346 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003347 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003348
Jens Axboe0fef9482020-08-26 10:36:20 -06003349 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003350 if (unlikely(ret))
3351 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003352
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003353 /*
3354 * Open-code file_start_write here to grab freeze protection,
3355 * which will be released by another thread in
3356 * io_complete_rw(). Fool lockdep by telling it the lock got
3357 * released so that it doesn't complain about the held lock when
3358 * we return to userspace.
3359 */
3360 if (req->flags & REQ_F_ISREG) {
3361 __sb_start_write(file_inode(req->file)->i_sb,
3362 SB_FREEZE_WRITE, true);
3363 __sb_writers_release(file_inode(req->file)->i_sb,
3364 SB_FREEZE_WRITE);
3365 }
3366 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003367
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003368 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003369 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003370 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003371 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003372 else
3373 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003374
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003375 /*
3376 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3377 * retry them without IOCB_NOWAIT.
3378 */
3379 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3380 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003381 /* no retry on NONBLOCK marked file */
3382 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3383 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003384 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003385 /* IOPOLL retry should happen for io-wq threads */
3386 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3387 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003388done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003389 kiocb_done(kiocb, ret2, cs);
3390 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003391copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003392 /* some cases will consume bytes even on error returns */
3393 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003394 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003395 if (!ret)
3396 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003397 }
Jens Axboe31b51512019-01-18 22:56:34 -07003398out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003399 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003400 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003401 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003402 return ret;
3403}
3404
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003405static int __io_splice_prep(struct io_kiocb *req,
3406 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003407{
3408 struct io_splice* sp = &req->splice;
3409 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3410 int ret;
3411
3412 if (req->flags & REQ_F_NEED_CLEANUP)
3413 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003414 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3415 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003416
3417 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003418 sp->len = READ_ONCE(sqe->len);
3419 sp->flags = READ_ONCE(sqe->splice_flags);
3420
3421 if (unlikely(sp->flags & ~valid_flags))
3422 return -EINVAL;
3423
3424 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3425 (sp->flags & SPLICE_F_FD_IN_FIXED));
3426 if (ret)
3427 return ret;
3428 req->flags |= REQ_F_NEED_CLEANUP;
3429
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003430 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3431 /*
3432 * Splice operation will be punted aync, and here need to
3433 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3434 */
3435 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003436 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003437 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003438
3439 return 0;
3440}
3441
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003442static int io_tee_prep(struct io_kiocb *req,
3443 const struct io_uring_sqe *sqe)
3444{
3445 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3446 return -EINVAL;
3447 return __io_splice_prep(req, sqe);
3448}
3449
3450static int io_tee(struct io_kiocb *req, bool force_nonblock)
3451{
3452 struct io_splice *sp = &req->splice;
3453 struct file *in = sp->file_in;
3454 struct file *out = sp->file_out;
3455 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3456 long ret = 0;
3457
3458 if (force_nonblock)
3459 return -EAGAIN;
3460 if (sp->len)
3461 ret = do_tee(in, out, sp->len, flags);
3462
3463 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3464 req->flags &= ~REQ_F_NEED_CLEANUP;
3465
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003466 if (ret != sp->len)
3467 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003468 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003469 return 0;
3470}
3471
3472static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3473{
3474 struct io_splice* sp = &req->splice;
3475
3476 sp->off_in = READ_ONCE(sqe->splice_off_in);
3477 sp->off_out = READ_ONCE(sqe->off);
3478 return __io_splice_prep(req, sqe);
3479}
3480
Pavel Begunkov014db002020-03-03 21:33:12 +03003481static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003482{
3483 struct io_splice *sp = &req->splice;
3484 struct file *in = sp->file_in;
3485 struct file *out = sp->file_out;
3486 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3487 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003488 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003489
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003490 if (force_nonblock)
3491 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003492
3493 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3494 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003495
Jens Axboe948a7742020-05-17 14:21:38 -06003496 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003497 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003498
3499 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3500 req->flags &= ~REQ_F_NEED_CLEANUP;
3501
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003502 if (ret != sp->len)
3503 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003504 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003505 return 0;
3506}
3507
Jens Axboe2b188cc2019-01-07 10:46:33 -07003508/*
3509 * IORING_OP_NOP just posts a completion event, nothing else.
3510 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003511static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003512{
3513 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003514
Jens Axboedef596e2019-01-09 08:59:42 -07003515 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3516 return -EINVAL;
3517
Jens Axboe229a7b62020-06-22 10:13:11 -06003518 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003519 return 0;
3520}
3521
Jens Axboe3529d8c2019-12-19 18:24:38 -07003522static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003523{
Jens Axboe6b063142019-01-10 22:13:58 -07003524 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003525
Jens Axboe09bb8392019-03-13 12:39:28 -06003526 if (!req->file)
3527 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003528
Jens Axboe6b063142019-01-10 22:13:58 -07003529 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003530 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003531 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003532 return -EINVAL;
3533
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003534 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3535 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3536 return -EINVAL;
3537
3538 req->sync.off = READ_ONCE(sqe->off);
3539 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003540 return 0;
3541}
3542
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003543static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003544{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003545 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003546 int ret;
3547
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003548 /* fsync always requires a blocking context */
3549 if (force_nonblock)
3550 return -EAGAIN;
3551
Jens Axboe9adbd452019-12-20 08:45:55 -07003552 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003553 end > 0 ? end : LLONG_MAX,
3554 req->sync.flags & IORING_FSYNC_DATASYNC);
3555 if (ret < 0)
3556 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003557 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003558 return 0;
3559}
3560
Jens Axboed63d1b52019-12-10 10:38:56 -07003561static int io_fallocate_prep(struct io_kiocb *req,
3562 const struct io_uring_sqe *sqe)
3563{
3564 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3565 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003566 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3567 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003568
3569 req->sync.off = READ_ONCE(sqe->off);
3570 req->sync.len = READ_ONCE(sqe->addr);
3571 req->sync.mode = READ_ONCE(sqe->len);
3572 return 0;
3573}
3574
Pavel Begunkov014db002020-03-03 21:33:12 +03003575static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003576{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003577 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003578
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003579 /* fallocate always requiring blocking context */
3580 if (force_nonblock)
3581 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003582 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3583 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003584 if (ret < 0)
3585 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003586 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003587 return 0;
3588}
3589
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003590static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003591{
Jens Axboef8748882020-01-08 17:47:02 -07003592 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003593 int ret;
3594
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003595 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003596 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003597 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003598 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003599
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003600 /* open.how should be already initialised */
3601 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003602 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003603
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003604 req->open.dfd = READ_ONCE(sqe->fd);
3605 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003606 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003607 if (IS_ERR(req->open.filename)) {
3608 ret = PTR_ERR(req->open.filename);
3609 req->open.filename = NULL;
3610 return ret;
3611 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003612 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003613 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003614 return 0;
3615}
3616
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003617static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3618{
3619 u64 flags, mode;
3620
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003621 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3622 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003623 if (req->flags & REQ_F_NEED_CLEANUP)
3624 return 0;
3625 mode = READ_ONCE(sqe->len);
3626 flags = READ_ONCE(sqe->open_flags);
3627 req->open.how = build_open_how(flags, mode);
3628 return __io_openat_prep(req, sqe);
3629}
3630
Jens Axboecebdb982020-01-08 17:59:24 -07003631static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3632{
3633 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003634 size_t len;
3635 int ret;
3636
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003637 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3638 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003639 if (req->flags & REQ_F_NEED_CLEANUP)
3640 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003641 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3642 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003643 if (len < OPEN_HOW_SIZE_VER0)
3644 return -EINVAL;
3645
3646 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3647 len);
3648 if (ret)
3649 return ret;
3650
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003651 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003652}
3653
Pavel Begunkov014db002020-03-03 21:33:12 +03003654static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003655{
3656 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003657 struct file *file;
3658 int ret;
3659
Jens Axboef86cd202020-01-29 13:46:44 -07003660 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003661 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003662
Jens Axboecebdb982020-01-08 17:59:24 -07003663 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003664 if (ret)
3665 goto err;
3666
Jens Axboe4022e7a2020-03-19 19:23:18 -06003667 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003668 if (ret < 0)
3669 goto err;
3670
3671 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3672 if (IS_ERR(file)) {
3673 put_unused_fd(ret);
3674 ret = PTR_ERR(file);
3675 } else {
3676 fsnotify_open(file);
3677 fd_install(ret, file);
3678 }
3679err:
3680 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003681 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003682 if (ret < 0)
3683 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003684 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003685 return 0;
3686}
3687
Pavel Begunkov014db002020-03-03 21:33:12 +03003688static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003689{
Pavel Begunkov014db002020-03-03 21:33:12 +03003690 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003691}
3692
Jens Axboe067524e2020-03-02 16:32:28 -07003693static int io_remove_buffers_prep(struct io_kiocb *req,
3694 const struct io_uring_sqe *sqe)
3695{
3696 struct io_provide_buf *p = &req->pbuf;
3697 u64 tmp;
3698
3699 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3700 return -EINVAL;
3701
3702 tmp = READ_ONCE(sqe->fd);
3703 if (!tmp || tmp > USHRT_MAX)
3704 return -EINVAL;
3705
3706 memset(p, 0, sizeof(*p));
3707 p->nbufs = tmp;
3708 p->bgid = READ_ONCE(sqe->buf_group);
3709 return 0;
3710}
3711
3712static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3713 int bgid, unsigned nbufs)
3714{
3715 unsigned i = 0;
3716
3717 /* shouldn't happen */
3718 if (!nbufs)
3719 return 0;
3720
3721 /* the head kbuf is the list itself */
3722 while (!list_empty(&buf->list)) {
3723 struct io_buffer *nxt;
3724
3725 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3726 list_del(&nxt->list);
3727 kfree(nxt);
3728 if (++i == nbufs)
3729 return i;
3730 }
3731 i++;
3732 kfree(buf);
3733 idr_remove(&ctx->io_buffer_idr, bgid);
3734
3735 return i;
3736}
3737
Jens Axboe229a7b62020-06-22 10:13:11 -06003738static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3739 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003740{
3741 struct io_provide_buf *p = &req->pbuf;
3742 struct io_ring_ctx *ctx = req->ctx;
3743 struct io_buffer *head;
3744 int ret = 0;
3745
3746 io_ring_submit_lock(ctx, !force_nonblock);
3747
3748 lockdep_assert_held(&ctx->uring_lock);
3749
3750 ret = -ENOENT;
3751 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3752 if (head)
3753 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3754
3755 io_ring_submit_lock(ctx, !force_nonblock);
3756 if (ret < 0)
3757 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003758 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003759 return 0;
3760}
3761
Jens Axboeddf0322d2020-02-23 16:41:33 -07003762static int io_provide_buffers_prep(struct io_kiocb *req,
3763 const struct io_uring_sqe *sqe)
3764{
3765 struct io_provide_buf *p = &req->pbuf;
3766 u64 tmp;
3767
3768 if (sqe->ioprio || sqe->rw_flags)
3769 return -EINVAL;
3770
3771 tmp = READ_ONCE(sqe->fd);
3772 if (!tmp || tmp > USHRT_MAX)
3773 return -E2BIG;
3774 p->nbufs = tmp;
3775 p->addr = READ_ONCE(sqe->addr);
3776 p->len = READ_ONCE(sqe->len);
3777
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003778 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003779 return -EFAULT;
3780
3781 p->bgid = READ_ONCE(sqe->buf_group);
3782 tmp = READ_ONCE(sqe->off);
3783 if (tmp > USHRT_MAX)
3784 return -E2BIG;
3785 p->bid = tmp;
3786 return 0;
3787}
3788
3789static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3790{
3791 struct io_buffer *buf;
3792 u64 addr = pbuf->addr;
3793 int i, bid = pbuf->bid;
3794
3795 for (i = 0; i < pbuf->nbufs; i++) {
3796 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3797 if (!buf)
3798 break;
3799
3800 buf->addr = addr;
3801 buf->len = pbuf->len;
3802 buf->bid = bid;
3803 addr += pbuf->len;
3804 bid++;
3805 if (!*head) {
3806 INIT_LIST_HEAD(&buf->list);
3807 *head = buf;
3808 } else {
3809 list_add_tail(&buf->list, &(*head)->list);
3810 }
3811 }
3812
3813 return i ? i : -ENOMEM;
3814}
3815
Jens Axboe229a7b62020-06-22 10:13:11 -06003816static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3817 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003818{
3819 struct io_provide_buf *p = &req->pbuf;
3820 struct io_ring_ctx *ctx = req->ctx;
3821 struct io_buffer *head, *list;
3822 int ret = 0;
3823
3824 io_ring_submit_lock(ctx, !force_nonblock);
3825
3826 lockdep_assert_held(&ctx->uring_lock);
3827
3828 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3829
3830 ret = io_add_buffers(p, &head);
3831 if (ret < 0)
3832 goto out;
3833
3834 if (!list) {
3835 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3836 GFP_KERNEL);
3837 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003838 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003839 goto out;
3840 }
3841 }
3842out:
3843 io_ring_submit_unlock(ctx, !force_nonblock);
3844 if (ret < 0)
3845 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003846 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003847 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003848}
3849
Jens Axboe3e4827b2020-01-08 15:18:09 -07003850static int io_epoll_ctl_prep(struct io_kiocb *req,
3851 const struct io_uring_sqe *sqe)
3852{
3853#if defined(CONFIG_EPOLL)
3854 if (sqe->ioprio || sqe->buf_index)
3855 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003856 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003857 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003858
3859 req->epoll.epfd = READ_ONCE(sqe->fd);
3860 req->epoll.op = READ_ONCE(sqe->len);
3861 req->epoll.fd = READ_ONCE(sqe->off);
3862
3863 if (ep_op_has_event(req->epoll.op)) {
3864 struct epoll_event __user *ev;
3865
3866 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3867 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3868 return -EFAULT;
3869 }
3870
3871 return 0;
3872#else
3873 return -EOPNOTSUPP;
3874#endif
3875}
3876
Jens Axboe229a7b62020-06-22 10:13:11 -06003877static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3878 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003879{
3880#if defined(CONFIG_EPOLL)
3881 struct io_epoll *ie = &req->epoll;
3882 int ret;
3883
3884 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3885 if (force_nonblock && ret == -EAGAIN)
3886 return -EAGAIN;
3887
3888 if (ret < 0)
3889 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003890 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003891 return 0;
3892#else
3893 return -EOPNOTSUPP;
3894#endif
3895}
3896
Jens Axboec1ca7572019-12-25 22:18:28 -07003897static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3898{
3899#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3900 if (sqe->ioprio || sqe->buf_index || sqe->off)
3901 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003902 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3903 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003904
3905 req->madvise.addr = READ_ONCE(sqe->addr);
3906 req->madvise.len = READ_ONCE(sqe->len);
3907 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3908 return 0;
3909#else
3910 return -EOPNOTSUPP;
3911#endif
3912}
3913
Pavel Begunkov014db002020-03-03 21:33:12 +03003914static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003915{
3916#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3917 struct io_madvise *ma = &req->madvise;
3918 int ret;
3919
3920 if (force_nonblock)
3921 return -EAGAIN;
3922
3923 ret = do_madvise(ma->addr, ma->len, ma->advice);
3924 if (ret < 0)
3925 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003926 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003927 return 0;
3928#else
3929 return -EOPNOTSUPP;
3930#endif
3931}
3932
Jens Axboe4840e412019-12-25 22:03:45 -07003933static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3934{
3935 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3936 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003937 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3938 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003939
3940 req->fadvise.offset = READ_ONCE(sqe->off);
3941 req->fadvise.len = READ_ONCE(sqe->len);
3942 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3943 return 0;
3944}
3945
Pavel Begunkov014db002020-03-03 21:33:12 +03003946static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003947{
3948 struct io_fadvise *fa = &req->fadvise;
3949 int ret;
3950
Jens Axboe3e694262020-02-01 09:22:49 -07003951 if (force_nonblock) {
3952 switch (fa->advice) {
3953 case POSIX_FADV_NORMAL:
3954 case POSIX_FADV_RANDOM:
3955 case POSIX_FADV_SEQUENTIAL:
3956 break;
3957 default:
3958 return -EAGAIN;
3959 }
3960 }
Jens Axboe4840e412019-12-25 22:03:45 -07003961
3962 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3963 if (ret < 0)
3964 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003965 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003966 return 0;
3967}
3968
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003969static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3970{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003971 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003972 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003973 if (sqe->ioprio || sqe->buf_index)
3974 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003975 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003976 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003977
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003978 req->statx.dfd = READ_ONCE(sqe->fd);
3979 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003980 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003981 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3982 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003983
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003984 return 0;
3985}
3986
Pavel Begunkov014db002020-03-03 21:33:12 +03003987static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003988{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003989 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003990 int ret;
3991
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003992 if (force_nonblock) {
3993 /* only need file table for an actual valid fd */
3994 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3995 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003996 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003997 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003998
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003999 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4000 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004001
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004002 if (ret < 0)
4003 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004004 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004005 return 0;
4006}
4007
Jens Axboeb5dba592019-12-11 14:02:38 -07004008static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4009{
4010 /*
4011 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004012 * leave the 'file' in an undeterminate state, and here need to modify
4013 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004014 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004015 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004016 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4017
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004018 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4019 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004020 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4021 sqe->rw_flags || sqe->buf_index)
4022 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004023 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004024 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004025
4026 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004027 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004028 return -EBADF;
4029
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004030 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004031 return 0;
4032}
4033
Jens Axboe229a7b62020-06-22 10:13:11 -06004034static int io_close(struct io_kiocb *req, bool force_nonblock,
4035 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004036{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004037 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004038 int ret;
4039
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004040 /* might be already done during nonblock submission */
4041 if (!close->put_file) {
4042 ret = __close_fd_get_file(close->fd, &close->put_file);
4043 if (ret < 0)
4044 return (ret == -ENOENT) ? -EBADF : ret;
4045 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004046
4047 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004048 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004049 /* was never set, but play safe */
4050 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004051 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004052 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004053 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004054 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004055
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004056 /* No ->flush() or already async, safely close from here */
4057 ret = filp_close(close->put_file, req->work.files);
4058 if (ret < 0)
4059 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004060 fput(close->put_file);
4061 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004062 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004063 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004064}
4065
Jens Axboe3529d8c2019-12-19 18:24:38 -07004066static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004067{
4068 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004069
4070 if (!req->file)
4071 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004072
4073 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4074 return -EINVAL;
4075 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4076 return -EINVAL;
4077
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004078 req->sync.off = READ_ONCE(sqe->off);
4079 req->sync.len = READ_ONCE(sqe->len);
4080 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004081 return 0;
4082}
4083
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004084static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004085{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004086 int ret;
4087
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004088 /* sync_file_range always requires a blocking context */
4089 if (force_nonblock)
4090 return -EAGAIN;
4091
Jens Axboe9adbd452019-12-20 08:45:55 -07004092 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004093 req->sync.flags);
4094 if (ret < 0)
4095 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004096 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004097 return 0;
4098}
4099
YueHaibing469956e2020-03-04 15:53:52 +08004100#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004101static int io_setup_async_msg(struct io_kiocb *req,
4102 struct io_async_msghdr *kmsg)
4103{
4104 if (req->io)
4105 return -EAGAIN;
4106 if (io_alloc_async_ctx(req)) {
4107 if (kmsg->iov != kmsg->fast_iov)
4108 kfree(kmsg->iov);
4109 return -ENOMEM;
4110 }
4111 req->flags |= REQ_F_NEED_CLEANUP;
4112 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4113 return -EAGAIN;
4114}
4115
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004116static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4117 struct io_async_msghdr *iomsg)
4118{
4119 iomsg->iov = iomsg->fast_iov;
4120 iomsg->msg.msg_name = &iomsg->addr;
4121 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4122 req->sr_msg.msg_flags, &iomsg->iov);
4123}
4124
Jens Axboe3529d8c2019-12-19 18:24:38 -07004125static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004126{
Jens Axboee47293f2019-12-20 08:58:21 -07004127 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004128 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004129 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004130
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004131 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4132 return -EINVAL;
4133
Jens Axboee47293f2019-12-20 08:58:21 -07004134 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004135 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004136 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004137
Jens Axboed8768362020-02-27 14:17:49 -07004138#ifdef CONFIG_COMPAT
4139 if (req->ctx->compat)
4140 sr->msg_flags |= MSG_CMSG_COMPAT;
4141#endif
4142
Jens Axboefddafac2020-01-04 20:19:44 -07004143 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004144 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004145 /* iovec is already imported */
4146 if (req->flags & REQ_F_NEED_CLEANUP)
4147 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004148
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004149 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004150 if (!ret)
4151 req->flags |= REQ_F_NEED_CLEANUP;
4152 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004153}
4154
Jens Axboe229a7b62020-06-22 10:13:11 -06004155static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4156 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004157{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004158 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004159 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004160 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004161 int ret;
4162
Jens Axboe03b12302019-12-02 18:50:25 -07004163 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004164 if (unlikely(!sock))
4165 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004166
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004167 if (req->io) {
4168 kmsg = &req->io->msg;
4169 kmsg->msg.msg_name = &req->io->msg.addr;
4170 /* if iov is set, it's allocated already */
4171 if (!kmsg->iov)
4172 kmsg->iov = kmsg->fast_iov;
4173 kmsg->msg.msg_iter.iov = kmsg->iov;
4174 } else {
4175 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004176 if (ret)
4177 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004178 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004179 }
4180
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004181 flags = req->sr_msg.msg_flags;
4182 if (flags & MSG_DONTWAIT)
4183 req->flags |= REQ_F_NOWAIT;
4184 else if (force_nonblock)
4185 flags |= MSG_DONTWAIT;
4186
4187 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4188 if (force_nonblock && ret == -EAGAIN)
4189 return io_setup_async_msg(req, kmsg);
4190 if (ret == -ERESTARTSYS)
4191 ret = -EINTR;
4192
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004193 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004194 kfree(kmsg->iov);
4195 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004196 if (ret < 0)
4197 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004198 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004199 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004200}
4201
Jens Axboe229a7b62020-06-22 10:13:11 -06004202static int io_send(struct io_kiocb *req, bool force_nonblock,
4203 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004204{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004205 struct io_sr_msg *sr = &req->sr_msg;
4206 struct msghdr msg;
4207 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004208 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004209 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004210 int ret;
4211
4212 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004213 if (unlikely(!sock))
4214 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004215
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004216 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4217 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004218 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004219
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004220 msg.msg_name = NULL;
4221 msg.msg_control = NULL;
4222 msg.msg_controllen = 0;
4223 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004224
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004225 flags = req->sr_msg.msg_flags;
4226 if (flags & MSG_DONTWAIT)
4227 req->flags |= REQ_F_NOWAIT;
4228 else if (force_nonblock)
4229 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004230
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004231 msg.msg_flags = flags;
4232 ret = sock_sendmsg(sock, &msg);
4233 if (force_nonblock && ret == -EAGAIN)
4234 return -EAGAIN;
4235 if (ret == -ERESTARTSYS)
4236 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004237
Jens Axboe03b12302019-12-02 18:50:25 -07004238 if (ret < 0)
4239 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004240 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004241 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004242}
4243
Pavel Begunkov1400e692020-07-12 20:41:05 +03004244static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4245 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004246{
4247 struct io_sr_msg *sr = &req->sr_msg;
4248 struct iovec __user *uiov;
4249 size_t iov_len;
4250 int ret;
4251
Pavel Begunkov1400e692020-07-12 20:41:05 +03004252 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4253 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004254 if (ret)
4255 return ret;
4256
4257 if (req->flags & REQ_F_BUFFER_SELECT) {
4258 if (iov_len > 1)
4259 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004260 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004261 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004262 sr->len = iomsg->iov[0].iov_len;
4263 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004264 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004265 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004266 } else {
4267 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004268 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004269 if (ret > 0)
4270 ret = 0;
4271 }
4272
4273 return ret;
4274}
4275
4276#ifdef CONFIG_COMPAT
4277static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004278 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004279{
4280 struct compat_msghdr __user *msg_compat;
4281 struct io_sr_msg *sr = &req->sr_msg;
4282 struct compat_iovec __user *uiov;
4283 compat_uptr_t ptr;
4284 compat_size_t len;
4285 int ret;
4286
Pavel Begunkov270a5942020-07-12 20:41:04 +03004287 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004288 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004289 &ptr, &len);
4290 if (ret)
4291 return ret;
4292
4293 uiov = compat_ptr(ptr);
4294 if (req->flags & REQ_F_BUFFER_SELECT) {
4295 compat_ssize_t clen;
4296
4297 if (len > 1)
4298 return -EINVAL;
4299 if (!access_ok(uiov, sizeof(*uiov)))
4300 return -EFAULT;
4301 if (__get_user(clen, &uiov->iov_len))
4302 return -EFAULT;
4303 if (clen < 0)
4304 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004305 sr->len = iomsg->iov[0].iov_len;
4306 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004307 } else {
4308 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004309 &iomsg->iov,
4310 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004311 if (ret < 0)
4312 return ret;
4313 }
4314
4315 return 0;
4316}
Jens Axboe03b12302019-12-02 18:50:25 -07004317#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004318
Pavel Begunkov1400e692020-07-12 20:41:05 +03004319static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4320 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004321{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004322 iomsg->msg.msg_name = &iomsg->addr;
4323 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004324
4325#ifdef CONFIG_COMPAT
4326 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004327 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004328#endif
4329
Pavel Begunkov1400e692020-07-12 20:41:05 +03004330 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004331}
4332
Jens Axboebcda7ba2020-02-23 16:42:51 -07004333static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004334 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004335{
4336 struct io_sr_msg *sr = &req->sr_msg;
4337 struct io_buffer *kbuf;
4338
Jens Axboebcda7ba2020-02-23 16:42:51 -07004339 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4340 if (IS_ERR(kbuf))
4341 return kbuf;
4342
4343 sr->kbuf = kbuf;
4344 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004345 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004346}
4347
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004348static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4349{
4350 return io_put_kbuf(req, req->sr_msg.kbuf);
4351}
4352
Jens Axboe3529d8c2019-12-19 18:24:38 -07004353static int io_recvmsg_prep(struct io_kiocb *req,
4354 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004355{
Jens Axboee47293f2019-12-20 08:58:21 -07004356 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004358 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004359
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004360 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4361 return -EINVAL;
4362
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004364 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004365 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004366 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004367
Jens Axboed8768362020-02-27 14:17:49 -07004368#ifdef CONFIG_COMPAT
4369 if (req->ctx->compat)
4370 sr->msg_flags |= MSG_CMSG_COMPAT;
4371#endif
4372
Jens Axboefddafac2020-01-04 20:19:44 -07004373 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004374 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004375 /* iovec is already imported */
4376 if (req->flags & REQ_F_NEED_CLEANUP)
4377 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004378
Pavel Begunkov1400e692020-07-12 20:41:05 +03004379 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004380 if (!ret)
4381 req->flags |= REQ_F_NEED_CLEANUP;
4382 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004383}
4384
Jens Axboe229a7b62020-06-22 10:13:11 -06004385static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4386 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004387{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004388 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004389 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004390 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004391 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004392 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004393
Jens Axboe0fa03c62019-04-19 13:34:07 -06004394 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004395 if (unlikely(!sock))
4396 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004397
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004398 if (req->io) {
4399 kmsg = &req->io->msg;
4400 kmsg->msg.msg_name = &req->io->msg.addr;
4401 /* if iov is set, it's allocated already */
4402 if (!kmsg->iov)
4403 kmsg->iov = kmsg->fast_iov;
4404 kmsg->msg.msg_iter.iov = kmsg->iov;
4405 } else {
4406 ret = io_recvmsg_copy_hdr(req, &iomsg);
4407 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004408 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004409 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004410 }
4411
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004412 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004413 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004414 if (IS_ERR(kbuf))
4415 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004416 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4417 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4418 1, req->sr_msg.len);
4419 }
4420
4421 flags = req->sr_msg.msg_flags;
4422 if (flags & MSG_DONTWAIT)
4423 req->flags |= REQ_F_NOWAIT;
4424 else if (force_nonblock)
4425 flags |= MSG_DONTWAIT;
4426
4427 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4428 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004429 if (force_nonblock && ret == -EAGAIN)
4430 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004431 if (ret == -ERESTARTSYS)
4432 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004433
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004434 if (req->flags & REQ_F_BUFFER_SELECTED)
4435 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004436 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004437 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004438 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004439 if (ret < 0)
4440 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004441 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004442 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004443}
4444
Jens Axboe229a7b62020-06-22 10:13:11 -06004445static int io_recv(struct io_kiocb *req, bool force_nonblock,
4446 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004447{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004448 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004449 struct io_sr_msg *sr = &req->sr_msg;
4450 struct msghdr msg;
4451 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004452 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004453 struct iovec iov;
4454 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004455 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004456
Jens Axboefddafac2020-01-04 20:19:44 -07004457 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004458 if (unlikely(!sock))
4459 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004460
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004461 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004462 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004463 if (IS_ERR(kbuf))
4464 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004465 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004466 }
4467
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004468 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004469 if (unlikely(ret))
4470 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004471
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004472 msg.msg_name = NULL;
4473 msg.msg_control = NULL;
4474 msg.msg_controllen = 0;
4475 msg.msg_namelen = 0;
4476 msg.msg_iocb = NULL;
4477 msg.msg_flags = 0;
4478
4479 flags = req->sr_msg.msg_flags;
4480 if (flags & MSG_DONTWAIT)
4481 req->flags |= REQ_F_NOWAIT;
4482 else if (force_nonblock)
4483 flags |= MSG_DONTWAIT;
4484
4485 ret = sock_recvmsg(sock, &msg, flags);
4486 if (force_nonblock && ret == -EAGAIN)
4487 return -EAGAIN;
4488 if (ret == -ERESTARTSYS)
4489 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004490out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004491 if (req->flags & REQ_F_BUFFER_SELECTED)
4492 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004493 if (ret < 0)
4494 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004495 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004496 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004497}
4498
Jens Axboe3529d8c2019-12-19 18:24:38 -07004499static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004500{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004501 struct io_accept *accept = &req->accept;
4502
Jens Axboe17f2fe32019-10-17 14:42:58 -06004503 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4504 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004505 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004506 return -EINVAL;
4507
Jens Axboed55e5f52019-12-11 16:12:15 -07004508 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4509 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004510 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004511 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004512 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004513}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004514
Jens Axboe229a7b62020-06-22 10:13:11 -06004515static int io_accept(struct io_kiocb *req, bool force_nonblock,
4516 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004517{
4518 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004519 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004520 int ret;
4521
Jiufei Xuee697dee2020-06-10 13:41:59 +08004522 if (req->file->f_flags & O_NONBLOCK)
4523 req->flags |= REQ_F_NOWAIT;
4524
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004525 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004526 accept->addr_len, accept->flags,
4527 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004528 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004529 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004530 if (ret < 0) {
4531 if (ret == -ERESTARTSYS)
4532 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004533 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004534 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004535 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004536 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004537}
4538
Jens Axboe3529d8c2019-12-19 18:24:38 -07004539static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004540{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004541 struct io_connect *conn = &req->connect;
4542 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004543
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004544 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4545 return -EINVAL;
4546 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4547 return -EINVAL;
4548
Jens Axboe3529d8c2019-12-19 18:24:38 -07004549 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4550 conn->addr_len = READ_ONCE(sqe->addr2);
4551
4552 if (!io)
4553 return 0;
4554
4555 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004556 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004557}
4558
Jens Axboe229a7b62020-06-22 10:13:11 -06004559static int io_connect(struct io_kiocb *req, bool force_nonblock,
4560 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004561{
Jens Axboef499a022019-12-02 16:28:46 -07004562 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004563 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004564 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004565
Jens Axboef499a022019-12-02 16:28:46 -07004566 if (req->io) {
4567 io = req->io;
4568 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004569 ret = move_addr_to_kernel(req->connect.addr,
4570 req->connect.addr_len,
4571 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004572 if (ret)
4573 goto out;
4574 io = &__io;
4575 }
4576
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004577 file_flags = force_nonblock ? O_NONBLOCK : 0;
4578
4579 ret = __sys_connect_file(req->file, &io->connect.address,
4580 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004581 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004582 if (req->io)
4583 return -EAGAIN;
4584 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004585 ret = -ENOMEM;
4586 goto out;
4587 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004588 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004589 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004590 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004591 if (ret == -ERESTARTSYS)
4592 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004593out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004594 if (ret < 0)
4595 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004596 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004597 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004598}
YueHaibing469956e2020-03-04 15:53:52 +08004599#else /* !CONFIG_NET */
4600static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4601{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004602 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004603}
4604
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004605static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4606 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004607{
YueHaibing469956e2020-03-04 15:53:52 +08004608 return -EOPNOTSUPP;
4609}
4610
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004611static int io_send(struct io_kiocb *req, bool force_nonblock,
4612 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004613{
4614 return -EOPNOTSUPP;
4615}
4616
4617static int io_recvmsg_prep(struct io_kiocb *req,
4618 const struct io_uring_sqe *sqe)
4619{
4620 return -EOPNOTSUPP;
4621}
4622
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004623static int io_recvmsg(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
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004629static int io_recv(struct io_kiocb *req, bool force_nonblock,
4630 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004631{
4632 return -EOPNOTSUPP;
4633}
4634
4635static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4636{
4637 return -EOPNOTSUPP;
4638}
4639
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004640static int io_accept(struct io_kiocb *req, bool force_nonblock,
4641 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004642{
4643 return -EOPNOTSUPP;
4644}
4645
4646static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4647{
4648 return -EOPNOTSUPP;
4649}
4650
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004651static int io_connect(struct io_kiocb *req, bool force_nonblock,
4652 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004653{
4654 return -EOPNOTSUPP;
4655}
4656#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004657
Jens Axboed7718a92020-02-14 22:23:12 -07004658struct io_poll_table {
4659 struct poll_table_struct pt;
4660 struct io_kiocb *req;
4661 int error;
4662};
4663
Jens Axboed7718a92020-02-14 22:23:12 -07004664static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4665 __poll_t mask, task_work_func_t func)
4666{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004667 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004668 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004669
4670 /* for instances that support it check for an event match first: */
4671 if (mask && !(mask & poll->events))
4672 return 0;
4673
4674 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4675
4676 list_del_init(&poll->wait.entry);
4677
Jens Axboed7718a92020-02-14 22:23:12 -07004678 req->result = mask;
4679 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004680 percpu_ref_get(&req->ctx->refs);
4681
Jens Axboed7718a92020-02-14 22:23:12 -07004682 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004683 * If we using the signalfd wait_queue_head for this wakeup, then
4684 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4685 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4686 * either, as the normal wakeup will suffice.
4687 */
4688 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4689
4690 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004691 * If this fails, then the task is exiting. When a task exits, the
4692 * work gets canceled, so just cancel this request as well instead
4693 * of executing it. We can't safely execute it anyway, as we may not
4694 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004695 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004696 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004697 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004698 struct task_struct *tsk;
4699
Jens Axboee3aabf92020-05-18 11:04:17 -06004700 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004701 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004702 task_work_add(tsk, &req->task_work, 0);
4703 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004704 }
Jens Axboed7718a92020-02-14 22:23:12 -07004705 return 1;
4706}
4707
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004708static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4709 __acquires(&req->ctx->completion_lock)
4710{
4711 struct io_ring_ctx *ctx = req->ctx;
4712
4713 if (!req->result && !READ_ONCE(poll->canceled)) {
4714 struct poll_table_struct pt = { ._key = poll->events };
4715
4716 req->result = vfs_poll(req->file, &pt) & poll->events;
4717 }
4718
4719 spin_lock_irq(&ctx->completion_lock);
4720 if (!req->result && !READ_ONCE(poll->canceled)) {
4721 add_wait_queue(poll->head, &poll->wait);
4722 return true;
4723 }
4724
4725 return false;
4726}
4727
Jens Axboed4e7cd32020-08-15 11:44:50 -07004728static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004729{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004730 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4731 if (req->opcode == IORING_OP_POLL_ADD)
4732 return (struct io_poll_iocb *) req->io;
4733 return req->apoll->double_poll;
4734}
4735
4736static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4737{
4738 if (req->opcode == IORING_OP_POLL_ADD)
4739 return &req->poll;
4740 return &req->apoll->poll;
4741}
4742
4743static void io_poll_remove_double(struct io_kiocb *req)
4744{
4745 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004746
4747 lockdep_assert_held(&req->ctx->completion_lock);
4748
4749 if (poll && poll->head) {
4750 struct wait_queue_head *head = poll->head;
4751
4752 spin_lock(&head->lock);
4753 list_del_init(&poll->wait.entry);
4754 if (poll->wait.private)
4755 refcount_dec(&req->refs);
4756 poll->head = NULL;
4757 spin_unlock(&head->lock);
4758 }
4759}
4760
4761static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4762{
4763 struct io_ring_ctx *ctx = req->ctx;
4764
Jens Axboed4e7cd32020-08-15 11:44:50 -07004765 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004766 req->poll.done = true;
4767 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4768 io_commit_cqring(ctx);
4769}
4770
4771static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4772{
4773 struct io_ring_ctx *ctx = req->ctx;
4774
4775 if (io_poll_rewait(req, &req->poll)) {
4776 spin_unlock_irq(&ctx->completion_lock);
4777 return;
4778 }
4779
4780 hash_del(&req->hash_node);
4781 io_poll_complete(req, req->result, 0);
4782 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004783 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004784 spin_unlock_irq(&ctx->completion_lock);
4785
4786 io_cqring_ev_posted(ctx);
4787}
4788
4789static void io_poll_task_func(struct callback_head *cb)
4790{
4791 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004793 struct io_kiocb *nxt = NULL;
4794
4795 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004796 if (nxt)
4797 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004798 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004799}
4800
4801static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4802 int sync, void *key)
4803{
4804 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004805 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004806 __poll_t mask = key_to_poll(key);
4807
4808 /* for instances that support it check for an event match first: */
4809 if (mask && !(mask & poll->events))
4810 return 0;
4811
Jens Axboe8706e042020-09-28 08:38:54 -06004812 list_del_init(&wait->entry);
4813
Jens Axboe807abcb2020-07-17 17:09:27 -06004814 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004815 bool done;
4816
Jens Axboe807abcb2020-07-17 17:09:27 -06004817 spin_lock(&poll->head->lock);
4818 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004819 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004820 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004821 /* make sure double remove sees this as being gone */
4822 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004823 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004824 if (!done)
4825 __io_async_wake(req, poll, mask, io_poll_task_func);
4826 }
4827 refcount_dec(&req->refs);
4828 return 1;
4829}
4830
4831static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4832 wait_queue_func_t wake_func)
4833{
4834 poll->head = NULL;
4835 poll->done = false;
4836 poll->canceled = false;
4837 poll->events = events;
4838 INIT_LIST_HEAD(&poll->wait.entry);
4839 init_waitqueue_func_entry(&poll->wait, wake_func);
4840}
4841
4842static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004843 struct wait_queue_head *head,
4844 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004845{
4846 struct io_kiocb *req = pt->req;
4847
4848 /*
4849 * If poll->head is already set, it's because the file being polled
4850 * uses multiple waitqueues for poll handling (eg one for read, one
4851 * for write). Setup a separate io_poll_iocb if this happens.
4852 */
4853 if (unlikely(poll->head)) {
4854 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004855 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004856 pt->error = -EINVAL;
4857 return;
4858 }
4859 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4860 if (!poll) {
4861 pt->error = -ENOMEM;
4862 return;
4863 }
4864 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4865 refcount_inc(&req->refs);
4866 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004867 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004868 }
4869
4870 pt->error = 0;
4871 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004872
4873 if (poll->events & EPOLLEXCLUSIVE)
4874 add_wait_queue_exclusive(head, &poll->wait);
4875 else
4876 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004877}
4878
4879static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4880 struct poll_table_struct *p)
4881{
4882 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004883 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004884
Jens Axboe807abcb2020-07-17 17:09:27 -06004885 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004886}
4887
Jens Axboed7718a92020-02-14 22:23:12 -07004888static void io_async_task_func(struct callback_head *cb)
4889{
4890 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4891 struct async_poll *apoll = req->apoll;
4892 struct io_ring_ctx *ctx = req->ctx;
4893
4894 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4895
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004896 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004897 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004898 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004899 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004900 }
4901
Jens Axboe31067252020-05-17 17:43:31 -06004902 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004903 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004904 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004905
Jens Axboed4e7cd32020-08-15 11:44:50 -07004906 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004907 spin_unlock_irq(&ctx->completion_lock);
4908
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004909 if (!READ_ONCE(apoll->poll.canceled))
4910 __io_req_task_submit(req);
4911 else
4912 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004913
Jens Axboe6d816e02020-08-11 08:04:14 -06004914 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004915 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004916 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004917}
4918
4919static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4920 void *key)
4921{
4922 struct io_kiocb *req = wait->private;
4923 struct io_poll_iocb *poll = &req->apoll->poll;
4924
4925 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4926 key_to_poll(key));
4927
4928 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4929}
4930
4931static void io_poll_req_insert(struct io_kiocb *req)
4932{
4933 struct io_ring_ctx *ctx = req->ctx;
4934 struct hlist_head *list;
4935
4936 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4937 hlist_add_head(&req->hash_node, list);
4938}
4939
4940static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4941 struct io_poll_iocb *poll,
4942 struct io_poll_table *ipt, __poll_t mask,
4943 wait_queue_func_t wake_func)
4944 __acquires(&ctx->completion_lock)
4945{
4946 struct io_ring_ctx *ctx = req->ctx;
4947 bool cancel = false;
4948
Jens Axboe18bceab2020-05-15 11:56:54 -06004949 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004950 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004951 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004952
4953 ipt->pt._key = mask;
4954 ipt->req = req;
4955 ipt->error = -EINVAL;
4956
Jens Axboed7718a92020-02-14 22:23:12 -07004957 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4958
4959 spin_lock_irq(&ctx->completion_lock);
4960 if (likely(poll->head)) {
4961 spin_lock(&poll->head->lock);
4962 if (unlikely(list_empty(&poll->wait.entry))) {
4963 if (ipt->error)
4964 cancel = true;
4965 ipt->error = 0;
4966 mask = 0;
4967 }
4968 if (mask || ipt->error)
4969 list_del_init(&poll->wait.entry);
4970 else if (cancel)
4971 WRITE_ONCE(poll->canceled, true);
4972 else if (!poll->done) /* actually waiting for an event */
4973 io_poll_req_insert(req);
4974 spin_unlock(&poll->head->lock);
4975 }
4976
4977 return mask;
4978}
4979
4980static bool io_arm_poll_handler(struct io_kiocb *req)
4981{
4982 const struct io_op_def *def = &io_op_defs[req->opcode];
4983 struct io_ring_ctx *ctx = req->ctx;
4984 struct async_poll *apoll;
4985 struct io_poll_table ipt;
4986 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004987 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004988
4989 if (!req->file || !file_can_poll(req->file))
4990 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004991 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004992 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004993 if (def->pollin)
4994 rw = READ;
4995 else if (def->pollout)
4996 rw = WRITE;
4997 else
4998 return false;
4999 /* if we can't nonblock try, then no point in arming a poll handler */
5000 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005001 return false;
5002
5003 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5004 if (unlikely(!apoll))
5005 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005006 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005007
5008 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005009 req->apoll = apoll;
5010 INIT_HLIST_NODE(&req->hash_node);
5011
Nathan Chancellor8755d972020-03-02 16:01:19 -07005012 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005013 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005014 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005015 if (def->pollout)
5016 mask |= POLLOUT | POLLWRNORM;
5017 mask |= POLLERR | POLLPRI;
5018
5019 ipt.pt._qproc = io_async_queue_proc;
5020
5021 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5022 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005023 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005024 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005025 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005026 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005027 kfree(apoll);
5028 return false;
5029 }
5030 spin_unlock_irq(&ctx->completion_lock);
5031 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5032 apoll->poll.events);
5033 return true;
5034}
5035
5036static bool __io_poll_remove_one(struct io_kiocb *req,
5037 struct io_poll_iocb *poll)
5038{
Jens Axboeb41e9852020-02-17 09:52:41 -07005039 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005040
5041 spin_lock(&poll->head->lock);
5042 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005043 if (!list_empty(&poll->wait.entry)) {
5044 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005045 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005046 }
5047 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005048 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005049 return do_complete;
5050}
5051
5052static bool io_poll_remove_one(struct io_kiocb *req)
5053{
5054 bool do_complete;
5055
Jens Axboed4e7cd32020-08-15 11:44:50 -07005056 io_poll_remove_double(req);
5057
Jens Axboed7718a92020-02-14 22:23:12 -07005058 if (req->opcode == IORING_OP_POLL_ADD) {
5059 do_complete = __io_poll_remove_one(req, &req->poll);
5060 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005061 struct async_poll *apoll = req->apoll;
5062
Jens Axboed7718a92020-02-14 22:23:12 -07005063 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005064 do_complete = __io_poll_remove_one(req, &apoll->poll);
5065 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005066 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005067 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005068 kfree(apoll);
5069 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005070 }
5071
Jens Axboeb41e9852020-02-17 09:52:41 -07005072 if (do_complete) {
5073 io_cqring_fill_event(req, -ECANCELED);
5074 io_commit_cqring(req->ctx);
5075 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005076 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005077 io_put_req(req);
5078 }
5079
5080 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005081}
5082
Jens Axboe76e1b642020-09-26 15:05:03 -06005083/*
5084 * Returns true if we found and killed one or more poll requests
5085 */
5086static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005087{
Jens Axboe78076bb2019-12-04 19:56:40 -07005088 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005089 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005090 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005091
5092 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005093 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5094 struct hlist_head *list;
5095
5096 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005097 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5098 if (io_task_match(req, tsk))
5099 posted += io_poll_remove_one(req);
5100 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005101 }
5102 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005103
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005104 if (posted)
5105 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005106
5107 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005108}
5109
Jens Axboe47f46762019-11-09 17:43:02 -07005110static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5111{
Jens Axboe78076bb2019-12-04 19:56:40 -07005112 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005113 struct io_kiocb *req;
5114
Jens Axboe78076bb2019-12-04 19:56:40 -07005115 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5116 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005117 if (sqe_addr != req->user_data)
5118 continue;
5119 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005120 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005121 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005122 }
5123
5124 return -ENOENT;
5125}
5126
Jens Axboe3529d8c2019-12-19 18:24:38 -07005127static int io_poll_remove_prep(struct io_kiocb *req,
5128 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005129{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005130 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5131 return -EINVAL;
5132 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5133 sqe->poll_events)
5134 return -EINVAL;
5135
Jens Axboe0969e782019-12-17 18:40:57 -07005136 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005137 return 0;
5138}
5139
5140/*
5141 * Find a running poll command that matches one specified in sqe->addr,
5142 * and remove it if found.
5143 */
5144static int io_poll_remove(struct io_kiocb *req)
5145{
5146 struct io_ring_ctx *ctx = req->ctx;
5147 u64 addr;
5148 int ret;
5149
Jens Axboe0969e782019-12-17 18:40:57 -07005150 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005151 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005152 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005153 spin_unlock_irq(&ctx->completion_lock);
5154
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005155 if (ret < 0)
5156 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005157 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005158 return 0;
5159}
5160
Jens Axboe221c5eb2019-01-17 09:41:58 -07005161static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5162 void *key)
5163{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005164 struct io_kiocb *req = wait->private;
5165 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005166
Jens Axboed7718a92020-02-14 22:23:12 -07005167 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005168}
5169
Jens Axboe221c5eb2019-01-17 09:41:58 -07005170static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5171 struct poll_table_struct *p)
5172{
5173 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5174
Jens Axboe807abcb2020-07-17 17:09:27 -06005175 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005176}
5177
Jens Axboe3529d8c2019-12-19 18:24:38 -07005178static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005179{
5180 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005181 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005182
5183 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5184 return -EINVAL;
5185 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5186 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005187 if (!poll->file)
5188 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005189
Jiufei Xue5769a352020-06-17 17:53:55 +08005190 events = READ_ONCE(sqe->poll32_events);
5191#ifdef __BIG_ENDIAN
5192 events = swahw32(events);
5193#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005194 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5195 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005196 return 0;
5197}
5198
Pavel Begunkov014db002020-03-03 21:33:12 +03005199static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005200{
5201 struct io_poll_iocb *poll = &req->poll;
5202 struct io_ring_ctx *ctx = req->ctx;
5203 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005204 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005205
Jens Axboe78076bb2019-12-04 19:56:40 -07005206 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005207 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005208
Jens Axboed7718a92020-02-14 22:23:12 -07005209 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5210 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005211
Jens Axboe8c838782019-03-12 15:48:16 -06005212 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005213 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005214 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005215 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005216 spin_unlock_irq(&ctx->completion_lock);
5217
Jens Axboe8c838782019-03-12 15:48:16 -06005218 if (mask) {
5219 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005220 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005221 }
Jens Axboe8c838782019-03-12 15:48:16 -06005222 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005223}
5224
Jens Axboe5262f562019-09-17 12:26:57 -06005225static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5226{
Jens Axboead8a48a2019-11-15 08:49:11 -07005227 struct io_timeout_data *data = container_of(timer,
5228 struct io_timeout_data, timer);
5229 struct io_kiocb *req = data->req;
5230 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005231 unsigned long flags;
5232
Jens Axboe5262f562019-09-17 12:26:57 -06005233 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005234 atomic_set(&req->ctx->cq_timeouts,
5235 atomic_read(&req->ctx->cq_timeouts) + 1);
5236
zhangyi (F)ef036812019-10-23 15:10:08 +08005237 /*
Jens Axboe11365042019-10-16 09:08:32 -06005238 * We could be racing with timeout deletion. If the list is empty,
5239 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005240 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005241 if (!list_empty(&req->timeout.list))
5242 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005243
Jens Axboe78e19bb2019-11-06 15:21:34 -07005244 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005245 io_commit_cqring(ctx);
5246 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5247
5248 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005249 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005250 io_put_req(req);
5251 return HRTIMER_NORESTART;
5252}
5253
Jens Axboef254ac02020-08-12 17:33:30 -06005254static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005255{
Jens Axboef254ac02020-08-12 17:33:30 -06005256 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005257
Jens Axboef254ac02020-08-12 17:33:30 -06005258 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005259
Jens Axboe2d283902019-12-04 11:08:05 -07005260 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005261 if (ret == -1)
5262 return -EALREADY;
5263
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005264 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005265 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005266 io_cqring_fill_event(req, -ECANCELED);
5267 io_put_req(req);
5268 return 0;
5269}
5270
Jens Axboef254ac02020-08-12 17:33:30 -06005271static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5272{
5273 struct io_kiocb *req;
5274 int ret = -ENOENT;
5275
5276 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5277 if (user_data == req->user_data) {
5278 ret = 0;
5279 break;
5280 }
5281 }
5282
5283 if (ret == -ENOENT)
5284 return ret;
5285
5286 return __io_timeout_cancel(req);
5287}
5288
Jens Axboe3529d8c2019-12-19 18:24:38 -07005289static int io_timeout_remove_prep(struct io_kiocb *req,
5290 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005291{
Jens Axboeb29472e2019-12-17 18:50:29 -07005292 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5293 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005294 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5295 return -EINVAL;
5296 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005297 return -EINVAL;
5298
5299 req->timeout.addr = READ_ONCE(sqe->addr);
5300 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5301 if (req->timeout.flags)
5302 return -EINVAL;
5303
Jens Axboeb29472e2019-12-17 18:50:29 -07005304 return 0;
5305}
5306
Jens Axboe11365042019-10-16 09:08:32 -06005307/*
5308 * Remove or update an existing timeout command
5309 */
Jens Axboefc4df992019-12-10 14:38:45 -07005310static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005311{
5312 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005313 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005314
Jens Axboe11365042019-10-16 09:08:32 -06005315 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005316 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005317
Jens Axboe47f46762019-11-09 17:43:02 -07005318 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005319 io_commit_cqring(ctx);
5320 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005321 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005322 if (ret < 0)
5323 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005324 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005325 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005326}
5327
Jens Axboe3529d8c2019-12-19 18:24:38 -07005328static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005329 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005330{
Jens Axboead8a48a2019-11-15 08:49:11 -07005331 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005332 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005333 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005334
Jens Axboead8a48a2019-11-15 08:49:11 -07005335 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005336 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005337 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005338 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005339 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005340 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005341 flags = READ_ONCE(sqe->timeout_flags);
5342 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005343 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005344
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005345 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005346
Jens Axboe3529d8c2019-12-19 18:24:38 -07005347 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005348 return -ENOMEM;
5349
5350 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005351 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005352
5353 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005354 return -EFAULT;
5355
Jens Axboe11365042019-10-16 09:08:32 -06005356 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005357 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005358 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005359 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005360
Jens Axboead8a48a2019-11-15 08:49:11 -07005361 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5362 return 0;
5363}
5364
Jens Axboefc4df992019-12-10 14:38:45 -07005365static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005366{
Jens Axboead8a48a2019-11-15 08:49:11 -07005367 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005368 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005369 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005370 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005371
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005372 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005373
Jens Axboe5262f562019-09-17 12:26:57 -06005374 /*
5375 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005376 * timeout event to be satisfied. If it isn't set, then this is
5377 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005378 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005379 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005380 entry = ctx->timeout_list.prev;
5381 goto add;
5382 }
Jens Axboe5262f562019-09-17 12:26:57 -06005383
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005384 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5385 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005386
5387 /*
5388 * Insertion sort, ensuring the first entry in the list is always
5389 * the one we need first.
5390 */
Jens Axboe5262f562019-09-17 12:26:57 -06005391 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005392 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5393 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005394
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005395 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005396 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005397 /* nxt.seq is behind @tail, otherwise would've been completed */
5398 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005399 break;
5400 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005401add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005402 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005403 data->timer.function = io_timeout_fn;
5404 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005405 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005406 return 0;
5407}
5408
Jens Axboe62755e32019-10-28 21:49:21 -06005409static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005410{
Jens Axboe62755e32019-10-28 21:49:21 -06005411 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005412
Jens Axboe62755e32019-10-28 21:49:21 -06005413 return req->user_data == (unsigned long) data;
5414}
5415
Jens Axboee977d6d2019-11-05 12:39:45 -07005416static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005417{
Jens Axboe62755e32019-10-28 21:49:21 -06005418 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005419 int ret = 0;
5420
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005421 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005422 switch (cancel_ret) {
5423 case IO_WQ_CANCEL_OK:
5424 ret = 0;
5425 break;
5426 case IO_WQ_CANCEL_RUNNING:
5427 ret = -EALREADY;
5428 break;
5429 case IO_WQ_CANCEL_NOTFOUND:
5430 ret = -ENOENT;
5431 break;
5432 }
5433
Jens Axboee977d6d2019-11-05 12:39:45 -07005434 return ret;
5435}
5436
Jens Axboe47f46762019-11-09 17:43:02 -07005437static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5438 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005439 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005440{
5441 unsigned long flags;
5442 int ret;
5443
5444 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5445 if (ret != -ENOENT) {
5446 spin_lock_irqsave(&ctx->completion_lock, flags);
5447 goto done;
5448 }
5449
5450 spin_lock_irqsave(&ctx->completion_lock, flags);
5451 ret = io_timeout_cancel(ctx, sqe_addr);
5452 if (ret != -ENOENT)
5453 goto done;
5454 ret = io_poll_cancel(ctx, sqe_addr);
5455done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005456 if (!ret)
5457 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005458 io_cqring_fill_event(req, ret);
5459 io_commit_cqring(ctx);
5460 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5461 io_cqring_ev_posted(ctx);
5462
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005463 if (ret < 0)
5464 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005465 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005466}
5467
Jens Axboe3529d8c2019-12-19 18:24:38 -07005468static int io_async_cancel_prep(struct io_kiocb *req,
5469 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005470{
Jens Axboefbf23842019-12-17 18:45:56 -07005471 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005472 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005473 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5474 return -EINVAL;
5475 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005476 return -EINVAL;
5477
Jens Axboefbf23842019-12-17 18:45:56 -07005478 req->cancel.addr = READ_ONCE(sqe->addr);
5479 return 0;
5480}
5481
Pavel Begunkov014db002020-03-03 21:33:12 +03005482static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005483{
5484 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005485
Pavel Begunkov014db002020-03-03 21:33:12 +03005486 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005487 return 0;
5488}
5489
Jens Axboe05f3fb32019-12-09 11:22:50 -07005490static int io_files_update_prep(struct io_kiocb *req,
5491 const struct io_uring_sqe *sqe)
5492{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005493 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5494 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005495 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5496 return -EINVAL;
5497 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005498 return -EINVAL;
5499
5500 req->files_update.offset = READ_ONCE(sqe->off);
5501 req->files_update.nr_args = READ_ONCE(sqe->len);
5502 if (!req->files_update.nr_args)
5503 return -EINVAL;
5504 req->files_update.arg = READ_ONCE(sqe->addr);
5505 return 0;
5506}
5507
Jens Axboe229a7b62020-06-22 10:13:11 -06005508static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5509 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005510{
5511 struct io_ring_ctx *ctx = req->ctx;
5512 struct io_uring_files_update up;
5513 int ret;
5514
Jens Axboef86cd202020-01-29 13:46:44 -07005515 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005516 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005517
5518 up.offset = req->files_update.offset;
5519 up.fds = req->files_update.arg;
5520
5521 mutex_lock(&ctx->uring_lock);
5522 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5523 mutex_unlock(&ctx->uring_lock);
5524
5525 if (ret < 0)
5526 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005527 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005528 return 0;
5529}
5530
Jens Axboe3529d8c2019-12-19 18:24:38 -07005531static int io_req_defer_prep(struct io_kiocb *req,
5532 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005533{
Jens Axboee7815732019-12-17 19:45:06 -07005534 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005535
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005536 if (!sqe)
5537 return 0;
5538
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005539 if (io_alloc_async_ctx(req))
5540 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005541 ret = io_prep_work_files(req);
5542 if (unlikely(ret))
5543 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005544
Jens Axboe202700e12020-09-12 13:18:10 -06005545 io_prep_async_work(req);
5546
Jens Axboed625c6e2019-12-17 19:53:05 -07005547 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005548 case IORING_OP_NOP:
5549 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005550 case IORING_OP_READV:
5551 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005552 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005553 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005554 break;
5555 case IORING_OP_WRITEV:
5556 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005557 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005558 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005559 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005560 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005561 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005562 break;
5563 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005564 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005565 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005566 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005567 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005568 break;
5569 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005570 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005571 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005572 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005573 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005574 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005575 break;
5576 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005577 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005578 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005579 break;
Jens Axboef499a022019-12-02 16:28:46 -07005580 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005581 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005582 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005583 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005584 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005585 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005586 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005587 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005588 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005589 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005590 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005591 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005592 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005593 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005594 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005595 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005596 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005597 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005598 case IORING_OP_FALLOCATE:
5599 ret = io_fallocate_prep(req, sqe);
5600 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005601 case IORING_OP_OPENAT:
5602 ret = io_openat_prep(req, sqe);
5603 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005604 case IORING_OP_CLOSE:
5605 ret = io_close_prep(req, sqe);
5606 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005607 case IORING_OP_FILES_UPDATE:
5608 ret = io_files_update_prep(req, sqe);
5609 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005610 case IORING_OP_STATX:
5611 ret = io_statx_prep(req, sqe);
5612 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005613 case IORING_OP_FADVISE:
5614 ret = io_fadvise_prep(req, sqe);
5615 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005616 case IORING_OP_MADVISE:
5617 ret = io_madvise_prep(req, sqe);
5618 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005619 case IORING_OP_OPENAT2:
5620 ret = io_openat2_prep(req, sqe);
5621 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005622 case IORING_OP_EPOLL_CTL:
5623 ret = io_epoll_ctl_prep(req, sqe);
5624 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005625 case IORING_OP_SPLICE:
5626 ret = io_splice_prep(req, sqe);
5627 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005628 case IORING_OP_PROVIDE_BUFFERS:
5629 ret = io_provide_buffers_prep(req, sqe);
5630 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005631 case IORING_OP_REMOVE_BUFFERS:
5632 ret = io_remove_buffers_prep(req, sqe);
5633 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005634 case IORING_OP_TEE:
5635 ret = io_tee_prep(req, sqe);
5636 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005637 default:
Jens Axboee7815732019-12-17 19:45:06 -07005638 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5639 req->opcode);
5640 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005641 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005642 }
5643
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005644 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005645}
5646
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005647static u32 io_get_sequence(struct io_kiocb *req)
5648{
5649 struct io_kiocb *pos;
5650 struct io_ring_ctx *ctx = req->ctx;
5651 u32 total_submitted, nr_reqs = 1;
5652
5653 if (req->flags & REQ_F_LINK_HEAD)
5654 list_for_each_entry(pos, &req->link_list, link_list)
5655 nr_reqs++;
5656
5657 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5658 return total_submitted - nr_reqs;
5659}
5660
Jens Axboe3529d8c2019-12-19 18:24:38 -07005661static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005662{
Jackie Liua197f662019-11-08 08:09:12 -07005663 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005664 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005665 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005666 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005667
Bob Liu9d858b22019-11-13 18:06:25 +08005668 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005669 if (likely(list_empty_careful(&ctx->defer_list) &&
5670 !(req->flags & REQ_F_IO_DRAIN)))
5671 return 0;
5672
5673 seq = io_get_sequence(req);
5674 /* Still a chance to pass the sequence check */
5675 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005676 return 0;
5677
Pavel Begunkov650b5482020-05-17 14:02:11 +03005678 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005679 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005680 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005681 return ret;
5682 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005683 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005684 de = kmalloc(sizeof(*de), GFP_KERNEL);
5685 if (!de)
5686 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005687
Jens Axboede0617e2019-04-06 21:51:27 -06005688 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005689 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005690 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005691 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005692 io_queue_async_work(req);
5693 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005694 }
5695
Jens Axboe915967f2019-11-21 09:01:20 -07005696 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005697 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005698 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005699 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005700 spin_unlock_irq(&ctx->completion_lock);
5701 return -EIOCBQUEUED;
5702}
5703
Jens Axboef573d382020-09-22 10:19:24 -06005704static void io_req_drop_files(struct io_kiocb *req)
5705{
5706 struct io_ring_ctx *ctx = req->ctx;
5707 unsigned long flags;
5708
5709 spin_lock_irqsave(&ctx->inflight_lock, flags);
5710 list_del(&req->inflight_entry);
5711 if (waitqueue_active(&ctx->inflight_wait))
5712 wake_up(&ctx->inflight_wait);
5713 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5714 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005715 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005716 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005717 req->work.files = NULL;
5718}
5719
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005720static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005721{
5722 struct io_async_ctx *io = req->io;
5723
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005724 if (req->flags & REQ_F_BUFFER_SELECTED) {
5725 switch (req->opcode) {
5726 case IORING_OP_READV:
5727 case IORING_OP_READ_FIXED:
5728 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005729 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005730 break;
5731 case IORING_OP_RECVMSG:
5732 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005733 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005734 break;
5735 }
5736 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005737 }
5738
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005739 if (req->flags & REQ_F_NEED_CLEANUP) {
5740 switch (req->opcode) {
5741 case IORING_OP_READV:
5742 case IORING_OP_READ_FIXED:
5743 case IORING_OP_READ:
5744 case IORING_OP_WRITEV:
5745 case IORING_OP_WRITE_FIXED:
5746 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005747 if (io->rw.free_iovec)
5748 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005749 break;
5750 case IORING_OP_RECVMSG:
5751 case IORING_OP_SENDMSG:
5752 if (io->msg.iov != io->msg.fast_iov)
5753 kfree(io->msg.iov);
5754 break;
5755 case IORING_OP_SPLICE:
5756 case IORING_OP_TEE:
5757 io_put_file(req, req->splice.file_in,
5758 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5759 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005760 case IORING_OP_OPENAT:
5761 case IORING_OP_OPENAT2:
5762 if (req->open.filename)
5763 putname(req->open.filename);
5764 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005765 }
5766 req->flags &= ~REQ_F_NEED_CLEANUP;
5767 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005768
Jens Axboef573d382020-09-22 10:19:24 -06005769 if (req->flags & REQ_F_INFLIGHT)
5770 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005771}
5772
Jens Axboe3529d8c2019-12-19 18:24:38 -07005773static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005774 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005775{
Jackie Liua197f662019-11-08 08:09:12 -07005776 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005777 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005778
Jens Axboed625c6e2019-12-17 19:53:05 -07005779 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005780 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005781 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005782 break;
5783 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005784 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005785 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005786 if (sqe) {
5787 ret = io_read_prep(req, sqe, force_nonblock);
5788 if (ret < 0)
5789 break;
5790 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005791 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005792 break;
5793 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005794 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005795 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005796 if (sqe) {
5797 ret = io_write_prep(req, sqe, force_nonblock);
5798 if (ret < 0)
5799 break;
5800 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005801 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005802 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005803 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005804 if (sqe) {
5805 ret = io_prep_fsync(req, sqe);
5806 if (ret < 0)
5807 break;
5808 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005809 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005810 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005811 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005812 if (sqe) {
5813 ret = io_poll_add_prep(req, sqe);
5814 if (ret)
5815 break;
5816 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005817 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005818 break;
5819 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005820 if (sqe) {
5821 ret = io_poll_remove_prep(req, sqe);
5822 if (ret < 0)
5823 break;
5824 }
Jens Axboefc4df992019-12-10 14:38:45 -07005825 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005826 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005827 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005828 if (sqe) {
5829 ret = io_prep_sfr(req, sqe);
5830 if (ret < 0)
5831 break;
5832 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005833 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005834 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005835 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005836 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005837 if (sqe) {
5838 ret = io_sendmsg_prep(req, sqe);
5839 if (ret < 0)
5840 break;
5841 }
Jens Axboefddafac2020-01-04 20:19:44 -07005842 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005843 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005844 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005845 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005846 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005847 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005848 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005849 if (sqe) {
5850 ret = io_recvmsg_prep(req, sqe);
5851 if (ret)
5852 break;
5853 }
Jens Axboefddafac2020-01-04 20:19:44 -07005854 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005855 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005856 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005857 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005858 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005859 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005860 if (sqe) {
5861 ret = io_timeout_prep(req, sqe, false);
5862 if (ret)
5863 break;
5864 }
Jens Axboefc4df992019-12-10 14:38:45 -07005865 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005866 break;
Jens Axboe11365042019-10-16 09:08:32 -06005867 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005868 if (sqe) {
5869 ret = io_timeout_remove_prep(req, sqe);
5870 if (ret)
5871 break;
5872 }
Jens Axboefc4df992019-12-10 14:38:45 -07005873 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005874 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005875 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005876 if (sqe) {
5877 ret = io_accept_prep(req, sqe);
5878 if (ret)
5879 break;
5880 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005881 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005882 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005883 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005884 if (sqe) {
5885 ret = io_connect_prep(req, sqe);
5886 if (ret)
5887 break;
5888 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005889 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005890 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005891 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005892 if (sqe) {
5893 ret = io_async_cancel_prep(req, sqe);
5894 if (ret)
5895 break;
5896 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005897 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005898 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005899 case IORING_OP_FALLOCATE:
5900 if (sqe) {
5901 ret = io_fallocate_prep(req, sqe);
5902 if (ret)
5903 break;
5904 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005905 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005906 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005907 case IORING_OP_OPENAT:
5908 if (sqe) {
5909 ret = io_openat_prep(req, sqe);
5910 if (ret)
5911 break;
5912 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005913 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005914 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005915 case IORING_OP_CLOSE:
5916 if (sqe) {
5917 ret = io_close_prep(req, sqe);
5918 if (ret)
5919 break;
5920 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005921 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005922 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005923 case IORING_OP_FILES_UPDATE:
5924 if (sqe) {
5925 ret = io_files_update_prep(req, sqe);
5926 if (ret)
5927 break;
5928 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005929 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005930 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005931 case IORING_OP_STATX:
5932 if (sqe) {
5933 ret = io_statx_prep(req, sqe);
5934 if (ret)
5935 break;
5936 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005937 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005938 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005939 case IORING_OP_FADVISE:
5940 if (sqe) {
5941 ret = io_fadvise_prep(req, sqe);
5942 if (ret)
5943 break;
5944 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005945 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005946 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005947 case IORING_OP_MADVISE:
5948 if (sqe) {
5949 ret = io_madvise_prep(req, sqe);
5950 if (ret)
5951 break;
5952 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005953 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005954 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005955 case IORING_OP_OPENAT2:
5956 if (sqe) {
5957 ret = io_openat2_prep(req, sqe);
5958 if (ret)
5959 break;
5960 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005961 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005962 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005963 case IORING_OP_EPOLL_CTL:
5964 if (sqe) {
5965 ret = io_epoll_ctl_prep(req, sqe);
5966 if (ret)
5967 break;
5968 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005969 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005970 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005971 case IORING_OP_SPLICE:
5972 if (sqe) {
5973 ret = io_splice_prep(req, sqe);
5974 if (ret < 0)
5975 break;
5976 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005977 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005978 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005979 case IORING_OP_PROVIDE_BUFFERS:
5980 if (sqe) {
5981 ret = io_provide_buffers_prep(req, sqe);
5982 if (ret)
5983 break;
5984 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005985 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005986 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005987 case IORING_OP_REMOVE_BUFFERS:
5988 if (sqe) {
5989 ret = io_remove_buffers_prep(req, sqe);
5990 if (ret)
5991 break;
5992 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005993 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005994 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005995 case IORING_OP_TEE:
5996 if (sqe) {
5997 ret = io_tee_prep(req, sqe);
5998 if (ret < 0)
5999 break;
6000 }
6001 ret = io_tee(req, force_nonblock);
6002 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006003 default:
6004 ret = -EINVAL;
6005 break;
6006 }
6007
6008 if (ret)
6009 return ret;
6010
Jens Axboeb5325762020-05-19 21:20:27 -06006011 /* If the op doesn't have a file, we're not polling for it */
6012 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006013 const bool in_async = io_wq_current_is_worker();
6014
Jens Axboe11ba8202020-01-15 21:51:17 -07006015 /* workqueue context doesn't hold uring_lock, grab it now */
6016 if (in_async)
6017 mutex_lock(&ctx->uring_lock);
6018
Jens Axboe2b188cc2019-01-07 10:46:33 -07006019 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006020
6021 if (in_async)
6022 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006023 }
6024
6025 return 0;
6026}
6027
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006028static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006029{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006030 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006031 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006032 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006033
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006034 timeout = io_prep_linked_timeout(req);
6035 if (timeout)
6036 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006037
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006038 /* if NO_CANCEL is set, we must still run the work */
6039 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6040 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006041 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006042 }
Jens Axboe31b51512019-01-18 22:56:34 -07006043
Jens Axboe561fb042019-10-24 07:25:42 -06006044 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006045 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006046 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006047 /*
6048 * We can get EAGAIN for polled IO even though we're
6049 * forcing a sync submission from here, since we can't
6050 * wait for request slots on the block side.
6051 */
6052 if (ret != -EAGAIN)
6053 break;
6054 cond_resched();
6055 } while (1);
6056 }
Jens Axboe31b51512019-01-18 22:56:34 -07006057
Jens Axboe561fb042019-10-24 07:25:42 -06006058 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006059 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006060 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006061 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006062
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006063 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006064}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006065
Jens Axboe65e19f52019-10-26 07:20:21 -06006066static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6067 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006068{
Jens Axboe65e19f52019-10-26 07:20:21 -06006069 struct fixed_file_table *table;
6070
Jens Axboe05f3fb32019-12-09 11:22:50 -07006071 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006072 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006073}
6074
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006075static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6076 int fd, struct file **out_file, bool fixed)
6077{
6078 struct io_ring_ctx *ctx = req->ctx;
6079 struct file *file;
6080
6081 if (fixed) {
6082 if (unlikely(!ctx->file_data ||
6083 (unsigned) fd >= ctx->nr_user_files))
6084 return -EBADF;
6085 fd = array_index_nospec(fd, ctx->nr_user_files);
6086 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006087 if (file) {
6088 req->fixed_file_refs = ctx->file_data->cur_refs;
6089 percpu_ref_get(req->fixed_file_refs);
6090 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006091 } else {
6092 trace_io_uring_file_get(ctx, fd);
6093 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006094 }
6095
Jens Axboefd2206e2020-06-02 16:40:47 -06006096 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6097 *out_file = file;
6098 return 0;
6099 }
6100 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006101}
6102
Jens Axboe3529d8c2019-12-19 18:24:38 -07006103static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006104 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006105{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006106 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006107
Jens Axboe63ff8222020-05-07 14:56:15 -06006108 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006109 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006110 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006111
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006112 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006113}
6114
Jackie Liua197f662019-11-08 08:09:12 -07006115static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006116{
Jackie Liua197f662019-11-08 08:09:12 -07006117 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006118
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006119 io_req_init_async(req);
6120
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006121 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006122 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006123
Jens Axboe0f212202020-09-13 13:09:39 -06006124 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006125 get_nsproxy(current->nsproxy);
6126 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006127 req->flags |= REQ_F_INFLIGHT;
6128
Jens Axboefcb323c2019-10-24 12:39:47 -06006129 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006130 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006131 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006132 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006133}
6134
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006135static inline int io_prep_work_files(struct io_kiocb *req)
6136{
6137 if (!io_op_defs[req->opcode].file_table)
6138 return 0;
6139 return io_grab_files(req);
6140}
6141
Jens Axboe2665abf2019-11-05 12:40:47 -07006142static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6143{
Jens Axboead8a48a2019-11-15 08:49:11 -07006144 struct io_timeout_data *data = container_of(timer,
6145 struct io_timeout_data, timer);
6146 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006147 struct io_ring_ctx *ctx = req->ctx;
6148 struct io_kiocb *prev = NULL;
6149 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006150
6151 spin_lock_irqsave(&ctx->completion_lock, flags);
6152
6153 /*
6154 * We don't expect the list to be empty, that will only happen if we
6155 * race with the completion of the linked work.
6156 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006157 if (!list_empty(&req->link_list)) {
6158 prev = list_entry(req->link_list.prev, struct io_kiocb,
6159 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006160 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006161 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006162 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6163 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006164 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006165 }
6166
6167 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6168
6169 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006170 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006171 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006172 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006173 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006174 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006175 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006176 return HRTIMER_NORESTART;
6177}
6178
Jens Axboe7271ef32020-08-10 09:55:22 -06006179static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006180{
Jens Axboe76a46e02019-11-10 23:34:16 -07006181 /*
6182 * If the list is now empty, then our linked request finished before
6183 * we got a chance to setup the timer
6184 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006185 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006186 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006187
Jens Axboead8a48a2019-11-15 08:49:11 -07006188 data->timer.function = io_link_timeout_fn;
6189 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6190 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006191 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006192}
6193
6194static void io_queue_linked_timeout(struct io_kiocb *req)
6195{
6196 struct io_ring_ctx *ctx = req->ctx;
6197
6198 spin_lock_irq(&ctx->completion_lock);
6199 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006200 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006201
Jens Axboe2665abf2019-11-05 12:40:47 -07006202 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006203 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006204}
6205
Jens Axboead8a48a2019-11-15 08:49:11 -07006206static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006207{
6208 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006210 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006211 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006212 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006213 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006214
Pavel Begunkov44932332019-12-05 16:16:35 +03006215 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6216 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006217 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006218 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006219
Jens Axboe76a46e02019-11-10 23:34:16 -07006220 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006221 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006222}
6223
Jens Axboef13fad72020-06-22 09:34:30 -06006224static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6225 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006226{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006227 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006228 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006229 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006230 int ret;
6231
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006232again:
6233 linked_timeout = io_prep_linked_timeout(req);
6234
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006235 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6236 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006237 if (old_creds)
6238 revert_creds(old_creds);
6239 if (old_creds == req->work.creds)
6240 old_creds = NULL; /* restored original creds */
6241 else
6242 old_creds = override_creds(req->work.creds);
6243 }
6244
Jens Axboef13fad72020-06-22 09:34:30 -06006245 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006246
6247 /*
6248 * We async punt it if the file wasn't marked NOWAIT, or if the file
6249 * doesn't support non-blocking read/write attempts
6250 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006251 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006252 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006253punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006254 ret = io_prep_work_files(req);
6255 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006256 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006257 /*
6258 * Queued up for async execution, worker will release
6259 * submit reference when the iocb is actually submitted.
6260 */
6261 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006262 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006263
Pavel Begunkovf063c542020-07-25 14:41:59 +03006264 if (linked_timeout)
6265 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006266 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006267 }
Jens Axboee65ef562019-03-12 10:16:44 -06006268
Pavel Begunkov652532a2020-07-03 22:15:07 +03006269 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006270err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006271 /* un-prep timeout, so it'll be killed as any other linked */
6272 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006273 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006274 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006275 io_req_complete(req, ret);
6276 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006277 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006278
Jens Axboe6c271ce2019-01-10 11:22:30 -07006279 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006280 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006281 if (linked_timeout)
6282 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006283
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006284 if (nxt) {
6285 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006286
6287 if (req->flags & REQ_F_FORCE_ASYNC)
6288 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006289 goto again;
6290 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006291exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006292 if (old_creds)
6293 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006294}
6295
Jens Axboef13fad72020-06-22 09:34:30 -06006296static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6297 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006298{
6299 int ret;
6300
Jens Axboe3529d8c2019-12-19 18:24:38 -07006301 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006302 if (ret) {
6303 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006304fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006305 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006306 io_put_req(req);
6307 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006308 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006309 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006310 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006311 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006312 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006313 goto fail_req;
6314 }
6315
Jens Axboece35a472019-12-17 08:04:44 -07006316 /*
6317 * Never try inline submit of IOSQE_ASYNC is set, go straight
6318 * to async execution.
6319 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006320 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006321 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6322 io_queue_async_work(req);
6323 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006324 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006325 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006326}
6327
Jens Axboef13fad72020-06-22 09:34:30 -06006328static inline void io_queue_link_head(struct io_kiocb *req,
6329 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006330{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006331 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006332 io_put_req(req);
6333 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006334 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006335 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006336}
6337
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006338static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006339 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006340{
Jackie Liua197f662019-11-08 08:09:12 -07006341 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006342 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006343
Jens Axboe9e645e112019-05-10 16:07:28 -06006344 /*
6345 * If we already have a head request, queue this one for async
6346 * submittal once the head completes. If we don't have a head but
6347 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6348 * submitted sync once the chain is complete. If none of those
6349 * conditions are true (normal request), then just queue it.
6350 */
6351 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006352 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006353
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006354 /*
6355 * Taking sequential execution of a link, draining both sides
6356 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6357 * requests in the link. So, it drains the head and the
6358 * next after the link request. The last one is done via
6359 * drain_next flag to persist the effect across calls.
6360 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006361 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006362 head->flags |= REQ_F_IO_DRAIN;
6363 ctx->drain_next = 1;
6364 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006365 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006366 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006367 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006368 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006369 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006370 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006371 trace_io_uring_link(ctx, req, head);
6372 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006373
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006374 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006375 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006376 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006377 *link = NULL;
6378 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006379 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006380 if (unlikely(ctx->drain_next)) {
6381 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006382 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006383 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006384 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006385 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006386 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006387
Pavel Begunkov711be032020-01-17 03:57:59 +03006388 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006389 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006390 req->flags |= REQ_F_FAIL_LINK;
6391 *link = req;
6392 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006393 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006394 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006395 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006396
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006397 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006398}
6399
Jens Axboe9a56a232019-01-09 09:06:50 -07006400/*
6401 * Batched submission is done, ensure local IO is flushed out.
6402 */
6403static void io_submit_state_end(struct io_submit_state *state)
6404{
Jens Axboef13fad72020-06-22 09:34:30 -06006405 if (!list_empty(&state->comp.list))
6406 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006407 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006408 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006409 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006410 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006411}
6412
6413/*
6414 * Start submission side cache.
6415 */
6416static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006417 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006418{
6419 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006420 state->comp.nr = 0;
6421 INIT_LIST_HEAD(&state->comp.list);
6422 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006423 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006424 state->file = NULL;
6425 state->ios_left = max_ios;
6426}
6427
Jens Axboe2b188cc2019-01-07 10:46:33 -07006428static void io_commit_sqring(struct io_ring_ctx *ctx)
6429{
Hristo Venev75b28af2019-08-26 17:23:46 +00006430 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006431
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006432 /*
6433 * Ensure any loads from the SQEs are done at this point,
6434 * since once we write the new head, the application could
6435 * write new data to them.
6436 */
6437 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006438}
6439
6440/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006441 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006442 * that is mapped by userspace. This means that care needs to be taken to
6443 * ensure that reads are stable, as we cannot rely on userspace always
6444 * being a good citizen. If members of the sqe are validated and then later
6445 * used, it's important that those reads are done through READ_ONCE() to
6446 * prevent a re-load down the line.
6447 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006448static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006449{
Hristo Venev75b28af2019-08-26 17:23:46 +00006450 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006451 unsigned head;
6452
6453 /*
6454 * The cached sq head (or cq tail) serves two purposes:
6455 *
6456 * 1) allows us to batch the cost of updating the user visible
6457 * head updates.
6458 * 2) allows the kernel side to track the head on its own, even
6459 * though the application is the one updating it.
6460 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006461 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006462 if (likely(head < ctx->sq_entries))
6463 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006464
6465 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006466 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006467 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006468 return NULL;
6469}
6470
6471static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6472{
6473 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006474}
6475
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006476/*
6477 * Check SQE restrictions (opcode and flags).
6478 *
6479 * Returns 'true' if SQE is allowed, 'false' otherwise.
6480 */
6481static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6482 struct io_kiocb *req,
6483 unsigned int sqe_flags)
6484{
6485 if (!ctx->restricted)
6486 return true;
6487
6488 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6489 return false;
6490
6491 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6492 ctx->restrictions.sqe_flags_required)
6493 return false;
6494
6495 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6496 ctx->restrictions.sqe_flags_required))
6497 return false;
6498
6499 return true;
6500}
6501
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006502#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6503 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6504 IOSQE_BUFFER_SELECT)
6505
6506static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6507 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006508 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006509{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006510 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006511 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006512
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006513 req->opcode = READ_ONCE(sqe->opcode);
6514 req->user_data = READ_ONCE(sqe->user_data);
6515 req->io = NULL;
6516 req->file = NULL;
6517 req->ctx = ctx;
6518 req->flags = 0;
6519 /* one is dropped after submission, the other at completion */
6520 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006521 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006522 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006523 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006524 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006525
6526 if (unlikely(req->opcode >= IORING_OP_LAST))
6527 return -EINVAL;
6528
Jens Axboe9d8426a2020-06-16 18:42:49 -06006529 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6530 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006531
6532 sqe_flags = READ_ONCE(sqe->flags);
6533 /* enforce forwards compatibility on users */
6534 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6535 return -EINVAL;
6536
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006537 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6538 return -EACCES;
6539
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006540 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6541 !io_op_defs[req->opcode].buffer_select)
6542 return -EOPNOTSUPP;
6543
6544 id = READ_ONCE(sqe->personality);
6545 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006546 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006547 req->work.creds = idr_find(&ctx->personality_idr, id);
6548 if (unlikely(!req->work.creds))
6549 return -EINVAL;
6550 get_cred(req->work.creds);
6551 }
6552
6553 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006554 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006555
Jens Axboe63ff8222020-05-07 14:56:15 -06006556 if (!io_op_defs[req->opcode].needs_file)
6557 return 0;
6558
6559 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006560}
6561
Jens Axboe0f212202020-09-13 13:09:39 -06006562static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006563{
Jens Axboeac8691c2020-06-01 08:30:41 -06006564 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006565 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006566 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006567
Jens Axboec4a2ed72019-11-21 21:01:26 -07006568 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006569 if (test_bit(0, &ctx->sq_check_overflow)) {
6570 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006571 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006572 return -EBUSY;
6573 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006574
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006575 /* make sure SQ entry isn't read before tail */
6576 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006577
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006578 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6579 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006580
Jens Axboe013538b2020-06-22 09:29:15 -06006581 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006582
6583 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006584 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006585 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006586 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006587
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006588 sqe = io_get_sqe(ctx);
6589 if (unlikely(!sqe)) {
6590 io_consume_sqe(ctx);
6591 break;
6592 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006593 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006594 if (unlikely(!req)) {
6595 if (!submitted)
6596 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006597 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006598 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006599
Jens Axboeac8691c2020-06-01 08:30:41 -06006600 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006601 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006602 /* will complete beyond this point, count as submitted */
6603 submitted++;
6604
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006605 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006606fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006607 io_put_req(req);
6608 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006609 break;
6610 }
6611
Jens Axboe354420f2020-01-08 18:55:15 -07006612 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006613 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006614 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006615 if (err)
6616 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006617 }
6618
Pavel Begunkov9466f432020-01-25 22:34:01 +03006619 if (unlikely(submitted != nr)) {
6620 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6621
6622 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6623 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006624 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006625 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006626 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006627
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006628 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6629 io_commit_sqring(ctx);
6630
Jens Axboe6c271ce2019-01-10 11:22:30 -07006631 return submitted;
6632}
6633
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006634static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6635{
6636 /* Tell userspace we may need a wakeup call */
6637 spin_lock_irq(&ctx->completion_lock);
6638 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6639 spin_unlock_irq(&ctx->completion_lock);
6640}
6641
6642static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6643{
6644 spin_lock_irq(&ctx->completion_lock);
6645 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6646 spin_unlock_irq(&ctx->completion_lock);
6647}
6648
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006649static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6650 int sync, void *key)
6651{
6652 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6653 int ret;
6654
6655 ret = autoremove_wake_function(wqe, mode, sync, key);
6656 if (ret) {
6657 unsigned long flags;
6658
6659 spin_lock_irqsave(&ctx->completion_lock, flags);
6660 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6661 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6662 }
6663 return ret;
6664}
6665
Jens Axboec8d1ba52020-09-14 11:07:26 -06006666enum sq_ret {
6667 SQT_IDLE = 1,
6668 SQT_SPIN = 2,
6669 SQT_DID_WORK = 4,
6670};
6671
6672static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
6673 unsigned long start_jiffies)
6674{
6675 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006676 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006677 unsigned int to_submit;
6678 int ret = 0;
6679
6680again:
6681 if (!list_empty(&ctx->iopoll_list)) {
6682 unsigned nr_events = 0;
6683
6684 mutex_lock(&ctx->uring_lock);
6685 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6686 io_do_iopoll(ctx, &nr_events, 0);
6687 mutex_unlock(&ctx->uring_lock);
6688 }
6689
6690 to_submit = io_sqring_entries(ctx);
6691
6692 /*
6693 * If submit got -EBUSY, flag us as needing the application
6694 * to enter the kernel to reap and flush events.
6695 */
6696 if (!to_submit || ret == -EBUSY || need_resched()) {
6697 /*
6698 * Drop cur_mm before scheduling, we can't hold it for
6699 * long periods (or over schedule()). Do this before
6700 * adding ourselves to the waitqueue, as the unuse/drop
6701 * may sleep.
6702 */
6703 io_sq_thread_drop_mm();
6704
6705 /*
6706 * We're polling. If we're within the defined idle
6707 * period, then let us spin without work before going
6708 * to sleep. The exception is if we got EBUSY doing
6709 * more IO, we should wait for the application to
6710 * reap events and wake us up.
6711 */
6712 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6713 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6714 !percpu_ref_is_dying(&ctx->refs)))
6715 return SQT_SPIN;
6716
Jens Axboe534ca6d2020-09-02 13:52:19 -06006717 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006718 TASK_INTERRUPTIBLE);
6719
6720 /*
6721 * While doing polled IO, before going to sleep, we need
6722 * to check if there are new reqs added to iopoll_list,
6723 * it is because reqs may have been punted to io worker
6724 * and will be added to iopoll_list later, hence check
6725 * the iopoll_list again.
6726 */
6727 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6728 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006729 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006730 goto again;
6731 }
6732
Jens Axboec8d1ba52020-09-14 11:07:26 -06006733 to_submit = io_sqring_entries(ctx);
6734 if (!to_submit || ret == -EBUSY)
6735 return SQT_IDLE;
6736 }
6737
Jens Axboe534ca6d2020-09-02 13:52:19 -06006738 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006739 io_ring_clear_wakeup_flag(ctx);
6740
6741 mutex_lock(&ctx->uring_lock);
6742 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6743 ret = io_submit_sqes(ctx, to_submit);
6744 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006745
6746 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6747 wake_up(&ctx->sqo_sq_wait);
6748
Jens Axboec8d1ba52020-09-14 11:07:26 -06006749 return SQT_DID_WORK;
6750}
6751
Jens Axboe69fb2132020-09-14 11:16:23 -06006752static void io_sqd_init_new(struct io_sq_data *sqd)
6753{
6754 struct io_ring_ctx *ctx;
6755
6756 while (!list_empty(&sqd->ctx_new_list)) {
6757 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6758 init_wait(&ctx->sqo_wait_entry);
6759 ctx->sqo_wait_entry.func = io_sq_wake_function;
6760 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6761 complete(&ctx->sq_thread_comp);
6762 }
6763}
6764
Jens Axboe6c271ce2019-01-10 11:22:30 -07006765static int io_sq_thread(void *data)
6766{
Jens Axboe69fb2132020-09-14 11:16:23 -06006767 const struct cred *old_cred = NULL;
6768 struct io_sq_data *sqd = data;
6769 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006770 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006771
Jens Axboec8d1ba52020-09-14 11:07:26 -06006772 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006773 while (!kthread_should_stop()) {
6774 enum sq_ret ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006775
Jens Axboe69fb2132020-09-14 11:16:23 -06006776 /*
6777 * Any changes to the sqd lists are synchronized through the
6778 * kthread parking. This synchronizes the thread vs users,
6779 * the users are synchronized on the sqd->ctx_lock.
6780 */
6781 if (kthread_should_park())
6782 kthread_parkme();
6783
6784 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6785 io_sqd_init_new(sqd);
6786
6787 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6788 if (current->cred != ctx->creds) {
6789 if (old_cred)
6790 revert_creds(old_cred);
6791 old_cred = override_creds(ctx->creds);
6792 }
6793
6794 ret |= __io_sq_thread(ctx, start_jiffies);
6795
6796 io_sq_thread_drop_mm();
6797 }
6798
6799 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006800 io_run_task_work();
6801 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006802 } else if (ret == SQT_IDLE) {
6803 if (kthread_should_park())
6804 continue;
6805 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6806 io_ring_set_wakeup_flag(ctx);
6807 schedule();
6808 start_jiffies = jiffies;
6809 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6810 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006811 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006812 }
6813
Jens Axboe4c6e2772020-07-01 11:29:10 -06006814 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006815
Jens Axboe69fb2132020-09-14 11:16:23 -06006816 if (old_cred)
6817 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006818
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006819 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006820
Jens Axboe6c271ce2019-01-10 11:22:30 -07006821 return 0;
6822}
6823
Jens Axboebda52162019-09-24 13:47:15 -06006824struct io_wait_queue {
6825 struct wait_queue_entry wq;
6826 struct io_ring_ctx *ctx;
6827 unsigned to_wait;
6828 unsigned nr_timeouts;
6829};
6830
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006831static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006832{
6833 struct io_ring_ctx *ctx = iowq->ctx;
6834
6835 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006836 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006837 * started waiting. For timeouts, we always want to return to userspace,
6838 * regardless of event count.
6839 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006840 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006841 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6842}
6843
6844static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6845 int wake_flags, void *key)
6846{
6847 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6848 wq);
6849
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006850 /* use noflush == true, as we can't safely rely on locking context */
6851 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006852 return -1;
6853
6854 return autoremove_wake_function(curr, mode, wake_flags, key);
6855}
6856
Jens Axboe2b188cc2019-01-07 10:46:33 -07006857/*
6858 * Wait until events become available, if we don't already have some. The
6859 * application must reap them itself, as they reside on the shared cq ring.
6860 */
6861static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6862 const sigset_t __user *sig, size_t sigsz)
6863{
Jens Axboebda52162019-09-24 13:47:15 -06006864 struct io_wait_queue iowq = {
6865 .wq = {
6866 .private = current,
6867 .func = io_wake_function,
6868 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6869 },
6870 .ctx = ctx,
6871 .to_wait = min_events,
6872 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006873 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006874 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006875
Jens Axboeb41e9852020-02-17 09:52:41 -07006876 do {
6877 if (io_cqring_events(ctx, false) >= min_events)
6878 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006879 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006880 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006881 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006882
6883 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006884#ifdef CONFIG_COMPAT
6885 if (in_compat_syscall())
6886 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006887 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006888 else
6889#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006890 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006891
Jens Axboe2b188cc2019-01-07 10:46:33 -07006892 if (ret)
6893 return ret;
6894 }
6895
Jens Axboebda52162019-09-24 13:47:15 -06006896 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006897 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006898 do {
6899 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6900 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006901 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006902 if (io_run_task_work())
6903 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006904 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006905 if (current->jobctl & JOBCTL_TASK_WORK) {
6906 spin_lock_irq(&current->sighand->siglock);
6907 current->jobctl &= ~JOBCTL_TASK_WORK;
6908 recalc_sigpending();
6909 spin_unlock_irq(&current->sighand->siglock);
6910 continue;
6911 }
6912 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006913 break;
6914 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006915 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006916 break;
6917 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006918 } while (1);
6919 finish_wait(&ctx->wait, &iowq.wq);
6920
Jens Axboeb7db41c2020-07-04 08:55:50 -06006921 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006922
Hristo Venev75b28af2019-08-26 17:23:46 +00006923 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006924}
6925
Jens Axboe6b063142019-01-10 22:13:58 -07006926static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6927{
6928#if defined(CONFIG_UNIX)
6929 if (ctx->ring_sock) {
6930 struct sock *sock = ctx->ring_sock->sk;
6931 struct sk_buff *skb;
6932
6933 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6934 kfree_skb(skb);
6935 }
6936#else
6937 int i;
6938
Jens Axboe65e19f52019-10-26 07:20:21 -06006939 for (i = 0; i < ctx->nr_user_files; i++) {
6940 struct file *file;
6941
6942 file = io_file_from_index(ctx, i);
6943 if (file)
6944 fput(file);
6945 }
Jens Axboe6b063142019-01-10 22:13:58 -07006946#endif
6947}
6948
Jens Axboe05f3fb32019-12-09 11:22:50 -07006949static void io_file_ref_kill(struct percpu_ref *ref)
6950{
6951 struct fixed_file_data *data;
6952
6953 data = container_of(ref, struct fixed_file_data, refs);
6954 complete(&data->done);
6955}
6956
Jens Axboe6b063142019-01-10 22:13:58 -07006957static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6958{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006959 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006960 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006961 unsigned nr_tables, i;
6962
Jens Axboe05f3fb32019-12-09 11:22:50 -07006963 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006964 return -ENXIO;
6965
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006966 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006967 if (!list_empty(&data->ref_list))
6968 ref_node = list_first_entry(&data->ref_list,
6969 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006970 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006971 if (ref_node)
6972 percpu_ref_kill(&ref_node->refs);
6973
6974 percpu_ref_kill(&data->refs);
6975
6976 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006977 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006978 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006979
Jens Axboe6b063142019-01-10 22:13:58 -07006980 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006981 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6982 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006983 kfree(data->table[i].files);
6984 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006985 percpu_ref_exit(&data->refs);
6986 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006987 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006988 ctx->nr_user_files = 0;
6989 return 0;
6990}
6991
Jens Axboe534ca6d2020-09-02 13:52:19 -06006992static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006993{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006994 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006995 /*
6996 * The park is a bit of a work-around, without it we get
6997 * warning spews on shutdown with SQPOLL set and affinity
6998 * set to a single CPU.
6999 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007000 if (sqd->thread) {
7001 kthread_park(sqd->thread);
7002 kthread_stop(sqd->thread);
7003 }
7004
7005 kfree(sqd);
7006 }
7007}
7008
Jens Axboeaa061652020-09-02 14:50:27 -06007009static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7010{
7011 struct io_ring_ctx *ctx_attach;
7012 struct io_sq_data *sqd;
7013 struct fd f;
7014
7015 f = fdget(p->wq_fd);
7016 if (!f.file)
7017 return ERR_PTR(-ENXIO);
7018 if (f.file->f_op != &io_uring_fops) {
7019 fdput(f);
7020 return ERR_PTR(-EINVAL);
7021 }
7022
7023 ctx_attach = f.file->private_data;
7024 sqd = ctx_attach->sq_data;
7025 if (!sqd) {
7026 fdput(f);
7027 return ERR_PTR(-EINVAL);
7028 }
7029
7030 refcount_inc(&sqd->refs);
7031 fdput(f);
7032 return sqd;
7033}
7034
Jens Axboe534ca6d2020-09-02 13:52:19 -06007035static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7036{
7037 struct io_sq_data *sqd;
7038
Jens Axboeaa061652020-09-02 14:50:27 -06007039 if (p->flags & IORING_SETUP_ATTACH_WQ)
7040 return io_attach_sq_data(p);
7041
Jens Axboe534ca6d2020-09-02 13:52:19 -06007042 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7043 if (!sqd)
7044 return ERR_PTR(-ENOMEM);
7045
7046 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007047 INIT_LIST_HEAD(&sqd->ctx_list);
7048 INIT_LIST_HEAD(&sqd->ctx_new_list);
7049 mutex_init(&sqd->ctx_lock);
7050 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007051 init_waitqueue_head(&sqd->wait);
7052 return sqd;
7053}
7054
Jens Axboe69fb2132020-09-14 11:16:23 -06007055static void io_sq_thread_unpark(struct io_sq_data *sqd)
7056 __releases(&sqd->lock)
7057{
7058 if (!sqd->thread)
7059 return;
7060 kthread_unpark(sqd->thread);
7061 mutex_unlock(&sqd->lock);
7062}
7063
7064static void io_sq_thread_park(struct io_sq_data *sqd)
7065 __acquires(&sqd->lock)
7066{
7067 if (!sqd->thread)
7068 return;
7069 mutex_lock(&sqd->lock);
7070 kthread_park(sqd->thread);
7071}
7072
Jens Axboe534ca6d2020-09-02 13:52:19 -06007073static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7074{
7075 struct io_sq_data *sqd = ctx->sq_data;
7076
7077 if (sqd) {
7078 if (sqd->thread) {
7079 /*
7080 * We may arrive here from the error branch in
7081 * io_sq_offload_create() where the kthread is created
7082 * without being waked up, thus wake it up now to make
7083 * sure the wait will complete.
7084 */
7085 wake_up_process(sqd->thread);
7086 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007087
7088 io_sq_thread_park(sqd);
7089 }
7090
7091 mutex_lock(&sqd->ctx_lock);
7092 list_del(&ctx->sqd_list);
7093 mutex_unlock(&sqd->ctx_lock);
7094
7095 if (sqd->thread) {
7096 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7097 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007098 }
7099
7100 io_put_sq_data(sqd);
7101 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007102 }
7103}
7104
Jens Axboe6b063142019-01-10 22:13:58 -07007105static void io_finish_async(struct io_ring_ctx *ctx)
7106{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007107 io_sq_thread_stop(ctx);
7108
Jens Axboe561fb042019-10-24 07:25:42 -06007109 if (ctx->io_wq) {
7110 io_wq_destroy(ctx->io_wq);
7111 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007112 }
7113}
7114
7115#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007116/*
7117 * Ensure the UNIX gc is aware of our file set, so we are certain that
7118 * the io_uring can be safely unregistered on process exit, even if we have
7119 * loops in the file referencing.
7120 */
7121static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7122{
7123 struct sock *sk = ctx->ring_sock->sk;
7124 struct scm_fp_list *fpl;
7125 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007126 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007127
Jens Axboe6b063142019-01-10 22:13:58 -07007128 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7129 if (!fpl)
7130 return -ENOMEM;
7131
7132 skb = alloc_skb(0, GFP_KERNEL);
7133 if (!skb) {
7134 kfree(fpl);
7135 return -ENOMEM;
7136 }
7137
7138 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007139
Jens Axboe08a45172019-10-03 08:11:03 -06007140 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007141 fpl->user = get_uid(ctx->user);
7142 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007143 struct file *file = io_file_from_index(ctx, i + offset);
7144
7145 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007146 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007147 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007148 unix_inflight(fpl->user, fpl->fp[nr_files]);
7149 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007150 }
7151
Jens Axboe08a45172019-10-03 08:11:03 -06007152 if (nr_files) {
7153 fpl->max = SCM_MAX_FD;
7154 fpl->count = nr_files;
7155 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007156 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007157 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7158 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007159
Jens Axboe08a45172019-10-03 08:11:03 -06007160 for (i = 0; i < nr_files; i++)
7161 fput(fpl->fp[i]);
7162 } else {
7163 kfree_skb(skb);
7164 kfree(fpl);
7165 }
Jens Axboe6b063142019-01-10 22:13:58 -07007166
7167 return 0;
7168}
7169
7170/*
7171 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7172 * causes regular reference counting to break down. We rely on the UNIX
7173 * garbage collection to take care of this problem for us.
7174 */
7175static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7176{
7177 unsigned left, total;
7178 int ret = 0;
7179
7180 total = 0;
7181 left = ctx->nr_user_files;
7182 while (left) {
7183 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007184
7185 ret = __io_sqe_files_scm(ctx, this_files, total);
7186 if (ret)
7187 break;
7188 left -= this_files;
7189 total += this_files;
7190 }
7191
7192 if (!ret)
7193 return 0;
7194
7195 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007196 struct file *file = io_file_from_index(ctx, total);
7197
7198 if (file)
7199 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007200 total++;
7201 }
7202
7203 return ret;
7204}
7205#else
7206static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7207{
7208 return 0;
7209}
7210#endif
7211
Jens Axboe65e19f52019-10-26 07:20:21 -06007212static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7213 unsigned nr_files)
7214{
7215 int i;
7216
7217 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007218 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007219 unsigned this_files;
7220
7221 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7222 table->files = kcalloc(this_files, sizeof(struct file *),
7223 GFP_KERNEL);
7224 if (!table->files)
7225 break;
7226 nr_files -= this_files;
7227 }
7228
7229 if (i == nr_tables)
7230 return 0;
7231
7232 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007233 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007234 kfree(table->files);
7235 }
7236 return 1;
7237}
7238
Jens Axboe05f3fb32019-12-09 11:22:50 -07007239static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007240{
7241#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007242 struct sock *sock = ctx->ring_sock->sk;
7243 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7244 struct sk_buff *skb;
7245 int i;
7246
7247 __skb_queue_head_init(&list);
7248
7249 /*
7250 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7251 * remove this entry and rearrange the file array.
7252 */
7253 skb = skb_dequeue(head);
7254 while (skb) {
7255 struct scm_fp_list *fp;
7256
7257 fp = UNIXCB(skb).fp;
7258 for (i = 0; i < fp->count; i++) {
7259 int left;
7260
7261 if (fp->fp[i] != file)
7262 continue;
7263
7264 unix_notinflight(fp->user, fp->fp[i]);
7265 left = fp->count - 1 - i;
7266 if (left) {
7267 memmove(&fp->fp[i], &fp->fp[i + 1],
7268 left * sizeof(struct file *));
7269 }
7270 fp->count--;
7271 if (!fp->count) {
7272 kfree_skb(skb);
7273 skb = NULL;
7274 } else {
7275 __skb_queue_tail(&list, skb);
7276 }
7277 fput(file);
7278 file = NULL;
7279 break;
7280 }
7281
7282 if (!file)
7283 break;
7284
7285 __skb_queue_tail(&list, skb);
7286
7287 skb = skb_dequeue(head);
7288 }
7289
7290 if (skb_peek(&list)) {
7291 spin_lock_irq(&head->lock);
7292 while ((skb = __skb_dequeue(&list)) != NULL)
7293 __skb_queue_tail(head, skb);
7294 spin_unlock_irq(&head->lock);
7295 }
7296#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007297 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007298#endif
7299}
7300
Jens Axboe05f3fb32019-12-09 11:22:50 -07007301struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007302 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007303 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007304};
7305
Jens Axboe4a38aed22020-05-14 17:21:15 -06007306static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007307{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007308 struct fixed_file_data *file_data = ref_node->file_data;
7309 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007310 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007311
7312 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007313 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007314 io_ring_file_put(ctx, pfile->file);
7315 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007316 }
7317
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007318 spin_lock(&file_data->lock);
7319 list_del(&ref_node->node);
7320 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007321
Xiaoguang Wang05589552020-03-31 14:05:18 +08007322 percpu_ref_exit(&ref_node->refs);
7323 kfree(ref_node);
7324 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007325}
7326
Jens Axboe4a38aed22020-05-14 17:21:15 -06007327static void io_file_put_work(struct work_struct *work)
7328{
7329 struct io_ring_ctx *ctx;
7330 struct llist_node *node;
7331
7332 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7333 node = llist_del_all(&ctx->file_put_llist);
7334
7335 while (node) {
7336 struct fixed_file_ref_node *ref_node;
7337 struct llist_node *next = node->next;
7338
7339 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7340 __io_file_put_work(ref_node);
7341 node = next;
7342 }
7343}
7344
Jens Axboe05f3fb32019-12-09 11:22:50 -07007345static void io_file_data_ref_zero(struct percpu_ref *ref)
7346{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007347 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007348 struct io_ring_ctx *ctx;
7349 bool first_add;
7350 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007351
Xiaoguang Wang05589552020-03-31 14:05:18 +08007352 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007353 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007354
Jens Axboe4a38aed22020-05-14 17:21:15 -06007355 if (percpu_ref_is_dying(&ctx->file_data->refs))
7356 delay = 0;
7357
7358 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7359 if (!delay)
7360 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7361 else if (first_add)
7362 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007363}
7364
7365static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7366 struct io_ring_ctx *ctx)
7367{
7368 struct fixed_file_ref_node *ref_node;
7369
7370 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7371 if (!ref_node)
7372 return ERR_PTR(-ENOMEM);
7373
7374 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7375 0, GFP_KERNEL)) {
7376 kfree(ref_node);
7377 return ERR_PTR(-ENOMEM);
7378 }
7379 INIT_LIST_HEAD(&ref_node->node);
7380 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007381 ref_node->file_data = ctx->file_data;
7382 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007383}
7384
7385static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7386{
7387 percpu_ref_exit(&ref_node->refs);
7388 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007389}
7390
7391static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7392 unsigned nr_args)
7393{
7394 __s32 __user *fds = (__s32 __user *) arg;
7395 unsigned nr_tables;
7396 struct file *file;
7397 int fd, ret = 0;
7398 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007399 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007400
7401 if (ctx->file_data)
7402 return -EBUSY;
7403 if (!nr_args)
7404 return -EINVAL;
7405 if (nr_args > IORING_MAX_FIXED_FILES)
7406 return -EMFILE;
7407
7408 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7409 if (!ctx->file_data)
7410 return -ENOMEM;
7411 ctx->file_data->ctx = ctx;
7412 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007413 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007414 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007415
7416 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7417 ctx->file_data->table = kcalloc(nr_tables,
7418 sizeof(struct fixed_file_table),
7419 GFP_KERNEL);
7420 if (!ctx->file_data->table) {
7421 kfree(ctx->file_data);
7422 ctx->file_data = NULL;
7423 return -ENOMEM;
7424 }
7425
Xiaoguang Wang05589552020-03-31 14:05:18 +08007426 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007427 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7428 kfree(ctx->file_data->table);
7429 kfree(ctx->file_data);
7430 ctx->file_data = NULL;
7431 return -ENOMEM;
7432 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007433
7434 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7435 percpu_ref_exit(&ctx->file_data->refs);
7436 kfree(ctx->file_data->table);
7437 kfree(ctx->file_data);
7438 ctx->file_data = NULL;
7439 return -ENOMEM;
7440 }
7441
7442 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7443 struct fixed_file_table *table;
7444 unsigned index;
7445
7446 ret = -EFAULT;
7447 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7448 break;
7449 /* allow sparse sets */
7450 if (fd == -1) {
7451 ret = 0;
7452 continue;
7453 }
7454
7455 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7456 index = i & IORING_FILE_TABLE_MASK;
7457 file = fget(fd);
7458
7459 ret = -EBADF;
7460 if (!file)
7461 break;
7462
7463 /*
7464 * Don't allow io_uring instances to be registered. If UNIX
7465 * isn't enabled, then this causes a reference cycle and this
7466 * instance can never get freed. If UNIX is enabled we'll
7467 * handle it just fine, but there's still no point in allowing
7468 * a ring fd as it doesn't support regular read/write anyway.
7469 */
7470 if (file->f_op == &io_uring_fops) {
7471 fput(file);
7472 break;
7473 }
7474 ret = 0;
7475 table->files[index] = file;
7476 }
7477
7478 if (ret) {
7479 for (i = 0; i < ctx->nr_user_files; i++) {
7480 file = io_file_from_index(ctx, i);
7481 if (file)
7482 fput(file);
7483 }
7484 for (i = 0; i < nr_tables; i++)
7485 kfree(ctx->file_data->table[i].files);
7486
Yang Yingliang667e57d2020-07-10 14:14:20 +00007487 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007488 kfree(ctx->file_data->table);
7489 kfree(ctx->file_data);
7490 ctx->file_data = NULL;
7491 ctx->nr_user_files = 0;
7492 return ret;
7493 }
7494
7495 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007496 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007497 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007498 return ret;
7499 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007500
Xiaoguang Wang05589552020-03-31 14:05:18 +08007501 ref_node = alloc_fixed_file_ref_node(ctx);
7502 if (IS_ERR(ref_node)) {
7503 io_sqe_files_unregister(ctx);
7504 return PTR_ERR(ref_node);
7505 }
7506
7507 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007508 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007509 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007510 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007511 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007512 return ret;
7513}
7514
Jens Axboec3a31e62019-10-03 13:59:56 -06007515static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7516 int index)
7517{
7518#if defined(CONFIG_UNIX)
7519 struct sock *sock = ctx->ring_sock->sk;
7520 struct sk_buff_head *head = &sock->sk_receive_queue;
7521 struct sk_buff *skb;
7522
7523 /*
7524 * See if we can merge this file into an existing skb SCM_RIGHTS
7525 * file set. If there's no room, fall back to allocating a new skb
7526 * and filling it in.
7527 */
7528 spin_lock_irq(&head->lock);
7529 skb = skb_peek(head);
7530 if (skb) {
7531 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7532
7533 if (fpl->count < SCM_MAX_FD) {
7534 __skb_unlink(skb, head);
7535 spin_unlock_irq(&head->lock);
7536 fpl->fp[fpl->count] = get_file(file);
7537 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7538 fpl->count++;
7539 spin_lock_irq(&head->lock);
7540 __skb_queue_head(head, skb);
7541 } else {
7542 skb = NULL;
7543 }
7544 }
7545 spin_unlock_irq(&head->lock);
7546
7547 if (skb) {
7548 fput(file);
7549 return 0;
7550 }
7551
7552 return __io_sqe_files_scm(ctx, 1, index);
7553#else
7554 return 0;
7555#endif
7556}
7557
Hillf Dantona5318d32020-03-23 17:47:15 +08007558static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007559 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007560{
Hillf Dantona5318d32020-03-23 17:47:15 +08007561 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007562 struct percpu_ref *refs = data->cur_refs;
7563 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007564
Jens Axboe05f3fb32019-12-09 11:22:50 -07007565 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007566 if (!pfile)
7567 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007568
Xiaoguang Wang05589552020-03-31 14:05:18 +08007569 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007570 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007571 list_add(&pfile->list, &ref_node->file_list);
7572
Hillf Dantona5318d32020-03-23 17:47:15 +08007573 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007574}
7575
7576static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7577 struct io_uring_files_update *up,
7578 unsigned nr_args)
7579{
7580 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007581 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007582 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007583 __s32 __user *fds;
7584 int fd, i, err;
7585 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007586 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007587
Jens Axboe05f3fb32019-12-09 11:22:50 -07007588 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007589 return -EOVERFLOW;
7590 if (done > ctx->nr_user_files)
7591 return -EINVAL;
7592
Xiaoguang Wang05589552020-03-31 14:05:18 +08007593 ref_node = alloc_fixed_file_ref_node(ctx);
7594 if (IS_ERR(ref_node))
7595 return PTR_ERR(ref_node);
7596
Jens Axboec3a31e62019-10-03 13:59:56 -06007597 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007598 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007599 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007600 struct fixed_file_table *table;
7601 unsigned index;
7602
Jens Axboec3a31e62019-10-03 13:59:56 -06007603 err = 0;
7604 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7605 err = -EFAULT;
7606 break;
7607 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007608 i = array_index_nospec(up->offset, ctx->nr_user_files);
7609 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007610 index = i & IORING_FILE_TABLE_MASK;
7611 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007612 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007613 err = io_queue_file_removal(data, file);
7614 if (err)
7615 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007616 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007617 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007618 }
7619 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007620 file = fget(fd);
7621 if (!file) {
7622 err = -EBADF;
7623 break;
7624 }
7625 /*
7626 * Don't allow io_uring instances to be registered. If
7627 * UNIX isn't enabled, then this causes a reference
7628 * cycle and this instance can never get freed. If UNIX
7629 * is enabled we'll handle it just fine, but there's
7630 * still no point in allowing a ring fd as it doesn't
7631 * support regular read/write anyway.
7632 */
7633 if (file->f_op == &io_uring_fops) {
7634 fput(file);
7635 err = -EBADF;
7636 break;
7637 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007638 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007639 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007640 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007641 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007642 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007643 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007644 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007645 }
7646 nr_args--;
7647 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007648 up->offset++;
7649 }
7650
Xiaoguang Wang05589552020-03-31 14:05:18 +08007651 if (needs_switch) {
7652 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007653 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007654 list_add(&ref_node->node, &data->ref_list);
7655 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007656 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007657 percpu_ref_get(&ctx->file_data->refs);
7658 } else
7659 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007660
7661 return done ? done : err;
7662}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007663
Jens Axboe05f3fb32019-12-09 11:22:50 -07007664static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7665 unsigned nr_args)
7666{
7667 struct io_uring_files_update up;
7668
7669 if (!ctx->file_data)
7670 return -ENXIO;
7671 if (!nr_args)
7672 return -EINVAL;
7673 if (copy_from_user(&up, arg, sizeof(up)))
7674 return -EFAULT;
7675 if (up.resv)
7676 return -EINVAL;
7677
7678 return __io_sqe_files_update(ctx, &up, nr_args);
7679}
Jens Axboec3a31e62019-10-03 13:59:56 -06007680
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007681static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007682{
7683 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7684
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007685 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007686 io_put_req(req);
7687}
7688
Pavel Begunkov24369c22020-01-28 03:15:48 +03007689static int io_init_wq_offload(struct io_ring_ctx *ctx,
7690 struct io_uring_params *p)
7691{
7692 struct io_wq_data data;
7693 struct fd f;
7694 struct io_ring_ctx *ctx_attach;
7695 unsigned int concurrency;
7696 int ret = 0;
7697
7698 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007699 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007700 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007701
7702 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7703 /* Do QD, or 4 * CPUS, whatever is smallest */
7704 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7705
7706 ctx->io_wq = io_wq_create(concurrency, &data);
7707 if (IS_ERR(ctx->io_wq)) {
7708 ret = PTR_ERR(ctx->io_wq);
7709 ctx->io_wq = NULL;
7710 }
7711 return ret;
7712 }
7713
7714 f = fdget(p->wq_fd);
7715 if (!f.file)
7716 return -EBADF;
7717
7718 if (f.file->f_op != &io_uring_fops) {
7719 ret = -EINVAL;
7720 goto out_fput;
7721 }
7722
7723 ctx_attach = f.file->private_data;
7724 /* @io_wq is protected by holding the fd */
7725 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7726 ret = -EINVAL;
7727 goto out_fput;
7728 }
7729
7730 ctx->io_wq = ctx_attach->io_wq;
7731out_fput:
7732 fdput(f);
7733 return ret;
7734}
7735
Jens Axboe0f212202020-09-13 13:09:39 -06007736static int io_uring_alloc_task_context(struct task_struct *task)
7737{
7738 struct io_uring_task *tctx;
7739
7740 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7741 if (unlikely(!tctx))
7742 return -ENOMEM;
7743
7744 xa_init(&tctx->xa);
7745 init_waitqueue_head(&tctx->wait);
7746 tctx->last = NULL;
7747 tctx->in_idle = 0;
7748 atomic_long_set(&tctx->req_issue, 0);
7749 atomic_long_set(&tctx->req_complete, 0);
7750 task->io_uring = tctx;
7751 return 0;
7752}
7753
7754void __io_uring_free(struct task_struct *tsk)
7755{
7756 struct io_uring_task *tctx = tsk->io_uring;
7757
7758 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7759 xa_destroy(&tctx->xa);
7760 kfree(tctx);
7761 tsk->io_uring = NULL;
7762}
7763
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007764static int io_sq_offload_create(struct io_ring_ctx *ctx,
7765 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007766{
7767 int ret;
7768
Jens Axboe6c271ce2019-01-10 11:22:30 -07007769 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007770 struct io_sq_data *sqd;
7771
Jens Axboe3ec482d2019-04-08 10:51:01 -06007772 ret = -EPERM;
7773 if (!capable(CAP_SYS_ADMIN))
7774 goto err;
7775
Jens Axboe534ca6d2020-09-02 13:52:19 -06007776 sqd = io_get_sq_data(p);
7777 if (IS_ERR(sqd)) {
7778 ret = PTR_ERR(sqd);
7779 goto err;
7780 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007781
Jens Axboe534ca6d2020-09-02 13:52:19 -06007782 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007783 io_sq_thread_park(sqd);
7784 mutex_lock(&sqd->ctx_lock);
7785 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7786 mutex_unlock(&sqd->ctx_lock);
7787 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007788
Jens Axboe917257d2019-04-13 09:28:55 -06007789 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7790 if (!ctx->sq_thread_idle)
7791 ctx->sq_thread_idle = HZ;
7792
Jens Axboeaa061652020-09-02 14:50:27 -06007793 if (sqd->thread)
7794 goto done;
7795
Jens Axboe6c271ce2019-01-10 11:22:30 -07007796 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007797 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007798
Jens Axboe917257d2019-04-13 09:28:55 -06007799 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007800 if (cpu >= nr_cpu_ids)
7801 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007802 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007803 goto err;
7804
Jens Axboe69fb2132020-09-14 11:16:23 -06007805 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007806 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007807 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007808 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007809 "io_uring-sq");
7810 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007811 if (IS_ERR(sqd->thread)) {
7812 ret = PTR_ERR(sqd->thread);
7813 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007814 goto err;
7815 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007816 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007817 if (ret)
7818 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007819 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7820 /* Can't have SQ_AFF without SQPOLL */
7821 ret = -EINVAL;
7822 goto err;
7823 }
7824
Jens Axboeaa061652020-09-02 14:50:27 -06007825done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007826 ret = io_init_wq_offload(ctx, p);
7827 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007828 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007829
7830 return 0;
7831err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007832 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007833 return ret;
7834}
7835
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007836static void io_sq_offload_start(struct io_ring_ctx *ctx)
7837{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007838 struct io_sq_data *sqd = ctx->sq_data;
7839
7840 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7841 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007842}
7843
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007844static inline void __io_unaccount_mem(struct user_struct *user,
7845 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007846{
7847 atomic_long_sub(nr_pages, &user->locked_vm);
7848}
7849
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007850static inline int __io_account_mem(struct user_struct *user,
7851 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007852{
7853 unsigned long page_limit, cur_pages, new_pages;
7854
7855 /* Don't allow more pages than we can safely lock */
7856 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7857
7858 do {
7859 cur_pages = atomic_long_read(&user->locked_vm);
7860 new_pages = cur_pages + nr_pages;
7861 if (new_pages > page_limit)
7862 return -ENOMEM;
7863 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7864 new_pages) != cur_pages);
7865
7866 return 0;
7867}
7868
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007869static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7870 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007871{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007872 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007873 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007874
Jens Axboe2aede0e2020-09-14 10:45:53 -06007875 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007876 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007877 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007878 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007879 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007880 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007881}
7882
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007883static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7884 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007885{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007886 int ret;
7887
7888 if (ctx->limit_mem) {
7889 ret = __io_account_mem(ctx->user, nr_pages);
7890 if (ret)
7891 return ret;
7892 }
7893
Jens Axboe2aede0e2020-09-14 10:45:53 -06007894 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007895 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007896 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007897 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007898 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007899 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007900
7901 return 0;
7902}
7903
Jens Axboe2b188cc2019-01-07 10:46:33 -07007904static void io_mem_free(void *ptr)
7905{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007906 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007907
Mark Rutland52e04ef2019-04-30 17:30:21 +01007908 if (!ptr)
7909 return;
7910
7911 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007912 if (put_page_testzero(page))
7913 free_compound_page(page);
7914}
7915
7916static void *io_mem_alloc(size_t size)
7917{
7918 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7919 __GFP_NORETRY;
7920
7921 return (void *) __get_free_pages(gfp_flags, get_order(size));
7922}
7923
Hristo Venev75b28af2019-08-26 17:23:46 +00007924static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7925 size_t *sq_offset)
7926{
7927 struct io_rings *rings;
7928 size_t off, sq_array_size;
7929
7930 off = struct_size(rings, cqes, cq_entries);
7931 if (off == SIZE_MAX)
7932 return SIZE_MAX;
7933
7934#ifdef CONFIG_SMP
7935 off = ALIGN(off, SMP_CACHE_BYTES);
7936 if (off == 0)
7937 return SIZE_MAX;
7938#endif
7939
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007940 if (sq_offset)
7941 *sq_offset = off;
7942
Hristo Venev75b28af2019-08-26 17:23:46 +00007943 sq_array_size = array_size(sizeof(u32), sq_entries);
7944 if (sq_array_size == SIZE_MAX)
7945 return SIZE_MAX;
7946
7947 if (check_add_overflow(off, sq_array_size, &off))
7948 return SIZE_MAX;
7949
Hristo Venev75b28af2019-08-26 17:23:46 +00007950 return off;
7951}
7952
Jens Axboe2b188cc2019-01-07 10:46:33 -07007953static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7954{
Hristo Venev75b28af2019-08-26 17:23:46 +00007955 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007956
Hristo Venev75b28af2019-08-26 17:23:46 +00007957 pages = (size_t)1 << get_order(
7958 rings_size(sq_entries, cq_entries, NULL));
7959 pages += (size_t)1 << get_order(
7960 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007961
Hristo Venev75b28af2019-08-26 17:23:46 +00007962 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007963}
7964
Jens Axboeedafcce2019-01-09 09:16:05 -07007965static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7966{
7967 int i, j;
7968
7969 if (!ctx->user_bufs)
7970 return -ENXIO;
7971
7972 for (i = 0; i < ctx->nr_user_bufs; i++) {
7973 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7974
7975 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007976 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007977
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007978 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007979 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007980 imu->nr_bvecs = 0;
7981 }
7982
7983 kfree(ctx->user_bufs);
7984 ctx->user_bufs = NULL;
7985 ctx->nr_user_bufs = 0;
7986 return 0;
7987}
7988
7989static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7990 void __user *arg, unsigned index)
7991{
7992 struct iovec __user *src;
7993
7994#ifdef CONFIG_COMPAT
7995 if (ctx->compat) {
7996 struct compat_iovec __user *ciovs;
7997 struct compat_iovec ciov;
7998
7999 ciovs = (struct compat_iovec __user *) arg;
8000 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8001 return -EFAULT;
8002
Jens Axboed55e5f52019-12-11 16:12:15 -07008003 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008004 dst->iov_len = ciov.iov_len;
8005 return 0;
8006 }
8007#endif
8008 src = (struct iovec __user *) arg;
8009 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8010 return -EFAULT;
8011 return 0;
8012}
8013
8014static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8015 unsigned nr_args)
8016{
8017 struct vm_area_struct **vmas = NULL;
8018 struct page **pages = NULL;
8019 int i, j, got_pages = 0;
8020 int ret = -EINVAL;
8021
8022 if (ctx->user_bufs)
8023 return -EBUSY;
8024 if (!nr_args || nr_args > UIO_MAXIOV)
8025 return -EINVAL;
8026
8027 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8028 GFP_KERNEL);
8029 if (!ctx->user_bufs)
8030 return -ENOMEM;
8031
8032 for (i = 0; i < nr_args; i++) {
8033 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8034 unsigned long off, start, end, ubuf;
8035 int pret, nr_pages;
8036 struct iovec iov;
8037 size_t size;
8038
8039 ret = io_copy_iov(ctx, &iov, arg, i);
8040 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008041 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008042
8043 /*
8044 * Don't impose further limits on the size and buffer
8045 * constraints here, we'll -EINVAL later when IO is
8046 * submitted if they are wrong.
8047 */
8048 ret = -EFAULT;
8049 if (!iov.iov_base || !iov.iov_len)
8050 goto err;
8051
8052 /* arbitrary limit, but we need something */
8053 if (iov.iov_len > SZ_1G)
8054 goto err;
8055
8056 ubuf = (unsigned long) iov.iov_base;
8057 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8058 start = ubuf >> PAGE_SHIFT;
8059 nr_pages = end - start;
8060
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008061 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008062 if (ret)
8063 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008064
8065 ret = 0;
8066 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008067 kvfree(vmas);
8068 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008069 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008070 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008071 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008072 sizeof(struct vm_area_struct *),
8073 GFP_KERNEL);
8074 if (!pages || !vmas) {
8075 ret = -ENOMEM;
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 got_pages = nr_pages;
8080 }
8081
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008082 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008083 GFP_KERNEL);
8084 ret = -ENOMEM;
8085 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008086 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008087 goto err;
8088 }
8089
8090 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008091 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008092 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008093 FOLL_WRITE | FOLL_LONGTERM,
8094 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008095 if (pret == nr_pages) {
8096 /* don't support file backed memory */
8097 for (j = 0; j < nr_pages; j++) {
8098 struct vm_area_struct *vma = vmas[j];
8099
8100 if (vma->vm_file &&
8101 !is_file_hugepages(vma->vm_file)) {
8102 ret = -EOPNOTSUPP;
8103 break;
8104 }
8105 }
8106 } else {
8107 ret = pret < 0 ? pret : -EFAULT;
8108 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008109 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008110 if (ret) {
8111 /*
8112 * if we did partial map, or found file backed vmas,
8113 * release any pages we did get
8114 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008115 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008116 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008117 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008118 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008119 goto err;
8120 }
8121
8122 off = ubuf & ~PAGE_MASK;
8123 size = iov.iov_len;
8124 for (j = 0; j < nr_pages; j++) {
8125 size_t vec_len;
8126
8127 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8128 imu->bvec[j].bv_page = pages[j];
8129 imu->bvec[j].bv_len = vec_len;
8130 imu->bvec[j].bv_offset = off;
8131 off = 0;
8132 size -= vec_len;
8133 }
8134 /* store original address for later verification */
8135 imu->ubuf = ubuf;
8136 imu->len = iov.iov_len;
8137 imu->nr_bvecs = nr_pages;
8138
8139 ctx->nr_user_bufs++;
8140 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008141 kvfree(pages);
8142 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008143 return 0;
8144err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008145 kvfree(pages);
8146 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008147 io_sqe_buffer_unregister(ctx);
8148 return ret;
8149}
8150
Jens Axboe9b402842019-04-11 11:45:41 -06008151static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8152{
8153 __s32 __user *fds = arg;
8154 int fd;
8155
8156 if (ctx->cq_ev_fd)
8157 return -EBUSY;
8158
8159 if (copy_from_user(&fd, fds, sizeof(*fds)))
8160 return -EFAULT;
8161
8162 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8163 if (IS_ERR(ctx->cq_ev_fd)) {
8164 int ret = PTR_ERR(ctx->cq_ev_fd);
8165 ctx->cq_ev_fd = NULL;
8166 return ret;
8167 }
8168
8169 return 0;
8170}
8171
8172static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8173{
8174 if (ctx->cq_ev_fd) {
8175 eventfd_ctx_put(ctx->cq_ev_fd);
8176 ctx->cq_ev_fd = NULL;
8177 return 0;
8178 }
8179
8180 return -ENXIO;
8181}
8182
Jens Axboe5a2e7452020-02-23 16:23:11 -07008183static int __io_destroy_buffers(int id, void *p, void *data)
8184{
8185 struct io_ring_ctx *ctx = data;
8186 struct io_buffer *buf = p;
8187
Jens Axboe067524e2020-03-02 16:32:28 -07008188 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008189 return 0;
8190}
8191
8192static void io_destroy_buffers(struct io_ring_ctx *ctx)
8193{
8194 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8195 idr_destroy(&ctx->io_buffer_idr);
8196}
8197
Jens Axboe2b188cc2019-01-07 10:46:33 -07008198static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8199{
Jens Axboe6b063142019-01-10 22:13:58 -07008200 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008201 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008202
8203 if (ctx->sqo_task) {
8204 put_task_struct(ctx->sqo_task);
8205 ctx->sqo_task = NULL;
8206 mmdrop(ctx->mm_account);
8207 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008208 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008209
Jens Axboe6b063142019-01-10 22:13:58 -07008210 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008211 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008212 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008213 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008214
Jens Axboe2b188cc2019-01-07 10:46:33 -07008215#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008216 if (ctx->ring_sock) {
8217 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008218 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008219 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008220#endif
8221
Hristo Venev75b28af2019-08-26 17:23:46 +00008222 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008223 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008224
8225 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008226 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008227 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008228 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008229 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008230 kfree(ctx);
8231}
8232
8233static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8234{
8235 struct io_ring_ctx *ctx = file->private_data;
8236 __poll_t mask = 0;
8237
8238 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008239 /*
8240 * synchronizes with barrier from wq_has_sleeper call in
8241 * io_commit_cqring
8242 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008243 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008244 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008245 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008246 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008247 mask |= EPOLLIN | EPOLLRDNORM;
8248
8249 return mask;
8250}
8251
8252static int io_uring_fasync(int fd, struct file *file, int on)
8253{
8254 struct io_ring_ctx *ctx = file->private_data;
8255
8256 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8257}
8258
Jens Axboe071698e2020-01-28 10:04:42 -07008259static int io_remove_personalities(int id, void *p, void *data)
8260{
8261 struct io_ring_ctx *ctx = data;
8262 const struct cred *cred;
8263
8264 cred = idr_remove(&ctx->personality_idr, id);
8265 if (cred)
8266 put_cred(cred);
8267 return 0;
8268}
8269
Jens Axboe85faa7b2020-04-09 18:14:00 -06008270static void io_ring_exit_work(struct work_struct *work)
8271{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008272 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8273 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008274
Jens Axboe56952e92020-06-17 15:00:04 -06008275 /*
8276 * If we're doing polled IO and end up having requests being
8277 * submitted async (out-of-line), then completions can come in while
8278 * we're waiting for refs to drop. We need to reap these manually,
8279 * as nobody else will be looking for them.
8280 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008281 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008282 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008283 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008284 io_iopoll_try_reap_events(ctx);
8285 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008286 io_ring_ctx_free(ctx);
8287}
8288
Jens Axboe2b188cc2019-01-07 10:46:33 -07008289static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8290{
8291 mutex_lock(&ctx->uring_lock);
8292 percpu_ref_kill(&ctx->refs);
8293 mutex_unlock(&ctx->uring_lock);
8294
Jens Axboef3606e32020-09-22 08:18:24 -06008295 io_kill_timeouts(ctx, NULL);
8296 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008297
8298 if (ctx->io_wq)
8299 io_wq_cancel_all(ctx->io_wq);
8300
Jens Axboe15dff282019-11-13 09:09:23 -07008301 /* if we failed setting up the ctx, we might not have any rings */
8302 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008303 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008304 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008305 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008306
8307 /*
8308 * Do this upfront, so we won't have a grace period where the ring
8309 * is closed but resources aren't reaped yet. This can cause
8310 * spurious failure in setting up a new ring.
8311 */
Jens Axboe760618f2020-07-24 12:53:31 -06008312 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8313 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008314
Jens Axboe85faa7b2020-04-09 18:14:00 -06008315 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008316 /*
8317 * Use system_unbound_wq to avoid spawning tons of event kworkers
8318 * if we're exiting a ton of rings at the same time. It just adds
8319 * noise and overhead, there's no discernable change in runtime
8320 * over using system_wq.
8321 */
8322 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008323}
8324
8325static int io_uring_release(struct inode *inode, struct file *file)
8326{
8327 struct io_ring_ctx *ctx = file->private_data;
8328
8329 file->private_data = NULL;
8330 io_ring_ctx_wait_and_kill(ctx);
8331 return 0;
8332}
8333
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008334static bool io_wq_files_match(struct io_wq_work *work, void *data)
8335{
8336 struct files_struct *files = data;
8337
Jens Axboe0f212202020-09-13 13:09:39 -06008338 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008339}
8340
Jens Axboef254ac02020-08-12 17:33:30 -06008341/*
8342 * Returns true if 'preq' is the link parent of 'req'
8343 */
8344static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8345{
8346 struct io_kiocb *link;
8347
8348 if (!(preq->flags & REQ_F_LINK_HEAD))
8349 return false;
8350
8351 list_for_each_entry(link, &preq->link_list, link_list) {
8352 if (link == req)
8353 return true;
8354 }
8355
8356 return false;
8357}
8358
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008359static bool io_match_link_files(struct io_kiocb *req,
8360 struct files_struct *files)
8361{
8362 struct io_kiocb *link;
8363
8364 if (io_match_files(req, files))
8365 return true;
8366 if (req->flags & REQ_F_LINK_HEAD) {
8367 list_for_each_entry(link, &req->link_list, link_list) {
8368 if (io_match_files(link, files))
8369 return true;
8370 }
8371 }
8372 return false;
8373}
8374
Jens Axboef254ac02020-08-12 17:33:30 -06008375/*
8376 * We're looking to cancel 'req' because it's holding on to our files, but
8377 * 'req' could be a link to another request. See if it is, and cancel that
8378 * parent request if so.
8379 */
8380static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8381{
8382 struct hlist_node *tmp;
8383 struct io_kiocb *preq;
8384 bool found = false;
8385 int i;
8386
8387 spin_lock_irq(&ctx->completion_lock);
8388 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8389 struct hlist_head *list;
8390
8391 list = &ctx->cancel_hash[i];
8392 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8393 found = io_match_link(preq, req);
8394 if (found) {
8395 io_poll_remove_one(preq);
8396 break;
8397 }
8398 }
8399 }
8400 spin_unlock_irq(&ctx->completion_lock);
8401 return found;
8402}
8403
8404static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8405 struct io_kiocb *req)
8406{
8407 struct io_kiocb *preq;
8408 bool found = false;
8409
8410 spin_lock_irq(&ctx->completion_lock);
8411 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8412 found = io_match_link(preq, req);
8413 if (found) {
8414 __io_timeout_cancel(preq);
8415 break;
8416 }
8417 }
8418 spin_unlock_irq(&ctx->completion_lock);
8419 return found;
8420}
8421
Jens Axboeb711d4e2020-08-16 08:23:05 -07008422static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8423{
8424 return io_match_link(container_of(work, struct io_kiocb, work), data);
8425}
8426
8427static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8428{
8429 enum io_wq_cancel cret;
8430
8431 /* cancel this particular work, if it's running */
8432 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8433 if (cret != IO_WQ_CANCEL_NOTFOUND)
8434 return;
8435
8436 /* find links that hold this pending, cancel those */
8437 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8438 if (cret != IO_WQ_CANCEL_NOTFOUND)
8439 return;
8440
8441 /* if we have a poll link holding this pending, cancel that */
8442 if (io_poll_remove_link(ctx, req))
8443 return;
8444
8445 /* final option, timeout link is holding this req pending */
8446 io_timeout_remove_link(ctx, req);
8447}
8448
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008449static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8450 struct files_struct *files)
8451{
8452 struct io_defer_entry *de = NULL;
8453 LIST_HEAD(list);
8454
8455 spin_lock_irq(&ctx->completion_lock);
8456 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008457 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008458 list_cut_position(&list, &ctx->defer_list, &de->list);
8459 break;
8460 }
8461 }
8462 spin_unlock_irq(&ctx->completion_lock);
8463
8464 while (!list_empty(&list)) {
8465 de = list_first_entry(&list, struct io_defer_entry, list);
8466 list_del_init(&de->list);
8467 req_set_fail_links(de->req);
8468 io_put_req(de->req);
8469 io_req_complete(de->req, -ECANCELED);
8470 kfree(de);
8471 }
8472}
8473
Jens Axboe76e1b642020-09-26 15:05:03 -06008474/*
8475 * Returns true if we found and killed one or more files pinning requests
8476 */
8477static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008478 struct files_struct *files)
8479{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008480 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008481 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008482
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008483 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008484 /* cancel all at once, should be faster than doing it one by one*/
8485 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8486
Jens Axboefcb323c2019-10-24 12:39:47 -06008487 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008488 struct io_kiocb *cancel_req = NULL, *req;
8489 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008490
8491 spin_lock_irq(&ctx->inflight_lock);
8492 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008493 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008494 continue;
8495 /* req is being completed, ignore */
8496 if (!refcount_inc_not_zero(&req->refs))
8497 continue;
8498 cancel_req = req;
8499 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008500 }
Jens Axboe768134d2019-11-10 20:30:53 -07008501 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008502 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008503 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008504 spin_unlock_irq(&ctx->inflight_lock);
8505
Jens Axboe768134d2019-11-10 20:30:53 -07008506 /* We need to keep going until we don't find a matching req */
8507 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008508 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008509 /* cancel this request, or head link requests */
8510 io_attempt_cancel(ctx, cancel_req);
8511 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008512 /* cancellations _may_ trigger task work */
8513 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008514 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008515 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008516 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008517
8518 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008519}
8520
Pavel Begunkov801dd572020-06-15 10:33:14 +03008521static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008522{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008523 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8524 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008525
Jens Axboef3606e32020-09-22 08:18:24 -06008526 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008527}
8528
Jens Axboe0f212202020-09-13 13:09:39 -06008529static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8530 struct task_struct *task,
8531 struct files_struct *files)
8532{
8533 bool ret;
8534
8535 ret = io_uring_cancel_files(ctx, files);
8536 if (!files) {
8537 enum io_wq_cancel cret;
8538
8539 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8540 if (cret != IO_WQ_CANCEL_NOTFOUND)
8541 ret = true;
8542
8543 /* SQPOLL thread does its own polling */
8544 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8545 while (!list_empty_careful(&ctx->iopoll_list)) {
8546 io_iopoll_try_reap_events(ctx);
8547 ret = true;
8548 }
8549 }
8550
8551 ret |= io_poll_remove_all(ctx, task);
8552 ret |= io_kill_timeouts(ctx, task);
8553 }
8554
8555 return ret;
8556}
8557
8558/*
8559 * We need to iteratively cancel requests, in case a request has dependent
8560 * hard links. These persist even for failure of cancelations, hence keep
8561 * looping until none are found.
8562 */
8563static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8564 struct files_struct *files)
8565{
8566 struct task_struct *task = current;
8567
Jens Axboe534ca6d2020-09-02 13:52:19 -06008568 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8569 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008570
8571 io_cqring_overflow_flush(ctx, true, task, files);
8572
8573 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8574 io_run_task_work();
8575 cond_resched();
8576 }
8577}
8578
8579/*
8580 * Note that this task has used io_uring. We use it for cancelation purposes.
8581 */
8582static int io_uring_add_task_file(struct file *file)
8583{
8584 if (unlikely(!current->io_uring)) {
8585 int ret;
8586
8587 ret = io_uring_alloc_task_context(current);
8588 if (unlikely(ret))
8589 return ret;
8590 }
8591 if (current->io_uring->last != file) {
8592 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8593 void *old;
8594
8595 rcu_read_lock();
8596 old = xas_load(&xas);
8597 if (old != file) {
8598 get_file(file);
8599 xas_lock(&xas);
8600 xas_store(&xas, file);
8601 xas_unlock(&xas);
8602 }
8603 rcu_read_unlock();
8604 current->io_uring->last = file;
8605 }
8606
8607 return 0;
8608}
8609
8610/*
8611 * Remove this io_uring_file -> task mapping.
8612 */
8613static void io_uring_del_task_file(struct file *file)
8614{
8615 struct io_uring_task *tctx = current->io_uring;
8616 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8617
8618 if (tctx->last == file)
8619 tctx->last = NULL;
8620
8621 xas_lock(&xas);
8622 file = xas_store(&xas, NULL);
8623 xas_unlock(&xas);
8624
8625 if (file)
8626 fput(file);
8627}
8628
8629static void __io_uring_attempt_task_drop(struct file *file)
8630{
8631 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8632 struct file *old;
8633
8634 rcu_read_lock();
8635 old = xas_load(&xas);
8636 rcu_read_unlock();
8637
8638 if (old == file)
8639 io_uring_del_task_file(file);
8640}
8641
8642/*
8643 * Drop task note for this file if we're the only ones that hold it after
8644 * pending fput()
8645 */
8646static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8647{
8648 if (!current->io_uring)
8649 return;
8650 /*
8651 * fput() is pending, will be 2 if the only other ref is our potential
8652 * task file note. If the task is exiting, drop regardless of count.
8653 */
8654 if (!exiting && atomic_long_read(&file->f_count) != 2)
8655 return;
8656
8657 __io_uring_attempt_task_drop(file);
8658}
8659
8660void __io_uring_files_cancel(struct files_struct *files)
8661{
8662 struct io_uring_task *tctx = current->io_uring;
8663 XA_STATE(xas, &tctx->xa, 0);
8664
8665 /* make sure overflow events are dropped */
8666 tctx->in_idle = true;
8667
8668 do {
8669 struct io_ring_ctx *ctx;
8670 struct file *file;
8671
8672 xas_lock(&xas);
8673 file = xas_next_entry(&xas, ULONG_MAX);
8674 xas_unlock(&xas);
8675
8676 if (!file)
8677 break;
8678
8679 ctx = file->private_data;
8680
8681 io_uring_cancel_task_requests(ctx, files);
8682 if (files)
8683 io_uring_del_task_file(file);
8684 } while (1);
8685}
8686
8687static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8688{
8689 return atomic_long_read(&tctx->req_issue) ==
8690 atomic_long_read(&tctx->req_complete);
8691}
8692
8693/*
8694 * Find any io_uring fd that this task has registered or done IO on, and cancel
8695 * requests.
8696 */
8697void __io_uring_task_cancel(void)
8698{
8699 struct io_uring_task *tctx = current->io_uring;
8700 DEFINE_WAIT(wait);
8701 long completions;
8702
8703 /* make sure overflow events are dropped */
8704 tctx->in_idle = true;
8705
8706 while (!io_uring_task_idle(tctx)) {
8707 /* read completions before cancelations */
8708 completions = atomic_long_read(&tctx->req_complete);
8709 __io_uring_files_cancel(NULL);
8710
8711 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8712
8713 /*
8714 * If we've seen completions, retry. This avoids a race where
8715 * a completion comes in before we did prepare_to_wait().
8716 */
8717 if (completions != atomic_long_read(&tctx->req_complete))
8718 continue;
8719 if (io_uring_task_idle(tctx))
8720 break;
8721 schedule();
8722 }
8723
8724 finish_wait(&tctx->wait, &wait);
8725 tctx->in_idle = false;
8726}
8727
Jens Axboefcb323c2019-10-24 12:39:47 -06008728static int io_uring_flush(struct file *file, void *data)
8729{
8730 struct io_ring_ctx *ctx = file->private_data;
8731
Jens Axboe6ab23142020-02-08 20:23:59 -07008732 /*
8733 * If the task is going away, cancel work it may have pending
8734 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008735 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008736 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008737
Jens Axboe0f212202020-09-13 13:09:39 -06008738 io_uring_cancel_task_requests(ctx, data);
8739 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008740 return 0;
8741}
8742
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008743static void *io_uring_validate_mmap_request(struct file *file,
8744 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008745{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008746 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008747 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008748 struct page *page;
8749 void *ptr;
8750
8751 switch (offset) {
8752 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008753 case IORING_OFF_CQ_RING:
8754 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008755 break;
8756 case IORING_OFF_SQES:
8757 ptr = ctx->sq_sqes;
8758 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008759 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008760 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008761 }
8762
8763 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008764 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008765 return ERR_PTR(-EINVAL);
8766
8767 return ptr;
8768}
8769
8770#ifdef CONFIG_MMU
8771
8772static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8773{
8774 size_t sz = vma->vm_end - vma->vm_start;
8775 unsigned long pfn;
8776 void *ptr;
8777
8778 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8779 if (IS_ERR(ptr))
8780 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008781
8782 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8783 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8784}
8785
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008786#else /* !CONFIG_MMU */
8787
8788static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8789{
8790 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8791}
8792
8793static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8794{
8795 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8796}
8797
8798static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8799 unsigned long addr, unsigned long len,
8800 unsigned long pgoff, unsigned long flags)
8801{
8802 void *ptr;
8803
8804 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8805 if (IS_ERR(ptr))
8806 return PTR_ERR(ptr);
8807
8808 return (unsigned long) ptr;
8809}
8810
8811#endif /* !CONFIG_MMU */
8812
Jens Axboe90554202020-09-03 12:12:41 -06008813static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8814{
8815 DEFINE_WAIT(wait);
8816
8817 do {
8818 if (!io_sqring_full(ctx))
8819 break;
8820
8821 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8822
8823 if (!io_sqring_full(ctx))
8824 break;
8825
8826 schedule();
8827 } while (!signal_pending(current));
8828
8829 finish_wait(&ctx->sqo_sq_wait, &wait);
8830}
8831
Jens Axboe2b188cc2019-01-07 10:46:33 -07008832SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8833 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8834 size_t, sigsz)
8835{
8836 struct io_ring_ctx *ctx;
8837 long ret = -EBADF;
8838 int submitted = 0;
8839 struct fd f;
8840
Jens Axboe4c6e2772020-07-01 11:29:10 -06008841 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008842
Jens Axboe90554202020-09-03 12:12:41 -06008843 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8844 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008845 return -EINVAL;
8846
8847 f = fdget(fd);
8848 if (!f.file)
8849 return -EBADF;
8850
8851 ret = -EOPNOTSUPP;
8852 if (f.file->f_op != &io_uring_fops)
8853 goto out_fput;
8854
8855 ret = -ENXIO;
8856 ctx = f.file->private_data;
8857 if (!percpu_ref_tryget(&ctx->refs))
8858 goto out_fput;
8859
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008860 ret = -EBADFD;
8861 if (ctx->flags & IORING_SETUP_R_DISABLED)
8862 goto out;
8863
Jens Axboe6c271ce2019-01-10 11:22:30 -07008864 /*
8865 * For SQ polling, the thread will do all submissions and completions.
8866 * Just return the requested submit count, and wake the thread if
8867 * we were asked to.
8868 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008869 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008870 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008871 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008872 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008873 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008874 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008875 if (flags & IORING_ENTER_SQ_WAIT)
8876 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008877 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008878 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008879 ret = io_uring_add_task_file(f.file);
8880 if (unlikely(ret))
8881 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008882 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008883 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008884 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008885
8886 if (submitted != to_submit)
8887 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008888 }
8889 if (flags & IORING_ENTER_GETEVENTS) {
8890 min_complete = min(min_complete, ctx->cq_entries);
8891
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008892 /*
8893 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8894 * space applications don't need to do io completion events
8895 * polling again, they can rely on io_sq_thread to do polling
8896 * work, which can reduce cpu usage and uring_lock contention.
8897 */
8898 if (ctx->flags & IORING_SETUP_IOPOLL &&
8899 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008900 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008901 } else {
8902 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8903 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008904 }
8905
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008906out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008907 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008908out_fput:
8909 fdput(f);
8910 return submitted ? submitted : ret;
8911}
8912
Tobias Klauserbebdb652020-02-26 18:38:32 +01008913#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008914static int io_uring_show_cred(int id, void *p, void *data)
8915{
8916 const struct cred *cred = p;
8917 struct seq_file *m = data;
8918 struct user_namespace *uns = seq_user_ns(m);
8919 struct group_info *gi;
8920 kernel_cap_t cap;
8921 unsigned __capi;
8922 int g;
8923
8924 seq_printf(m, "%5d\n", id);
8925 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8926 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8927 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8928 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8929 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8930 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8931 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8932 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8933 seq_puts(m, "\n\tGroups:\t");
8934 gi = cred->group_info;
8935 for (g = 0; g < gi->ngroups; g++) {
8936 seq_put_decimal_ull(m, g ? " " : "",
8937 from_kgid_munged(uns, gi->gid[g]));
8938 }
8939 seq_puts(m, "\n\tCapEff:\t");
8940 cap = cred->cap_effective;
8941 CAP_FOR_EACH_U32(__capi)
8942 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8943 seq_putc(m, '\n');
8944 return 0;
8945}
8946
8947static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8948{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008949 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008950 int i;
8951
Jens Axboefad8e0d2020-09-28 08:57:48 -06008952 /*
8953 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8954 * since fdinfo case grabs it in the opposite direction of normal use
8955 * cases. If we fail to get the lock, we just don't iterate any
8956 * structures that could be going away outside the io_uring mutex.
8957 */
8958 has_lock = mutex_trylock(&ctx->uring_lock);
8959
Jens Axboe87ce9552020-01-30 08:25:34 -07008960 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008961 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008962 struct fixed_file_table *table;
8963 struct file *f;
8964
8965 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8966 f = table->files[i & IORING_FILE_TABLE_MASK];
8967 if (f)
8968 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8969 else
8970 seq_printf(m, "%5u: <none>\n", i);
8971 }
8972 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008973 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008974 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8975
8976 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8977 (unsigned int) buf->len);
8978 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008979 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008980 seq_printf(m, "Personalities:\n");
8981 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8982 }
Jens Axboed7718a92020-02-14 22:23:12 -07008983 seq_printf(m, "PollList:\n");
8984 spin_lock_irq(&ctx->completion_lock);
8985 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8986 struct hlist_head *list = &ctx->cancel_hash[i];
8987 struct io_kiocb *req;
8988
8989 hlist_for_each_entry(req, list, hash_node)
8990 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8991 req->task->task_works != NULL);
8992 }
8993 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008994 if (has_lock)
8995 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008996}
8997
8998static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8999{
9000 struct io_ring_ctx *ctx = f->private_data;
9001
9002 if (percpu_ref_tryget(&ctx->refs)) {
9003 __io_uring_show_fdinfo(ctx, m);
9004 percpu_ref_put(&ctx->refs);
9005 }
9006}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009007#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009008
Jens Axboe2b188cc2019-01-07 10:46:33 -07009009static const struct file_operations io_uring_fops = {
9010 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009011 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009012 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009013#ifndef CONFIG_MMU
9014 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9015 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9016#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009017 .poll = io_uring_poll,
9018 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009019#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009020 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009021#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009022};
9023
9024static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9025 struct io_uring_params *p)
9026{
Hristo Venev75b28af2019-08-26 17:23:46 +00009027 struct io_rings *rings;
9028 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009029
Jens Axboebd740482020-08-05 12:58:23 -06009030 /* make sure these are sane, as we already accounted them */
9031 ctx->sq_entries = p->sq_entries;
9032 ctx->cq_entries = p->cq_entries;
9033
Hristo Venev75b28af2019-08-26 17:23:46 +00009034 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9035 if (size == SIZE_MAX)
9036 return -EOVERFLOW;
9037
9038 rings = io_mem_alloc(size);
9039 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009040 return -ENOMEM;
9041
Hristo Venev75b28af2019-08-26 17:23:46 +00009042 ctx->rings = rings;
9043 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9044 rings->sq_ring_mask = p->sq_entries - 1;
9045 rings->cq_ring_mask = p->cq_entries - 1;
9046 rings->sq_ring_entries = p->sq_entries;
9047 rings->cq_ring_entries = p->cq_entries;
9048 ctx->sq_mask = rings->sq_ring_mask;
9049 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009050
9051 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009052 if (size == SIZE_MAX) {
9053 io_mem_free(ctx->rings);
9054 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009055 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009056 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009057
9058 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009059 if (!ctx->sq_sqes) {
9060 io_mem_free(ctx->rings);
9061 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009062 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009063 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009064
Jens Axboe2b188cc2019-01-07 10:46:33 -07009065 return 0;
9066}
9067
9068/*
9069 * Allocate an anonymous fd, this is what constitutes the application
9070 * visible backing of an io_uring instance. The application mmaps this
9071 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9072 * we have to tie this fd to a socket for file garbage collection purposes.
9073 */
9074static int io_uring_get_fd(struct io_ring_ctx *ctx)
9075{
9076 struct file *file;
9077 int ret;
9078
9079#if defined(CONFIG_UNIX)
9080 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9081 &ctx->ring_sock);
9082 if (ret)
9083 return ret;
9084#endif
9085
9086 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9087 if (ret < 0)
9088 goto err;
9089
9090 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9091 O_RDWR | O_CLOEXEC);
9092 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009093err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009094 put_unused_fd(ret);
9095 ret = PTR_ERR(file);
9096 goto err;
9097 }
9098
9099#if defined(CONFIG_UNIX)
9100 ctx->ring_sock->file = file;
9101#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009102 if (unlikely(io_uring_add_task_file(file))) {
9103 file = ERR_PTR(-ENOMEM);
9104 goto err_fd;
9105 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009106 fd_install(ret, file);
9107 return ret;
9108err:
9109#if defined(CONFIG_UNIX)
9110 sock_release(ctx->ring_sock);
9111 ctx->ring_sock = NULL;
9112#endif
9113 return ret;
9114}
9115
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009116static int io_uring_create(unsigned entries, struct io_uring_params *p,
9117 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009118{
9119 struct user_struct *user = NULL;
9120 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009121 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009122 int ret;
9123
Jens Axboe8110c1a2019-12-28 15:39:54 -07009124 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009125 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009126 if (entries > IORING_MAX_ENTRIES) {
9127 if (!(p->flags & IORING_SETUP_CLAMP))
9128 return -EINVAL;
9129 entries = IORING_MAX_ENTRIES;
9130 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009131
9132 /*
9133 * Use twice as many entries for the CQ ring. It's possible for the
9134 * application to drive a higher depth than the size of the SQ ring,
9135 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009136 * some flexibility in overcommitting a bit. If the application has
9137 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9138 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009139 */
9140 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009141 if (p->flags & IORING_SETUP_CQSIZE) {
9142 /*
9143 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9144 * to a power-of-two, if it isn't already. We do NOT impose
9145 * any cq vs sq ring sizing.
9146 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009147 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009148 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009149 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9150 if (!(p->flags & IORING_SETUP_CLAMP))
9151 return -EINVAL;
9152 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9153 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009154 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9155 } else {
9156 p->cq_entries = 2 * p->sq_entries;
9157 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009158
9159 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009160 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009161
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009162 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009163 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009164 ring_pages(p->sq_entries, p->cq_entries));
9165 if (ret) {
9166 free_uid(user);
9167 return ret;
9168 }
9169 }
9170
9171 ctx = io_ring_ctx_alloc(p);
9172 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009173 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009174 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009175 p->cq_entries));
9176 free_uid(user);
9177 return -ENOMEM;
9178 }
9179 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009180 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009181 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009182
Jens Axboe2aede0e2020-09-14 10:45:53 -06009183 ctx->sqo_task = get_task_struct(current);
9184
9185 /*
9186 * This is just grabbed for accounting purposes. When a process exits,
9187 * the mm is exited and dropped before the files, hence we need to hang
9188 * on to this mm purely for the purposes of being able to unaccount
9189 * memory (locked/pinned vm). It's not used for anything else.
9190 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009191 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009192 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009193
Jens Axboef74441e2020-08-05 13:00:44 -06009194 /*
9195 * Account memory _before_ installing the file descriptor. Once
9196 * the descriptor is installed, it can get closed at any time. Also
9197 * do this before hitting the general error path, as ring freeing
9198 * will un-account as well.
9199 */
9200 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9201 ACCT_LOCKED);
9202 ctx->limit_mem = limit_mem;
9203
Jens Axboe2b188cc2019-01-07 10:46:33 -07009204 ret = io_allocate_scq_urings(ctx, p);
9205 if (ret)
9206 goto err;
9207
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009208 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009209 if (ret)
9210 goto err;
9211
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009212 if (!(p->flags & IORING_SETUP_R_DISABLED))
9213 io_sq_offload_start(ctx);
9214
Jens Axboe2b188cc2019-01-07 10:46:33 -07009215 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009216 p->sq_off.head = offsetof(struct io_rings, sq.head);
9217 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9218 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9219 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9220 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9221 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9222 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009223
9224 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009225 p->cq_off.head = offsetof(struct io_rings, cq.head);
9226 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9227 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9228 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9229 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9230 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009231 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009232
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009233 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9234 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009235 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9236 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009237
9238 if (copy_to_user(params, p, sizeof(*p))) {
9239 ret = -EFAULT;
9240 goto err;
9241 }
Jens Axboed1719f72020-07-30 13:43:53 -06009242
9243 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009244 * Install ring fd as the very last thing, so we don't risk someone
9245 * having closed it before we finish setup
9246 */
9247 ret = io_uring_get_fd(ctx);
9248 if (ret < 0)
9249 goto err;
9250
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009251 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009252 return ret;
9253err:
9254 io_ring_ctx_wait_and_kill(ctx);
9255 return ret;
9256}
9257
9258/*
9259 * Sets up an aio uring context, and returns the fd. Applications asks for a
9260 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9261 * params structure passed in.
9262 */
9263static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9264{
9265 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009266 int i;
9267
9268 if (copy_from_user(&p, params, sizeof(p)))
9269 return -EFAULT;
9270 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9271 if (p.resv[i])
9272 return -EINVAL;
9273 }
9274
Jens Axboe6c271ce2019-01-10 11:22:30 -07009275 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009276 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009277 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9278 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009279 return -EINVAL;
9280
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009281 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009282}
9283
9284SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9285 struct io_uring_params __user *, params)
9286{
9287 return io_uring_setup(entries, params);
9288}
9289
Jens Axboe66f4af92020-01-16 15:36:52 -07009290static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9291{
9292 struct io_uring_probe *p;
9293 size_t size;
9294 int i, ret;
9295
9296 size = struct_size(p, ops, nr_args);
9297 if (size == SIZE_MAX)
9298 return -EOVERFLOW;
9299 p = kzalloc(size, GFP_KERNEL);
9300 if (!p)
9301 return -ENOMEM;
9302
9303 ret = -EFAULT;
9304 if (copy_from_user(p, arg, size))
9305 goto out;
9306 ret = -EINVAL;
9307 if (memchr_inv(p, 0, size))
9308 goto out;
9309
9310 p->last_op = IORING_OP_LAST - 1;
9311 if (nr_args > IORING_OP_LAST)
9312 nr_args = IORING_OP_LAST;
9313
9314 for (i = 0; i < nr_args; i++) {
9315 p->ops[i].op = i;
9316 if (!io_op_defs[i].not_supported)
9317 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9318 }
9319 p->ops_len = i;
9320
9321 ret = 0;
9322 if (copy_to_user(arg, p, size))
9323 ret = -EFAULT;
9324out:
9325 kfree(p);
9326 return ret;
9327}
9328
Jens Axboe071698e2020-01-28 10:04:42 -07009329static int io_register_personality(struct io_ring_ctx *ctx)
9330{
9331 const struct cred *creds = get_current_cred();
9332 int id;
9333
9334 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9335 USHRT_MAX, GFP_KERNEL);
9336 if (id < 0)
9337 put_cred(creds);
9338 return id;
9339}
9340
9341static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9342{
9343 const struct cred *old_creds;
9344
9345 old_creds = idr_remove(&ctx->personality_idr, id);
9346 if (old_creds) {
9347 put_cred(old_creds);
9348 return 0;
9349 }
9350
9351 return -EINVAL;
9352}
9353
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009354static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9355 unsigned int nr_args)
9356{
9357 struct io_uring_restriction *res;
9358 size_t size;
9359 int i, ret;
9360
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009361 /* Restrictions allowed only if rings started disabled */
9362 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9363 return -EBADFD;
9364
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009365 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009366 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009367 return -EBUSY;
9368
9369 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9370 return -EINVAL;
9371
9372 size = array_size(nr_args, sizeof(*res));
9373 if (size == SIZE_MAX)
9374 return -EOVERFLOW;
9375
9376 res = memdup_user(arg, size);
9377 if (IS_ERR(res))
9378 return PTR_ERR(res);
9379
9380 ret = 0;
9381
9382 for (i = 0; i < nr_args; i++) {
9383 switch (res[i].opcode) {
9384 case IORING_RESTRICTION_REGISTER_OP:
9385 if (res[i].register_op >= IORING_REGISTER_LAST) {
9386 ret = -EINVAL;
9387 goto out;
9388 }
9389
9390 __set_bit(res[i].register_op,
9391 ctx->restrictions.register_op);
9392 break;
9393 case IORING_RESTRICTION_SQE_OP:
9394 if (res[i].sqe_op >= IORING_OP_LAST) {
9395 ret = -EINVAL;
9396 goto out;
9397 }
9398
9399 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9400 break;
9401 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9402 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9403 break;
9404 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9405 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9406 break;
9407 default:
9408 ret = -EINVAL;
9409 goto out;
9410 }
9411 }
9412
9413out:
9414 /* Reset all restrictions if an error happened */
9415 if (ret != 0)
9416 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9417 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009418 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009419
9420 kfree(res);
9421 return ret;
9422}
9423
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009424static int io_register_enable_rings(struct io_ring_ctx *ctx)
9425{
9426 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9427 return -EBADFD;
9428
9429 if (ctx->restrictions.registered)
9430 ctx->restricted = 1;
9431
9432 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9433
9434 io_sq_offload_start(ctx);
9435
9436 return 0;
9437}
9438
Jens Axboe071698e2020-01-28 10:04:42 -07009439static bool io_register_op_must_quiesce(int op)
9440{
9441 switch (op) {
9442 case IORING_UNREGISTER_FILES:
9443 case IORING_REGISTER_FILES_UPDATE:
9444 case IORING_REGISTER_PROBE:
9445 case IORING_REGISTER_PERSONALITY:
9446 case IORING_UNREGISTER_PERSONALITY:
9447 return false;
9448 default:
9449 return true;
9450 }
9451}
9452
Jens Axboeedafcce2019-01-09 09:16:05 -07009453static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9454 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009455 __releases(ctx->uring_lock)
9456 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009457{
9458 int ret;
9459
Jens Axboe35fa71a2019-04-22 10:23:23 -06009460 /*
9461 * We're inside the ring mutex, if the ref is already dying, then
9462 * someone else killed the ctx or is already going through
9463 * io_uring_register().
9464 */
9465 if (percpu_ref_is_dying(&ctx->refs))
9466 return -ENXIO;
9467
Jens Axboe071698e2020-01-28 10:04:42 -07009468 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009469 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009470
Jens Axboe05f3fb32019-12-09 11:22:50 -07009471 /*
9472 * Drop uring mutex before waiting for references to exit. If
9473 * another thread is currently inside io_uring_enter() it might
9474 * need to grab the uring_lock to make progress. If we hold it
9475 * here across the drain wait, then we can deadlock. It's safe
9476 * to drop the mutex here, since no new references will come in
9477 * after we've killed the percpu ref.
9478 */
9479 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009480 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009481 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009482 if (ret) {
9483 percpu_ref_resurrect(&ctx->refs);
9484 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009485 goto out_quiesce;
9486 }
9487 }
9488
9489 if (ctx->restricted) {
9490 if (opcode >= IORING_REGISTER_LAST) {
9491 ret = -EINVAL;
9492 goto out;
9493 }
9494
9495 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9496 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009497 goto out;
9498 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009499 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009500
9501 switch (opcode) {
9502 case IORING_REGISTER_BUFFERS:
9503 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9504 break;
9505 case IORING_UNREGISTER_BUFFERS:
9506 ret = -EINVAL;
9507 if (arg || nr_args)
9508 break;
9509 ret = io_sqe_buffer_unregister(ctx);
9510 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009511 case IORING_REGISTER_FILES:
9512 ret = io_sqe_files_register(ctx, arg, nr_args);
9513 break;
9514 case IORING_UNREGISTER_FILES:
9515 ret = -EINVAL;
9516 if (arg || nr_args)
9517 break;
9518 ret = io_sqe_files_unregister(ctx);
9519 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009520 case IORING_REGISTER_FILES_UPDATE:
9521 ret = io_sqe_files_update(ctx, arg, nr_args);
9522 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009523 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009524 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009525 ret = -EINVAL;
9526 if (nr_args != 1)
9527 break;
9528 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009529 if (ret)
9530 break;
9531 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9532 ctx->eventfd_async = 1;
9533 else
9534 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009535 break;
9536 case IORING_UNREGISTER_EVENTFD:
9537 ret = -EINVAL;
9538 if (arg || nr_args)
9539 break;
9540 ret = io_eventfd_unregister(ctx);
9541 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009542 case IORING_REGISTER_PROBE:
9543 ret = -EINVAL;
9544 if (!arg || nr_args > 256)
9545 break;
9546 ret = io_probe(ctx, arg, nr_args);
9547 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009548 case IORING_REGISTER_PERSONALITY:
9549 ret = -EINVAL;
9550 if (arg || nr_args)
9551 break;
9552 ret = io_register_personality(ctx);
9553 break;
9554 case IORING_UNREGISTER_PERSONALITY:
9555 ret = -EINVAL;
9556 if (arg)
9557 break;
9558 ret = io_unregister_personality(ctx, nr_args);
9559 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009560 case IORING_REGISTER_ENABLE_RINGS:
9561 ret = -EINVAL;
9562 if (arg || nr_args)
9563 break;
9564 ret = io_register_enable_rings(ctx);
9565 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009566 case IORING_REGISTER_RESTRICTIONS:
9567 ret = io_register_restrictions(ctx, arg, nr_args);
9568 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009569 default:
9570 ret = -EINVAL;
9571 break;
9572 }
9573
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009574out:
Jens Axboe071698e2020-01-28 10:04:42 -07009575 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009576 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009577 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009578out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009579 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009580 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009581 return ret;
9582}
9583
9584SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9585 void __user *, arg, unsigned int, nr_args)
9586{
9587 struct io_ring_ctx *ctx;
9588 long ret = -EBADF;
9589 struct fd f;
9590
9591 f = fdget(fd);
9592 if (!f.file)
9593 return -EBADF;
9594
9595 ret = -EOPNOTSUPP;
9596 if (f.file->f_op != &io_uring_fops)
9597 goto out_fput;
9598
9599 ctx = f.file->private_data;
9600
9601 mutex_lock(&ctx->uring_lock);
9602 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9603 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009604 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9605 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009606out_fput:
9607 fdput(f);
9608 return ret;
9609}
9610
Jens Axboe2b188cc2019-01-07 10:46:33 -07009611static int __init io_uring_init(void)
9612{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009613#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9614 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9615 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9616} while (0)
9617
9618#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9619 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9620 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9621 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9622 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9623 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9624 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9625 BUILD_BUG_SQE_ELEM(8, __u64, off);
9626 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9627 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009628 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009629 BUILD_BUG_SQE_ELEM(24, __u32, len);
9630 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9631 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9632 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9633 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009634 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9635 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009636 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9637 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9638 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9639 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9640 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9641 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9642 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9643 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009644 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009645 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9646 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9647 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009648 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009649
Jens Axboed3656342019-12-18 09:50:26 -07009650 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009651 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009652 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9653 return 0;
9654};
9655__initcall(io_uring_init);