blob: f3d90a6108f82bd420cd4adb640c715a96df05b2 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020084#define CREATE_TRACE_POINTS
85#include <trace/events/io_uring.h>
86
Jens Axboe2b188cc2019-01-07 10:46:33 -070087#include <uapi/linux/io_uring.h>
88
89#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060090#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
Daniel Xu5277dea2019-09-14 14:23:45 -070092#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060093#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060094
95/*
96 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
97 */
98#define IORING_FILE_TABLE_SHIFT 9
99#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
100#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
101#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200102#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
103 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700104
105struct io_uring {
106 u32 head ____cacheline_aligned_in_smp;
107 u32 tail ____cacheline_aligned_in_smp;
108};
109
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000111 * This data is shared with the application through the mmap at offsets
112 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 *
114 * The offsets to the member fields are published through struct
115 * io_sqring_offsets when calling io_uring_setup.
116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
119 * Head and tail offsets into the ring; the offsets need to be
120 * masked to get valid indices.
121 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * The kernel controls head of the sq ring and the tail of the cq ring,
123 * and the application controls tail of the sq ring and the head of the
124 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 * ring_entries - 1)
130 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 u32 sq_ring_mask, cq_ring_mask;
132 /* Ring sizes (constant, power of 2) */
133 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Number of invalid entries dropped by the kernel due to
136 * invalid index stored in array
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application (i.e. get number of "new events" by comparing to
140 * cached value).
141 *
142 * After a new SQ head value was read by the application this
143 * counter includes all submissions that were dropped reaching
144 * the new SQ head (and possibly more).
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200148 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application.
152 *
153 * The application needs a full memory barrier before checking
154 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
155 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000156 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200158 * Runtime CQ flags
159 *
160 * Written by the application, shouldn't be modified by the
161 * kernel.
162 */
163 u32 cq_flags;
164 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * Number of completion events lost because the queue was full;
166 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800167 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 * the completion queue.
169 *
170 * Written by the kernel, shouldn't be modified by the
171 * application (i.e. get number of "new events" by comparing to
172 * cached value).
173 *
174 * As completion events come in out of order this counter is not
175 * ordered with any other data.
176 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000177 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200178 /*
179 * Ring buffer of completion events.
180 *
181 * The kernel writes completion events fresh every time they are
182 * produced, so the application is allowed to modify pending
183 * entries.
184 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000185 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700186};
187
Jens Axboeedafcce2019-01-09 09:16:05 -0700188struct io_mapped_ubuf {
189 u64 ubuf;
190 size_t len;
191 struct bio_vec *bvec;
192 unsigned int nr_bvecs;
193};
194
Jens Axboe65e19f52019-10-26 07:20:21 -0600195struct fixed_file_table {
196 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700197};
198
Xiaoguang Wang05589552020-03-31 14:05:18 +0800199struct fixed_file_ref_node {
200 struct percpu_ref refs;
201 struct list_head node;
202 struct list_head file_list;
203 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600204 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800205};
206
Jens Axboe05f3fb32019-12-09 11:22:50 -0700207struct fixed_file_data {
208 struct fixed_file_table *table;
209 struct io_ring_ctx *ctx;
210
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700212 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800214 struct list_head ref_list;
215 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216};
217
Jens Axboe5a2e7452020-02-23 16:23:11 -0700218struct io_buffer {
219 struct list_head list;
220 __u64 addr;
221 __s32 len;
222 __u16 bid;
223};
224
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200225struct io_restriction {
226 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
227 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
228 u8 sqe_flags_allowed;
229 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200230 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200231};
232
Jens Axboe534ca6d2020-09-02 13:52:19 -0600233struct io_sq_data {
234 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600235 struct mutex lock;
236
237 /* ctx's that are using this sqd */
238 struct list_head ctx_list;
239 struct list_head ctx_new_list;
240 struct mutex ctx_lock;
241
Jens Axboe534ca6d2020-09-02 13:52:19 -0600242 struct task_struct *thread;
243 struct wait_queue_head wait;
244};
245
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246struct io_ring_ctx {
247 struct {
248 struct percpu_ref refs;
249 } ____cacheline_aligned_in_smp;
250
251 struct {
252 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800253 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700254 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800255 unsigned int cq_overflow_flushed: 1;
256 unsigned int drain_next: 1;
257 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200258 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259
Hristo Venev75b28af2019-08-26 17:23:46 +0000260 /*
261 * Ring buffer of indices into array of io_uring_sqe, which is
262 * mmapped by the application using the IORING_OFF_SQES offset.
263 *
264 * This indirection could e.g. be used to assign fixed
265 * io_uring_sqe entries to operations and only submit them to
266 * the queue when needed.
267 *
268 * The kernel modifies neither the indices array nor the entries
269 * array.
270 */
271 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700272 unsigned cached_sq_head;
273 unsigned sq_entries;
274 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700275 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600276 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700278 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600279
280 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600281 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700282 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283
Jens Axboefcb323c2019-10-24 12:39:47 -0600284 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700285 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286 } ____cacheline_aligned_in_smp;
287
Hristo Venev75b28af2019-08-26 17:23:46 +0000288 struct io_rings *rings;
289
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600291 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600292
293 /*
294 * For SQPOLL usage - we hold a reference to the parent task, so we
295 * have access to the ->files
296 */
297 struct task_struct *sqo_task;
298
299 /* Only used for accounting purposes */
300 struct mm_struct *mm_account;
301
Jens Axboe534ca6d2020-09-02 13:52:19 -0600302 struct io_sq_data *sq_data; /* if using sq thread polling */
303
Jens Axboe90554202020-09-03 12:12:41 -0600304 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600305 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600306 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307
Jens Axboe6b063142019-01-10 22:13:58 -0700308 /*
309 * If used, fixed file set. Writers must ensure that ->refs is dead,
310 * readers must ensure that ->refs is alive as long as the file* is
311 * used. Only updated through io_uring_register(2).
312 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700313 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700314 unsigned nr_user_files;
315
Jens Axboeedafcce2019-01-09 09:16:05 -0700316 /* if used, fixed mapped user buffers */
317 unsigned nr_user_bufs;
318 struct io_mapped_ubuf *user_bufs;
319
Jens Axboe2b188cc2019-01-07 10:46:33 -0700320 struct user_struct *user;
321
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700322 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700323
Jens Axboe0f158b42020-05-14 17:18:39 -0600324 struct completion ref_comp;
325 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700326
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700327 /* if all else fails... */
328 struct io_kiocb *fallback_req;
329
Jens Axboe206aefd2019-11-07 18:27:42 -0700330#if defined(CONFIG_UNIX)
331 struct socket *ring_sock;
332#endif
333
Jens Axboe5a2e7452020-02-23 16:23:11 -0700334 struct idr io_buffer_idr;
335
Jens Axboe071698e2020-01-28 10:04:42 -0700336 struct idr personality_idr;
337
Jens Axboe206aefd2019-11-07 18:27:42 -0700338 struct {
339 unsigned cached_cq_tail;
340 unsigned cq_entries;
341 unsigned cq_mask;
342 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700343 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700344 struct wait_queue_head cq_wait;
345 struct fasync_struct *cq_fasync;
346 struct eventfd_ctx *cq_ev_fd;
347 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700348
349 struct {
350 struct mutex uring_lock;
351 wait_queue_head_t wait;
352 } ____cacheline_aligned_in_smp;
353
354 struct {
355 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700356
Jens Axboedef596e2019-01-09 08:59:42 -0700357 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300358 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700359 * io_uring instances that don't use IORING_SETUP_SQPOLL.
360 * For SQPOLL, only the single threaded io_sq_thread() will
361 * manipulate the list, hence no extra locking is needed there.
362 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300363 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700364 struct hlist_head *cancel_hash;
365 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700366 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600367
368 spinlock_t inflight_lock;
369 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700370 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600371
Jens Axboe4a38aed22020-05-14 17:21:15 -0600372 struct delayed_work file_put_work;
373 struct llist_head file_put_llist;
374
Jens Axboe85faa7b2020-04-09 18:14:00 -0600375 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200376 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700377};
378
Jens Axboe09bb8392019-03-13 12:39:28 -0600379/*
380 * First field must be the file pointer in all the
381 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
382 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700383struct io_poll_iocb {
384 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700385 union {
386 struct wait_queue_head *head;
387 u64 addr;
388 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700389 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600390 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700391 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700392 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700393};
394
Jens Axboeb5dba592019-12-11 14:02:38 -0700395struct io_close {
396 struct file *file;
397 struct file *put_file;
398 int fd;
399};
400
Jens Axboead8a48a2019-11-15 08:49:11 -0700401struct io_timeout_data {
402 struct io_kiocb *req;
403 struct hrtimer timer;
404 struct timespec64 ts;
405 enum hrtimer_mode mode;
406};
407
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700408struct io_accept {
409 struct file *file;
410 struct sockaddr __user *addr;
411 int __user *addr_len;
412 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600413 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700414};
415
416struct io_sync {
417 struct file *file;
418 loff_t len;
419 loff_t off;
420 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700421 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700422};
423
Jens Axboefbf23842019-12-17 18:45:56 -0700424struct io_cancel {
425 struct file *file;
426 u64 addr;
427};
428
Jens Axboeb29472e2019-12-17 18:50:29 -0700429struct io_timeout {
430 struct file *file;
431 u64 addr;
432 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300433 u32 off;
434 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300435 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700436};
437
Jens Axboe9adbd452019-12-20 08:45:55 -0700438struct io_rw {
439 /* NOTE: kiocb has the file as the first member, so don't do it here */
440 struct kiocb kiocb;
441 u64 addr;
442 u64 len;
443};
444
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700445struct io_connect {
446 struct file *file;
447 struct sockaddr __user *addr;
448 int addr_len;
449};
450
Jens Axboee47293f2019-12-20 08:58:21 -0700451struct io_sr_msg {
452 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700453 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300454 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700455 void __user *buf;
456 };
Jens Axboee47293f2019-12-20 08:58:21 -0700457 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700458 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700459 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700460 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700461};
462
Jens Axboe15b71ab2019-12-11 11:20:36 -0700463struct io_open {
464 struct file *file;
465 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700466 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700467 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600468 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700469};
470
Jens Axboe05f3fb32019-12-09 11:22:50 -0700471struct io_files_update {
472 struct file *file;
473 u64 arg;
474 u32 nr_args;
475 u32 offset;
476};
477
Jens Axboe4840e412019-12-25 22:03:45 -0700478struct io_fadvise {
479 struct file *file;
480 u64 offset;
481 u32 len;
482 u32 advice;
483};
484
Jens Axboec1ca7572019-12-25 22:18:28 -0700485struct io_madvise {
486 struct file *file;
487 u64 addr;
488 u32 len;
489 u32 advice;
490};
491
Jens Axboe3e4827b2020-01-08 15:18:09 -0700492struct io_epoll {
493 struct file *file;
494 int epfd;
495 int op;
496 int fd;
497 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498};
499
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300500struct io_splice {
501 struct file *file_out;
502 struct file *file_in;
503 loff_t off_out;
504 loff_t off_in;
505 u64 len;
506 unsigned int flags;
507};
508
Jens Axboeddf0322d2020-02-23 16:41:33 -0700509struct io_provide_buf {
510 struct file *file;
511 __u64 addr;
512 __s32 len;
513 __u32 bgid;
514 __u16 nbufs;
515 __u16 bid;
516};
517
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700518struct io_statx {
519 struct file *file;
520 int dfd;
521 unsigned int mask;
522 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700523 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700524 struct statx __user *buffer;
525};
526
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300527struct io_completion {
528 struct file *file;
529 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300530 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300531};
532
Jens Axboef499a022019-12-02 16:28:46 -0700533struct io_async_connect {
534 struct sockaddr_storage address;
535};
536
Jens Axboe03b12302019-12-02 18:50:25 -0700537struct io_async_msghdr {
538 struct iovec fast_iov[UIO_FASTIOV];
539 struct iovec *iov;
540 struct sockaddr __user *uaddr;
541 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700542 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700543};
544
Jens Axboef67676d2019-12-02 11:03:47 -0700545struct io_async_rw {
546 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600547 const struct iovec *free_iovec;
548 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600549 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600550 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700551};
552
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700553struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700554 union {
555 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700556 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700557 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700558 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700559 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700560};
561
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562enum {
563 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
564 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
565 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
566 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
567 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700568 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300569
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300570 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300571 REQ_F_FAIL_LINK_BIT,
572 REQ_F_INFLIGHT_BIT,
573 REQ_F_CUR_POS_BIT,
574 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300576 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300578 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700579 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700580 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600581 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800582 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700583
584 /* not a real bit, just to check we're not overflowing the space */
585 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300586};
587
588enum {
589 /* ctx owns file */
590 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
591 /* drain existing IO first */
592 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
593 /* linked sqes */
594 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
595 /* doesn't sever on completion < 0 */
596 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
597 /* IOSQE_ASYNC */
598 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700599 /* IOSQE_BUFFER_SELECT */
600 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300601
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300602 /* head of a link */
603 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300604 /* fail rest of links */
605 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
606 /* on inflight list */
607 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
608 /* read/write uses file position */
609 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
610 /* must not punt to workers */
611 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300612 /* has linked timeout */
613 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300614 /* regular file */
615 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300616 /* completion under lock */
617 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300618 /* needs cleanup */
619 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700620 /* already went through poll handler */
621 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700622 /* buffer already selected */
623 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600624 /* doesn't need file table for this request */
625 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800626 /* io_wq_work is initialized */
627 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700628};
629
630struct async_poll {
631 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600632 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300633};
634
Jens Axboe09bb8392019-03-13 12:39:28 -0600635/*
636 * NOTE! Each of the iocb union members has the file pointer
637 * as the first entry in their struct definition. So you can
638 * access the file pointer through any of the sub-structs,
639 * or directly as just 'ki_filp' in this struct.
640 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700642 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600643 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700644 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700645 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700646 struct io_accept accept;
647 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700648 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700649 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700650 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700651 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700652 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700653 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700654 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700655 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700656 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700657 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300658 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700659 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700660 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300661 /* use only after cleaning per-op data, see io_clean_op() */
662 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700663 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700664
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700665 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700666 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800667 /* polled IO has completed */
668 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700669
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700670 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300671 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700672
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300673 struct io_ring_ctx *ctx;
674 unsigned int flags;
675 refcount_t refs;
676 struct task_struct *task;
677 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700678
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300679 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700680
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300681 /*
682 * 1. used with ctx->iopoll_list with reads/writes
683 * 2. to track reqs with ->files (see io_op_def::file_table)
684 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300685 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600686
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300687 struct percpu_ref *fixed_file_refs;
688 struct callback_head task_work;
689 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
690 struct hlist_node hash_node;
691 struct async_poll *apoll;
692 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700693};
694
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300695struct io_defer_entry {
696 struct list_head list;
697 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300698 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300699};
700
Jens Axboedef596e2019-01-09 08:59:42 -0700701#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700702
Jens Axboe013538b2020-06-22 09:29:15 -0600703struct io_comp_state {
704 unsigned int nr;
705 struct list_head list;
706 struct io_ring_ctx *ctx;
707};
708
Jens Axboe9a56a232019-01-09 09:06:50 -0700709struct io_submit_state {
710 struct blk_plug plug;
711
712 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700713 * io_kiocb alloc cache
714 */
715 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300716 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700717
718 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600719 * Batch completion logic
720 */
721 struct io_comp_state comp;
722
723 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700724 * File reference cache
725 */
726 struct file *file;
727 unsigned int fd;
728 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700729 unsigned int ios_left;
730};
731
Jens Axboed3656342019-12-18 09:50:26 -0700732struct io_op_def {
733 /* needs req->io allocated for deferral/async */
734 unsigned async_ctx : 1;
735 /* needs current->mm setup, does mm access */
736 unsigned needs_mm : 1;
737 /* needs req->file assigned */
738 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600739 /* don't fail if file grab fails */
740 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700741 /* hash wq insertion if file is a regular file */
742 unsigned hash_reg_file : 1;
743 /* unbound wq insertion if file is a non-regular file */
744 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700745 /* opcode is not supported by this kernel */
746 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700747 /* needs file table */
748 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700749 /* needs ->fs */
750 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700751 /* set if opcode supports polled "wait" */
752 unsigned pollin : 1;
753 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700754 /* op supports buffer selection */
755 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300756 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700757};
758
Jens Axboe738277a2020-09-03 05:54:56 -0600759static const struct io_op_def io_op_defs[] __read_mostly = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_NOP] = {},
761 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700762 .async_ctx = 1,
763 .needs_mm = 1,
764 .needs_file = 1,
765 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700766 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700767 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .async_ctx = 1,
771 .needs_mm = 1,
772 .needs_file = 1,
773 .hash_reg_file = 1,
774 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700775 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300776 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700779 .needs_file = 1,
780 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300781 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700782 .needs_file = 1,
783 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700784 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700785 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300786 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700787 .needs_file = 1,
788 .hash_reg_file = 1,
789 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700790 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300791 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700792 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300793 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700794 .needs_file = 1,
795 .unbound_nonreg_file = 1,
796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_POLL_REMOVE] = {},
798 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700799 .needs_file = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700802 .async_ctx = 1,
803 .needs_mm = 1,
804 .needs_file = 1,
805 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700806 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700807 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .async_ctx = 1,
811 .needs_mm = 1,
812 .needs_file = 1,
813 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700814 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700815 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700816 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .async_ctx = 1,
820 .needs_mm = 1,
821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_TIMEOUT_REMOVE] = {},
823 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_mm = 1,
825 .needs_file = 1,
826 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700827 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700828 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_ASYNC_CANCEL] = {},
831 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700832 .async_ctx = 1,
833 .needs_mm = 1,
834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .async_ctx = 1,
837 .needs_mm = 1,
838 .needs_file = 1,
839 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700840 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700843 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300844 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700847 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700848 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700849 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300850 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600851 .needs_file = 1,
852 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700853 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700856 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700857 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700858 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300859 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700860 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700861 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600862 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700865 .needs_mm = 1,
866 .needs_file = 1,
867 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700868 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700869 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700872 .needs_mm = 1,
873 .needs_file = 1,
874 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700875 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300876 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700879 .needs_file = 1,
880 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300881 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700882 .needs_mm = 1,
883 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300884 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700885 .needs_mm = 1,
886 .needs_file = 1,
887 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700888 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700889 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300890 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700891 .needs_mm = 1,
892 .needs_file = 1,
893 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700894 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700895 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700898 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700899 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700900 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700901 [IORING_OP_EPOLL_CTL] = {
902 .unbound_nonreg_file = 1,
903 .file_table = 1,
904 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300905 [IORING_OP_SPLICE] = {
906 .needs_file = 1,
907 .hash_reg_file = 1,
908 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700909 },
910 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700911 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300912 [IORING_OP_TEE] = {
913 .needs_file = 1,
914 .hash_reg_file = 1,
915 .unbound_nonreg_file = 1,
916 },
Jens Axboed3656342019-12-18 09:50:26 -0700917};
918
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700919enum io_mem_account {
920 ACCT_LOCKED,
921 ACCT_PINNED,
922};
923
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300924static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
925 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700926static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800927static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600928static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700929static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700930static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600931static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700932static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700933static int __io_sqe_files_update(struct io_ring_ctx *ctx,
934 struct io_uring_files_update *ip,
935 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300936static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300937static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700938static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
939 int fd, struct file **out_file, bool fixed);
940static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600941 const struct io_uring_sqe *sqe,
942 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600943static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600944
Jens Axboeb63534c2020-06-04 11:28:00 -0600945static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
946 struct iovec **iovec, struct iov_iter *iter,
947 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600948static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
949 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600950 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700951
952static struct kmem_cache *req_cachep;
953
Jens Axboe738277a2020-09-03 05:54:56 -0600954static const struct file_operations io_uring_fops __read_mostly;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700955
956struct sock *io_uring_get_socket(struct file *file)
957{
958#if defined(CONFIG_UNIX)
959 if (file->f_op == &io_uring_fops) {
960 struct io_ring_ctx *ctx = file->private_data;
961
962 return ctx->ring_sock->sk;
963 }
964#endif
965 return NULL;
966}
967EXPORT_SYMBOL(io_uring_get_socket);
968
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300969static inline void io_clean_op(struct io_kiocb *req)
970{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300971 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
972 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300973 __io_clean_op(req);
974}
975
Jens Axboe4349f302020-07-09 15:07:01 -0600976static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600977{
978 struct mm_struct *mm = current->mm;
979
980 if (mm) {
981 kthread_unuse_mm(mm);
982 mmput(mm);
983 }
984}
985
986static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
987{
988 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300989 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600990 !ctx->sqo_task->mm ||
991 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600992 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600993 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600994 }
995
996 return 0;
997}
998
999static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1000 struct io_kiocb *req)
1001{
1002 if (!io_op_defs[req->opcode].needs_mm)
1003 return 0;
1004 return __io_sq_thread_acquire_mm(ctx);
1005}
1006
1007static inline void req_set_fail_links(struct io_kiocb *req)
1008{
1009 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1010 req->flags |= REQ_F_FAIL_LINK;
1011}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001012
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001013/*
1014 * Note: must call io_req_init_async() for the first time you
1015 * touch any members of io_wq_work.
1016 */
1017static inline void io_req_init_async(struct io_kiocb *req)
1018{
1019 if (req->flags & REQ_F_WORK_INITIALIZED)
1020 return;
1021
1022 memset(&req->work, 0, sizeof(req->work));
1023 req->flags |= REQ_F_WORK_INITIALIZED;
1024}
1025
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001026static inline bool io_async_submit(struct io_ring_ctx *ctx)
1027{
1028 return ctx->flags & IORING_SETUP_SQPOLL;
1029}
1030
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1032{
1033 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1034
Jens Axboe0f158b42020-05-14 17:18:39 -06001035 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001036}
1037
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001038static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1039{
1040 return !req->timeout.off;
1041}
1042
Jens Axboe2b188cc2019-01-07 10:46:33 -07001043static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1044{
1045 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001046 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047
1048 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1049 if (!ctx)
1050 return NULL;
1051
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001052 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1053 if (!ctx->fallback_req)
1054 goto err;
1055
Jens Axboe78076bb2019-12-04 19:56:40 -07001056 /*
1057 * Use 5 bits less than the max cq entries, that should give us around
1058 * 32 entries per hash list if totally full and uniformly spread.
1059 */
1060 hash_bits = ilog2(p->cq_entries);
1061 hash_bits -= 5;
1062 if (hash_bits <= 0)
1063 hash_bits = 1;
1064 ctx->cancel_hash_bits = hash_bits;
1065 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1066 GFP_KERNEL);
1067 if (!ctx->cancel_hash)
1068 goto err;
1069 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1070
Roman Gushchin21482892019-05-07 10:01:48 -07001071 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001072 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1073 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074
1075 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001076 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001077 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001078 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001079 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001080 init_completion(&ctx->ref_comp);
1081 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001082 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001083 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 mutex_init(&ctx->uring_lock);
1085 init_waitqueue_head(&ctx->wait);
1086 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001087 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001088 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001089 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001090 init_waitqueue_head(&ctx->inflight_wait);
1091 spin_lock_init(&ctx->inflight_lock);
1092 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001093 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1094 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001096err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001097 if (ctx->fallback_req)
1098 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001099 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001100 kfree(ctx);
1101 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102}
1103
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001104static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001105{
Jens Axboe2bc99302020-07-09 09:43:27 -06001106 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1107 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001108
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001109 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001110 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001111 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001112
Bob Liu9d858b22019-11-13 18:06:25 +08001113 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001114}
1115
Jens Axboede0617e2019-04-06 21:51:27 -06001116static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117{
Hristo Venev75b28af2019-08-26 17:23:46 +00001118 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119
Pavel Begunkov07910152020-01-17 03:52:46 +03001120 /* order cqe stores with ring update */
1121 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001122
Pavel Begunkov07910152020-01-17 03:52:46 +03001123 if (wq_has_sleeper(&ctx->cq_wait)) {
1124 wake_up_interruptible(&ctx->cq_wait);
1125 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001126 }
1127}
1128
Jens Axboe51a4cc12020-08-10 10:55:56 -06001129/*
1130 * Returns true if we need to defer file table putting. This can only happen
1131 * from the error path with REQ_F_COMP_LOCKED set.
1132 */
1133static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001134{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001135 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001136 return false;
1137
1138 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001139
Jens Axboecccf0ee2020-01-27 16:34:48 -07001140 if (req->work.mm) {
1141 mmdrop(req->work.mm);
1142 req->work.mm = NULL;
1143 }
1144 if (req->work.creds) {
1145 put_cred(req->work.creds);
1146 req->work.creds = NULL;
1147 }
Jens Axboeff002b32020-02-07 16:05:21 -07001148 if (req->work.fs) {
1149 struct fs_struct *fs = req->work.fs;
1150
Jens Axboe51a4cc12020-08-10 10:55:56 -06001151 if (req->flags & REQ_F_COMP_LOCKED)
1152 return true;
1153
Jens Axboeff002b32020-02-07 16:05:21 -07001154 spin_lock(&req->work.fs->lock);
1155 if (--fs->users)
1156 fs = NULL;
1157 spin_unlock(&req->work.fs->lock);
1158 if (fs)
1159 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001160 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001161 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001162
1163 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001164}
1165
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001166static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001167{
Jens Axboed3656342019-12-18 09:50:26 -07001168 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001169
Pavel Begunkov16d59802020-07-12 16:16:47 +03001170 io_req_init_async(req);
1171
Jens Axboed3656342019-12-18 09:50:26 -07001172 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001173 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001174 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001175 } else {
1176 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001177 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001178 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001179 if (!req->work.mm && def->needs_mm) {
1180 mmgrab(current->mm);
1181 req->work.mm = current->mm;
1182 }
1183 if (!req->work.creds)
1184 req->work.creds = get_current_cred();
1185 if (!req->work.fs && def->needs_fs) {
1186 spin_lock(&current->fs->lock);
1187 if (!current->fs->in_exec) {
1188 req->work.fs = current->fs;
1189 req->work.fs->users++;
1190 } else {
1191 req->work.flags |= IO_WQ_WORK_CANCEL;
1192 }
1193 spin_unlock(&current->fs->lock);
1194 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001195 if (def->needs_fsize)
1196 req->work.fsize = rlimit(RLIMIT_FSIZE);
1197 else
1198 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001199}
1200
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001201static void io_prep_async_link(struct io_kiocb *req)
1202{
1203 struct io_kiocb *cur;
1204
1205 io_prep_async_work(req);
1206 if (req->flags & REQ_F_LINK_HEAD)
1207 list_for_each_entry(cur, &req->link_list, link_list)
1208 io_prep_async_work(cur);
1209}
1210
Jens Axboe7271ef32020-08-10 09:55:22 -06001211static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001212{
Jackie Liua197f662019-11-08 08:09:12 -07001213 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001214 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001215
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001216 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1217 &req->work, req->flags);
1218 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001219 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001220}
1221
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001222static void io_queue_async_work(struct io_kiocb *req)
1223{
Jens Axboe7271ef32020-08-10 09:55:22 -06001224 struct io_kiocb *link;
1225
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001226 /* init ->work of the whole link before punting */
1227 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001228 link = __io_queue_async_work(req);
1229
1230 if (link)
1231 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001232}
1233
Jens Axboe5262f562019-09-17 12:26:57 -06001234static void io_kill_timeout(struct io_kiocb *req)
1235{
1236 int ret;
1237
Jens Axboe2d283902019-12-04 11:08:05 -07001238 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001239 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001240 atomic_set(&req->ctx->cq_timeouts,
1241 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001242 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001243 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001244 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001245 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001246 }
1247}
1248
Jens Axboef3606e32020-09-22 08:18:24 -06001249static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1250{
1251 struct io_ring_ctx *ctx = req->ctx;
1252
1253 if (!tsk || req->task == tsk)
1254 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001255 if (ctx->flags & IORING_SETUP_SQPOLL) {
1256 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1257 return true;
1258 }
Jens Axboef3606e32020-09-22 08:18:24 -06001259 return false;
1260}
1261
Jens Axboe76e1b642020-09-26 15:05:03 -06001262/*
1263 * Returns true if we found and killed one or more timeouts
1264 */
1265static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001266{
1267 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001268 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001269
1270 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001271 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001272 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001273 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001274 canceled++;
1275 }
Jens Axboef3606e32020-09-22 08:18:24 -06001276 }
Jens Axboe5262f562019-09-17 12:26:57 -06001277 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001278 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001279}
1280
Pavel Begunkov04518942020-05-26 20:34:05 +03001281static void __io_queue_deferred(struct io_ring_ctx *ctx)
1282{
1283 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001284 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1285 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001286 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001287
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001288 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001289 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001290 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001291 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001292 link = __io_queue_async_work(de->req);
1293 if (link) {
1294 __io_queue_linked_timeout(link);
1295 /* drop submission reference */
1296 link->flags |= REQ_F_COMP_LOCKED;
1297 io_put_req(link);
1298 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001299 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001300 } while (!list_empty(&ctx->defer_list));
1301}
1302
Pavel Begunkov360428f2020-05-30 14:54:17 +03001303static void io_flush_timeouts(struct io_ring_ctx *ctx)
1304{
1305 while (!list_empty(&ctx->timeout_list)) {
1306 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001307 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001308
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001309 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001310 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001311 if (req->timeout.target_seq != ctx->cached_cq_tail
1312 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001313 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001314
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001315 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001316 io_kill_timeout(req);
1317 }
1318}
1319
Jens Axboede0617e2019-04-06 21:51:27 -06001320static void io_commit_cqring(struct io_ring_ctx *ctx)
1321{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001322 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001323 __io_commit_cqring(ctx);
1324
Pavel Begunkov04518942020-05-26 20:34:05 +03001325 if (unlikely(!list_empty(&ctx->defer_list)))
1326 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001327}
1328
Jens Axboe90554202020-09-03 12:12:41 -06001329static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1330{
1331 struct io_rings *r = ctx->rings;
1332
1333 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1334}
1335
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1337{
Hristo Venev75b28af2019-08-26 17:23:46 +00001338 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339 unsigned tail;
1340
1341 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001342 /*
1343 * writes to the cq entry need to come after reading head; the
1344 * control dependency is enough as we're using WRITE_ONCE to
1345 * fill the cq entry
1346 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001347 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348 return NULL;
1349
1350 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001351 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352}
1353
Jens Axboef2842ab2020-01-08 11:04:00 -07001354static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1355{
Jens Axboef0b493e2020-02-01 21:30:11 -07001356 if (!ctx->cq_ev_fd)
1357 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001358 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1359 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001360 if (!ctx->eventfd_async)
1361 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001362 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001363}
1364
Jens Axboeb41e9852020-02-17 09:52:41 -07001365static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001366{
1367 if (waitqueue_active(&ctx->wait))
1368 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001369 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1370 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001371 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001372 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001373}
1374
Pavel Begunkov46930142020-07-30 18:43:49 +03001375static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1376{
1377 if (list_empty(&ctx->cq_overflow_list)) {
1378 clear_bit(0, &ctx->sq_check_overflow);
1379 clear_bit(0, &ctx->cq_check_overflow);
1380 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1381 }
1382}
1383
Jens Axboee6c8aa92020-09-28 13:10:13 -06001384static inline bool io_match_files(struct io_kiocb *req,
1385 struct files_struct *files)
1386{
1387 if (!files)
1388 return true;
1389 if (req->flags & REQ_F_WORK_INITIALIZED)
1390 return req->work.files == files;
1391 return false;
1392}
1393
Jens Axboec4a2ed72019-11-21 21:01:26 -07001394/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001395static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1396 struct task_struct *tsk,
1397 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001399 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001400 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001401 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001402 unsigned long flags;
1403 LIST_HEAD(list);
1404
1405 if (!force) {
1406 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001407 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001408 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1409 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001410 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001411 }
1412
1413 spin_lock_irqsave(&ctx->completion_lock, flags);
1414
1415 /* if force is set, the ring is going away. always drop after that */
1416 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001417 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001418
Jens Axboec4a2ed72019-11-21 21:01:26 -07001419 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001420 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1421 if (tsk && req->task != tsk)
1422 continue;
1423 if (!io_match_files(req, files))
1424 continue;
1425
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001426 cqe = io_get_cqring(ctx);
1427 if (!cqe && !force)
1428 break;
1429
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001430 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001431 if (cqe) {
1432 WRITE_ONCE(cqe->user_data, req->user_data);
1433 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001434 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001435 } else {
1436 WRITE_ONCE(ctx->rings->cq_overflow,
1437 atomic_inc_return(&ctx->cached_cq_overflow));
1438 }
1439 }
1440
1441 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001442 io_cqring_mark_overflow(ctx);
1443
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001444 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1445 io_cqring_ev_posted(ctx);
1446
1447 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001448 req = list_first_entry(&list, struct io_kiocb, compl.list);
1449 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001450 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001451 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001452
1453 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001454}
1455
Jens Axboebcda7ba2020-02-23 16:42:51 -07001456static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001457{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001459 struct io_uring_cqe *cqe;
1460
Jens Axboe78e19bb2019-11-06 15:21:34 -07001461 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001462
Jens Axboe2b188cc2019-01-07 10:46:33 -07001463 /*
1464 * If we can't get a cq entry, userspace overflowed the
1465 * submission (by quite a lot). Increment the overflow count in
1466 * the ring.
1467 */
1468 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001469 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001470 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001471 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001472 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001473 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1474 /*
1475 * If we're in ring overflow flush mode, or in task cancel mode,
1476 * then we cannot store the request for later flushing, we need
1477 * to drop it on the floor.
1478 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479 WRITE_ONCE(ctx->rings->cq_overflow,
1480 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001481 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001482 if (list_empty(&ctx->cq_overflow_list)) {
1483 set_bit(0, &ctx->sq_check_overflow);
1484 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001485 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001486 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001487 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001488 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001489 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001490 refcount_inc(&req->refs);
1491 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001492 }
1493}
1494
Jens Axboebcda7ba2020-02-23 16:42:51 -07001495static void io_cqring_fill_event(struct io_kiocb *req, long res)
1496{
1497 __io_cqring_fill_event(req, res, 0);
1498}
1499
Jens Axboee1e16092020-06-22 09:17:17 -06001500static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001503 unsigned long flags;
1504
1505 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001506 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001507 io_commit_cqring(ctx);
1508 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1509
Jens Axboe8c838782019-03-12 15:48:16 -06001510 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001511}
1512
Jens Axboe229a7b62020-06-22 10:13:11 -06001513static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001514{
Jens Axboe229a7b62020-06-22 10:13:11 -06001515 struct io_ring_ctx *ctx = cs->ctx;
1516
1517 spin_lock_irq(&ctx->completion_lock);
1518 while (!list_empty(&cs->list)) {
1519 struct io_kiocb *req;
1520
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001521 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1522 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001523 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001524 if (!(req->flags & REQ_F_LINK_HEAD)) {
1525 req->flags |= REQ_F_COMP_LOCKED;
1526 io_put_req(req);
1527 } else {
1528 spin_unlock_irq(&ctx->completion_lock);
1529 io_put_req(req);
1530 spin_lock_irq(&ctx->completion_lock);
1531 }
1532 }
1533 io_commit_cqring(ctx);
1534 spin_unlock_irq(&ctx->completion_lock);
1535
1536 io_cqring_ev_posted(ctx);
1537 cs->nr = 0;
1538}
1539
1540static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1541 struct io_comp_state *cs)
1542{
1543 if (!cs) {
1544 io_cqring_add_event(req, res, cflags);
1545 io_put_req(req);
1546 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001547 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001548 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001549 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001550 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001551 if (++cs->nr >= 32)
1552 io_submit_flush_completions(cs);
1553 }
Jens Axboee1e16092020-06-22 09:17:17 -06001554}
1555
1556static void io_req_complete(struct io_kiocb *req, long res)
1557{
Jens Axboe229a7b62020-06-22 10:13:11 -06001558 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001559}
1560
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001561static inline bool io_is_fallback_req(struct io_kiocb *req)
1562{
1563 return req == (struct io_kiocb *)
1564 ((unsigned long) req->ctx->fallback_req & ~1UL);
1565}
1566
1567static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1568{
1569 struct io_kiocb *req;
1570
1571 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001572 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001573 return req;
1574
1575 return NULL;
1576}
1577
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001578static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1579 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001580{
Jens Axboefd6fab22019-03-14 16:30:06 -06001581 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001582 struct io_kiocb *req;
1583
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001584 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001585 size_t sz;
1586 int ret;
1587
1588 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001589 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1590
1591 /*
1592 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1593 * retry single alloc to be on the safe side.
1594 */
1595 if (unlikely(ret <= 0)) {
1596 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1597 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001598 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001599 ret = 1;
1600 }
Jens Axboe2579f912019-01-09 09:10:43 -07001601 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001602 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001603 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001604 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001605 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606 }
1607
Jens Axboe2579f912019-01-09 09:10:43 -07001608 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001609fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001610 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001611}
1612
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001613static inline void io_put_file(struct io_kiocb *req, struct file *file,
1614 bool fixed)
1615{
1616 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001617 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001618 else
1619 fput(file);
1620}
1621
Jens Axboe51a4cc12020-08-10 10:55:56 -06001622static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001623{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001624 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001625
Jens Axboe5acbbc82020-07-08 15:15:26 -06001626 if (req->io)
1627 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001628 if (req->file)
1629 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001630
Jens Axboe51a4cc12020-08-10 10:55:56 -06001631 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001632}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001633
Jens Axboe51a4cc12020-08-10 10:55:56 -06001634static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001635{
Jens Axboe0f212202020-09-13 13:09:39 -06001636 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001637 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001638
Jens Axboe0f212202020-09-13 13:09:39 -06001639 atomic_long_inc(&tctx->req_complete);
1640 if (tctx->in_idle)
1641 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001642 put_task_struct(req->task);
1643
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001644 if (likely(!io_is_fallback_req(req)))
1645 kmem_cache_free(req_cachep, req);
1646 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001647 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1648 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001649}
1650
Jens Axboe51a4cc12020-08-10 10:55:56 -06001651static void io_req_task_file_table_put(struct callback_head *cb)
1652{
1653 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1654 struct fs_struct *fs = req->work.fs;
1655
1656 spin_lock(&req->work.fs->lock);
1657 if (--fs->users)
1658 fs = NULL;
1659 spin_unlock(&req->work.fs->lock);
1660 if (fs)
1661 free_fs_struct(fs);
1662 req->work.fs = NULL;
1663 __io_free_req_finish(req);
1664}
1665
1666static void __io_free_req(struct io_kiocb *req)
1667{
1668 if (!io_dismantle_req(req)) {
1669 __io_free_req_finish(req);
1670 } else {
1671 int ret;
1672
1673 init_task_work(&req->task_work, io_req_task_file_table_put);
1674 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1675 if (unlikely(ret)) {
1676 struct task_struct *tsk;
1677
1678 tsk = io_wq_get_task(req->ctx->io_wq);
1679 task_work_add(tsk, &req->task_work, 0);
1680 }
1681 }
1682}
1683
Jackie Liua197f662019-11-08 08:09:12 -07001684static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001685{
Jackie Liua197f662019-11-08 08:09:12 -07001686 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001687 int ret;
1688
Jens Axboe2d283902019-12-04 11:08:05 -07001689 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001690 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001691 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001692 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001693 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001694 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001695 return true;
1696 }
1697
1698 return false;
1699}
1700
Jens Axboeab0b6452020-06-30 08:43:15 -06001701static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001702{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001703 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001704 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001705
1706 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001707 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001708 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1709 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001710 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001711
1712 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001713 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001714 wake_ev = io_link_cancel_timeout(link);
1715 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001716 return wake_ev;
1717}
1718
1719static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001720{
Jens Axboe2665abf2019-11-05 12:40:47 -07001721 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001722 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001723
Jens Axboeab0b6452020-06-30 08:43:15 -06001724 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1725 unsigned long flags;
1726
1727 spin_lock_irqsave(&ctx->completion_lock, flags);
1728 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001729 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001730 } else {
1731 wake_ev = __io_kill_linked_timeout(req);
1732 }
1733
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001734 if (wake_ev)
1735 io_cqring_ev_posted(ctx);
1736}
1737
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001738static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001739{
1740 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001741
Jens Axboe9e645e112019-05-10 16:07:28 -06001742 /*
1743 * The list should never be empty when we are called here. But could
1744 * potentially happen if the chain is messed up, check to be on the
1745 * safe side.
1746 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001747 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001748 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001749
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001750 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1751 list_del_init(&req->link_list);
1752 if (!list_empty(&nxt->link_list))
1753 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001754 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001755}
1756
1757/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001758 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001759 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001760static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001761{
Jens Axboe2665abf2019-11-05 12:40:47 -07001762 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001763
1764 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001765 struct io_kiocb *link = list_first_entry(&req->link_list,
1766 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001767
Pavel Begunkov44932332019-12-05 16:16:35 +03001768 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001769 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001770
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001771 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001772 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001773 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001774 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001775 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001776
1777 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001778 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001779}
1780
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001781static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001782{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001783 struct io_ring_ctx *ctx = req->ctx;
1784
1785 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1786 unsigned long flags;
1787
1788 spin_lock_irqsave(&ctx->completion_lock, flags);
1789 __io_fail_links(req);
1790 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1791 } else {
1792 __io_fail_links(req);
1793 }
1794
Jens Axboe9e645e112019-05-10 16:07:28 -06001795 io_cqring_ev_posted(ctx);
1796}
1797
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001798static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001799{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001800 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001801 if (req->flags & REQ_F_LINK_TIMEOUT)
1802 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001803
Jens Axboe9e645e112019-05-10 16:07:28 -06001804 /*
1805 * If LINK is set, we have dependent requests in this chain. If we
1806 * didn't fail this request, queue the first one up, moving any other
1807 * dependencies to the next request. In case of failure, fail the rest
1808 * of the chain.
1809 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001810 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1811 return io_req_link_next(req);
1812 io_fail_links(req);
1813 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001814}
Jens Axboe2665abf2019-11-05 12:40:47 -07001815
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001816static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1817{
1818 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1819 return NULL;
1820 return __io_req_find_next(req);
1821}
1822
Jens Axboefd7d6de2020-08-23 11:00:37 -06001823static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1824 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001825{
1826 struct task_struct *tsk = req->task;
1827 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001828 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001829
Jens Axboe6200b0a2020-09-13 14:38:30 -06001830 if (tsk->flags & PF_EXITING)
1831 return -ESRCH;
1832
Jens Axboec2c4c832020-07-01 15:37:11 -06001833 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001834 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1835 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1836 * processing task_work. There's no reliable way to tell if TWA_RESUME
1837 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001838 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001839 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001840 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001841 notify = TWA_SIGNAL;
1842
1843 ret = task_work_add(tsk, cb, notify);
1844 if (!ret)
1845 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001846
Jens Axboec2c4c832020-07-01 15:37:11 -06001847 return ret;
1848}
1849
Jens Axboec40f6372020-06-25 15:39:59 -06001850static void __io_req_task_cancel(struct io_kiocb *req, int error)
1851{
1852 struct io_ring_ctx *ctx = req->ctx;
1853
1854 spin_lock_irq(&ctx->completion_lock);
1855 io_cqring_fill_event(req, error);
1856 io_commit_cqring(ctx);
1857 spin_unlock_irq(&ctx->completion_lock);
1858
1859 io_cqring_ev_posted(ctx);
1860 req_set_fail_links(req);
1861 io_double_put_req(req);
1862}
1863
1864static void io_req_task_cancel(struct callback_head *cb)
1865{
1866 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001867 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001868
1869 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001870 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001871}
1872
1873static void __io_req_task_submit(struct io_kiocb *req)
1874{
1875 struct io_ring_ctx *ctx = req->ctx;
1876
Jens Axboec40f6372020-06-25 15:39:59 -06001877 if (!__io_sq_thread_acquire_mm(ctx)) {
1878 mutex_lock(&ctx->uring_lock);
1879 __io_queue_sqe(req, NULL, NULL);
1880 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001881 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001882 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001883 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001884}
1885
Jens Axboec40f6372020-06-25 15:39:59 -06001886static void io_req_task_submit(struct callback_head *cb)
1887{
1888 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001889 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001890
1891 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001892 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001893}
1894
1895static void io_req_task_queue(struct io_kiocb *req)
1896{
Jens Axboec40f6372020-06-25 15:39:59 -06001897 int ret;
1898
1899 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001900 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001901
Jens Axboefd7d6de2020-08-23 11:00:37 -06001902 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001903 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001904 struct task_struct *tsk;
1905
Jens Axboec40f6372020-06-25 15:39:59 -06001906 init_task_work(&req->task_work, io_req_task_cancel);
1907 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001908 task_work_add(tsk, &req->task_work, 0);
1909 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001910 }
Jens Axboec40f6372020-06-25 15:39:59 -06001911}
1912
Pavel Begunkovc3524382020-06-28 12:52:32 +03001913static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001914{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001915 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001916
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001917 if (nxt)
1918 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001919}
1920
Jens Axboe9e645e112019-05-10 16:07:28 -06001921static void io_free_req(struct io_kiocb *req)
1922{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001923 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001924 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001925}
1926
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001927struct req_batch {
1928 void *reqs[IO_IOPOLL_BATCH];
1929 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001930
1931 struct task_struct *task;
1932 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001933};
1934
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001935static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001936{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001937 rb->to_free = 0;
1938 rb->task_refs = 0;
1939 rb->task = NULL;
1940}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001941
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001942static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1943 struct req_batch *rb)
1944{
1945 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1946 percpu_ref_put_many(&ctx->refs, rb->to_free);
1947 rb->to_free = 0;
1948}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001949
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001950static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1951 struct req_batch *rb)
1952{
1953 if (rb->to_free)
1954 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001955 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001956 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001957 put_task_struct_many(rb->task, rb->task_refs);
1958 rb->task = NULL;
1959 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001960}
1961
1962static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1963{
1964 if (unlikely(io_is_fallback_req(req))) {
1965 io_free_req(req);
1966 return;
1967 }
1968 if (req->flags & REQ_F_LINK_HEAD)
1969 io_queue_next(req);
1970
Jens Axboee3bc8e92020-09-24 08:45:57 -06001971 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001972 if (rb->task) {
1973 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001974 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06001975 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001976 rb->task = req->task;
1977 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001978 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001979 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001980
Jens Axboe51a4cc12020-08-10 10:55:56 -06001981 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001982 rb->reqs[rb->to_free++] = req;
1983 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1984 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001985}
1986
Jens Axboeba816ad2019-09-28 11:36:45 -06001987/*
1988 * Drop reference to request, return next in chain (if there is one) if this
1989 * was the last reference to this request.
1990 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001991static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001992{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001993 struct io_kiocb *nxt = NULL;
1994
Jens Axboe2a44f462020-02-25 13:25:41 -07001995 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001996 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001997 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001998 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001999 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002000}
2001
Jens Axboe2b188cc2019-01-07 10:46:33 -07002002static void io_put_req(struct io_kiocb *req)
2003{
Jens Axboedef596e2019-01-09 08:59:42 -07002004 if (refcount_dec_and_test(&req->refs))
2005 io_free_req(req);
2006}
2007
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002008static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002009{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002010 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002011
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002012 /*
2013 * A ref is owned by io-wq in which context we're. So, if that's the
2014 * last one, it's safe to steal next work. False negatives are Ok,
2015 * it just will be re-punted async in io_put_work()
2016 */
2017 if (refcount_read(&req->refs) != 1)
2018 return NULL;
2019
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002020 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002021 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002022}
2023
Jens Axboe978db572019-11-14 22:39:04 -07002024/*
2025 * Must only be used if we don't need to care about links, usually from
2026 * within the completion handling itself.
2027 */
2028static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002029{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002030 /* drop both submit and complete references */
2031 if (refcount_sub_and_test(2, &req->refs))
2032 __io_free_req(req);
2033}
2034
Jens Axboe978db572019-11-14 22:39:04 -07002035static void io_double_put_req(struct io_kiocb *req)
2036{
2037 /* drop both submit and complete references */
2038 if (refcount_sub_and_test(2, &req->refs))
2039 io_free_req(req);
2040}
2041
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002042static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002043{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002044 struct io_rings *rings = ctx->rings;
2045
Jens Axboead3eb2c2019-12-18 17:12:20 -07002046 if (test_bit(0, &ctx->cq_check_overflow)) {
2047 /*
2048 * noflush == true is from the waitqueue handler, just ensure
2049 * we wake up the task, and the next invocation will flush the
2050 * entries. We cannot safely to it from here.
2051 */
2052 if (noflush && !list_empty(&ctx->cq_overflow_list))
2053 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002054
Jens Axboee6c8aa92020-09-28 13:10:13 -06002055 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002056 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002057
Jens Axboea3a0e432019-08-20 11:03:11 -06002058 /* See comment at the top of this file */
2059 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002060 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002061}
2062
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002063static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2064{
2065 struct io_rings *rings = ctx->rings;
2066
2067 /* make sure SQ entry isn't read before tail */
2068 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2069}
2070
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002071static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002072{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002073 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002074
Jens Axboebcda7ba2020-02-23 16:42:51 -07002075 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2076 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002077 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002078 kfree(kbuf);
2079 return cflags;
2080}
2081
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002082static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2083{
2084 struct io_buffer *kbuf;
2085
2086 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2087 return io_put_kbuf(req, kbuf);
2088}
2089
Jens Axboe4c6e2772020-07-01 11:29:10 -06002090static inline bool io_run_task_work(void)
2091{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002092 /*
2093 * Not safe to run on exiting task, and the task_work handling will
2094 * not add work to such a task.
2095 */
2096 if (unlikely(current->flags & PF_EXITING))
2097 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002098 if (current->task_works) {
2099 __set_current_state(TASK_RUNNING);
2100 task_work_run();
2101 return true;
2102 }
2103
2104 return false;
2105}
2106
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002107static void io_iopoll_queue(struct list_head *again)
2108{
2109 struct io_kiocb *req;
2110
2111 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002112 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2113 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002114 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002115 } while (!list_empty(again));
2116}
2117
Jens Axboedef596e2019-01-09 08:59:42 -07002118/*
2119 * Find and free completed poll iocbs
2120 */
2121static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2122 struct list_head *done)
2123{
Jens Axboe8237e042019-12-28 10:48:22 -07002124 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002125 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002126 LIST_HEAD(again);
2127
2128 /* order with ->result store in io_complete_rw_iopoll() */
2129 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002130
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002131 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002132 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002133 int cflags = 0;
2134
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002135 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002136 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002137 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002138 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002139 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002140 continue;
2141 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002142 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002143
Jens Axboebcda7ba2020-02-23 16:42:51 -07002144 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002145 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002146
2147 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002148 (*nr_events)++;
2149
Pavel Begunkovc3524382020-06-28 12:52:32 +03002150 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002151 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002152 }
Jens Axboedef596e2019-01-09 08:59:42 -07002153
Jens Axboe09bb8392019-03-13 12:39:28 -06002154 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002155 if (ctx->flags & IORING_SETUP_SQPOLL)
2156 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002157 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002158
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002159 if (!list_empty(&again))
2160 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002161}
2162
Jens Axboedef596e2019-01-09 08:59:42 -07002163static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2164 long min)
2165{
2166 struct io_kiocb *req, *tmp;
2167 LIST_HEAD(done);
2168 bool spin;
2169 int ret;
2170
2171 /*
2172 * Only spin for completions if we don't have multiple devices hanging
2173 * off our complete list, and we're under the requested amount.
2174 */
2175 spin = !ctx->poll_multi_file && *nr_events < min;
2176
2177 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002178 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002179 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002180
2181 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002182 * Move completed and retryable entries to our local lists.
2183 * If we find a request that requires polling, break out
2184 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002185 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002186 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002187 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002188 continue;
2189 }
2190 if (!list_empty(&done))
2191 break;
2192
2193 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2194 if (ret < 0)
2195 break;
2196
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002197 /* iopoll may have completed current req */
2198 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002199 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002200
Jens Axboedef596e2019-01-09 08:59:42 -07002201 if (ret && spin)
2202 spin = false;
2203 ret = 0;
2204 }
2205
2206 if (!list_empty(&done))
2207 io_iopoll_complete(ctx, nr_events, &done);
2208
2209 return ret;
2210}
2211
2212/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002213 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002214 * non-spinning poll check - we'll still enter the driver poll loop, but only
2215 * as a non-spinning completion check.
2216 */
2217static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2218 long min)
2219{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002220 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002221 int ret;
2222
2223 ret = io_do_iopoll(ctx, nr_events, min);
2224 if (ret < 0)
2225 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002226 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002227 return 0;
2228 }
2229
2230 return 1;
2231}
2232
2233/*
2234 * We can't just wait for polled events to come to us, we have to actively
2235 * find and complete them.
2236 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002237static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002238{
2239 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2240 return;
2241
2242 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002243 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002244 unsigned int nr_events = 0;
2245
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002246 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002247
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002248 /* let it sleep and repeat later if can't complete a request */
2249 if (nr_events == 0)
2250 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002251 /*
2252 * Ensure we allow local-to-the-cpu processing to take place,
2253 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002254 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002255 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002256 if (need_resched()) {
2257 mutex_unlock(&ctx->uring_lock);
2258 cond_resched();
2259 mutex_lock(&ctx->uring_lock);
2260 }
Jens Axboedef596e2019-01-09 08:59:42 -07002261 }
2262 mutex_unlock(&ctx->uring_lock);
2263}
2264
Pavel Begunkov7668b922020-07-07 16:36:21 +03002265static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002266{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002267 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002268 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002269
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002270 /*
2271 * We disallow the app entering submit/complete with polling, but we
2272 * still need to lock the ring to prevent racing with polled issue
2273 * that got punted to a workqueue.
2274 */
2275 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002276 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002277 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002278 * Don't enter poll loop if we already have events pending.
2279 * If we do, we can potentially be spinning for commands that
2280 * already triggered a CQE (eg in error).
2281 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002282 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002283 break;
2284
2285 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002286 * If a submit got punted to a workqueue, we can have the
2287 * application entering polling for a command before it gets
2288 * issued. That app will hold the uring_lock for the duration
2289 * of the poll right here, so we need to take a breather every
2290 * now and then to ensure that the issue has a chance to add
2291 * the poll to the issued list. Otherwise we can spin here
2292 * forever, while the workqueue is stuck trying to acquire the
2293 * very same mutex.
2294 */
2295 if (!(++iters & 7)) {
2296 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002297 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002298 mutex_lock(&ctx->uring_lock);
2299 }
2300
Pavel Begunkov7668b922020-07-07 16:36:21 +03002301 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002302 if (ret <= 0)
2303 break;
2304 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002305 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002306
Jens Axboe500f9fb2019-08-19 12:15:59 -06002307 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002308 return ret;
2309}
2310
Jens Axboe491381ce2019-10-17 09:20:46 -06002311static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312{
Jens Axboe491381ce2019-10-17 09:20:46 -06002313 /*
2314 * Tell lockdep we inherited freeze protection from submission
2315 * thread.
2316 */
2317 if (req->flags & REQ_F_ISREG) {
2318 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319
Jens Axboe491381ce2019-10-17 09:20:46 -06002320 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002322 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323}
2324
Jens Axboea1d7c392020-06-22 11:09:46 -06002325static void io_complete_rw_common(struct kiocb *kiocb, long res,
2326 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002327{
Jens Axboe9adbd452019-12-20 08:45:55 -07002328 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002329 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002330
Jens Axboe491381ce2019-10-17 09:20:46 -06002331 if (kiocb->ki_flags & IOCB_WRITE)
2332 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002334 if (res != req->result)
2335 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002336 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002337 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002338 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002339}
2340
Jens Axboeb63534c2020-06-04 11:28:00 -06002341#ifdef CONFIG_BLOCK
2342static bool io_resubmit_prep(struct io_kiocb *req, int error)
2343{
2344 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2345 ssize_t ret = -ECANCELED;
2346 struct iov_iter iter;
2347 int rw;
2348
2349 if (error) {
2350 ret = error;
2351 goto end_req;
2352 }
2353
2354 switch (req->opcode) {
2355 case IORING_OP_READV:
2356 case IORING_OP_READ_FIXED:
2357 case IORING_OP_READ:
2358 rw = READ;
2359 break;
2360 case IORING_OP_WRITEV:
2361 case IORING_OP_WRITE_FIXED:
2362 case IORING_OP_WRITE:
2363 rw = WRITE;
2364 break;
2365 default:
2366 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2367 req->opcode);
2368 goto end_req;
2369 }
2370
Jens Axboe8f3d7492020-09-14 09:28:14 -06002371 if (!req->io) {
2372 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2373 if (ret < 0)
2374 goto end_req;
2375 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2376 if (!ret)
2377 return true;
2378 kfree(iovec);
2379 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002380 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002381 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002382end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002383 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002384 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002385 return false;
2386}
Jens Axboeb63534c2020-06-04 11:28:00 -06002387#endif
2388
2389static bool io_rw_reissue(struct io_kiocb *req, long res)
2390{
2391#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002392 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002393 int ret;
2394
Jens Axboe355afae2020-09-02 09:30:31 -06002395 if (!S_ISBLK(mode) && !S_ISREG(mode))
2396 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002397 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2398 return false;
2399
Jens Axboefdee9462020-08-27 16:46:24 -06002400 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002401
Jens Axboefdee9462020-08-27 16:46:24 -06002402 if (io_resubmit_prep(req, ret)) {
2403 refcount_inc(&req->refs);
2404 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002405 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002406 }
2407
Jens Axboeb63534c2020-06-04 11:28:00 -06002408#endif
2409 return false;
2410}
2411
Jens Axboea1d7c392020-06-22 11:09:46 -06002412static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2413 struct io_comp_state *cs)
2414{
2415 if (!io_rw_reissue(req, res))
2416 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002417}
2418
2419static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2420{
Jens Axboe9adbd452019-12-20 08:45:55 -07002421 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002422
Jens Axboea1d7c392020-06-22 11:09:46 -06002423 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002424}
2425
Jens Axboedef596e2019-01-09 08:59:42 -07002426static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2427{
Jens Axboe9adbd452019-12-20 08:45:55 -07002428 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002429
Jens Axboe491381ce2019-10-17 09:20:46 -06002430 if (kiocb->ki_flags & IOCB_WRITE)
2431 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002432
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002433 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002434 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002435
2436 WRITE_ONCE(req->result, res);
2437 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002438 smp_wmb();
2439 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002440}
2441
2442/*
2443 * After the iocb has been issued, it's safe to be found on the poll list.
2444 * Adding the kiocb to the list AFTER submission ensures that we don't
2445 * find it from a io_iopoll_getevents() thread before the issuer is done
2446 * accessing the kiocb cookie.
2447 */
2448static void io_iopoll_req_issued(struct io_kiocb *req)
2449{
2450 struct io_ring_ctx *ctx = req->ctx;
2451
2452 /*
2453 * Track whether we have multiple files in our lists. This will impact
2454 * how we do polling eventually, not spinning if we're on potentially
2455 * different devices.
2456 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002457 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002458 ctx->poll_multi_file = false;
2459 } else if (!ctx->poll_multi_file) {
2460 struct io_kiocb *list_req;
2461
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002462 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002463 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002464 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002465 ctx->poll_multi_file = true;
2466 }
2467
2468 /*
2469 * For fast devices, IO may have already completed. If it has, add
2470 * it to the front so we find it first.
2471 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002472 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002473 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002474 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002475 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002476
Jens Axboe534ca6d2020-09-02 13:52:19 -06002477 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2478 wq_has_sleeper(&ctx->sq_data->wait))
2479 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002480}
2481
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002482static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002483{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002484 if (state->has_refs)
2485 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002486 state->file = NULL;
2487}
2488
2489static inline void io_state_file_put(struct io_submit_state *state)
2490{
2491 if (state->file)
2492 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002493}
2494
2495/*
2496 * Get as many references to a file as we have IOs left in this submission,
2497 * assuming most submissions are for one file, or at least that each file
2498 * has more than one submission.
2499 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002500static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002501{
2502 if (!state)
2503 return fget(fd);
2504
2505 if (state->file) {
2506 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002507 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002508 state->ios_left--;
2509 return state->file;
2510 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002511 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002512 }
2513 state->file = fget_many(fd, state->ios_left);
2514 if (!state->file)
2515 return NULL;
2516
2517 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002518 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002519 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002520 return state->file;
2521}
2522
Jens Axboe4503b762020-06-01 10:00:27 -06002523static bool io_bdev_nowait(struct block_device *bdev)
2524{
2525#ifdef CONFIG_BLOCK
2526 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2527#else
2528 return true;
2529#endif
2530}
2531
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532/*
2533 * If we tracked the file through the SCM inflight mechanism, we could support
2534 * any file. For now, just ensure that anything potentially problematic is done
2535 * inline.
2536 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002537static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002538{
2539 umode_t mode = file_inode(file)->i_mode;
2540
Jens Axboe4503b762020-06-01 10:00:27 -06002541 if (S_ISBLK(mode)) {
2542 if (io_bdev_nowait(file->f_inode->i_bdev))
2543 return true;
2544 return false;
2545 }
2546 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002548 if (S_ISREG(mode)) {
2549 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2550 file->f_op != &io_uring_fops)
2551 return true;
2552 return false;
2553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554
Jens Axboec5b85622020-06-09 19:23:05 -06002555 /* any ->read/write should understand O_NONBLOCK */
2556 if (file->f_flags & O_NONBLOCK)
2557 return true;
2558
Jens Axboeaf197f52020-04-28 13:15:06 -06002559 if (!(file->f_mode & FMODE_NOWAIT))
2560 return false;
2561
2562 if (rw == READ)
2563 return file->f_op->read_iter != NULL;
2564
2565 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002566}
2567
Jens Axboe3529d8c2019-12-19 18:24:38 -07002568static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2569 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002570{
Jens Axboedef596e2019-01-09 08:59:42 -07002571 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002572 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002573 unsigned ioprio;
2574 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575
Jens Axboe491381ce2019-10-17 09:20:46 -06002576 if (S_ISREG(file_inode(req->file)->i_mode))
2577 req->flags |= REQ_F_ISREG;
2578
Jens Axboe2b188cc2019-01-07 10:46:33 -07002579 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002580 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2581 req->flags |= REQ_F_CUR_POS;
2582 kiocb->ki_pos = req->file->f_pos;
2583 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002585 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2586 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2587 if (unlikely(ret))
2588 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002589
2590 ioprio = READ_ONCE(sqe->ioprio);
2591 if (ioprio) {
2592 ret = ioprio_check_cap(ioprio);
2593 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002594 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002595
2596 kiocb->ki_ioprio = ioprio;
2597 } else
2598 kiocb->ki_ioprio = get_current_ioprio();
2599
Stefan Bühler8449eed2019-04-27 20:34:19 +02002600 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002601 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002602 req->flags |= REQ_F_NOWAIT;
2603
2604 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002606
Jens Axboedef596e2019-01-09 08:59:42 -07002607 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002608 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2609 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002610 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002611
Jens Axboedef596e2019-01-09 08:59:42 -07002612 kiocb->ki_flags |= IOCB_HIPRI;
2613 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002614 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002615 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002616 if (kiocb->ki_flags & IOCB_HIPRI)
2617 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002618 kiocb->ki_complete = io_complete_rw;
2619 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002620
Jens Axboe3529d8c2019-12-19 18:24:38 -07002621 req->rw.addr = READ_ONCE(sqe->addr);
2622 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002623 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002624 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625}
2626
2627static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2628{
2629 switch (ret) {
2630 case -EIOCBQUEUED:
2631 break;
2632 case -ERESTARTSYS:
2633 case -ERESTARTNOINTR:
2634 case -ERESTARTNOHAND:
2635 case -ERESTART_RESTARTBLOCK:
2636 /*
2637 * We can't just restart the syscall, since previously
2638 * submitted sqes may already be in progress. Just fail this
2639 * IO with EINTR.
2640 */
2641 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002642 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643 default:
2644 kiocb->ki_complete(kiocb, ret, 0);
2645 }
2646}
2647
Jens Axboea1d7c392020-06-22 11:09:46 -06002648static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2649 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002650{
Jens Axboeba042912019-12-25 16:33:42 -07002651 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2652
Jens Axboe227c0c92020-08-13 11:51:40 -06002653 /* add previously done IO, if any */
2654 if (req->io && req->io->rw.bytes_done > 0) {
2655 if (ret < 0)
2656 ret = req->io->rw.bytes_done;
2657 else
2658 ret += req->io->rw.bytes_done;
2659 }
2660
Jens Axboeba042912019-12-25 16:33:42 -07002661 if (req->flags & REQ_F_CUR_POS)
2662 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002663 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002664 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002665 else
2666 io_rw_done(kiocb, ret);
2667}
2668
Jens Axboe9adbd452019-12-20 08:45:55 -07002669static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002670 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002671{
Jens Axboe9adbd452019-12-20 08:45:55 -07002672 struct io_ring_ctx *ctx = req->ctx;
2673 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002674 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002675 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002676 size_t offset;
2677 u64 buf_addr;
2678
Jens Axboeedafcce2019-01-09 09:16:05 -07002679 if (unlikely(buf_index >= ctx->nr_user_bufs))
2680 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002681 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2682 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002683 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002684
2685 /* overflow */
2686 if (buf_addr + len < buf_addr)
2687 return -EFAULT;
2688 /* not inside the mapped region */
2689 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2690 return -EFAULT;
2691
2692 /*
2693 * May not be a start of buffer, set size appropriately
2694 * and advance us to the beginning.
2695 */
2696 offset = buf_addr - imu->ubuf;
2697 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002698
2699 if (offset) {
2700 /*
2701 * Don't use iov_iter_advance() here, as it's really slow for
2702 * using the latter parts of a big fixed buffer - it iterates
2703 * over each segment manually. We can cheat a bit here, because
2704 * we know that:
2705 *
2706 * 1) it's a BVEC iter, we set it up
2707 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2708 * first and last bvec
2709 *
2710 * So just find our index, and adjust the iterator afterwards.
2711 * If the offset is within the first bvec (or the whole first
2712 * bvec, just use iov_iter_advance(). This makes it easier
2713 * since we can just skip the first segment, which may not
2714 * be PAGE_SIZE aligned.
2715 */
2716 const struct bio_vec *bvec = imu->bvec;
2717
2718 if (offset <= bvec->bv_len) {
2719 iov_iter_advance(iter, offset);
2720 } else {
2721 unsigned long seg_skip;
2722
2723 /* skip first vec */
2724 offset -= bvec->bv_len;
2725 seg_skip = 1 + (offset >> PAGE_SHIFT);
2726
2727 iter->bvec = bvec + seg_skip;
2728 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002729 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002730 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002731 }
2732 }
2733
Jens Axboe5e559562019-11-13 16:12:46 -07002734 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002735}
2736
Jens Axboebcda7ba2020-02-23 16:42:51 -07002737static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2738{
2739 if (needs_lock)
2740 mutex_unlock(&ctx->uring_lock);
2741}
2742
2743static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2744{
2745 /*
2746 * "Normal" inline submissions always hold the uring_lock, since we
2747 * grab it from the system call. Same is true for the SQPOLL offload.
2748 * The only exception is when we've detached the request and issue it
2749 * from an async worker thread, grab the lock for that case.
2750 */
2751 if (needs_lock)
2752 mutex_lock(&ctx->uring_lock);
2753}
2754
2755static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2756 int bgid, struct io_buffer *kbuf,
2757 bool needs_lock)
2758{
2759 struct io_buffer *head;
2760
2761 if (req->flags & REQ_F_BUFFER_SELECTED)
2762 return kbuf;
2763
2764 io_ring_submit_lock(req->ctx, needs_lock);
2765
2766 lockdep_assert_held(&req->ctx->uring_lock);
2767
2768 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2769 if (head) {
2770 if (!list_empty(&head->list)) {
2771 kbuf = list_last_entry(&head->list, struct io_buffer,
2772 list);
2773 list_del(&kbuf->list);
2774 } else {
2775 kbuf = head;
2776 idr_remove(&req->ctx->io_buffer_idr, bgid);
2777 }
2778 if (*len > kbuf->len)
2779 *len = kbuf->len;
2780 } else {
2781 kbuf = ERR_PTR(-ENOBUFS);
2782 }
2783
2784 io_ring_submit_unlock(req->ctx, needs_lock);
2785
2786 return kbuf;
2787}
2788
Jens Axboe4d954c22020-02-27 07:31:19 -07002789static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2790 bool needs_lock)
2791{
2792 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002793 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002794
2795 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002796 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002797 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2798 if (IS_ERR(kbuf))
2799 return kbuf;
2800 req->rw.addr = (u64) (unsigned long) kbuf;
2801 req->flags |= REQ_F_BUFFER_SELECTED;
2802 return u64_to_user_ptr(kbuf->addr);
2803}
2804
2805#ifdef CONFIG_COMPAT
2806static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2807 bool needs_lock)
2808{
2809 struct compat_iovec __user *uiov;
2810 compat_ssize_t clen;
2811 void __user *buf;
2812 ssize_t len;
2813
2814 uiov = u64_to_user_ptr(req->rw.addr);
2815 if (!access_ok(uiov, sizeof(*uiov)))
2816 return -EFAULT;
2817 if (__get_user(clen, &uiov->iov_len))
2818 return -EFAULT;
2819 if (clen < 0)
2820 return -EINVAL;
2821
2822 len = clen;
2823 buf = io_rw_buffer_select(req, &len, needs_lock);
2824 if (IS_ERR(buf))
2825 return PTR_ERR(buf);
2826 iov[0].iov_base = buf;
2827 iov[0].iov_len = (compat_size_t) len;
2828 return 0;
2829}
2830#endif
2831
2832static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2833 bool needs_lock)
2834{
2835 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2836 void __user *buf;
2837 ssize_t len;
2838
2839 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2840 return -EFAULT;
2841
2842 len = iov[0].iov_len;
2843 if (len < 0)
2844 return -EINVAL;
2845 buf = io_rw_buffer_select(req, &len, needs_lock);
2846 if (IS_ERR(buf))
2847 return PTR_ERR(buf);
2848 iov[0].iov_base = buf;
2849 iov[0].iov_len = len;
2850 return 0;
2851}
2852
2853static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2854 bool needs_lock)
2855{
Jens Axboedddb3e22020-06-04 11:27:01 -06002856 if (req->flags & REQ_F_BUFFER_SELECTED) {
2857 struct io_buffer *kbuf;
2858
2859 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2860 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2861 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002862 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002863 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002864 if (!req->rw.len)
2865 return 0;
2866 else if (req->rw.len > 1)
2867 return -EINVAL;
2868
2869#ifdef CONFIG_COMPAT
2870 if (req->ctx->compat)
2871 return io_compat_import(req, iov, needs_lock);
2872#endif
2873
2874 return __io_iov_buffer_select(req, iov, needs_lock);
2875}
2876
Jens Axboe8452fd02020-08-18 13:58:33 -07002877static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2878 struct iovec **iovec, struct iov_iter *iter,
2879 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002880{
Jens Axboe9adbd452019-12-20 08:45:55 -07002881 void __user *buf = u64_to_user_ptr(req->rw.addr);
2882 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002883 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002884 u8 opcode;
2885
Jens Axboed625c6e2019-12-17 19:53:05 -07002886 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002887 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002888 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002889 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002890 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002891
Jens Axboebcda7ba2020-02-23 16:42:51 -07002892 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002893 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002894 return -EINVAL;
2895
Jens Axboe3a6820f2019-12-22 15:19:35 -07002896 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002897 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002898 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002899 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002900 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002901 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002902 }
2903
Jens Axboe3a6820f2019-12-22 15:19:35 -07002904 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2905 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002906 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002907 }
2908
Jens Axboe4d954c22020-02-27 07:31:19 -07002909 if (req->flags & REQ_F_BUFFER_SELECT) {
2910 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002911 if (!ret) {
2912 ret = (*iovec)->iov_len;
2913 iov_iter_init(iter, rw, *iovec, 1, ret);
2914 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002915 *iovec = NULL;
2916 return ret;
2917 }
2918
Jens Axboe2b188cc2019-01-07 10:46:33 -07002919#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002920 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2922 iovec, iter);
2923#endif
2924
2925 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2926}
2927
Jens Axboe8452fd02020-08-18 13:58:33 -07002928static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2929 struct iovec **iovec, struct iov_iter *iter,
2930 bool needs_lock)
2931{
2932 if (!req->io)
2933 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2934 *iovec = NULL;
2935 return iov_iter_count(&req->io->rw.iter);
2936}
2937
Jens Axboe0fef9482020-08-26 10:36:20 -06002938static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2939{
2940 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2941}
2942
Jens Axboe32960612019-09-23 11:05:34 -06002943/*
2944 * For files that don't have ->read_iter() and ->write_iter(), handle them
2945 * by looping over ->read() or ->write() manually.
2946 */
2947static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2948 struct iov_iter *iter)
2949{
2950 ssize_t ret = 0;
2951
2952 /*
2953 * Don't support polled IO through this interface, and we can't
2954 * support non-blocking either. For the latter, this just causes
2955 * the kiocb to be handled from an async context.
2956 */
2957 if (kiocb->ki_flags & IOCB_HIPRI)
2958 return -EOPNOTSUPP;
2959 if (kiocb->ki_flags & IOCB_NOWAIT)
2960 return -EAGAIN;
2961
2962 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002963 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002964 ssize_t nr;
2965
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002966 if (!iov_iter_is_bvec(iter)) {
2967 iovec = iov_iter_iovec(iter);
2968 } else {
2969 /* fixed buffers import bvec */
2970 iovec.iov_base = kmap(iter->bvec->bv_page)
2971 + iter->iov_offset;
2972 iovec.iov_len = min(iter->count,
2973 iter->bvec->bv_len - iter->iov_offset);
2974 }
2975
Jens Axboe32960612019-09-23 11:05:34 -06002976 if (rw == READ) {
2977 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002978 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002979 } else {
2980 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002981 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002982 }
2983
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002984 if (iov_iter_is_bvec(iter))
2985 kunmap(iter->bvec->bv_page);
2986
Jens Axboe32960612019-09-23 11:05:34 -06002987 if (nr < 0) {
2988 if (!ret)
2989 ret = nr;
2990 break;
2991 }
2992 ret += nr;
2993 if (nr != iovec.iov_len)
2994 break;
2995 iov_iter_advance(iter, nr);
2996 }
2997
2998 return ret;
2999}
3000
Jens Axboeff6165b2020-08-13 09:47:43 -06003001static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3002 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003003{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003004 struct io_async_rw *rw = &req->io->rw;
3005
Jens Axboeff6165b2020-08-13 09:47:43 -06003006 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003007 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003008 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003009 /* can only be fixed buffers, no need to do anything */
3010 if (iter->type == ITER_BVEC)
3011 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003012 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003013 unsigned iov_off = 0;
3014
3015 rw->iter.iov = rw->fast_iov;
3016 if (iter->iov != fast_iov) {
3017 iov_off = iter->iov - fast_iov;
3018 rw->iter.iov += iov_off;
3019 }
3020 if (rw->fast_iov != fast_iov)
3021 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003022 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003023 } else {
3024 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003025 }
3026}
3027
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003028static inline int __io_alloc_async_ctx(struct io_kiocb *req)
3029{
3030 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
3031 return req->io == NULL;
3032}
3033
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003034static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003035{
Jens Axboed3656342019-12-18 09:50:26 -07003036 if (!io_op_defs[req->opcode].async_ctx)
3037 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003038
3039 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003040}
3041
Jens Axboeff6165b2020-08-13 09:47:43 -06003042static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3043 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003044 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003045{
Jens Axboe227c0c92020-08-13 11:51:40 -06003046 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003047 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003048 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003049 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003050 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003051
Jens Axboeff6165b2020-08-13 09:47:43 -06003052 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003053 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003054 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003055}
3056
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003057static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3058 bool force_nonblock)
3059{
Jens Axboeff6165b2020-08-13 09:47:43 -06003060 struct io_async_rw *iorw = &req->io->rw;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003061 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003062 ssize_t ret;
3063
Jens Axboec183edf2020-09-04 22:36:52 -06003064 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003065 if (unlikely(ret < 0))
3066 return ret;
3067
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003068 iorw->bytes_done = 0;
3069 iorw->free_iovec = iov;
3070 if (iov)
3071 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003072 return 0;
3073}
3074
Jens Axboe3529d8c2019-12-19 18:24:38 -07003075static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3076 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003077{
3078 ssize_t ret;
3079
Jens Axboe3529d8c2019-12-19 18:24:38 -07003080 ret = io_prep_rw(req, sqe, force_nonblock);
3081 if (ret)
3082 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003083
Jens Axboe3529d8c2019-12-19 18:24:38 -07003084 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3085 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003086
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003087 /* either don't need iovec imported or already have it */
3088 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003089 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003090 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003091}
3092
Jens Axboec1dd91d2020-08-03 16:43:59 -06003093/*
3094 * This is our waitqueue callback handler, registered through lock_page_async()
3095 * when we initially tried to do the IO with the iocb armed our waitqueue.
3096 * This gets called when the page is unlocked, and we generally expect that to
3097 * happen when the page IO is completed and the page is now uptodate. This will
3098 * queue a task_work based retry of the operation, attempting to copy the data
3099 * again. If the latter fails because the page was NOT uptodate, then we will
3100 * do a thread based blocking retry of the operation. That's the unexpected
3101 * slow path.
3102 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003103static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3104 int sync, void *arg)
3105{
3106 struct wait_page_queue *wpq;
3107 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003108 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003109 int ret;
3110
3111 wpq = container_of(wait, struct wait_page_queue, wait);
3112
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003113 if (!wake_page_match(wpq, key))
3114 return 0;
3115
Hao Xuc8d317a2020-09-29 20:00:45 +08003116 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003117 list_del_init(&wait->entry);
3118
Pavel Begunkove7375122020-07-12 20:42:04 +03003119 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003120 percpu_ref_get(&req->ctx->refs);
3121
Jens Axboebcf5a062020-05-22 09:24:42 -06003122 /* submit ref gets dropped, acquire a new one */
3123 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003124 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003125 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003126 struct task_struct *tsk;
3127
Jens Axboebcf5a062020-05-22 09:24:42 -06003128 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003129 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003130 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003131 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003132 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003133 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003134 return 1;
3135}
3136
Jens Axboec1dd91d2020-08-03 16:43:59 -06003137/*
3138 * This controls whether a given IO request should be armed for async page
3139 * based retry. If we return false here, the request is handed to the async
3140 * worker threads for retry. If we're doing buffered reads on a regular file,
3141 * we prepare a private wait_page_queue entry and retry the operation. This
3142 * will either succeed because the page is now uptodate and unlocked, or it
3143 * will register a callback when the page is unlocked at IO completion. Through
3144 * that callback, io_uring uses task_work to setup a retry of the operation.
3145 * That retry will attempt the buffered read again. The retry will generally
3146 * succeed, or in rare cases where it fails, we then fall back to using the
3147 * async worker threads for a blocking retry.
3148 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003149static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003150{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003151 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003152 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003153
3154 /* never retry for NOWAIT, we just complete with -EAGAIN */
3155 if (req->flags & REQ_F_NOWAIT)
3156 return false;
3157
Jens Axboe227c0c92020-08-13 11:51:40 -06003158 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003159 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003160 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003161
Jens Axboebcf5a062020-05-22 09:24:42 -06003162 /*
3163 * just use poll if we can, and don't attempt if the fs doesn't
3164 * support callback based unlocks
3165 */
3166 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3167 return false;
3168
Jens Axboe3b2a4432020-08-16 10:58:43 -07003169 wait->wait.func = io_async_buf_func;
3170 wait->wait.private = req;
3171 wait->wait.flags = 0;
3172 INIT_LIST_HEAD(&wait->wait.entry);
3173 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003174 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003175 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003176 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003177}
3178
3179static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3180{
3181 if (req->file->f_op->read_iter)
3182 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003183 else if (req->file->f_op->read)
3184 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3185 else
3186 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003187}
3188
Jens Axboea1d7c392020-06-22 11:09:46 -06003189static int io_read(struct io_kiocb *req, bool force_nonblock,
3190 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003191{
3192 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003193 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003194 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003195 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003196 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003197 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003198
Jens Axboeff6165b2020-08-13 09:47:43 -06003199 if (req->io)
3200 iter = &req->io->rw.iter;
3201
3202 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003203 if (ret < 0)
3204 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003205 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003206 io_size = ret;
3207 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003208 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003209
Jens Axboefd6c2e42019-12-18 12:19:41 -07003210 /* Ensure we clear previously set non-block flag */
3211 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003212 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003213
Pavel Begunkov24c74672020-06-21 13:09:51 +03003214 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003215 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3216 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003217 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003218
Jens Axboe0fef9482020-08-26 10:36:20 -06003219 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003220 if (unlikely(ret))
3221 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003222
Jens Axboe227c0c92020-08-13 11:51:40 -06003223 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003224
Jens Axboe227c0c92020-08-13 11:51:40 -06003225 if (!ret) {
3226 goto done;
3227 } else if (ret == -EIOCBQUEUED) {
3228 ret = 0;
3229 goto out_free;
3230 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003231 /* IOPOLL retry should happen for io-wq threads */
3232 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003233 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003234 /* no retry on NONBLOCK marked file */
3235 if (req->file->f_flags & O_NONBLOCK)
3236 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003237 /* some cases will consume bytes even on error returns */
3238 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003239 ret = 0;
3240 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003241 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003242 /* make sure -ERESTARTSYS -> -EINTR is done */
3243 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003244 }
3245
3246 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003247 if (!iov_iter_count(iter) || !force_nonblock ||
3248 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003249 goto done;
3250
3251 io_size -= ret;
3252copy_iov:
3253 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3254 if (ret2) {
3255 ret = ret2;
3256 goto out_free;
3257 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003258 if (no_async)
3259 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003260 /* it's copied and will be cleaned with ->io */
3261 iovec = NULL;
3262 /* now use our persistent iterator, if we aren't already */
3263 iter = &req->io->rw.iter;
3264retry:
3265 req->io->rw.bytes_done += ret;
3266 /* if we can retry, do so with the callbacks armed */
3267 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003268 kiocb->ki_flags &= ~IOCB_WAITQ;
3269 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003270 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003271
3272 /*
3273 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3274 * get -EIOCBQUEUED, then we'll get a notification when the desired
3275 * page gets unlocked. We can also get a partial read here, and if we
3276 * do, then just retry at the new offset.
3277 */
3278 ret = io_iter_do_read(req, iter);
3279 if (ret == -EIOCBQUEUED) {
3280 ret = 0;
3281 goto out_free;
3282 } else if (ret > 0 && ret < io_size) {
3283 /* we got some bytes, but not all. retry. */
3284 goto retry;
3285 }
3286done:
3287 kiocb_done(kiocb, ret, cs);
3288 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003289out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003290 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003291 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003292 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003293 return ret;
3294}
3295
Jens Axboe3529d8c2019-12-19 18:24:38 -07003296static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3297 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003298{
3299 ssize_t ret;
3300
Jens Axboe3529d8c2019-12-19 18:24:38 -07003301 ret = io_prep_rw(req, sqe, force_nonblock);
3302 if (ret)
3303 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003304
Jens Axboe3529d8c2019-12-19 18:24:38 -07003305 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3306 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003307
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003308 /* either don't need iovec imported or already have it */
3309 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003310 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003311 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003312}
3313
Jens Axboea1d7c392020-06-22 11:09:46 -06003314static int io_write(struct io_kiocb *req, bool force_nonblock,
3315 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003316{
3317 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003318 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003319 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003320 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003321 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003322
Jens Axboeff6165b2020-08-13 09:47:43 -06003323 if (req->io)
3324 iter = &req->io->rw.iter;
3325
3326 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003327 if (ret < 0)
3328 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003329 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003330 io_size = ret;
3331 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003332
Jens Axboefd6c2e42019-12-18 12:19:41 -07003333 /* Ensure we clear previously set non-block flag */
3334 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003335 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003336
Pavel Begunkov24c74672020-06-21 13:09:51 +03003337 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003338 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003339 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003340
Jens Axboe10d59342019-12-09 20:16:22 -07003341 /* file path doesn't support NOWAIT for non-direct_IO */
3342 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3343 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003344 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003345
Jens Axboe0fef9482020-08-26 10:36:20 -06003346 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003347 if (unlikely(ret))
3348 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003349
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003350 /*
3351 * Open-code file_start_write here to grab freeze protection,
3352 * which will be released by another thread in
3353 * io_complete_rw(). Fool lockdep by telling it the lock got
3354 * released so that it doesn't complain about the held lock when
3355 * we return to userspace.
3356 */
3357 if (req->flags & REQ_F_ISREG) {
3358 __sb_start_write(file_inode(req->file)->i_sb,
3359 SB_FREEZE_WRITE, true);
3360 __sb_writers_release(file_inode(req->file)->i_sb,
3361 SB_FREEZE_WRITE);
3362 }
3363 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003364
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003365 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003366 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003367 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003368 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003369 else
3370 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003371
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003372 /*
3373 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3374 * retry them without IOCB_NOWAIT.
3375 */
3376 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3377 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003378 /* no retry on NONBLOCK marked file */
3379 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3380 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003381 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003382 /* IOPOLL retry should happen for io-wq threads */
3383 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3384 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003385done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003386 kiocb_done(kiocb, ret2, cs);
3387 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003388copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003389 /* some cases will consume bytes even on error returns */
3390 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003391 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003392 if (!ret)
3393 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003394 }
Jens Axboe31b51512019-01-18 22:56:34 -07003395out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003396 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003397 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003398 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003399 return ret;
3400}
3401
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003402static int __io_splice_prep(struct io_kiocb *req,
3403 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003404{
3405 struct io_splice* sp = &req->splice;
3406 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3407 int ret;
3408
3409 if (req->flags & REQ_F_NEED_CLEANUP)
3410 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003411 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3412 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003413
3414 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003415 sp->len = READ_ONCE(sqe->len);
3416 sp->flags = READ_ONCE(sqe->splice_flags);
3417
3418 if (unlikely(sp->flags & ~valid_flags))
3419 return -EINVAL;
3420
3421 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3422 (sp->flags & SPLICE_F_FD_IN_FIXED));
3423 if (ret)
3424 return ret;
3425 req->flags |= REQ_F_NEED_CLEANUP;
3426
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003427 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3428 /*
3429 * Splice operation will be punted aync, and here need to
3430 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3431 */
3432 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003433 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003434 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003435
3436 return 0;
3437}
3438
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003439static int io_tee_prep(struct io_kiocb *req,
3440 const struct io_uring_sqe *sqe)
3441{
3442 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3443 return -EINVAL;
3444 return __io_splice_prep(req, sqe);
3445}
3446
3447static int io_tee(struct io_kiocb *req, bool force_nonblock)
3448{
3449 struct io_splice *sp = &req->splice;
3450 struct file *in = sp->file_in;
3451 struct file *out = sp->file_out;
3452 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3453 long ret = 0;
3454
3455 if (force_nonblock)
3456 return -EAGAIN;
3457 if (sp->len)
3458 ret = do_tee(in, out, sp->len, flags);
3459
3460 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3461 req->flags &= ~REQ_F_NEED_CLEANUP;
3462
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003463 if (ret != sp->len)
3464 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003465 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003466 return 0;
3467}
3468
3469static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3470{
3471 struct io_splice* sp = &req->splice;
3472
3473 sp->off_in = READ_ONCE(sqe->splice_off_in);
3474 sp->off_out = READ_ONCE(sqe->off);
3475 return __io_splice_prep(req, sqe);
3476}
3477
Pavel Begunkov014db002020-03-03 21:33:12 +03003478static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003479{
3480 struct io_splice *sp = &req->splice;
3481 struct file *in = sp->file_in;
3482 struct file *out = sp->file_out;
3483 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3484 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003485 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003486
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003487 if (force_nonblock)
3488 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003489
3490 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3491 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003492
Jens Axboe948a7742020-05-17 14:21:38 -06003493 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003494 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003495
3496 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3497 req->flags &= ~REQ_F_NEED_CLEANUP;
3498
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003499 if (ret != sp->len)
3500 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003501 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003502 return 0;
3503}
3504
Jens Axboe2b188cc2019-01-07 10:46:33 -07003505/*
3506 * IORING_OP_NOP just posts a completion event, nothing else.
3507 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003508static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003509{
3510 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003511
Jens Axboedef596e2019-01-09 08:59:42 -07003512 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3513 return -EINVAL;
3514
Jens Axboe229a7b62020-06-22 10:13:11 -06003515 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003516 return 0;
3517}
3518
Jens Axboe3529d8c2019-12-19 18:24:38 -07003519static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003520{
Jens Axboe6b063142019-01-10 22:13:58 -07003521 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003522
Jens Axboe09bb8392019-03-13 12:39:28 -06003523 if (!req->file)
3524 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003525
Jens Axboe6b063142019-01-10 22:13:58 -07003526 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003527 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003528 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003529 return -EINVAL;
3530
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003531 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3532 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3533 return -EINVAL;
3534
3535 req->sync.off = READ_ONCE(sqe->off);
3536 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003537 return 0;
3538}
3539
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003540static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003541{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003542 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003543 int ret;
3544
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003545 /* fsync always requires a blocking context */
3546 if (force_nonblock)
3547 return -EAGAIN;
3548
Jens Axboe9adbd452019-12-20 08:45:55 -07003549 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003550 end > 0 ? end : LLONG_MAX,
3551 req->sync.flags & IORING_FSYNC_DATASYNC);
3552 if (ret < 0)
3553 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003554 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003555 return 0;
3556}
3557
Jens Axboed63d1b52019-12-10 10:38:56 -07003558static int io_fallocate_prep(struct io_kiocb *req,
3559 const struct io_uring_sqe *sqe)
3560{
3561 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3562 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003563 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3564 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003565
3566 req->sync.off = READ_ONCE(sqe->off);
3567 req->sync.len = READ_ONCE(sqe->addr);
3568 req->sync.mode = READ_ONCE(sqe->len);
3569 return 0;
3570}
3571
Pavel Begunkov014db002020-03-03 21:33:12 +03003572static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003573{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003574 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003575
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003576 /* fallocate always requiring blocking context */
3577 if (force_nonblock)
3578 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003579 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3580 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003581 if (ret < 0)
3582 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003583 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003584 return 0;
3585}
3586
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003587static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003588{
Jens Axboef8748882020-01-08 17:47:02 -07003589 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003590 int ret;
3591
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003592 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003593 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003594 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003595 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003596
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003597 /* open.how should be already initialised */
3598 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003599 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003600
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003601 req->open.dfd = READ_ONCE(sqe->fd);
3602 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003603 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003604 if (IS_ERR(req->open.filename)) {
3605 ret = PTR_ERR(req->open.filename);
3606 req->open.filename = NULL;
3607 return ret;
3608 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003609 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003610 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003611 return 0;
3612}
3613
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003614static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3615{
3616 u64 flags, mode;
3617
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003618 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3619 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003620 if (req->flags & REQ_F_NEED_CLEANUP)
3621 return 0;
3622 mode = READ_ONCE(sqe->len);
3623 flags = READ_ONCE(sqe->open_flags);
3624 req->open.how = build_open_how(flags, mode);
3625 return __io_openat_prep(req, sqe);
3626}
3627
Jens Axboecebdb982020-01-08 17:59:24 -07003628static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3629{
3630 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003631 size_t len;
3632 int ret;
3633
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003634 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3635 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003636 if (req->flags & REQ_F_NEED_CLEANUP)
3637 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003638 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3639 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003640 if (len < OPEN_HOW_SIZE_VER0)
3641 return -EINVAL;
3642
3643 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3644 len);
3645 if (ret)
3646 return ret;
3647
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003648 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003649}
3650
Pavel Begunkov014db002020-03-03 21:33:12 +03003651static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003652{
3653 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003654 struct file *file;
3655 int ret;
3656
Jens Axboef86cd202020-01-29 13:46:44 -07003657 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003658 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003659
Jens Axboecebdb982020-01-08 17:59:24 -07003660 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003661 if (ret)
3662 goto err;
3663
Jens Axboe4022e7a2020-03-19 19:23:18 -06003664 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003665 if (ret < 0)
3666 goto err;
3667
3668 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3669 if (IS_ERR(file)) {
3670 put_unused_fd(ret);
3671 ret = PTR_ERR(file);
3672 } else {
3673 fsnotify_open(file);
3674 fd_install(ret, file);
3675 }
3676err:
3677 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003678 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003679 if (ret < 0)
3680 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003681 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003682 return 0;
3683}
3684
Pavel Begunkov014db002020-03-03 21:33:12 +03003685static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003686{
Pavel Begunkov014db002020-03-03 21:33:12 +03003687 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003688}
3689
Jens Axboe067524e2020-03-02 16:32:28 -07003690static int io_remove_buffers_prep(struct io_kiocb *req,
3691 const struct io_uring_sqe *sqe)
3692{
3693 struct io_provide_buf *p = &req->pbuf;
3694 u64 tmp;
3695
3696 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3697 return -EINVAL;
3698
3699 tmp = READ_ONCE(sqe->fd);
3700 if (!tmp || tmp > USHRT_MAX)
3701 return -EINVAL;
3702
3703 memset(p, 0, sizeof(*p));
3704 p->nbufs = tmp;
3705 p->bgid = READ_ONCE(sqe->buf_group);
3706 return 0;
3707}
3708
3709static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3710 int bgid, unsigned nbufs)
3711{
3712 unsigned i = 0;
3713
3714 /* shouldn't happen */
3715 if (!nbufs)
3716 return 0;
3717
3718 /* the head kbuf is the list itself */
3719 while (!list_empty(&buf->list)) {
3720 struct io_buffer *nxt;
3721
3722 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3723 list_del(&nxt->list);
3724 kfree(nxt);
3725 if (++i == nbufs)
3726 return i;
3727 }
3728 i++;
3729 kfree(buf);
3730 idr_remove(&ctx->io_buffer_idr, bgid);
3731
3732 return i;
3733}
3734
Jens Axboe229a7b62020-06-22 10:13:11 -06003735static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3736 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003737{
3738 struct io_provide_buf *p = &req->pbuf;
3739 struct io_ring_ctx *ctx = req->ctx;
3740 struct io_buffer *head;
3741 int ret = 0;
3742
3743 io_ring_submit_lock(ctx, !force_nonblock);
3744
3745 lockdep_assert_held(&ctx->uring_lock);
3746
3747 ret = -ENOENT;
3748 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3749 if (head)
3750 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3751
3752 io_ring_submit_lock(ctx, !force_nonblock);
3753 if (ret < 0)
3754 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003755 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003756 return 0;
3757}
3758
Jens Axboeddf0322d2020-02-23 16:41:33 -07003759static int io_provide_buffers_prep(struct io_kiocb *req,
3760 const struct io_uring_sqe *sqe)
3761{
3762 struct io_provide_buf *p = &req->pbuf;
3763 u64 tmp;
3764
3765 if (sqe->ioprio || sqe->rw_flags)
3766 return -EINVAL;
3767
3768 tmp = READ_ONCE(sqe->fd);
3769 if (!tmp || tmp > USHRT_MAX)
3770 return -E2BIG;
3771 p->nbufs = tmp;
3772 p->addr = READ_ONCE(sqe->addr);
3773 p->len = READ_ONCE(sqe->len);
3774
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003775 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003776 return -EFAULT;
3777
3778 p->bgid = READ_ONCE(sqe->buf_group);
3779 tmp = READ_ONCE(sqe->off);
3780 if (tmp > USHRT_MAX)
3781 return -E2BIG;
3782 p->bid = tmp;
3783 return 0;
3784}
3785
3786static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3787{
3788 struct io_buffer *buf;
3789 u64 addr = pbuf->addr;
3790 int i, bid = pbuf->bid;
3791
3792 for (i = 0; i < pbuf->nbufs; i++) {
3793 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3794 if (!buf)
3795 break;
3796
3797 buf->addr = addr;
3798 buf->len = pbuf->len;
3799 buf->bid = bid;
3800 addr += pbuf->len;
3801 bid++;
3802 if (!*head) {
3803 INIT_LIST_HEAD(&buf->list);
3804 *head = buf;
3805 } else {
3806 list_add_tail(&buf->list, &(*head)->list);
3807 }
3808 }
3809
3810 return i ? i : -ENOMEM;
3811}
3812
Jens Axboe229a7b62020-06-22 10:13:11 -06003813static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3814 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003815{
3816 struct io_provide_buf *p = &req->pbuf;
3817 struct io_ring_ctx *ctx = req->ctx;
3818 struct io_buffer *head, *list;
3819 int ret = 0;
3820
3821 io_ring_submit_lock(ctx, !force_nonblock);
3822
3823 lockdep_assert_held(&ctx->uring_lock);
3824
3825 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3826
3827 ret = io_add_buffers(p, &head);
3828 if (ret < 0)
3829 goto out;
3830
3831 if (!list) {
3832 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3833 GFP_KERNEL);
3834 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003835 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003836 goto out;
3837 }
3838 }
3839out:
3840 io_ring_submit_unlock(ctx, !force_nonblock);
3841 if (ret < 0)
3842 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003843 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003844 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003845}
3846
Jens Axboe3e4827b2020-01-08 15:18:09 -07003847static int io_epoll_ctl_prep(struct io_kiocb *req,
3848 const struct io_uring_sqe *sqe)
3849{
3850#if defined(CONFIG_EPOLL)
3851 if (sqe->ioprio || sqe->buf_index)
3852 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003853 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003854 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003855
3856 req->epoll.epfd = READ_ONCE(sqe->fd);
3857 req->epoll.op = READ_ONCE(sqe->len);
3858 req->epoll.fd = READ_ONCE(sqe->off);
3859
3860 if (ep_op_has_event(req->epoll.op)) {
3861 struct epoll_event __user *ev;
3862
3863 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3864 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3865 return -EFAULT;
3866 }
3867
3868 return 0;
3869#else
3870 return -EOPNOTSUPP;
3871#endif
3872}
3873
Jens Axboe229a7b62020-06-22 10:13:11 -06003874static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3875 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003876{
3877#if defined(CONFIG_EPOLL)
3878 struct io_epoll *ie = &req->epoll;
3879 int ret;
3880
3881 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3882 if (force_nonblock && ret == -EAGAIN)
3883 return -EAGAIN;
3884
3885 if (ret < 0)
3886 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003887 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003888 return 0;
3889#else
3890 return -EOPNOTSUPP;
3891#endif
3892}
3893
Jens Axboec1ca7572019-12-25 22:18:28 -07003894static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3895{
3896#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3897 if (sqe->ioprio || sqe->buf_index || sqe->off)
3898 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003899 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3900 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003901
3902 req->madvise.addr = READ_ONCE(sqe->addr);
3903 req->madvise.len = READ_ONCE(sqe->len);
3904 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3905 return 0;
3906#else
3907 return -EOPNOTSUPP;
3908#endif
3909}
3910
Pavel Begunkov014db002020-03-03 21:33:12 +03003911static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003912{
3913#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3914 struct io_madvise *ma = &req->madvise;
3915 int ret;
3916
3917 if (force_nonblock)
3918 return -EAGAIN;
3919
3920 ret = do_madvise(ma->addr, ma->len, ma->advice);
3921 if (ret < 0)
3922 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003923 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003924 return 0;
3925#else
3926 return -EOPNOTSUPP;
3927#endif
3928}
3929
Jens Axboe4840e412019-12-25 22:03:45 -07003930static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3931{
3932 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3933 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003934 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3935 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003936
3937 req->fadvise.offset = READ_ONCE(sqe->off);
3938 req->fadvise.len = READ_ONCE(sqe->len);
3939 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3940 return 0;
3941}
3942
Pavel Begunkov014db002020-03-03 21:33:12 +03003943static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003944{
3945 struct io_fadvise *fa = &req->fadvise;
3946 int ret;
3947
Jens Axboe3e694262020-02-01 09:22:49 -07003948 if (force_nonblock) {
3949 switch (fa->advice) {
3950 case POSIX_FADV_NORMAL:
3951 case POSIX_FADV_RANDOM:
3952 case POSIX_FADV_SEQUENTIAL:
3953 break;
3954 default:
3955 return -EAGAIN;
3956 }
3957 }
Jens Axboe4840e412019-12-25 22:03:45 -07003958
3959 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3960 if (ret < 0)
3961 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003962 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003963 return 0;
3964}
3965
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003966static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3967{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003968 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003969 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003970 if (sqe->ioprio || sqe->buf_index)
3971 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003972 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003973 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003974
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003975 req->statx.dfd = READ_ONCE(sqe->fd);
3976 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003977 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003978 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3979 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003980
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003981 return 0;
3982}
3983
Pavel Begunkov014db002020-03-03 21:33:12 +03003984static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003985{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003986 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003987 int ret;
3988
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003989 if (force_nonblock) {
3990 /* only need file table for an actual valid fd */
3991 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3992 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003993 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003994 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003995
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003996 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3997 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003998
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003999 if (ret < 0)
4000 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004001 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004002 return 0;
4003}
4004
Jens Axboeb5dba592019-12-11 14:02:38 -07004005static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4006{
4007 /*
4008 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004009 * leave the 'file' in an undeterminate state, and here need to modify
4010 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004011 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004012 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004013 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4014
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004015 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4016 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004017 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4018 sqe->rw_flags || sqe->buf_index)
4019 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004020 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004021 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004022
4023 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004024 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004025 return -EBADF;
4026
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004027 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004028 return 0;
4029}
4030
Jens Axboe229a7b62020-06-22 10:13:11 -06004031static int io_close(struct io_kiocb *req, bool force_nonblock,
4032 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004033{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004034 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004035 int ret;
4036
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004037 /* might be already done during nonblock submission */
4038 if (!close->put_file) {
4039 ret = __close_fd_get_file(close->fd, &close->put_file);
4040 if (ret < 0)
4041 return (ret == -ENOENT) ? -EBADF : ret;
4042 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004043
4044 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004045 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004046 /* was never set, but play safe */
4047 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004048 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004049 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004050 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004051 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004052
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004053 /* No ->flush() or already async, safely close from here */
4054 ret = filp_close(close->put_file, req->work.files);
4055 if (ret < 0)
4056 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004057 fput(close->put_file);
4058 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004059 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004060 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004061}
4062
Jens Axboe3529d8c2019-12-19 18:24:38 -07004063static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004064{
4065 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004066
4067 if (!req->file)
4068 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004069
4070 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4071 return -EINVAL;
4072 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4073 return -EINVAL;
4074
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004075 req->sync.off = READ_ONCE(sqe->off);
4076 req->sync.len = READ_ONCE(sqe->len);
4077 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004078 return 0;
4079}
4080
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004081static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004082{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004083 int ret;
4084
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004085 /* sync_file_range always requires a blocking context */
4086 if (force_nonblock)
4087 return -EAGAIN;
4088
Jens Axboe9adbd452019-12-20 08:45:55 -07004089 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004090 req->sync.flags);
4091 if (ret < 0)
4092 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004093 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004094 return 0;
4095}
4096
YueHaibing469956e2020-03-04 15:53:52 +08004097#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004098static int io_setup_async_msg(struct io_kiocb *req,
4099 struct io_async_msghdr *kmsg)
4100{
4101 if (req->io)
4102 return -EAGAIN;
4103 if (io_alloc_async_ctx(req)) {
4104 if (kmsg->iov != kmsg->fast_iov)
4105 kfree(kmsg->iov);
4106 return -ENOMEM;
4107 }
4108 req->flags |= REQ_F_NEED_CLEANUP;
4109 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4110 return -EAGAIN;
4111}
4112
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004113static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4114 struct io_async_msghdr *iomsg)
4115{
4116 iomsg->iov = iomsg->fast_iov;
4117 iomsg->msg.msg_name = &iomsg->addr;
4118 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4119 req->sr_msg.msg_flags, &iomsg->iov);
4120}
4121
Jens Axboe3529d8c2019-12-19 18:24:38 -07004122static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004123{
Jens Axboee47293f2019-12-20 08:58:21 -07004124 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004125 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004126 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004127
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004128 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4129 return -EINVAL;
4130
Jens Axboee47293f2019-12-20 08:58:21 -07004131 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004132 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004133 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004134
Jens Axboed8768362020-02-27 14:17:49 -07004135#ifdef CONFIG_COMPAT
4136 if (req->ctx->compat)
4137 sr->msg_flags |= MSG_CMSG_COMPAT;
4138#endif
4139
Jens Axboefddafac2020-01-04 20:19:44 -07004140 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004141 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004142 /* iovec is already imported */
4143 if (req->flags & REQ_F_NEED_CLEANUP)
4144 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004145
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004146 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004147 if (!ret)
4148 req->flags |= REQ_F_NEED_CLEANUP;
4149 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004150}
4151
Jens Axboe229a7b62020-06-22 10:13:11 -06004152static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4153 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004154{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004155 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004156 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004157 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004158 int ret;
4159
Jens Axboe03b12302019-12-02 18:50:25 -07004160 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004161 if (unlikely(!sock))
4162 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004163
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004164 if (req->io) {
4165 kmsg = &req->io->msg;
4166 kmsg->msg.msg_name = &req->io->msg.addr;
4167 /* if iov is set, it's allocated already */
4168 if (!kmsg->iov)
4169 kmsg->iov = kmsg->fast_iov;
4170 kmsg->msg.msg_iter.iov = kmsg->iov;
4171 } else {
4172 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004173 if (ret)
4174 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004175 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004176 }
4177
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004178 flags = req->sr_msg.msg_flags;
4179 if (flags & MSG_DONTWAIT)
4180 req->flags |= REQ_F_NOWAIT;
4181 else if (force_nonblock)
4182 flags |= MSG_DONTWAIT;
4183
4184 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4185 if (force_nonblock && ret == -EAGAIN)
4186 return io_setup_async_msg(req, kmsg);
4187 if (ret == -ERESTARTSYS)
4188 ret = -EINTR;
4189
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004190 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004191 kfree(kmsg->iov);
4192 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004193 if (ret < 0)
4194 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004195 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004196 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004197}
4198
Jens Axboe229a7b62020-06-22 10:13:11 -06004199static int io_send(struct io_kiocb *req, bool force_nonblock,
4200 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004201{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004202 struct io_sr_msg *sr = &req->sr_msg;
4203 struct msghdr msg;
4204 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004205 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004206 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004207 int ret;
4208
4209 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004210 if (unlikely(!sock))
4211 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004212
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004213 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4214 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004215 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004216
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004217 msg.msg_name = NULL;
4218 msg.msg_control = NULL;
4219 msg.msg_controllen = 0;
4220 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004221
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004222 flags = req->sr_msg.msg_flags;
4223 if (flags & MSG_DONTWAIT)
4224 req->flags |= REQ_F_NOWAIT;
4225 else if (force_nonblock)
4226 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004227
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004228 msg.msg_flags = flags;
4229 ret = sock_sendmsg(sock, &msg);
4230 if (force_nonblock && ret == -EAGAIN)
4231 return -EAGAIN;
4232 if (ret == -ERESTARTSYS)
4233 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004234
Jens Axboe03b12302019-12-02 18:50:25 -07004235 if (ret < 0)
4236 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004237 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004238 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004239}
4240
Pavel Begunkov1400e692020-07-12 20:41:05 +03004241static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4242 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004243{
4244 struct io_sr_msg *sr = &req->sr_msg;
4245 struct iovec __user *uiov;
4246 size_t iov_len;
4247 int ret;
4248
Pavel Begunkov1400e692020-07-12 20:41:05 +03004249 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4250 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004251 if (ret)
4252 return ret;
4253
4254 if (req->flags & REQ_F_BUFFER_SELECT) {
4255 if (iov_len > 1)
4256 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004257 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004258 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004259 sr->len = iomsg->iov[0].iov_len;
4260 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004261 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004262 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004263 } else {
4264 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004265 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004266 if (ret > 0)
4267 ret = 0;
4268 }
4269
4270 return ret;
4271}
4272
4273#ifdef CONFIG_COMPAT
4274static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004275 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004276{
4277 struct compat_msghdr __user *msg_compat;
4278 struct io_sr_msg *sr = &req->sr_msg;
4279 struct compat_iovec __user *uiov;
4280 compat_uptr_t ptr;
4281 compat_size_t len;
4282 int ret;
4283
Pavel Begunkov270a5942020-07-12 20:41:04 +03004284 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004285 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004286 &ptr, &len);
4287 if (ret)
4288 return ret;
4289
4290 uiov = compat_ptr(ptr);
4291 if (req->flags & REQ_F_BUFFER_SELECT) {
4292 compat_ssize_t clen;
4293
4294 if (len > 1)
4295 return -EINVAL;
4296 if (!access_ok(uiov, sizeof(*uiov)))
4297 return -EFAULT;
4298 if (__get_user(clen, &uiov->iov_len))
4299 return -EFAULT;
4300 if (clen < 0)
4301 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004302 sr->len = iomsg->iov[0].iov_len;
4303 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004304 } else {
4305 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004306 &iomsg->iov,
4307 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004308 if (ret < 0)
4309 return ret;
4310 }
4311
4312 return 0;
4313}
Jens Axboe03b12302019-12-02 18:50:25 -07004314#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004315
Pavel Begunkov1400e692020-07-12 20:41:05 +03004316static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4317 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004318{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004319 iomsg->msg.msg_name = &iomsg->addr;
4320 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004321
4322#ifdef CONFIG_COMPAT
4323 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004324 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004325#endif
4326
Pavel Begunkov1400e692020-07-12 20:41:05 +03004327 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004328}
4329
Jens Axboebcda7ba2020-02-23 16:42:51 -07004330static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004331 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004332{
4333 struct io_sr_msg *sr = &req->sr_msg;
4334 struct io_buffer *kbuf;
4335
Jens Axboebcda7ba2020-02-23 16:42:51 -07004336 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4337 if (IS_ERR(kbuf))
4338 return kbuf;
4339
4340 sr->kbuf = kbuf;
4341 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004342 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004343}
4344
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004345static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4346{
4347 return io_put_kbuf(req, req->sr_msg.kbuf);
4348}
4349
Jens Axboe3529d8c2019-12-19 18:24:38 -07004350static int io_recvmsg_prep(struct io_kiocb *req,
4351 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004352{
Jens Axboee47293f2019-12-20 08:58:21 -07004353 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004354 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004355 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004356
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004357 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4358 return -EINVAL;
4359
Jens Axboe3529d8c2019-12-19 18:24:38 -07004360 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004361 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004362 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004363 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004364
Jens Axboed8768362020-02-27 14:17:49 -07004365#ifdef CONFIG_COMPAT
4366 if (req->ctx->compat)
4367 sr->msg_flags |= MSG_CMSG_COMPAT;
4368#endif
4369
Jens Axboefddafac2020-01-04 20:19:44 -07004370 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004371 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004372 /* iovec is already imported */
4373 if (req->flags & REQ_F_NEED_CLEANUP)
4374 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004375
Pavel Begunkov1400e692020-07-12 20:41:05 +03004376 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004377 if (!ret)
4378 req->flags |= REQ_F_NEED_CLEANUP;
4379 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004380}
4381
Jens Axboe229a7b62020-06-22 10:13:11 -06004382static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4383 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004384{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004385 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004386 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004387 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004388 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004389 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004390
Jens Axboe0fa03c62019-04-19 13:34:07 -06004391 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004392 if (unlikely(!sock))
4393 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004394
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004395 if (req->io) {
4396 kmsg = &req->io->msg;
4397 kmsg->msg.msg_name = &req->io->msg.addr;
4398 /* if iov is set, it's allocated already */
4399 if (!kmsg->iov)
4400 kmsg->iov = kmsg->fast_iov;
4401 kmsg->msg.msg_iter.iov = kmsg->iov;
4402 } else {
4403 ret = io_recvmsg_copy_hdr(req, &iomsg);
4404 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004405 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004406 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004407 }
4408
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004409 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004410 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004411 if (IS_ERR(kbuf))
4412 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004413 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4414 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4415 1, req->sr_msg.len);
4416 }
4417
4418 flags = req->sr_msg.msg_flags;
4419 if (flags & MSG_DONTWAIT)
4420 req->flags |= REQ_F_NOWAIT;
4421 else if (force_nonblock)
4422 flags |= MSG_DONTWAIT;
4423
4424 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4425 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004426 if (force_nonblock && ret == -EAGAIN)
4427 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004428 if (ret == -ERESTARTSYS)
4429 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004430
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004431 if (req->flags & REQ_F_BUFFER_SELECTED)
4432 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004433 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004434 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004435 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004436 if (ret < 0)
4437 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004438 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004439 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004440}
4441
Jens Axboe229a7b62020-06-22 10:13:11 -06004442static int io_recv(struct io_kiocb *req, bool force_nonblock,
4443 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004444{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004445 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004446 struct io_sr_msg *sr = &req->sr_msg;
4447 struct msghdr msg;
4448 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004449 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004450 struct iovec iov;
4451 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004452 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004453
Jens Axboefddafac2020-01-04 20:19:44 -07004454 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004455 if (unlikely(!sock))
4456 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004457
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004458 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004459 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004460 if (IS_ERR(kbuf))
4461 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004462 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004463 }
4464
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004465 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004466 if (unlikely(ret))
4467 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004468
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004469 msg.msg_name = NULL;
4470 msg.msg_control = NULL;
4471 msg.msg_controllen = 0;
4472 msg.msg_namelen = 0;
4473 msg.msg_iocb = NULL;
4474 msg.msg_flags = 0;
4475
4476 flags = req->sr_msg.msg_flags;
4477 if (flags & MSG_DONTWAIT)
4478 req->flags |= REQ_F_NOWAIT;
4479 else if (force_nonblock)
4480 flags |= MSG_DONTWAIT;
4481
4482 ret = sock_recvmsg(sock, &msg, flags);
4483 if (force_nonblock && ret == -EAGAIN)
4484 return -EAGAIN;
4485 if (ret == -ERESTARTSYS)
4486 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004487out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004488 if (req->flags & REQ_F_BUFFER_SELECTED)
4489 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004490 if (ret < 0)
4491 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004492 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004493 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004494}
4495
Jens Axboe3529d8c2019-12-19 18:24:38 -07004496static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004497{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004498 struct io_accept *accept = &req->accept;
4499
Jens Axboe17f2fe32019-10-17 14:42:58 -06004500 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4501 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004502 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004503 return -EINVAL;
4504
Jens Axboed55e5f52019-12-11 16:12:15 -07004505 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4506 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004507 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004508 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004509 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004510}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004511
Jens Axboe229a7b62020-06-22 10:13:11 -06004512static int io_accept(struct io_kiocb *req, bool force_nonblock,
4513 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004514{
4515 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004516 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004517 int ret;
4518
Jiufei Xuee697dee2020-06-10 13:41:59 +08004519 if (req->file->f_flags & O_NONBLOCK)
4520 req->flags |= REQ_F_NOWAIT;
4521
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004522 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004523 accept->addr_len, accept->flags,
4524 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004525 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004526 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004527 if (ret < 0) {
4528 if (ret == -ERESTARTSYS)
4529 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004530 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004531 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004532 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004533 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004534}
4535
Jens Axboe3529d8c2019-12-19 18:24:38 -07004536static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004537{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004538 struct io_connect *conn = &req->connect;
4539 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004540
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004541 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4542 return -EINVAL;
4543 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4544 return -EINVAL;
4545
Jens Axboe3529d8c2019-12-19 18:24:38 -07004546 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4547 conn->addr_len = READ_ONCE(sqe->addr2);
4548
4549 if (!io)
4550 return 0;
4551
4552 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004553 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004554}
4555
Jens Axboe229a7b62020-06-22 10:13:11 -06004556static int io_connect(struct io_kiocb *req, bool force_nonblock,
4557 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004558{
Jens Axboef499a022019-12-02 16:28:46 -07004559 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004560 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004561 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004562
Jens Axboef499a022019-12-02 16:28:46 -07004563 if (req->io) {
4564 io = req->io;
4565 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004566 ret = move_addr_to_kernel(req->connect.addr,
4567 req->connect.addr_len,
4568 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004569 if (ret)
4570 goto out;
4571 io = &__io;
4572 }
4573
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004574 file_flags = force_nonblock ? O_NONBLOCK : 0;
4575
4576 ret = __sys_connect_file(req->file, &io->connect.address,
4577 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004578 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004579 if (req->io)
4580 return -EAGAIN;
4581 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004582 ret = -ENOMEM;
4583 goto out;
4584 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004585 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004586 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004587 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004588 if (ret == -ERESTARTSYS)
4589 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004590out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004591 if (ret < 0)
4592 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004593 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004594 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004595}
YueHaibing469956e2020-03-04 15:53:52 +08004596#else /* !CONFIG_NET */
4597static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4598{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004599 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004600}
4601
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004602static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4603 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004604{
YueHaibing469956e2020-03-04 15:53:52 +08004605 return -EOPNOTSUPP;
4606}
4607
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004608static int io_send(struct io_kiocb *req, bool force_nonblock,
4609 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004610{
4611 return -EOPNOTSUPP;
4612}
4613
4614static int io_recvmsg_prep(struct io_kiocb *req,
4615 const struct io_uring_sqe *sqe)
4616{
4617 return -EOPNOTSUPP;
4618}
4619
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004620static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4621 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004622{
4623 return -EOPNOTSUPP;
4624}
4625
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004626static int io_recv(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
4632static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4633{
4634 return -EOPNOTSUPP;
4635}
4636
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004637static int io_accept(struct io_kiocb *req, bool force_nonblock,
4638 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004639{
4640 return -EOPNOTSUPP;
4641}
4642
4643static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4644{
4645 return -EOPNOTSUPP;
4646}
4647
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004648static int io_connect(struct io_kiocb *req, bool force_nonblock,
4649 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004650{
4651 return -EOPNOTSUPP;
4652}
4653#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004654
Jens Axboed7718a92020-02-14 22:23:12 -07004655struct io_poll_table {
4656 struct poll_table_struct pt;
4657 struct io_kiocb *req;
4658 int error;
4659};
4660
Jens Axboed7718a92020-02-14 22:23:12 -07004661static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4662 __poll_t mask, task_work_func_t func)
4663{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004664 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004665 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004666
4667 /* for instances that support it check for an event match first: */
4668 if (mask && !(mask & poll->events))
4669 return 0;
4670
4671 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4672
4673 list_del_init(&poll->wait.entry);
4674
Jens Axboed7718a92020-02-14 22:23:12 -07004675 req->result = mask;
4676 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004677 percpu_ref_get(&req->ctx->refs);
4678
Jens Axboed7718a92020-02-14 22:23:12 -07004679 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004680 * If we using the signalfd wait_queue_head for this wakeup, then
4681 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4682 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4683 * either, as the normal wakeup will suffice.
4684 */
4685 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4686
4687 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004688 * If this fails, then the task is exiting. When a task exits, the
4689 * work gets canceled, so just cancel this request as well instead
4690 * of executing it. We can't safely execute it anyway, as we may not
4691 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004692 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004693 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004694 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004695 struct task_struct *tsk;
4696
Jens Axboee3aabf92020-05-18 11:04:17 -06004697 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004698 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004699 task_work_add(tsk, &req->task_work, 0);
4700 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004701 }
Jens Axboed7718a92020-02-14 22:23:12 -07004702 return 1;
4703}
4704
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004705static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4706 __acquires(&req->ctx->completion_lock)
4707{
4708 struct io_ring_ctx *ctx = req->ctx;
4709
4710 if (!req->result && !READ_ONCE(poll->canceled)) {
4711 struct poll_table_struct pt = { ._key = poll->events };
4712
4713 req->result = vfs_poll(req->file, &pt) & poll->events;
4714 }
4715
4716 spin_lock_irq(&ctx->completion_lock);
4717 if (!req->result && !READ_ONCE(poll->canceled)) {
4718 add_wait_queue(poll->head, &poll->wait);
4719 return true;
4720 }
4721
4722 return false;
4723}
4724
Jens Axboed4e7cd32020-08-15 11:44:50 -07004725static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004726{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004727 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4728 if (req->opcode == IORING_OP_POLL_ADD)
4729 return (struct io_poll_iocb *) req->io;
4730 return req->apoll->double_poll;
4731}
4732
4733static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4734{
4735 if (req->opcode == IORING_OP_POLL_ADD)
4736 return &req->poll;
4737 return &req->apoll->poll;
4738}
4739
4740static void io_poll_remove_double(struct io_kiocb *req)
4741{
4742 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004743
4744 lockdep_assert_held(&req->ctx->completion_lock);
4745
4746 if (poll && poll->head) {
4747 struct wait_queue_head *head = poll->head;
4748
4749 spin_lock(&head->lock);
4750 list_del_init(&poll->wait.entry);
4751 if (poll->wait.private)
4752 refcount_dec(&req->refs);
4753 poll->head = NULL;
4754 spin_unlock(&head->lock);
4755 }
4756}
4757
4758static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4759{
4760 struct io_ring_ctx *ctx = req->ctx;
4761
Jens Axboed4e7cd32020-08-15 11:44:50 -07004762 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004763 req->poll.done = true;
4764 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4765 io_commit_cqring(ctx);
4766}
4767
4768static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4769{
4770 struct io_ring_ctx *ctx = req->ctx;
4771
4772 if (io_poll_rewait(req, &req->poll)) {
4773 spin_unlock_irq(&ctx->completion_lock);
4774 return;
4775 }
4776
4777 hash_del(&req->hash_node);
4778 io_poll_complete(req, req->result, 0);
4779 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004780 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004781 spin_unlock_irq(&ctx->completion_lock);
4782
4783 io_cqring_ev_posted(ctx);
4784}
4785
4786static void io_poll_task_func(struct callback_head *cb)
4787{
4788 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004789 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004790 struct io_kiocb *nxt = NULL;
4791
4792 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004793 if (nxt)
4794 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004795 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004796}
4797
4798static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4799 int sync, void *key)
4800{
4801 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004802 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004803 __poll_t mask = key_to_poll(key);
4804
4805 /* for instances that support it check for an event match first: */
4806 if (mask && !(mask & poll->events))
4807 return 0;
4808
Jens Axboe8706e042020-09-28 08:38:54 -06004809 list_del_init(&wait->entry);
4810
Jens Axboe807abcb2020-07-17 17:09:27 -06004811 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004812 bool done;
4813
Jens Axboe807abcb2020-07-17 17:09:27 -06004814 spin_lock(&poll->head->lock);
4815 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004816 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004817 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004818 /* make sure double remove sees this as being gone */
4819 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004820 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004821 if (!done)
4822 __io_async_wake(req, poll, mask, io_poll_task_func);
4823 }
4824 refcount_dec(&req->refs);
4825 return 1;
4826}
4827
4828static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4829 wait_queue_func_t wake_func)
4830{
4831 poll->head = NULL;
4832 poll->done = false;
4833 poll->canceled = false;
4834 poll->events = events;
4835 INIT_LIST_HEAD(&poll->wait.entry);
4836 init_waitqueue_func_entry(&poll->wait, wake_func);
4837}
4838
4839static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004840 struct wait_queue_head *head,
4841 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004842{
4843 struct io_kiocb *req = pt->req;
4844
4845 /*
4846 * If poll->head is already set, it's because the file being polled
4847 * uses multiple waitqueues for poll handling (eg one for read, one
4848 * for write). Setup a separate io_poll_iocb if this happens.
4849 */
4850 if (unlikely(poll->head)) {
4851 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004852 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004853 pt->error = -EINVAL;
4854 return;
4855 }
4856 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4857 if (!poll) {
4858 pt->error = -ENOMEM;
4859 return;
4860 }
4861 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4862 refcount_inc(&req->refs);
4863 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004864 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004865 }
4866
4867 pt->error = 0;
4868 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004869
4870 if (poll->events & EPOLLEXCLUSIVE)
4871 add_wait_queue_exclusive(head, &poll->wait);
4872 else
4873 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004874}
4875
4876static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4877 struct poll_table_struct *p)
4878{
4879 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004880 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004881
Jens Axboe807abcb2020-07-17 17:09:27 -06004882 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004883}
4884
Jens Axboed7718a92020-02-14 22:23:12 -07004885static void io_async_task_func(struct callback_head *cb)
4886{
4887 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4888 struct async_poll *apoll = req->apoll;
4889 struct io_ring_ctx *ctx = req->ctx;
4890
4891 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4892
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004893 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004894 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004895 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004896 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004897 }
4898
Jens Axboe31067252020-05-17 17:43:31 -06004899 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004900 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004901 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004902
Jens Axboed4e7cd32020-08-15 11:44:50 -07004903 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004904 spin_unlock_irq(&ctx->completion_lock);
4905
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004906 if (!READ_ONCE(apoll->poll.canceled))
4907 __io_req_task_submit(req);
4908 else
4909 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004910
Jens Axboe6d816e02020-08-11 08:04:14 -06004911 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004912 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004913 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004914}
4915
4916static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4917 void *key)
4918{
4919 struct io_kiocb *req = wait->private;
4920 struct io_poll_iocb *poll = &req->apoll->poll;
4921
4922 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4923 key_to_poll(key));
4924
4925 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4926}
4927
4928static void io_poll_req_insert(struct io_kiocb *req)
4929{
4930 struct io_ring_ctx *ctx = req->ctx;
4931 struct hlist_head *list;
4932
4933 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4934 hlist_add_head(&req->hash_node, list);
4935}
4936
4937static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4938 struct io_poll_iocb *poll,
4939 struct io_poll_table *ipt, __poll_t mask,
4940 wait_queue_func_t wake_func)
4941 __acquires(&ctx->completion_lock)
4942{
4943 struct io_ring_ctx *ctx = req->ctx;
4944 bool cancel = false;
4945
Jens Axboe18bceab2020-05-15 11:56:54 -06004946 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004947 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004948 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004949
4950 ipt->pt._key = mask;
4951 ipt->req = req;
4952 ipt->error = -EINVAL;
4953
Jens Axboed7718a92020-02-14 22:23:12 -07004954 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4955
4956 spin_lock_irq(&ctx->completion_lock);
4957 if (likely(poll->head)) {
4958 spin_lock(&poll->head->lock);
4959 if (unlikely(list_empty(&poll->wait.entry))) {
4960 if (ipt->error)
4961 cancel = true;
4962 ipt->error = 0;
4963 mask = 0;
4964 }
4965 if (mask || ipt->error)
4966 list_del_init(&poll->wait.entry);
4967 else if (cancel)
4968 WRITE_ONCE(poll->canceled, true);
4969 else if (!poll->done) /* actually waiting for an event */
4970 io_poll_req_insert(req);
4971 spin_unlock(&poll->head->lock);
4972 }
4973
4974 return mask;
4975}
4976
4977static bool io_arm_poll_handler(struct io_kiocb *req)
4978{
4979 const struct io_op_def *def = &io_op_defs[req->opcode];
4980 struct io_ring_ctx *ctx = req->ctx;
4981 struct async_poll *apoll;
4982 struct io_poll_table ipt;
4983 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004984 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004985
4986 if (!req->file || !file_can_poll(req->file))
4987 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004988 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004989 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004990 if (def->pollin)
4991 rw = READ;
4992 else if (def->pollout)
4993 rw = WRITE;
4994 else
4995 return false;
4996 /* if we can't nonblock try, then no point in arming a poll handler */
4997 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004998 return false;
4999
5000 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5001 if (unlikely(!apoll))
5002 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005003 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005004
5005 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005006 req->apoll = apoll;
5007 INIT_HLIST_NODE(&req->hash_node);
5008
Nathan Chancellor8755d972020-03-02 16:01:19 -07005009 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005010 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005011 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005012 if (def->pollout)
5013 mask |= POLLOUT | POLLWRNORM;
5014 mask |= POLLERR | POLLPRI;
5015
5016 ipt.pt._qproc = io_async_queue_proc;
5017
5018 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5019 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005020 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005021 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005022 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005023 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005024 kfree(apoll);
5025 return false;
5026 }
5027 spin_unlock_irq(&ctx->completion_lock);
5028 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5029 apoll->poll.events);
5030 return true;
5031}
5032
5033static bool __io_poll_remove_one(struct io_kiocb *req,
5034 struct io_poll_iocb *poll)
5035{
Jens Axboeb41e9852020-02-17 09:52:41 -07005036 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005037
5038 spin_lock(&poll->head->lock);
5039 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005040 if (!list_empty(&poll->wait.entry)) {
5041 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005042 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005043 }
5044 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005045 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005046 return do_complete;
5047}
5048
5049static bool io_poll_remove_one(struct io_kiocb *req)
5050{
5051 bool do_complete;
5052
Jens Axboed4e7cd32020-08-15 11:44:50 -07005053 io_poll_remove_double(req);
5054
Jens Axboed7718a92020-02-14 22:23:12 -07005055 if (req->opcode == IORING_OP_POLL_ADD) {
5056 do_complete = __io_poll_remove_one(req, &req->poll);
5057 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005058 struct async_poll *apoll = req->apoll;
5059
Jens Axboed7718a92020-02-14 22:23:12 -07005060 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005061 do_complete = __io_poll_remove_one(req, &apoll->poll);
5062 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005063 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005064 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005065 kfree(apoll);
5066 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005067 }
5068
Jens Axboeb41e9852020-02-17 09:52:41 -07005069 if (do_complete) {
5070 io_cqring_fill_event(req, -ECANCELED);
5071 io_commit_cqring(req->ctx);
5072 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005073 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005074 io_put_req(req);
5075 }
5076
5077 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005078}
5079
Jens Axboe76e1b642020-09-26 15:05:03 -06005080/*
5081 * Returns true if we found and killed one or more poll requests
5082 */
5083static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005084{
Jens Axboe78076bb2019-12-04 19:56:40 -07005085 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005086 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005087 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005088
5089 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005090 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5091 struct hlist_head *list;
5092
5093 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005094 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5095 if (io_task_match(req, tsk))
5096 posted += io_poll_remove_one(req);
5097 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005098 }
5099 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005100
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005101 if (posted)
5102 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005103
5104 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005105}
5106
Jens Axboe47f46762019-11-09 17:43:02 -07005107static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5108{
Jens Axboe78076bb2019-12-04 19:56:40 -07005109 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005110 struct io_kiocb *req;
5111
Jens Axboe78076bb2019-12-04 19:56:40 -07005112 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5113 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005114 if (sqe_addr != req->user_data)
5115 continue;
5116 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005117 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005118 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005119 }
5120
5121 return -ENOENT;
5122}
5123
Jens Axboe3529d8c2019-12-19 18:24:38 -07005124static int io_poll_remove_prep(struct io_kiocb *req,
5125 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005126{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005127 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5128 return -EINVAL;
5129 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5130 sqe->poll_events)
5131 return -EINVAL;
5132
Jens Axboe0969e782019-12-17 18:40:57 -07005133 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005134 return 0;
5135}
5136
5137/*
5138 * Find a running poll command that matches one specified in sqe->addr,
5139 * and remove it if found.
5140 */
5141static int io_poll_remove(struct io_kiocb *req)
5142{
5143 struct io_ring_ctx *ctx = req->ctx;
5144 u64 addr;
5145 int ret;
5146
Jens Axboe0969e782019-12-17 18:40:57 -07005147 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005148 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005149 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005150 spin_unlock_irq(&ctx->completion_lock);
5151
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005152 if (ret < 0)
5153 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005154 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005155 return 0;
5156}
5157
Jens Axboe221c5eb2019-01-17 09:41:58 -07005158static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5159 void *key)
5160{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005161 struct io_kiocb *req = wait->private;
5162 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005163
Jens Axboed7718a92020-02-14 22:23:12 -07005164 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005165}
5166
Jens Axboe221c5eb2019-01-17 09:41:58 -07005167static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5168 struct poll_table_struct *p)
5169{
5170 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5171
Jens Axboe807abcb2020-07-17 17:09:27 -06005172 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005173}
5174
Jens Axboe3529d8c2019-12-19 18:24:38 -07005175static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005176{
5177 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005178 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005179
5180 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5181 return -EINVAL;
5182 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5183 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005184 if (!poll->file)
5185 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005186
Jiufei Xue5769a352020-06-17 17:53:55 +08005187 events = READ_ONCE(sqe->poll32_events);
5188#ifdef __BIG_ENDIAN
5189 events = swahw32(events);
5190#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005191 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5192 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005193 return 0;
5194}
5195
Pavel Begunkov014db002020-03-03 21:33:12 +03005196static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005197{
5198 struct io_poll_iocb *poll = &req->poll;
5199 struct io_ring_ctx *ctx = req->ctx;
5200 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005201 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005202
Jens Axboe78076bb2019-12-04 19:56:40 -07005203 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005204 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005205
Jens Axboed7718a92020-02-14 22:23:12 -07005206 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5207 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005208
Jens Axboe8c838782019-03-12 15:48:16 -06005209 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005210 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005211 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005212 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005213 spin_unlock_irq(&ctx->completion_lock);
5214
Jens Axboe8c838782019-03-12 15:48:16 -06005215 if (mask) {
5216 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005217 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005218 }
Jens Axboe8c838782019-03-12 15:48:16 -06005219 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005220}
5221
Jens Axboe5262f562019-09-17 12:26:57 -06005222static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5223{
Jens Axboead8a48a2019-11-15 08:49:11 -07005224 struct io_timeout_data *data = container_of(timer,
5225 struct io_timeout_data, timer);
5226 struct io_kiocb *req = data->req;
5227 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005228 unsigned long flags;
5229
Jens Axboe5262f562019-09-17 12:26:57 -06005230 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005231 atomic_set(&req->ctx->cq_timeouts,
5232 atomic_read(&req->ctx->cq_timeouts) + 1);
5233
zhangyi (F)ef036812019-10-23 15:10:08 +08005234 /*
Jens Axboe11365042019-10-16 09:08:32 -06005235 * We could be racing with timeout deletion. If the list is empty,
5236 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005237 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005238 if (!list_empty(&req->timeout.list))
5239 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005240
Jens Axboe78e19bb2019-11-06 15:21:34 -07005241 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005242 io_commit_cqring(ctx);
5243 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5244
5245 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005246 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005247 io_put_req(req);
5248 return HRTIMER_NORESTART;
5249}
5250
Jens Axboef254ac02020-08-12 17:33:30 -06005251static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005252{
Jens Axboef254ac02020-08-12 17:33:30 -06005253 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005254
Jens Axboef254ac02020-08-12 17:33:30 -06005255 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005256
Jens Axboe2d283902019-12-04 11:08:05 -07005257 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005258 if (ret == -1)
5259 return -EALREADY;
5260
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005261 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005262 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005263 io_cqring_fill_event(req, -ECANCELED);
5264 io_put_req(req);
5265 return 0;
5266}
5267
Jens Axboef254ac02020-08-12 17:33:30 -06005268static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5269{
5270 struct io_kiocb *req;
5271 int ret = -ENOENT;
5272
5273 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5274 if (user_data == req->user_data) {
5275 ret = 0;
5276 break;
5277 }
5278 }
5279
5280 if (ret == -ENOENT)
5281 return ret;
5282
5283 return __io_timeout_cancel(req);
5284}
5285
Jens Axboe3529d8c2019-12-19 18:24:38 -07005286static int io_timeout_remove_prep(struct io_kiocb *req,
5287 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005288{
Jens Axboeb29472e2019-12-17 18:50:29 -07005289 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5290 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005291 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5292 return -EINVAL;
5293 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005294 return -EINVAL;
5295
5296 req->timeout.addr = READ_ONCE(sqe->addr);
5297 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5298 if (req->timeout.flags)
5299 return -EINVAL;
5300
Jens Axboeb29472e2019-12-17 18:50:29 -07005301 return 0;
5302}
5303
Jens Axboe11365042019-10-16 09:08:32 -06005304/*
5305 * Remove or update an existing timeout command
5306 */
Jens Axboefc4df992019-12-10 14:38:45 -07005307static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005308{
5309 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005310 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005311
Jens Axboe11365042019-10-16 09:08:32 -06005312 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005313 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005314
Jens Axboe47f46762019-11-09 17:43:02 -07005315 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005316 io_commit_cqring(ctx);
5317 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005318 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005319 if (ret < 0)
5320 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005321 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005322 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005323}
5324
Jens Axboe3529d8c2019-12-19 18:24:38 -07005325static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005326 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005327{
Jens Axboead8a48a2019-11-15 08:49:11 -07005328 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005329 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005330 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005331
Jens Axboead8a48a2019-11-15 08:49:11 -07005332 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005333 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005334 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005335 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005336 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005337 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005338 flags = READ_ONCE(sqe->timeout_flags);
5339 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005340 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005341
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005342 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005343
Jens Axboe3529d8c2019-12-19 18:24:38 -07005344 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005345 return -ENOMEM;
5346
5347 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005348 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005349
5350 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005351 return -EFAULT;
5352
Jens Axboe11365042019-10-16 09:08:32 -06005353 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005354 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005355 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005356 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005357
Jens Axboead8a48a2019-11-15 08:49:11 -07005358 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5359 return 0;
5360}
5361
Jens Axboefc4df992019-12-10 14:38:45 -07005362static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005363{
Jens Axboead8a48a2019-11-15 08:49:11 -07005364 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005365 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005366 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005367 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005368
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005369 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005370
Jens Axboe5262f562019-09-17 12:26:57 -06005371 /*
5372 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005373 * timeout event to be satisfied. If it isn't set, then this is
5374 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005375 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005376 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005377 entry = ctx->timeout_list.prev;
5378 goto add;
5379 }
Jens Axboe5262f562019-09-17 12:26:57 -06005380
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005381 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5382 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005383
5384 /*
5385 * Insertion sort, ensuring the first entry in the list is always
5386 * the one we need first.
5387 */
Jens Axboe5262f562019-09-17 12:26:57 -06005388 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005389 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5390 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005391
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005392 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005393 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005394 /* nxt.seq is behind @tail, otherwise would've been completed */
5395 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005396 break;
5397 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005398add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005399 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005400 data->timer.function = io_timeout_fn;
5401 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005402 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005403 return 0;
5404}
5405
Jens Axboe62755e32019-10-28 21:49:21 -06005406static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005407{
Jens Axboe62755e32019-10-28 21:49:21 -06005408 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005409
Jens Axboe62755e32019-10-28 21:49:21 -06005410 return req->user_data == (unsigned long) data;
5411}
5412
Jens Axboee977d6d2019-11-05 12:39:45 -07005413static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005414{
Jens Axboe62755e32019-10-28 21:49:21 -06005415 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005416 int ret = 0;
5417
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005418 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005419 switch (cancel_ret) {
5420 case IO_WQ_CANCEL_OK:
5421 ret = 0;
5422 break;
5423 case IO_WQ_CANCEL_RUNNING:
5424 ret = -EALREADY;
5425 break;
5426 case IO_WQ_CANCEL_NOTFOUND:
5427 ret = -ENOENT;
5428 break;
5429 }
5430
Jens Axboee977d6d2019-11-05 12:39:45 -07005431 return ret;
5432}
5433
Jens Axboe47f46762019-11-09 17:43:02 -07005434static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5435 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005436 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005437{
5438 unsigned long flags;
5439 int ret;
5440
5441 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5442 if (ret != -ENOENT) {
5443 spin_lock_irqsave(&ctx->completion_lock, flags);
5444 goto done;
5445 }
5446
5447 spin_lock_irqsave(&ctx->completion_lock, flags);
5448 ret = io_timeout_cancel(ctx, sqe_addr);
5449 if (ret != -ENOENT)
5450 goto done;
5451 ret = io_poll_cancel(ctx, sqe_addr);
5452done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005453 if (!ret)
5454 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005455 io_cqring_fill_event(req, ret);
5456 io_commit_cqring(ctx);
5457 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5458 io_cqring_ev_posted(ctx);
5459
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005460 if (ret < 0)
5461 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005462 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005463}
5464
Jens Axboe3529d8c2019-12-19 18:24:38 -07005465static int io_async_cancel_prep(struct io_kiocb *req,
5466 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005467{
Jens Axboefbf23842019-12-17 18:45:56 -07005468 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005469 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005470 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5471 return -EINVAL;
5472 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005473 return -EINVAL;
5474
Jens Axboefbf23842019-12-17 18:45:56 -07005475 req->cancel.addr = READ_ONCE(sqe->addr);
5476 return 0;
5477}
5478
Pavel Begunkov014db002020-03-03 21:33:12 +03005479static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005480{
5481 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005482
Pavel Begunkov014db002020-03-03 21:33:12 +03005483 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005484 return 0;
5485}
5486
Jens Axboe05f3fb32019-12-09 11:22:50 -07005487static int io_files_update_prep(struct io_kiocb *req,
5488 const struct io_uring_sqe *sqe)
5489{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005490 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5491 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005492 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5493 return -EINVAL;
5494 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005495 return -EINVAL;
5496
5497 req->files_update.offset = READ_ONCE(sqe->off);
5498 req->files_update.nr_args = READ_ONCE(sqe->len);
5499 if (!req->files_update.nr_args)
5500 return -EINVAL;
5501 req->files_update.arg = READ_ONCE(sqe->addr);
5502 return 0;
5503}
5504
Jens Axboe229a7b62020-06-22 10:13:11 -06005505static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5506 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005507{
5508 struct io_ring_ctx *ctx = req->ctx;
5509 struct io_uring_files_update up;
5510 int ret;
5511
Jens Axboef86cd202020-01-29 13:46:44 -07005512 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005513 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005514
5515 up.offset = req->files_update.offset;
5516 up.fds = req->files_update.arg;
5517
5518 mutex_lock(&ctx->uring_lock);
5519 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5520 mutex_unlock(&ctx->uring_lock);
5521
5522 if (ret < 0)
5523 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005524 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005525 return 0;
5526}
5527
Jens Axboe3529d8c2019-12-19 18:24:38 -07005528static int io_req_defer_prep(struct io_kiocb *req,
5529 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005530{
Jens Axboee7815732019-12-17 19:45:06 -07005531 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005532
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005533 if (!sqe)
5534 return 0;
5535
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005536 if (io_alloc_async_ctx(req))
5537 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005538 ret = io_prep_work_files(req);
5539 if (unlikely(ret))
5540 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005541
Jens Axboe202700e12020-09-12 13:18:10 -06005542 io_prep_async_work(req);
5543
Jens Axboed625c6e2019-12-17 19:53:05 -07005544 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005545 case IORING_OP_NOP:
5546 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005547 case IORING_OP_READV:
5548 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005549 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005550 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005551 break;
5552 case IORING_OP_WRITEV:
5553 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005554 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005555 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005556 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005557 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005558 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005559 break;
5560 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005561 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005562 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005563 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005564 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005565 break;
5566 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005567 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005568 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005569 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005570 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005571 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005572 break;
5573 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005574 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005575 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005576 break;
Jens Axboef499a022019-12-02 16:28:46 -07005577 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005578 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005579 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005580 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005581 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005582 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005583 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005584 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005585 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005586 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005587 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005588 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005589 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005590 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005591 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005592 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005593 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005594 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005595 case IORING_OP_FALLOCATE:
5596 ret = io_fallocate_prep(req, sqe);
5597 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005598 case IORING_OP_OPENAT:
5599 ret = io_openat_prep(req, sqe);
5600 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005601 case IORING_OP_CLOSE:
5602 ret = io_close_prep(req, sqe);
5603 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005604 case IORING_OP_FILES_UPDATE:
5605 ret = io_files_update_prep(req, sqe);
5606 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005607 case IORING_OP_STATX:
5608 ret = io_statx_prep(req, sqe);
5609 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005610 case IORING_OP_FADVISE:
5611 ret = io_fadvise_prep(req, sqe);
5612 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005613 case IORING_OP_MADVISE:
5614 ret = io_madvise_prep(req, sqe);
5615 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005616 case IORING_OP_OPENAT2:
5617 ret = io_openat2_prep(req, sqe);
5618 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005619 case IORING_OP_EPOLL_CTL:
5620 ret = io_epoll_ctl_prep(req, sqe);
5621 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005622 case IORING_OP_SPLICE:
5623 ret = io_splice_prep(req, sqe);
5624 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005625 case IORING_OP_PROVIDE_BUFFERS:
5626 ret = io_provide_buffers_prep(req, sqe);
5627 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005628 case IORING_OP_REMOVE_BUFFERS:
5629 ret = io_remove_buffers_prep(req, sqe);
5630 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005631 case IORING_OP_TEE:
5632 ret = io_tee_prep(req, sqe);
5633 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005634 default:
Jens Axboee7815732019-12-17 19:45:06 -07005635 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5636 req->opcode);
5637 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005638 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005639 }
5640
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005641 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005642}
5643
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005644static u32 io_get_sequence(struct io_kiocb *req)
5645{
5646 struct io_kiocb *pos;
5647 struct io_ring_ctx *ctx = req->ctx;
5648 u32 total_submitted, nr_reqs = 1;
5649
5650 if (req->flags & REQ_F_LINK_HEAD)
5651 list_for_each_entry(pos, &req->link_list, link_list)
5652 nr_reqs++;
5653
5654 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5655 return total_submitted - nr_reqs;
5656}
5657
Jens Axboe3529d8c2019-12-19 18:24:38 -07005658static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005659{
Jackie Liua197f662019-11-08 08:09:12 -07005660 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005661 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005662 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005663 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005664
Bob Liu9d858b22019-11-13 18:06:25 +08005665 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005666 if (likely(list_empty_careful(&ctx->defer_list) &&
5667 !(req->flags & REQ_F_IO_DRAIN)))
5668 return 0;
5669
5670 seq = io_get_sequence(req);
5671 /* Still a chance to pass the sequence check */
5672 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005673 return 0;
5674
Pavel Begunkov650b5482020-05-17 14:02:11 +03005675 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005676 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005677 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005678 return ret;
5679 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005680 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005681 de = kmalloc(sizeof(*de), GFP_KERNEL);
5682 if (!de)
5683 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005684
Jens Axboede0617e2019-04-06 21:51:27 -06005685 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005686 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005687 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005688 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005689 io_queue_async_work(req);
5690 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005691 }
5692
Jens Axboe915967f2019-11-21 09:01:20 -07005693 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005694 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005695 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005696 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005697 spin_unlock_irq(&ctx->completion_lock);
5698 return -EIOCBQUEUED;
5699}
5700
Jens Axboef573d382020-09-22 10:19:24 -06005701static void io_req_drop_files(struct io_kiocb *req)
5702{
5703 struct io_ring_ctx *ctx = req->ctx;
5704 unsigned long flags;
5705
5706 spin_lock_irqsave(&ctx->inflight_lock, flags);
5707 list_del(&req->inflight_entry);
5708 if (waitqueue_active(&ctx->inflight_wait))
5709 wake_up(&ctx->inflight_wait);
5710 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5711 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005712 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005713 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005714 req->work.files = NULL;
5715}
5716
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005717static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005718{
5719 struct io_async_ctx *io = req->io;
5720
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005721 if (req->flags & REQ_F_BUFFER_SELECTED) {
5722 switch (req->opcode) {
5723 case IORING_OP_READV:
5724 case IORING_OP_READ_FIXED:
5725 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005726 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005727 break;
5728 case IORING_OP_RECVMSG:
5729 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005730 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005731 break;
5732 }
5733 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005734 }
5735
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005736 if (req->flags & REQ_F_NEED_CLEANUP) {
5737 switch (req->opcode) {
5738 case IORING_OP_READV:
5739 case IORING_OP_READ_FIXED:
5740 case IORING_OP_READ:
5741 case IORING_OP_WRITEV:
5742 case IORING_OP_WRITE_FIXED:
5743 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005744 if (io->rw.free_iovec)
5745 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005746 break;
5747 case IORING_OP_RECVMSG:
5748 case IORING_OP_SENDMSG:
5749 if (io->msg.iov != io->msg.fast_iov)
5750 kfree(io->msg.iov);
5751 break;
5752 case IORING_OP_SPLICE:
5753 case IORING_OP_TEE:
5754 io_put_file(req, req->splice.file_in,
5755 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5756 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005757 case IORING_OP_OPENAT:
5758 case IORING_OP_OPENAT2:
5759 if (req->open.filename)
5760 putname(req->open.filename);
5761 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005762 }
5763 req->flags &= ~REQ_F_NEED_CLEANUP;
5764 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005765
Jens Axboef573d382020-09-22 10:19:24 -06005766 if (req->flags & REQ_F_INFLIGHT)
5767 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005768}
5769
Jens Axboe3529d8c2019-12-19 18:24:38 -07005770static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005771 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005772{
Jackie Liua197f662019-11-08 08:09:12 -07005773 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005774 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005775
Jens Axboed625c6e2019-12-17 19:53:05 -07005776 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005777 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005778 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005779 break;
5780 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005781 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005782 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005783 if (sqe) {
5784 ret = io_read_prep(req, sqe, force_nonblock);
5785 if (ret < 0)
5786 break;
5787 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005788 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005789 break;
5790 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005791 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005792 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005793 if (sqe) {
5794 ret = io_write_prep(req, sqe, force_nonblock);
5795 if (ret < 0)
5796 break;
5797 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005798 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005799 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005800 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005801 if (sqe) {
5802 ret = io_prep_fsync(req, sqe);
5803 if (ret < 0)
5804 break;
5805 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005806 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005807 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005808 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005809 if (sqe) {
5810 ret = io_poll_add_prep(req, sqe);
5811 if (ret)
5812 break;
5813 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005814 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005815 break;
5816 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005817 if (sqe) {
5818 ret = io_poll_remove_prep(req, sqe);
5819 if (ret < 0)
5820 break;
5821 }
Jens Axboefc4df992019-12-10 14:38:45 -07005822 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005823 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005824 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005825 if (sqe) {
5826 ret = io_prep_sfr(req, sqe);
5827 if (ret < 0)
5828 break;
5829 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005830 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005831 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005832 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005833 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005834 if (sqe) {
5835 ret = io_sendmsg_prep(req, sqe);
5836 if (ret < 0)
5837 break;
5838 }
Jens Axboefddafac2020-01-04 20:19:44 -07005839 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005840 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005841 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005842 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005843 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005844 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005845 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005846 if (sqe) {
5847 ret = io_recvmsg_prep(req, sqe);
5848 if (ret)
5849 break;
5850 }
Jens Axboefddafac2020-01-04 20:19:44 -07005851 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005852 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005853 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005854 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005855 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005856 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005857 if (sqe) {
5858 ret = io_timeout_prep(req, sqe, false);
5859 if (ret)
5860 break;
5861 }
Jens Axboefc4df992019-12-10 14:38:45 -07005862 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005863 break;
Jens Axboe11365042019-10-16 09:08:32 -06005864 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005865 if (sqe) {
5866 ret = io_timeout_remove_prep(req, sqe);
5867 if (ret)
5868 break;
5869 }
Jens Axboefc4df992019-12-10 14:38:45 -07005870 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005871 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005872 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005873 if (sqe) {
5874 ret = io_accept_prep(req, sqe);
5875 if (ret)
5876 break;
5877 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005878 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005879 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005880 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005881 if (sqe) {
5882 ret = io_connect_prep(req, sqe);
5883 if (ret)
5884 break;
5885 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005886 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005887 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005888 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005889 if (sqe) {
5890 ret = io_async_cancel_prep(req, sqe);
5891 if (ret)
5892 break;
5893 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005894 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005895 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005896 case IORING_OP_FALLOCATE:
5897 if (sqe) {
5898 ret = io_fallocate_prep(req, sqe);
5899 if (ret)
5900 break;
5901 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005902 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005903 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005904 case IORING_OP_OPENAT:
5905 if (sqe) {
5906 ret = io_openat_prep(req, sqe);
5907 if (ret)
5908 break;
5909 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005910 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005911 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005912 case IORING_OP_CLOSE:
5913 if (sqe) {
5914 ret = io_close_prep(req, sqe);
5915 if (ret)
5916 break;
5917 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005918 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005919 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005920 case IORING_OP_FILES_UPDATE:
5921 if (sqe) {
5922 ret = io_files_update_prep(req, sqe);
5923 if (ret)
5924 break;
5925 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005926 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005927 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005928 case IORING_OP_STATX:
5929 if (sqe) {
5930 ret = io_statx_prep(req, sqe);
5931 if (ret)
5932 break;
5933 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005934 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005935 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005936 case IORING_OP_FADVISE:
5937 if (sqe) {
5938 ret = io_fadvise_prep(req, sqe);
5939 if (ret)
5940 break;
5941 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005942 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005943 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005944 case IORING_OP_MADVISE:
5945 if (sqe) {
5946 ret = io_madvise_prep(req, sqe);
5947 if (ret)
5948 break;
5949 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005950 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005951 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005952 case IORING_OP_OPENAT2:
5953 if (sqe) {
5954 ret = io_openat2_prep(req, sqe);
5955 if (ret)
5956 break;
5957 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005958 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005959 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005960 case IORING_OP_EPOLL_CTL:
5961 if (sqe) {
5962 ret = io_epoll_ctl_prep(req, sqe);
5963 if (ret)
5964 break;
5965 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005966 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005967 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005968 case IORING_OP_SPLICE:
5969 if (sqe) {
5970 ret = io_splice_prep(req, sqe);
5971 if (ret < 0)
5972 break;
5973 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005974 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005975 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005976 case IORING_OP_PROVIDE_BUFFERS:
5977 if (sqe) {
5978 ret = io_provide_buffers_prep(req, sqe);
5979 if (ret)
5980 break;
5981 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005982 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005983 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005984 case IORING_OP_REMOVE_BUFFERS:
5985 if (sqe) {
5986 ret = io_remove_buffers_prep(req, sqe);
5987 if (ret)
5988 break;
5989 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005990 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005991 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005992 case IORING_OP_TEE:
5993 if (sqe) {
5994 ret = io_tee_prep(req, sqe);
5995 if (ret < 0)
5996 break;
5997 }
5998 ret = io_tee(req, force_nonblock);
5999 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006000 default:
6001 ret = -EINVAL;
6002 break;
6003 }
6004
6005 if (ret)
6006 return ret;
6007
Jens Axboeb5325762020-05-19 21:20:27 -06006008 /* If the op doesn't have a file, we're not polling for it */
6009 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006010 const bool in_async = io_wq_current_is_worker();
6011
Jens Axboe11ba8202020-01-15 21:51:17 -07006012 /* workqueue context doesn't hold uring_lock, grab it now */
6013 if (in_async)
6014 mutex_lock(&ctx->uring_lock);
6015
Jens Axboe2b188cc2019-01-07 10:46:33 -07006016 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006017
6018 if (in_async)
6019 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006020 }
6021
6022 return 0;
6023}
6024
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006025static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006026{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006027 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006028 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006029 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006030
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006031 timeout = io_prep_linked_timeout(req);
6032 if (timeout)
6033 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006034
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006035 /* if NO_CANCEL is set, we must still run the work */
6036 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6037 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006038 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006039 }
Jens Axboe31b51512019-01-18 22:56:34 -07006040
Jens Axboe561fb042019-10-24 07:25:42 -06006041 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006042 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006043 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006044 /*
6045 * We can get EAGAIN for polled IO even though we're
6046 * forcing a sync submission from here, since we can't
6047 * wait for request slots on the block side.
6048 */
6049 if (ret != -EAGAIN)
6050 break;
6051 cond_resched();
6052 } while (1);
6053 }
Jens Axboe31b51512019-01-18 22:56:34 -07006054
Jens Axboe561fb042019-10-24 07:25:42 -06006055 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006056 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006057 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006058 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006059
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006060 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006061}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006062
Jens Axboe65e19f52019-10-26 07:20:21 -06006063static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6064 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006065{
Jens Axboe65e19f52019-10-26 07:20:21 -06006066 struct fixed_file_table *table;
6067
Jens Axboe05f3fb32019-12-09 11:22:50 -07006068 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006069 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006070}
6071
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006072static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6073 int fd, struct file **out_file, bool fixed)
6074{
6075 struct io_ring_ctx *ctx = req->ctx;
6076 struct file *file;
6077
6078 if (fixed) {
6079 if (unlikely(!ctx->file_data ||
6080 (unsigned) fd >= ctx->nr_user_files))
6081 return -EBADF;
6082 fd = array_index_nospec(fd, ctx->nr_user_files);
6083 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006084 if (file) {
6085 req->fixed_file_refs = ctx->file_data->cur_refs;
6086 percpu_ref_get(req->fixed_file_refs);
6087 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006088 } else {
6089 trace_io_uring_file_get(ctx, fd);
6090 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006091 }
6092
Jens Axboefd2206e2020-06-02 16:40:47 -06006093 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6094 *out_file = file;
6095 return 0;
6096 }
6097 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006098}
6099
Jens Axboe3529d8c2019-12-19 18:24:38 -07006100static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006101 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006102{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006103 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006104
Jens Axboe63ff8222020-05-07 14:56:15 -06006105 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006106 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006107 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006108
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006109 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006110}
6111
Jackie Liua197f662019-11-08 08:09:12 -07006112static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006113{
Jackie Liua197f662019-11-08 08:09:12 -07006114 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006115
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006116 io_req_init_async(req);
6117
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006118 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006119 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006120
Jens Axboe0f212202020-09-13 13:09:39 -06006121 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006122 get_nsproxy(current->nsproxy);
6123 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006124 req->flags |= REQ_F_INFLIGHT;
6125
Jens Axboefcb323c2019-10-24 12:39:47 -06006126 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006127 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006128 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006129 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006130}
6131
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006132static inline int io_prep_work_files(struct io_kiocb *req)
6133{
6134 if (!io_op_defs[req->opcode].file_table)
6135 return 0;
6136 return io_grab_files(req);
6137}
6138
Jens Axboe2665abf2019-11-05 12:40:47 -07006139static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6140{
Jens Axboead8a48a2019-11-15 08:49:11 -07006141 struct io_timeout_data *data = container_of(timer,
6142 struct io_timeout_data, timer);
6143 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006144 struct io_ring_ctx *ctx = req->ctx;
6145 struct io_kiocb *prev = NULL;
6146 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006147
6148 spin_lock_irqsave(&ctx->completion_lock, flags);
6149
6150 /*
6151 * We don't expect the list to be empty, that will only happen if we
6152 * race with the completion of the linked work.
6153 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006154 if (!list_empty(&req->link_list)) {
6155 prev = list_entry(req->link_list.prev, struct io_kiocb,
6156 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006157 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006158 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006159 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6160 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006161 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006162 }
6163
6164 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6165
6166 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006167 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006168 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006169 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006170 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006171 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006172 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006173 return HRTIMER_NORESTART;
6174}
6175
Jens Axboe7271ef32020-08-10 09:55:22 -06006176static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006177{
Jens Axboe76a46e02019-11-10 23:34:16 -07006178 /*
6179 * If the list is now empty, then our linked request finished before
6180 * we got a chance to setup the timer
6181 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006182 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006183 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006184
Jens Axboead8a48a2019-11-15 08:49:11 -07006185 data->timer.function = io_link_timeout_fn;
6186 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6187 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006188 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006189}
6190
6191static void io_queue_linked_timeout(struct io_kiocb *req)
6192{
6193 struct io_ring_ctx *ctx = req->ctx;
6194
6195 spin_lock_irq(&ctx->completion_lock);
6196 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006197 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006198
Jens Axboe2665abf2019-11-05 12:40:47 -07006199 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006200 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006201}
6202
Jens Axboead8a48a2019-11-15 08:49:11 -07006203static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006204{
6205 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006207 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006208 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006209 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006210 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006211
Pavel Begunkov44932332019-12-05 16:16:35 +03006212 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6213 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006214 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006215 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006216
Jens Axboe76a46e02019-11-10 23:34:16 -07006217 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006218 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006219}
6220
Jens Axboef13fad72020-06-22 09:34:30 -06006221static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6222 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006224 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006225 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006226 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006227 int ret;
6228
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006229again:
6230 linked_timeout = io_prep_linked_timeout(req);
6231
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006232 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6233 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006234 if (old_creds)
6235 revert_creds(old_creds);
6236 if (old_creds == req->work.creds)
6237 old_creds = NULL; /* restored original creds */
6238 else
6239 old_creds = override_creds(req->work.creds);
6240 }
6241
Jens Axboef13fad72020-06-22 09:34:30 -06006242 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006243
6244 /*
6245 * We async punt it if the file wasn't marked NOWAIT, or if the file
6246 * doesn't support non-blocking read/write attempts
6247 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006248 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006249 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006250punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006251 ret = io_prep_work_files(req);
6252 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006253 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006254 /*
6255 * Queued up for async execution, worker will release
6256 * submit reference when the iocb is actually submitted.
6257 */
6258 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006259 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006260
Pavel Begunkovf063c542020-07-25 14:41:59 +03006261 if (linked_timeout)
6262 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006263 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006264 }
Jens Axboee65ef562019-03-12 10:16:44 -06006265
Pavel Begunkov652532a2020-07-03 22:15:07 +03006266 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006267err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006268 /* un-prep timeout, so it'll be killed as any other linked */
6269 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006270 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006271 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006272 io_req_complete(req, ret);
6273 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006274 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006275
Jens Axboe6c271ce2019-01-10 11:22:30 -07006276 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006277 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006278 if (linked_timeout)
6279 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006280
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006281 if (nxt) {
6282 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006283
6284 if (req->flags & REQ_F_FORCE_ASYNC)
6285 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006286 goto again;
6287 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006288exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006289 if (old_creds)
6290 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006291}
6292
Jens Axboef13fad72020-06-22 09:34:30 -06006293static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6294 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006295{
6296 int ret;
6297
Jens Axboe3529d8c2019-12-19 18:24:38 -07006298 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006299 if (ret) {
6300 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006301fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006302 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006303 io_put_req(req);
6304 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006305 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006306 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006307 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006308 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006309 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006310 goto fail_req;
6311 }
6312
Jens Axboece35a472019-12-17 08:04:44 -07006313 /*
6314 * Never try inline submit of IOSQE_ASYNC is set, go straight
6315 * to async execution.
6316 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006317 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006318 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6319 io_queue_async_work(req);
6320 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006321 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006322 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006323}
6324
Jens Axboef13fad72020-06-22 09:34:30 -06006325static inline void io_queue_link_head(struct io_kiocb *req,
6326 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006327{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006328 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006329 io_put_req(req);
6330 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006331 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006332 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006333}
6334
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006335static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006336 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006337{
Jackie Liua197f662019-11-08 08:09:12 -07006338 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006339 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006340
Jens Axboe9e645e112019-05-10 16:07:28 -06006341 /*
6342 * If we already have a head request, queue this one for async
6343 * submittal once the head completes. If we don't have a head but
6344 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6345 * submitted sync once the chain is complete. If none of those
6346 * conditions are true (normal request), then just queue it.
6347 */
6348 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006349 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006350
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006351 /*
6352 * Taking sequential execution of a link, draining both sides
6353 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6354 * requests in the link. So, it drains the head and the
6355 * next after the link request. The last one is done via
6356 * drain_next flag to persist the effect across calls.
6357 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006358 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006359 head->flags |= REQ_F_IO_DRAIN;
6360 ctx->drain_next = 1;
6361 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006362 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006363 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006364 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006365 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006366 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006367 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006368 trace_io_uring_link(ctx, req, head);
6369 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006370
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006371 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006372 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006373 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006374 *link = NULL;
6375 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006376 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006377 if (unlikely(ctx->drain_next)) {
6378 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006379 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006380 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006381 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006382 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006383 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006384
Pavel Begunkov711be032020-01-17 03:57:59 +03006385 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006386 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006387 req->flags |= REQ_F_FAIL_LINK;
6388 *link = req;
6389 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006390 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006391 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006392 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006393
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006394 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006395}
6396
Jens Axboe9a56a232019-01-09 09:06:50 -07006397/*
6398 * Batched submission is done, ensure local IO is flushed out.
6399 */
6400static void io_submit_state_end(struct io_submit_state *state)
6401{
Jens Axboef13fad72020-06-22 09:34:30 -06006402 if (!list_empty(&state->comp.list))
6403 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006404 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006405 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006406 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006407 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006408}
6409
6410/*
6411 * Start submission side cache.
6412 */
6413static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006414 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006415{
6416 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006417 state->comp.nr = 0;
6418 INIT_LIST_HEAD(&state->comp.list);
6419 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006420 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006421 state->file = NULL;
6422 state->ios_left = max_ios;
6423}
6424
Jens Axboe2b188cc2019-01-07 10:46:33 -07006425static void io_commit_sqring(struct io_ring_ctx *ctx)
6426{
Hristo Venev75b28af2019-08-26 17:23:46 +00006427 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006428
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006429 /*
6430 * Ensure any loads from the SQEs are done at this point,
6431 * since once we write the new head, the application could
6432 * write new data to them.
6433 */
6434 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006435}
6436
6437/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006438 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006439 * that is mapped by userspace. This means that care needs to be taken to
6440 * ensure that reads are stable, as we cannot rely on userspace always
6441 * being a good citizen. If members of the sqe are validated and then later
6442 * used, it's important that those reads are done through READ_ONCE() to
6443 * prevent a re-load down the line.
6444 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006445static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006446{
Hristo Venev75b28af2019-08-26 17:23:46 +00006447 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006448 unsigned head;
6449
6450 /*
6451 * The cached sq head (or cq tail) serves two purposes:
6452 *
6453 * 1) allows us to batch the cost of updating the user visible
6454 * head updates.
6455 * 2) allows the kernel side to track the head on its own, even
6456 * though the application is the one updating it.
6457 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006458 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006459 if (likely(head < ctx->sq_entries))
6460 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006461
6462 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006463 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006464 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006465 return NULL;
6466}
6467
6468static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6469{
6470 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006471}
6472
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006473/*
6474 * Check SQE restrictions (opcode and flags).
6475 *
6476 * Returns 'true' if SQE is allowed, 'false' otherwise.
6477 */
6478static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6479 struct io_kiocb *req,
6480 unsigned int sqe_flags)
6481{
6482 if (!ctx->restricted)
6483 return true;
6484
6485 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6486 return false;
6487
6488 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6489 ctx->restrictions.sqe_flags_required)
6490 return false;
6491
6492 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6493 ctx->restrictions.sqe_flags_required))
6494 return false;
6495
6496 return true;
6497}
6498
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006499#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6500 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6501 IOSQE_BUFFER_SELECT)
6502
6503static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6504 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006505 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006506{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006507 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006508 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006509
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006510 req->opcode = READ_ONCE(sqe->opcode);
6511 req->user_data = READ_ONCE(sqe->user_data);
6512 req->io = NULL;
6513 req->file = NULL;
6514 req->ctx = ctx;
6515 req->flags = 0;
6516 /* one is dropped after submission, the other at completion */
6517 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006518 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006519 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006520 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006521 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006522
6523 if (unlikely(req->opcode >= IORING_OP_LAST))
6524 return -EINVAL;
6525
Jens Axboe9d8426a2020-06-16 18:42:49 -06006526 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6527 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006528
6529 sqe_flags = READ_ONCE(sqe->flags);
6530 /* enforce forwards compatibility on users */
6531 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6532 return -EINVAL;
6533
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006534 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6535 return -EACCES;
6536
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006537 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6538 !io_op_defs[req->opcode].buffer_select)
6539 return -EOPNOTSUPP;
6540
6541 id = READ_ONCE(sqe->personality);
6542 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006543 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006544 req->work.creds = idr_find(&ctx->personality_idr, id);
6545 if (unlikely(!req->work.creds))
6546 return -EINVAL;
6547 get_cred(req->work.creds);
6548 }
6549
6550 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006551 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006552
Jens Axboe63ff8222020-05-07 14:56:15 -06006553 if (!io_op_defs[req->opcode].needs_file)
6554 return 0;
6555
6556 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006557}
6558
Jens Axboe0f212202020-09-13 13:09:39 -06006559static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006560{
Jens Axboeac8691c2020-06-01 08:30:41 -06006561 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006562 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006563 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006564
Jens Axboec4a2ed72019-11-21 21:01:26 -07006565 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006566 if (test_bit(0, &ctx->sq_check_overflow)) {
6567 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006568 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006569 return -EBUSY;
6570 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006571
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006572 /* make sure SQ entry isn't read before tail */
6573 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006574
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006575 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6576 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006577
Jens Axboe013538b2020-06-22 09:29:15 -06006578 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006579
6580 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006581 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006582 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006583 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006584
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006585 sqe = io_get_sqe(ctx);
6586 if (unlikely(!sqe)) {
6587 io_consume_sqe(ctx);
6588 break;
6589 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006590 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006591 if (unlikely(!req)) {
6592 if (!submitted)
6593 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006594 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006595 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006596
Jens Axboeac8691c2020-06-01 08:30:41 -06006597 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006598 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006599 /* will complete beyond this point, count as submitted */
6600 submitted++;
6601
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006602 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006603fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006604 io_put_req(req);
6605 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006606 break;
6607 }
6608
Jens Axboe354420f2020-01-08 18:55:15 -07006609 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006610 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006611 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006612 if (err)
6613 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006614 }
6615
Pavel Begunkov9466f432020-01-25 22:34:01 +03006616 if (unlikely(submitted != nr)) {
6617 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6618
6619 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6620 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006621 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006622 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006623 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006624
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006625 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6626 io_commit_sqring(ctx);
6627
Jens Axboe6c271ce2019-01-10 11:22:30 -07006628 return submitted;
6629}
6630
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006631static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6632{
6633 /* Tell userspace we may need a wakeup call */
6634 spin_lock_irq(&ctx->completion_lock);
6635 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6636 spin_unlock_irq(&ctx->completion_lock);
6637}
6638
6639static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6640{
6641 spin_lock_irq(&ctx->completion_lock);
6642 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6643 spin_unlock_irq(&ctx->completion_lock);
6644}
6645
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006646static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6647 int sync, void *key)
6648{
6649 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6650 int ret;
6651
6652 ret = autoremove_wake_function(wqe, mode, sync, key);
6653 if (ret) {
6654 unsigned long flags;
6655
6656 spin_lock_irqsave(&ctx->completion_lock, flags);
6657 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6658 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6659 }
6660 return ret;
6661}
6662
Jens Axboec8d1ba52020-09-14 11:07:26 -06006663enum sq_ret {
6664 SQT_IDLE = 1,
6665 SQT_SPIN = 2,
6666 SQT_DID_WORK = 4,
6667};
6668
6669static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
6670 unsigned long start_jiffies)
6671{
6672 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006673 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006674 unsigned int to_submit;
6675 int ret = 0;
6676
6677again:
6678 if (!list_empty(&ctx->iopoll_list)) {
6679 unsigned nr_events = 0;
6680
6681 mutex_lock(&ctx->uring_lock);
6682 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6683 io_do_iopoll(ctx, &nr_events, 0);
6684 mutex_unlock(&ctx->uring_lock);
6685 }
6686
6687 to_submit = io_sqring_entries(ctx);
6688
6689 /*
6690 * If submit got -EBUSY, flag us as needing the application
6691 * to enter the kernel to reap and flush events.
6692 */
6693 if (!to_submit || ret == -EBUSY || need_resched()) {
6694 /*
6695 * Drop cur_mm before scheduling, we can't hold it for
6696 * long periods (or over schedule()). Do this before
6697 * adding ourselves to the waitqueue, as the unuse/drop
6698 * may sleep.
6699 */
6700 io_sq_thread_drop_mm();
6701
6702 /*
6703 * We're polling. If we're within the defined idle
6704 * period, then let us spin without work before going
6705 * to sleep. The exception is if we got EBUSY doing
6706 * more IO, we should wait for the application to
6707 * reap events and wake us up.
6708 */
6709 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6710 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6711 !percpu_ref_is_dying(&ctx->refs)))
6712 return SQT_SPIN;
6713
Jens Axboe534ca6d2020-09-02 13:52:19 -06006714 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006715 TASK_INTERRUPTIBLE);
6716
6717 /*
6718 * While doing polled IO, before going to sleep, we need
6719 * to check if there are new reqs added to iopoll_list,
6720 * it is because reqs may have been punted to io worker
6721 * and will be added to iopoll_list later, hence check
6722 * the iopoll_list again.
6723 */
6724 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6725 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006726 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006727 goto again;
6728 }
6729
Jens Axboec8d1ba52020-09-14 11:07:26 -06006730 to_submit = io_sqring_entries(ctx);
6731 if (!to_submit || ret == -EBUSY)
6732 return SQT_IDLE;
6733 }
6734
Jens Axboe534ca6d2020-09-02 13:52:19 -06006735 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006736 io_ring_clear_wakeup_flag(ctx);
6737
6738 mutex_lock(&ctx->uring_lock);
6739 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6740 ret = io_submit_sqes(ctx, to_submit);
6741 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006742
6743 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6744 wake_up(&ctx->sqo_sq_wait);
6745
Jens Axboec8d1ba52020-09-14 11:07:26 -06006746 return SQT_DID_WORK;
6747}
6748
Jens Axboe69fb2132020-09-14 11:16:23 -06006749static void io_sqd_init_new(struct io_sq_data *sqd)
6750{
6751 struct io_ring_ctx *ctx;
6752
6753 while (!list_empty(&sqd->ctx_new_list)) {
6754 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6755 init_wait(&ctx->sqo_wait_entry);
6756 ctx->sqo_wait_entry.func = io_sq_wake_function;
6757 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6758 complete(&ctx->sq_thread_comp);
6759 }
6760}
6761
Jens Axboe6c271ce2019-01-10 11:22:30 -07006762static int io_sq_thread(void *data)
6763{
Jens Axboe69fb2132020-09-14 11:16:23 -06006764 const struct cred *old_cred = NULL;
6765 struct io_sq_data *sqd = data;
6766 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006767 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006768
Jens Axboec8d1ba52020-09-14 11:07:26 -06006769 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006770 while (!kthread_should_stop()) {
6771 enum sq_ret ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006772
Jens Axboe69fb2132020-09-14 11:16:23 -06006773 /*
6774 * Any changes to the sqd lists are synchronized through the
6775 * kthread parking. This synchronizes the thread vs users,
6776 * the users are synchronized on the sqd->ctx_lock.
6777 */
6778 if (kthread_should_park())
6779 kthread_parkme();
6780
6781 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6782 io_sqd_init_new(sqd);
6783
6784 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6785 if (current->cred != ctx->creds) {
6786 if (old_cred)
6787 revert_creds(old_cred);
6788 old_cred = override_creds(ctx->creds);
6789 }
6790
6791 ret |= __io_sq_thread(ctx, start_jiffies);
6792
6793 io_sq_thread_drop_mm();
6794 }
6795
6796 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006797 io_run_task_work();
6798 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006799 } else if (ret == SQT_IDLE) {
6800 if (kthread_should_park())
6801 continue;
6802 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6803 io_ring_set_wakeup_flag(ctx);
6804 schedule();
6805 start_jiffies = jiffies;
6806 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6807 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006808 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006809 }
6810
Jens Axboe4c6e2772020-07-01 11:29:10 -06006811 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006812
Jens Axboe69fb2132020-09-14 11:16:23 -06006813 if (old_cred)
6814 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006815
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006816 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006817
Jens Axboe6c271ce2019-01-10 11:22:30 -07006818 return 0;
6819}
6820
Jens Axboebda52162019-09-24 13:47:15 -06006821struct io_wait_queue {
6822 struct wait_queue_entry wq;
6823 struct io_ring_ctx *ctx;
6824 unsigned to_wait;
6825 unsigned nr_timeouts;
6826};
6827
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006828static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006829{
6830 struct io_ring_ctx *ctx = iowq->ctx;
6831
6832 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006833 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006834 * started waiting. For timeouts, we always want to return to userspace,
6835 * regardless of event count.
6836 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006837 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006838 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6839}
6840
6841static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6842 int wake_flags, void *key)
6843{
6844 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6845 wq);
6846
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006847 /* use noflush == true, as we can't safely rely on locking context */
6848 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006849 return -1;
6850
6851 return autoremove_wake_function(curr, mode, wake_flags, key);
6852}
6853
Jens Axboe2b188cc2019-01-07 10:46:33 -07006854/*
6855 * Wait until events become available, if we don't already have some. The
6856 * application must reap them itself, as they reside on the shared cq ring.
6857 */
6858static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6859 const sigset_t __user *sig, size_t sigsz)
6860{
Jens Axboebda52162019-09-24 13:47:15 -06006861 struct io_wait_queue iowq = {
6862 .wq = {
6863 .private = current,
6864 .func = io_wake_function,
6865 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6866 },
6867 .ctx = ctx,
6868 .to_wait = min_events,
6869 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006870 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006871 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006872
Jens Axboeb41e9852020-02-17 09:52:41 -07006873 do {
6874 if (io_cqring_events(ctx, false) >= min_events)
6875 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006876 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006877 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006878 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006879
6880 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006881#ifdef CONFIG_COMPAT
6882 if (in_compat_syscall())
6883 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006884 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006885 else
6886#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006887 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006888
Jens Axboe2b188cc2019-01-07 10:46:33 -07006889 if (ret)
6890 return ret;
6891 }
6892
Jens Axboebda52162019-09-24 13:47:15 -06006893 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006894 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006895 do {
6896 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6897 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006898 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006899 if (io_run_task_work())
6900 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006901 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006902 if (current->jobctl & JOBCTL_TASK_WORK) {
6903 spin_lock_irq(&current->sighand->siglock);
6904 current->jobctl &= ~JOBCTL_TASK_WORK;
6905 recalc_sigpending();
6906 spin_unlock_irq(&current->sighand->siglock);
6907 continue;
6908 }
6909 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006910 break;
6911 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006912 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006913 break;
6914 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006915 } while (1);
6916 finish_wait(&ctx->wait, &iowq.wq);
6917
Jens Axboeb7db41c2020-07-04 08:55:50 -06006918 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006919
Hristo Venev75b28af2019-08-26 17:23:46 +00006920 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006921}
6922
Jens Axboe6b063142019-01-10 22:13:58 -07006923static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6924{
6925#if defined(CONFIG_UNIX)
6926 if (ctx->ring_sock) {
6927 struct sock *sock = ctx->ring_sock->sk;
6928 struct sk_buff *skb;
6929
6930 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6931 kfree_skb(skb);
6932 }
6933#else
6934 int i;
6935
Jens Axboe65e19f52019-10-26 07:20:21 -06006936 for (i = 0; i < ctx->nr_user_files; i++) {
6937 struct file *file;
6938
6939 file = io_file_from_index(ctx, i);
6940 if (file)
6941 fput(file);
6942 }
Jens Axboe6b063142019-01-10 22:13:58 -07006943#endif
6944}
6945
Jens Axboe05f3fb32019-12-09 11:22:50 -07006946static void io_file_ref_kill(struct percpu_ref *ref)
6947{
6948 struct fixed_file_data *data;
6949
6950 data = container_of(ref, struct fixed_file_data, refs);
6951 complete(&data->done);
6952}
6953
Jens Axboe6b063142019-01-10 22:13:58 -07006954static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6955{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006956 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006957 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006958 unsigned nr_tables, i;
6959
Jens Axboe05f3fb32019-12-09 11:22:50 -07006960 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006961 return -ENXIO;
6962
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006963 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006964 if (!list_empty(&data->ref_list))
6965 ref_node = list_first_entry(&data->ref_list,
6966 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006967 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006968 if (ref_node)
6969 percpu_ref_kill(&ref_node->refs);
6970
6971 percpu_ref_kill(&data->refs);
6972
6973 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006974 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006975 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006976
Jens Axboe6b063142019-01-10 22:13:58 -07006977 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006978 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6979 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006980 kfree(data->table[i].files);
6981 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006982 percpu_ref_exit(&data->refs);
6983 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006984 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006985 ctx->nr_user_files = 0;
6986 return 0;
6987}
6988
Jens Axboe534ca6d2020-09-02 13:52:19 -06006989static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006990{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006991 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006992 /*
6993 * The park is a bit of a work-around, without it we get
6994 * warning spews on shutdown with SQPOLL set and affinity
6995 * set to a single CPU.
6996 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06006997 if (sqd->thread) {
6998 kthread_park(sqd->thread);
6999 kthread_stop(sqd->thread);
7000 }
7001
7002 kfree(sqd);
7003 }
7004}
7005
Jens Axboeaa061652020-09-02 14:50:27 -06007006static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7007{
7008 struct io_ring_ctx *ctx_attach;
7009 struct io_sq_data *sqd;
7010 struct fd f;
7011
7012 f = fdget(p->wq_fd);
7013 if (!f.file)
7014 return ERR_PTR(-ENXIO);
7015 if (f.file->f_op != &io_uring_fops) {
7016 fdput(f);
7017 return ERR_PTR(-EINVAL);
7018 }
7019
7020 ctx_attach = f.file->private_data;
7021 sqd = ctx_attach->sq_data;
7022 if (!sqd) {
7023 fdput(f);
7024 return ERR_PTR(-EINVAL);
7025 }
7026
7027 refcount_inc(&sqd->refs);
7028 fdput(f);
7029 return sqd;
7030}
7031
Jens Axboe534ca6d2020-09-02 13:52:19 -06007032static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7033{
7034 struct io_sq_data *sqd;
7035
Jens Axboeaa061652020-09-02 14:50:27 -06007036 if (p->flags & IORING_SETUP_ATTACH_WQ)
7037 return io_attach_sq_data(p);
7038
Jens Axboe534ca6d2020-09-02 13:52:19 -06007039 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7040 if (!sqd)
7041 return ERR_PTR(-ENOMEM);
7042
7043 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007044 INIT_LIST_HEAD(&sqd->ctx_list);
7045 INIT_LIST_HEAD(&sqd->ctx_new_list);
7046 mutex_init(&sqd->ctx_lock);
7047 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007048 init_waitqueue_head(&sqd->wait);
7049 return sqd;
7050}
7051
Jens Axboe69fb2132020-09-14 11:16:23 -06007052static void io_sq_thread_unpark(struct io_sq_data *sqd)
7053 __releases(&sqd->lock)
7054{
7055 if (!sqd->thread)
7056 return;
7057 kthread_unpark(sqd->thread);
7058 mutex_unlock(&sqd->lock);
7059}
7060
7061static void io_sq_thread_park(struct io_sq_data *sqd)
7062 __acquires(&sqd->lock)
7063{
7064 if (!sqd->thread)
7065 return;
7066 mutex_lock(&sqd->lock);
7067 kthread_park(sqd->thread);
7068}
7069
Jens Axboe534ca6d2020-09-02 13:52:19 -06007070static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7071{
7072 struct io_sq_data *sqd = ctx->sq_data;
7073
7074 if (sqd) {
7075 if (sqd->thread) {
7076 /*
7077 * We may arrive here from the error branch in
7078 * io_sq_offload_create() where the kthread is created
7079 * without being waked up, thus wake it up now to make
7080 * sure the wait will complete.
7081 */
7082 wake_up_process(sqd->thread);
7083 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007084
7085 io_sq_thread_park(sqd);
7086 }
7087
7088 mutex_lock(&sqd->ctx_lock);
7089 list_del(&ctx->sqd_list);
7090 mutex_unlock(&sqd->ctx_lock);
7091
7092 if (sqd->thread) {
7093 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7094 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007095 }
7096
7097 io_put_sq_data(sqd);
7098 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007099 }
7100}
7101
Jens Axboe6b063142019-01-10 22:13:58 -07007102static void io_finish_async(struct io_ring_ctx *ctx)
7103{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007104 io_sq_thread_stop(ctx);
7105
Jens Axboe561fb042019-10-24 07:25:42 -06007106 if (ctx->io_wq) {
7107 io_wq_destroy(ctx->io_wq);
7108 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007109 }
7110}
7111
7112#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007113/*
7114 * Ensure the UNIX gc is aware of our file set, so we are certain that
7115 * the io_uring can be safely unregistered on process exit, even if we have
7116 * loops in the file referencing.
7117 */
7118static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7119{
7120 struct sock *sk = ctx->ring_sock->sk;
7121 struct scm_fp_list *fpl;
7122 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007123 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007124
Jens Axboe6b063142019-01-10 22:13:58 -07007125 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7126 if (!fpl)
7127 return -ENOMEM;
7128
7129 skb = alloc_skb(0, GFP_KERNEL);
7130 if (!skb) {
7131 kfree(fpl);
7132 return -ENOMEM;
7133 }
7134
7135 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007136
Jens Axboe08a45172019-10-03 08:11:03 -06007137 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007138 fpl->user = get_uid(ctx->user);
7139 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007140 struct file *file = io_file_from_index(ctx, i + offset);
7141
7142 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007143 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007144 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007145 unix_inflight(fpl->user, fpl->fp[nr_files]);
7146 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007147 }
7148
Jens Axboe08a45172019-10-03 08:11:03 -06007149 if (nr_files) {
7150 fpl->max = SCM_MAX_FD;
7151 fpl->count = nr_files;
7152 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007153 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007154 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7155 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007156
Jens Axboe08a45172019-10-03 08:11:03 -06007157 for (i = 0; i < nr_files; i++)
7158 fput(fpl->fp[i]);
7159 } else {
7160 kfree_skb(skb);
7161 kfree(fpl);
7162 }
Jens Axboe6b063142019-01-10 22:13:58 -07007163
7164 return 0;
7165}
7166
7167/*
7168 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7169 * causes regular reference counting to break down. We rely on the UNIX
7170 * garbage collection to take care of this problem for us.
7171 */
7172static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7173{
7174 unsigned left, total;
7175 int ret = 0;
7176
7177 total = 0;
7178 left = ctx->nr_user_files;
7179 while (left) {
7180 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007181
7182 ret = __io_sqe_files_scm(ctx, this_files, total);
7183 if (ret)
7184 break;
7185 left -= this_files;
7186 total += this_files;
7187 }
7188
7189 if (!ret)
7190 return 0;
7191
7192 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007193 struct file *file = io_file_from_index(ctx, total);
7194
7195 if (file)
7196 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007197 total++;
7198 }
7199
7200 return ret;
7201}
7202#else
7203static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7204{
7205 return 0;
7206}
7207#endif
7208
Jens Axboe65e19f52019-10-26 07:20:21 -06007209static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7210 unsigned nr_files)
7211{
7212 int i;
7213
7214 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007215 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007216 unsigned this_files;
7217
7218 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7219 table->files = kcalloc(this_files, sizeof(struct file *),
7220 GFP_KERNEL);
7221 if (!table->files)
7222 break;
7223 nr_files -= this_files;
7224 }
7225
7226 if (i == nr_tables)
7227 return 0;
7228
7229 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007230 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007231 kfree(table->files);
7232 }
7233 return 1;
7234}
7235
Jens Axboe05f3fb32019-12-09 11:22:50 -07007236static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007237{
7238#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007239 struct sock *sock = ctx->ring_sock->sk;
7240 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7241 struct sk_buff *skb;
7242 int i;
7243
7244 __skb_queue_head_init(&list);
7245
7246 /*
7247 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7248 * remove this entry and rearrange the file array.
7249 */
7250 skb = skb_dequeue(head);
7251 while (skb) {
7252 struct scm_fp_list *fp;
7253
7254 fp = UNIXCB(skb).fp;
7255 for (i = 0; i < fp->count; i++) {
7256 int left;
7257
7258 if (fp->fp[i] != file)
7259 continue;
7260
7261 unix_notinflight(fp->user, fp->fp[i]);
7262 left = fp->count - 1 - i;
7263 if (left) {
7264 memmove(&fp->fp[i], &fp->fp[i + 1],
7265 left * sizeof(struct file *));
7266 }
7267 fp->count--;
7268 if (!fp->count) {
7269 kfree_skb(skb);
7270 skb = NULL;
7271 } else {
7272 __skb_queue_tail(&list, skb);
7273 }
7274 fput(file);
7275 file = NULL;
7276 break;
7277 }
7278
7279 if (!file)
7280 break;
7281
7282 __skb_queue_tail(&list, skb);
7283
7284 skb = skb_dequeue(head);
7285 }
7286
7287 if (skb_peek(&list)) {
7288 spin_lock_irq(&head->lock);
7289 while ((skb = __skb_dequeue(&list)) != NULL)
7290 __skb_queue_tail(head, skb);
7291 spin_unlock_irq(&head->lock);
7292 }
7293#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007294 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007295#endif
7296}
7297
Jens Axboe05f3fb32019-12-09 11:22:50 -07007298struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007299 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007300 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007301};
7302
Jens Axboe4a38aed22020-05-14 17:21:15 -06007303static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007304{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007305 struct fixed_file_data *file_data = ref_node->file_data;
7306 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007307 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007308
7309 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007310 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007311 io_ring_file_put(ctx, pfile->file);
7312 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007313 }
7314
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007315 spin_lock(&file_data->lock);
7316 list_del(&ref_node->node);
7317 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007318
Xiaoguang Wang05589552020-03-31 14:05:18 +08007319 percpu_ref_exit(&ref_node->refs);
7320 kfree(ref_node);
7321 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007322}
7323
Jens Axboe4a38aed22020-05-14 17:21:15 -06007324static void io_file_put_work(struct work_struct *work)
7325{
7326 struct io_ring_ctx *ctx;
7327 struct llist_node *node;
7328
7329 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7330 node = llist_del_all(&ctx->file_put_llist);
7331
7332 while (node) {
7333 struct fixed_file_ref_node *ref_node;
7334 struct llist_node *next = node->next;
7335
7336 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7337 __io_file_put_work(ref_node);
7338 node = next;
7339 }
7340}
7341
Jens Axboe05f3fb32019-12-09 11:22:50 -07007342static void io_file_data_ref_zero(struct percpu_ref *ref)
7343{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007344 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007345 struct io_ring_ctx *ctx;
7346 bool first_add;
7347 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007348
Xiaoguang Wang05589552020-03-31 14:05:18 +08007349 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007350 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007351
Jens Axboe4a38aed22020-05-14 17:21:15 -06007352 if (percpu_ref_is_dying(&ctx->file_data->refs))
7353 delay = 0;
7354
7355 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7356 if (!delay)
7357 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7358 else if (first_add)
7359 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007360}
7361
7362static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7363 struct io_ring_ctx *ctx)
7364{
7365 struct fixed_file_ref_node *ref_node;
7366
7367 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7368 if (!ref_node)
7369 return ERR_PTR(-ENOMEM);
7370
7371 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7372 0, GFP_KERNEL)) {
7373 kfree(ref_node);
7374 return ERR_PTR(-ENOMEM);
7375 }
7376 INIT_LIST_HEAD(&ref_node->node);
7377 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007378 ref_node->file_data = ctx->file_data;
7379 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007380}
7381
7382static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7383{
7384 percpu_ref_exit(&ref_node->refs);
7385 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007386}
7387
7388static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7389 unsigned nr_args)
7390{
7391 __s32 __user *fds = (__s32 __user *) arg;
7392 unsigned nr_tables;
7393 struct file *file;
7394 int fd, ret = 0;
7395 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007396 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007397
7398 if (ctx->file_data)
7399 return -EBUSY;
7400 if (!nr_args)
7401 return -EINVAL;
7402 if (nr_args > IORING_MAX_FIXED_FILES)
7403 return -EMFILE;
7404
7405 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7406 if (!ctx->file_data)
7407 return -ENOMEM;
7408 ctx->file_data->ctx = ctx;
7409 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007410 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007411 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007412
7413 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7414 ctx->file_data->table = kcalloc(nr_tables,
7415 sizeof(struct fixed_file_table),
7416 GFP_KERNEL);
7417 if (!ctx->file_data->table) {
7418 kfree(ctx->file_data);
7419 ctx->file_data = NULL;
7420 return -ENOMEM;
7421 }
7422
Xiaoguang Wang05589552020-03-31 14:05:18 +08007423 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007424 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7425 kfree(ctx->file_data->table);
7426 kfree(ctx->file_data);
7427 ctx->file_data = NULL;
7428 return -ENOMEM;
7429 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007430
7431 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7432 percpu_ref_exit(&ctx->file_data->refs);
7433 kfree(ctx->file_data->table);
7434 kfree(ctx->file_data);
7435 ctx->file_data = NULL;
7436 return -ENOMEM;
7437 }
7438
7439 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7440 struct fixed_file_table *table;
7441 unsigned index;
7442
7443 ret = -EFAULT;
7444 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7445 break;
7446 /* allow sparse sets */
7447 if (fd == -1) {
7448 ret = 0;
7449 continue;
7450 }
7451
7452 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7453 index = i & IORING_FILE_TABLE_MASK;
7454 file = fget(fd);
7455
7456 ret = -EBADF;
7457 if (!file)
7458 break;
7459
7460 /*
7461 * Don't allow io_uring instances to be registered. If UNIX
7462 * isn't enabled, then this causes a reference cycle and this
7463 * instance can never get freed. If UNIX is enabled we'll
7464 * handle it just fine, but there's still no point in allowing
7465 * a ring fd as it doesn't support regular read/write anyway.
7466 */
7467 if (file->f_op == &io_uring_fops) {
7468 fput(file);
7469 break;
7470 }
7471 ret = 0;
7472 table->files[index] = file;
7473 }
7474
7475 if (ret) {
7476 for (i = 0; i < ctx->nr_user_files; i++) {
7477 file = io_file_from_index(ctx, i);
7478 if (file)
7479 fput(file);
7480 }
7481 for (i = 0; i < nr_tables; i++)
7482 kfree(ctx->file_data->table[i].files);
7483
Yang Yingliang667e57d2020-07-10 14:14:20 +00007484 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007485 kfree(ctx->file_data->table);
7486 kfree(ctx->file_data);
7487 ctx->file_data = NULL;
7488 ctx->nr_user_files = 0;
7489 return ret;
7490 }
7491
7492 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007493 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007494 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007495 return ret;
7496 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007497
Xiaoguang Wang05589552020-03-31 14:05:18 +08007498 ref_node = alloc_fixed_file_ref_node(ctx);
7499 if (IS_ERR(ref_node)) {
7500 io_sqe_files_unregister(ctx);
7501 return PTR_ERR(ref_node);
7502 }
7503
7504 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007505 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007506 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007507 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007508 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007509 return ret;
7510}
7511
Jens Axboec3a31e62019-10-03 13:59:56 -06007512static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7513 int index)
7514{
7515#if defined(CONFIG_UNIX)
7516 struct sock *sock = ctx->ring_sock->sk;
7517 struct sk_buff_head *head = &sock->sk_receive_queue;
7518 struct sk_buff *skb;
7519
7520 /*
7521 * See if we can merge this file into an existing skb SCM_RIGHTS
7522 * file set. If there's no room, fall back to allocating a new skb
7523 * and filling it in.
7524 */
7525 spin_lock_irq(&head->lock);
7526 skb = skb_peek(head);
7527 if (skb) {
7528 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7529
7530 if (fpl->count < SCM_MAX_FD) {
7531 __skb_unlink(skb, head);
7532 spin_unlock_irq(&head->lock);
7533 fpl->fp[fpl->count] = get_file(file);
7534 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7535 fpl->count++;
7536 spin_lock_irq(&head->lock);
7537 __skb_queue_head(head, skb);
7538 } else {
7539 skb = NULL;
7540 }
7541 }
7542 spin_unlock_irq(&head->lock);
7543
7544 if (skb) {
7545 fput(file);
7546 return 0;
7547 }
7548
7549 return __io_sqe_files_scm(ctx, 1, index);
7550#else
7551 return 0;
7552#endif
7553}
7554
Hillf Dantona5318d32020-03-23 17:47:15 +08007555static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007556 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007557{
Hillf Dantona5318d32020-03-23 17:47:15 +08007558 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007559 struct percpu_ref *refs = data->cur_refs;
7560 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007561
Jens Axboe05f3fb32019-12-09 11:22:50 -07007562 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007563 if (!pfile)
7564 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007565
Xiaoguang Wang05589552020-03-31 14:05:18 +08007566 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007567 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007568 list_add(&pfile->list, &ref_node->file_list);
7569
Hillf Dantona5318d32020-03-23 17:47:15 +08007570 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007571}
7572
7573static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7574 struct io_uring_files_update *up,
7575 unsigned nr_args)
7576{
7577 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007578 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007579 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007580 __s32 __user *fds;
7581 int fd, i, err;
7582 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007583 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007584
Jens Axboe05f3fb32019-12-09 11:22:50 -07007585 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007586 return -EOVERFLOW;
7587 if (done > ctx->nr_user_files)
7588 return -EINVAL;
7589
Xiaoguang Wang05589552020-03-31 14:05:18 +08007590 ref_node = alloc_fixed_file_ref_node(ctx);
7591 if (IS_ERR(ref_node))
7592 return PTR_ERR(ref_node);
7593
Jens Axboec3a31e62019-10-03 13:59:56 -06007594 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007595 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007596 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007597 struct fixed_file_table *table;
7598 unsigned index;
7599
Jens Axboec3a31e62019-10-03 13:59:56 -06007600 err = 0;
7601 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7602 err = -EFAULT;
7603 break;
7604 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007605 i = array_index_nospec(up->offset, ctx->nr_user_files);
7606 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007607 index = i & IORING_FILE_TABLE_MASK;
7608 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007609 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007610 err = io_queue_file_removal(data, file);
7611 if (err)
7612 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007613 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007614 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007615 }
7616 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007617 file = fget(fd);
7618 if (!file) {
7619 err = -EBADF;
7620 break;
7621 }
7622 /*
7623 * Don't allow io_uring instances to be registered. If
7624 * UNIX isn't enabled, then this causes a reference
7625 * cycle and this instance can never get freed. If UNIX
7626 * is enabled we'll handle it just fine, but there's
7627 * still no point in allowing a ring fd as it doesn't
7628 * support regular read/write anyway.
7629 */
7630 if (file->f_op == &io_uring_fops) {
7631 fput(file);
7632 err = -EBADF;
7633 break;
7634 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007635 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007636 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007637 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007638 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007639 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007640 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007641 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007642 }
7643 nr_args--;
7644 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007645 up->offset++;
7646 }
7647
Xiaoguang Wang05589552020-03-31 14:05:18 +08007648 if (needs_switch) {
7649 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007650 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007651 list_add(&ref_node->node, &data->ref_list);
7652 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007653 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007654 percpu_ref_get(&ctx->file_data->refs);
7655 } else
7656 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007657
7658 return done ? done : err;
7659}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007660
Jens Axboe05f3fb32019-12-09 11:22:50 -07007661static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7662 unsigned nr_args)
7663{
7664 struct io_uring_files_update up;
7665
7666 if (!ctx->file_data)
7667 return -ENXIO;
7668 if (!nr_args)
7669 return -EINVAL;
7670 if (copy_from_user(&up, arg, sizeof(up)))
7671 return -EFAULT;
7672 if (up.resv)
7673 return -EINVAL;
7674
7675 return __io_sqe_files_update(ctx, &up, nr_args);
7676}
Jens Axboec3a31e62019-10-03 13:59:56 -06007677
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007678static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007679{
7680 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7681
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007682 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007683 io_put_req(req);
7684}
7685
Pavel Begunkov24369c22020-01-28 03:15:48 +03007686static int io_init_wq_offload(struct io_ring_ctx *ctx,
7687 struct io_uring_params *p)
7688{
7689 struct io_wq_data data;
7690 struct fd f;
7691 struct io_ring_ctx *ctx_attach;
7692 unsigned int concurrency;
7693 int ret = 0;
7694
7695 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007696 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007697 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007698
7699 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7700 /* Do QD, or 4 * CPUS, whatever is smallest */
7701 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7702
7703 ctx->io_wq = io_wq_create(concurrency, &data);
7704 if (IS_ERR(ctx->io_wq)) {
7705 ret = PTR_ERR(ctx->io_wq);
7706 ctx->io_wq = NULL;
7707 }
7708 return ret;
7709 }
7710
7711 f = fdget(p->wq_fd);
7712 if (!f.file)
7713 return -EBADF;
7714
7715 if (f.file->f_op != &io_uring_fops) {
7716 ret = -EINVAL;
7717 goto out_fput;
7718 }
7719
7720 ctx_attach = f.file->private_data;
7721 /* @io_wq is protected by holding the fd */
7722 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7723 ret = -EINVAL;
7724 goto out_fput;
7725 }
7726
7727 ctx->io_wq = ctx_attach->io_wq;
7728out_fput:
7729 fdput(f);
7730 return ret;
7731}
7732
Jens Axboe0f212202020-09-13 13:09:39 -06007733static int io_uring_alloc_task_context(struct task_struct *task)
7734{
7735 struct io_uring_task *tctx;
7736
7737 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7738 if (unlikely(!tctx))
7739 return -ENOMEM;
7740
7741 xa_init(&tctx->xa);
7742 init_waitqueue_head(&tctx->wait);
7743 tctx->last = NULL;
7744 tctx->in_idle = 0;
7745 atomic_long_set(&tctx->req_issue, 0);
7746 atomic_long_set(&tctx->req_complete, 0);
7747 task->io_uring = tctx;
7748 return 0;
7749}
7750
7751void __io_uring_free(struct task_struct *tsk)
7752{
7753 struct io_uring_task *tctx = tsk->io_uring;
7754
7755 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7756 xa_destroy(&tctx->xa);
7757 kfree(tctx);
7758 tsk->io_uring = NULL;
7759}
7760
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007761static int io_sq_offload_create(struct io_ring_ctx *ctx,
7762 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007763{
7764 int ret;
7765
Jens Axboe6c271ce2019-01-10 11:22:30 -07007766 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007767 struct io_sq_data *sqd;
7768
Jens Axboe3ec482d2019-04-08 10:51:01 -06007769 ret = -EPERM;
7770 if (!capable(CAP_SYS_ADMIN))
7771 goto err;
7772
Jens Axboe534ca6d2020-09-02 13:52:19 -06007773 sqd = io_get_sq_data(p);
7774 if (IS_ERR(sqd)) {
7775 ret = PTR_ERR(sqd);
7776 goto err;
7777 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007778
Jens Axboe534ca6d2020-09-02 13:52:19 -06007779 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007780 io_sq_thread_park(sqd);
7781 mutex_lock(&sqd->ctx_lock);
7782 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7783 mutex_unlock(&sqd->ctx_lock);
7784 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007785
Jens Axboe917257d2019-04-13 09:28:55 -06007786 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7787 if (!ctx->sq_thread_idle)
7788 ctx->sq_thread_idle = HZ;
7789
Jens Axboeaa061652020-09-02 14:50:27 -06007790 if (sqd->thread)
7791 goto done;
7792
Jens Axboe6c271ce2019-01-10 11:22:30 -07007793 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007794 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007795
Jens Axboe917257d2019-04-13 09:28:55 -06007796 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007797 if (cpu >= nr_cpu_ids)
7798 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007799 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007800 goto err;
7801
Jens Axboe69fb2132020-09-14 11:16:23 -06007802 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007803 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007804 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007805 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007806 "io_uring-sq");
7807 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007808 if (IS_ERR(sqd->thread)) {
7809 ret = PTR_ERR(sqd->thread);
7810 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007811 goto err;
7812 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007813 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007814 if (ret)
7815 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007816 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7817 /* Can't have SQ_AFF without SQPOLL */
7818 ret = -EINVAL;
7819 goto err;
7820 }
7821
Jens Axboeaa061652020-09-02 14:50:27 -06007822done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007823 ret = io_init_wq_offload(ctx, p);
7824 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007825 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007826
7827 return 0;
7828err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007829 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007830 return ret;
7831}
7832
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007833static void io_sq_offload_start(struct io_ring_ctx *ctx)
7834{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007835 struct io_sq_data *sqd = ctx->sq_data;
7836
7837 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7838 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007839}
7840
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007841static inline void __io_unaccount_mem(struct user_struct *user,
7842 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007843{
7844 atomic_long_sub(nr_pages, &user->locked_vm);
7845}
7846
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007847static inline int __io_account_mem(struct user_struct *user,
7848 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007849{
7850 unsigned long page_limit, cur_pages, new_pages;
7851
7852 /* Don't allow more pages than we can safely lock */
7853 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7854
7855 do {
7856 cur_pages = atomic_long_read(&user->locked_vm);
7857 new_pages = cur_pages + nr_pages;
7858 if (new_pages > page_limit)
7859 return -ENOMEM;
7860 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7861 new_pages) != cur_pages);
7862
7863 return 0;
7864}
7865
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007866static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7867 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007868{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007869 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007870 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007871
Jens Axboe2aede0e2020-09-14 10:45:53 -06007872 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007873 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007874 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007875 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007876 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007877 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007878}
7879
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007880static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7881 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007882{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007883 int ret;
7884
7885 if (ctx->limit_mem) {
7886 ret = __io_account_mem(ctx->user, nr_pages);
7887 if (ret)
7888 return ret;
7889 }
7890
Jens Axboe2aede0e2020-09-14 10:45:53 -06007891 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007892 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007893 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007894 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007895 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007896 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007897
7898 return 0;
7899}
7900
Jens Axboe2b188cc2019-01-07 10:46:33 -07007901static void io_mem_free(void *ptr)
7902{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007903 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007904
Mark Rutland52e04ef2019-04-30 17:30:21 +01007905 if (!ptr)
7906 return;
7907
7908 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007909 if (put_page_testzero(page))
7910 free_compound_page(page);
7911}
7912
7913static void *io_mem_alloc(size_t size)
7914{
7915 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7916 __GFP_NORETRY;
7917
7918 return (void *) __get_free_pages(gfp_flags, get_order(size));
7919}
7920
Hristo Venev75b28af2019-08-26 17:23:46 +00007921static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7922 size_t *sq_offset)
7923{
7924 struct io_rings *rings;
7925 size_t off, sq_array_size;
7926
7927 off = struct_size(rings, cqes, cq_entries);
7928 if (off == SIZE_MAX)
7929 return SIZE_MAX;
7930
7931#ifdef CONFIG_SMP
7932 off = ALIGN(off, SMP_CACHE_BYTES);
7933 if (off == 0)
7934 return SIZE_MAX;
7935#endif
7936
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007937 if (sq_offset)
7938 *sq_offset = off;
7939
Hristo Venev75b28af2019-08-26 17:23:46 +00007940 sq_array_size = array_size(sizeof(u32), sq_entries);
7941 if (sq_array_size == SIZE_MAX)
7942 return SIZE_MAX;
7943
7944 if (check_add_overflow(off, sq_array_size, &off))
7945 return SIZE_MAX;
7946
Hristo Venev75b28af2019-08-26 17:23:46 +00007947 return off;
7948}
7949
Jens Axboe2b188cc2019-01-07 10:46:33 -07007950static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7951{
Hristo Venev75b28af2019-08-26 17:23:46 +00007952 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007953
Hristo Venev75b28af2019-08-26 17:23:46 +00007954 pages = (size_t)1 << get_order(
7955 rings_size(sq_entries, cq_entries, NULL));
7956 pages += (size_t)1 << get_order(
7957 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007958
Hristo Venev75b28af2019-08-26 17:23:46 +00007959 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007960}
7961
Jens Axboeedafcce2019-01-09 09:16:05 -07007962static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7963{
7964 int i, j;
7965
7966 if (!ctx->user_bufs)
7967 return -ENXIO;
7968
7969 for (i = 0; i < ctx->nr_user_bufs; i++) {
7970 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7971
7972 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007973 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007974
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007975 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007976 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007977 imu->nr_bvecs = 0;
7978 }
7979
7980 kfree(ctx->user_bufs);
7981 ctx->user_bufs = NULL;
7982 ctx->nr_user_bufs = 0;
7983 return 0;
7984}
7985
7986static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7987 void __user *arg, unsigned index)
7988{
7989 struct iovec __user *src;
7990
7991#ifdef CONFIG_COMPAT
7992 if (ctx->compat) {
7993 struct compat_iovec __user *ciovs;
7994 struct compat_iovec ciov;
7995
7996 ciovs = (struct compat_iovec __user *) arg;
7997 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7998 return -EFAULT;
7999
Jens Axboed55e5f52019-12-11 16:12:15 -07008000 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008001 dst->iov_len = ciov.iov_len;
8002 return 0;
8003 }
8004#endif
8005 src = (struct iovec __user *) arg;
8006 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8007 return -EFAULT;
8008 return 0;
8009}
8010
8011static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8012 unsigned nr_args)
8013{
8014 struct vm_area_struct **vmas = NULL;
8015 struct page **pages = NULL;
8016 int i, j, got_pages = 0;
8017 int ret = -EINVAL;
8018
8019 if (ctx->user_bufs)
8020 return -EBUSY;
8021 if (!nr_args || nr_args > UIO_MAXIOV)
8022 return -EINVAL;
8023
8024 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8025 GFP_KERNEL);
8026 if (!ctx->user_bufs)
8027 return -ENOMEM;
8028
8029 for (i = 0; i < nr_args; i++) {
8030 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8031 unsigned long off, start, end, ubuf;
8032 int pret, nr_pages;
8033 struct iovec iov;
8034 size_t size;
8035
8036 ret = io_copy_iov(ctx, &iov, arg, i);
8037 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008038 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008039
8040 /*
8041 * Don't impose further limits on the size and buffer
8042 * constraints here, we'll -EINVAL later when IO is
8043 * submitted if they are wrong.
8044 */
8045 ret = -EFAULT;
8046 if (!iov.iov_base || !iov.iov_len)
8047 goto err;
8048
8049 /* arbitrary limit, but we need something */
8050 if (iov.iov_len > SZ_1G)
8051 goto err;
8052
8053 ubuf = (unsigned long) iov.iov_base;
8054 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8055 start = ubuf >> PAGE_SHIFT;
8056 nr_pages = end - start;
8057
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008058 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008059 if (ret)
8060 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008061
8062 ret = 0;
8063 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008064 kvfree(vmas);
8065 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008066 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008067 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008068 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008069 sizeof(struct vm_area_struct *),
8070 GFP_KERNEL);
8071 if (!pages || !vmas) {
8072 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008073 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008074 goto err;
8075 }
8076 got_pages = nr_pages;
8077 }
8078
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008079 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008080 GFP_KERNEL);
8081 ret = -ENOMEM;
8082 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008083 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008084 goto err;
8085 }
8086
8087 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008088 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008089 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008090 FOLL_WRITE | FOLL_LONGTERM,
8091 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008092 if (pret == nr_pages) {
8093 /* don't support file backed memory */
8094 for (j = 0; j < nr_pages; j++) {
8095 struct vm_area_struct *vma = vmas[j];
8096
8097 if (vma->vm_file &&
8098 !is_file_hugepages(vma->vm_file)) {
8099 ret = -EOPNOTSUPP;
8100 break;
8101 }
8102 }
8103 } else {
8104 ret = pret < 0 ? pret : -EFAULT;
8105 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008106 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008107 if (ret) {
8108 /*
8109 * if we did partial map, or found file backed vmas,
8110 * release any pages we did get
8111 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008112 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008113 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008114 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008115 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008116 goto err;
8117 }
8118
8119 off = ubuf & ~PAGE_MASK;
8120 size = iov.iov_len;
8121 for (j = 0; j < nr_pages; j++) {
8122 size_t vec_len;
8123
8124 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8125 imu->bvec[j].bv_page = pages[j];
8126 imu->bvec[j].bv_len = vec_len;
8127 imu->bvec[j].bv_offset = off;
8128 off = 0;
8129 size -= vec_len;
8130 }
8131 /* store original address for later verification */
8132 imu->ubuf = ubuf;
8133 imu->len = iov.iov_len;
8134 imu->nr_bvecs = nr_pages;
8135
8136 ctx->nr_user_bufs++;
8137 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008138 kvfree(pages);
8139 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008140 return 0;
8141err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008142 kvfree(pages);
8143 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008144 io_sqe_buffer_unregister(ctx);
8145 return ret;
8146}
8147
Jens Axboe9b402842019-04-11 11:45:41 -06008148static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8149{
8150 __s32 __user *fds = arg;
8151 int fd;
8152
8153 if (ctx->cq_ev_fd)
8154 return -EBUSY;
8155
8156 if (copy_from_user(&fd, fds, sizeof(*fds)))
8157 return -EFAULT;
8158
8159 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8160 if (IS_ERR(ctx->cq_ev_fd)) {
8161 int ret = PTR_ERR(ctx->cq_ev_fd);
8162 ctx->cq_ev_fd = NULL;
8163 return ret;
8164 }
8165
8166 return 0;
8167}
8168
8169static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8170{
8171 if (ctx->cq_ev_fd) {
8172 eventfd_ctx_put(ctx->cq_ev_fd);
8173 ctx->cq_ev_fd = NULL;
8174 return 0;
8175 }
8176
8177 return -ENXIO;
8178}
8179
Jens Axboe5a2e7452020-02-23 16:23:11 -07008180static int __io_destroy_buffers(int id, void *p, void *data)
8181{
8182 struct io_ring_ctx *ctx = data;
8183 struct io_buffer *buf = p;
8184
Jens Axboe067524e2020-03-02 16:32:28 -07008185 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008186 return 0;
8187}
8188
8189static void io_destroy_buffers(struct io_ring_ctx *ctx)
8190{
8191 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8192 idr_destroy(&ctx->io_buffer_idr);
8193}
8194
Jens Axboe2b188cc2019-01-07 10:46:33 -07008195static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8196{
Jens Axboe6b063142019-01-10 22:13:58 -07008197 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008198 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008199
8200 if (ctx->sqo_task) {
8201 put_task_struct(ctx->sqo_task);
8202 ctx->sqo_task = NULL;
8203 mmdrop(ctx->mm_account);
8204 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008205 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008206
Jens Axboe6b063142019-01-10 22:13:58 -07008207 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008208 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008209 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008210 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008211
Jens Axboe2b188cc2019-01-07 10:46:33 -07008212#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008213 if (ctx->ring_sock) {
8214 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008215 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008216 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008217#endif
8218
Hristo Venev75b28af2019-08-26 17:23:46 +00008219 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008220 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008221
8222 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008223 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008224 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008225 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008226 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008227 kfree(ctx);
8228}
8229
8230static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8231{
8232 struct io_ring_ctx *ctx = file->private_data;
8233 __poll_t mask = 0;
8234
8235 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008236 /*
8237 * synchronizes with barrier from wq_has_sleeper call in
8238 * io_commit_cqring
8239 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008240 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008241 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008242 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008243 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008244 mask |= EPOLLIN | EPOLLRDNORM;
8245
8246 return mask;
8247}
8248
8249static int io_uring_fasync(int fd, struct file *file, int on)
8250{
8251 struct io_ring_ctx *ctx = file->private_data;
8252
8253 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8254}
8255
Jens Axboe071698e2020-01-28 10:04:42 -07008256static int io_remove_personalities(int id, void *p, void *data)
8257{
8258 struct io_ring_ctx *ctx = data;
8259 const struct cred *cred;
8260
8261 cred = idr_remove(&ctx->personality_idr, id);
8262 if (cred)
8263 put_cred(cred);
8264 return 0;
8265}
8266
Jens Axboe85faa7b2020-04-09 18:14:00 -06008267static void io_ring_exit_work(struct work_struct *work)
8268{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008269 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8270 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008271
Jens Axboe56952e92020-06-17 15:00:04 -06008272 /*
8273 * If we're doing polled IO and end up having requests being
8274 * submitted async (out-of-line), then completions can come in while
8275 * we're waiting for refs to drop. We need to reap these manually,
8276 * as nobody else will be looking for them.
8277 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008278 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008279 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008280 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008281 io_iopoll_try_reap_events(ctx);
8282 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008283 io_ring_ctx_free(ctx);
8284}
8285
Jens Axboe2b188cc2019-01-07 10:46:33 -07008286static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8287{
8288 mutex_lock(&ctx->uring_lock);
8289 percpu_ref_kill(&ctx->refs);
8290 mutex_unlock(&ctx->uring_lock);
8291
Jens Axboef3606e32020-09-22 08:18:24 -06008292 io_kill_timeouts(ctx, NULL);
8293 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008294
8295 if (ctx->io_wq)
8296 io_wq_cancel_all(ctx->io_wq);
8297
Jens Axboe15dff282019-11-13 09:09:23 -07008298 /* if we failed setting up the ctx, we might not have any rings */
8299 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008300 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008301 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008302 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008303
8304 /*
8305 * Do this upfront, so we won't have a grace period where the ring
8306 * is closed but resources aren't reaped yet. This can cause
8307 * spurious failure in setting up a new ring.
8308 */
Jens Axboe760618f2020-07-24 12:53:31 -06008309 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8310 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008311
Jens Axboe85faa7b2020-04-09 18:14:00 -06008312 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008313 /*
8314 * Use system_unbound_wq to avoid spawning tons of event kworkers
8315 * if we're exiting a ton of rings at the same time. It just adds
8316 * noise and overhead, there's no discernable change in runtime
8317 * over using system_wq.
8318 */
8319 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008320}
8321
8322static int io_uring_release(struct inode *inode, struct file *file)
8323{
8324 struct io_ring_ctx *ctx = file->private_data;
8325
8326 file->private_data = NULL;
8327 io_ring_ctx_wait_and_kill(ctx);
8328 return 0;
8329}
8330
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008331static bool io_wq_files_match(struct io_wq_work *work, void *data)
8332{
8333 struct files_struct *files = data;
8334
Jens Axboe0f212202020-09-13 13:09:39 -06008335 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008336}
8337
Jens Axboef254ac02020-08-12 17:33:30 -06008338/*
8339 * Returns true if 'preq' is the link parent of 'req'
8340 */
8341static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8342{
8343 struct io_kiocb *link;
8344
8345 if (!(preq->flags & REQ_F_LINK_HEAD))
8346 return false;
8347
8348 list_for_each_entry(link, &preq->link_list, link_list) {
8349 if (link == req)
8350 return true;
8351 }
8352
8353 return false;
8354}
8355
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008356static bool io_match_link_files(struct io_kiocb *req,
8357 struct files_struct *files)
8358{
8359 struct io_kiocb *link;
8360
8361 if (io_match_files(req, files))
8362 return true;
8363 if (req->flags & REQ_F_LINK_HEAD) {
8364 list_for_each_entry(link, &req->link_list, link_list) {
8365 if (io_match_files(link, files))
8366 return true;
8367 }
8368 }
8369 return false;
8370}
8371
Jens Axboef254ac02020-08-12 17:33:30 -06008372/*
8373 * We're looking to cancel 'req' because it's holding on to our files, but
8374 * 'req' could be a link to another request. See if it is, and cancel that
8375 * parent request if so.
8376 */
8377static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8378{
8379 struct hlist_node *tmp;
8380 struct io_kiocb *preq;
8381 bool found = false;
8382 int i;
8383
8384 spin_lock_irq(&ctx->completion_lock);
8385 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8386 struct hlist_head *list;
8387
8388 list = &ctx->cancel_hash[i];
8389 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8390 found = io_match_link(preq, req);
8391 if (found) {
8392 io_poll_remove_one(preq);
8393 break;
8394 }
8395 }
8396 }
8397 spin_unlock_irq(&ctx->completion_lock);
8398 return found;
8399}
8400
8401static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8402 struct io_kiocb *req)
8403{
8404 struct io_kiocb *preq;
8405 bool found = false;
8406
8407 spin_lock_irq(&ctx->completion_lock);
8408 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8409 found = io_match_link(preq, req);
8410 if (found) {
8411 __io_timeout_cancel(preq);
8412 break;
8413 }
8414 }
8415 spin_unlock_irq(&ctx->completion_lock);
8416 return found;
8417}
8418
Jens Axboeb711d4e2020-08-16 08:23:05 -07008419static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8420{
8421 return io_match_link(container_of(work, struct io_kiocb, work), data);
8422}
8423
8424static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8425{
8426 enum io_wq_cancel cret;
8427
8428 /* cancel this particular work, if it's running */
8429 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8430 if (cret != IO_WQ_CANCEL_NOTFOUND)
8431 return;
8432
8433 /* find links that hold this pending, cancel those */
8434 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8435 if (cret != IO_WQ_CANCEL_NOTFOUND)
8436 return;
8437
8438 /* if we have a poll link holding this pending, cancel that */
8439 if (io_poll_remove_link(ctx, req))
8440 return;
8441
8442 /* final option, timeout link is holding this req pending */
8443 io_timeout_remove_link(ctx, req);
8444}
8445
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008446static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8447 struct files_struct *files)
8448{
8449 struct io_defer_entry *de = NULL;
8450 LIST_HEAD(list);
8451
8452 spin_lock_irq(&ctx->completion_lock);
8453 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008454 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008455 list_cut_position(&list, &ctx->defer_list, &de->list);
8456 break;
8457 }
8458 }
8459 spin_unlock_irq(&ctx->completion_lock);
8460
8461 while (!list_empty(&list)) {
8462 de = list_first_entry(&list, struct io_defer_entry, list);
8463 list_del_init(&de->list);
8464 req_set_fail_links(de->req);
8465 io_put_req(de->req);
8466 io_req_complete(de->req, -ECANCELED);
8467 kfree(de);
8468 }
8469}
8470
Jens Axboe76e1b642020-09-26 15:05:03 -06008471/*
8472 * Returns true if we found and killed one or more files pinning requests
8473 */
8474static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008475 struct files_struct *files)
8476{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008477 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008478 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008479
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008480 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008481 /* cancel all at once, should be faster than doing it one by one*/
8482 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8483
Jens Axboefcb323c2019-10-24 12:39:47 -06008484 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008485 struct io_kiocb *cancel_req = NULL, *req;
8486 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008487
8488 spin_lock_irq(&ctx->inflight_lock);
8489 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008490 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008491 continue;
8492 /* req is being completed, ignore */
8493 if (!refcount_inc_not_zero(&req->refs))
8494 continue;
8495 cancel_req = req;
8496 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008497 }
Jens Axboe768134d2019-11-10 20:30:53 -07008498 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008499 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008500 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008501 spin_unlock_irq(&ctx->inflight_lock);
8502
Jens Axboe768134d2019-11-10 20:30:53 -07008503 /* We need to keep going until we don't find a matching req */
8504 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008505 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008506 /* cancel this request, or head link requests */
8507 io_attempt_cancel(ctx, cancel_req);
8508 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008509 /* cancellations _may_ trigger task work */
8510 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008511 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008512 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008513 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008514
8515 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008516}
8517
Pavel Begunkov801dd572020-06-15 10:33:14 +03008518static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008519{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008520 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8521 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008522
Jens Axboef3606e32020-09-22 08:18:24 -06008523 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008524}
8525
Jens Axboe0f212202020-09-13 13:09:39 -06008526static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8527 struct task_struct *task,
8528 struct files_struct *files)
8529{
8530 bool ret;
8531
8532 ret = io_uring_cancel_files(ctx, files);
8533 if (!files) {
8534 enum io_wq_cancel cret;
8535
8536 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8537 if (cret != IO_WQ_CANCEL_NOTFOUND)
8538 ret = true;
8539
8540 /* SQPOLL thread does its own polling */
8541 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8542 while (!list_empty_careful(&ctx->iopoll_list)) {
8543 io_iopoll_try_reap_events(ctx);
8544 ret = true;
8545 }
8546 }
8547
8548 ret |= io_poll_remove_all(ctx, task);
8549 ret |= io_kill_timeouts(ctx, task);
8550 }
8551
8552 return ret;
8553}
8554
8555/*
8556 * We need to iteratively cancel requests, in case a request has dependent
8557 * hard links. These persist even for failure of cancelations, hence keep
8558 * looping until none are found.
8559 */
8560static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8561 struct files_struct *files)
8562{
8563 struct task_struct *task = current;
8564
Jens Axboe534ca6d2020-09-02 13:52:19 -06008565 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8566 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008567
8568 io_cqring_overflow_flush(ctx, true, task, files);
8569
8570 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8571 io_run_task_work();
8572 cond_resched();
8573 }
8574}
8575
8576/*
8577 * Note that this task has used io_uring. We use it for cancelation purposes.
8578 */
8579static int io_uring_add_task_file(struct file *file)
8580{
8581 if (unlikely(!current->io_uring)) {
8582 int ret;
8583
8584 ret = io_uring_alloc_task_context(current);
8585 if (unlikely(ret))
8586 return ret;
8587 }
8588 if (current->io_uring->last != file) {
8589 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8590 void *old;
8591
8592 rcu_read_lock();
8593 old = xas_load(&xas);
8594 if (old != file) {
8595 get_file(file);
8596 xas_lock(&xas);
8597 xas_store(&xas, file);
8598 xas_unlock(&xas);
8599 }
8600 rcu_read_unlock();
8601 current->io_uring->last = file;
8602 }
8603
8604 return 0;
8605}
8606
8607/*
8608 * Remove this io_uring_file -> task mapping.
8609 */
8610static void io_uring_del_task_file(struct file *file)
8611{
8612 struct io_uring_task *tctx = current->io_uring;
8613 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8614
8615 if (tctx->last == file)
8616 tctx->last = NULL;
8617
8618 xas_lock(&xas);
8619 file = xas_store(&xas, NULL);
8620 xas_unlock(&xas);
8621
8622 if (file)
8623 fput(file);
8624}
8625
8626static void __io_uring_attempt_task_drop(struct file *file)
8627{
8628 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8629 struct file *old;
8630
8631 rcu_read_lock();
8632 old = xas_load(&xas);
8633 rcu_read_unlock();
8634
8635 if (old == file)
8636 io_uring_del_task_file(file);
8637}
8638
8639/*
8640 * Drop task note for this file if we're the only ones that hold it after
8641 * pending fput()
8642 */
8643static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8644{
8645 if (!current->io_uring)
8646 return;
8647 /*
8648 * fput() is pending, will be 2 if the only other ref is our potential
8649 * task file note. If the task is exiting, drop regardless of count.
8650 */
8651 if (!exiting && atomic_long_read(&file->f_count) != 2)
8652 return;
8653
8654 __io_uring_attempt_task_drop(file);
8655}
8656
8657void __io_uring_files_cancel(struct files_struct *files)
8658{
8659 struct io_uring_task *tctx = current->io_uring;
8660 XA_STATE(xas, &tctx->xa, 0);
8661
8662 /* make sure overflow events are dropped */
8663 tctx->in_idle = true;
8664
8665 do {
8666 struct io_ring_ctx *ctx;
8667 struct file *file;
8668
8669 xas_lock(&xas);
8670 file = xas_next_entry(&xas, ULONG_MAX);
8671 xas_unlock(&xas);
8672
8673 if (!file)
8674 break;
8675
8676 ctx = file->private_data;
8677
8678 io_uring_cancel_task_requests(ctx, files);
8679 if (files)
8680 io_uring_del_task_file(file);
8681 } while (1);
8682}
8683
8684static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8685{
8686 return atomic_long_read(&tctx->req_issue) ==
8687 atomic_long_read(&tctx->req_complete);
8688}
8689
8690/*
8691 * Find any io_uring fd that this task has registered or done IO on, and cancel
8692 * requests.
8693 */
8694void __io_uring_task_cancel(void)
8695{
8696 struct io_uring_task *tctx = current->io_uring;
8697 DEFINE_WAIT(wait);
8698 long completions;
8699
8700 /* make sure overflow events are dropped */
8701 tctx->in_idle = true;
8702
8703 while (!io_uring_task_idle(tctx)) {
8704 /* read completions before cancelations */
8705 completions = atomic_long_read(&tctx->req_complete);
8706 __io_uring_files_cancel(NULL);
8707
8708 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8709
8710 /*
8711 * If we've seen completions, retry. This avoids a race where
8712 * a completion comes in before we did prepare_to_wait().
8713 */
8714 if (completions != atomic_long_read(&tctx->req_complete))
8715 continue;
8716 if (io_uring_task_idle(tctx))
8717 break;
8718 schedule();
8719 }
8720
8721 finish_wait(&tctx->wait, &wait);
8722 tctx->in_idle = false;
8723}
8724
Jens Axboefcb323c2019-10-24 12:39:47 -06008725static int io_uring_flush(struct file *file, void *data)
8726{
8727 struct io_ring_ctx *ctx = file->private_data;
8728
Jens Axboe6ab23142020-02-08 20:23:59 -07008729 /*
8730 * If the task is going away, cancel work it may have pending
8731 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008732 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008733 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008734
Jens Axboe0f212202020-09-13 13:09:39 -06008735 io_uring_cancel_task_requests(ctx, data);
8736 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008737 return 0;
8738}
8739
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008740static void *io_uring_validate_mmap_request(struct file *file,
8741 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008742{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008743 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008744 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008745 struct page *page;
8746 void *ptr;
8747
8748 switch (offset) {
8749 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008750 case IORING_OFF_CQ_RING:
8751 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008752 break;
8753 case IORING_OFF_SQES:
8754 ptr = ctx->sq_sqes;
8755 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008756 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008757 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008758 }
8759
8760 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008761 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008762 return ERR_PTR(-EINVAL);
8763
8764 return ptr;
8765}
8766
8767#ifdef CONFIG_MMU
8768
8769static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8770{
8771 size_t sz = vma->vm_end - vma->vm_start;
8772 unsigned long pfn;
8773 void *ptr;
8774
8775 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8776 if (IS_ERR(ptr))
8777 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008778
8779 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8780 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8781}
8782
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008783#else /* !CONFIG_MMU */
8784
8785static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8786{
8787 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8788}
8789
8790static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8791{
8792 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8793}
8794
8795static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8796 unsigned long addr, unsigned long len,
8797 unsigned long pgoff, unsigned long flags)
8798{
8799 void *ptr;
8800
8801 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8802 if (IS_ERR(ptr))
8803 return PTR_ERR(ptr);
8804
8805 return (unsigned long) ptr;
8806}
8807
8808#endif /* !CONFIG_MMU */
8809
Jens Axboe90554202020-09-03 12:12:41 -06008810static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8811{
8812 DEFINE_WAIT(wait);
8813
8814 do {
8815 if (!io_sqring_full(ctx))
8816 break;
8817
8818 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8819
8820 if (!io_sqring_full(ctx))
8821 break;
8822
8823 schedule();
8824 } while (!signal_pending(current));
8825
8826 finish_wait(&ctx->sqo_sq_wait, &wait);
8827}
8828
Jens Axboe2b188cc2019-01-07 10:46:33 -07008829SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8830 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8831 size_t, sigsz)
8832{
8833 struct io_ring_ctx *ctx;
8834 long ret = -EBADF;
8835 int submitted = 0;
8836 struct fd f;
8837
Jens Axboe4c6e2772020-07-01 11:29:10 -06008838 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008839
Jens Axboe90554202020-09-03 12:12:41 -06008840 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8841 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008842 return -EINVAL;
8843
8844 f = fdget(fd);
8845 if (!f.file)
8846 return -EBADF;
8847
8848 ret = -EOPNOTSUPP;
8849 if (f.file->f_op != &io_uring_fops)
8850 goto out_fput;
8851
8852 ret = -ENXIO;
8853 ctx = f.file->private_data;
8854 if (!percpu_ref_tryget(&ctx->refs))
8855 goto out_fput;
8856
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008857 ret = -EBADFD;
8858 if (ctx->flags & IORING_SETUP_R_DISABLED)
8859 goto out;
8860
Jens Axboe6c271ce2019-01-10 11:22:30 -07008861 /*
8862 * For SQ polling, the thread will do all submissions and completions.
8863 * Just return the requested submit count, and wake the thread if
8864 * we were asked to.
8865 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008866 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008867 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008868 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008869 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008870 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008871 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008872 if (flags & IORING_ENTER_SQ_WAIT)
8873 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008874 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008875 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008876 ret = io_uring_add_task_file(f.file);
8877 if (unlikely(ret))
8878 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008879 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008880 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008881 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008882
8883 if (submitted != to_submit)
8884 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008885 }
8886 if (flags & IORING_ENTER_GETEVENTS) {
8887 min_complete = min(min_complete, ctx->cq_entries);
8888
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008889 /*
8890 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8891 * space applications don't need to do io completion events
8892 * polling again, they can rely on io_sq_thread to do polling
8893 * work, which can reduce cpu usage and uring_lock contention.
8894 */
8895 if (ctx->flags & IORING_SETUP_IOPOLL &&
8896 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008897 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008898 } else {
8899 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8900 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008901 }
8902
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008903out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008904 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008905out_fput:
8906 fdput(f);
8907 return submitted ? submitted : ret;
8908}
8909
Tobias Klauserbebdb652020-02-26 18:38:32 +01008910#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008911static int io_uring_show_cred(int id, void *p, void *data)
8912{
8913 const struct cred *cred = p;
8914 struct seq_file *m = data;
8915 struct user_namespace *uns = seq_user_ns(m);
8916 struct group_info *gi;
8917 kernel_cap_t cap;
8918 unsigned __capi;
8919 int g;
8920
8921 seq_printf(m, "%5d\n", id);
8922 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8923 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8924 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8925 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8926 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8927 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8928 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8929 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8930 seq_puts(m, "\n\tGroups:\t");
8931 gi = cred->group_info;
8932 for (g = 0; g < gi->ngroups; g++) {
8933 seq_put_decimal_ull(m, g ? " " : "",
8934 from_kgid_munged(uns, gi->gid[g]));
8935 }
8936 seq_puts(m, "\n\tCapEff:\t");
8937 cap = cred->cap_effective;
8938 CAP_FOR_EACH_U32(__capi)
8939 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8940 seq_putc(m, '\n');
8941 return 0;
8942}
8943
8944static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8945{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008946 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008947 int i;
8948
Jens Axboefad8e0d2020-09-28 08:57:48 -06008949 /*
8950 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8951 * since fdinfo case grabs it in the opposite direction of normal use
8952 * cases. If we fail to get the lock, we just don't iterate any
8953 * structures that could be going away outside the io_uring mutex.
8954 */
8955 has_lock = mutex_trylock(&ctx->uring_lock);
8956
Jens Axboe87ce9552020-01-30 08:25:34 -07008957 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008958 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008959 struct fixed_file_table *table;
8960 struct file *f;
8961
8962 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8963 f = table->files[i & IORING_FILE_TABLE_MASK];
8964 if (f)
8965 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8966 else
8967 seq_printf(m, "%5u: <none>\n", i);
8968 }
8969 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008970 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008971 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8972
8973 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8974 (unsigned int) buf->len);
8975 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008976 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008977 seq_printf(m, "Personalities:\n");
8978 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8979 }
Jens Axboed7718a92020-02-14 22:23:12 -07008980 seq_printf(m, "PollList:\n");
8981 spin_lock_irq(&ctx->completion_lock);
8982 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8983 struct hlist_head *list = &ctx->cancel_hash[i];
8984 struct io_kiocb *req;
8985
8986 hlist_for_each_entry(req, list, hash_node)
8987 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8988 req->task->task_works != NULL);
8989 }
8990 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008991 if (has_lock)
8992 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008993}
8994
8995static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8996{
8997 struct io_ring_ctx *ctx = f->private_data;
8998
8999 if (percpu_ref_tryget(&ctx->refs)) {
9000 __io_uring_show_fdinfo(ctx, m);
9001 percpu_ref_put(&ctx->refs);
9002 }
9003}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009004#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009005
Jens Axboe2b188cc2019-01-07 10:46:33 -07009006static const struct file_operations io_uring_fops = {
9007 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009008 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009009 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009010#ifndef CONFIG_MMU
9011 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9012 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9013#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009014 .poll = io_uring_poll,
9015 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009016#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009017 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009018#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009019};
9020
9021static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9022 struct io_uring_params *p)
9023{
Hristo Venev75b28af2019-08-26 17:23:46 +00009024 struct io_rings *rings;
9025 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009026
Jens Axboebd740482020-08-05 12:58:23 -06009027 /* make sure these are sane, as we already accounted them */
9028 ctx->sq_entries = p->sq_entries;
9029 ctx->cq_entries = p->cq_entries;
9030
Hristo Venev75b28af2019-08-26 17:23:46 +00009031 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9032 if (size == SIZE_MAX)
9033 return -EOVERFLOW;
9034
9035 rings = io_mem_alloc(size);
9036 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009037 return -ENOMEM;
9038
Hristo Venev75b28af2019-08-26 17:23:46 +00009039 ctx->rings = rings;
9040 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9041 rings->sq_ring_mask = p->sq_entries - 1;
9042 rings->cq_ring_mask = p->cq_entries - 1;
9043 rings->sq_ring_entries = p->sq_entries;
9044 rings->cq_ring_entries = p->cq_entries;
9045 ctx->sq_mask = rings->sq_ring_mask;
9046 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009047
9048 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009049 if (size == SIZE_MAX) {
9050 io_mem_free(ctx->rings);
9051 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009052 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009053 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009054
9055 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009056 if (!ctx->sq_sqes) {
9057 io_mem_free(ctx->rings);
9058 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009059 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009060 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009061
Jens Axboe2b188cc2019-01-07 10:46:33 -07009062 return 0;
9063}
9064
9065/*
9066 * Allocate an anonymous fd, this is what constitutes the application
9067 * visible backing of an io_uring instance. The application mmaps this
9068 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9069 * we have to tie this fd to a socket for file garbage collection purposes.
9070 */
9071static int io_uring_get_fd(struct io_ring_ctx *ctx)
9072{
9073 struct file *file;
9074 int ret;
9075
9076#if defined(CONFIG_UNIX)
9077 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9078 &ctx->ring_sock);
9079 if (ret)
9080 return ret;
9081#endif
9082
9083 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9084 if (ret < 0)
9085 goto err;
9086
9087 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9088 O_RDWR | O_CLOEXEC);
9089 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009090err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009091 put_unused_fd(ret);
9092 ret = PTR_ERR(file);
9093 goto err;
9094 }
9095
9096#if defined(CONFIG_UNIX)
9097 ctx->ring_sock->file = file;
9098#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009099 if (unlikely(io_uring_add_task_file(file))) {
9100 file = ERR_PTR(-ENOMEM);
9101 goto err_fd;
9102 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009103 fd_install(ret, file);
9104 return ret;
9105err:
9106#if defined(CONFIG_UNIX)
9107 sock_release(ctx->ring_sock);
9108 ctx->ring_sock = NULL;
9109#endif
9110 return ret;
9111}
9112
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009113static int io_uring_create(unsigned entries, struct io_uring_params *p,
9114 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009115{
9116 struct user_struct *user = NULL;
9117 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009118 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009119 int ret;
9120
Jens Axboe8110c1a2019-12-28 15:39:54 -07009121 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009122 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009123 if (entries > IORING_MAX_ENTRIES) {
9124 if (!(p->flags & IORING_SETUP_CLAMP))
9125 return -EINVAL;
9126 entries = IORING_MAX_ENTRIES;
9127 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009128
9129 /*
9130 * Use twice as many entries for the CQ ring. It's possible for the
9131 * application to drive a higher depth than the size of the SQ ring,
9132 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009133 * some flexibility in overcommitting a bit. If the application has
9134 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9135 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009136 */
9137 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009138 if (p->flags & IORING_SETUP_CQSIZE) {
9139 /*
9140 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9141 * to a power-of-two, if it isn't already. We do NOT impose
9142 * any cq vs sq ring sizing.
9143 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009144 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009145 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009146 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9147 if (!(p->flags & IORING_SETUP_CLAMP))
9148 return -EINVAL;
9149 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9150 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009151 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9152 } else {
9153 p->cq_entries = 2 * p->sq_entries;
9154 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009155
9156 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009157 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009158
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009159 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009160 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009161 ring_pages(p->sq_entries, p->cq_entries));
9162 if (ret) {
9163 free_uid(user);
9164 return ret;
9165 }
9166 }
9167
9168 ctx = io_ring_ctx_alloc(p);
9169 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009170 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009171 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009172 p->cq_entries));
9173 free_uid(user);
9174 return -ENOMEM;
9175 }
9176 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009177 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009178 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009179
Jens Axboe2aede0e2020-09-14 10:45:53 -06009180 ctx->sqo_task = get_task_struct(current);
9181
9182 /*
9183 * This is just grabbed for accounting purposes. When a process exits,
9184 * the mm is exited and dropped before the files, hence we need to hang
9185 * on to this mm purely for the purposes of being able to unaccount
9186 * memory (locked/pinned vm). It's not used for anything else.
9187 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009188 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009189 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009190
Jens Axboef74441e2020-08-05 13:00:44 -06009191 /*
9192 * Account memory _before_ installing the file descriptor. Once
9193 * the descriptor is installed, it can get closed at any time. Also
9194 * do this before hitting the general error path, as ring freeing
9195 * will un-account as well.
9196 */
9197 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9198 ACCT_LOCKED);
9199 ctx->limit_mem = limit_mem;
9200
Jens Axboe2b188cc2019-01-07 10:46:33 -07009201 ret = io_allocate_scq_urings(ctx, p);
9202 if (ret)
9203 goto err;
9204
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009205 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009206 if (ret)
9207 goto err;
9208
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009209 if (!(p->flags & IORING_SETUP_R_DISABLED))
9210 io_sq_offload_start(ctx);
9211
Jens Axboe2b188cc2019-01-07 10:46:33 -07009212 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009213 p->sq_off.head = offsetof(struct io_rings, sq.head);
9214 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9215 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9216 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9217 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9218 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9219 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009220
9221 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009222 p->cq_off.head = offsetof(struct io_rings, cq.head);
9223 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9224 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9225 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9226 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9227 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009228 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009229
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009230 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9231 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009232 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9233 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009234
9235 if (copy_to_user(params, p, sizeof(*p))) {
9236 ret = -EFAULT;
9237 goto err;
9238 }
Jens Axboed1719f72020-07-30 13:43:53 -06009239
9240 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009241 * Install ring fd as the very last thing, so we don't risk someone
9242 * having closed it before we finish setup
9243 */
9244 ret = io_uring_get_fd(ctx);
9245 if (ret < 0)
9246 goto err;
9247
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009248 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009249 return ret;
9250err:
9251 io_ring_ctx_wait_and_kill(ctx);
9252 return ret;
9253}
9254
9255/*
9256 * Sets up an aio uring context, and returns the fd. Applications asks for a
9257 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9258 * params structure passed in.
9259 */
9260static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9261{
9262 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009263 int i;
9264
9265 if (copy_from_user(&p, params, sizeof(p)))
9266 return -EFAULT;
9267 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9268 if (p.resv[i])
9269 return -EINVAL;
9270 }
9271
Jens Axboe6c271ce2019-01-10 11:22:30 -07009272 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009273 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009274 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9275 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009276 return -EINVAL;
9277
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009278 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009279}
9280
9281SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9282 struct io_uring_params __user *, params)
9283{
9284 return io_uring_setup(entries, params);
9285}
9286
Jens Axboe66f4af92020-01-16 15:36:52 -07009287static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9288{
9289 struct io_uring_probe *p;
9290 size_t size;
9291 int i, ret;
9292
9293 size = struct_size(p, ops, nr_args);
9294 if (size == SIZE_MAX)
9295 return -EOVERFLOW;
9296 p = kzalloc(size, GFP_KERNEL);
9297 if (!p)
9298 return -ENOMEM;
9299
9300 ret = -EFAULT;
9301 if (copy_from_user(p, arg, size))
9302 goto out;
9303 ret = -EINVAL;
9304 if (memchr_inv(p, 0, size))
9305 goto out;
9306
9307 p->last_op = IORING_OP_LAST - 1;
9308 if (nr_args > IORING_OP_LAST)
9309 nr_args = IORING_OP_LAST;
9310
9311 for (i = 0; i < nr_args; i++) {
9312 p->ops[i].op = i;
9313 if (!io_op_defs[i].not_supported)
9314 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9315 }
9316 p->ops_len = i;
9317
9318 ret = 0;
9319 if (copy_to_user(arg, p, size))
9320 ret = -EFAULT;
9321out:
9322 kfree(p);
9323 return ret;
9324}
9325
Jens Axboe071698e2020-01-28 10:04:42 -07009326static int io_register_personality(struct io_ring_ctx *ctx)
9327{
9328 const struct cred *creds = get_current_cred();
9329 int id;
9330
9331 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9332 USHRT_MAX, GFP_KERNEL);
9333 if (id < 0)
9334 put_cred(creds);
9335 return id;
9336}
9337
9338static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9339{
9340 const struct cred *old_creds;
9341
9342 old_creds = idr_remove(&ctx->personality_idr, id);
9343 if (old_creds) {
9344 put_cred(old_creds);
9345 return 0;
9346 }
9347
9348 return -EINVAL;
9349}
9350
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009351static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9352 unsigned int nr_args)
9353{
9354 struct io_uring_restriction *res;
9355 size_t size;
9356 int i, ret;
9357
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009358 /* Restrictions allowed only if rings started disabled */
9359 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9360 return -EBADFD;
9361
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009362 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009363 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009364 return -EBUSY;
9365
9366 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9367 return -EINVAL;
9368
9369 size = array_size(nr_args, sizeof(*res));
9370 if (size == SIZE_MAX)
9371 return -EOVERFLOW;
9372
9373 res = memdup_user(arg, size);
9374 if (IS_ERR(res))
9375 return PTR_ERR(res);
9376
9377 ret = 0;
9378
9379 for (i = 0; i < nr_args; i++) {
9380 switch (res[i].opcode) {
9381 case IORING_RESTRICTION_REGISTER_OP:
9382 if (res[i].register_op >= IORING_REGISTER_LAST) {
9383 ret = -EINVAL;
9384 goto out;
9385 }
9386
9387 __set_bit(res[i].register_op,
9388 ctx->restrictions.register_op);
9389 break;
9390 case IORING_RESTRICTION_SQE_OP:
9391 if (res[i].sqe_op >= IORING_OP_LAST) {
9392 ret = -EINVAL;
9393 goto out;
9394 }
9395
9396 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9397 break;
9398 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9399 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9400 break;
9401 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9402 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9403 break;
9404 default:
9405 ret = -EINVAL;
9406 goto out;
9407 }
9408 }
9409
9410out:
9411 /* Reset all restrictions if an error happened */
9412 if (ret != 0)
9413 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9414 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009415 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009416
9417 kfree(res);
9418 return ret;
9419}
9420
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009421static int io_register_enable_rings(struct io_ring_ctx *ctx)
9422{
9423 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9424 return -EBADFD;
9425
9426 if (ctx->restrictions.registered)
9427 ctx->restricted = 1;
9428
9429 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9430
9431 io_sq_offload_start(ctx);
9432
9433 return 0;
9434}
9435
Jens Axboe071698e2020-01-28 10:04:42 -07009436static bool io_register_op_must_quiesce(int op)
9437{
9438 switch (op) {
9439 case IORING_UNREGISTER_FILES:
9440 case IORING_REGISTER_FILES_UPDATE:
9441 case IORING_REGISTER_PROBE:
9442 case IORING_REGISTER_PERSONALITY:
9443 case IORING_UNREGISTER_PERSONALITY:
9444 return false;
9445 default:
9446 return true;
9447 }
9448}
9449
Jens Axboeedafcce2019-01-09 09:16:05 -07009450static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9451 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009452 __releases(ctx->uring_lock)
9453 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009454{
9455 int ret;
9456
Jens Axboe35fa71a2019-04-22 10:23:23 -06009457 /*
9458 * We're inside the ring mutex, if the ref is already dying, then
9459 * someone else killed the ctx or is already going through
9460 * io_uring_register().
9461 */
9462 if (percpu_ref_is_dying(&ctx->refs))
9463 return -ENXIO;
9464
Jens Axboe071698e2020-01-28 10:04:42 -07009465 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009466 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009467
Jens Axboe05f3fb32019-12-09 11:22:50 -07009468 /*
9469 * Drop uring mutex before waiting for references to exit. If
9470 * another thread is currently inside io_uring_enter() it might
9471 * need to grab the uring_lock to make progress. If we hold it
9472 * here across the drain wait, then we can deadlock. It's safe
9473 * to drop the mutex here, since no new references will come in
9474 * after we've killed the percpu ref.
9475 */
9476 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009477 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009478 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009479 if (ret) {
9480 percpu_ref_resurrect(&ctx->refs);
9481 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009482 goto out_quiesce;
9483 }
9484 }
9485
9486 if (ctx->restricted) {
9487 if (opcode >= IORING_REGISTER_LAST) {
9488 ret = -EINVAL;
9489 goto out;
9490 }
9491
9492 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9493 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009494 goto out;
9495 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009496 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009497
9498 switch (opcode) {
9499 case IORING_REGISTER_BUFFERS:
9500 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9501 break;
9502 case IORING_UNREGISTER_BUFFERS:
9503 ret = -EINVAL;
9504 if (arg || nr_args)
9505 break;
9506 ret = io_sqe_buffer_unregister(ctx);
9507 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009508 case IORING_REGISTER_FILES:
9509 ret = io_sqe_files_register(ctx, arg, nr_args);
9510 break;
9511 case IORING_UNREGISTER_FILES:
9512 ret = -EINVAL;
9513 if (arg || nr_args)
9514 break;
9515 ret = io_sqe_files_unregister(ctx);
9516 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009517 case IORING_REGISTER_FILES_UPDATE:
9518 ret = io_sqe_files_update(ctx, arg, nr_args);
9519 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009520 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009521 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009522 ret = -EINVAL;
9523 if (nr_args != 1)
9524 break;
9525 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009526 if (ret)
9527 break;
9528 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9529 ctx->eventfd_async = 1;
9530 else
9531 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009532 break;
9533 case IORING_UNREGISTER_EVENTFD:
9534 ret = -EINVAL;
9535 if (arg || nr_args)
9536 break;
9537 ret = io_eventfd_unregister(ctx);
9538 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009539 case IORING_REGISTER_PROBE:
9540 ret = -EINVAL;
9541 if (!arg || nr_args > 256)
9542 break;
9543 ret = io_probe(ctx, arg, nr_args);
9544 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009545 case IORING_REGISTER_PERSONALITY:
9546 ret = -EINVAL;
9547 if (arg || nr_args)
9548 break;
9549 ret = io_register_personality(ctx);
9550 break;
9551 case IORING_UNREGISTER_PERSONALITY:
9552 ret = -EINVAL;
9553 if (arg)
9554 break;
9555 ret = io_unregister_personality(ctx, nr_args);
9556 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009557 case IORING_REGISTER_ENABLE_RINGS:
9558 ret = -EINVAL;
9559 if (arg || nr_args)
9560 break;
9561 ret = io_register_enable_rings(ctx);
9562 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009563 case IORING_REGISTER_RESTRICTIONS:
9564 ret = io_register_restrictions(ctx, arg, nr_args);
9565 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009566 default:
9567 ret = -EINVAL;
9568 break;
9569 }
9570
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009571out:
Jens Axboe071698e2020-01-28 10:04:42 -07009572 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009573 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009574 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009575out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009576 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009577 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009578 return ret;
9579}
9580
9581SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9582 void __user *, arg, unsigned int, nr_args)
9583{
9584 struct io_ring_ctx *ctx;
9585 long ret = -EBADF;
9586 struct fd f;
9587
9588 f = fdget(fd);
9589 if (!f.file)
9590 return -EBADF;
9591
9592 ret = -EOPNOTSUPP;
9593 if (f.file->f_op != &io_uring_fops)
9594 goto out_fput;
9595
9596 ctx = f.file->private_data;
9597
9598 mutex_lock(&ctx->uring_lock);
9599 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9600 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009601 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9602 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009603out_fput:
9604 fdput(f);
9605 return ret;
9606}
9607
Jens Axboe2b188cc2019-01-07 10:46:33 -07009608static int __init io_uring_init(void)
9609{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009610#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9611 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9612 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9613} while (0)
9614
9615#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9616 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9617 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9618 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9619 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9620 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9621 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9622 BUILD_BUG_SQE_ELEM(8, __u64, off);
9623 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9624 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009625 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009626 BUILD_BUG_SQE_ELEM(24, __u32, len);
9627 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9628 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9629 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9630 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009631 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9632 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009633 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9634 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9635 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9636 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9637 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9638 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9639 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9640 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009641 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009642 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9643 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9644 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009645 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009646
Jens Axboed3656342019-12-18 09:50:26 -07009647 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009648 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009649 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9650 return 0;
9651};
9652__initcall(io_uring_init);