blob: ed7032bb76bf7fe21b850129b6e84242066aac2e [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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkov9b5f7bd92020-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 Begunkovab0b1962020-09-06 00:45:47 +03003074 iorw->bytes_done = 0;
3075 iorw->free_iovec = iov;
3076 if (iov)
3077 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003078 return 0;
3079}
3080
Jens Axboe3529d8c2019-12-19 18:24:38 -07003081static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3082 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003083{
3084 ssize_t ret;
3085
Jens Axboe3529d8c2019-12-19 18:24:38 -07003086 ret = io_prep_rw(req, sqe, force_nonblock);
3087 if (ret)
3088 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003089
Jens Axboe3529d8c2019-12-19 18:24:38 -07003090 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3091 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003092
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003093 /* either don't need iovec imported or already have it */
3094 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003095 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003096 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003097}
3098
Jens Axboec1dd91d2020-08-03 16:43:59 -06003099/*
3100 * This is our waitqueue callback handler, registered through lock_page_async()
3101 * when we initially tried to do the IO with the iocb armed our waitqueue.
3102 * This gets called when the page is unlocked, and we generally expect that to
3103 * happen when the page IO is completed and the page is now uptodate. This will
3104 * queue a task_work based retry of the operation, attempting to copy the data
3105 * again. If the latter fails because the page was NOT uptodate, then we will
3106 * do a thread based blocking retry of the operation. That's the unexpected
3107 * slow path.
3108 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003109static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3110 int sync, void *arg)
3111{
3112 struct wait_page_queue *wpq;
3113 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003114 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003115 int ret;
3116
3117 wpq = container_of(wait, struct wait_page_queue, wait);
3118
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003119 if (!wake_page_match(wpq, key))
3120 return 0;
3121
Hao Xuc8d317a2020-09-29 20:00:45 +08003122 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003123 list_del_init(&wait->entry);
3124
Pavel Begunkove7375122020-07-12 20:42:04 +03003125 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003126 percpu_ref_get(&req->ctx->refs);
3127
Jens Axboebcf5a062020-05-22 09:24:42 -06003128 /* submit ref gets dropped, acquire a new one */
3129 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003130 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003131 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003132 struct task_struct *tsk;
3133
Jens Axboebcf5a062020-05-22 09:24:42 -06003134 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003135 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003136 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003137 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003138 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003139 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003140 return 1;
3141}
3142
Jens Axboec1dd91d2020-08-03 16:43:59 -06003143/*
3144 * This controls whether a given IO request should be armed for async page
3145 * based retry. If we return false here, the request is handed to the async
3146 * worker threads for retry. If we're doing buffered reads on a regular file,
3147 * we prepare a private wait_page_queue entry and retry the operation. This
3148 * will either succeed because the page is now uptodate and unlocked, or it
3149 * will register a callback when the page is unlocked at IO completion. Through
3150 * that callback, io_uring uses task_work to setup a retry of the operation.
3151 * That retry will attempt the buffered read again. The retry will generally
3152 * succeed, or in rare cases where it fails, we then fall back to using the
3153 * async worker threads for a blocking retry.
3154 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003155static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003156{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003157 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003158 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003159
3160 /* never retry for NOWAIT, we just complete with -EAGAIN */
3161 if (req->flags & REQ_F_NOWAIT)
3162 return false;
3163
Jens Axboe227c0c92020-08-13 11:51:40 -06003164 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003165 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003166 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003167
Jens Axboebcf5a062020-05-22 09:24:42 -06003168 /*
3169 * just use poll if we can, and don't attempt if the fs doesn't
3170 * support callback based unlocks
3171 */
3172 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3173 return false;
3174
Jens Axboe3b2a4432020-08-16 10:58:43 -07003175 wait->wait.func = io_async_buf_func;
3176 wait->wait.private = req;
3177 wait->wait.flags = 0;
3178 INIT_LIST_HEAD(&wait->wait.entry);
3179 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003180 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003181 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003182 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003183}
3184
3185static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3186{
3187 if (req->file->f_op->read_iter)
3188 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003189 else if (req->file->f_op->read)
3190 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3191 else
3192 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003193}
3194
Jens Axboea1d7c392020-06-22 11:09:46 -06003195static int io_read(struct io_kiocb *req, bool force_nonblock,
3196 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003197{
3198 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003199 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003200 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003201 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003202 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003203 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003204
Jens Axboeff6165b2020-08-13 09:47:43 -06003205 if (req->io)
3206 iter = &req->io->rw.iter;
3207
3208 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003209 if (ret < 0)
3210 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003211 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003212 io_size = ret;
3213 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003214 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003215
Jens Axboefd6c2e42019-12-18 12:19:41 -07003216 /* Ensure we clear previously set non-block flag */
3217 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003218 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003219
Pavel Begunkov24c74672020-06-21 13:09:51 +03003220 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003221 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3222 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003223 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003224
Jens Axboe0fef9482020-08-26 10:36:20 -06003225 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003226 if (unlikely(ret))
3227 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003228
Jens Axboe227c0c92020-08-13 11:51:40 -06003229 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003230
Jens Axboe227c0c92020-08-13 11:51:40 -06003231 if (!ret) {
3232 goto done;
3233 } else if (ret == -EIOCBQUEUED) {
3234 ret = 0;
3235 goto out_free;
3236 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003237 /* IOPOLL retry should happen for io-wq threads */
3238 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003239 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003240 /* no retry on NONBLOCK marked file */
3241 if (req->file->f_flags & O_NONBLOCK)
3242 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003243 /* some cases will consume bytes even on error returns */
3244 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003245 ret = 0;
3246 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003247 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003248 /* make sure -ERESTARTSYS -> -EINTR is done */
3249 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003250 }
3251
3252 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003253 if (!iov_iter_count(iter) || !force_nonblock ||
3254 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003255 goto done;
3256
3257 io_size -= ret;
3258copy_iov:
3259 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3260 if (ret2) {
3261 ret = ret2;
3262 goto out_free;
3263 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003264 if (no_async)
3265 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003266 /* it's copied and will be cleaned with ->io */
3267 iovec = NULL;
3268 /* now use our persistent iterator, if we aren't already */
3269 iter = &req->io->rw.iter;
3270retry:
3271 req->io->rw.bytes_done += ret;
3272 /* if we can retry, do so with the callbacks armed */
3273 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003274 kiocb->ki_flags &= ~IOCB_WAITQ;
3275 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003276 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003277
3278 /*
3279 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3280 * get -EIOCBQUEUED, then we'll get a notification when the desired
3281 * page gets unlocked. We can also get a partial read here, and if we
3282 * do, then just retry at the new offset.
3283 */
3284 ret = io_iter_do_read(req, iter);
3285 if (ret == -EIOCBQUEUED) {
3286 ret = 0;
3287 goto out_free;
3288 } else if (ret > 0 && ret < io_size) {
3289 /* we got some bytes, but not all. retry. */
3290 goto retry;
3291 }
3292done:
3293 kiocb_done(kiocb, ret, cs);
3294 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003295out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003296 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003297 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003298 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003299 return ret;
3300}
3301
Jens Axboe3529d8c2019-12-19 18:24:38 -07003302static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3303 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003304{
3305 ssize_t ret;
3306
Jens Axboe3529d8c2019-12-19 18:24:38 -07003307 ret = io_prep_rw(req, sqe, force_nonblock);
3308 if (ret)
3309 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003310
Jens Axboe3529d8c2019-12-19 18:24:38 -07003311 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3312 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003313
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003314 /* either don't need iovec imported or already have it */
3315 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003316 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003317 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003318}
3319
Jens Axboea1d7c392020-06-22 11:09:46 -06003320static int io_write(struct io_kiocb *req, bool force_nonblock,
3321 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003322{
3323 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003324 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003325 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003326 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003327 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003328
Jens Axboeff6165b2020-08-13 09:47:43 -06003329 if (req->io)
3330 iter = &req->io->rw.iter;
3331
3332 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003333 if (ret < 0)
3334 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003335 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003336 io_size = ret;
3337 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003338
Jens Axboefd6c2e42019-12-18 12:19:41 -07003339 /* Ensure we clear previously set non-block flag */
3340 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003341 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003342
Pavel Begunkov24c74672020-06-21 13:09:51 +03003343 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003344 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003345 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003346
Jens Axboe10d59342019-12-09 20:16:22 -07003347 /* file path doesn't support NOWAIT for non-direct_IO */
3348 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3349 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003350 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003351
Jens Axboe0fef9482020-08-26 10:36:20 -06003352 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003353 if (unlikely(ret))
3354 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003355
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003356 /*
3357 * Open-code file_start_write here to grab freeze protection,
3358 * which will be released by another thread in
3359 * io_complete_rw(). Fool lockdep by telling it the lock got
3360 * released so that it doesn't complain about the held lock when
3361 * we return to userspace.
3362 */
3363 if (req->flags & REQ_F_ISREG) {
3364 __sb_start_write(file_inode(req->file)->i_sb,
3365 SB_FREEZE_WRITE, true);
3366 __sb_writers_release(file_inode(req->file)->i_sb,
3367 SB_FREEZE_WRITE);
3368 }
3369 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003370
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003371 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003372 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003373 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003374 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003375 else
3376 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003377
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003378 /*
3379 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3380 * retry them without IOCB_NOWAIT.
3381 */
3382 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3383 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003384 /* no retry on NONBLOCK marked file */
3385 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3386 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003387 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003388 /* IOPOLL retry should happen for io-wq threads */
3389 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3390 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003391done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003392 kiocb_done(kiocb, ret2, cs);
3393 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003394copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003395 /* some cases will consume bytes even on error returns */
3396 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003397 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003398 if (!ret)
3399 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003400 }
Jens Axboe31b51512019-01-18 22:56:34 -07003401out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003402 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003403 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003404 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003405 return ret;
3406}
3407
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003408static int __io_splice_prep(struct io_kiocb *req,
3409 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003410{
3411 struct io_splice* sp = &req->splice;
3412 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3413 int ret;
3414
3415 if (req->flags & REQ_F_NEED_CLEANUP)
3416 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003417 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3418 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003419
3420 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003421 sp->len = READ_ONCE(sqe->len);
3422 sp->flags = READ_ONCE(sqe->splice_flags);
3423
3424 if (unlikely(sp->flags & ~valid_flags))
3425 return -EINVAL;
3426
3427 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3428 (sp->flags & SPLICE_F_FD_IN_FIXED));
3429 if (ret)
3430 return ret;
3431 req->flags |= REQ_F_NEED_CLEANUP;
3432
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003433 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3434 /*
3435 * Splice operation will be punted aync, and here need to
3436 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3437 */
3438 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003439 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003440 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003441
3442 return 0;
3443}
3444
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003445static int io_tee_prep(struct io_kiocb *req,
3446 const struct io_uring_sqe *sqe)
3447{
3448 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3449 return -EINVAL;
3450 return __io_splice_prep(req, sqe);
3451}
3452
3453static int io_tee(struct io_kiocb *req, bool force_nonblock)
3454{
3455 struct io_splice *sp = &req->splice;
3456 struct file *in = sp->file_in;
3457 struct file *out = sp->file_out;
3458 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3459 long ret = 0;
3460
3461 if (force_nonblock)
3462 return -EAGAIN;
3463 if (sp->len)
3464 ret = do_tee(in, out, sp->len, flags);
3465
3466 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3467 req->flags &= ~REQ_F_NEED_CLEANUP;
3468
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003469 if (ret != sp->len)
3470 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003471 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003472 return 0;
3473}
3474
3475static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3476{
3477 struct io_splice* sp = &req->splice;
3478
3479 sp->off_in = READ_ONCE(sqe->splice_off_in);
3480 sp->off_out = READ_ONCE(sqe->off);
3481 return __io_splice_prep(req, sqe);
3482}
3483
Pavel Begunkov014db002020-03-03 21:33:12 +03003484static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003485{
3486 struct io_splice *sp = &req->splice;
3487 struct file *in = sp->file_in;
3488 struct file *out = sp->file_out;
3489 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3490 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003491 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003492
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003493 if (force_nonblock)
3494 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003495
3496 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3497 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003498
Jens Axboe948a7742020-05-17 14:21:38 -06003499 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003500 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003501
3502 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3503 req->flags &= ~REQ_F_NEED_CLEANUP;
3504
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003505 if (ret != sp->len)
3506 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003507 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003508 return 0;
3509}
3510
Jens Axboe2b188cc2019-01-07 10:46:33 -07003511/*
3512 * IORING_OP_NOP just posts a completion event, nothing else.
3513 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003514static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003515{
3516 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003517
Jens Axboedef596e2019-01-09 08:59:42 -07003518 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3519 return -EINVAL;
3520
Jens Axboe229a7b62020-06-22 10:13:11 -06003521 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003522 return 0;
3523}
3524
Jens Axboe3529d8c2019-12-19 18:24:38 -07003525static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003526{
Jens Axboe6b063142019-01-10 22:13:58 -07003527 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003528
Jens Axboe09bb8392019-03-13 12:39:28 -06003529 if (!req->file)
3530 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003531
Jens Axboe6b063142019-01-10 22:13:58 -07003532 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003533 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003534 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003535 return -EINVAL;
3536
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003537 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3538 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3539 return -EINVAL;
3540
3541 req->sync.off = READ_ONCE(sqe->off);
3542 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003543 return 0;
3544}
3545
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003546static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003547{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003548 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003549 int ret;
3550
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003551 /* fsync always requires a blocking context */
3552 if (force_nonblock)
3553 return -EAGAIN;
3554
Jens Axboe9adbd452019-12-20 08:45:55 -07003555 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003556 end > 0 ? end : LLONG_MAX,
3557 req->sync.flags & IORING_FSYNC_DATASYNC);
3558 if (ret < 0)
3559 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003560 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003561 return 0;
3562}
3563
Jens Axboed63d1b52019-12-10 10:38:56 -07003564static int io_fallocate_prep(struct io_kiocb *req,
3565 const struct io_uring_sqe *sqe)
3566{
3567 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3568 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003569 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3570 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003571
3572 req->sync.off = READ_ONCE(sqe->off);
3573 req->sync.len = READ_ONCE(sqe->addr);
3574 req->sync.mode = READ_ONCE(sqe->len);
3575 return 0;
3576}
3577
Pavel Begunkov014db002020-03-03 21:33:12 +03003578static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003579{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003580 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003581
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003582 /* fallocate always requiring blocking context */
3583 if (force_nonblock)
3584 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003585 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3586 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003587 if (ret < 0)
3588 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003589 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003590 return 0;
3591}
3592
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003593static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003594{
Jens Axboef8748882020-01-08 17:47:02 -07003595 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003596 int ret;
3597
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003598 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003599 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003600 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003601 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003602
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003603 /* open.how should be already initialised */
3604 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003605 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003606
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003607 req->open.dfd = READ_ONCE(sqe->fd);
3608 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003609 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003610 if (IS_ERR(req->open.filename)) {
3611 ret = PTR_ERR(req->open.filename);
3612 req->open.filename = NULL;
3613 return ret;
3614 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003615 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003616 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003617 return 0;
3618}
3619
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003620static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3621{
3622 u64 flags, mode;
3623
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003624 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3625 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003626 if (req->flags & REQ_F_NEED_CLEANUP)
3627 return 0;
3628 mode = READ_ONCE(sqe->len);
3629 flags = READ_ONCE(sqe->open_flags);
3630 req->open.how = build_open_how(flags, mode);
3631 return __io_openat_prep(req, sqe);
3632}
3633
Jens Axboecebdb982020-01-08 17:59:24 -07003634static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3635{
3636 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003637 size_t len;
3638 int ret;
3639
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003640 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3641 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003642 if (req->flags & REQ_F_NEED_CLEANUP)
3643 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003644 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3645 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003646 if (len < OPEN_HOW_SIZE_VER0)
3647 return -EINVAL;
3648
3649 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3650 len);
3651 if (ret)
3652 return ret;
3653
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003654 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003655}
3656
Pavel Begunkov014db002020-03-03 21:33:12 +03003657static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003658{
3659 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003660 struct file *file;
3661 int ret;
3662
Jens Axboef86cd202020-01-29 13:46:44 -07003663 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003664 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003665
Jens Axboecebdb982020-01-08 17:59:24 -07003666 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003667 if (ret)
3668 goto err;
3669
Jens Axboe4022e7a2020-03-19 19:23:18 -06003670 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003671 if (ret < 0)
3672 goto err;
3673
3674 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3675 if (IS_ERR(file)) {
3676 put_unused_fd(ret);
3677 ret = PTR_ERR(file);
3678 } else {
3679 fsnotify_open(file);
3680 fd_install(ret, file);
3681 }
3682err:
3683 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003684 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003685 if (ret < 0)
3686 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003687 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003688 return 0;
3689}
3690
Pavel Begunkov014db002020-03-03 21:33:12 +03003691static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003692{
Pavel Begunkov014db002020-03-03 21:33:12 +03003693 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003694}
3695
Jens Axboe067524e2020-03-02 16:32:28 -07003696static int io_remove_buffers_prep(struct io_kiocb *req,
3697 const struct io_uring_sqe *sqe)
3698{
3699 struct io_provide_buf *p = &req->pbuf;
3700 u64 tmp;
3701
3702 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3703 return -EINVAL;
3704
3705 tmp = READ_ONCE(sqe->fd);
3706 if (!tmp || tmp > USHRT_MAX)
3707 return -EINVAL;
3708
3709 memset(p, 0, sizeof(*p));
3710 p->nbufs = tmp;
3711 p->bgid = READ_ONCE(sqe->buf_group);
3712 return 0;
3713}
3714
3715static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3716 int bgid, unsigned nbufs)
3717{
3718 unsigned i = 0;
3719
3720 /* shouldn't happen */
3721 if (!nbufs)
3722 return 0;
3723
3724 /* the head kbuf is the list itself */
3725 while (!list_empty(&buf->list)) {
3726 struct io_buffer *nxt;
3727
3728 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3729 list_del(&nxt->list);
3730 kfree(nxt);
3731 if (++i == nbufs)
3732 return i;
3733 }
3734 i++;
3735 kfree(buf);
3736 idr_remove(&ctx->io_buffer_idr, bgid);
3737
3738 return i;
3739}
3740
Jens Axboe229a7b62020-06-22 10:13:11 -06003741static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3742 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003743{
3744 struct io_provide_buf *p = &req->pbuf;
3745 struct io_ring_ctx *ctx = req->ctx;
3746 struct io_buffer *head;
3747 int ret = 0;
3748
3749 io_ring_submit_lock(ctx, !force_nonblock);
3750
3751 lockdep_assert_held(&ctx->uring_lock);
3752
3753 ret = -ENOENT;
3754 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3755 if (head)
3756 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3757
3758 io_ring_submit_lock(ctx, !force_nonblock);
3759 if (ret < 0)
3760 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003761 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003762 return 0;
3763}
3764
Jens Axboeddf0322d2020-02-23 16:41:33 -07003765static int io_provide_buffers_prep(struct io_kiocb *req,
3766 const struct io_uring_sqe *sqe)
3767{
3768 struct io_provide_buf *p = &req->pbuf;
3769 u64 tmp;
3770
3771 if (sqe->ioprio || sqe->rw_flags)
3772 return -EINVAL;
3773
3774 tmp = READ_ONCE(sqe->fd);
3775 if (!tmp || tmp > USHRT_MAX)
3776 return -E2BIG;
3777 p->nbufs = tmp;
3778 p->addr = READ_ONCE(sqe->addr);
3779 p->len = READ_ONCE(sqe->len);
3780
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003781 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003782 return -EFAULT;
3783
3784 p->bgid = READ_ONCE(sqe->buf_group);
3785 tmp = READ_ONCE(sqe->off);
3786 if (tmp > USHRT_MAX)
3787 return -E2BIG;
3788 p->bid = tmp;
3789 return 0;
3790}
3791
3792static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3793{
3794 struct io_buffer *buf;
3795 u64 addr = pbuf->addr;
3796 int i, bid = pbuf->bid;
3797
3798 for (i = 0; i < pbuf->nbufs; i++) {
3799 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3800 if (!buf)
3801 break;
3802
3803 buf->addr = addr;
3804 buf->len = pbuf->len;
3805 buf->bid = bid;
3806 addr += pbuf->len;
3807 bid++;
3808 if (!*head) {
3809 INIT_LIST_HEAD(&buf->list);
3810 *head = buf;
3811 } else {
3812 list_add_tail(&buf->list, &(*head)->list);
3813 }
3814 }
3815
3816 return i ? i : -ENOMEM;
3817}
3818
Jens Axboe229a7b62020-06-22 10:13:11 -06003819static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3820 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003821{
3822 struct io_provide_buf *p = &req->pbuf;
3823 struct io_ring_ctx *ctx = req->ctx;
3824 struct io_buffer *head, *list;
3825 int ret = 0;
3826
3827 io_ring_submit_lock(ctx, !force_nonblock);
3828
3829 lockdep_assert_held(&ctx->uring_lock);
3830
3831 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3832
3833 ret = io_add_buffers(p, &head);
3834 if (ret < 0)
3835 goto out;
3836
3837 if (!list) {
3838 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3839 GFP_KERNEL);
3840 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003841 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003842 goto out;
3843 }
3844 }
3845out:
3846 io_ring_submit_unlock(ctx, !force_nonblock);
3847 if (ret < 0)
3848 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003849 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003850 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003851}
3852
Jens Axboe3e4827b2020-01-08 15:18:09 -07003853static int io_epoll_ctl_prep(struct io_kiocb *req,
3854 const struct io_uring_sqe *sqe)
3855{
3856#if defined(CONFIG_EPOLL)
3857 if (sqe->ioprio || sqe->buf_index)
3858 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003859 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003860 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003861
3862 req->epoll.epfd = READ_ONCE(sqe->fd);
3863 req->epoll.op = READ_ONCE(sqe->len);
3864 req->epoll.fd = READ_ONCE(sqe->off);
3865
3866 if (ep_op_has_event(req->epoll.op)) {
3867 struct epoll_event __user *ev;
3868
3869 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3870 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3871 return -EFAULT;
3872 }
3873
3874 return 0;
3875#else
3876 return -EOPNOTSUPP;
3877#endif
3878}
3879
Jens Axboe229a7b62020-06-22 10:13:11 -06003880static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3881 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003882{
3883#if defined(CONFIG_EPOLL)
3884 struct io_epoll *ie = &req->epoll;
3885 int ret;
3886
3887 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3888 if (force_nonblock && ret == -EAGAIN)
3889 return -EAGAIN;
3890
3891 if (ret < 0)
3892 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003893 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003894 return 0;
3895#else
3896 return -EOPNOTSUPP;
3897#endif
3898}
3899
Jens Axboec1ca7572019-12-25 22:18:28 -07003900static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3901{
3902#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3903 if (sqe->ioprio || sqe->buf_index || sqe->off)
3904 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003905 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3906 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003907
3908 req->madvise.addr = READ_ONCE(sqe->addr);
3909 req->madvise.len = READ_ONCE(sqe->len);
3910 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3911 return 0;
3912#else
3913 return -EOPNOTSUPP;
3914#endif
3915}
3916
Pavel Begunkov014db002020-03-03 21:33:12 +03003917static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003918{
3919#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3920 struct io_madvise *ma = &req->madvise;
3921 int ret;
3922
3923 if (force_nonblock)
3924 return -EAGAIN;
3925
3926 ret = do_madvise(ma->addr, ma->len, ma->advice);
3927 if (ret < 0)
3928 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003929 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003930 return 0;
3931#else
3932 return -EOPNOTSUPP;
3933#endif
3934}
3935
Jens Axboe4840e412019-12-25 22:03:45 -07003936static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3937{
3938 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3939 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003940 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3941 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003942
3943 req->fadvise.offset = READ_ONCE(sqe->off);
3944 req->fadvise.len = READ_ONCE(sqe->len);
3945 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3946 return 0;
3947}
3948
Pavel Begunkov014db002020-03-03 21:33:12 +03003949static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003950{
3951 struct io_fadvise *fa = &req->fadvise;
3952 int ret;
3953
Jens Axboe3e694262020-02-01 09:22:49 -07003954 if (force_nonblock) {
3955 switch (fa->advice) {
3956 case POSIX_FADV_NORMAL:
3957 case POSIX_FADV_RANDOM:
3958 case POSIX_FADV_SEQUENTIAL:
3959 break;
3960 default:
3961 return -EAGAIN;
3962 }
3963 }
Jens Axboe4840e412019-12-25 22:03:45 -07003964
3965 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3966 if (ret < 0)
3967 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003968 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003969 return 0;
3970}
3971
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003972static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3973{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003974 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003975 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003976 if (sqe->ioprio || sqe->buf_index)
3977 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003978 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003979 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003980
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003981 req->statx.dfd = READ_ONCE(sqe->fd);
3982 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003983 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003984 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3985 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003986
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003987 return 0;
3988}
3989
Pavel Begunkov014db002020-03-03 21:33:12 +03003990static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003991{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003992 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003993 int ret;
3994
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003995 if (force_nonblock) {
3996 /* only need file table for an actual valid fd */
3997 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3998 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003999 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004000 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004001
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004002 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4003 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004004
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004005 if (ret < 0)
4006 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004007 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004008 return 0;
4009}
4010
Jens Axboeb5dba592019-12-11 14:02:38 -07004011static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4012{
4013 /*
4014 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004015 * leave the 'file' in an undeterminate state, and here need to modify
4016 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004017 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004018 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004019 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4020
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004021 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4022 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004023 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4024 sqe->rw_flags || sqe->buf_index)
4025 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004026 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004027 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004028
4029 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004030 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004031 return -EBADF;
4032
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004033 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004034 return 0;
4035}
4036
Jens Axboe229a7b62020-06-22 10:13:11 -06004037static int io_close(struct io_kiocb *req, bool force_nonblock,
4038 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004039{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004040 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004041 int ret;
4042
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004043 /* might be already done during nonblock submission */
4044 if (!close->put_file) {
4045 ret = __close_fd_get_file(close->fd, &close->put_file);
4046 if (ret < 0)
4047 return (ret == -ENOENT) ? -EBADF : ret;
4048 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004049
4050 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004051 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004052 /* was never set, but play safe */
4053 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004054 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004055 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004056 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004057 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004058
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004059 /* No ->flush() or already async, safely close from here */
4060 ret = filp_close(close->put_file, req->work.files);
4061 if (ret < 0)
4062 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004063 fput(close->put_file);
4064 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004065 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004066 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004067}
4068
Jens Axboe3529d8c2019-12-19 18:24:38 -07004069static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004070{
4071 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004072
4073 if (!req->file)
4074 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004075
4076 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4077 return -EINVAL;
4078 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4079 return -EINVAL;
4080
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004081 req->sync.off = READ_ONCE(sqe->off);
4082 req->sync.len = READ_ONCE(sqe->len);
4083 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004084 return 0;
4085}
4086
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004087static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004088{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004089 int ret;
4090
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004091 /* sync_file_range always requires a blocking context */
4092 if (force_nonblock)
4093 return -EAGAIN;
4094
Jens Axboe9adbd452019-12-20 08:45:55 -07004095 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004096 req->sync.flags);
4097 if (ret < 0)
4098 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004099 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004100 return 0;
4101}
4102
YueHaibing469956e2020-03-04 15:53:52 +08004103#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004104static int io_setup_async_msg(struct io_kiocb *req,
4105 struct io_async_msghdr *kmsg)
4106{
4107 if (req->io)
4108 return -EAGAIN;
4109 if (io_alloc_async_ctx(req)) {
4110 if (kmsg->iov != kmsg->fast_iov)
4111 kfree(kmsg->iov);
4112 return -ENOMEM;
4113 }
4114 req->flags |= REQ_F_NEED_CLEANUP;
4115 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4116 return -EAGAIN;
4117}
4118
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004119static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4120 struct io_async_msghdr *iomsg)
4121{
4122 iomsg->iov = iomsg->fast_iov;
4123 iomsg->msg.msg_name = &iomsg->addr;
4124 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4125 req->sr_msg.msg_flags, &iomsg->iov);
4126}
4127
Jens Axboe3529d8c2019-12-19 18:24:38 -07004128static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004129{
Jens Axboee47293f2019-12-20 08:58:21 -07004130 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004131 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004132 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004133
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004134 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4135 return -EINVAL;
4136
Jens Axboee47293f2019-12-20 08:58:21 -07004137 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004138 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004139 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004140
Jens Axboed8768362020-02-27 14:17:49 -07004141#ifdef CONFIG_COMPAT
4142 if (req->ctx->compat)
4143 sr->msg_flags |= MSG_CMSG_COMPAT;
4144#endif
4145
Jens Axboefddafac2020-01-04 20:19:44 -07004146 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004147 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004148 /* iovec is already imported */
4149 if (req->flags & REQ_F_NEED_CLEANUP)
4150 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004151
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004152 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004153 if (!ret)
4154 req->flags |= REQ_F_NEED_CLEANUP;
4155 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004156}
4157
Jens Axboe229a7b62020-06-22 10:13:11 -06004158static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4159 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004160{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004161 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004162 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004163 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004164 int ret;
4165
Jens Axboe03b12302019-12-02 18:50:25 -07004166 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004167 if (unlikely(!sock))
4168 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004169
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004170 if (req->io) {
4171 kmsg = &req->io->msg;
4172 kmsg->msg.msg_name = &req->io->msg.addr;
4173 /* if iov is set, it's allocated already */
4174 if (!kmsg->iov)
4175 kmsg->iov = kmsg->fast_iov;
4176 kmsg->msg.msg_iter.iov = kmsg->iov;
4177 } else {
4178 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004179 if (ret)
4180 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004181 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004182 }
4183
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004184 flags = req->sr_msg.msg_flags;
4185 if (flags & MSG_DONTWAIT)
4186 req->flags |= REQ_F_NOWAIT;
4187 else if (force_nonblock)
4188 flags |= MSG_DONTWAIT;
4189
4190 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4191 if (force_nonblock && ret == -EAGAIN)
4192 return io_setup_async_msg(req, kmsg);
4193 if (ret == -ERESTARTSYS)
4194 ret = -EINTR;
4195
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004196 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004197 kfree(kmsg->iov);
4198 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004199 if (ret < 0)
4200 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004201 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004202 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004203}
4204
Jens Axboe229a7b62020-06-22 10:13:11 -06004205static int io_send(struct io_kiocb *req, bool force_nonblock,
4206 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004207{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004208 struct io_sr_msg *sr = &req->sr_msg;
4209 struct msghdr msg;
4210 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004211 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004212 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004213 int ret;
4214
4215 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004216 if (unlikely(!sock))
4217 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004218
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004219 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4220 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004221 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004222
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004223 msg.msg_name = NULL;
4224 msg.msg_control = NULL;
4225 msg.msg_controllen = 0;
4226 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004227
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004228 flags = req->sr_msg.msg_flags;
4229 if (flags & MSG_DONTWAIT)
4230 req->flags |= REQ_F_NOWAIT;
4231 else if (force_nonblock)
4232 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004233
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004234 msg.msg_flags = flags;
4235 ret = sock_sendmsg(sock, &msg);
4236 if (force_nonblock && ret == -EAGAIN)
4237 return -EAGAIN;
4238 if (ret == -ERESTARTSYS)
4239 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004240
Jens Axboe03b12302019-12-02 18:50:25 -07004241 if (ret < 0)
4242 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004243 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004244 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004245}
4246
Pavel Begunkov1400e692020-07-12 20:41:05 +03004247static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4248 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004249{
4250 struct io_sr_msg *sr = &req->sr_msg;
4251 struct iovec __user *uiov;
4252 size_t iov_len;
4253 int ret;
4254
Pavel Begunkov1400e692020-07-12 20:41:05 +03004255 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4256 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004257 if (ret)
4258 return ret;
4259
4260 if (req->flags & REQ_F_BUFFER_SELECT) {
4261 if (iov_len > 1)
4262 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004263 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004264 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004265 sr->len = iomsg->iov[0].iov_len;
4266 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004267 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004268 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004269 } else {
4270 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004271 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004272 if (ret > 0)
4273 ret = 0;
4274 }
4275
4276 return ret;
4277}
4278
4279#ifdef CONFIG_COMPAT
4280static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004281 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004282{
4283 struct compat_msghdr __user *msg_compat;
4284 struct io_sr_msg *sr = &req->sr_msg;
4285 struct compat_iovec __user *uiov;
4286 compat_uptr_t ptr;
4287 compat_size_t len;
4288 int ret;
4289
Pavel Begunkov270a5942020-07-12 20:41:04 +03004290 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004291 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004292 &ptr, &len);
4293 if (ret)
4294 return ret;
4295
4296 uiov = compat_ptr(ptr);
4297 if (req->flags & REQ_F_BUFFER_SELECT) {
4298 compat_ssize_t clen;
4299
4300 if (len > 1)
4301 return -EINVAL;
4302 if (!access_ok(uiov, sizeof(*uiov)))
4303 return -EFAULT;
4304 if (__get_user(clen, &uiov->iov_len))
4305 return -EFAULT;
4306 if (clen < 0)
4307 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004308 sr->len = iomsg->iov[0].iov_len;
4309 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004310 } else {
4311 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004312 &iomsg->iov,
4313 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004314 if (ret < 0)
4315 return ret;
4316 }
4317
4318 return 0;
4319}
Jens Axboe03b12302019-12-02 18:50:25 -07004320#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004321
Pavel Begunkov1400e692020-07-12 20:41:05 +03004322static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4323 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004324{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004325 iomsg->msg.msg_name = &iomsg->addr;
4326 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004327
4328#ifdef CONFIG_COMPAT
4329 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004330 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004331#endif
4332
Pavel Begunkov1400e692020-07-12 20:41:05 +03004333 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004334}
4335
Jens Axboebcda7ba2020-02-23 16:42:51 -07004336static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004337 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004338{
4339 struct io_sr_msg *sr = &req->sr_msg;
4340 struct io_buffer *kbuf;
4341
Jens Axboebcda7ba2020-02-23 16:42:51 -07004342 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4343 if (IS_ERR(kbuf))
4344 return kbuf;
4345
4346 sr->kbuf = kbuf;
4347 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004348 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004349}
4350
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004351static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4352{
4353 return io_put_kbuf(req, req->sr_msg.kbuf);
4354}
4355
Jens Axboe3529d8c2019-12-19 18:24:38 -07004356static int io_recvmsg_prep(struct io_kiocb *req,
4357 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004358{
Jens Axboee47293f2019-12-20 08:58:21 -07004359 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004360 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004361 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004362
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004363 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4364 return -EINVAL;
4365
Jens Axboe3529d8c2019-12-19 18:24:38 -07004366 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004367 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004368 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004369 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004370
Jens Axboed8768362020-02-27 14:17:49 -07004371#ifdef CONFIG_COMPAT
4372 if (req->ctx->compat)
4373 sr->msg_flags |= MSG_CMSG_COMPAT;
4374#endif
4375
Jens Axboefddafac2020-01-04 20:19:44 -07004376 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004377 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004378 /* iovec is already imported */
4379 if (req->flags & REQ_F_NEED_CLEANUP)
4380 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004381
Pavel Begunkov1400e692020-07-12 20:41:05 +03004382 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004383 if (!ret)
4384 req->flags |= REQ_F_NEED_CLEANUP;
4385 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004386}
4387
Jens Axboe229a7b62020-06-22 10:13:11 -06004388static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4389 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004390{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004391 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004392 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004393 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004394 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004395 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004396
Jens Axboe0fa03c62019-04-19 13:34:07 -06004397 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004398 if (unlikely(!sock))
4399 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004400
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004401 if (req->io) {
4402 kmsg = &req->io->msg;
4403 kmsg->msg.msg_name = &req->io->msg.addr;
4404 /* if iov is set, it's allocated already */
4405 if (!kmsg->iov)
4406 kmsg->iov = kmsg->fast_iov;
4407 kmsg->msg.msg_iter.iov = kmsg->iov;
4408 } else {
4409 ret = io_recvmsg_copy_hdr(req, &iomsg);
4410 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004411 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004412 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004413 }
4414
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004415 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004416 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004417 if (IS_ERR(kbuf))
4418 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004419 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4420 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4421 1, req->sr_msg.len);
4422 }
4423
4424 flags = req->sr_msg.msg_flags;
4425 if (flags & MSG_DONTWAIT)
4426 req->flags |= REQ_F_NOWAIT;
4427 else if (force_nonblock)
4428 flags |= MSG_DONTWAIT;
4429
4430 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4431 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004432 if (force_nonblock && ret == -EAGAIN)
4433 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004434 if (ret == -ERESTARTSYS)
4435 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004436
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004437 if (req->flags & REQ_F_BUFFER_SELECTED)
4438 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004439 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004440 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004441 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004442 if (ret < 0)
4443 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004444 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004445 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004446}
4447
Jens Axboe229a7b62020-06-22 10:13:11 -06004448static int io_recv(struct io_kiocb *req, bool force_nonblock,
4449 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004450{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004451 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004452 struct io_sr_msg *sr = &req->sr_msg;
4453 struct msghdr msg;
4454 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004455 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004456 struct iovec iov;
4457 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004458 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004459
Jens Axboefddafac2020-01-04 20:19:44 -07004460 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004461 if (unlikely(!sock))
4462 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004463
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004464 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004465 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004466 if (IS_ERR(kbuf))
4467 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004468 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004469 }
4470
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004471 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004472 if (unlikely(ret))
4473 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004474
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004475 msg.msg_name = NULL;
4476 msg.msg_control = NULL;
4477 msg.msg_controllen = 0;
4478 msg.msg_namelen = 0;
4479 msg.msg_iocb = NULL;
4480 msg.msg_flags = 0;
4481
4482 flags = req->sr_msg.msg_flags;
4483 if (flags & MSG_DONTWAIT)
4484 req->flags |= REQ_F_NOWAIT;
4485 else if (force_nonblock)
4486 flags |= MSG_DONTWAIT;
4487
4488 ret = sock_recvmsg(sock, &msg, flags);
4489 if (force_nonblock && ret == -EAGAIN)
4490 return -EAGAIN;
4491 if (ret == -ERESTARTSYS)
4492 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004493out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004494 if (req->flags & REQ_F_BUFFER_SELECTED)
4495 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004496 if (ret < 0)
4497 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004498 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004499 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004500}
4501
Jens Axboe3529d8c2019-12-19 18:24:38 -07004502static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004503{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004504 struct io_accept *accept = &req->accept;
4505
Jens Axboe17f2fe32019-10-17 14:42:58 -06004506 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4507 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004508 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004509 return -EINVAL;
4510
Jens Axboed55e5f52019-12-11 16:12:15 -07004511 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4512 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004513 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004514 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004515 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004516}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004517
Jens Axboe229a7b62020-06-22 10:13:11 -06004518static int io_accept(struct io_kiocb *req, bool force_nonblock,
4519 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004520{
4521 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004522 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004523 int ret;
4524
Jiufei Xuee697dee2020-06-10 13:41:59 +08004525 if (req->file->f_flags & O_NONBLOCK)
4526 req->flags |= REQ_F_NOWAIT;
4527
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004528 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004529 accept->addr_len, accept->flags,
4530 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004531 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004532 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004533 if (ret < 0) {
4534 if (ret == -ERESTARTSYS)
4535 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004536 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004537 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004538 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004539 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004540}
4541
Jens Axboe3529d8c2019-12-19 18:24:38 -07004542static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004543{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004544 struct io_connect *conn = &req->connect;
4545 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004546
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004547 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4548 return -EINVAL;
4549 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4550 return -EINVAL;
4551
Jens Axboe3529d8c2019-12-19 18:24:38 -07004552 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4553 conn->addr_len = READ_ONCE(sqe->addr2);
4554
4555 if (!io)
4556 return 0;
4557
4558 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004559 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004560}
4561
Jens Axboe229a7b62020-06-22 10:13:11 -06004562static int io_connect(struct io_kiocb *req, bool force_nonblock,
4563 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004564{
Jens Axboef499a022019-12-02 16:28:46 -07004565 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004566 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004567 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004568
Jens Axboef499a022019-12-02 16:28:46 -07004569 if (req->io) {
4570 io = req->io;
4571 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004572 ret = move_addr_to_kernel(req->connect.addr,
4573 req->connect.addr_len,
4574 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004575 if (ret)
4576 goto out;
4577 io = &__io;
4578 }
4579
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004580 file_flags = force_nonblock ? O_NONBLOCK : 0;
4581
4582 ret = __sys_connect_file(req->file, &io->connect.address,
4583 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004584 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004585 if (req->io)
4586 return -EAGAIN;
4587 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004588 ret = -ENOMEM;
4589 goto out;
4590 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004591 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004592 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004593 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004594 if (ret == -ERESTARTSYS)
4595 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004596out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004597 if (ret < 0)
4598 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004599 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004600 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004601}
YueHaibing469956e2020-03-04 15:53:52 +08004602#else /* !CONFIG_NET */
4603static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4604{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004605 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004606}
4607
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004608static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4609 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004610{
YueHaibing469956e2020-03-04 15:53:52 +08004611 return -EOPNOTSUPP;
4612}
4613
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004614static int io_send(struct io_kiocb *req, bool force_nonblock,
4615 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004616{
4617 return -EOPNOTSUPP;
4618}
4619
4620static int io_recvmsg_prep(struct io_kiocb *req,
4621 const struct io_uring_sqe *sqe)
4622{
4623 return -EOPNOTSUPP;
4624}
4625
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004626static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4627 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004628{
4629 return -EOPNOTSUPP;
4630}
4631
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004632static int io_recv(struct io_kiocb *req, bool force_nonblock,
4633 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004634{
4635 return -EOPNOTSUPP;
4636}
4637
4638static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4639{
4640 return -EOPNOTSUPP;
4641}
4642
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004643static int io_accept(struct io_kiocb *req, bool force_nonblock,
4644 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004645{
4646 return -EOPNOTSUPP;
4647}
4648
4649static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4650{
4651 return -EOPNOTSUPP;
4652}
4653
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004654static int io_connect(struct io_kiocb *req, bool force_nonblock,
4655 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004656{
4657 return -EOPNOTSUPP;
4658}
4659#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660
Jens Axboed7718a92020-02-14 22:23:12 -07004661struct io_poll_table {
4662 struct poll_table_struct pt;
4663 struct io_kiocb *req;
4664 int error;
4665};
4666
Jens Axboed7718a92020-02-14 22:23:12 -07004667static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4668 __poll_t mask, task_work_func_t func)
4669{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004670 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004671 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004672
4673 /* for instances that support it check for an event match first: */
4674 if (mask && !(mask & poll->events))
4675 return 0;
4676
4677 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4678
4679 list_del_init(&poll->wait.entry);
4680
Jens Axboed7718a92020-02-14 22:23:12 -07004681 req->result = mask;
4682 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004683 percpu_ref_get(&req->ctx->refs);
4684
Jens Axboed7718a92020-02-14 22:23:12 -07004685 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004686 * If we using the signalfd wait_queue_head for this wakeup, then
4687 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4688 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4689 * either, as the normal wakeup will suffice.
4690 */
4691 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4692
4693 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004694 * If this fails, then the task is exiting. When a task exits, the
4695 * work gets canceled, so just cancel this request as well instead
4696 * of executing it. We can't safely execute it anyway, as we may not
4697 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004698 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004699 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004700 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004701 struct task_struct *tsk;
4702
Jens Axboee3aabf92020-05-18 11:04:17 -06004703 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004704 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004705 task_work_add(tsk, &req->task_work, 0);
4706 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004707 }
Jens Axboed7718a92020-02-14 22:23:12 -07004708 return 1;
4709}
4710
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004711static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4712 __acquires(&req->ctx->completion_lock)
4713{
4714 struct io_ring_ctx *ctx = req->ctx;
4715
4716 if (!req->result && !READ_ONCE(poll->canceled)) {
4717 struct poll_table_struct pt = { ._key = poll->events };
4718
4719 req->result = vfs_poll(req->file, &pt) & poll->events;
4720 }
4721
4722 spin_lock_irq(&ctx->completion_lock);
4723 if (!req->result && !READ_ONCE(poll->canceled)) {
4724 add_wait_queue(poll->head, &poll->wait);
4725 return true;
4726 }
4727
4728 return false;
4729}
4730
Jens Axboed4e7cd32020-08-15 11:44:50 -07004731static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004732{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004733 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4734 if (req->opcode == IORING_OP_POLL_ADD)
4735 return (struct io_poll_iocb *) req->io;
4736 return req->apoll->double_poll;
4737}
4738
4739static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4740{
4741 if (req->opcode == IORING_OP_POLL_ADD)
4742 return &req->poll;
4743 return &req->apoll->poll;
4744}
4745
4746static void io_poll_remove_double(struct io_kiocb *req)
4747{
4748 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004749
4750 lockdep_assert_held(&req->ctx->completion_lock);
4751
4752 if (poll && poll->head) {
4753 struct wait_queue_head *head = poll->head;
4754
4755 spin_lock(&head->lock);
4756 list_del_init(&poll->wait.entry);
4757 if (poll->wait.private)
4758 refcount_dec(&req->refs);
4759 poll->head = NULL;
4760 spin_unlock(&head->lock);
4761 }
4762}
4763
4764static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4765{
4766 struct io_ring_ctx *ctx = req->ctx;
4767
Jens Axboed4e7cd32020-08-15 11:44:50 -07004768 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004769 req->poll.done = true;
4770 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4771 io_commit_cqring(ctx);
4772}
4773
4774static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4775{
4776 struct io_ring_ctx *ctx = req->ctx;
4777
4778 if (io_poll_rewait(req, &req->poll)) {
4779 spin_unlock_irq(&ctx->completion_lock);
4780 return;
4781 }
4782
4783 hash_del(&req->hash_node);
4784 io_poll_complete(req, req->result, 0);
4785 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03004786 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004787 spin_unlock_irq(&ctx->completion_lock);
4788
4789 io_cqring_ev_posted(ctx);
4790}
4791
4792static void io_poll_task_func(struct callback_head *cb)
4793{
4794 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004795 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004796 struct io_kiocb *nxt = NULL;
4797
4798 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004799 if (nxt)
4800 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004801 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004802}
4803
4804static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4805 int sync, void *key)
4806{
4807 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004808 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004809 __poll_t mask = key_to_poll(key);
4810
4811 /* for instances that support it check for an event match first: */
4812 if (mask && !(mask & poll->events))
4813 return 0;
4814
Jens Axboe8706e042020-09-28 08:38:54 -06004815 list_del_init(&wait->entry);
4816
Jens Axboe807abcb2020-07-17 17:09:27 -06004817 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004818 bool done;
4819
Jens Axboe807abcb2020-07-17 17:09:27 -06004820 spin_lock(&poll->head->lock);
4821 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004822 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004823 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004824 /* make sure double remove sees this as being gone */
4825 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004826 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004827 if (!done)
4828 __io_async_wake(req, poll, mask, io_poll_task_func);
4829 }
4830 refcount_dec(&req->refs);
4831 return 1;
4832}
4833
4834static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4835 wait_queue_func_t wake_func)
4836{
4837 poll->head = NULL;
4838 poll->done = false;
4839 poll->canceled = false;
4840 poll->events = events;
4841 INIT_LIST_HEAD(&poll->wait.entry);
4842 init_waitqueue_func_entry(&poll->wait, wake_func);
4843}
4844
4845static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004846 struct wait_queue_head *head,
4847 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004848{
4849 struct io_kiocb *req = pt->req;
4850
4851 /*
4852 * If poll->head is already set, it's because the file being polled
4853 * uses multiple waitqueues for poll handling (eg one for read, one
4854 * for write). Setup a separate io_poll_iocb if this happens.
4855 */
4856 if (unlikely(poll->head)) {
4857 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004858 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004859 pt->error = -EINVAL;
4860 return;
4861 }
4862 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4863 if (!poll) {
4864 pt->error = -ENOMEM;
4865 return;
4866 }
4867 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4868 refcount_inc(&req->refs);
4869 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004870 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004871 }
4872
4873 pt->error = 0;
4874 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004875
4876 if (poll->events & EPOLLEXCLUSIVE)
4877 add_wait_queue_exclusive(head, &poll->wait);
4878 else
4879 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004880}
4881
4882static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4883 struct poll_table_struct *p)
4884{
4885 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004886 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004887
Jens Axboe807abcb2020-07-17 17:09:27 -06004888 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004889}
4890
Jens Axboed7718a92020-02-14 22:23:12 -07004891static void io_async_task_func(struct callback_head *cb)
4892{
4893 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4894 struct async_poll *apoll = req->apoll;
4895 struct io_ring_ctx *ctx = req->ctx;
4896
4897 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4898
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004899 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004900 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004901 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004902 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004903 }
4904
Jens Axboe31067252020-05-17 17:43:31 -06004905 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004906 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004907 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004908
Jens Axboed4e7cd32020-08-15 11:44:50 -07004909 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004910 spin_unlock_irq(&ctx->completion_lock);
4911
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004912 if (!READ_ONCE(apoll->poll.canceled))
4913 __io_req_task_submit(req);
4914 else
4915 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004916
Jens Axboe6d816e02020-08-11 08:04:14 -06004917 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004918 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004919 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004920}
4921
4922static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4923 void *key)
4924{
4925 struct io_kiocb *req = wait->private;
4926 struct io_poll_iocb *poll = &req->apoll->poll;
4927
4928 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4929 key_to_poll(key));
4930
4931 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4932}
4933
4934static void io_poll_req_insert(struct io_kiocb *req)
4935{
4936 struct io_ring_ctx *ctx = req->ctx;
4937 struct hlist_head *list;
4938
4939 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4940 hlist_add_head(&req->hash_node, list);
4941}
4942
4943static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4944 struct io_poll_iocb *poll,
4945 struct io_poll_table *ipt, __poll_t mask,
4946 wait_queue_func_t wake_func)
4947 __acquires(&ctx->completion_lock)
4948{
4949 struct io_ring_ctx *ctx = req->ctx;
4950 bool cancel = false;
4951
Jens Axboe18bceab2020-05-15 11:56:54 -06004952 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004953 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004954 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004955
4956 ipt->pt._key = mask;
4957 ipt->req = req;
4958 ipt->error = -EINVAL;
4959
Jens Axboed7718a92020-02-14 22:23:12 -07004960 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4961
4962 spin_lock_irq(&ctx->completion_lock);
4963 if (likely(poll->head)) {
4964 spin_lock(&poll->head->lock);
4965 if (unlikely(list_empty(&poll->wait.entry))) {
4966 if (ipt->error)
4967 cancel = true;
4968 ipt->error = 0;
4969 mask = 0;
4970 }
4971 if (mask || ipt->error)
4972 list_del_init(&poll->wait.entry);
4973 else if (cancel)
4974 WRITE_ONCE(poll->canceled, true);
4975 else if (!poll->done) /* actually waiting for an event */
4976 io_poll_req_insert(req);
4977 spin_unlock(&poll->head->lock);
4978 }
4979
4980 return mask;
4981}
4982
4983static bool io_arm_poll_handler(struct io_kiocb *req)
4984{
4985 const struct io_op_def *def = &io_op_defs[req->opcode];
4986 struct io_ring_ctx *ctx = req->ctx;
4987 struct async_poll *apoll;
4988 struct io_poll_table ipt;
4989 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004990 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004991
4992 if (!req->file || !file_can_poll(req->file))
4993 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004994 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004995 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004996 if (def->pollin)
4997 rw = READ;
4998 else if (def->pollout)
4999 rw = WRITE;
5000 else
5001 return false;
5002 /* if we can't nonblock try, then no point in arming a poll handler */
5003 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005004 return false;
5005
5006 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5007 if (unlikely(!apoll))
5008 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005009 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005010
5011 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005012 req->apoll = apoll;
5013 INIT_HLIST_NODE(&req->hash_node);
5014
Nathan Chancellor8755d972020-03-02 16:01:19 -07005015 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005016 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005017 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005018 if (def->pollout)
5019 mask |= POLLOUT | POLLWRNORM;
5020 mask |= POLLERR | POLLPRI;
5021
5022 ipt.pt._qproc = io_async_queue_proc;
5023
5024 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5025 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005026 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005027 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005028 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005029 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005030 kfree(apoll);
5031 return false;
5032 }
5033 spin_unlock_irq(&ctx->completion_lock);
5034 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5035 apoll->poll.events);
5036 return true;
5037}
5038
5039static bool __io_poll_remove_one(struct io_kiocb *req,
5040 struct io_poll_iocb *poll)
5041{
Jens Axboeb41e9852020-02-17 09:52:41 -07005042 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005043
5044 spin_lock(&poll->head->lock);
5045 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005046 if (!list_empty(&poll->wait.entry)) {
5047 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005048 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005049 }
5050 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005051 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005052 return do_complete;
5053}
5054
5055static bool io_poll_remove_one(struct io_kiocb *req)
5056{
5057 bool do_complete;
5058
Jens Axboed4e7cd32020-08-15 11:44:50 -07005059 io_poll_remove_double(req);
5060
Jens Axboed7718a92020-02-14 22:23:12 -07005061 if (req->opcode == IORING_OP_POLL_ADD) {
5062 do_complete = __io_poll_remove_one(req, &req->poll);
5063 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005064 struct async_poll *apoll = req->apoll;
5065
Jens Axboed7718a92020-02-14 22:23:12 -07005066 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005067 do_complete = __io_poll_remove_one(req, &apoll->poll);
5068 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005069 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005070 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005071 kfree(apoll);
5072 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005073 }
5074
Jens Axboeb41e9852020-02-17 09:52:41 -07005075 if (do_complete) {
5076 io_cqring_fill_event(req, -ECANCELED);
5077 io_commit_cqring(req->ctx);
5078 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005079 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005080 io_put_req(req);
5081 }
5082
5083 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005084}
5085
Jens Axboe76e1b642020-09-26 15:05:03 -06005086/*
5087 * Returns true if we found and killed one or more poll requests
5088 */
5089static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005090{
Jens Axboe78076bb2019-12-04 19:56:40 -07005091 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005092 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005093 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005094
5095 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005096 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5097 struct hlist_head *list;
5098
5099 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005100 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5101 if (io_task_match(req, tsk))
5102 posted += io_poll_remove_one(req);
5103 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005104 }
5105 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005106
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005107 if (posted)
5108 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005109
5110 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005111}
5112
Jens Axboe47f46762019-11-09 17:43:02 -07005113static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5114{
Jens Axboe78076bb2019-12-04 19:56:40 -07005115 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005116 struct io_kiocb *req;
5117
Jens Axboe78076bb2019-12-04 19:56:40 -07005118 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5119 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005120 if (sqe_addr != req->user_data)
5121 continue;
5122 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005123 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005124 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005125 }
5126
5127 return -ENOENT;
5128}
5129
Jens Axboe3529d8c2019-12-19 18:24:38 -07005130static int io_poll_remove_prep(struct io_kiocb *req,
5131 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005132{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005133 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5134 return -EINVAL;
5135 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5136 sqe->poll_events)
5137 return -EINVAL;
5138
Jens Axboe0969e782019-12-17 18:40:57 -07005139 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005140 return 0;
5141}
5142
5143/*
5144 * Find a running poll command that matches one specified in sqe->addr,
5145 * and remove it if found.
5146 */
5147static int io_poll_remove(struct io_kiocb *req)
5148{
5149 struct io_ring_ctx *ctx = req->ctx;
5150 u64 addr;
5151 int ret;
5152
Jens Axboe0969e782019-12-17 18:40:57 -07005153 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005154 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005155 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005156 spin_unlock_irq(&ctx->completion_lock);
5157
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005158 if (ret < 0)
5159 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005160 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005161 return 0;
5162}
5163
Jens Axboe221c5eb2019-01-17 09:41:58 -07005164static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5165 void *key)
5166{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005167 struct io_kiocb *req = wait->private;
5168 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005169
Jens Axboed7718a92020-02-14 22:23:12 -07005170 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005171}
5172
Jens Axboe221c5eb2019-01-17 09:41:58 -07005173static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5174 struct poll_table_struct *p)
5175{
5176 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5177
Jens Axboe807abcb2020-07-17 17:09:27 -06005178 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005179}
5180
Jens Axboe3529d8c2019-12-19 18:24:38 -07005181static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005182{
5183 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005184 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005185
5186 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5187 return -EINVAL;
5188 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5189 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005190 if (!poll->file)
5191 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005192
Jiufei Xue5769a352020-06-17 17:53:55 +08005193 events = READ_ONCE(sqe->poll32_events);
5194#ifdef __BIG_ENDIAN
5195 events = swahw32(events);
5196#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005197 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5198 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005199 return 0;
5200}
5201
Pavel Begunkov014db002020-03-03 21:33:12 +03005202static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005203{
5204 struct io_poll_iocb *poll = &req->poll;
5205 struct io_ring_ctx *ctx = req->ctx;
5206 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005207 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005208
Jens Axboe78076bb2019-12-04 19:56:40 -07005209 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005210 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005211
Jens Axboed7718a92020-02-14 22:23:12 -07005212 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5213 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005214
Jens Axboe8c838782019-03-12 15:48:16 -06005215 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005216 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005217 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005218 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005219 spin_unlock_irq(&ctx->completion_lock);
5220
Jens Axboe8c838782019-03-12 15:48:16 -06005221 if (mask) {
5222 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005223 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005224 }
Jens Axboe8c838782019-03-12 15:48:16 -06005225 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005226}
5227
Jens Axboe5262f562019-09-17 12:26:57 -06005228static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5229{
Jens Axboead8a48a2019-11-15 08:49:11 -07005230 struct io_timeout_data *data = container_of(timer,
5231 struct io_timeout_data, timer);
5232 struct io_kiocb *req = data->req;
5233 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005234 unsigned long flags;
5235
Jens Axboe5262f562019-09-17 12:26:57 -06005236 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005237 atomic_set(&req->ctx->cq_timeouts,
5238 atomic_read(&req->ctx->cq_timeouts) + 1);
5239
zhangyi (F)ef036812019-10-23 15:10:08 +08005240 /*
Jens Axboe11365042019-10-16 09:08:32 -06005241 * We could be racing with timeout deletion. If the list is empty,
5242 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005243 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005244 if (!list_empty(&req->timeout.list))
5245 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005246
Jens Axboe78e19bb2019-11-06 15:21:34 -07005247 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005248 io_commit_cqring(ctx);
5249 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5250
5251 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005252 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005253 io_put_req(req);
5254 return HRTIMER_NORESTART;
5255}
5256
Jens Axboef254ac02020-08-12 17:33:30 -06005257static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005258{
Jens Axboef254ac02020-08-12 17:33:30 -06005259 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005260
Jens Axboef254ac02020-08-12 17:33:30 -06005261 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005262
Jens Axboe2d283902019-12-04 11:08:05 -07005263 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005264 if (ret == -1)
5265 return -EALREADY;
5266
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005267 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005268 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005269 io_cqring_fill_event(req, -ECANCELED);
5270 io_put_req(req);
5271 return 0;
5272}
5273
Jens Axboef254ac02020-08-12 17:33:30 -06005274static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5275{
5276 struct io_kiocb *req;
5277 int ret = -ENOENT;
5278
5279 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5280 if (user_data == req->user_data) {
5281 ret = 0;
5282 break;
5283 }
5284 }
5285
5286 if (ret == -ENOENT)
5287 return ret;
5288
5289 return __io_timeout_cancel(req);
5290}
5291
Jens Axboe3529d8c2019-12-19 18:24:38 -07005292static int io_timeout_remove_prep(struct io_kiocb *req,
5293 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005294{
Jens Axboeb29472e2019-12-17 18:50:29 -07005295 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5296 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005297 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5298 return -EINVAL;
5299 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005300 return -EINVAL;
5301
5302 req->timeout.addr = READ_ONCE(sqe->addr);
5303 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5304 if (req->timeout.flags)
5305 return -EINVAL;
5306
Jens Axboeb29472e2019-12-17 18:50:29 -07005307 return 0;
5308}
5309
Jens Axboe11365042019-10-16 09:08:32 -06005310/*
5311 * Remove or update an existing timeout command
5312 */
Jens Axboefc4df992019-12-10 14:38:45 -07005313static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005314{
5315 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005316 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005317
Jens Axboe11365042019-10-16 09:08:32 -06005318 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005319 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005320
Jens Axboe47f46762019-11-09 17:43:02 -07005321 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005322 io_commit_cqring(ctx);
5323 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005324 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005325 if (ret < 0)
5326 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005327 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005328 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005329}
5330
Jens Axboe3529d8c2019-12-19 18:24:38 -07005331static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005332 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005333{
Jens Axboead8a48a2019-11-15 08:49:11 -07005334 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005335 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005336 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005337
Jens Axboead8a48a2019-11-15 08:49:11 -07005338 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005339 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005340 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005341 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005342 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005343 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005344 flags = READ_ONCE(sqe->timeout_flags);
5345 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005346 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005347
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005348 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005349
Jens Axboe3529d8c2019-12-19 18:24:38 -07005350 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005351 return -ENOMEM;
5352
5353 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005354 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005355
5356 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005357 return -EFAULT;
5358
Jens Axboe11365042019-10-16 09:08:32 -06005359 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005360 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005361 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005362 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005363
Jens Axboead8a48a2019-11-15 08:49:11 -07005364 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5365 return 0;
5366}
5367
Jens Axboefc4df992019-12-10 14:38:45 -07005368static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005369{
Jens Axboead8a48a2019-11-15 08:49:11 -07005370 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005371 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005372 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005373 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005374
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005375 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005376
Jens Axboe5262f562019-09-17 12:26:57 -06005377 /*
5378 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005379 * timeout event to be satisfied. If it isn't set, then this is
5380 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005381 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005382 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005383 entry = ctx->timeout_list.prev;
5384 goto add;
5385 }
Jens Axboe5262f562019-09-17 12:26:57 -06005386
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005387 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5388 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005389
5390 /*
5391 * Insertion sort, ensuring the first entry in the list is always
5392 * the one we need first.
5393 */
Jens Axboe5262f562019-09-17 12:26:57 -06005394 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005395 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5396 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005397
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005398 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005399 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005400 /* nxt.seq is behind @tail, otherwise would've been completed */
5401 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005402 break;
5403 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005404add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005405 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005406 data->timer.function = io_timeout_fn;
5407 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005408 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005409 return 0;
5410}
5411
Jens Axboe62755e32019-10-28 21:49:21 -06005412static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005413{
Jens Axboe62755e32019-10-28 21:49:21 -06005414 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005415
Jens Axboe62755e32019-10-28 21:49:21 -06005416 return req->user_data == (unsigned long) data;
5417}
5418
Jens Axboee977d6d2019-11-05 12:39:45 -07005419static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005420{
Jens Axboe62755e32019-10-28 21:49:21 -06005421 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005422 int ret = 0;
5423
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005424 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005425 switch (cancel_ret) {
5426 case IO_WQ_CANCEL_OK:
5427 ret = 0;
5428 break;
5429 case IO_WQ_CANCEL_RUNNING:
5430 ret = -EALREADY;
5431 break;
5432 case IO_WQ_CANCEL_NOTFOUND:
5433 ret = -ENOENT;
5434 break;
5435 }
5436
Jens Axboee977d6d2019-11-05 12:39:45 -07005437 return ret;
5438}
5439
Jens Axboe47f46762019-11-09 17:43:02 -07005440static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5441 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005442 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005443{
5444 unsigned long flags;
5445 int ret;
5446
5447 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5448 if (ret != -ENOENT) {
5449 spin_lock_irqsave(&ctx->completion_lock, flags);
5450 goto done;
5451 }
5452
5453 spin_lock_irqsave(&ctx->completion_lock, flags);
5454 ret = io_timeout_cancel(ctx, sqe_addr);
5455 if (ret != -ENOENT)
5456 goto done;
5457 ret = io_poll_cancel(ctx, sqe_addr);
5458done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005459 if (!ret)
5460 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005461 io_cqring_fill_event(req, ret);
5462 io_commit_cqring(ctx);
5463 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5464 io_cqring_ev_posted(ctx);
5465
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005466 if (ret < 0)
5467 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005468 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005469}
5470
Jens Axboe3529d8c2019-12-19 18:24:38 -07005471static int io_async_cancel_prep(struct io_kiocb *req,
5472 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005473{
Jens Axboefbf23842019-12-17 18:45:56 -07005474 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005475 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005476 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5477 return -EINVAL;
5478 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005479 return -EINVAL;
5480
Jens Axboefbf23842019-12-17 18:45:56 -07005481 req->cancel.addr = READ_ONCE(sqe->addr);
5482 return 0;
5483}
5484
Pavel Begunkov014db002020-03-03 21:33:12 +03005485static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005486{
5487 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005488
Pavel Begunkov014db002020-03-03 21:33:12 +03005489 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005490 return 0;
5491}
5492
Jens Axboe05f3fb32019-12-09 11:22:50 -07005493static int io_files_update_prep(struct io_kiocb *req,
5494 const struct io_uring_sqe *sqe)
5495{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005496 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5497 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005498 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5499 return -EINVAL;
5500 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005501 return -EINVAL;
5502
5503 req->files_update.offset = READ_ONCE(sqe->off);
5504 req->files_update.nr_args = READ_ONCE(sqe->len);
5505 if (!req->files_update.nr_args)
5506 return -EINVAL;
5507 req->files_update.arg = READ_ONCE(sqe->addr);
5508 return 0;
5509}
5510
Jens Axboe229a7b62020-06-22 10:13:11 -06005511static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5512 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005513{
5514 struct io_ring_ctx *ctx = req->ctx;
5515 struct io_uring_files_update up;
5516 int ret;
5517
Jens Axboef86cd202020-01-29 13:46:44 -07005518 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005519 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005520
5521 up.offset = req->files_update.offset;
5522 up.fds = req->files_update.arg;
5523
5524 mutex_lock(&ctx->uring_lock);
5525 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5526 mutex_unlock(&ctx->uring_lock);
5527
5528 if (ret < 0)
5529 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005530 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005531 return 0;
5532}
5533
Jens Axboe3529d8c2019-12-19 18:24:38 -07005534static int io_req_defer_prep(struct io_kiocb *req,
5535 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005536{
Jens Axboee7815732019-12-17 19:45:06 -07005537 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005538
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005539 if (!sqe)
5540 return 0;
5541
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005542 if (io_alloc_async_ctx(req))
5543 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005544 ret = io_prep_work_files(req);
5545 if (unlikely(ret))
5546 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005547
Jens Axboe202700e12020-09-12 13:18:10 -06005548 io_prep_async_work(req);
5549
Jens Axboed625c6e2019-12-17 19:53:05 -07005550 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005551 case IORING_OP_NOP:
5552 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005553 case IORING_OP_READV:
5554 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005555 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005556 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005557 break;
5558 case IORING_OP_WRITEV:
5559 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005560 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005561 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005562 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005563 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005564 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005565 break;
5566 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005567 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005568 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005569 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005570 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005571 break;
5572 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005573 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005574 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005575 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005576 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005577 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005578 break;
5579 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005580 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005581 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005582 break;
Jens Axboef499a022019-12-02 16:28:46 -07005583 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005584 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005585 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005586 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005587 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005588 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005589 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005590 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005591 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005592 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005593 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005594 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005595 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005596 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005597 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005598 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005599 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005600 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005601 case IORING_OP_FALLOCATE:
5602 ret = io_fallocate_prep(req, sqe);
5603 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005604 case IORING_OP_OPENAT:
5605 ret = io_openat_prep(req, sqe);
5606 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005607 case IORING_OP_CLOSE:
5608 ret = io_close_prep(req, sqe);
5609 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005610 case IORING_OP_FILES_UPDATE:
5611 ret = io_files_update_prep(req, sqe);
5612 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005613 case IORING_OP_STATX:
5614 ret = io_statx_prep(req, sqe);
5615 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005616 case IORING_OP_FADVISE:
5617 ret = io_fadvise_prep(req, sqe);
5618 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005619 case IORING_OP_MADVISE:
5620 ret = io_madvise_prep(req, sqe);
5621 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005622 case IORING_OP_OPENAT2:
5623 ret = io_openat2_prep(req, sqe);
5624 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005625 case IORING_OP_EPOLL_CTL:
5626 ret = io_epoll_ctl_prep(req, sqe);
5627 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005628 case IORING_OP_SPLICE:
5629 ret = io_splice_prep(req, sqe);
5630 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005631 case IORING_OP_PROVIDE_BUFFERS:
5632 ret = io_provide_buffers_prep(req, sqe);
5633 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005634 case IORING_OP_REMOVE_BUFFERS:
5635 ret = io_remove_buffers_prep(req, sqe);
5636 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005637 case IORING_OP_TEE:
5638 ret = io_tee_prep(req, sqe);
5639 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005640 default:
Jens Axboee7815732019-12-17 19:45:06 -07005641 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5642 req->opcode);
5643 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005644 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005645 }
5646
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005647 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005648}
5649
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005650static u32 io_get_sequence(struct io_kiocb *req)
5651{
5652 struct io_kiocb *pos;
5653 struct io_ring_ctx *ctx = req->ctx;
5654 u32 total_submitted, nr_reqs = 1;
5655
5656 if (req->flags & REQ_F_LINK_HEAD)
5657 list_for_each_entry(pos, &req->link_list, link_list)
5658 nr_reqs++;
5659
5660 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5661 return total_submitted - nr_reqs;
5662}
5663
Jens Axboe3529d8c2019-12-19 18:24:38 -07005664static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005665{
Jackie Liua197f662019-11-08 08:09:12 -07005666 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005667 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005668 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005669 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005670
Bob Liu9d858b22019-11-13 18:06:25 +08005671 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005672 if (likely(list_empty_careful(&ctx->defer_list) &&
5673 !(req->flags & REQ_F_IO_DRAIN)))
5674 return 0;
5675
5676 seq = io_get_sequence(req);
5677 /* Still a chance to pass the sequence check */
5678 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005679 return 0;
5680
Pavel Begunkov650b5482020-05-17 14:02:11 +03005681 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005682 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005683 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005684 return ret;
5685 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005686 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005687 de = kmalloc(sizeof(*de), GFP_KERNEL);
5688 if (!de)
5689 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005690
Jens Axboede0617e2019-04-06 21:51:27 -06005691 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005692 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005693 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005694 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005695 io_queue_async_work(req);
5696 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005697 }
5698
Jens Axboe915967f2019-11-21 09:01:20 -07005699 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005700 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005701 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005702 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005703 spin_unlock_irq(&ctx->completion_lock);
5704 return -EIOCBQUEUED;
5705}
5706
Jens Axboef573d382020-09-22 10:19:24 -06005707static void io_req_drop_files(struct io_kiocb *req)
5708{
5709 struct io_ring_ctx *ctx = req->ctx;
5710 unsigned long flags;
5711
5712 spin_lock_irqsave(&ctx->inflight_lock, flags);
5713 list_del(&req->inflight_entry);
5714 if (waitqueue_active(&ctx->inflight_wait))
5715 wake_up(&ctx->inflight_wait);
5716 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5717 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005718 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005719 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005720 req->work.files = NULL;
5721}
5722
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005723static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005724{
5725 struct io_async_ctx *io = req->io;
5726
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005727 if (req->flags & REQ_F_BUFFER_SELECTED) {
5728 switch (req->opcode) {
5729 case IORING_OP_READV:
5730 case IORING_OP_READ_FIXED:
5731 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005732 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005733 break;
5734 case IORING_OP_RECVMSG:
5735 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005736 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005737 break;
5738 }
5739 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005740 }
5741
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005742 if (req->flags & REQ_F_NEED_CLEANUP) {
5743 switch (req->opcode) {
5744 case IORING_OP_READV:
5745 case IORING_OP_READ_FIXED:
5746 case IORING_OP_READ:
5747 case IORING_OP_WRITEV:
5748 case IORING_OP_WRITE_FIXED:
5749 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005750 if (io->rw.free_iovec)
5751 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005752 break;
5753 case IORING_OP_RECVMSG:
5754 case IORING_OP_SENDMSG:
5755 if (io->msg.iov != io->msg.fast_iov)
5756 kfree(io->msg.iov);
5757 break;
5758 case IORING_OP_SPLICE:
5759 case IORING_OP_TEE:
5760 io_put_file(req, req->splice.file_in,
5761 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5762 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005763 case IORING_OP_OPENAT:
5764 case IORING_OP_OPENAT2:
5765 if (req->open.filename)
5766 putname(req->open.filename);
5767 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005768 }
5769 req->flags &= ~REQ_F_NEED_CLEANUP;
5770 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005771
Jens Axboef573d382020-09-22 10:19:24 -06005772 if (req->flags & REQ_F_INFLIGHT)
5773 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005774}
5775
Jens Axboe3529d8c2019-12-19 18:24:38 -07005776static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005777 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005778{
Jackie Liua197f662019-11-08 08:09:12 -07005779 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005780 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005781
Jens Axboed625c6e2019-12-17 19:53:05 -07005782 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005783 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005784 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005785 break;
5786 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005787 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005788 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005789 if (sqe) {
5790 ret = io_read_prep(req, sqe, force_nonblock);
5791 if (ret < 0)
5792 break;
5793 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005794 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005795 break;
5796 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005797 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005798 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005799 if (sqe) {
5800 ret = io_write_prep(req, sqe, force_nonblock);
5801 if (ret < 0)
5802 break;
5803 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005804 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005805 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005806 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005807 if (sqe) {
5808 ret = io_prep_fsync(req, sqe);
5809 if (ret < 0)
5810 break;
5811 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005812 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005813 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005814 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005815 if (sqe) {
5816 ret = io_poll_add_prep(req, sqe);
5817 if (ret)
5818 break;
5819 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005820 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005821 break;
5822 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005823 if (sqe) {
5824 ret = io_poll_remove_prep(req, sqe);
5825 if (ret < 0)
5826 break;
5827 }
Jens Axboefc4df992019-12-10 14:38:45 -07005828 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005829 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005830 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005831 if (sqe) {
5832 ret = io_prep_sfr(req, sqe);
5833 if (ret < 0)
5834 break;
5835 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005836 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005837 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005838 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005839 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005840 if (sqe) {
5841 ret = io_sendmsg_prep(req, sqe);
5842 if (ret < 0)
5843 break;
5844 }
Jens Axboefddafac2020-01-04 20:19:44 -07005845 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005846 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005847 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005848 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005849 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005850 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005851 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005852 if (sqe) {
5853 ret = io_recvmsg_prep(req, sqe);
5854 if (ret)
5855 break;
5856 }
Jens Axboefddafac2020-01-04 20:19:44 -07005857 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005858 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005859 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005860 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005861 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005862 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005863 if (sqe) {
5864 ret = io_timeout_prep(req, sqe, false);
5865 if (ret)
5866 break;
5867 }
Jens Axboefc4df992019-12-10 14:38:45 -07005868 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005869 break;
Jens Axboe11365042019-10-16 09:08:32 -06005870 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005871 if (sqe) {
5872 ret = io_timeout_remove_prep(req, sqe);
5873 if (ret)
5874 break;
5875 }
Jens Axboefc4df992019-12-10 14:38:45 -07005876 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005877 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005878 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005879 if (sqe) {
5880 ret = io_accept_prep(req, sqe);
5881 if (ret)
5882 break;
5883 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005884 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005885 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005886 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005887 if (sqe) {
5888 ret = io_connect_prep(req, sqe);
5889 if (ret)
5890 break;
5891 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005892 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005893 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005894 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005895 if (sqe) {
5896 ret = io_async_cancel_prep(req, sqe);
5897 if (ret)
5898 break;
5899 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005900 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005901 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005902 case IORING_OP_FALLOCATE:
5903 if (sqe) {
5904 ret = io_fallocate_prep(req, sqe);
5905 if (ret)
5906 break;
5907 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005908 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005909 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005910 case IORING_OP_OPENAT:
5911 if (sqe) {
5912 ret = io_openat_prep(req, sqe);
5913 if (ret)
5914 break;
5915 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005916 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005917 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005918 case IORING_OP_CLOSE:
5919 if (sqe) {
5920 ret = io_close_prep(req, sqe);
5921 if (ret)
5922 break;
5923 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005924 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005925 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005926 case IORING_OP_FILES_UPDATE:
5927 if (sqe) {
5928 ret = io_files_update_prep(req, sqe);
5929 if (ret)
5930 break;
5931 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005932 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005933 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005934 case IORING_OP_STATX:
5935 if (sqe) {
5936 ret = io_statx_prep(req, sqe);
5937 if (ret)
5938 break;
5939 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005940 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005941 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005942 case IORING_OP_FADVISE:
5943 if (sqe) {
5944 ret = io_fadvise_prep(req, sqe);
5945 if (ret)
5946 break;
5947 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005948 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005949 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005950 case IORING_OP_MADVISE:
5951 if (sqe) {
5952 ret = io_madvise_prep(req, sqe);
5953 if (ret)
5954 break;
5955 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005956 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005957 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005958 case IORING_OP_OPENAT2:
5959 if (sqe) {
5960 ret = io_openat2_prep(req, sqe);
5961 if (ret)
5962 break;
5963 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005964 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005965 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005966 case IORING_OP_EPOLL_CTL:
5967 if (sqe) {
5968 ret = io_epoll_ctl_prep(req, sqe);
5969 if (ret)
5970 break;
5971 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005972 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005973 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005974 case IORING_OP_SPLICE:
5975 if (sqe) {
5976 ret = io_splice_prep(req, sqe);
5977 if (ret < 0)
5978 break;
5979 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005980 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005981 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005982 case IORING_OP_PROVIDE_BUFFERS:
5983 if (sqe) {
5984 ret = io_provide_buffers_prep(req, sqe);
5985 if (ret)
5986 break;
5987 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005988 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005989 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005990 case IORING_OP_REMOVE_BUFFERS:
5991 if (sqe) {
5992 ret = io_remove_buffers_prep(req, sqe);
5993 if (ret)
5994 break;
5995 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005996 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005997 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005998 case IORING_OP_TEE:
5999 if (sqe) {
6000 ret = io_tee_prep(req, sqe);
6001 if (ret < 0)
6002 break;
6003 }
6004 ret = io_tee(req, force_nonblock);
6005 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006006 default:
6007 ret = -EINVAL;
6008 break;
6009 }
6010
6011 if (ret)
6012 return ret;
6013
Jens Axboeb5325762020-05-19 21:20:27 -06006014 /* If the op doesn't have a file, we're not polling for it */
6015 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006016 const bool in_async = io_wq_current_is_worker();
6017
Jens Axboe11ba8202020-01-15 21:51:17 -07006018 /* workqueue context doesn't hold uring_lock, grab it now */
6019 if (in_async)
6020 mutex_lock(&ctx->uring_lock);
6021
Jens Axboe2b188cc2019-01-07 10:46:33 -07006022 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006023
6024 if (in_async)
6025 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006026 }
6027
6028 return 0;
6029}
6030
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006031static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006032{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006033 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006034 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006035 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006036
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006037 timeout = io_prep_linked_timeout(req);
6038 if (timeout)
6039 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006040
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006041 /* if NO_CANCEL is set, we must still run the work */
6042 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6043 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006044 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006045 }
Jens Axboe31b51512019-01-18 22:56:34 -07006046
Jens Axboe561fb042019-10-24 07:25:42 -06006047 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006048 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006049 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006050 /*
6051 * We can get EAGAIN for polled IO even though we're
6052 * forcing a sync submission from here, since we can't
6053 * wait for request slots on the block side.
6054 */
6055 if (ret != -EAGAIN)
6056 break;
6057 cond_resched();
6058 } while (1);
6059 }
Jens Axboe31b51512019-01-18 22:56:34 -07006060
Jens Axboe561fb042019-10-24 07:25:42 -06006061 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006062 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006063 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006064 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006065
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006066 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006067}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006068
Jens Axboe65e19f52019-10-26 07:20:21 -06006069static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6070 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006071{
Jens Axboe65e19f52019-10-26 07:20:21 -06006072 struct fixed_file_table *table;
6073
Jens Axboe05f3fb32019-12-09 11:22:50 -07006074 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006075 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006076}
6077
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006078static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6079 int fd, struct file **out_file, bool fixed)
6080{
6081 struct io_ring_ctx *ctx = req->ctx;
6082 struct file *file;
6083
6084 if (fixed) {
6085 if (unlikely(!ctx->file_data ||
6086 (unsigned) fd >= ctx->nr_user_files))
6087 return -EBADF;
6088 fd = array_index_nospec(fd, ctx->nr_user_files);
6089 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006090 if (file) {
6091 req->fixed_file_refs = ctx->file_data->cur_refs;
6092 percpu_ref_get(req->fixed_file_refs);
6093 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006094 } else {
6095 trace_io_uring_file_get(ctx, fd);
6096 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006097 }
6098
Jens Axboefd2206e2020-06-02 16:40:47 -06006099 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6100 *out_file = file;
6101 return 0;
6102 }
6103 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006104}
6105
Jens Axboe3529d8c2019-12-19 18:24:38 -07006106static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006107 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006108{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006109 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006110
Jens Axboe63ff8222020-05-07 14:56:15 -06006111 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006112 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006113 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006114
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006115 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006116}
6117
Jackie Liua197f662019-11-08 08:09:12 -07006118static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006119{
Jackie Liua197f662019-11-08 08:09:12 -07006120 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006121
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006122 io_req_init_async(req);
6123
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006124 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006125 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006126
Jens Axboe0f212202020-09-13 13:09:39 -06006127 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006128 get_nsproxy(current->nsproxy);
6129 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006130 req->flags |= REQ_F_INFLIGHT;
6131
Jens Axboefcb323c2019-10-24 12:39:47 -06006132 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006133 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006134 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006135 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006136}
6137
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006138static inline int io_prep_work_files(struct io_kiocb *req)
6139{
6140 if (!io_op_defs[req->opcode].file_table)
6141 return 0;
6142 return io_grab_files(req);
6143}
6144
Jens Axboe2665abf2019-11-05 12:40:47 -07006145static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6146{
Jens Axboead8a48a2019-11-15 08:49:11 -07006147 struct io_timeout_data *data = container_of(timer,
6148 struct io_timeout_data, timer);
6149 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006150 struct io_ring_ctx *ctx = req->ctx;
6151 struct io_kiocb *prev = NULL;
6152 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006153
6154 spin_lock_irqsave(&ctx->completion_lock, flags);
6155
6156 /*
6157 * We don't expect the list to be empty, that will only happen if we
6158 * race with the completion of the linked work.
6159 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006160 if (!list_empty(&req->link_list)) {
6161 prev = list_entry(req->link_list.prev, struct io_kiocb,
6162 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006163 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006164 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006165 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6166 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006167 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006168 }
6169
6170 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6171
6172 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006173 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006174 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006175 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006176 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006177 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006178 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006179 return HRTIMER_NORESTART;
6180}
6181
Jens Axboe7271ef32020-08-10 09:55:22 -06006182static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006183{
Jens Axboe76a46e02019-11-10 23:34:16 -07006184 /*
6185 * If the list is now empty, then our linked request finished before
6186 * we got a chance to setup the timer
6187 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006188 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006189 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006190
Jens Axboead8a48a2019-11-15 08:49:11 -07006191 data->timer.function = io_link_timeout_fn;
6192 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6193 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006194 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006195}
6196
6197static void io_queue_linked_timeout(struct io_kiocb *req)
6198{
6199 struct io_ring_ctx *ctx = req->ctx;
6200
6201 spin_lock_irq(&ctx->completion_lock);
6202 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006203 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006204
Jens Axboe2665abf2019-11-05 12:40:47 -07006205 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006206 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006207}
6208
Jens Axboead8a48a2019-11-15 08:49:11 -07006209static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006210{
6211 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006212
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006213 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006214 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006215 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006216 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006217
Pavel Begunkov44932332019-12-05 16:16:35 +03006218 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6219 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006220 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006221 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006222
Jens Axboe76a46e02019-11-10 23:34:16 -07006223 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006224 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006225}
6226
Jens Axboef13fad72020-06-22 09:34:30 -06006227static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6228 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006229{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006230 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006231 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006232 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006233 int ret;
6234
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006235again:
6236 linked_timeout = io_prep_linked_timeout(req);
6237
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006238 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6239 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006240 if (old_creds)
6241 revert_creds(old_creds);
6242 if (old_creds == req->work.creds)
6243 old_creds = NULL; /* restored original creds */
6244 else
6245 old_creds = override_creds(req->work.creds);
6246 }
6247
Jens Axboef13fad72020-06-22 09:34:30 -06006248 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006249
6250 /*
6251 * We async punt it if the file wasn't marked NOWAIT, or if the file
6252 * doesn't support non-blocking read/write attempts
6253 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006254 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006255 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006256punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006257 ret = io_prep_work_files(req);
6258 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006259 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006260 /*
6261 * Queued up for async execution, worker will release
6262 * submit reference when the iocb is actually submitted.
6263 */
6264 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006266
Pavel Begunkovf063c542020-07-25 14:41:59 +03006267 if (linked_timeout)
6268 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006269 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006270 }
Jens Axboee65ef562019-03-12 10:16:44 -06006271
Pavel Begunkov652532a2020-07-03 22:15:07 +03006272 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006273err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006274 /* un-prep timeout, so it'll be killed as any other linked */
6275 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006276 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006277 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006278 io_req_complete(req, ret);
6279 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006280 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006281
Jens Axboe6c271ce2019-01-10 11:22:30 -07006282 /* drop submission reference */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03006283 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006284 if (linked_timeout)
6285 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006286
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006287 if (nxt) {
6288 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006289
6290 if (req->flags & REQ_F_FORCE_ASYNC)
6291 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006292 goto again;
6293 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006294exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006295 if (old_creds)
6296 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006297}
6298
Jens Axboef13fad72020-06-22 09:34:30 -06006299static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6300 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006301{
6302 int ret;
6303
Jens Axboe3529d8c2019-12-19 18:24:38 -07006304 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006305 if (ret) {
6306 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006307fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006308 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006309 io_put_req(req);
6310 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006311 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006312 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006313 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006314 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006315 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006316 goto fail_req;
6317 }
6318
Jens Axboece35a472019-12-17 08:04:44 -07006319 /*
6320 * Never try inline submit of IOSQE_ASYNC is set, go straight
6321 * to async execution.
6322 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006323 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006324 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6325 io_queue_async_work(req);
6326 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006327 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006328 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006329}
6330
Jens Axboef13fad72020-06-22 09:34:30 -06006331static inline void io_queue_link_head(struct io_kiocb *req,
6332 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006333{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006334 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006335 io_put_req(req);
6336 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006337 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006338 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006339}
6340
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006341static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006342 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006343{
Jackie Liua197f662019-11-08 08:09:12 -07006344 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006345 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006346
Jens Axboe9e645e112019-05-10 16:07:28 -06006347 /*
6348 * If we already have a head request, queue this one for async
6349 * submittal once the head completes. If we don't have a head but
6350 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6351 * submitted sync once the chain is complete. If none of those
6352 * conditions are true (normal request), then just queue it.
6353 */
6354 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006355 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006356
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006357 /*
6358 * Taking sequential execution of a link, draining both sides
6359 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6360 * requests in the link. So, it drains the head and the
6361 * next after the link request. The last one is done via
6362 * drain_next flag to persist the effect across calls.
6363 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006364 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006365 head->flags |= REQ_F_IO_DRAIN;
6366 ctx->drain_next = 1;
6367 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006368 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006369 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006370 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006371 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006372 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006373 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006374 trace_io_uring_link(ctx, req, head);
6375 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006376
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006377 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006378 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006379 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006380 *link = NULL;
6381 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006382 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006383 if (unlikely(ctx->drain_next)) {
6384 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006385 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006386 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006387 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006388 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006389 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006390
Pavel Begunkov711be032020-01-17 03:57:59 +03006391 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006392 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006393 req->flags |= REQ_F_FAIL_LINK;
6394 *link = req;
6395 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006396 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006397 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006398 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006399
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006400 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006401}
6402
Jens Axboe9a56a232019-01-09 09:06:50 -07006403/*
6404 * Batched submission is done, ensure local IO is flushed out.
6405 */
6406static void io_submit_state_end(struct io_submit_state *state)
6407{
Jens Axboef13fad72020-06-22 09:34:30 -06006408 if (!list_empty(&state->comp.list))
6409 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006410 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006411 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006412 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006413 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006414}
6415
6416/*
6417 * Start submission side cache.
6418 */
6419static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006420 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006421{
6422 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006423 state->comp.nr = 0;
6424 INIT_LIST_HEAD(&state->comp.list);
6425 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006426 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006427 state->file = NULL;
6428 state->ios_left = max_ios;
6429}
6430
Jens Axboe2b188cc2019-01-07 10:46:33 -07006431static void io_commit_sqring(struct io_ring_ctx *ctx)
6432{
Hristo Venev75b28af2019-08-26 17:23:46 +00006433 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006434
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006435 /*
6436 * Ensure any loads from the SQEs are done at this point,
6437 * since once we write the new head, the application could
6438 * write new data to them.
6439 */
6440 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006441}
6442
6443/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006444 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006445 * that is mapped by userspace. This means that care needs to be taken to
6446 * ensure that reads are stable, as we cannot rely on userspace always
6447 * being a good citizen. If members of the sqe are validated and then later
6448 * used, it's important that those reads are done through READ_ONCE() to
6449 * prevent a re-load down the line.
6450 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006451static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006452{
Hristo Venev75b28af2019-08-26 17:23:46 +00006453 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006454 unsigned head;
6455
6456 /*
6457 * The cached sq head (or cq tail) serves two purposes:
6458 *
6459 * 1) allows us to batch the cost of updating the user visible
6460 * head updates.
6461 * 2) allows the kernel side to track the head on its own, even
6462 * though the application is the one updating it.
6463 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006464 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006465 if (likely(head < ctx->sq_entries))
6466 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006467
6468 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006469 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006470 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006471 return NULL;
6472}
6473
6474static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6475{
6476 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006477}
6478
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006479/*
6480 * Check SQE restrictions (opcode and flags).
6481 *
6482 * Returns 'true' if SQE is allowed, 'false' otherwise.
6483 */
6484static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6485 struct io_kiocb *req,
6486 unsigned int sqe_flags)
6487{
6488 if (!ctx->restricted)
6489 return true;
6490
6491 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6492 return false;
6493
6494 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6495 ctx->restrictions.sqe_flags_required)
6496 return false;
6497
6498 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6499 ctx->restrictions.sqe_flags_required))
6500 return false;
6501
6502 return true;
6503}
6504
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006505#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6506 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6507 IOSQE_BUFFER_SELECT)
6508
6509static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6510 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006511 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006512{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006513 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006514 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006515
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006516 req->opcode = READ_ONCE(sqe->opcode);
6517 req->user_data = READ_ONCE(sqe->user_data);
6518 req->io = NULL;
6519 req->file = NULL;
6520 req->ctx = ctx;
6521 req->flags = 0;
6522 /* one is dropped after submission, the other at completion */
6523 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006524 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006525 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006526 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006527 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006528
6529 if (unlikely(req->opcode >= IORING_OP_LAST))
6530 return -EINVAL;
6531
Jens Axboe9d8426a2020-06-16 18:42:49 -06006532 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6533 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006534
6535 sqe_flags = READ_ONCE(sqe->flags);
6536 /* enforce forwards compatibility on users */
6537 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6538 return -EINVAL;
6539
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006540 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6541 return -EACCES;
6542
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006543 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6544 !io_op_defs[req->opcode].buffer_select)
6545 return -EOPNOTSUPP;
6546
6547 id = READ_ONCE(sqe->personality);
6548 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006549 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006550 req->work.creds = idr_find(&ctx->personality_idr, id);
6551 if (unlikely(!req->work.creds))
6552 return -EINVAL;
6553 get_cred(req->work.creds);
6554 }
6555
6556 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006557 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006558
Jens Axboe63ff8222020-05-07 14:56:15 -06006559 if (!io_op_defs[req->opcode].needs_file)
6560 return 0;
6561
6562 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006563}
6564
Jens Axboe0f212202020-09-13 13:09:39 -06006565static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006566{
Jens Axboeac8691c2020-06-01 08:30:41 -06006567 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006568 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006569 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006570
Jens Axboec4a2ed72019-11-21 21:01:26 -07006571 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006572 if (test_bit(0, &ctx->sq_check_overflow)) {
6573 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006574 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006575 return -EBUSY;
6576 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006577
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006578 /* make sure SQ entry isn't read before tail */
6579 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006580
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006581 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6582 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006583
Jens Axboe013538b2020-06-22 09:29:15 -06006584 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006585
6586 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006587 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006588 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006589 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006590
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006591 sqe = io_get_sqe(ctx);
6592 if (unlikely(!sqe)) {
6593 io_consume_sqe(ctx);
6594 break;
6595 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006596 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006597 if (unlikely(!req)) {
6598 if (!submitted)
6599 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006600 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006601 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006602
Jens Axboeac8691c2020-06-01 08:30:41 -06006603 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006604 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006605 /* will complete beyond this point, count as submitted */
6606 submitted++;
6607
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006608 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006609fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006610 io_put_req(req);
6611 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006612 break;
6613 }
6614
Jens Axboe354420f2020-01-08 18:55:15 -07006615 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006616 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006617 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006618 if (err)
6619 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006620 }
6621
Pavel Begunkov9466f432020-01-25 22:34:01 +03006622 if (unlikely(submitted != nr)) {
6623 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6624
6625 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6626 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006627 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006628 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006629 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006630
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006631 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6632 io_commit_sqring(ctx);
6633
Jens Axboe6c271ce2019-01-10 11:22:30 -07006634 return submitted;
6635}
6636
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006637static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6638{
6639 /* Tell userspace we may need a wakeup call */
6640 spin_lock_irq(&ctx->completion_lock);
6641 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6642 spin_unlock_irq(&ctx->completion_lock);
6643}
6644
6645static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6646{
6647 spin_lock_irq(&ctx->completion_lock);
6648 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6649 spin_unlock_irq(&ctx->completion_lock);
6650}
6651
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006652static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6653 int sync, void *key)
6654{
6655 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6656 int ret;
6657
6658 ret = autoremove_wake_function(wqe, mode, sync, key);
6659 if (ret) {
6660 unsigned long flags;
6661
6662 spin_lock_irqsave(&ctx->completion_lock, flags);
6663 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6664 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6665 }
6666 return ret;
6667}
6668
Jens Axboec8d1ba52020-09-14 11:07:26 -06006669enum sq_ret {
6670 SQT_IDLE = 1,
6671 SQT_SPIN = 2,
6672 SQT_DID_WORK = 4,
6673};
6674
6675static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
6676 unsigned long start_jiffies)
6677{
6678 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006679 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006680 unsigned int to_submit;
6681 int ret = 0;
6682
6683again:
6684 if (!list_empty(&ctx->iopoll_list)) {
6685 unsigned nr_events = 0;
6686
6687 mutex_lock(&ctx->uring_lock);
6688 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6689 io_do_iopoll(ctx, &nr_events, 0);
6690 mutex_unlock(&ctx->uring_lock);
6691 }
6692
6693 to_submit = io_sqring_entries(ctx);
6694
6695 /*
6696 * If submit got -EBUSY, flag us as needing the application
6697 * to enter the kernel to reap and flush events.
6698 */
6699 if (!to_submit || ret == -EBUSY || need_resched()) {
6700 /*
6701 * Drop cur_mm before scheduling, we can't hold it for
6702 * long periods (or over schedule()). Do this before
6703 * adding ourselves to the waitqueue, as the unuse/drop
6704 * may sleep.
6705 */
6706 io_sq_thread_drop_mm();
6707
6708 /*
6709 * We're polling. If we're within the defined idle
6710 * period, then let us spin without work before going
6711 * to sleep. The exception is if we got EBUSY doing
6712 * more IO, we should wait for the application to
6713 * reap events and wake us up.
6714 */
6715 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6716 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6717 !percpu_ref_is_dying(&ctx->refs)))
6718 return SQT_SPIN;
6719
Jens Axboe534ca6d2020-09-02 13:52:19 -06006720 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006721 TASK_INTERRUPTIBLE);
6722
6723 /*
6724 * While doing polled IO, before going to sleep, we need
6725 * to check if there are new reqs added to iopoll_list,
6726 * it is because reqs may have been punted to io worker
6727 * and will be added to iopoll_list later, hence check
6728 * the iopoll_list again.
6729 */
6730 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6731 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006732 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006733 goto again;
6734 }
6735
Jens Axboec8d1ba52020-09-14 11:07:26 -06006736 to_submit = io_sqring_entries(ctx);
6737 if (!to_submit || ret == -EBUSY)
6738 return SQT_IDLE;
6739 }
6740
Jens Axboe534ca6d2020-09-02 13:52:19 -06006741 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006742 io_ring_clear_wakeup_flag(ctx);
6743
6744 mutex_lock(&ctx->uring_lock);
6745 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6746 ret = io_submit_sqes(ctx, to_submit);
6747 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006748
6749 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6750 wake_up(&ctx->sqo_sq_wait);
6751
Jens Axboec8d1ba52020-09-14 11:07:26 -06006752 return SQT_DID_WORK;
6753}
6754
Jens Axboe69fb2132020-09-14 11:16:23 -06006755static void io_sqd_init_new(struct io_sq_data *sqd)
6756{
6757 struct io_ring_ctx *ctx;
6758
6759 while (!list_empty(&sqd->ctx_new_list)) {
6760 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6761 init_wait(&ctx->sqo_wait_entry);
6762 ctx->sqo_wait_entry.func = io_sq_wake_function;
6763 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6764 complete(&ctx->sq_thread_comp);
6765 }
6766}
6767
Jens Axboe6c271ce2019-01-10 11:22:30 -07006768static int io_sq_thread(void *data)
6769{
Jens Axboe69fb2132020-09-14 11:16:23 -06006770 const struct cred *old_cred = NULL;
6771 struct io_sq_data *sqd = data;
6772 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006773 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006774
Jens Axboec8d1ba52020-09-14 11:07:26 -06006775 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006776 while (!kthread_should_stop()) {
6777 enum sq_ret ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006778
Jens Axboe69fb2132020-09-14 11:16:23 -06006779 /*
6780 * Any changes to the sqd lists are synchronized through the
6781 * kthread parking. This synchronizes the thread vs users,
6782 * the users are synchronized on the sqd->ctx_lock.
6783 */
6784 if (kthread_should_park())
6785 kthread_parkme();
6786
6787 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6788 io_sqd_init_new(sqd);
6789
6790 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6791 if (current->cred != ctx->creds) {
6792 if (old_cred)
6793 revert_creds(old_cred);
6794 old_cred = override_creds(ctx->creds);
6795 }
6796
6797 ret |= __io_sq_thread(ctx, start_jiffies);
6798
6799 io_sq_thread_drop_mm();
6800 }
6801
6802 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006803 io_run_task_work();
6804 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006805 } else if (ret == SQT_IDLE) {
6806 if (kthread_should_park())
6807 continue;
6808 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6809 io_ring_set_wakeup_flag(ctx);
6810 schedule();
6811 start_jiffies = jiffies;
6812 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6813 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006814 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006815 }
6816
Jens Axboe4c6e2772020-07-01 11:29:10 -06006817 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006818
Jens Axboe69fb2132020-09-14 11:16:23 -06006819 if (old_cred)
6820 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006821
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006822 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006823
Jens Axboe6c271ce2019-01-10 11:22:30 -07006824 return 0;
6825}
6826
Jens Axboebda52162019-09-24 13:47:15 -06006827struct io_wait_queue {
6828 struct wait_queue_entry wq;
6829 struct io_ring_ctx *ctx;
6830 unsigned to_wait;
6831 unsigned nr_timeouts;
6832};
6833
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006834static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006835{
6836 struct io_ring_ctx *ctx = iowq->ctx;
6837
6838 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006839 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006840 * started waiting. For timeouts, we always want to return to userspace,
6841 * regardless of event count.
6842 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006843 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006844 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6845}
6846
6847static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6848 int wake_flags, void *key)
6849{
6850 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6851 wq);
6852
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006853 /* use noflush == true, as we can't safely rely on locking context */
6854 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006855 return -1;
6856
6857 return autoremove_wake_function(curr, mode, wake_flags, key);
6858}
6859
Jens Axboe2b188cc2019-01-07 10:46:33 -07006860/*
6861 * Wait until events become available, if we don't already have some. The
6862 * application must reap them itself, as they reside on the shared cq ring.
6863 */
6864static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6865 const sigset_t __user *sig, size_t sigsz)
6866{
Jens Axboebda52162019-09-24 13:47:15 -06006867 struct io_wait_queue iowq = {
6868 .wq = {
6869 .private = current,
6870 .func = io_wake_function,
6871 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6872 },
6873 .ctx = ctx,
6874 .to_wait = min_events,
6875 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006876 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006877 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006878
Jens Axboeb41e9852020-02-17 09:52:41 -07006879 do {
6880 if (io_cqring_events(ctx, false) >= min_events)
6881 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006882 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006883 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006884 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006885
6886 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006887#ifdef CONFIG_COMPAT
6888 if (in_compat_syscall())
6889 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006890 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006891 else
6892#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006893 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006894
Jens Axboe2b188cc2019-01-07 10:46:33 -07006895 if (ret)
6896 return ret;
6897 }
6898
Jens Axboebda52162019-09-24 13:47:15 -06006899 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006900 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006901 do {
6902 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6903 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006904 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006905 if (io_run_task_work())
6906 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006907 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006908 if (current->jobctl & JOBCTL_TASK_WORK) {
6909 spin_lock_irq(&current->sighand->siglock);
6910 current->jobctl &= ~JOBCTL_TASK_WORK;
6911 recalc_sigpending();
6912 spin_unlock_irq(&current->sighand->siglock);
6913 continue;
6914 }
6915 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006916 break;
6917 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006918 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006919 break;
6920 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006921 } while (1);
6922 finish_wait(&ctx->wait, &iowq.wq);
6923
Jens Axboeb7db41c2020-07-04 08:55:50 -06006924 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006925
Hristo Venev75b28af2019-08-26 17:23:46 +00006926 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006927}
6928
Jens Axboe6b063142019-01-10 22:13:58 -07006929static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6930{
6931#if defined(CONFIG_UNIX)
6932 if (ctx->ring_sock) {
6933 struct sock *sock = ctx->ring_sock->sk;
6934 struct sk_buff *skb;
6935
6936 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6937 kfree_skb(skb);
6938 }
6939#else
6940 int i;
6941
Jens Axboe65e19f52019-10-26 07:20:21 -06006942 for (i = 0; i < ctx->nr_user_files; i++) {
6943 struct file *file;
6944
6945 file = io_file_from_index(ctx, i);
6946 if (file)
6947 fput(file);
6948 }
Jens Axboe6b063142019-01-10 22:13:58 -07006949#endif
6950}
6951
Jens Axboe05f3fb32019-12-09 11:22:50 -07006952static void io_file_ref_kill(struct percpu_ref *ref)
6953{
6954 struct fixed_file_data *data;
6955
6956 data = container_of(ref, struct fixed_file_data, refs);
6957 complete(&data->done);
6958}
6959
Jens Axboe6b063142019-01-10 22:13:58 -07006960static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6961{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006962 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006963 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006964 unsigned nr_tables, i;
6965
Jens Axboe05f3fb32019-12-09 11:22:50 -07006966 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006967 return -ENXIO;
6968
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006969 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006970 if (!list_empty(&data->ref_list))
6971 ref_node = list_first_entry(&data->ref_list,
6972 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006973 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006974 if (ref_node)
6975 percpu_ref_kill(&ref_node->refs);
6976
6977 percpu_ref_kill(&data->refs);
6978
6979 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006980 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006981 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006982
Jens Axboe6b063142019-01-10 22:13:58 -07006983 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006984 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6985 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006986 kfree(data->table[i].files);
6987 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006988 percpu_ref_exit(&data->refs);
6989 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006990 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006991 ctx->nr_user_files = 0;
6992 return 0;
6993}
6994
Jens Axboe534ca6d2020-09-02 13:52:19 -06006995static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006996{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006997 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006998 /*
6999 * The park is a bit of a work-around, without it we get
7000 * warning spews on shutdown with SQPOLL set and affinity
7001 * set to a single CPU.
7002 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007003 if (sqd->thread) {
7004 kthread_park(sqd->thread);
7005 kthread_stop(sqd->thread);
7006 }
7007
7008 kfree(sqd);
7009 }
7010}
7011
Jens Axboeaa061652020-09-02 14:50:27 -06007012static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7013{
7014 struct io_ring_ctx *ctx_attach;
7015 struct io_sq_data *sqd;
7016 struct fd f;
7017
7018 f = fdget(p->wq_fd);
7019 if (!f.file)
7020 return ERR_PTR(-ENXIO);
7021 if (f.file->f_op != &io_uring_fops) {
7022 fdput(f);
7023 return ERR_PTR(-EINVAL);
7024 }
7025
7026 ctx_attach = f.file->private_data;
7027 sqd = ctx_attach->sq_data;
7028 if (!sqd) {
7029 fdput(f);
7030 return ERR_PTR(-EINVAL);
7031 }
7032
7033 refcount_inc(&sqd->refs);
7034 fdput(f);
7035 return sqd;
7036}
7037
Jens Axboe534ca6d2020-09-02 13:52:19 -06007038static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7039{
7040 struct io_sq_data *sqd;
7041
Jens Axboeaa061652020-09-02 14:50:27 -06007042 if (p->flags & IORING_SETUP_ATTACH_WQ)
7043 return io_attach_sq_data(p);
7044
Jens Axboe534ca6d2020-09-02 13:52:19 -06007045 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7046 if (!sqd)
7047 return ERR_PTR(-ENOMEM);
7048
7049 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007050 INIT_LIST_HEAD(&sqd->ctx_list);
7051 INIT_LIST_HEAD(&sqd->ctx_new_list);
7052 mutex_init(&sqd->ctx_lock);
7053 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007054 init_waitqueue_head(&sqd->wait);
7055 return sqd;
7056}
7057
Jens Axboe69fb2132020-09-14 11:16:23 -06007058static void io_sq_thread_unpark(struct io_sq_data *sqd)
7059 __releases(&sqd->lock)
7060{
7061 if (!sqd->thread)
7062 return;
7063 kthread_unpark(sqd->thread);
7064 mutex_unlock(&sqd->lock);
7065}
7066
7067static void io_sq_thread_park(struct io_sq_data *sqd)
7068 __acquires(&sqd->lock)
7069{
7070 if (!sqd->thread)
7071 return;
7072 mutex_lock(&sqd->lock);
7073 kthread_park(sqd->thread);
7074}
7075
Jens Axboe534ca6d2020-09-02 13:52:19 -06007076static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7077{
7078 struct io_sq_data *sqd = ctx->sq_data;
7079
7080 if (sqd) {
7081 if (sqd->thread) {
7082 /*
7083 * We may arrive here from the error branch in
7084 * io_sq_offload_create() where the kthread is created
7085 * without being waked up, thus wake it up now to make
7086 * sure the wait will complete.
7087 */
7088 wake_up_process(sqd->thread);
7089 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007090
7091 io_sq_thread_park(sqd);
7092 }
7093
7094 mutex_lock(&sqd->ctx_lock);
7095 list_del(&ctx->sqd_list);
7096 mutex_unlock(&sqd->ctx_lock);
7097
7098 if (sqd->thread) {
7099 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7100 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007101 }
7102
7103 io_put_sq_data(sqd);
7104 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007105 }
7106}
7107
Jens Axboe6b063142019-01-10 22:13:58 -07007108static void io_finish_async(struct io_ring_ctx *ctx)
7109{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007110 io_sq_thread_stop(ctx);
7111
Jens Axboe561fb042019-10-24 07:25:42 -06007112 if (ctx->io_wq) {
7113 io_wq_destroy(ctx->io_wq);
7114 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007115 }
7116}
7117
7118#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007119/*
7120 * Ensure the UNIX gc is aware of our file set, so we are certain that
7121 * the io_uring can be safely unregistered on process exit, even if we have
7122 * loops in the file referencing.
7123 */
7124static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7125{
7126 struct sock *sk = ctx->ring_sock->sk;
7127 struct scm_fp_list *fpl;
7128 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007129 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007130
Jens Axboe6b063142019-01-10 22:13:58 -07007131 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7132 if (!fpl)
7133 return -ENOMEM;
7134
7135 skb = alloc_skb(0, GFP_KERNEL);
7136 if (!skb) {
7137 kfree(fpl);
7138 return -ENOMEM;
7139 }
7140
7141 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007142
Jens Axboe08a45172019-10-03 08:11:03 -06007143 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007144 fpl->user = get_uid(ctx->user);
7145 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007146 struct file *file = io_file_from_index(ctx, i + offset);
7147
7148 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007149 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007150 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007151 unix_inflight(fpl->user, fpl->fp[nr_files]);
7152 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007153 }
7154
Jens Axboe08a45172019-10-03 08:11:03 -06007155 if (nr_files) {
7156 fpl->max = SCM_MAX_FD;
7157 fpl->count = nr_files;
7158 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007159 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007160 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7161 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007162
Jens Axboe08a45172019-10-03 08:11:03 -06007163 for (i = 0; i < nr_files; i++)
7164 fput(fpl->fp[i]);
7165 } else {
7166 kfree_skb(skb);
7167 kfree(fpl);
7168 }
Jens Axboe6b063142019-01-10 22:13:58 -07007169
7170 return 0;
7171}
7172
7173/*
7174 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7175 * causes regular reference counting to break down. We rely on the UNIX
7176 * garbage collection to take care of this problem for us.
7177 */
7178static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7179{
7180 unsigned left, total;
7181 int ret = 0;
7182
7183 total = 0;
7184 left = ctx->nr_user_files;
7185 while (left) {
7186 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007187
7188 ret = __io_sqe_files_scm(ctx, this_files, total);
7189 if (ret)
7190 break;
7191 left -= this_files;
7192 total += this_files;
7193 }
7194
7195 if (!ret)
7196 return 0;
7197
7198 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007199 struct file *file = io_file_from_index(ctx, total);
7200
7201 if (file)
7202 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007203 total++;
7204 }
7205
7206 return ret;
7207}
7208#else
7209static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7210{
7211 return 0;
7212}
7213#endif
7214
Jens Axboe65e19f52019-10-26 07:20:21 -06007215static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7216 unsigned nr_files)
7217{
7218 int i;
7219
7220 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007221 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007222 unsigned this_files;
7223
7224 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7225 table->files = kcalloc(this_files, sizeof(struct file *),
7226 GFP_KERNEL);
7227 if (!table->files)
7228 break;
7229 nr_files -= this_files;
7230 }
7231
7232 if (i == nr_tables)
7233 return 0;
7234
7235 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007236 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007237 kfree(table->files);
7238 }
7239 return 1;
7240}
7241
Jens Axboe05f3fb32019-12-09 11:22:50 -07007242static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007243{
7244#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007245 struct sock *sock = ctx->ring_sock->sk;
7246 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7247 struct sk_buff *skb;
7248 int i;
7249
7250 __skb_queue_head_init(&list);
7251
7252 /*
7253 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7254 * remove this entry and rearrange the file array.
7255 */
7256 skb = skb_dequeue(head);
7257 while (skb) {
7258 struct scm_fp_list *fp;
7259
7260 fp = UNIXCB(skb).fp;
7261 for (i = 0; i < fp->count; i++) {
7262 int left;
7263
7264 if (fp->fp[i] != file)
7265 continue;
7266
7267 unix_notinflight(fp->user, fp->fp[i]);
7268 left = fp->count - 1 - i;
7269 if (left) {
7270 memmove(&fp->fp[i], &fp->fp[i + 1],
7271 left * sizeof(struct file *));
7272 }
7273 fp->count--;
7274 if (!fp->count) {
7275 kfree_skb(skb);
7276 skb = NULL;
7277 } else {
7278 __skb_queue_tail(&list, skb);
7279 }
7280 fput(file);
7281 file = NULL;
7282 break;
7283 }
7284
7285 if (!file)
7286 break;
7287
7288 __skb_queue_tail(&list, skb);
7289
7290 skb = skb_dequeue(head);
7291 }
7292
7293 if (skb_peek(&list)) {
7294 spin_lock_irq(&head->lock);
7295 while ((skb = __skb_dequeue(&list)) != NULL)
7296 __skb_queue_tail(head, skb);
7297 spin_unlock_irq(&head->lock);
7298 }
7299#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007300 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007301#endif
7302}
7303
Jens Axboe05f3fb32019-12-09 11:22:50 -07007304struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007305 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007306 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007307};
7308
Jens Axboe4a38aed22020-05-14 17:21:15 -06007309static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007310{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007311 struct fixed_file_data *file_data = ref_node->file_data;
7312 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007313 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007314
7315 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007316 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007317 io_ring_file_put(ctx, pfile->file);
7318 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007319 }
7320
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007321 spin_lock(&file_data->lock);
7322 list_del(&ref_node->node);
7323 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007324
Xiaoguang Wang05589552020-03-31 14:05:18 +08007325 percpu_ref_exit(&ref_node->refs);
7326 kfree(ref_node);
7327 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007328}
7329
Jens Axboe4a38aed22020-05-14 17:21:15 -06007330static void io_file_put_work(struct work_struct *work)
7331{
7332 struct io_ring_ctx *ctx;
7333 struct llist_node *node;
7334
7335 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7336 node = llist_del_all(&ctx->file_put_llist);
7337
7338 while (node) {
7339 struct fixed_file_ref_node *ref_node;
7340 struct llist_node *next = node->next;
7341
7342 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7343 __io_file_put_work(ref_node);
7344 node = next;
7345 }
7346}
7347
Jens Axboe05f3fb32019-12-09 11:22:50 -07007348static void io_file_data_ref_zero(struct percpu_ref *ref)
7349{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007350 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007351 struct io_ring_ctx *ctx;
7352 bool first_add;
7353 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007354
Xiaoguang Wang05589552020-03-31 14:05:18 +08007355 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007356 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007357
Jens Axboe4a38aed22020-05-14 17:21:15 -06007358 if (percpu_ref_is_dying(&ctx->file_data->refs))
7359 delay = 0;
7360
7361 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7362 if (!delay)
7363 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7364 else if (first_add)
7365 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007366}
7367
7368static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7369 struct io_ring_ctx *ctx)
7370{
7371 struct fixed_file_ref_node *ref_node;
7372
7373 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7374 if (!ref_node)
7375 return ERR_PTR(-ENOMEM);
7376
7377 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7378 0, GFP_KERNEL)) {
7379 kfree(ref_node);
7380 return ERR_PTR(-ENOMEM);
7381 }
7382 INIT_LIST_HEAD(&ref_node->node);
7383 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007384 ref_node->file_data = ctx->file_data;
7385 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007386}
7387
7388static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7389{
7390 percpu_ref_exit(&ref_node->refs);
7391 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007392}
7393
7394static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7395 unsigned nr_args)
7396{
7397 __s32 __user *fds = (__s32 __user *) arg;
7398 unsigned nr_tables;
7399 struct file *file;
7400 int fd, ret = 0;
7401 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007402 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007403
7404 if (ctx->file_data)
7405 return -EBUSY;
7406 if (!nr_args)
7407 return -EINVAL;
7408 if (nr_args > IORING_MAX_FIXED_FILES)
7409 return -EMFILE;
7410
7411 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7412 if (!ctx->file_data)
7413 return -ENOMEM;
7414 ctx->file_data->ctx = ctx;
7415 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007416 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007417 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007418
7419 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7420 ctx->file_data->table = kcalloc(nr_tables,
7421 sizeof(struct fixed_file_table),
7422 GFP_KERNEL);
7423 if (!ctx->file_data->table) {
7424 kfree(ctx->file_data);
7425 ctx->file_data = NULL;
7426 return -ENOMEM;
7427 }
7428
Xiaoguang Wang05589552020-03-31 14:05:18 +08007429 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007430 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7431 kfree(ctx->file_data->table);
7432 kfree(ctx->file_data);
7433 ctx->file_data = NULL;
7434 return -ENOMEM;
7435 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007436
7437 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7438 percpu_ref_exit(&ctx->file_data->refs);
7439 kfree(ctx->file_data->table);
7440 kfree(ctx->file_data);
7441 ctx->file_data = NULL;
7442 return -ENOMEM;
7443 }
7444
7445 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7446 struct fixed_file_table *table;
7447 unsigned index;
7448
7449 ret = -EFAULT;
7450 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7451 break;
7452 /* allow sparse sets */
7453 if (fd == -1) {
7454 ret = 0;
7455 continue;
7456 }
7457
7458 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7459 index = i & IORING_FILE_TABLE_MASK;
7460 file = fget(fd);
7461
7462 ret = -EBADF;
7463 if (!file)
7464 break;
7465
7466 /*
7467 * Don't allow io_uring instances to be registered. If UNIX
7468 * isn't enabled, then this causes a reference cycle and this
7469 * instance can never get freed. If UNIX is enabled we'll
7470 * handle it just fine, but there's still no point in allowing
7471 * a ring fd as it doesn't support regular read/write anyway.
7472 */
7473 if (file->f_op == &io_uring_fops) {
7474 fput(file);
7475 break;
7476 }
7477 ret = 0;
7478 table->files[index] = file;
7479 }
7480
7481 if (ret) {
7482 for (i = 0; i < ctx->nr_user_files; i++) {
7483 file = io_file_from_index(ctx, i);
7484 if (file)
7485 fput(file);
7486 }
7487 for (i = 0; i < nr_tables; i++)
7488 kfree(ctx->file_data->table[i].files);
7489
Yang Yingliang667e57d2020-07-10 14:14:20 +00007490 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007491 kfree(ctx->file_data->table);
7492 kfree(ctx->file_data);
7493 ctx->file_data = NULL;
7494 ctx->nr_user_files = 0;
7495 return ret;
7496 }
7497
7498 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007499 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007500 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007501 return ret;
7502 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007503
Xiaoguang Wang05589552020-03-31 14:05:18 +08007504 ref_node = alloc_fixed_file_ref_node(ctx);
7505 if (IS_ERR(ref_node)) {
7506 io_sqe_files_unregister(ctx);
7507 return PTR_ERR(ref_node);
7508 }
7509
7510 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007511 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007512 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007513 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007514 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007515 return ret;
7516}
7517
Jens Axboec3a31e62019-10-03 13:59:56 -06007518static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7519 int index)
7520{
7521#if defined(CONFIG_UNIX)
7522 struct sock *sock = ctx->ring_sock->sk;
7523 struct sk_buff_head *head = &sock->sk_receive_queue;
7524 struct sk_buff *skb;
7525
7526 /*
7527 * See if we can merge this file into an existing skb SCM_RIGHTS
7528 * file set. If there's no room, fall back to allocating a new skb
7529 * and filling it in.
7530 */
7531 spin_lock_irq(&head->lock);
7532 skb = skb_peek(head);
7533 if (skb) {
7534 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7535
7536 if (fpl->count < SCM_MAX_FD) {
7537 __skb_unlink(skb, head);
7538 spin_unlock_irq(&head->lock);
7539 fpl->fp[fpl->count] = get_file(file);
7540 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7541 fpl->count++;
7542 spin_lock_irq(&head->lock);
7543 __skb_queue_head(head, skb);
7544 } else {
7545 skb = NULL;
7546 }
7547 }
7548 spin_unlock_irq(&head->lock);
7549
7550 if (skb) {
7551 fput(file);
7552 return 0;
7553 }
7554
7555 return __io_sqe_files_scm(ctx, 1, index);
7556#else
7557 return 0;
7558#endif
7559}
7560
Hillf Dantona5318d32020-03-23 17:47:15 +08007561static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007562 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007563{
Hillf Dantona5318d32020-03-23 17:47:15 +08007564 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007565 struct percpu_ref *refs = data->cur_refs;
7566 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007567
Jens Axboe05f3fb32019-12-09 11:22:50 -07007568 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007569 if (!pfile)
7570 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007571
Xiaoguang Wang05589552020-03-31 14:05:18 +08007572 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007573 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007574 list_add(&pfile->list, &ref_node->file_list);
7575
Hillf Dantona5318d32020-03-23 17:47:15 +08007576 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007577}
7578
7579static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7580 struct io_uring_files_update *up,
7581 unsigned nr_args)
7582{
7583 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007584 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007585 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007586 __s32 __user *fds;
7587 int fd, i, err;
7588 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007589 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007590
Jens Axboe05f3fb32019-12-09 11:22:50 -07007591 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007592 return -EOVERFLOW;
7593 if (done > ctx->nr_user_files)
7594 return -EINVAL;
7595
Xiaoguang Wang05589552020-03-31 14:05:18 +08007596 ref_node = alloc_fixed_file_ref_node(ctx);
7597 if (IS_ERR(ref_node))
7598 return PTR_ERR(ref_node);
7599
Jens Axboec3a31e62019-10-03 13:59:56 -06007600 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007601 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007602 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007603 struct fixed_file_table *table;
7604 unsigned index;
7605
Jens Axboec3a31e62019-10-03 13:59:56 -06007606 err = 0;
7607 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7608 err = -EFAULT;
7609 break;
7610 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007611 i = array_index_nospec(up->offset, ctx->nr_user_files);
7612 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007613 index = i & IORING_FILE_TABLE_MASK;
7614 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007615 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007616 err = io_queue_file_removal(data, file);
7617 if (err)
7618 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007619 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007620 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007621 }
7622 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007623 file = fget(fd);
7624 if (!file) {
7625 err = -EBADF;
7626 break;
7627 }
7628 /*
7629 * Don't allow io_uring instances to be registered. If
7630 * UNIX isn't enabled, then this causes a reference
7631 * cycle and this instance can never get freed. If UNIX
7632 * is enabled we'll handle it just fine, but there's
7633 * still no point in allowing a ring fd as it doesn't
7634 * support regular read/write anyway.
7635 */
7636 if (file->f_op == &io_uring_fops) {
7637 fput(file);
7638 err = -EBADF;
7639 break;
7640 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007641 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007642 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007643 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007644 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007645 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007646 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007647 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007648 }
7649 nr_args--;
7650 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007651 up->offset++;
7652 }
7653
Xiaoguang Wang05589552020-03-31 14:05:18 +08007654 if (needs_switch) {
7655 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007656 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007657 list_add(&ref_node->node, &data->ref_list);
7658 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007659 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007660 percpu_ref_get(&ctx->file_data->refs);
7661 } else
7662 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007663
7664 return done ? done : err;
7665}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007666
Jens Axboe05f3fb32019-12-09 11:22:50 -07007667static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7668 unsigned nr_args)
7669{
7670 struct io_uring_files_update up;
7671
7672 if (!ctx->file_data)
7673 return -ENXIO;
7674 if (!nr_args)
7675 return -EINVAL;
7676 if (copy_from_user(&up, arg, sizeof(up)))
7677 return -EFAULT;
7678 if (up.resv)
7679 return -EINVAL;
7680
7681 return __io_sqe_files_update(ctx, &up, nr_args);
7682}
Jens Axboec3a31e62019-10-03 13:59:56 -06007683
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007684static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007685{
7686 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7687
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007688 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007689 io_put_req(req);
7690}
7691
Pavel Begunkov24369c22020-01-28 03:15:48 +03007692static int io_init_wq_offload(struct io_ring_ctx *ctx,
7693 struct io_uring_params *p)
7694{
7695 struct io_wq_data data;
7696 struct fd f;
7697 struct io_ring_ctx *ctx_attach;
7698 unsigned int concurrency;
7699 int ret = 0;
7700
7701 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007702 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007703 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007704
7705 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7706 /* Do QD, or 4 * CPUS, whatever is smallest */
7707 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7708
7709 ctx->io_wq = io_wq_create(concurrency, &data);
7710 if (IS_ERR(ctx->io_wq)) {
7711 ret = PTR_ERR(ctx->io_wq);
7712 ctx->io_wq = NULL;
7713 }
7714 return ret;
7715 }
7716
7717 f = fdget(p->wq_fd);
7718 if (!f.file)
7719 return -EBADF;
7720
7721 if (f.file->f_op != &io_uring_fops) {
7722 ret = -EINVAL;
7723 goto out_fput;
7724 }
7725
7726 ctx_attach = f.file->private_data;
7727 /* @io_wq is protected by holding the fd */
7728 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7729 ret = -EINVAL;
7730 goto out_fput;
7731 }
7732
7733 ctx->io_wq = ctx_attach->io_wq;
7734out_fput:
7735 fdput(f);
7736 return ret;
7737}
7738
Jens Axboe0f212202020-09-13 13:09:39 -06007739static int io_uring_alloc_task_context(struct task_struct *task)
7740{
7741 struct io_uring_task *tctx;
7742
7743 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7744 if (unlikely(!tctx))
7745 return -ENOMEM;
7746
7747 xa_init(&tctx->xa);
7748 init_waitqueue_head(&tctx->wait);
7749 tctx->last = NULL;
7750 tctx->in_idle = 0;
7751 atomic_long_set(&tctx->req_issue, 0);
7752 atomic_long_set(&tctx->req_complete, 0);
7753 task->io_uring = tctx;
7754 return 0;
7755}
7756
7757void __io_uring_free(struct task_struct *tsk)
7758{
7759 struct io_uring_task *tctx = tsk->io_uring;
7760
7761 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7762 xa_destroy(&tctx->xa);
7763 kfree(tctx);
7764 tsk->io_uring = NULL;
7765}
7766
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007767static int io_sq_offload_create(struct io_ring_ctx *ctx,
7768 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007769{
7770 int ret;
7771
Jens Axboe6c271ce2019-01-10 11:22:30 -07007772 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007773 struct io_sq_data *sqd;
7774
Jens Axboe3ec482d2019-04-08 10:51:01 -06007775 ret = -EPERM;
7776 if (!capable(CAP_SYS_ADMIN))
7777 goto err;
7778
Jens Axboe534ca6d2020-09-02 13:52:19 -06007779 sqd = io_get_sq_data(p);
7780 if (IS_ERR(sqd)) {
7781 ret = PTR_ERR(sqd);
7782 goto err;
7783 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007784
Jens Axboe534ca6d2020-09-02 13:52:19 -06007785 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007786 io_sq_thread_park(sqd);
7787 mutex_lock(&sqd->ctx_lock);
7788 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7789 mutex_unlock(&sqd->ctx_lock);
7790 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007791
Jens Axboe917257d2019-04-13 09:28:55 -06007792 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7793 if (!ctx->sq_thread_idle)
7794 ctx->sq_thread_idle = HZ;
7795
Jens Axboeaa061652020-09-02 14:50:27 -06007796 if (sqd->thread)
7797 goto done;
7798
Jens Axboe6c271ce2019-01-10 11:22:30 -07007799 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007800 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007801
Jens Axboe917257d2019-04-13 09:28:55 -06007802 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007803 if (cpu >= nr_cpu_ids)
7804 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007805 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007806 goto err;
7807
Jens Axboe69fb2132020-09-14 11:16:23 -06007808 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007809 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007810 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007811 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007812 "io_uring-sq");
7813 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007814 if (IS_ERR(sqd->thread)) {
7815 ret = PTR_ERR(sqd->thread);
7816 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007817 goto err;
7818 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007819 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007820 if (ret)
7821 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007822 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7823 /* Can't have SQ_AFF without SQPOLL */
7824 ret = -EINVAL;
7825 goto err;
7826 }
7827
Jens Axboeaa061652020-09-02 14:50:27 -06007828done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007829 ret = io_init_wq_offload(ctx, p);
7830 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007831 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007832
7833 return 0;
7834err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007835 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007836 return ret;
7837}
7838
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007839static void io_sq_offload_start(struct io_ring_ctx *ctx)
7840{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007841 struct io_sq_data *sqd = ctx->sq_data;
7842
7843 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7844 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007845}
7846
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007847static inline void __io_unaccount_mem(struct user_struct *user,
7848 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007849{
7850 atomic_long_sub(nr_pages, &user->locked_vm);
7851}
7852
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007853static inline int __io_account_mem(struct user_struct *user,
7854 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007855{
7856 unsigned long page_limit, cur_pages, new_pages;
7857
7858 /* Don't allow more pages than we can safely lock */
7859 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7860
7861 do {
7862 cur_pages = atomic_long_read(&user->locked_vm);
7863 new_pages = cur_pages + nr_pages;
7864 if (new_pages > page_limit)
7865 return -ENOMEM;
7866 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7867 new_pages) != cur_pages);
7868
7869 return 0;
7870}
7871
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007872static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7873 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007874{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007875 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007876 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007877
Jens Axboe2aede0e2020-09-14 10:45:53 -06007878 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007879 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007880 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007881 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007882 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007883 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007884}
7885
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007886static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7887 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007888{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007889 int ret;
7890
7891 if (ctx->limit_mem) {
7892 ret = __io_account_mem(ctx->user, nr_pages);
7893 if (ret)
7894 return ret;
7895 }
7896
Jens Axboe2aede0e2020-09-14 10:45:53 -06007897 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007898 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007899 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007900 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007901 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007902 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007903
7904 return 0;
7905}
7906
Jens Axboe2b188cc2019-01-07 10:46:33 -07007907static void io_mem_free(void *ptr)
7908{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007909 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007910
Mark Rutland52e04ef2019-04-30 17:30:21 +01007911 if (!ptr)
7912 return;
7913
7914 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007915 if (put_page_testzero(page))
7916 free_compound_page(page);
7917}
7918
7919static void *io_mem_alloc(size_t size)
7920{
7921 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7922 __GFP_NORETRY;
7923
7924 return (void *) __get_free_pages(gfp_flags, get_order(size));
7925}
7926
Hristo Venev75b28af2019-08-26 17:23:46 +00007927static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7928 size_t *sq_offset)
7929{
7930 struct io_rings *rings;
7931 size_t off, sq_array_size;
7932
7933 off = struct_size(rings, cqes, cq_entries);
7934 if (off == SIZE_MAX)
7935 return SIZE_MAX;
7936
7937#ifdef CONFIG_SMP
7938 off = ALIGN(off, SMP_CACHE_BYTES);
7939 if (off == 0)
7940 return SIZE_MAX;
7941#endif
7942
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007943 if (sq_offset)
7944 *sq_offset = off;
7945
Hristo Venev75b28af2019-08-26 17:23:46 +00007946 sq_array_size = array_size(sizeof(u32), sq_entries);
7947 if (sq_array_size == SIZE_MAX)
7948 return SIZE_MAX;
7949
7950 if (check_add_overflow(off, sq_array_size, &off))
7951 return SIZE_MAX;
7952
Hristo Venev75b28af2019-08-26 17:23:46 +00007953 return off;
7954}
7955
Jens Axboe2b188cc2019-01-07 10:46:33 -07007956static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7957{
Hristo Venev75b28af2019-08-26 17:23:46 +00007958 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007959
Hristo Venev75b28af2019-08-26 17:23:46 +00007960 pages = (size_t)1 << get_order(
7961 rings_size(sq_entries, cq_entries, NULL));
7962 pages += (size_t)1 << get_order(
7963 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007964
Hristo Venev75b28af2019-08-26 17:23:46 +00007965 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007966}
7967
Jens Axboeedafcce2019-01-09 09:16:05 -07007968static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7969{
7970 int i, j;
7971
7972 if (!ctx->user_bufs)
7973 return -ENXIO;
7974
7975 for (i = 0; i < ctx->nr_user_bufs; i++) {
7976 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7977
7978 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007979 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007980
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007981 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007982 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007983 imu->nr_bvecs = 0;
7984 }
7985
7986 kfree(ctx->user_bufs);
7987 ctx->user_bufs = NULL;
7988 ctx->nr_user_bufs = 0;
7989 return 0;
7990}
7991
7992static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7993 void __user *arg, unsigned index)
7994{
7995 struct iovec __user *src;
7996
7997#ifdef CONFIG_COMPAT
7998 if (ctx->compat) {
7999 struct compat_iovec __user *ciovs;
8000 struct compat_iovec ciov;
8001
8002 ciovs = (struct compat_iovec __user *) arg;
8003 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8004 return -EFAULT;
8005
Jens Axboed55e5f52019-12-11 16:12:15 -07008006 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008007 dst->iov_len = ciov.iov_len;
8008 return 0;
8009 }
8010#endif
8011 src = (struct iovec __user *) arg;
8012 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8013 return -EFAULT;
8014 return 0;
8015}
8016
8017static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8018 unsigned nr_args)
8019{
8020 struct vm_area_struct **vmas = NULL;
8021 struct page **pages = NULL;
8022 int i, j, got_pages = 0;
8023 int ret = -EINVAL;
8024
8025 if (ctx->user_bufs)
8026 return -EBUSY;
8027 if (!nr_args || nr_args > UIO_MAXIOV)
8028 return -EINVAL;
8029
8030 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8031 GFP_KERNEL);
8032 if (!ctx->user_bufs)
8033 return -ENOMEM;
8034
8035 for (i = 0; i < nr_args; i++) {
8036 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8037 unsigned long off, start, end, ubuf;
8038 int pret, nr_pages;
8039 struct iovec iov;
8040 size_t size;
8041
8042 ret = io_copy_iov(ctx, &iov, arg, i);
8043 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008044 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008045
8046 /*
8047 * Don't impose further limits on the size and buffer
8048 * constraints here, we'll -EINVAL later when IO is
8049 * submitted if they are wrong.
8050 */
8051 ret = -EFAULT;
8052 if (!iov.iov_base || !iov.iov_len)
8053 goto err;
8054
8055 /* arbitrary limit, but we need something */
8056 if (iov.iov_len > SZ_1G)
8057 goto err;
8058
8059 ubuf = (unsigned long) iov.iov_base;
8060 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8061 start = ubuf >> PAGE_SHIFT;
8062 nr_pages = end - start;
8063
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008064 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008065 if (ret)
8066 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008067
8068 ret = 0;
8069 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008070 kvfree(vmas);
8071 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008072 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008073 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008074 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008075 sizeof(struct vm_area_struct *),
8076 GFP_KERNEL);
8077 if (!pages || !vmas) {
8078 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008079 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008080 goto err;
8081 }
8082 got_pages = nr_pages;
8083 }
8084
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008085 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008086 GFP_KERNEL);
8087 ret = -ENOMEM;
8088 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008089 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008090 goto err;
8091 }
8092
8093 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008094 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008095 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008096 FOLL_WRITE | FOLL_LONGTERM,
8097 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008098 if (pret == nr_pages) {
8099 /* don't support file backed memory */
8100 for (j = 0; j < nr_pages; j++) {
8101 struct vm_area_struct *vma = vmas[j];
8102
8103 if (vma->vm_file &&
8104 !is_file_hugepages(vma->vm_file)) {
8105 ret = -EOPNOTSUPP;
8106 break;
8107 }
8108 }
8109 } else {
8110 ret = pret < 0 ? pret : -EFAULT;
8111 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008112 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008113 if (ret) {
8114 /*
8115 * if we did partial map, or found file backed vmas,
8116 * release any pages we did get
8117 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008118 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008119 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008120 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008121 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008122 goto err;
8123 }
8124
8125 off = ubuf & ~PAGE_MASK;
8126 size = iov.iov_len;
8127 for (j = 0; j < nr_pages; j++) {
8128 size_t vec_len;
8129
8130 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8131 imu->bvec[j].bv_page = pages[j];
8132 imu->bvec[j].bv_len = vec_len;
8133 imu->bvec[j].bv_offset = off;
8134 off = 0;
8135 size -= vec_len;
8136 }
8137 /* store original address for later verification */
8138 imu->ubuf = ubuf;
8139 imu->len = iov.iov_len;
8140 imu->nr_bvecs = nr_pages;
8141
8142 ctx->nr_user_bufs++;
8143 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008144 kvfree(pages);
8145 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008146 return 0;
8147err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008148 kvfree(pages);
8149 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008150 io_sqe_buffer_unregister(ctx);
8151 return ret;
8152}
8153
Jens Axboe9b402842019-04-11 11:45:41 -06008154static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8155{
8156 __s32 __user *fds = arg;
8157 int fd;
8158
8159 if (ctx->cq_ev_fd)
8160 return -EBUSY;
8161
8162 if (copy_from_user(&fd, fds, sizeof(*fds)))
8163 return -EFAULT;
8164
8165 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8166 if (IS_ERR(ctx->cq_ev_fd)) {
8167 int ret = PTR_ERR(ctx->cq_ev_fd);
8168 ctx->cq_ev_fd = NULL;
8169 return ret;
8170 }
8171
8172 return 0;
8173}
8174
8175static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8176{
8177 if (ctx->cq_ev_fd) {
8178 eventfd_ctx_put(ctx->cq_ev_fd);
8179 ctx->cq_ev_fd = NULL;
8180 return 0;
8181 }
8182
8183 return -ENXIO;
8184}
8185
Jens Axboe5a2e7452020-02-23 16:23:11 -07008186static int __io_destroy_buffers(int id, void *p, void *data)
8187{
8188 struct io_ring_ctx *ctx = data;
8189 struct io_buffer *buf = p;
8190
Jens Axboe067524e2020-03-02 16:32:28 -07008191 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008192 return 0;
8193}
8194
8195static void io_destroy_buffers(struct io_ring_ctx *ctx)
8196{
8197 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8198 idr_destroy(&ctx->io_buffer_idr);
8199}
8200
Jens Axboe2b188cc2019-01-07 10:46:33 -07008201static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8202{
Jens Axboe6b063142019-01-10 22:13:58 -07008203 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008204 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008205
8206 if (ctx->sqo_task) {
8207 put_task_struct(ctx->sqo_task);
8208 ctx->sqo_task = NULL;
8209 mmdrop(ctx->mm_account);
8210 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008211 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008212
Jens Axboe6b063142019-01-10 22:13:58 -07008213 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008214 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008215 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008216 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008217
Jens Axboe2b188cc2019-01-07 10:46:33 -07008218#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008219 if (ctx->ring_sock) {
8220 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008221 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008222 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008223#endif
8224
Hristo Venev75b28af2019-08-26 17:23:46 +00008225 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008226 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008227
8228 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008229 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008230 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008231 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008232 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008233 kfree(ctx);
8234}
8235
8236static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8237{
8238 struct io_ring_ctx *ctx = file->private_data;
8239 __poll_t mask = 0;
8240
8241 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008242 /*
8243 * synchronizes with barrier from wq_has_sleeper call in
8244 * io_commit_cqring
8245 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008246 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008247 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008248 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008249 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008250 mask |= EPOLLIN | EPOLLRDNORM;
8251
8252 return mask;
8253}
8254
8255static int io_uring_fasync(int fd, struct file *file, int on)
8256{
8257 struct io_ring_ctx *ctx = file->private_data;
8258
8259 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8260}
8261
Jens Axboe071698e2020-01-28 10:04:42 -07008262static int io_remove_personalities(int id, void *p, void *data)
8263{
8264 struct io_ring_ctx *ctx = data;
8265 const struct cred *cred;
8266
8267 cred = idr_remove(&ctx->personality_idr, id);
8268 if (cred)
8269 put_cred(cred);
8270 return 0;
8271}
8272
Jens Axboe85faa7b2020-04-09 18:14:00 -06008273static void io_ring_exit_work(struct work_struct *work)
8274{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008275 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8276 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008277
Jens Axboe56952e92020-06-17 15:00:04 -06008278 /*
8279 * If we're doing polled IO and end up having requests being
8280 * submitted async (out-of-line), then completions can come in while
8281 * we're waiting for refs to drop. We need to reap these manually,
8282 * as nobody else will be looking for them.
8283 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008284 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008285 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008286 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008287 io_iopoll_try_reap_events(ctx);
8288 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008289 io_ring_ctx_free(ctx);
8290}
8291
Jens Axboe2b188cc2019-01-07 10:46:33 -07008292static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8293{
8294 mutex_lock(&ctx->uring_lock);
8295 percpu_ref_kill(&ctx->refs);
8296 mutex_unlock(&ctx->uring_lock);
8297
Jens Axboef3606e32020-09-22 08:18:24 -06008298 io_kill_timeouts(ctx, NULL);
8299 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008300
8301 if (ctx->io_wq)
8302 io_wq_cancel_all(ctx->io_wq);
8303
Jens Axboe15dff282019-11-13 09:09:23 -07008304 /* if we failed setting up the ctx, we might not have any rings */
8305 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008306 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008307 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008308 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008309
8310 /*
8311 * Do this upfront, so we won't have a grace period where the ring
8312 * is closed but resources aren't reaped yet. This can cause
8313 * spurious failure in setting up a new ring.
8314 */
Jens Axboe760618f2020-07-24 12:53:31 -06008315 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8316 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008317
Jens Axboe85faa7b2020-04-09 18:14:00 -06008318 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008319 /*
8320 * Use system_unbound_wq to avoid spawning tons of event kworkers
8321 * if we're exiting a ton of rings at the same time. It just adds
8322 * noise and overhead, there's no discernable change in runtime
8323 * over using system_wq.
8324 */
8325 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008326}
8327
8328static int io_uring_release(struct inode *inode, struct file *file)
8329{
8330 struct io_ring_ctx *ctx = file->private_data;
8331
8332 file->private_data = NULL;
8333 io_ring_ctx_wait_and_kill(ctx);
8334 return 0;
8335}
8336
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008337static bool io_wq_files_match(struct io_wq_work *work, void *data)
8338{
8339 struct files_struct *files = data;
8340
Jens Axboe0f212202020-09-13 13:09:39 -06008341 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008342}
8343
Jens Axboef254ac02020-08-12 17:33:30 -06008344/*
8345 * Returns true if 'preq' is the link parent of 'req'
8346 */
8347static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8348{
8349 struct io_kiocb *link;
8350
8351 if (!(preq->flags & REQ_F_LINK_HEAD))
8352 return false;
8353
8354 list_for_each_entry(link, &preq->link_list, link_list) {
8355 if (link == req)
8356 return true;
8357 }
8358
8359 return false;
8360}
8361
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008362static bool io_match_link_files(struct io_kiocb *req,
8363 struct files_struct *files)
8364{
8365 struct io_kiocb *link;
8366
8367 if (io_match_files(req, files))
8368 return true;
8369 if (req->flags & REQ_F_LINK_HEAD) {
8370 list_for_each_entry(link, &req->link_list, link_list) {
8371 if (io_match_files(link, files))
8372 return true;
8373 }
8374 }
8375 return false;
8376}
8377
Jens Axboef254ac02020-08-12 17:33:30 -06008378/*
8379 * We're looking to cancel 'req' because it's holding on to our files, but
8380 * 'req' could be a link to another request. See if it is, and cancel that
8381 * parent request if so.
8382 */
8383static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8384{
8385 struct hlist_node *tmp;
8386 struct io_kiocb *preq;
8387 bool found = false;
8388 int i;
8389
8390 spin_lock_irq(&ctx->completion_lock);
8391 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8392 struct hlist_head *list;
8393
8394 list = &ctx->cancel_hash[i];
8395 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8396 found = io_match_link(preq, req);
8397 if (found) {
8398 io_poll_remove_one(preq);
8399 break;
8400 }
8401 }
8402 }
8403 spin_unlock_irq(&ctx->completion_lock);
8404 return found;
8405}
8406
8407static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8408 struct io_kiocb *req)
8409{
8410 struct io_kiocb *preq;
8411 bool found = false;
8412
8413 spin_lock_irq(&ctx->completion_lock);
8414 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8415 found = io_match_link(preq, req);
8416 if (found) {
8417 __io_timeout_cancel(preq);
8418 break;
8419 }
8420 }
8421 spin_unlock_irq(&ctx->completion_lock);
8422 return found;
8423}
8424
Jens Axboeb711d4e2020-08-16 08:23:05 -07008425static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8426{
8427 return io_match_link(container_of(work, struct io_kiocb, work), data);
8428}
8429
8430static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8431{
8432 enum io_wq_cancel cret;
8433
8434 /* cancel this particular work, if it's running */
8435 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8436 if (cret != IO_WQ_CANCEL_NOTFOUND)
8437 return;
8438
8439 /* find links that hold this pending, cancel those */
8440 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8441 if (cret != IO_WQ_CANCEL_NOTFOUND)
8442 return;
8443
8444 /* if we have a poll link holding this pending, cancel that */
8445 if (io_poll_remove_link(ctx, req))
8446 return;
8447
8448 /* final option, timeout link is holding this req pending */
8449 io_timeout_remove_link(ctx, req);
8450}
8451
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008452static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8453 struct files_struct *files)
8454{
8455 struct io_defer_entry *de = NULL;
8456 LIST_HEAD(list);
8457
8458 spin_lock_irq(&ctx->completion_lock);
8459 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008460 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008461 list_cut_position(&list, &ctx->defer_list, &de->list);
8462 break;
8463 }
8464 }
8465 spin_unlock_irq(&ctx->completion_lock);
8466
8467 while (!list_empty(&list)) {
8468 de = list_first_entry(&list, struct io_defer_entry, list);
8469 list_del_init(&de->list);
8470 req_set_fail_links(de->req);
8471 io_put_req(de->req);
8472 io_req_complete(de->req, -ECANCELED);
8473 kfree(de);
8474 }
8475}
8476
Jens Axboe76e1b642020-09-26 15:05:03 -06008477/*
8478 * Returns true if we found and killed one or more files pinning requests
8479 */
8480static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008481 struct files_struct *files)
8482{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008483 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008484 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008485
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008486 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008487 /* cancel all at once, should be faster than doing it one by one*/
8488 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8489
Jens Axboefcb323c2019-10-24 12:39:47 -06008490 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008491 struct io_kiocb *cancel_req = NULL, *req;
8492 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008493
8494 spin_lock_irq(&ctx->inflight_lock);
8495 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008496 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008497 continue;
8498 /* req is being completed, ignore */
8499 if (!refcount_inc_not_zero(&req->refs))
8500 continue;
8501 cancel_req = req;
8502 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008503 }
Jens Axboe768134d2019-11-10 20:30:53 -07008504 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008505 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008506 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008507 spin_unlock_irq(&ctx->inflight_lock);
8508
Jens Axboe768134d2019-11-10 20:30:53 -07008509 /* We need to keep going until we don't find a matching req */
8510 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008511 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008512 /* cancel this request, or head link requests */
8513 io_attempt_cancel(ctx, cancel_req);
8514 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008515 /* cancellations _may_ trigger task work */
8516 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008517 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008518 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008519 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008520
8521 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008522}
8523
Pavel Begunkov801dd572020-06-15 10:33:14 +03008524static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008525{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008526 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8527 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008528
Jens Axboef3606e32020-09-22 08:18:24 -06008529 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008530}
8531
Jens Axboe0f212202020-09-13 13:09:39 -06008532static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8533 struct task_struct *task,
8534 struct files_struct *files)
8535{
8536 bool ret;
8537
8538 ret = io_uring_cancel_files(ctx, files);
8539 if (!files) {
8540 enum io_wq_cancel cret;
8541
8542 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8543 if (cret != IO_WQ_CANCEL_NOTFOUND)
8544 ret = true;
8545
8546 /* SQPOLL thread does its own polling */
8547 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8548 while (!list_empty_careful(&ctx->iopoll_list)) {
8549 io_iopoll_try_reap_events(ctx);
8550 ret = true;
8551 }
8552 }
8553
8554 ret |= io_poll_remove_all(ctx, task);
8555 ret |= io_kill_timeouts(ctx, task);
8556 }
8557
8558 return ret;
8559}
8560
8561/*
8562 * We need to iteratively cancel requests, in case a request has dependent
8563 * hard links. These persist even for failure of cancelations, hence keep
8564 * looping until none are found.
8565 */
8566static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8567 struct files_struct *files)
8568{
8569 struct task_struct *task = current;
8570
Jens Axboe534ca6d2020-09-02 13:52:19 -06008571 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8572 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008573
8574 io_cqring_overflow_flush(ctx, true, task, files);
8575
8576 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8577 io_run_task_work();
8578 cond_resched();
8579 }
8580}
8581
8582/*
8583 * Note that this task has used io_uring. We use it for cancelation purposes.
8584 */
8585static int io_uring_add_task_file(struct file *file)
8586{
8587 if (unlikely(!current->io_uring)) {
8588 int ret;
8589
8590 ret = io_uring_alloc_task_context(current);
8591 if (unlikely(ret))
8592 return ret;
8593 }
8594 if (current->io_uring->last != file) {
8595 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8596 void *old;
8597
8598 rcu_read_lock();
8599 old = xas_load(&xas);
8600 if (old != file) {
8601 get_file(file);
8602 xas_lock(&xas);
8603 xas_store(&xas, file);
8604 xas_unlock(&xas);
8605 }
8606 rcu_read_unlock();
8607 current->io_uring->last = file;
8608 }
8609
8610 return 0;
8611}
8612
8613/*
8614 * Remove this io_uring_file -> task mapping.
8615 */
8616static void io_uring_del_task_file(struct file *file)
8617{
8618 struct io_uring_task *tctx = current->io_uring;
8619 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8620
8621 if (tctx->last == file)
8622 tctx->last = NULL;
8623
8624 xas_lock(&xas);
8625 file = xas_store(&xas, NULL);
8626 xas_unlock(&xas);
8627
8628 if (file)
8629 fput(file);
8630}
8631
8632static void __io_uring_attempt_task_drop(struct file *file)
8633{
8634 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8635 struct file *old;
8636
8637 rcu_read_lock();
8638 old = xas_load(&xas);
8639 rcu_read_unlock();
8640
8641 if (old == file)
8642 io_uring_del_task_file(file);
8643}
8644
8645/*
8646 * Drop task note for this file if we're the only ones that hold it after
8647 * pending fput()
8648 */
8649static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8650{
8651 if (!current->io_uring)
8652 return;
8653 /*
8654 * fput() is pending, will be 2 if the only other ref is our potential
8655 * task file note. If the task is exiting, drop regardless of count.
8656 */
8657 if (!exiting && atomic_long_read(&file->f_count) != 2)
8658 return;
8659
8660 __io_uring_attempt_task_drop(file);
8661}
8662
8663void __io_uring_files_cancel(struct files_struct *files)
8664{
8665 struct io_uring_task *tctx = current->io_uring;
8666 XA_STATE(xas, &tctx->xa, 0);
8667
8668 /* make sure overflow events are dropped */
8669 tctx->in_idle = true;
8670
8671 do {
8672 struct io_ring_ctx *ctx;
8673 struct file *file;
8674
8675 xas_lock(&xas);
8676 file = xas_next_entry(&xas, ULONG_MAX);
8677 xas_unlock(&xas);
8678
8679 if (!file)
8680 break;
8681
8682 ctx = file->private_data;
8683
8684 io_uring_cancel_task_requests(ctx, files);
8685 if (files)
8686 io_uring_del_task_file(file);
8687 } while (1);
8688}
8689
8690static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8691{
8692 return atomic_long_read(&tctx->req_issue) ==
8693 atomic_long_read(&tctx->req_complete);
8694}
8695
8696/*
8697 * Find any io_uring fd that this task has registered or done IO on, and cancel
8698 * requests.
8699 */
8700void __io_uring_task_cancel(void)
8701{
8702 struct io_uring_task *tctx = current->io_uring;
8703 DEFINE_WAIT(wait);
8704 long completions;
8705
8706 /* make sure overflow events are dropped */
8707 tctx->in_idle = true;
8708
8709 while (!io_uring_task_idle(tctx)) {
8710 /* read completions before cancelations */
8711 completions = atomic_long_read(&tctx->req_complete);
8712 __io_uring_files_cancel(NULL);
8713
8714 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8715
8716 /*
8717 * If we've seen completions, retry. This avoids a race where
8718 * a completion comes in before we did prepare_to_wait().
8719 */
8720 if (completions != atomic_long_read(&tctx->req_complete))
8721 continue;
8722 if (io_uring_task_idle(tctx))
8723 break;
8724 schedule();
8725 }
8726
8727 finish_wait(&tctx->wait, &wait);
8728 tctx->in_idle = false;
8729}
8730
Jens Axboefcb323c2019-10-24 12:39:47 -06008731static int io_uring_flush(struct file *file, void *data)
8732{
8733 struct io_ring_ctx *ctx = file->private_data;
8734
Jens Axboe6ab23142020-02-08 20:23:59 -07008735 /*
8736 * If the task is going away, cancel work it may have pending
8737 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008738 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008739 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008740
Jens Axboe0f212202020-09-13 13:09:39 -06008741 io_uring_cancel_task_requests(ctx, data);
8742 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008743 return 0;
8744}
8745
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008746static void *io_uring_validate_mmap_request(struct file *file,
8747 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008748{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008749 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008750 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008751 struct page *page;
8752 void *ptr;
8753
8754 switch (offset) {
8755 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008756 case IORING_OFF_CQ_RING:
8757 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008758 break;
8759 case IORING_OFF_SQES:
8760 ptr = ctx->sq_sqes;
8761 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008762 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008763 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008764 }
8765
8766 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008767 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008768 return ERR_PTR(-EINVAL);
8769
8770 return ptr;
8771}
8772
8773#ifdef CONFIG_MMU
8774
8775static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8776{
8777 size_t sz = vma->vm_end - vma->vm_start;
8778 unsigned long pfn;
8779 void *ptr;
8780
8781 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8782 if (IS_ERR(ptr))
8783 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008784
8785 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8786 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8787}
8788
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008789#else /* !CONFIG_MMU */
8790
8791static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8792{
8793 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8794}
8795
8796static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8797{
8798 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8799}
8800
8801static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8802 unsigned long addr, unsigned long len,
8803 unsigned long pgoff, unsigned long flags)
8804{
8805 void *ptr;
8806
8807 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8808 if (IS_ERR(ptr))
8809 return PTR_ERR(ptr);
8810
8811 return (unsigned long) ptr;
8812}
8813
8814#endif /* !CONFIG_MMU */
8815
Jens Axboe90554202020-09-03 12:12:41 -06008816static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8817{
8818 DEFINE_WAIT(wait);
8819
8820 do {
8821 if (!io_sqring_full(ctx))
8822 break;
8823
8824 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8825
8826 if (!io_sqring_full(ctx))
8827 break;
8828
8829 schedule();
8830 } while (!signal_pending(current));
8831
8832 finish_wait(&ctx->sqo_sq_wait, &wait);
8833}
8834
Jens Axboe2b188cc2019-01-07 10:46:33 -07008835SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8836 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8837 size_t, sigsz)
8838{
8839 struct io_ring_ctx *ctx;
8840 long ret = -EBADF;
8841 int submitted = 0;
8842 struct fd f;
8843
Jens Axboe4c6e2772020-07-01 11:29:10 -06008844 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008845
Jens Axboe90554202020-09-03 12:12:41 -06008846 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8847 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008848 return -EINVAL;
8849
8850 f = fdget(fd);
8851 if (!f.file)
8852 return -EBADF;
8853
8854 ret = -EOPNOTSUPP;
8855 if (f.file->f_op != &io_uring_fops)
8856 goto out_fput;
8857
8858 ret = -ENXIO;
8859 ctx = f.file->private_data;
8860 if (!percpu_ref_tryget(&ctx->refs))
8861 goto out_fput;
8862
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008863 ret = -EBADFD;
8864 if (ctx->flags & IORING_SETUP_R_DISABLED)
8865 goto out;
8866
Jens Axboe6c271ce2019-01-10 11:22:30 -07008867 /*
8868 * For SQ polling, the thread will do all submissions and completions.
8869 * Just return the requested submit count, and wake the thread if
8870 * we were asked to.
8871 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008872 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008873 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008874 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008875 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008876 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008877 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008878 if (flags & IORING_ENTER_SQ_WAIT)
8879 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008880 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008881 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008882 ret = io_uring_add_task_file(f.file);
8883 if (unlikely(ret))
8884 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008885 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008886 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008887 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008888
8889 if (submitted != to_submit)
8890 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008891 }
8892 if (flags & IORING_ENTER_GETEVENTS) {
8893 min_complete = min(min_complete, ctx->cq_entries);
8894
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008895 /*
8896 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8897 * space applications don't need to do io completion events
8898 * polling again, they can rely on io_sq_thread to do polling
8899 * work, which can reduce cpu usage and uring_lock contention.
8900 */
8901 if (ctx->flags & IORING_SETUP_IOPOLL &&
8902 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008903 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008904 } else {
8905 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8906 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008907 }
8908
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008909out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008910 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008911out_fput:
8912 fdput(f);
8913 return submitted ? submitted : ret;
8914}
8915
Tobias Klauserbebdb652020-02-26 18:38:32 +01008916#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008917static int io_uring_show_cred(int id, void *p, void *data)
8918{
8919 const struct cred *cred = p;
8920 struct seq_file *m = data;
8921 struct user_namespace *uns = seq_user_ns(m);
8922 struct group_info *gi;
8923 kernel_cap_t cap;
8924 unsigned __capi;
8925 int g;
8926
8927 seq_printf(m, "%5d\n", id);
8928 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8929 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8930 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8931 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8932 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8933 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8934 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8935 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8936 seq_puts(m, "\n\tGroups:\t");
8937 gi = cred->group_info;
8938 for (g = 0; g < gi->ngroups; g++) {
8939 seq_put_decimal_ull(m, g ? " " : "",
8940 from_kgid_munged(uns, gi->gid[g]));
8941 }
8942 seq_puts(m, "\n\tCapEff:\t");
8943 cap = cred->cap_effective;
8944 CAP_FOR_EACH_U32(__capi)
8945 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8946 seq_putc(m, '\n');
8947 return 0;
8948}
8949
8950static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8951{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008952 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008953 int i;
8954
Jens Axboefad8e0d2020-09-28 08:57:48 -06008955 /*
8956 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8957 * since fdinfo case grabs it in the opposite direction of normal use
8958 * cases. If we fail to get the lock, we just don't iterate any
8959 * structures that could be going away outside the io_uring mutex.
8960 */
8961 has_lock = mutex_trylock(&ctx->uring_lock);
8962
Jens Axboe87ce9552020-01-30 08:25:34 -07008963 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008964 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008965 struct fixed_file_table *table;
8966 struct file *f;
8967
8968 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8969 f = table->files[i & IORING_FILE_TABLE_MASK];
8970 if (f)
8971 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8972 else
8973 seq_printf(m, "%5u: <none>\n", i);
8974 }
8975 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008976 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008977 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8978
8979 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8980 (unsigned int) buf->len);
8981 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008982 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008983 seq_printf(m, "Personalities:\n");
8984 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8985 }
Jens Axboed7718a92020-02-14 22:23:12 -07008986 seq_printf(m, "PollList:\n");
8987 spin_lock_irq(&ctx->completion_lock);
8988 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8989 struct hlist_head *list = &ctx->cancel_hash[i];
8990 struct io_kiocb *req;
8991
8992 hlist_for_each_entry(req, list, hash_node)
8993 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8994 req->task->task_works != NULL);
8995 }
8996 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008997 if (has_lock)
8998 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008999}
9000
9001static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9002{
9003 struct io_ring_ctx *ctx = f->private_data;
9004
9005 if (percpu_ref_tryget(&ctx->refs)) {
9006 __io_uring_show_fdinfo(ctx, m);
9007 percpu_ref_put(&ctx->refs);
9008 }
9009}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009010#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009011
Jens Axboe2b188cc2019-01-07 10:46:33 -07009012static const struct file_operations io_uring_fops = {
9013 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009014 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009015 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009016#ifndef CONFIG_MMU
9017 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9018 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9019#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009020 .poll = io_uring_poll,
9021 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009022#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009023 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009024#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009025};
9026
9027static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9028 struct io_uring_params *p)
9029{
Hristo Venev75b28af2019-08-26 17:23:46 +00009030 struct io_rings *rings;
9031 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009032
Jens Axboebd740482020-08-05 12:58:23 -06009033 /* make sure these are sane, as we already accounted them */
9034 ctx->sq_entries = p->sq_entries;
9035 ctx->cq_entries = p->cq_entries;
9036
Hristo Venev75b28af2019-08-26 17:23:46 +00009037 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9038 if (size == SIZE_MAX)
9039 return -EOVERFLOW;
9040
9041 rings = io_mem_alloc(size);
9042 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009043 return -ENOMEM;
9044
Hristo Venev75b28af2019-08-26 17:23:46 +00009045 ctx->rings = rings;
9046 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9047 rings->sq_ring_mask = p->sq_entries - 1;
9048 rings->cq_ring_mask = p->cq_entries - 1;
9049 rings->sq_ring_entries = p->sq_entries;
9050 rings->cq_ring_entries = p->cq_entries;
9051 ctx->sq_mask = rings->sq_ring_mask;
9052 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009053
9054 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009055 if (size == SIZE_MAX) {
9056 io_mem_free(ctx->rings);
9057 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009058 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009059 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009060
9061 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009062 if (!ctx->sq_sqes) {
9063 io_mem_free(ctx->rings);
9064 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009065 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009066 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009067
Jens Axboe2b188cc2019-01-07 10:46:33 -07009068 return 0;
9069}
9070
9071/*
9072 * Allocate an anonymous fd, this is what constitutes the application
9073 * visible backing of an io_uring instance. The application mmaps this
9074 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9075 * we have to tie this fd to a socket for file garbage collection purposes.
9076 */
9077static int io_uring_get_fd(struct io_ring_ctx *ctx)
9078{
9079 struct file *file;
9080 int ret;
9081
9082#if defined(CONFIG_UNIX)
9083 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9084 &ctx->ring_sock);
9085 if (ret)
9086 return ret;
9087#endif
9088
9089 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9090 if (ret < 0)
9091 goto err;
9092
9093 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9094 O_RDWR | O_CLOEXEC);
9095 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009096err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009097 put_unused_fd(ret);
9098 ret = PTR_ERR(file);
9099 goto err;
9100 }
9101
9102#if defined(CONFIG_UNIX)
9103 ctx->ring_sock->file = file;
9104#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009105 if (unlikely(io_uring_add_task_file(file))) {
9106 file = ERR_PTR(-ENOMEM);
9107 goto err_fd;
9108 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009109 fd_install(ret, file);
9110 return ret;
9111err:
9112#if defined(CONFIG_UNIX)
9113 sock_release(ctx->ring_sock);
9114 ctx->ring_sock = NULL;
9115#endif
9116 return ret;
9117}
9118
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009119static int io_uring_create(unsigned entries, struct io_uring_params *p,
9120 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009121{
9122 struct user_struct *user = NULL;
9123 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009124 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009125 int ret;
9126
Jens Axboe8110c1a2019-12-28 15:39:54 -07009127 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009128 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009129 if (entries > IORING_MAX_ENTRIES) {
9130 if (!(p->flags & IORING_SETUP_CLAMP))
9131 return -EINVAL;
9132 entries = IORING_MAX_ENTRIES;
9133 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009134
9135 /*
9136 * Use twice as many entries for the CQ ring. It's possible for the
9137 * application to drive a higher depth than the size of the SQ ring,
9138 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009139 * some flexibility in overcommitting a bit. If the application has
9140 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9141 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009142 */
9143 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009144 if (p->flags & IORING_SETUP_CQSIZE) {
9145 /*
9146 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9147 * to a power-of-two, if it isn't already. We do NOT impose
9148 * any cq vs sq ring sizing.
9149 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009150 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009151 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009152 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9153 if (!(p->flags & IORING_SETUP_CLAMP))
9154 return -EINVAL;
9155 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9156 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009157 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9158 } else {
9159 p->cq_entries = 2 * p->sq_entries;
9160 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009161
9162 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009163 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009164
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009165 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009166 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009167 ring_pages(p->sq_entries, p->cq_entries));
9168 if (ret) {
9169 free_uid(user);
9170 return ret;
9171 }
9172 }
9173
9174 ctx = io_ring_ctx_alloc(p);
9175 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009176 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009177 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009178 p->cq_entries));
9179 free_uid(user);
9180 return -ENOMEM;
9181 }
9182 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009183 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009184 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009185
Jens Axboe2aede0e2020-09-14 10:45:53 -06009186 ctx->sqo_task = get_task_struct(current);
9187
9188 /*
9189 * This is just grabbed for accounting purposes. When a process exits,
9190 * the mm is exited and dropped before the files, hence we need to hang
9191 * on to this mm purely for the purposes of being able to unaccount
9192 * memory (locked/pinned vm). It's not used for anything else.
9193 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009194 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009195 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009196
Jens Axboef74441e2020-08-05 13:00:44 -06009197 /*
9198 * Account memory _before_ installing the file descriptor. Once
9199 * the descriptor is installed, it can get closed at any time. Also
9200 * do this before hitting the general error path, as ring freeing
9201 * will un-account as well.
9202 */
9203 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9204 ACCT_LOCKED);
9205 ctx->limit_mem = limit_mem;
9206
Jens Axboe2b188cc2019-01-07 10:46:33 -07009207 ret = io_allocate_scq_urings(ctx, p);
9208 if (ret)
9209 goto err;
9210
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009211 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009212 if (ret)
9213 goto err;
9214
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009215 if (!(p->flags & IORING_SETUP_R_DISABLED))
9216 io_sq_offload_start(ctx);
9217
Jens Axboe2b188cc2019-01-07 10:46:33 -07009218 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009219 p->sq_off.head = offsetof(struct io_rings, sq.head);
9220 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9221 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9222 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9223 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9224 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9225 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009226
9227 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009228 p->cq_off.head = offsetof(struct io_rings, cq.head);
9229 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9230 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9231 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9232 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9233 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009234 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009235
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009236 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9237 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009238 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9239 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009240
9241 if (copy_to_user(params, p, sizeof(*p))) {
9242 ret = -EFAULT;
9243 goto err;
9244 }
Jens Axboed1719f72020-07-30 13:43:53 -06009245
9246 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009247 * Install ring fd as the very last thing, so we don't risk someone
9248 * having closed it before we finish setup
9249 */
9250 ret = io_uring_get_fd(ctx);
9251 if (ret < 0)
9252 goto err;
9253
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009254 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009255 return ret;
9256err:
9257 io_ring_ctx_wait_and_kill(ctx);
9258 return ret;
9259}
9260
9261/*
9262 * Sets up an aio uring context, and returns the fd. Applications asks for a
9263 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9264 * params structure passed in.
9265 */
9266static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9267{
9268 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009269 int i;
9270
9271 if (copy_from_user(&p, params, sizeof(p)))
9272 return -EFAULT;
9273 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9274 if (p.resv[i])
9275 return -EINVAL;
9276 }
9277
Jens Axboe6c271ce2019-01-10 11:22:30 -07009278 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009279 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009280 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9281 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009282 return -EINVAL;
9283
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009284 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009285}
9286
9287SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9288 struct io_uring_params __user *, params)
9289{
9290 return io_uring_setup(entries, params);
9291}
9292
Jens Axboe66f4af92020-01-16 15:36:52 -07009293static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9294{
9295 struct io_uring_probe *p;
9296 size_t size;
9297 int i, ret;
9298
9299 size = struct_size(p, ops, nr_args);
9300 if (size == SIZE_MAX)
9301 return -EOVERFLOW;
9302 p = kzalloc(size, GFP_KERNEL);
9303 if (!p)
9304 return -ENOMEM;
9305
9306 ret = -EFAULT;
9307 if (copy_from_user(p, arg, size))
9308 goto out;
9309 ret = -EINVAL;
9310 if (memchr_inv(p, 0, size))
9311 goto out;
9312
9313 p->last_op = IORING_OP_LAST - 1;
9314 if (nr_args > IORING_OP_LAST)
9315 nr_args = IORING_OP_LAST;
9316
9317 for (i = 0; i < nr_args; i++) {
9318 p->ops[i].op = i;
9319 if (!io_op_defs[i].not_supported)
9320 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9321 }
9322 p->ops_len = i;
9323
9324 ret = 0;
9325 if (copy_to_user(arg, p, size))
9326 ret = -EFAULT;
9327out:
9328 kfree(p);
9329 return ret;
9330}
9331
Jens Axboe071698e2020-01-28 10:04:42 -07009332static int io_register_personality(struct io_ring_ctx *ctx)
9333{
9334 const struct cred *creds = get_current_cred();
9335 int id;
9336
9337 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9338 USHRT_MAX, GFP_KERNEL);
9339 if (id < 0)
9340 put_cred(creds);
9341 return id;
9342}
9343
9344static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9345{
9346 const struct cred *old_creds;
9347
9348 old_creds = idr_remove(&ctx->personality_idr, id);
9349 if (old_creds) {
9350 put_cred(old_creds);
9351 return 0;
9352 }
9353
9354 return -EINVAL;
9355}
9356
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009357static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9358 unsigned int nr_args)
9359{
9360 struct io_uring_restriction *res;
9361 size_t size;
9362 int i, ret;
9363
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009364 /* Restrictions allowed only if rings started disabled */
9365 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9366 return -EBADFD;
9367
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009368 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009369 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009370 return -EBUSY;
9371
9372 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9373 return -EINVAL;
9374
9375 size = array_size(nr_args, sizeof(*res));
9376 if (size == SIZE_MAX)
9377 return -EOVERFLOW;
9378
9379 res = memdup_user(arg, size);
9380 if (IS_ERR(res))
9381 return PTR_ERR(res);
9382
9383 ret = 0;
9384
9385 for (i = 0; i < nr_args; i++) {
9386 switch (res[i].opcode) {
9387 case IORING_RESTRICTION_REGISTER_OP:
9388 if (res[i].register_op >= IORING_REGISTER_LAST) {
9389 ret = -EINVAL;
9390 goto out;
9391 }
9392
9393 __set_bit(res[i].register_op,
9394 ctx->restrictions.register_op);
9395 break;
9396 case IORING_RESTRICTION_SQE_OP:
9397 if (res[i].sqe_op >= IORING_OP_LAST) {
9398 ret = -EINVAL;
9399 goto out;
9400 }
9401
9402 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9403 break;
9404 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9405 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9406 break;
9407 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9408 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9409 break;
9410 default:
9411 ret = -EINVAL;
9412 goto out;
9413 }
9414 }
9415
9416out:
9417 /* Reset all restrictions if an error happened */
9418 if (ret != 0)
9419 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9420 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009421 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009422
9423 kfree(res);
9424 return ret;
9425}
9426
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009427static int io_register_enable_rings(struct io_ring_ctx *ctx)
9428{
9429 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9430 return -EBADFD;
9431
9432 if (ctx->restrictions.registered)
9433 ctx->restricted = 1;
9434
9435 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9436
9437 io_sq_offload_start(ctx);
9438
9439 return 0;
9440}
9441
Jens Axboe071698e2020-01-28 10:04:42 -07009442static bool io_register_op_must_quiesce(int op)
9443{
9444 switch (op) {
9445 case IORING_UNREGISTER_FILES:
9446 case IORING_REGISTER_FILES_UPDATE:
9447 case IORING_REGISTER_PROBE:
9448 case IORING_REGISTER_PERSONALITY:
9449 case IORING_UNREGISTER_PERSONALITY:
9450 return false;
9451 default:
9452 return true;
9453 }
9454}
9455
Jens Axboeedafcce2019-01-09 09:16:05 -07009456static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9457 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009458 __releases(ctx->uring_lock)
9459 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009460{
9461 int ret;
9462
Jens Axboe35fa71a2019-04-22 10:23:23 -06009463 /*
9464 * We're inside the ring mutex, if the ref is already dying, then
9465 * someone else killed the ctx or is already going through
9466 * io_uring_register().
9467 */
9468 if (percpu_ref_is_dying(&ctx->refs))
9469 return -ENXIO;
9470
Jens Axboe071698e2020-01-28 10:04:42 -07009471 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009472 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009473
Jens Axboe05f3fb32019-12-09 11:22:50 -07009474 /*
9475 * Drop uring mutex before waiting for references to exit. If
9476 * another thread is currently inside io_uring_enter() it might
9477 * need to grab the uring_lock to make progress. If we hold it
9478 * here across the drain wait, then we can deadlock. It's safe
9479 * to drop the mutex here, since no new references will come in
9480 * after we've killed the percpu ref.
9481 */
9482 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009483 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009484 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009485 if (ret) {
9486 percpu_ref_resurrect(&ctx->refs);
9487 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009488 goto out_quiesce;
9489 }
9490 }
9491
9492 if (ctx->restricted) {
9493 if (opcode >= IORING_REGISTER_LAST) {
9494 ret = -EINVAL;
9495 goto out;
9496 }
9497
9498 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9499 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009500 goto out;
9501 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009502 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009503
9504 switch (opcode) {
9505 case IORING_REGISTER_BUFFERS:
9506 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9507 break;
9508 case IORING_UNREGISTER_BUFFERS:
9509 ret = -EINVAL;
9510 if (arg || nr_args)
9511 break;
9512 ret = io_sqe_buffer_unregister(ctx);
9513 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009514 case IORING_REGISTER_FILES:
9515 ret = io_sqe_files_register(ctx, arg, nr_args);
9516 break;
9517 case IORING_UNREGISTER_FILES:
9518 ret = -EINVAL;
9519 if (arg || nr_args)
9520 break;
9521 ret = io_sqe_files_unregister(ctx);
9522 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009523 case IORING_REGISTER_FILES_UPDATE:
9524 ret = io_sqe_files_update(ctx, arg, nr_args);
9525 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009526 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009527 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009528 ret = -EINVAL;
9529 if (nr_args != 1)
9530 break;
9531 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009532 if (ret)
9533 break;
9534 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9535 ctx->eventfd_async = 1;
9536 else
9537 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009538 break;
9539 case IORING_UNREGISTER_EVENTFD:
9540 ret = -EINVAL;
9541 if (arg || nr_args)
9542 break;
9543 ret = io_eventfd_unregister(ctx);
9544 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009545 case IORING_REGISTER_PROBE:
9546 ret = -EINVAL;
9547 if (!arg || nr_args > 256)
9548 break;
9549 ret = io_probe(ctx, arg, nr_args);
9550 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009551 case IORING_REGISTER_PERSONALITY:
9552 ret = -EINVAL;
9553 if (arg || nr_args)
9554 break;
9555 ret = io_register_personality(ctx);
9556 break;
9557 case IORING_UNREGISTER_PERSONALITY:
9558 ret = -EINVAL;
9559 if (arg)
9560 break;
9561 ret = io_unregister_personality(ctx, nr_args);
9562 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009563 case IORING_REGISTER_ENABLE_RINGS:
9564 ret = -EINVAL;
9565 if (arg || nr_args)
9566 break;
9567 ret = io_register_enable_rings(ctx);
9568 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009569 case IORING_REGISTER_RESTRICTIONS:
9570 ret = io_register_restrictions(ctx, arg, nr_args);
9571 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009572 default:
9573 ret = -EINVAL;
9574 break;
9575 }
9576
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009577out:
Jens Axboe071698e2020-01-28 10:04:42 -07009578 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009579 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009580 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009581out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009582 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009583 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009584 return ret;
9585}
9586
9587SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9588 void __user *, arg, unsigned int, nr_args)
9589{
9590 struct io_ring_ctx *ctx;
9591 long ret = -EBADF;
9592 struct fd f;
9593
9594 f = fdget(fd);
9595 if (!f.file)
9596 return -EBADF;
9597
9598 ret = -EOPNOTSUPP;
9599 if (f.file->f_op != &io_uring_fops)
9600 goto out_fput;
9601
9602 ctx = f.file->private_data;
9603
9604 mutex_lock(&ctx->uring_lock);
9605 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9606 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009607 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9608 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009609out_fput:
9610 fdput(f);
9611 return ret;
9612}
9613
Jens Axboe2b188cc2019-01-07 10:46:33 -07009614static int __init io_uring_init(void)
9615{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009616#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9617 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9618 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9619} while (0)
9620
9621#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9622 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9623 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9624 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9625 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9626 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9627 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9628 BUILD_BUG_SQE_ELEM(8, __u64, off);
9629 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9630 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009631 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009632 BUILD_BUG_SQE_ELEM(24, __u32, len);
9633 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9634 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9635 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9636 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009637 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9638 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009639 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9640 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9641 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9642 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9643 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9644 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9645 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9646 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009647 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009648 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9649 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9650 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009651 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009652
Jens Axboed3656342019-12-18 09:50:26 -07009653 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009654 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009655 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9656 return 0;
9657};
9658__initcall(io_uring_init);