blob: 9a7c645f971c1f542553217344a3b6328d10aeee [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;
235 struct task_struct *thread;
236 struct wait_queue_head wait;
237};
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239struct io_ring_ctx {
240 struct {
241 struct percpu_ref refs;
242 } ____cacheline_aligned_in_smp;
243
244 struct {
245 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800246 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700247 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800248 unsigned int cq_overflow_flushed: 1;
249 unsigned int drain_next: 1;
250 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200251 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700252
Hristo Venev75b28af2019-08-26 17:23:46 +0000253 /*
254 * Ring buffer of indices into array of io_uring_sqe, which is
255 * mmapped by the application using the IORING_OFF_SQES offset.
256 *
257 * This indirection could e.g. be used to assign fixed
258 * io_uring_sqe entries to operations and only submit them to
259 * the queue when needed.
260 *
261 * The kernel modifies neither the indices array nor the entries
262 * array.
263 */
264 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700265 unsigned cached_sq_head;
266 unsigned sq_entries;
267 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700268 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600269 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700270 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700271 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600272
273 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600274 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700275 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276
Jens Axboefcb323c2019-10-24 12:39:47 -0600277 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700278 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279 } ____cacheline_aligned_in_smp;
280
Hristo Venev75b28af2019-08-26 17:23:46 +0000281 struct io_rings *rings;
282
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600284 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600285
286 /*
287 * For SQPOLL usage - we hold a reference to the parent task, so we
288 * have access to the ->files
289 */
290 struct task_struct *sqo_task;
291
292 /* Only used for accounting purposes */
293 struct mm_struct *mm_account;
294
Jens Axboe534ca6d2020-09-02 13:52:19 -0600295 struct io_sq_data *sq_data; /* if using sq thread polling */
296
Jens Axboe6a779382020-09-02 12:21:41 -0600297 struct wait_queue_entry sqo_wait_entry;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700298
Jens Axboe6b063142019-01-10 22:13:58 -0700299 /*
300 * If used, fixed file set. Writers must ensure that ->refs is dead,
301 * readers must ensure that ->refs is alive as long as the file* is
302 * used. Only updated through io_uring_register(2).
303 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700304 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700305 unsigned nr_user_files;
306
Jens Axboeedafcce2019-01-09 09:16:05 -0700307 /* if used, fixed mapped user buffers */
308 unsigned nr_user_bufs;
309 struct io_mapped_ubuf *user_bufs;
310
Jens Axboe2b188cc2019-01-07 10:46:33 -0700311 struct user_struct *user;
312
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700313 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700314
Jens Axboe0f158b42020-05-14 17:18:39 -0600315 struct completion ref_comp;
316 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700317
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700318 /* if all else fails... */
319 struct io_kiocb *fallback_req;
320
Jens Axboe206aefd2019-11-07 18:27:42 -0700321#if defined(CONFIG_UNIX)
322 struct socket *ring_sock;
323#endif
324
Jens Axboe5a2e7452020-02-23 16:23:11 -0700325 struct idr io_buffer_idr;
326
Jens Axboe071698e2020-01-28 10:04:42 -0700327 struct idr personality_idr;
328
Jens Axboe206aefd2019-11-07 18:27:42 -0700329 struct {
330 unsigned cached_cq_tail;
331 unsigned cq_entries;
332 unsigned cq_mask;
333 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700334 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700335 struct wait_queue_head cq_wait;
336 struct fasync_struct *cq_fasync;
337 struct eventfd_ctx *cq_ev_fd;
338 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339
340 struct {
341 struct mutex uring_lock;
342 wait_queue_head_t wait;
343 } ____cacheline_aligned_in_smp;
344
345 struct {
346 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700347
Jens Axboedef596e2019-01-09 08:59:42 -0700348 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300349 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700350 * io_uring instances that don't use IORING_SETUP_SQPOLL.
351 * For SQPOLL, only the single threaded io_sq_thread() will
352 * manipulate the list, hence no extra locking is needed there.
353 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300354 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700355 struct hlist_head *cancel_hash;
356 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700357 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600358
359 spinlock_t inflight_lock;
360 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700361 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600362
Jens Axboe4a38aed22020-05-14 17:21:15 -0600363 struct delayed_work file_put_work;
364 struct llist_head file_put_llist;
365
Jens Axboe85faa7b2020-04-09 18:14:00 -0600366 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200367 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700368};
369
Jens Axboe09bb8392019-03-13 12:39:28 -0600370/*
371 * First field must be the file pointer in all the
372 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
373 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700374struct io_poll_iocb {
375 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700376 union {
377 struct wait_queue_head *head;
378 u64 addr;
379 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700380 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600381 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700382 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700383 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700384};
385
Jens Axboeb5dba592019-12-11 14:02:38 -0700386struct io_close {
387 struct file *file;
388 struct file *put_file;
389 int fd;
390};
391
Jens Axboead8a48a2019-11-15 08:49:11 -0700392struct io_timeout_data {
393 struct io_kiocb *req;
394 struct hrtimer timer;
395 struct timespec64 ts;
396 enum hrtimer_mode mode;
397};
398
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700399struct io_accept {
400 struct file *file;
401 struct sockaddr __user *addr;
402 int __user *addr_len;
403 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600404 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700405};
406
407struct io_sync {
408 struct file *file;
409 loff_t len;
410 loff_t off;
411 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700412 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700413};
414
Jens Axboefbf23842019-12-17 18:45:56 -0700415struct io_cancel {
416 struct file *file;
417 u64 addr;
418};
419
Jens Axboeb29472e2019-12-17 18:50:29 -0700420struct io_timeout {
421 struct file *file;
422 u64 addr;
423 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300424 u32 off;
425 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300426 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700427};
428
Jens Axboe9adbd452019-12-20 08:45:55 -0700429struct io_rw {
430 /* NOTE: kiocb has the file as the first member, so don't do it here */
431 struct kiocb kiocb;
432 u64 addr;
433 u64 len;
434};
435
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700436struct io_connect {
437 struct file *file;
438 struct sockaddr __user *addr;
439 int addr_len;
440};
441
Jens Axboee47293f2019-12-20 08:58:21 -0700442struct io_sr_msg {
443 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700444 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300445 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700446 void __user *buf;
447 };
Jens Axboee47293f2019-12-20 08:58:21 -0700448 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700449 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700450 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700451 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700452};
453
Jens Axboe15b71ab2019-12-11 11:20:36 -0700454struct io_open {
455 struct file *file;
456 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700457 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700458 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600459 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700460};
461
Jens Axboe05f3fb32019-12-09 11:22:50 -0700462struct io_files_update {
463 struct file *file;
464 u64 arg;
465 u32 nr_args;
466 u32 offset;
467};
468
Jens Axboe4840e412019-12-25 22:03:45 -0700469struct io_fadvise {
470 struct file *file;
471 u64 offset;
472 u32 len;
473 u32 advice;
474};
475
Jens Axboec1ca7572019-12-25 22:18:28 -0700476struct io_madvise {
477 struct file *file;
478 u64 addr;
479 u32 len;
480 u32 advice;
481};
482
Jens Axboe3e4827b2020-01-08 15:18:09 -0700483struct io_epoll {
484 struct file *file;
485 int epfd;
486 int op;
487 int fd;
488 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700489};
490
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300491struct io_splice {
492 struct file *file_out;
493 struct file *file_in;
494 loff_t off_out;
495 loff_t off_in;
496 u64 len;
497 unsigned int flags;
498};
499
Jens Axboeddf0322d2020-02-23 16:41:33 -0700500struct io_provide_buf {
501 struct file *file;
502 __u64 addr;
503 __s32 len;
504 __u32 bgid;
505 __u16 nbufs;
506 __u16 bid;
507};
508
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700509struct io_statx {
510 struct file *file;
511 int dfd;
512 unsigned int mask;
513 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700514 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700515 struct statx __user *buffer;
516};
517
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300518struct io_completion {
519 struct file *file;
520 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300521 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300522};
523
Jens Axboef499a022019-12-02 16:28:46 -0700524struct io_async_connect {
525 struct sockaddr_storage address;
526};
527
Jens Axboe03b12302019-12-02 18:50:25 -0700528struct io_async_msghdr {
529 struct iovec fast_iov[UIO_FASTIOV];
530 struct iovec *iov;
531 struct sockaddr __user *uaddr;
532 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700533 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700534};
535
Jens Axboef67676d2019-12-02 11:03:47 -0700536struct io_async_rw {
537 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600538 const struct iovec *free_iovec;
539 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600540 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600541 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700542};
543
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700544struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700545 union {
546 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700547 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700548 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700549 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700550 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700551};
552
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300553enum {
554 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
555 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
556 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
557 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
558 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700559 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300560
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300561 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562 REQ_F_FAIL_LINK_BIT,
563 REQ_F_INFLIGHT_BIT,
564 REQ_F_CUR_POS_BIT,
565 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300566 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300567 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300569 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700570 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700571 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600572 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800573 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700574
575 /* not a real bit, just to check we're not overflowing the space */
576 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577};
578
579enum {
580 /* ctx owns file */
581 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
582 /* drain existing IO first */
583 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
584 /* linked sqes */
585 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
586 /* doesn't sever on completion < 0 */
587 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
588 /* IOSQE_ASYNC */
589 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700590 /* IOSQE_BUFFER_SELECT */
591 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300592
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300593 /* head of a link */
594 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300595 /* fail rest of links */
596 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
597 /* on inflight list */
598 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
599 /* read/write uses file position */
600 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
601 /* must not punt to workers */
602 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300603 /* has linked timeout */
604 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300605 /* regular file */
606 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300607 /* completion under lock */
608 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300609 /* needs cleanup */
610 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700611 /* already went through poll handler */
612 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700613 /* buffer already selected */
614 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600615 /* doesn't need file table for this request */
616 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800617 /* io_wq_work is initialized */
618 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700619};
620
621struct async_poll {
622 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600623 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300624};
625
Jens Axboe09bb8392019-03-13 12:39:28 -0600626/*
627 * NOTE! Each of the iocb union members has the file pointer
628 * as the first entry in their struct definition. So you can
629 * access the file pointer through any of the sub-structs,
630 * or directly as just 'ki_filp' in this struct.
631 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700633 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600634 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700635 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700636 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700637 struct io_accept accept;
638 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700639 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700640 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700641 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700642 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700643 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700644 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700645 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700646 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700647 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700648 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300649 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700650 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700651 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300652 /* use only after cleaning per-op data, see io_clean_op() */
653 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700654 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700655
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700656 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700657 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800658 /* polled IO has completed */
659 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700660
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700661 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300662 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700663
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300664 struct io_ring_ctx *ctx;
665 unsigned int flags;
666 refcount_t refs;
667 struct task_struct *task;
668 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700669
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300670 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700671
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300672 /*
673 * 1. used with ctx->iopoll_list with reads/writes
674 * 2. to track reqs with ->files (see io_op_def::file_table)
675 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300676 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600677
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300678 struct percpu_ref *fixed_file_refs;
679 struct callback_head task_work;
680 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
681 struct hlist_node hash_node;
682 struct async_poll *apoll;
683 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700684};
685
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300686struct io_defer_entry {
687 struct list_head list;
688 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300689 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300690};
691
Jens Axboedef596e2019-01-09 08:59:42 -0700692#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700693
Jens Axboe013538b2020-06-22 09:29:15 -0600694struct io_comp_state {
695 unsigned int nr;
696 struct list_head list;
697 struct io_ring_ctx *ctx;
698};
699
Jens Axboe9a56a232019-01-09 09:06:50 -0700700struct io_submit_state {
701 struct blk_plug plug;
702
703 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700704 * io_kiocb alloc cache
705 */
706 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300707 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700708
709 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600710 * Batch completion logic
711 */
712 struct io_comp_state comp;
713
714 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700715 * File reference cache
716 */
717 struct file *file;
718 unsigned int fd;
719 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700720 unsigned int ios_left;
721};
722
Jens Axboed3656342019-12-18 09:50:26 -0700723struct io_op_def {
724 /* needs req->io allocated for deferral/async */
725 unsigned async_ctx : 1;
726 /* needs current->mm setup, does mm access */
727 unsigned needs_mm : 1;
728 /* needs req->file assigned */
729 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600730 /* don't fail if file grab fails */
731 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700732 /* hash wq insertion if file is a regular file */
733 unsigned hash_reg_file : 1;
734 /* unbound wq insertion if file is a non-regular file */
735 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700736 /* opcode is not supported by this kernel */
737 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700738 /* needs file table */
739 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700740 /* needs ->fs */
741 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700742 /* set if opcode supports polled "wait" */
743 unsigned pollin : 1;
744 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700745 /* op supports buffer selection */
746 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300747 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700748};
749
750static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300751 [IORING_OP_NOP] = {},
752 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .async_ctx = 1,
754 .needs_mm = 1,
755 .needs_file = 1,
756 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700757 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700758 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700759 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700761 .async_ctx = 1,
762 .needs_mm = 1,
763 .needs_file = 1,
764 .hash_reg_file = 1,
765 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700766 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300767 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .needs_file = 1,
771 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300772 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700773 .needs_file = 1,
774 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700775 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700776 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300777 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .needs_file = 1,
779 .hash_reg_file = 1,
780 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700781 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300782 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700783 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300784 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700785 .needs_file = 1,
786 .unbound_nonreg_file = 1,
787 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300788 [IORING_OP_POLL_REMOVE] = {},
789 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700790 .needs_file = 1,
791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700793 .async_ctx = 1,
794 .needs_mm = 1,
795 .needs_file = 1,
796 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700797 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700798 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700799 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300800 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700801 .async_ctx = 1,
802 .needs_mm = 1,
803 .needs_file = 1,
804 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700805 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700806 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700807 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .async_ctx = 1,
811 .needs_mm = 1,
812 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300813 [IORING_OP_TIMEOUT_REMOVE] = {},
814 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700815 .needs_mm = 1,
816 .needs_file = 1,
817 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700818 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700819 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_ASYNC_CANCEL] = {},
822 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700823 .async_ctx = 1,
824 .needs_mm = 1,
825 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300826 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700827 .async_ctx = 1,
828 .needs_mm = 1,
829 .needs_file = 1,
830 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700831 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700832 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300833 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700834 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300835 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700836 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300837 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700838 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700839 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700840 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300841 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600842 .needs_file = 1,
843 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700844 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700847 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700848 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700849 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300850 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700851 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700852 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600853 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700856 .needs_mm = 1,
857 .needs_file = 1,
858 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700859 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700860 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700863 .needs_mm = 1,
864 .needs_file = 1,
865 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700866 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300867 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700868 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300869 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700870 .needs_file = 1,
871 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300872 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700873 .needs_mm = 1,
874 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300875 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700876 .needs_mm = 1,
877 .needs_file = 1,
878 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700879 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700880 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300881 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700882 .needs_mm = 1,
883 .needs_file = 1,
884 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700885 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700886 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700889 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700890 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700891 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700892 [IORING_OP_EPOLL_CTL] = {
893 .unbound_nonreg_file = 1,
894 .file_table = 1,
895 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300896 [IORING_OP_SPLICE] = {
897 .needs_file = 1,
898 .hash_reg_file = 1,
899 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700900 },
901 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700902 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300903 [IORING_OP_TEE] = {
904 .needs_file = 1,
905 .hash_reg_file = 1,
906 .unbound_nonreg_file = 1,
907 },
Jens Axboed3656342019-12-18 09:50:26 -0700908};
909
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700910enum io_mem_account {
911 ACCT_LOCKED,
912 ACCT_PINNED,
913};
914
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300915static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
916 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700917static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800918static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600919static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700920static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700921static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600922static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700923static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700924static int __io_sqe_files_update(struct io_ring_ctx *ctx,
925 struct io_uring_files_update *ip,
926 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300927static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300928static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700929static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
930 int fd, struct file **out_file, bool fixed);
931static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600932 const struct io_uring_sqe *sqe,
933 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600934static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600935
Jens Axboeb63534c2020-06-04 11:28:00 -0600936static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
937 struct iovec **iovec, struct iov_iter *iter,
938 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600939static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
940 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600941 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700942
943static struct kmem_cache *req_cachep;
944
945static const struct file_operations io_uring_fops;
946
947struct sock *io_uring_get_socket(struct file *file)
948{
949#if defined(CONFIG_UNIX)
950 if (file->f_op == &io_uring_fops) {
951 struct io_ring_ctx *ctx = file->private_data;
952
953 return ctx->ring_sock->sk;
954 }
955#endif
956 return NULL;
957}
958EXPORT_SYMBOL(io_uring_get_socket);
959
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300960static inline void io_clean_op(struct io_kiocb *req)
961{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300962 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
963 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300964 __io_clean_op(req);
965}
966
Jens Axboe4349f302020-07-09 15:07:01 -0600967static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600968{
969 struct mm_struct *mm = current->mm;
970
971 if (mm) {
972 kthread_unuse_mm(mm);
973 mmput(mm);
974 }
975}
976
977static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
978{
979 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300980 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600981 !ctx->sqo_task->mm ||
982 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600983 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600984 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600985 }
986
987 return 0;
988}
989
990static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
991 struct io_kiocb *req)
992{
993 if (!io_op_defs[req->opcode].needs_mm)
994 return 0;
995 return __io_sq_thread_acquire_mm(ctx);
996}
997
998static inline void req_set_fail_links(struct io_kiocb *req)
999{
1000 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1001 req->flags |= REQ_F_FAIL_LINK;
1002}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001003
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001004/*
1005 * Note: must call io_req_init_async() for the first time you
1006 * touch any members of io_wq_work.
1007 */
1008static inline void io_req_init_async(struct io_kiocb *req)
1009{
1010 if (req->flags & REQ_F_WORK_INITIALIZED)
1011 return;
1012
1013 memset(&req->work, 0, sizeof(req->work));
1014 req->flags |= REQ_F_WORK_INITIALIZED;
1015}
1016
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001017static inline bool io_async_submit(struct io_ring_ctx *ctx)
1018{
1019 return ctx->flags & IORING_SETUP_SQPOLL;
1020}
1021
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1023{
1024 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1025
Jens Axboe0f158b42020-05-14 17:18:39 -06001026 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001027}
1028
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001029static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1030{
1031 return !req->timeout.off;
1032}
1033
Jens Axboe2b188cc2019-01-07 10:46:33 -07001034static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1035{
1036 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001037 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001038
1039 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1040 if (!ctx)
1041 return NULL;
1042
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001043 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1044 if (!ctx->fallback_req)
1045 goto err;
1046
Jens Axboe78076bb2019-12-04 19:56:40 -07001047 /*
1048 * Use 5 bits less than the max cq entries, that should give us around
1049 * 32 entries per hash list if totally full and uniformly spread.
1050 */
1051 hash_bits = ilog2(p->cq_entries);
1052 hash_bits -= 5;
1053 if (hash_bits <= 0)
1054 hash_bits = 1;
1055 ctx->cancel_hash_bits = hash_bits;
1056 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1057 GFP_KERNEL);
1058 if (!ctx->cancel_hash)
1059 goto err;
1060 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1061
Roman Gushchin21482892019-05-07 10:01:48 -07001062 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001063 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1064 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001065
1066 ctx->flags = p->flags;
1067 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001068 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001069 init_completion(&ctx->ref_comp);
1070 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001071 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001072 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073 mutex_init(&ctx->uring_lock);
1074 init_waitqueue_head(&ctx->wait);
1075 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001076 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001077 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001078 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001079 init_waitqueue_head(&ctx->inflight_wait);
1080 spin_lock_init(&ctx->inflight_lock);
1081 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001082 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1083 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001085err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001086 if (ctx->fallback_req)
1087 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001088 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001089 kfree(ctx);
1090 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091}
1092
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001093static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001094{
Jens Axboe2bc99302020-07-09 09:43:27 -06001095 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1096 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001097
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001098 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001099 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001100 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001101
Bob Liu9d858b22019-11-13 18:06:25 +08001102 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001103}
1104
Jens Axboede0617e2019-04-06 21:51:27 -06001105static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106{
Hristo Venev75b28af2019-08-26 17:23:46 +00001107 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001108
Pavel Begunkov07910152020-01-17 03:52:46 +03001109 /* order cqe stores with ring update */
1110 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111
Pavel Begunkov07910152020-01-17 03:52:46 +03001112 if (wq_has_sleeper(&ctx->cq_wait)) {
1113 wake_up_interruptible(&ctx->cq_wait);
1114 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115 }
1116}
1117
Jens Axboe51a4cc12020-08-10 10:55:56 -06001118/*
1119 * Returns true if we need to defer file table putting. This can only happen
1120 * from the error path with REQ_F_COMP_LOCKED set.
1121 */
1122static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001123{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001124 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001125 return false;
1126
1127 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001128
Jens Axboecccf0ee2020-01-27 16:34:48 -07001129 if (req->work.mm) {
1130 mmdrop(req->work.mm);
1131 req->work.mm = NULL;
1132 }
1133 if (req->work.creds) {
1134 put_cred(req->work.creds);
1135 req->work.creds = NULL;
1136 }
Jens Axboeff002b32020-02-07 16:05:21 -07001137 if (req->work.fs) {
1138 struct fs_struct *fs = req->work.fs;
1139
Jens Axboe51a4cc12020-08-10 10:55:56 -06001140 if (req->flags & REQ_F_COMP_LOCKED)
1141 return true;
1142
Jens Axboeff002b32020-02-07 16:05:21 -07001143 spin_lock(&req->work.fs->lock);
1144 if (--fs->users)
1145 fs = NULL;
1146 spin_unlock(&req->work.fs->lock);
1147 if (fs)
1148 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001149 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001150 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001151
1152 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001153}
1154
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001155static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001156{
Jens Axboed3656342019-12-18 09:50:26 -07001157 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001158
Pavel Begunkov16d59802020-07-12 16:16:47 +03001159 io_req_init_async(req);
1160
Jens Axboed3656342019-12-18 09:50:26 -07001161 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001162 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001163 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001164 } else {
1165 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001166 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001167 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001168 if (!req->work.mm && def->needs_mm) {
1169 mmgrab(current->mm);
1170 req->work.mm = current->mm;
1171 }
1172 if (!req->work.creds)
1173 req->work.creds = get_current_cred();
1174 if (!req->work.fs && def->needs_fs) {
1175 spin_lock(&current->fs->lock);
1176 if (!current->fs->in_exec) {
1177 req->work.fs = current->fs;
1178 req->work.fs->users++;
1179 } else {
1180 req->work.flags |= IO_WQ_WORK_CANCEL;
1181 }
1182 spin_unlock(&current->fs->lock);
1183 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001184 if (def->needs_fsize)
1185 req->work.fsize = rlimit(RLIMIT_FSIZE);
1186 else
1187 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001188}
1189
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001190static void io_prep_async_link(struct io_kiocb *req)
1191{
1192 struct io_kiocb *cur;
1193
1194 io_prep_async_work(req);
1195 if (req->flags & REQ_F_LINK_HEAD)
1196 list_for_each_entry(cur, &req->link_list, link_list)
1197 io_prep_async_work(cur);
1198}
1199
Jens Axboe7271ef32020-08-10 09:55:22 -06001200static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001201{
Jackie Liua197f662019-11-08 08:09:12 -07001202 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001203 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001204
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001205 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1206 &req->work, req->flags);
1207 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001208 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001209}
1210
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001211static void io_queue_async_work(struct io_kiocb *req)
1212{
Jens Axboe7271ef32020-08-10 09:55:22 -06001213 struct io_kiocb *link;
1214
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001215 /* init ->work of the whole link before punting */
1216 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001217 link = __io_queue_async_work(req);
1218
1219 if (link)
1220 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001221}
1222
Jens Axboe5262f562019-09-17 12:26:57 -06001223static void io_kill_timeout(struct io_kiocb *req)
1224{
1225 int ret;
1226
Jens Axboe2d283902019-12-04 11:08:05 -07001227 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001228 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001229 atomic_set(&req->ctx->cq_timeouts,
1230 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001231 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001232 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001233 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001234 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001235 }
1236}
1237
Jens Axboef3606e32020-09-22 08:18:24 -06001238static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1239{
1240 struct io_ring_ctx *ctx = req->ctx;
1241
1242 if (!tsk || req->task == tsk)
1243 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001244 if (ctx->flags & IORING_SETUP_SQPOLL) {
1245 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1246 return true;
1247 }
Jens Axboef3606e32020-09-22 08:18:24 -06001248 return false;
1249}
1250
Jens Axboe76e1b642020-09-26 15:05:03 -06001251/*
1252 * Returns true if we found and killed one or more timeouts
1253 */
1254static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001255{
1256 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001257 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001258
1259 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001260 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001261 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001262 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001263 canceled++;
1264 }
Jens Axboef3606e32020-09-22 08:18:24 -06001265 }
Jens Axboe5262f562019-09-17 12:26:57 -06001266 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001267 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001268}
1269
Pavel Begunkov04518942020-05-26 20:34:05 +03001270static void __io_queue_deferred(struct io_ring_ctx *ctx)
1271{
1272 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001273 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1274 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001275 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001276
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001277 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001278 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001279 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001280 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001281 link = __io_queue_async_work(de->req);
1282 if (link) {
1283 __io_queue_linked_timeout(link);
1284 /* drop submission reference */
1285 link->flags |= REQ_F_COMP_LOCKED;
1286 io_put_req(link);
1287 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001288 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001289 } while (!list_empty(&ctx->defer_list));
1290}
1291
Pavel Begunkov360428f2020-05-30 14:54:17 +03001292static void io_flush_timeouts(struct io_ring_ctx *ctx)
1293{
1294 while (!list_empty(&ctx->timeout_list)) {
1295 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001296 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001297
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001298 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001299 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001300 if (req->timeout.target_seq != ctx->cached_cq_tail
1301 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001302 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001303
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001304 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001305 io_kill_timeout(req);
1306 }
1307}
1308
Jens Axboede0617e2019-04-06 21:51:27 -06001309static void io_commit_cqring(struct io_ring_ctx *ctx)
1310{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001311 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001312 __io_commit_cqring(ctx);
1313
Pavel Begunkov04518942020-05-26 20:34:05 +03001314 if (unlikely(!list_empty(&ctx->defer_list)))
1315 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001316}
1317
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1319{
Hristo Venev75b28af2019-08-26 17:23:46 +00001320 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001321 unsigned tail;
1322
1323 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001324 /*
1325 * writes to the cq entry need to come after reading head; the
1326 * control dependency is enough as we're using WRITE_ONCE to
1327 * fill the cq entry
1328 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001329 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330 return NULL;
1331
1332 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001333 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001334}
1335
Jens Axboef2842ab2020-01-08 11:04:00 -07001336static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1337{
Jens Axboef0b493e2020-02-01 21:30:11 -07001338 if (!ctx->cq_ev_fd)
1339 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001340 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1341 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001342 if (!ctx->eventfd_async)
1343 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001344 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001345}
1346
Jens Axboeb41e9852020-02-17 09:52:41 -07001347static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001348{
1349 if (waitqueue_active(&ctx->wait))
1350 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001351 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1352 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001353 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001354 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001355}
1356
Pavel Begunkov46930142020-07-30 18:43:49 +03001357static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1358{
1359 if (list_empty(&ctx->cq_overflow_list)) {
1360 clear_bit(0, &ctx->sq_check_overflow);
1361 clear_bit(0, &ctx->cq_check_overflow);
1362 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1363 }
1364}
1365
Jens Axboee6c8aa92020-09-28 13:10:13 -06001366static inline bool io_match_files(struct io_kiocb *req,
1367 struct files_struct *files)
1368{
1369 if (!files)
1370 return true;
1371 if (req->flags & REQ_F_WORK_INITIALIZED)
1372 return req->work.files == files;
1373 return false;
1374}
1375
Jens Axboec4a2ed72019-11-21 21:01:26 -07001376/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001377static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1378 struct task_struct *tsk,
1379 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001381 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001382 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001383 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001384 unsigned long flags;
1385 LIST_HEAD(list);
1386
1387 if (!force) {
1388 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001389 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001390 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1391 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001392 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001393 }
1394
1395 spin_lock_irqsave(&ctx->completion_lock, flags);
1396
1397 /* if force is set, the ring is going away. always drop after that */
1398 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001399 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001400
Jens Axboec4a2ed72019-11-21 21:01:26 -07001401 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001402 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1403 if (tsk && req->task != tsk)
1404 continue;
1405 if (!io_match_files(req, files))
1406 continue;
1407
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001408 cqe = io_get_cqring(ctx);
1409 if (!cqe && !force)
1410 break;
1411
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001412 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001413 if (cqe) {
1414 WRITE_ONCE(cqe->user_data, req->user_data);
1415 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001416 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001417 } else {
1418 WRITE_ONCE(ctx->rings->cq_overflow,
1419 atomic_inc_return(&ctx->cached_cq_overflow));
1420 }
1421 }
1422
1423 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001424 io_cqring_mark_overflow(ctx);
1425
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001426 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1427 io_cqring_ev_posted(ctx);
1428
1429 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001430 req = list_first_entry(&list, struct io_kiocb, compl.list);
1431 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001432 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001433 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001434
1435 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001436}
1437
Jens Axboebcda7ba2020-02-23 16:42:51 -07001438static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001439{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001440 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001441 struct io_uring_cqe *cqe;
1442
Jens Axboe78e19bb2019-11-06 15:21:34 -07001443 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001444
Jens Axboe2b188cc2019-01-07 10:46:33 -07001445 /*
1446 * If we can't get a cq entry, userspace overflowed the
1447 * submission (by quite a lot). Increment the overflow count in
1448 * the ring.
1449 */
1450 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001451 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001452 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001453 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001454 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001455 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1456 /*
1457 * If we're in ring overflow flush mode, or in task cancel mode,
1458 * then we cannot store the request for later flushing, we need
1459 * to drop it on the floor.
1460 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 WRITE_ONCE(ctx->rings->cq_overflow,
1462 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001463 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001464 if (list_empty(&ctx->cq_overflow_list)) {
1465 set_bit(0, &ctx->sq_check_overflow);
1466 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001467 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001468 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001469 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001470 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001471 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001472 refcount_inc(&req->refs);
1473 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001474 }
1475}
1476
Jens Axboebcda7ba2020-02-23 16:42:51 -07001477static void io_cqring_fill_event(struct io_kiocb *req, long res)
1478{
1479 __io_cqring_fill_event(req, res, 0);
1480}
1481
Jens Axboee1e16092020-06-22 09:17:17 -06001482static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001483{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001484 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001485 unsigned long flags;
1486
1487 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001488 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001489 io_commit_cqring(ctx);
1490 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1491
Jens Axboe8c838782019-03-12 15:48:16 -06001492 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001493}
1494
Jens Axboe229a7b62020-06-22 10:13:11 -06001495static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001496{
Jens Axboe229a7b62020-06-22 10:13:11 -06001497 struct io_ring_ctx *ctx = cs->ctx;
1498
1499 spin_lock_irq(&ctx->completion_lock);
1500 while (!list_empty(&cs->list)) {
1501 struct io_kiocb *req;
1502
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001503 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1504 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001505 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001506 if (!(req->flags & REQ_F_LINK_HEAD)) {
1507 req->flags |= REQ_F_COMP_LOCKED;
1508 io_put_req(req);
1509 } else {
1510 spin_unlock_irq(&ctx->completion_lock);
1511 io_put_req(req);
1512 spin_lock_irq(&ctx->completion_lock);
1513 }
1514 }
1515 io_commit_cqring(ctx);
1516 spin_unlock_irq(&ctx->completion_lock);
1517
1518 io_cqring_ev_posted(ctx);
1519 cs->nr = 0;
1520}
1521
1522static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1523 struct io_comp_state *cs)
1524{
1525 if (!cs) {
1526 io_cqring_add_event(req, res, cflags);
1527 io_put_req(req);
1528 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001529 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001530 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001531 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001532 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001533 if (++cs->nr >= 32)
1534 io_submit_flush_completions(cs);
1535 }
Jens Axboee1e16092020-06-22 09:17:17 -06001536}
1537
1538static void io_req_complete(struct io_kiocb *req, long res)
1539{
Jens Axboe229a7b62020-06-22 10:13:11 -06001540 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001541}
1542
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001543static inline bool io_is_fallback_req(struct io_kiocb *req)
1544{
1545 return req == (struct io_kiocb *)
1546 ((unsigned long) req->ctx->fallback_req & ~1UL);
1547}
1548
1549static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1550{
1551 struct io_kiocb *req;
1552
1553 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001554 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001555 return req;
1556
1557 return NULL;
1558}
1559
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001560static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1561 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001562{
Jens Axboefd6fab22019-03-14 16:30:06 -06001563 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001564 struct io_kiocb *req;
1565
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001566 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001567 size_t sz;
1568 int ret;
1569
1570 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001571 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1572
1573 /*
1574 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1575 * retry single alloc to be on the safe side.
1576 */
1577 if (unlikely(ret <= 0)) {
1578 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1579 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001580 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001581 ret = 1;
1582 }
Jens Axboe2579f912019-01-09 09:10:43 -07001583 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001584 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001585 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001586 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001587 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001588 }
1589
Jens Axboe2579f912019-01-09 09:10:43 -07001590 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001591fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001592 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001593}
1594
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001595static inline void io_put_file(struct io_kiocb *req, struct file *file,
1596 bool fixed)
1597{
1598 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001599 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001600 else
1601 fput(file);
1602}
1603
Jens Axboe51a4cc12020-08-10 10:55:56 -06001604static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001605{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001606 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001607
Jens Axboe5acbbc82020-07-08 15:15:26 -06001608 if (req->io)
1609 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001610 if (req->file)
1611 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001612
Jens Axboe51a4cc12020-08-10 10:55:56 -06001613 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001614}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001615
Jens Axboe51a4cc12020-08-10 10:55:56 -06001616static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001617{
Jens Axboe0f212202020-09-13 13:09:39 -06001618 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001619 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001620
Jens Axboe0f212202020-09-13 13:09:39 -06001621 atomic_long_inc(&tctx->req_complete);
1622 if (tctx->in_idle)
1623 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001624 put_task_struct(req->task);
1625
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001626 if (likely(!io_is_fallback_req(req)))
1627 kmem_cache_free(req_cachep, req);
1628 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001629 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1630 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001631}
1632
Jens Axboe51a4cc12020-08-10 10:55:56 -06001633static void io_req_task_file_table_put(struct callback_head *cb)
1634{
1635 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1636 struct fs_struct *fs = req->work.fs;
1637
1638 spin_lock(&req->work.fs->lock);
1639 if (--fs->users)
1640 fs = NULL;
1641 spin_unlock(&req->work.fs->lock);
1642 if (fs)
1643 free_fs_struct(fs);
1644 req->work.fs = NULL;
1645 __io_free_req_finish(req);
1646}
1647
1648static void __io_free_req(struct io_kiocb *req)
1649{
1650 if (!io_dismantle_req(req)) {
1651 __io_free_req_finish(req);
1652 } else {
1653 int ret;
1654
1655 init_task_work(&req->task_work, io_req_task_file_table_put);
1656 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1657 if (unlikely(ret)) {
1658 struct task_struct *tsk;
1659
1660 tsk = io_wq_get_task(req->ctx->io_wq);
1661 task_work_add(tsk, &req->task_work, 0);
1662 }
1663 }
1664}
1665
Jackie Liua197f662019-11-08 08:09:12 -07001666static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001667{
Jackie Liua197f662019-11-08 08:09:12 -07001668 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001669 int ret;
1670
Jens Axboe2d283902019-12-04 11:08:05 -07001671 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001672 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001673 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001674 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001675 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001676 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001677 return true;
1678 }
1679
1680 return false;
1681}
1682
Jens Axboeab0b6452020-06-30 08:43:15 -06001683static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001684{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001685 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001686 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001687
1688 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001689 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001690 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1691 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001692 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001693
1694 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001695 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001696 wake_ev = io_link_cancel_timeout(link);
1697 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001698 return wake_ev;
1699}
1700
1701static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001702{
Jens Axboe2665abf2019-11-05 12:40:47 -07001703 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001704 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001705
Jens Axboeab0b6452020-06-30 08:43:15 -06001706 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1707 unsigned long flags;
1708
1709 spin_lock_irqsave(&ctx->completion_lock, flags);
1710 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001711 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001712 } else {
1713 wake_ev = __io_kill_linked_timeout(req);
1714 }
1715
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001716 if (wake_ev)
1717 io_cqring_ev_posted(ctx);
1718}
1719
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001720static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001721{
1722 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001723
Jens Axboe9e645e112019-05-10 16:07:28 -06001724 /*
1725 * The list should never be empty when we are called here. But could
1726 * potentially happen if the chain is messed up, check to be on the
1727 * safe side.
1728 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001729 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001730 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001731
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001732 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1733 list_del_init(&req->link_list);
1734 if (!list_empty(&nxt->link_list))
1735 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001736 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001737}
1738
1739/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001740 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001741 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001742static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001743{
Jens Axboe2665abf2019-11-05 12:40:47 -07001744 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001745
1746 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001747 struct io_kiocb *link = list_first_entry(&req->link_list,
1748 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001749
Pavel Begunkov44932332019-12-05 16:16:35 +03001750 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001751 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001752
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001753 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001754 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001755 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001756 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001757 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001758
1759 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001760 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001761}
1762
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001763static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001764{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001765 struct io_ring_ctx *ctx = req->ctx;
1766
1767 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1768 unsigned long flags;
1769
1770 spin_lock_irqsave(&ctx->completion_lock, flags);
1771 __io_fail_links(req);
1772 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1773 } else {
1774 __io_fail_links(req);
1775 }
1776
Jens Axboe9e645e112019-05-10 16:07:28 -06001777 io_cqring_ev_posted(ctx);
1778}
1779
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001780static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001781{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001782 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001783 if (req->flags & REQ_F_LINK_TIMEOUT)
1784 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001785
Jens Axboe9e645e112019-05-10 16:07:28 -06001786 /*
1787 * If LINK is set, we have dependent requests in this chain. If we
1788 * didn't fail this request, queue the first one up, moving any other
1789 * dependencies to the next request. In case of failure, fail the rest
1790 * of the chain.
1791 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001792 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1793 return io_req_link_next(req);
1794 io_fail_links(req);
1795 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001796}
Jens Axboe2665abf2019-11-05 12:40:47 -07001797
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001798static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1799{
1800 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1801 return NULL;
1802 return __io_req_find_next(req);
1803}
1804
Jens Axboefd7d6de2020-08-23 11:00:37 -06001805static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1806 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001807{
1808 struct task_struct *tsk = req->task;
1809 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001810 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001811
Jens Axboe6200b0a2020-09-13 14:38:30 -06001812 if (tsk->flags & PF_EXITING)
1813 return -ESRCH;
1814
Jens Axboec2c4c832020-07-01 15:37:11 -06001815 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001816 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1817 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1818 * processing task_work. There's no reliable way to tell if TWA_RESUME
1819 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001820 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001821 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001822 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001823 notify = TWA_SIGNAL;
1824
1825 ret = task_work_add(tsk, cb, notify);
1826 if (!ret)
1827 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001828
Jens Axboec2c4c832020-07-01 15:37:11 -06001829 return ret;
1830}
1831
Jens Axboec40f6372020-06-25 15:39:59 -06001832static void __io_req_task_cancel(struct io_kiocb *req, int error)
1833{
1834 struct io_ring_ctx *ctx = req->ctx;
1835
1836 spin_lock_irq(&ctx->completion_lock);
1837 io_cqring_fill_event(req, error);
1838 io_commit_cqring(ctx);
1839 spin_unlock_irq(&ctx->completion_lock);
1840
1841 io_cqring_ev_posted(ctx);
1842 req_set_fail_links(req);
1843 io_double_put_req(req);
1844}
1845
1846static void io_req_task_cancel(struct callback_head *cb)
1847{
1848 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001849 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001850
1851 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001852 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001853}
1854
1855static void __io_req_task_submit(struct io_kiocb *req)
1856{
1857 struct io_ring_ctx *ctx = req->ctx;
1858
Jens Axboec40f6372020-06-25 15:39:59 -06001859 if (!__io_sq_thread_acquire_mm(ctx)) {
1860 mutex_lock(&ctx->uring_lock);
1861 __io_queue_sqe(req, NULL, NULL);
1862 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001863 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001864 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001865 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001866}
1867
Jens Axboec40f6372020-06-25 15:39:59 -06001868static void io_req_task_submit(struct callback_head *cb)
1869{
1870 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001871 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001872
1873 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001874 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001875}
1876
1877static void io_req_task_queue(struct io_kiocb *req)
1878{
Jens Axboec40f6372020-06-25 15:39:59 -06001879 int ret;
1880
1881 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001882 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001883
Jens Axboefd7d6de2020-08-23 11:00:37 -06001884 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001885 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001886 struct task_struct *tsk;
1887
Jens Axboec40f6372020-06-25 15:39:59 -06001888 init_task_work(&req->task_work, io_req_task_cancel);
1889 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001890 task_work_add(tsk, &req->task_work, 0);
1891 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001892 }
Jens Axboec40f6372020-06-25 15:39:59 -06001893}
1894
Pavel Begunkovc3524382020-06-28 12:52:32 +03001895static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001896{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001897 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001898
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001899 if (nxt)
1900 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001901}
1902
Jens Axboe9e645e112019-05-10 16:07:28 -06001903static void io_free_req(struct io_kiocb *req)
1904{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001905 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001906 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001907}
1908
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001909struct req_batch {
1910 void *reqs[IO_IOPOLL_BATCH];
1911 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001912
1913 struct task_struct *task;
1914 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001915};
1916
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001917static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001918{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001919 rb->to_free = 0;
1920 rb->task_refs = 0;
1921 rb->task = NULL;
1922}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001923
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001924static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1925 struct req_batch *rb)
1926{
1927 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1928 percpu_ref_put_many(&ctx->refs, rb->to_free);
1929 rb->to_free = 0;
1930}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001931
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001932static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1933 struct req_batch *rb)
1934{
1935 if (rb->to_free)
1936 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001937 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001938 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001939 put_task_struct_many(rb->task, rb->task_refs);
1940 rb->task = NULL;
1941 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001942}
1943
1944static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1945{
1946 if (unlikely(io_is_fallback_req(req))) {
1947 io_free_req(req);
1948 return;
1949 }
1950 if (req->flags & REQ_F_LINK_HEAD)
1951 io_queue_next(req);
1952
Jens Axboee3bc8e92020-09-24 08:45:57 -06001953 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001954 if (rb->task) {
1955 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001956 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06001957 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001958 rb->task = req->task;
1959 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001960 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001961 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001962
Jens Axboe51a4cc12020-08-10 10:55:56 -06001963 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001964 rb->reqs[rb->to_free++] = req;
1965 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1966 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001967}
1968
Jens Axboeba816ad2019-09-28 11:36:45 -06001969/*
1970 * Drop reference to request, return next in chain (if there is one) if this
1971 * was the last reference to this request.
1972 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001973static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001974{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001975 struct io_kiocb *nxt = NULL;
1976
Jens Axboe2a44f462020-02-25 13:25:41 -07001977 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001978 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001979 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001980 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001981 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001982}
1983
Jens Axboe2b188cc2019-01-07 10:46:33 -07001984static void io_put_req(struct io_kiocb *req)
1985{
Jens Axboedef596e2019-01-09 08:59:42 -07001986 if (refcount_dec_and_test(&req->refs))
1987 io_free_req(req);
1988}
1989
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001990static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001991{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001992 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001993
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001994 /*
1995 * A ref is owned by io-wq in which context we're. So, if that's the
1996 * last one, it's safe to steal next work. False negatives are Ok,
1997 * it just will be re-punted async in io_put_work()
1998 */
1999 if (refcount_read(&req->refs) != 1)
2000 return NULL;
2001
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002002 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002003 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002004}
2005
Jens Axboe978db572019-11-14 22:39:04 -07002006/*
2007 * Must only be used if we don't need to care about links, usually from
2008 * within the completion handling itself.
2009 */
2010static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002011{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002012 /* drop both submit and complete references */
2013 if (refcount_sub_and_test(2, &req->refs))
2014 __io_free_req(req);
2015}
2016
Jens Axboe978db572019-11-14 22:39:04 -07002017static void io_double_put_req(struct io_kiocb *req)
2018{
2019 /* drop both submit and complete references */
2020 if (refcount_sub_and_test(2, &req->refs))
2021 io_free_req(req);
2022}
2023
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002024static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002025{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002026 struct io_rings *rings = ctx->rings;
2027
Jens Axboead3eb2c2019-12-18 17:12:20 -07002028 if (test_bit(0, &ctx->cq_check_overflow)) {
2029 /*
2030 * noflush == true is from the waitqueue handler, just ensure
2031 * we wake up the task, and the next invocation will flush the
2032 * entries. We cannot safely to it from here.
2033 */
2034 if (noflush && !list_empty(&ctx->cq_overflow_list))
2035 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002036
Jens Axboee6c8aa92020-09-28 13:10:13 -06002037 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002038 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002039
Jens Axboea3a0e432019-08-20 11:03:11 -06002040 /* See comment at the top of this file */
2041 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002042 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002043}
2044
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002045static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2046{
2047 struct io_rings *rings = ctx->rings;
2048
2049 /* make sure SQ entry isn't read before tail */
2050 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2051}
2052
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002053static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002054{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002055 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002056
Jens Axboebcda7ba2020-02-23 16:42:51 -07002057 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2058 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002059 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002060 kfree(kbuf);
2061 return cflags;
2062}
2063
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002064static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2065{
2066 struct io_buffer *kbuf;
2067
2068 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2069 return io_put_kbuf(req, kbuf);
2070}
2071
Jens Axboe4c6e2772020-07-01 11:29:10 -06002072static inline bool io_run_task_work(void)
2073{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002074 /*
2075 * Not safe to run on exiting task, and the task_work handling will
2076 * not add work to such a task.
2077 */
2078 if (unlikely(current->flags & PF_EXITING))
2079 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002080 if (current->task_works) {
2081 __set_current_state(TASK_RUNNING);
2082 task_work_run();
2083 return true;
2084 }
2085
2086 return false;
2087}
2088
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002089static void io_iopoll_queue(struct list_head *again)
2090{
2091 struct io_kiocb *req;
2092
2093 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002094 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2095 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002096 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002097 } while (!list_empty(again));
2098}
2099
Jens Axboedef596e2019-01-09 08:59:42 -07002100/*
2101 * Find and free completed poll iocbs
2102 */
2103static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2104 struct list_head *done)
2105{
Jens Axboe8237e042019-12-28 10:48:22 -07002106 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002107 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002108 LIST_HEAD(again);
2109
2110 /* order with ->result store in io_complete_rw_iopoll() */
2111 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002112
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002113 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002114 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002115 int cflags = 0;
2116
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002117 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002118 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002119 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002120 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002121 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002122 continue;
2123 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002124 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002125
Jens Axboebcda7ba2020-02-23 16:42:51 -07002126 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002127 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002128
2129 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002130 (*nr_events)++;
2131
Pavel Begunkovc3524382020-06-28 12:52:32 +03002132 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002133 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002134 }
Jens Axboedef596e2019-01-09 08:59:42 -07002135
Jens Axboe09bb8392019-03-13 12:39:28 -06002136 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002137 if (ctx->flags & IORING_SETUP_SQPOLL)
2138 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002139 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002140
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002141 if (!list_empty(&again))
2142 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002143}
2144
Jens Axboedef596e2019-01-09 08:59:42 -07002145static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2146 long min)
2147{
2148 struct io_kiocb *req, *tmp;
2149 LIST_HEAD(done);
2150 bool spin;
2151 int ret;
2152
2153 /*
2154 * Only spin for completions if we don't have multiple devices hanging
2155 * off our complete list, and we're under the requested amount.
2156 */
2157 spin = !ctx->poll_multi_file && *nr_events < min;
2158
2159 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002160 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002161 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002162
2163 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002164 * Move completed and retryable entries to our local lists.
2165 * If we find a request that requires polling, break out
2166 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002167 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002168 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002169 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002170 continue;
2171 }
2172 if (!list_empty(&done))
2173 break;
2174
2175 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2176 if (ret < 0)
2177 break;
2178
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002179 /* iopoll may have completed current req */
2180 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002181 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002182
Jens Axboedef596e2019-01-09 08:59:42 -07002183 if (ret && spin)
2184 spin = false;
2185 ret = 0;
2186 }
2187
2188 if (!list_empty(&done))
2189 io_iopoll_complete(ctx, nr_events, &done);
2190
2191 return ret;
2192}
2193
2194/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002195 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002196 * non-spinning poll check - we'll still enter the driver poll loop, but only
2197 * as a non-spinning completion check.
2198 */
2199static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2200 long min)
2201{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002202 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002203 int ret;
2204
2205 ret = io_do_iopoll(ctx, nr_events, min);
2206 if (ret < 0)
2207 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002208 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002209 return 0;
2210 }
2211
2212 return 1;
2213}
2214
2215/*
2216 * We can't just wait for polled events to come to us, we have to actively
2217 * find and complete them.
2218 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002219static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002220{
2221 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2222 return;
2223
2224 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002225 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002226 unsigned int nr_events = 0;
2227
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002228 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002229
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002230 /* let it sleep and repeat later if can't complete a request */
2231 if (nr_events == 0)
2232 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002233 /*
2234 * Ensure we allow local-to-the-cpu processing to take place,
2235 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002236 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002237 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002238 if (need_resched()) {
2239 mutex_unlock(&ctx->uring_lock);
2240 cond_resched();
2241 mutex_lock(&ctx->uring_lock);
2242 }
Jens Axboedef596e2019-01-09 08:59:42 -07002243 }
2244 mutex_unlock(&ctx->uring_lock);
2245}
2246
Pavel Begunkov7668b922020-07-07 16:36:21 +03002247static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002248{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002249 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002250 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002251
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002252 /*
2253 * We disallow the app entering submit/complete with polling, but we
2254 * still need to lock the ring to prevent racing with polled issue
2255 * that got punted to a workqueue.
2256 */
2257 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002258 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002259 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002260 * Don't enter poll loop if we already have events pending.
2261 * If we do, we can potentially be spinning for commands that
2262 * already triggered a CQE (eg in error).
2263 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002264 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002265 break;
2266
2267 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002268 * If a submit got punted to a workqueue, we can have the
2269 * application entering polling for a command before it gets
2270 * issued. That app will hold the uring_lock for the duration
2271 * of the poll right here, so we need to take a breather every
2272 * now and then to ensure that the issue has a chance to add
2273 * the poll to the issued list. Otherwise we can spin here
2274 * forever, while the workqueue is stuck trying to acquire the
2275 * very same mutex.
2276 */
2277 if (!(++iters & 7)) {
2278 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002279 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002280 mutex_lock(&ctx->uring_lock);
2281 }
2282
Pavel Begunkov7668b922020-07-07 16:36:21 +03002283 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002284 if (ret <= 0)
2285 break;
2286 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002287 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002288
Jens Axboe500f9fb2019-08-19 12:15:59 -06002289 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002290 return ret;
2291}
2292
Jens Axboe491381ce2019-10-17 09:20:46 -06002293static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002294{
Jens Axboe491381ce2019-10-17 09:20:46 -06002295 /*
2296 * Tell lockdep we inherited freeze protection from submission
2297 * thread.
2298 */
2299 if (req->flags & REQ_F_ISREG) {
2300 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002301
Jens Axboe491381ce2019-10-17 09:20:46 -06002302 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002304 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002305}
2306
Jens Axboea1d7c392020-06-22 11:09:46 -06002307static void io_complete_rw_common(struct kiocb *kiocb, long res,
2308 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002309{
Jens Axboe9adbd452019-12-20 08:45:55 -07002310 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002311 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312
Jens Axboe491381ce2019-10-17 09:20:46 -06002313 if (kiocb->ki_flags & IOCB_WRITE)
2314 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002315
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002316 if (res != req->result)
2317 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002318 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002319 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002320 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002321}
2322
Jens Axboeb63534c2020-06-04 11:28:00 -06002323#ifdef CONFIG_BLOCK
2324static bool io_resubmit_prep(struct io_kiocb *req, int error)
2325{
2326 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2327 ssize_t ret = -ECANCELED;
2328 struct iov_iter iter;
2329 int rw;
2330
2331 if (error) {
2332 ret = error;
2333 goto end_req;
2334 }
2335
2336 switch (req->opcode) {
2337 case IORING_OP_READV:
2338 case IORING_OP_READ_FIXED:
2339 case IORING_OP_READ:
2340 rw = READ;
2341 break;
2342 case IORING_OP_WRITEV:
2343 case IORING_OP_WRITE_FIXED:
2344 case IORING_OP_WRITE:
2345 rw = WRITE;
2346 break;
2347 default:
2348 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2349 req->opcode);
2350 goto end_req;
2351 }
2352
Jens Axboe8f3d7492020-09-14 09:28:14 -06002353 if (!req->io) {
2354 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2355 if (ret < 0)
2356 goto end_req;
2357 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2358 if (!ret)
2359 return true;
2360 kfree(iovec);
2361 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002362 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002363 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002364end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002365 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002366 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002367 return false;
2368}
Jens Axboeb63534c2020-06-04 11:28:00 -06002369#endif
2370
2371static bool io_rw_reissue(struct io_kiocb *req, long res)
2372{
2373#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002374 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002375 int ret;
2376
Jens Axboe355afae2020-09-02 09:30:31 -06002377 if (!S_ISBLK(mode) && !S_ISREG(mode))
2378 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002379 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2380 return false;
2381
Jens Axboefdee9462020-08-27 16:46:24 -06002382 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002383
Jens Axboefdee9462020-08-27 16:46:24 -06002384 if (io_resubmit_prep(req, ret)) {
2385 refcount_inc(&req->refs);
2386 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002387 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002388 }
2389
Jens Axboeb63534c2020-06-04 11:28:00 -06002390#endif
2391 return false;
2392}
2393
Jens Axboea1d7c392020-06-22 11:09:46 -06002394static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2395 struct io_comp_state *cs)
2396{
2397 if (!io_rw_reissue(req, res))
2398 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002399}
2400
2401static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2402{
Jens Axboe9adbd452019-12-20 08:45:55 -07002403 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002404
Jens Axboea1d7c392020-06-22 11:09:46 -06002405 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002406}
2407
Jens Axboedef596e2019-01-09 08:59:42 -07002408static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2409{
Jens Axboe9adbd452019-12-20 08:45:55 -07002410 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002411
Jens Axboe491381ce2019-10-17 09:20:46 -06002412 if (kiocb->ki_flags & IOCB_WRITE)
2413 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002414
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002415 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002416 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002417
2418 WRITE_ONCE(req->result, res);
2419 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002420 smp_wmb();
2421 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002422}
2423
2424/*
2425 * After the iocb has been issued, it's safe to be found on the poll list.
2426 * Adding the kiocb to the list AFTER submission ensures that we don't
2427 * find it from a io_iopoll_getevents() thread before the issuer is done
2428 * accessing the kiocb cookie.
2429 */
2430static void io_iopoll_req_issued(struct io_kiocb *req)
2431{
2432 struct io_ring_ctx *ctx = req->ctx;
2433
2434 /*
2435 * Track whether we have multiple files in our lists. This will impact
2436 * how we do polling eventually, not spinning if we're on potentially
2437 * different devices.
2438 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002439 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002440 ctx->poll_multi_file = false;
2441 } else if (!ctx->poll_multi_file) {
2442 struct io_kiocb *list_req;
2443
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002444 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002445 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002446 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002447 ctx->poll_multi_file = true;
2448 }
2449
2450 /*
2451 * For fast devices, IO may have already completed. If it has, add
2452 * it to the front so we find it first.
2453 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002454 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002455 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002456 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002457 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002458
Jens Axboe534ca6d2020-09-02 13:52:19 -06002459 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2460 wq_has_sleeper(&ctx->sq_data->wait))
2461 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002462}
2463
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002464static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002465{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002466 if (state->has_refs)
2467 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002468 state->file = NULL;
2469}
2470
2471static inline void io_state_file_put(struct io_submit_state *state)
2472{
2473 if (state->file)
2474 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002475}
2476
2477/*
2478 * Get as many references to a file as we have IOs left in this submission,
2479 * assuming most submissions are for one file, or at least that each file
2480 * has more than one submission.
2481 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002482static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002483{
2484 if (!state)
2485 return fget(fd);
2486
2487 if (state->file) {
2488 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002489 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002490 state->ios_left--;
2491 return state->file;
2492 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002493 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002494 }
2495 state->file = fget_many(fd, state->ios_left);
2496 if (!state->file)
2497 return NULL;
2498
2499 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002500 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002501 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002502 return state->file;
2503}
2504
Jens Axboe4503b762020-06-01 10:00:27 -06002505static bool io_bdev_nowait(struct block_device *bdev)
2506{
2507#ifdef CONFIG_BLOCK
2508 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2509#else
2510 return true;
2511#endif
2512}
2513
Jens Axboe2b188cc2019-01-07 10:46:33 -07002514/*
2515 * If we tracked the file through the SCM inflight mechanism, we could support
2516 * any file. For now, just ensure that anything potentially problematic is done
2517 * inline.
2518 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002519static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520{
2521 umode_t mode = file_inode(file)->i_mode;
2522
Jens Axboe4503b762020-06-01 10:00:27 -06002523 if (S_ISBLK(mode)) {
2524 if (io_bdev_nowait(file->f_inode->i_bdev))
2525 return true;
2526 return false;
2527 }
2528 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002529 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002530 if (S_ISREG(mode)) {
2531 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2532 file->f_op != &io_uring_fops)
2533 return true;
2534 return false;
2535 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002536
Jens Axboec5b85622020-06-09 19:23:05 -06002537 /* any ->read/write should understand O_NONBLOCK */
2538 if (file->f_flags & O_NONBLOCK)
2539 return true;
2540
Jens Axboeaf197f52020-04-28 13:15:06 -06002541 if (!(file->f_mode & FMODE_NOWAIT))
2542 return false;
2543
2544 if (rw == READ)
2545 return file->f_op->read_iter != NULL;
2546
2547 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002548}
2549
Jens Axboe3529d8c2019-12-19 18:24:38 -07002550static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2551 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002552{
Jens Axboedef596e2019-01-09 08:59:42 -07002553 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002554 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002555 unsigned ioprio;
2556 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002557
Jens Axboe491381ce2019-10-17 09:20:46 -06002558 if (S_ISREG(file_inode(req->file)->i_mode))
2559 req->flags |= REQ_F_ISREG;
2560
Jens Axboe2b188cc2019-01-07 10:46:33 -07002561 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002562 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2563 req->flags |= REQ_F_CUR_POS;
2564 kiocb->ki_pos = req->file->f_pos;
2565 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002566 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002567 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2568 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2569 if (unlikely(ret))
2570 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002571
2572 ioprio = READ_ONCE(sqe->ioprio);
2573 if (ioprio) {
2574 ret = ioprio_check_cap(ioprio);
2575 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002576 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002577
2578 kiocb->ki_ioprio = ioprio;
2579 } else
2580 kiocb->ki_ioprio = get_current_ioprio();
2581
Stefan Bühler8449eed2019-04-27 20:34:19 +02002582 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002583 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002584 req->flags |= REQ_F_NOWAIT;
2585
2586 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002587 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002588
Jens Axboedef596e2019-01-09 08:59:42 -07002589 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002590 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2591 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002592 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002593
Jens Axboedef596e2019-01-09 08:59:42 -07002594 kiocb->ki_flags |= IOCB_HIPRI;
2595 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002596 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002597 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002598 if (kiocb->ki_flags & IOCB_HIPRI)
2599 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002600 kiocb->ki_complete = io_complete_rw;
2601 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002602
Jens Axboe3529d8c2019-12-19 18:24:38 -07002603 req->rw.addr = READ_ONCE(sqe->addr);
2604 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002605 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002606 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002607}
2608
2609static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2610{
2611 switch (ret) {
2612 case -EIOCBQUEUED:
2613 break;
2614 case -ERESTARTSYS:
2615 case -ERESTARTNOINTR:
2616 case -ERESTARTNOHAND:
2617 case -ERESTART_RESTARTBLOCK:
2618 /*
2619 * We can't just restart the syscall, since previously
2620 * submitted sqes may already be in progress. Just fail this
2621 * IO with EINTR.
2622 */
2623 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002624 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625 default:
2626 kiocb->ki_complete(kiocb, ret, 0);
2627 }
2628}
2629
Jens Axboea1d7c392020-06-22 11:09:46 -06002630static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2631 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002632{
Jens Axboeba042912019-12-25 16:33:42 -07002633 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2634
Jens Axboe227c0c92020-08-13 11:51:40 -06002635 /* add previously done IO, if any */
2636 if (req->io && req->io->rw.bytes_done > 0) {
2637 if (ret < 0)
2638 ret = req->io->rw.bytes_done;
2639 else
2640 ret += req->io->rw.bytes_done;
2641 }
2642
Jens Axboeba042912019-12-25 16:33:42 -07002643 if (req->flags & REQ_F_CUR_POS)
2644 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002645 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002646 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002647 else
2648 io_rw_done(kiocb, ret);
2649}
2650
Jens Axboe9adbd452019-12-20 08:45:55 -07002651static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002652 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002653{
Jens Axboe9adbd452019-12-20 08:45:55 -07002654 struct io_ring_ctx *ctx = req->ctx;
2655 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002656 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002657 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002658 size_t offset;
2659 u64 buf_addr;
2660
2661 /* attempt to use fixed buffers without having provided iovecs */
2662 if (unlikely(!ctx->user_bufs))
2663 return -EFAULT;
2664
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002665 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002666 if (unlikely(buf_index >= ctx->nr_user_bufs))
2667 return -EFAULT;
2668
2669 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2670 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002671 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002672
2673 /* overflow */
2674 if (buf_addr + len < buf_addr)
2675 return -EFAULT;
2676 /* not inside the mapped region */
2677 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2678 return -EFAULT;
2679
2680 /*
2681 * May not be a start of buffer, set size appropriately
2682 * and advance us to the beginning.
2683 */
2684 offset = buf_addr - imu->ubuf;
2685 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002686
2687 if (offset) {
2688 /*
2689 * Don't use iov_iter_advance() here, as it's really slow for
2690 * using the latter parts of a big fixed buffer - it iterates
2691 * over each segment manually. We can cheat a bit here, because
2692 * we know that:
2693 *
2694 * 1) it's a BVEC iter, we set it up
2695 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2696 * first and last bvec
2697 *
2698 * So just find our index, and adjust the iterator afterwards.
2699 * If the offset is within the first bvec (or the whole first
2700 * bvec, just use iov_iter_advance(). This makes it easier
2701 * since we can just skip the first segment, which may not
2702 * be PAGE_SIZE aligned.
2703 */
2704 const struct bio_vec *bvec = imu->bvec;
2705
2706 if (offset <= bvec->bv_len) {
2707 iov_iter_advance(iter, offset);
2708 } else {
2709 unsigned long seg_skip;
2710
2711 /* skip first vec */
2712 offset -= bvec->bv_len;
2713 seg_skip = 1 + (offset >> PAGE_SHIFT);
2714
2715 iter->bvec = bvec + seg_skip;
2716 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002717 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002718 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002719 }
2720 }
2721
Jens Axboe5e559562019-11-13 16:12:46 -07002722 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002723}
2724
Jens Axboebcda7ba2020-02-23 16:42:51 -07002725static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2726{
2727 if (needs_lock)
2728 mutex_unlock(&ctx->uring_lock);
2729}
2730
2731static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2732{
2733 /*
2734 * "Normal" inline submissions always hold the uring_lock, since we
2735 * grab it from the system call. Same is true for the SQPOLL offload.
2736 * The only exception is when we've detached the request and issue it
2737 * from an async worker thread, grab the lock for that case.
2738 */
2739 if (needs_lock)
2740 mutex_lock(&ctx->uring_lock);
2741}
2742
2743static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2744 int bgid, struct io_buffer *kbuf,
2745 bool needs_lock)
2746{
2747 struct io_buffer *head;
2748
2749 if (req->flags & REQ_F_BUFFER_SELECTED)
2750 return kbuf;
2751
2752 io_ring_submit_lock(req->ctx, needs_lock);
2753
2754 lockdep_assert_held(&req->ctx->uring_lock);
2755
2756 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2757 if (head) {
2758 if (!list_empty(&head->list)) {
2759 kbuf = list_last_entry(&head->list, struct io_buffer,
2760 list);
2761 list_del(&kbuf->list);
2762 } else {
2763 kbuf = head;
2764 idr_remove(&req->ctx->io_buffer_idr, bgid);
2765 }
2766 if (*len > kbuf->len)
2767 *len = kbuf->len;
2768 } else {
2769 kbuf = ERR_PTR(-ENOBUFS);
2770 }
2771
2772 io_ring_submit_unlock(req->ctx, needs_lock);
2773
2774 return kbuf;
2775}
2776
Jens Axboe4d954c22020-02-27 07:31:19 -07002777static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2778 bool needs_lock)
2779{
2780 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002781 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002782
2783 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002784 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002785 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2786 if (IS_ERR(kbuf))
2787 return kbuf;
2788 req->rw.addr = (u64) (unsigned long) kbuf;
2789 req->flags |= REQ_F_BUFFER_SELECTED;
2790 return u64_to_user_ptr(kbuf->addr);
2791}
2792
2793#ifdef CONFIG_COMPAT
2794static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2795 bool needs_lock)
2796{
2797 struct compat_iovec __user *uiov;
2798 compat_ssize_t clen;
2799 void __user *buf;
2800 ssize_t len;
2801
2802 uiov = u64_to_user_ptr(req->rw.addr);
2803 if (!access_ok(uiov, sizeof(*uiov)))
2804 return -EFAULT;
2805 if (__get_user(clen, &uiov->iov_len))
2806 return -EFAULT;
2807 if (clen < 0)
2808 return -EINVAL;
2809
2810 len = clen;
2811 buf = io_rw_buffer_select(req, &len, needs_lock);
2812 if (IS_ERR(buf))
2813 return PTR_ERR(buf);
2814 iov[0].iov_base = buf;
2815 iov[0].iov_len = (compat_size_t) len;
2816 return 0;
2817}
2818#endif
2819
2820static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2821 bool needs_lock)
2822{
2823 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2824 void __user *buf;
2825 ssize_t len;
2826
2827 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2828 return -EFAULT;
2829
2830 len = iov[0].iov_len;
2831 if (len < 0)
2832 return -EINVAL;
2833 buf = io_rw_buffer_select(req, &len, needs_lock);
2834 if (IS_ERR(buf))
2835 return PTR_ERR(buf);
2836 iov[0].iov_base = buf;
2837 iov[0].iov_len = len;
2838 return 0;
2839}
2840
2841static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2842 bool needs_lock)
2843{
Jens Axboedddb3e22020-06-04 11:27:01 -06002844 if (req->flags & REQ_F_BUFFER_SELECTED) {
2845 struct io_buffer *kbuf;
2846
2847 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2848 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2849 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002850 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002851 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002852 if (!req->rw.len)
2853 return 0;
2854 else if (req->rw.len > 1)
2855 return -EINVAL;
2856
2857#ifdef CONFIG_COMPAT
2858 if (req->ctx->compat)
2859 return io_compat_import(req, iov, needs_lock);
2860#endif
2861
2862 return __io_iov_buffer_select(req, iov, needs_lock);
2863}
2864
Jens Axboe8452fd02020-08-18 13:58:33 -07002865static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2866 struct iovec **iovec, struct iov_iter *iter,
2867 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002868{
Jens Axboe9adbd452019-12-20 08:45:55 -07002869 void __user *buf = u64_to_user_ptr(req->rw.addr);
2870 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002871 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002872 u8 opcode;
2873
Jens Axboed625c6e2019-12-17 19:53:05 -07002874 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002875 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002876 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002877 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002878 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002879
Jens Axboebcda7ba2020-02-23 16:42:51 -07002880 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002881 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002882 return -EINVAL;
2883
Jens Axboe3a6820f2019-12-22 15:19:35 -07002884 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002885 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002886 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002887 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002888 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002889 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002890 }
2891
Jens Axboe3a6820f2019-12-22 15:19:35 -07002892 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2893 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002894 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002895 }
2896
Jens Axboe4d954c22020-02-27 07:31:19 -07002897 if (req->flags & REQ_F_BUFFER_SELECT) {
2898 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002899 if (!ret) {
2900 ret = (*iovec)->iov_len;
2901 iov_iter_init(iter, rw, *iovec, 1, ret);
2902 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002903 *iovec = NULL;
2904 return ret;
2905 }
2906
Jens Axboe2b188cc2019-01-07 10:46:33 -07002907#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002908 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002909 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2910 iovec, iter);
2911#endif
2912
2913 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2914}
2915
Jens Axboe8452fd02020-08-18 13:58:33 -07002916static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2917 struct iovec **iovec, struct iov_iter *iter,
2918 bool needs_lock)
2919{
2920 if (!req->io)
2921 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2922 *iovec = NULL;
2923 return iov_iter_count(&req->io->rw.iter);
2924}
2925
Jens Axboe0fef9482020-08-26 10:36:20 -06002926static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2927{
2928 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2929}
2930
Jens Axboe32960612019-09-23 11:05:34 -06002931/*
2932 * For files that don't have ->read_iter() and ->write_iter(), handle them
2933 * by looping over ->read() or ->write() manually.
2934 */
2935static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2936 struct iov_iter *iter)
2937{
2938 ssize_t ret = 0;
2939
2940 /*
2941 * Don't support polled IO through this interface, and we can't
2942 * support non-blocking either. For the latter, this just causes
2943 * the kiocb to be handled from an async context.
2944 */
2945 if (kiocb->ki_flags & IOCB_HIPRI)
2946 return -EOPNOTSUPP;
2947 if (kiocb->ki_flags & IOCB_NOWAIT)
2948 return -EAGAIN;
2949
2950 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002951 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002952 ssize_t nr;
2953
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002954 if (!iov_iter_is_bvec(iter)) {
2955 iovec = iov_iter_iovec(iter);
2956 } else {
2957 /* fixed buffers import bvec */
2958 iovec.iov_base = kmap(iter->bvec->bv_page)
2959 + iter->iov_offset;
2960 iovec.iov_len = min(iter->count,
2961 iter->bvec->bv_len - iter->iov_offset);
2962 }
2963
Jens Axboe32960612019-09-23 11:05:34 -06002964 if (rw == READ) {
2965 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002966 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002967 } else {
2968 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002969 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002970 }
2971
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002972 if (iov_iter_is_bvec(iter))
2973 kunmap(iter->bvec->bv_page);
2974
Jens Axboe32960612019-09-23 11:05:34 -06002975 if (nr < 0) {
2976 if (!ret)
2977 ret = nr;
2978 break;
2979 }
2980 ret += nr;
2981 if (nr != iovec.iov_len)
2982 break;
2983 iov_iter_advance(iter, nr);
2984 }
2985
2986 return ret;
2987}
2988
Jens Axboeff6165b2020-08-13 09:47:43 -06002989static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2990 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07002991{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002992 struct io_async_rw *rw = &req->io->rw;
2993
Jens Axboeff6165b2020-08-13 09:47:43 -06002994 memcpy(&rw->iter, iter, sizeof(*iter));
2995 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06002996 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06002997 /* can only be fixed buffers, no need to do anything */
2998 if (iter->type == ITER_BVEC)
2999 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003000 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003001 unsigned iov_off = 0;
3002
3003 rw->iter.iov = rw->fast_iov;
3004 if (iter->iov != fast_iov) {
3005 iov_off = iter->iov - fast_iov;
3006 rw->iter.iov += iov_off;
3007 }
3008 if (rw->fast_iov != fast_iov)
3009 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003010 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003011 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06003012 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003013 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003014 }
3015}
3016
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003017static inline int __io_alloc_async_ctx(struct io_kiocb *req)
3018{
3019 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
3020 return req->io == NULL;
3021}
3022
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003023static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003024{
Jens Axboed3656342019-12-18 09:50:26 -07003025 if (!io_op_defs[req->opcode].async_ctx)
3026 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003027
3028 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003029}
3030
Jens Axboeff6165b2020-08-13 09:47:43 -06003031static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3032 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003033 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003034{
Jens Axboe227c0c92020-08-13 11:51:40 -06003035 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003036 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003037 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003038 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003039 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003040
Jens Axboeff6165b2020-08-13 09:47:43 -06003041 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003042 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003043 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003044}
3045
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003046static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3047 bool force_nonblock)
3048{
Jens Axboeff6165b2020-08-13 09:47:43 -06003049 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06003050 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003051 ssize_t ret;
3052
Jens Axboec183edf2020-09-04 22:36:52 -06003053 iorw->iter.iov = iov = iorw->fast_iov;
3054 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003055 if (unlikely(ret < 0))
3056 return ret;
3057
Jens Axboec183edf2020-09-04 22:36:52 -06003058 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003059 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003060 return 0;
3061}
3062
Jens Axboe3529d8c2019-12-19 18:24:38 -07003063static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3064 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003065{
3066 ssize_t ret;
3067
Jens Axboe3529d8c2019-12-19 18:24:38 -07003068 ret = io_prep_rw(req, sqe, force_nonblock);
3069 if (ret)
3070 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003071
Jens Axboe3529d8c2019-12-19 18:24:38 -07003072 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3073 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003074
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003075 /* either don't need iovec imported or already have it */
3076 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003077 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003078 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003079}
3080
Jens Axboec1dd91d2020-08-03 16:43:59 -06003081/*
3082 * This is our waitqueue callback handler, registered through lock_page_async()
3083 * when we initially tried to do the IO with the iocb armed our waitqueue.
3084 * This gets called when the page is unlocked, and we generally expect that to
3085 * happen when the page IO is completed and the page is now uptodate. This will
3086 * queue a task_work based retry of the operation, attempting to copy the data
3087 * again. If the latter fails because the page was NOT uptodate, then we will
3088 * do a thread based blocking retry of the operation. That's the unexpected
3089 * slow path.
3090 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003091static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3092 int sync, void *arg)
3093{
3094 struct wait_page_queue *wpq;
3095 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003096 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003097 int ret;
3098
3099 wpq = container_of(wait, struct wait_page_queue, wait);
3100
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003101 if (!wake_page_match(wpq, key))
3102 return 0;
3103
Hao Xuc8d317a2020-09-29 20:00:45 +08003104 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003105 list_del_init(&wait->entry);
3106
Pavel Begunkove7375122020-07-12 20:42:04 +03003107 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003108 percpu_ref_get(&req->ctx->refs);
3109
Jens Axboebcf5a062020-05-22 09:24:42 -06003110 /* submit ref gets dropped, acquire a new one */
3111 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003112 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003113 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003114 struct task_struct *tsk;
3115
Jens Axboebcf5a062020-05-22 09:24:42 -06003116 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003117 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003118 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003119 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003120 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003121 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003122 return 1;
3123}
3124
Jens Axboec1dd91d2020-08-03 16:43:59 -06003125/*
3126 * This controls whether a given IO request should be armed for async page
3127 * based retry. If we return false here, the request is handed to the async
3128 * worker threads for retry. If we're doing buffered reads on a regular file,
3129 * we prepare a private wait_page_queue entry and retry the operation. This
3130 * will either succeed because the page is now uptodate and unlocked, or it
3131 * will register a callback when the page is unlocked at IO completion. Through
3132 * that callback, io_uring uses task_work to setup a retry of the operation.
3133 * That retry will attempt the buffered read again. The retry will generally
3134 * succeed, or in rare cases where it fails, we then fall back to using the
3135 * async worker threads for a blocking retry.
3136 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003137static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003138{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003139 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003140 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003141
3142 /* never retry for NOWAIT, we just complete with -EAGAIN */
3143 if (req->flags & REQ_F_NOWAIT)
3144 return false;
3145
Jens Axboe227c0c92020-08-13 11:51:40 -06003146 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003147 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003148 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003149
Jens Axboebcf5a062020-05-22 09:24:42 -06003150 /*
3151 * just use poll if we can, and don't attempt if the fs doesn't
3152 * support callback based unlocks
3153 */
3154 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3155 return false;
3156
Jens Axboe3b2a4432020-08-16 10:58:43 -07003157 wait->wait.func = io_async_buf_func;
3158 wait->wait.private = req;
3159 wait->wait.flags = 0;
3160 INIT_LIST_HEAD(&wait->wait.entry);
3161 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003162 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003163 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003164 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003165}
3166
3167static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3168{
3169 if (req->file->f_op->read_iter)
3170 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003171 else if (req->file->f_op->read)
3172 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3173 else
3174 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003175}
3176
Jens Axboea1d7c392020-06-22 11:09:46 -06003177static int io_read(struct io_kiocb *req, bool force_nonblock,
3178 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003179{
3180 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003181 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003182 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003183 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003184 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003185 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003186
Jens Axboeff6165b2020-08-13 09:47:43 -06003187 if (req->io)
3188 iter = &req->io->rw.iter;
3189
3190 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003191 if (ret < 0)
3192 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003193 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003194 io_size = ret;
3195 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003196 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003197
Jens Axboefd6c2e42019-12-18 12:19:41 -07003198 /* Ensure we clear previously set non-block flag */
3199 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003200 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003201
Pavel Begunkov24c74672020-06-21 13:09:51 +03003202 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003203 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3204 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003205 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003206
Jens Axboe0fef9482020-08-26 10:36:20 -06003207 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003208 if (unlikely(ret))
3209 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003210
Jens Axboe227c0c92020-08-13 11:51:40 -06003211 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003212
Jens Axboe227c0c92020-08-13 11:51:40 -06003213 if (!ret) {
3214 goto done;
3215 } else if (ret == -EIOCBQUEUED) {
3216 ret = 0;
3217 goto out_free;
3218 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003219 /* IOPOLL retry should happen for io-wq threads */
3220 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003221 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003222 /* no retry on NONBLOCK marked file */
3223 if (req->file->f_flags & O_NONBLOCK)
3224 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003225 /* some cases will consume bytes even on error returns */
3226 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003227 ret = 0;
3228 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003229 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003230 /* make sure -ERESTARTSYS -> -EINTR is done */
3231 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003232 }
3233
3234 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003235 if (!iov_iter_count(iter) || !force_nonblock ||
3236 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003237 goto done;
3238
3239 io_size -= ret;
3240copy_iov:
3241 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3242 if (ret2) {
3243 ret = ret2;
3244 goto out_free;
3245 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003246 if (no_async)
3247 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003248 /* it's copied and will be cleaned with ->io */
3249 iovec = NULL;
3250 /* now use our persistent iterator, if we aren't already */
3251 iter = &req->io->rw.iter;
3252retry:
3253 req->io->rw.bytes_done += ret;
3254 /* if we can retry, do so with the callbacks armed */
3255 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003256 kiocb->ki_flags &= ~IOCB_WAITQ;
3257 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003258 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003259
3260 /*
3261 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3262 * get -EIOCBQUEUED, then we'll get a notification when the desired
3263 * page gets unlocked. We can also get a partial read here, and if we
3264 * do, then just retry at the new offset.
3265 */
3266 ret = io_iter_do_read(req, iter);
3267 if (ret == -EIOCBQUEUED) {
3268 ret = 0;
3269 goto out_free;
3270 } else if (ret > 0 && ret < io_size) {
3271 /* we got some bytes, but not all. retry. */
3272 goto retry;
3273 }
3274done:
3275 kiocb_done(kiocb, ret, cs);
3276 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003277out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003278 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003279 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003280 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003281 return ret;
3282}
3283
Jens Axboe3529d8c2019-12-19 18:24:38 -07003284static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3285 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003286{
3287 ssize_t ret;
3288
Jens Axboe3529d8c2019-12-19 18:24:38 -07003289 ret = io_prep_rw(req, sqe, force_nonblock);
3290 if (ret)
3291 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003292
Jens Axboe3529d8c2019-12-19 18:24:38 -07003293 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3294 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003295
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003296 /* either don't need iovec imported or already have it */
3297 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003298 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003299 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003300}
3301
Jens Axboea1d7c392020-06-22 11:09:46 -06003302static int io_write(struct io_kiocb *req, bool force_nonblock,
3303 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003304{
3305 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003306 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003307 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003308 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003309 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003310
Jens Axboeff6165b2020-08-13 09:47:43 -06003311 if (req->io)
3312 iter = &req->io->rw.iter;
3313
3314 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003315 if (ret < 0)
3316 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003317 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003318 io_size = ret;
3319 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003320
Jens Axboefd6c2e42019-12-18 12:19:41 -07003321 /* Ensure we clear previously set non-block flag */
3322 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003323 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003324
Pavel Begunkov24c74672020-06-21 13:09:51 +03003325 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003326 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003327 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003328
Jens Axboe10d59342019-12-09 20:16:22 -07003329 /* file path doesn't support NOWAIT for non-direct_IO */
3330 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3331 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003332 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003333
Jens Axboe0fef9482020-08-26 10:36:20 -06003334 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003335 if (unlikely(ret))
3336 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003337
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003338 /*
3339 * Open-code file_start_write here to grab freeze protection,
3340 * which will be released by another thread in
3341 * io_complete_rw(). Fool lockdep by telling it the lock got
3342 * released so that it doesn't complain about the held lock when
3343 * we return to userspace.
3344 */
3345 if (req->flags & REQ_F_ISREG) {
3346 __sb_start_write(file_inode(req->file)->i_sb,
3347 SB_FREEZE_WRITE, true);
3348 __sb_writers_release(file_inode(req->file)->i_sb,
3349 SB_FREEZE_WRITE);
3350 }
3351 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003352
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003353 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003354 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003355 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003356 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003357 else
3358 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003359
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003360 /*
3361 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3362 * retry them without IOCB_NOWAIT.
3363 */
3364 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3365 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003366 /* no retry on NONBLOCK marked file */
3367 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3368 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003369 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003370 /* IOPOLL retry should happen for io-wq threads */
3371 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3372 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003373done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003374 kiocb_done(kiocb, ret2, cs);
3375 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003376copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003377 /* some cases will consume bytes even on error returns */
3378 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003379 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003380 if (!ret)
3381 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003382 }
Jens Axboe31b51512019-01-18 22:56:34 -07003383out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003384 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003385 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003386 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003387 return ret;
3388}
3389
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003390static int __io_splice_prep(struct io_kiocb *req,
3391 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003392{
3393 struct io_splice* sp = &req->splice;
3394 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3395 int ret;
3396
3397 if (req->flags & REQ_F_NEED_CLEANUP)
3398 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003399 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3400 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003401
3402 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003403 sp->len = READ_ONCE(sqe->len);
3404 sp->flags = READ_ONCE(sqe->splice_flags);
3405
3406 if (unlikely(sp->flags & ~valid_flags))
3407 return -EINVAL;
3408
3409 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3410 (sp->flags & SPLICE_F_FD_IN_FIXED));
3411 if (ret)
3412 return ret;
3413 req->flags |= REQ_F_NEED_CLEANUP;
3414
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003415 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3416 /*
3417 * Splice operation will be punted aync, and here need to
3418 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3419 */
3420 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003421 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003422 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003423
3424 return 0;
3425}
3426
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003427static int io_tee_prep(struct io_kiocb *req,
3428 const struct io_uring_sqe *sqe)
3429{
3430 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3431 return -EINVAL;
3432 return __io_splice_prep(req, sqe);
3433}
3434
3435static int io_tee(struct io_kiocb *req, bool force_nonblock)
3436{
3437 struct io_splice *sp = &req->splice;
3438 struct file *in = sp->file_in;
3439 struct file *out = sp->file_out;
3440 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3441 long ret = 0;
3442
3443 if (force_nonblock)
3444 return -EAGAIN;
3445 if (sp->len)
3446 ret = do_tee(in, out, sp->len, flags);
3447
3448 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3449 req->flags &= ~REQ_F_NEED_CLEANUP;
3450
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003451 if (ret != sp->len)
3452 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003453 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003454 return 0;
3455}
3456
3457static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3458{
3459 struct io_splice* sp = &req->splice;
3460
3461 sp->off_in = READ_ONCE(sqe->splice_off_in);
3462 sp->off_out = READ_ONCE(sqe->off);
3463 return __io_splice_prep(req, sqe);
3464}
3465
Pavel Begunkov014db002020-03-03 21:33:12 +03003466static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003467{
3468 struct io_splice *sp = &req->splice;
3469 struct file *in = sp->file_in;
3470 struct file *out = sp->file_out;
3471 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3472 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003473 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003474
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003475 if (force_nonblock)
3476 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003477
3478 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3479 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003480
Jens Axboe948a7742020-05-17 14:21:38 -06003481 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003482 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003483
3484 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3485 req->flags &= ~REQ_F_NEED_CLEANUP;
3486
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003487 if (ret != sp->len)
3488 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003489 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003490 return 0;
3491}
3492
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493/*
3494 * IORING_OP_NOP just posts a completion event, nothing else.
3495 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003496static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003497{
3498 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003499
Jens Axboedef596e2019-01-09 08:59:42 -07003500 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3501 return -EINVAL;
3502
Jens Axboe229a7b62020-06-22 10:13:11 -06003503 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003504 return 0;
3505}
3506
Jens Axboe3529d8c2019-12-19 18:24:38 -07003507static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003508{
Jens Axboe6b063142019-01-10 22:13:58 -07003509 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003510
Jens Axboe09bb8392019-03-13 12:39:28 -06003511 if (!req->file)
3512 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003513
Jens Axboe6b063142019-01-10 22:13:58 -07003514 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003515 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003516 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003517 return -EINVAL;
3518
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003519 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3520 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3521 return -EINVAL;
3522
3523 req->sync.off = READ_ONCE(sqe->off);
3524 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003525 return 0;
3526}
3527
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003528static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003529{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003530 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003531 int ret;
3532
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003533 /* fsync always requires a blocking context */
3534 if (force_nonblock)
3535 return -EAGAIN;
3536
Jens Axboe9adbd452019-12-20 08:45:55 -07003537 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003538 end > 0 ? end : LLONG_MAX,
3539 req->sync.flags & IORING_FSYNC_DATASYNC);
3540 if (ret < 0)
3541 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003542 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003543 return 0;
3544}
3545
Jens Axboed63d1b52019-12-10 10:38:56 -07003546static int io_fallocate_prep(struct io_kiocb *req,
3547 const struct io_uring_sqe *sqe)
3548{
3549 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3550 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003551 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3552 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003553
3554 req->sync.off = READ_ONCE(sqe->off);
3555 req->sync.len = READ_ONCE(sqe->addr);
3556 req->sync.mode = READ_ONCE(sqe->len);
3557 return 0;
3558}
3559
Pavel Begunkov014db002020-03-03 21:33:12 +03003560static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003561{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003562 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003563
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003564 /* fallocate always requiring blocking context */
3565 if (force_nonblock)
3566 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003567 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3568 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003569 if (ret < 0)
3570 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003571 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003572 return 0;
3573}
3574
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003575static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003576{
Jens Axboef8748882020-01-08 17:47:02 -07003577 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003578 int ret;
3579
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003580 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003581 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003582 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003583 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003584
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003585 /* open.how should be already initialised */
3586 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003587 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003588
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003589 req->open.dfd = READ_ONCE(sqe->fd);
3590 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003591 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003592 if (IS_ERR(req->open.filename)) {
3593 ret = PTR_ERR(req->open.filename);
3594 req->open.filename = NULL;
3595 return ret;
3596 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003597 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003598 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003599 return 0;
3600}
3601
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003602static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3603{
3604 u64 flags, mode;
3605
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003606 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3607 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003608 if (req->flags & REQ_F_NEED_CLEANUP)
3609 return 0;
3610 mode = READ_ONCE(sqe->len);
3611 flags = READ_ONCE(sqe->open_flags);
3612 req->open.how = build_open_how(flags, mode);
3613 return __io_openat_prep(req, sqe);
3614}
3615
Jens Axboecebdb982020-01-08 17:59:24 -07003616static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3617{
3618 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003619 size_t len;
3620 int ret;
3621
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003622 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3623 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003624 if (req->flags & REQ_F_NEED_CLEANUP)
3625 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003626 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3627 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003628 if (len < OPEN_HOW_SIZE_VER0)
3629 return -EINVAL;
3630
3631 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3632 len);
3633 if (ret)
3634 return ret;
3635
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003636 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003637}
3638
Pavel Begunkov014db002020-03-03 21:33:12 +03003639static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003640{
3641 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003642 struct file *file;
3643 int ret;
3644
Jens Axboef86cd202020-01-29 13:46:44 -07003645 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003646 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003647
Jens Axboecebdb982020-01-08 17:59:24 -07003648 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003649 if (ret)
3650 goto err;
3651
Jens Axboe4022e7a2020-03-19 19:23:18 -06003652 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003653 if (ret < 0)
3654 goto err;
3655
3656 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3657 if (IS_ERR(file)) {
3658 put_unused_fd(ret);
3659 ret = PTR_ERR(file);
3660 } else {
3661 fsnotify_open(file);
3662 fd_install(ret, file);
3663 }
3664err:
3665 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003666 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003667 if (ret < 0)
3668 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003669 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003670 return 0;
3671}
3672
Pavel Begunkov014db002020-03-03 21:33:12 +03003673static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003674{
Pavel Begunkov014db002020-03-03 21:33:12 +03003675 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003676}
3677
Jens Axboe067524e2020-03-02 16:32:28 -07003678static int io_remove_buffers_prep(struct io_kiocb *req,
3679 const struct io_uring_sqe *sqe)
3680{
3681 struct io_provide_buf *p = &req->pbuf;
3682 u64 tmp;
3683
3684 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3685 return -EINVAL;
3686
3687 tmp = READ_ONCE(sqe->fd);
3688 if (!tmp || tmp > USHRT_MAX)
3689 return -EINVAL;
3690
3691 memset(p, 0, sizeof(*p));
3692 p->nbufs = tmp;
3693 p->bgid = READ_ONCE(sqe->buf_group);
3694 return 0;
3695}
3696
3697static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3698 int bgid, unsigned nbufs)
3699{
3700 unsigned i = 0;
3701
3702 /* shouldn't happen */
3703 if (!nbufs)
3704 return 0;
3705
3706 /* the head kbuf is the list itself */
3707 while (!list_empty(&buf->list)) {
3708 struct io_buffer *nxt;
3709
3710 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3711 list_del(&nxt->list);
3712 kfree(nxt);
3713 if (++i == nbufs)
3714 return i;
3715 }
3716 i++;
3717 kfree(buf);
3718 idr_remove(&ctx->io_buffer_idr, bgid);
3719
3720 return i;
3721}
3722
Jens Axboe229a7b62020-06-22 10:13:11 -06003723static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3724 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003725{
3726 struct io_provide_buf *p = &req->pbuf;
3727 struct io_ring_ctx *ctx = req->ctx;
3728 struct io_buffer *head;
3729 int ret = 0;
3730
3731 io_ring_submit_lock(ctx, !force_nonblock);
3732
3733 lockdep_assert_held(&ctx->uring_lock);
3734
3735 ret = -ENOENT;
3736 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3737 if (head)
3738 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3739
3740 io_ring_submit_lock(ctx, !force_nonblock);
3741 if (ret < 0)
3742 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003743 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003744 return 0;
3745}
3746
Jens Axboeddf0322d2020-02-23 16:41:33 -07003747static int io_provide_buffers_prep(struct io_kiocb *req,
3748 const struct io_uring_sqe *sqe)
3749{
3750 struct io_provide_buf *p = &req->pbuf;
3751 u64 tmp;
3752
3753 if (sqe->ioprio || sqe->rw_flags)
3754 return -EINVAL;
3755
3756 tmp = READ_ONCE(sqe->fd);
3757 if (!tmp || tmp > USHRT_MAX)
3758 return -E2BIG;
3759 p->nbufs = tmp;
3760 p->addr = READ_ONCE(sqe->addr);
3761 p->len = READ_ONCE(sqe->len);
3762
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003763 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003764 return -EFAULT;
3765
3766 p->bgid = READ_ONCE(sqe->buf_group);
3767 tmp = READ_ONCE(sqe->off);
3768 if (tmp > USHRT_MAX)
3769 return -E2BIG;
3770 p->bid = tmp;
3771 return 0;
3772}
3773
3774static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3775{
3776 struct io_buffer *buf;
3777 u64 addr = pbuf->addr;
3778 int i, bid = pbuf->bid;
3779
3780 for (i = 0; i < pbuf->nbufs; i++) {
3781 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3782 if (!buf)
3783 break;
3784
3785 buf->addr = addr;
3786 buf->len = pbuf->len;
3787 buf->bid = bid;
3788 addr += pbuf->len;
3789 bid++;
3790 if (!*head) {
3791 INIT_LIST_HEAD(&buf->list);
3792 *head = buf;
3793 } else {
3794 list_add_tail(&buf->list, &(*head)->list);
3795 }
3796 }
3797
3798 return i ? i : -ENOMEM;
3799}
3800
Jens Axboe229a7b62020-06-22 10:13:11 -06003801static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3802 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003803{
3804 struct io_provide_buf *p = &req->pbuf;
3805 struct io_ring_ctx *ctx = req->ctx;
3806 struct io_buffer *head, *list;
3807 int ret = 0;
3808
3809 io_ring_submit_lock(ctx, !force_nonblock);
3810
3811 lockdep_assert_held(&ctx->uring_lock);
3812
3813 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3814
3815 ret = io_add_buffers(p, &head);
3816 if (ret < 0)
3817 goto out;
3818
3819 if (!list) {
3820 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3821 GFP_KERNEL);
3822 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003823 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003824 goto out;
3825 }
3826 }
3827out:
3828 io_ring_submit_unlock(ctx, !force_nonblock);
3829 if (ret < 0)
3830 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003831 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003832 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003833}
3834
Jens Axboe3e4827b2020-01-08 15:18:09 -07003835static int io_epoll_ctl_prep(struct io_kiocb *req,
3836 const struct io_uring_sqe *sqe)
3837{
3838#if defined(CONFIG_EPOLL)
3839 if (sqe->ioprio || sqe->buf_index)
3840 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003841 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003842 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003843
3844 req->epoll.epfd = READ_ONCE(sqe->fd);
3845 req->epoll.op = READ_ONCE(sqe->len);
3846 req->epoll.fd = READ_ONCE(sqe->off);
3847
3848 if (ep_op_has_event(req->epoll.op)) {
3849 struct epoll_event __user *ev;
3850
3851 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3852 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3853 return -EFAULT;
3854 }
3855
3856 return 0;
3857#else
3858 return -EOPNOTSUPP;
3859#endif
3860}
3861
Jens Axboe229a7b62020-06-22 10:13:11 -06003862static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3863 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003864{
3865#if defined(CONFIG_EPOLL)
3866 struct io_epoll *ie = &req->epoll;
3867 int ret;
3868
3869 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3870 if (force_nonblock && ret == -EAGAIN)
3871 return -EAGAIN;
3872
3873 if (ret < 0)
3874 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003875 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003876 return 0;
3877#else
3878 return -EOPNOTSUPP;
3879#endif
3880}
3881
Jens Axboec1ca7572019-12-25 22:18:28 -07003882static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3883{
3884#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3885 if (sqe->ioprio || sqe->buf_index || sqe->off)
3886 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003887 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3888 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003889
3890 req->madvise.addr = READ_ONCE(sqe->addr);
3891 req->madvise.len = READ_ONCE(sqe->len);
3892 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3893 return 0;
3894#else
3895 return -EOPNOTSUPP;
3896#endif
3897}
3898
Pavel Begunkov014db002020-03-03 21:33:12 +03003899static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003900{
3901#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3902 struct io_madvise *ma = &req->madvise;
3903 int ret;
3904
3905 if (force_nonblock)
3906 return -EAGAIN;
3907
3908 ret = do_madvise(ma->addr, ma->len, ma->advice);
3909 if (ret < 0)
3910 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003911 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003912 return 0;
3913#else
3914 return -EOPNOTSUPP;
3915#endif
3916}
3917
Jens Axboe4840e412019-12-25 22:03:45 -07003918static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3919{
3920 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3921 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003922 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3923 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003924
3925 req->fadvise.offset = READ_ONCE(sqe->off);
3926 req->fadvise.len = READ_ONCE(sqe->len);
3927 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3928 return 0;
3929}
3930
Pavel Begunkov014db002020-03-03 21:33:12 +03003931static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003932{
3933 struct io_fadvise *fa = &req->fadvise;
3934 int ret;
3935
Jens Axboe3e694262020-02-01 09:22:49 -07003936 if (force_nonblock) {
3937 switch (fa->advice) {
3938 case POSIX_FADV_NORMAL:
3939 case POSIX_FADV_RANDOM:
3940 case POSIX_FADV_SEQUENTIAL:
3941 break;
3942 default:
3943 return -EAGAIN;
3944 }
3945 }
Jens Axboe4840e412019-12-25 22:03:45 -07003946
3947 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3948 if (ret < 0)
3949 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003950 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003951 return 0;
3952}
3953
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003954static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3955{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003956 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003957 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003958 if (sqe->ioprio || sqe->buf_index)
3959 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003960 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003961 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003962
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003963 req->statx.dfd = READ_ONCE(sqe->fd);
3964 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003965 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003966 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3967 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003968
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003969 return 0;
3970}
3971
Pavel Begunkov014db002020-03-03 21:33:12 +03003972static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003973{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003974 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003975 int ret;
3976
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003977 if (force_nonblock) {
3978 /* only need file table for an actual valid fd */
3979 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3980 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003981 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003982 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003983
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003984 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3985 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003986
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003987 if (ret < 0)
3988 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003989 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003990 return 0;
3991}
3992
Jens Axboeb5dba592019-12-11 14:02:38 -07003993static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3994{
3995 /*
3996 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003997 * leave the 'file' in an undeterminate state, and here need to modify
3998 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003999 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004000 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004001 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4002
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004003 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4004 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004005 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4006 sqe->rw_flags || sqe->buf_index)
4007 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004008 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004009 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004010
4011 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004012 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004013 return -EBADF;
4014
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004015 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004016 return 0;
4017}
4018
Jens Axboe229a7b62020-06-22 10:13:11 -06004019static int io_close(struct io_kiocb *req, bool force_nonblock,
4020 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004021{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004022 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004023 int ret;
4024
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004025 /* might be already done during nonblock submission */
4026 if (!close->put_file) {
4027 ret = __close_fd_get_file(close->fd, &close->put_file);
4028 if (ret < 0)
4029 return (ret == -ENOENT) ? -EBADF : ret;
4030 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004031
4032 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004033 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004034 /* was never set, but play safe */
4035 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004036 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004037 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004038 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004039 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004040
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004041 /* No ->flush() or already async, safely close from here */
4042 ret = filp_close(close->put_file, req->work.files);
4043 if (ret < 0)
4044 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004045 fput(close->put_file);
4046 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004047 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004048 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004049}
4050
Jens Axboe3529d8c2019-12-19 18:24:38 -07004051static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004052{
4053 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004054
4055 if (!req->file)
4056 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004057
4058 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4059 return -EINVAL;
4060 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4061 return -EINVAL;
4062
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004063 req->sync.off = READ_ONCE(sqe->off);
4064 req->sync.len = READ_ONCE(sqe->len);
4065 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004066 return 0;
4067}
4068
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004069static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004070{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004071 int ret;
4072
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004073 /* sync_file_range always requires a blocking context */
4074 if (force_nonblock)
4075 return -EAGAIN;
4076
Jens Axboe9adbd452019-12-20 08:45:55 -07004077 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004078 req->sync.flags);
4079 if (ret < 0)
4080 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004081 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004082 return 0;
4083}
4084
YueHaibing469956e2020-03-04 15:53:52 +08004085#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004086static int io_setup_async_msg(struct io_kiocb *req,
4087 struct io_async_msghdr *kmsg)
4088{
4089 if (req->io)
4090 return -EAGAIN;
4091 if (io_alloc_async_ctx(req)) {
4092 if (kmsg->iov != kmsg->fast_iov)
4093 kfree(kmsg->iov);
4094 return -ENOMEM;
4095 }
4096 req->flags |= REQ_F_NEED_CLEANUP;
4097 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4098 return -EAGAIN;
4099}
4100
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004101static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4102 struct io_async_msghdr *iomsg)
4103{
4104 iomsg->iov = iomsg->fast_iov;
4105 iomsg->msg.msg_name = &iomsg->addr;
4106 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4107 req->sr_msg.msg_flags, &iomsg->iov);
4108}
4109
Jens Axboe3529d8c2019-12-19 18:24:38 -07004110static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004111{
Jens Axboee47293f2019-12-20 08:58:21 -07004112 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004113 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004114 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004115
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004116 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4117 return -EINVAL;
4118
Jens Axboee47293f2019-12-20 08:58:21 -07004119 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004120 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004121 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004122
Jens Axboed8768362020-02-27 14:17:49 -07004123#ifdef CONFIG_COMPAT
4124 if (req->ctx->compat)
4125 sr->msg_flags |= MSG_CMSG_COMPAT;
4126#endif
4127
Jens Axboefddafac2020-01-04 20:19:44 -07004128 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004129 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004130 /* iovec is already imported */
4131 if (req->flags & REQ_F_NEED_CLEANUP)
4132 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004133
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004134 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004135 if (!ret)
4136 req->flags |= REQ_F_NEED_CLEANUP;
4137 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004138}
4139
Jens Axboe229a7b62020-06-22 10:13:11 -06004140static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4141 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004142{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004143 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004144 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004145 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004146 int ret;
4147
Jens Axboe03b12302019-12-02 18:50:25 -07004148 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004149 if (unlikely(!sock))
4150 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004151
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004152 if (req->io) {
4153 kmsg = &req->io->msg;
4154 kmsg->msg.msg_name = &req->io->msg.addr;
4155 /* if iov is set, it's allocated already */
4156 if (!kmsg->iov)
4157 kmsg->iov = kmsg->fast_iov;
4158 kmsg->msg.msg_iter.iov = kmsg->iov;
4159 } else {
4160 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004161 if (ret)
4162 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004163 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004164 }
4165
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004166 flags = req->sr_msg.msg_flags;
4167 if (flags & MSG_DONTWAIT)
4168 req->flags |= REQ_F_NOWAIT;
4169 else if (force_nonblock)
4170 flags |= MSG_DONTWAIT;
4171
4172 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4173 if (force_nonblock && ret == -EAGAIN)
4174 return io_setup_async_msg(req, kmsg);
4175 if (ret == -ERESTARTSYS)
4176 ret = -EINTR;
4177
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004178 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004179 kfree(kmsg->iov);
4180 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004181 if (ret < 0)
4182 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004183 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004184 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004185}
4186
Jens Axboe229a7b62020-06-22 10:13:11 -06004187static int io_send(struct io_kiocb *req, bool force_nonblock,
4188 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004189{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004190 struct io_sr_msg *sr = &req->sr_msg;
4191 struct msghdr msg;
4192 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004193 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004194 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004195 int ret;
4196
4197 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004198 if (unlikely(!sock))
4199 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004200
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004201 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4202 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004203 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004204
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004205 msg.msg_name = NULL;
4206 msg.msg_control = NULL;
4207 msg.msg_controllen = 0;
4208 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004209
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004210 flags = req->sr_msg.msg_flags;
4211 if (flags & MSG_DONTWAIT)
4212 req->flags |= REQ_F_NOWAIT;
4213 else if (force_nonblock)
4214 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004215
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004216 msg.msg_flags = flags;
4217 ret = sock_sendmsg(sock, &msg);
4218 if (force_nonblock && ret == -EAGAIN)
4219 return -EAGAIN;
4220 if (ret == -ERESTARTSYS)
4221 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004222
Jens Axboe03b12302019-12-02 18:50:25 -07004223 if (ret < 0)
4224 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004225 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004226 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004227}
4228
Pavel Begunkov1400e692020-07-12 20:41:05 +03004229static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4230 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004231{
4232 struct io_sr_msg *sr = &req->sr_msg;
4233 struct iovec __user *uiov;
4234 size_t iov_len;
4235 int ret;
4236
Pavel Begunkov1400e692020-07-12 20:41:05 +03004237 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4238 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004239 if (ret)
4240 return ret;
4241
4242 if (req->flags & REQ_F_BUFFER_SELECT) {
4243 if (iov_len > 1)
4244 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004245 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004246 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004247 sr->len = iomsg->iov[0].iov_len;
4248 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004249 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004250 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004251 } else {
4252 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004253 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004254 if (ret > 0)
4255 ret = 0;
4256 }
4257
4258 return ret;
4259}
4260
4261#ifdef CONFIG_COMPAT
4262static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004263 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004264{
4265 struct compat_msghdr __user *msg_compat;
4266 struct io_sr_msg *sr = &req->sr_msg;
4267 struct compat_iovec __user *uiov;
4268 compat_uptr_t ptr;
4269 compat_size_t len;
4270 int ret;
4271
Pavel Begunkov270a5942020-07-12 20:41:04 +03004272 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004273 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004274 &ptr, &len);
4275 if (ret)
4276 return ret;
4277
4278 uiov = compat_ptr(ptr);
4279 if (req->flags & REQ_F_BUFFER_SELECT) {
4280 compat_ssize_t clen;
4281
4282 if (len > 1)
4283 return -EINVAL;
4284 if (!access_ok(uiov, sizeof(*uiov)))
4285 return -EFAULT;
4286 if (__get_user(clen, &uiov->iov_len))
4287 return -EFAULT;
4288 if (clen < 0)
4289 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004290 sr->len = iomsg->iov[0].iov_len;
4291 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004292 } else {
4293 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004294 &iomsg->iov,
4295 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004296 if (ret < 0)
4297 return ret;
4298 }
4299
4300 return 0;
4301}
Jens Axboe03b12302019-12-02 18:50:25 -07004302#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004303
Pavel Begunkov1400e692020-07-12 20:41:05 +03004304static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4305 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004306{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004307 iomsg->msg.msg_name = &iomsg->addr;
4308 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004309
4310#ifdef CONFIG_COMPAT
4311 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004312 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004313#endif
4314
Pavel Begunkov1400e692020-07-12 20:41:05 +03004315 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004316}
4317
Jens Axboebcda7ba2020-02-23 16:42:51 -07004318static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004319 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004320{
4321 struct io_sr_msg *sr = &req->sr_msg;
4322 struct io_buffer *kbuf;
4323
Jens Axboebcda7ba2020-02-23 16:42:51 -07004324 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4325 if (IS_ERR(kbuf))
4326 return kbuf;
4327
4328 sr->kbuf = kbuf;
4329 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004330 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004331}
4332
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004333static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4334{
4335 return io_put_kbuf(req, req->sr_msg.kbuf);
4336}
4337
Jens Axboe3529d8c2019-12-19 18:24:38 -07004338static int io_recvmsg_prep(struct io_kiocb *req,
4339 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004340{
Jens Axboee47293f2019-12-20 08:58:21 -07004341 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004342 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004343 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004344
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004345 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4346 return -EINVAL;
4347
Jens Axboe3529d8c2019-12-19 18:24:38 -07004348 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004349 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004350 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004351 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004352
Jens Axboed8768362020-02-27 14:17:49 -07004353#ifdef CONFIG_COMPAT
4354 if (req->ctx->compat)
4355 sr->msg_flags |= MSG_CMSG_COMPAT;
4356#endif
4357
Jens Axboefddafac2020-01-04 20:19:44 -07004358 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004359 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004360 /* iovec is already imported */
4361 if (req->flags & REQ_F_NEED_CLEANUP)
4362 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004363
Pavel Begunkov1400e692020-07-12 20:41:05 +03004364 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004365 if (!ret)
4366 req->flags |= REQ_F_NEED_CLEANUP;
4367 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004368}
4369
Jens Axboe229a7b62020-06-22 10:13:11 -06004370static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4371 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004372{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004373 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004374 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004375 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004376 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004377 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004378
Jens Axboe0fa03c62019-04-19 13:34:07 -06004379 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004380 if (unlikely(!sock))
4381 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004382
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004383 if (req->io) {
4384 kmsg = &req->io->msg;
4385 kmsg->msg.msg_name = &req->io->msg.addr;
4386 /* if iov is set, it's allocated already */
4387 if (!kmsg->iov)
4388 kmsg->iov = kmsg->fast_iov;
4389 kmsg->msg.msg_iter.iov = kmsg->iov;
4390 } else {
4391 ret = io_recvmsg_copy_hdr(req, &iomsg);
4392 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004393 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004394 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004395 }
4396
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004397 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004398 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004399 if (IS_ERR(kbuf))
4400 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004401 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4402 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4403 1, req->sr_msg.len);
4404 }
4405
4406 flags = req->sr_msg.msg_flags;
4407 if (flags & MSG_DONTWAIT)
4408 req->flags |= REQ_F_NOWAIT;
4409 else if (force_nonblock)
4410 flags |= MSG_DONTWAIT;
4411
4412 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4413 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004414 if (force_nonblock && ret == -EAGAIN)
4415 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004416 if (ret == -ERESTARTSYS)
4417 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004418
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004419 if (req->flags & REQ_F_BUFFER_SELECTED)
4420 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004421 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004422 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004423 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004424 if (ret < 0)
4425 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004426 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004427 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004428}
4429
Jens Axboe229a7b62020-06-22 10:13:11 -06004430static int io_recv(struct io_kiocb *req, bool force_nonblock,
4431 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004432{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004433 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004434 struct io_sr_msg *sr = &req->sr_msg;
4435 struct msghdr msg;
4436 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004437 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004438 struct iovec iov;
4439 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004440 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004441
Jens Axboefddafac2020-01-04 20:19:44 -07004442 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004443 if (unlikely(!sock))
4444 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004445
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004446 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004447 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004448 if (IS_ERR(kbuf))
4449 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004450 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004451 }
4452
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004453 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004454 if (unlikely(ret))
4455 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004456
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004457 msg.msg_name = NULL;
4458 msg.msg_control = NULL;
4459 msg.msg_controllen = 0;
4460 msg.msg_namelen = 0;
4461 msg.msg_iocb = NULL;
4462 msg.msg_flags = 0;
4463
4464 flags = req->sr_msg.msg_flags;
4465 if (flags & MSG_DONTWAIT)
4466 req->flags |= REQ_F_NOWAIT;
4467 else if (force_nonblock)
4468 flags |= MSG_DONTWAIT;
4469
4470 ret = sock_recvmsg(sock, &msg, flags);
4471 if (force_nonblock && ret == -EAGAIN)
4472 return -EAGAIN;
4473 if (ret == -ERESTARTSYS)
4474 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004475out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004476 if (req->flags & REQ_F_BUFFER_SELECTED)
4477 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004478 if (ret < 0)
4479 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004480 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004481 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004482}
4483
Jens Axboe3529d8c2019-12-19 18:24:38 -07004484static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004485{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004486 struct io_accept *accept = &req->accept;
4487
Jens Axboe17f2fe32019-10-17 14:42:58 -06004488 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4489 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004490 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004491 return -EINVAL;
4492
Jens Axboed55e5f52019-12-11 16:12:15 -07004493 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4494 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004495 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004496 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004497 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004498}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004499
Jens Axboe229a7b62020-06-22 10:13:11 -06004500static int io_accept(struct io_kiocb *req, bool force_nonblock,
4501 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004502{
4503 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004504 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004505 int ret;
4506
Jiufei Xuee697dee2020-06-10 13:41:59 +08004507 if (req->file->f_flags & O_NONBLOCK)
4508 req->flags |= REQ_F_NOWAIT;
4509
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004510 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004511 accept->addr_len, accept->flags,
4512 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004513 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004514 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004515 if (ret < 0) {
4516 if (ret == -ERESTARTSYS)
4517 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004518 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004519 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004520 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004521 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004522}
4523
Jens Axboe3529d8c2019-12-19 18:24:38 -07004524static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004525{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004526 struct io_connect *conn = &req->connect;
4527 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004528
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004529 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4530 return -EINVAL;
4531 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4532 return -EINVAL;
4533
Jens Axboe3529d8c2019-12-19 18:24:38 -07004534 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4535 conn->addr_len = READ_ONCE(sqe->addr2);
4536
4537 if (!io)
4538 return 0;
4539
4540 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004541 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004542}
4543
Jens Axboe229a7b62020-06-22 10:13:11 -06004544static int io_connect(struct io_kiocb *req, bool force_nonblock,
4545 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004546{
Jens Axboef499a022019-12-02 16:28:46 -07004547 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004548 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004549 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004550
Jens Axboef499a022019-12-02 16:28:46 -07004551 if (req->io) {
4552 io = req->io;
4553 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004554 ret = move_addr_to_kernel(req->connect.addr,
4555 req->connect.addr_len,
4556 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004557 if (ret)
4558 goto out;
4559 io = &__io;
4560 }
4561
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004562 file_flags = force_nonblock ? O_NONBLOCK : 0;
4563
4564 ret = __sys_connect_file(req->file, &io->connect.address,
4565 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004566 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004567 if (req->io)
4568 return -EAGAIN;
4569 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004570 ret = -ENOMEM;
4571 goto out;
4572 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004573 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004574 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004575 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004576 if (ret == -ERESTARTSYS)
4577 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004578out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004579 if (ret < 0)
4580 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004581 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004582 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004583}
YueHaibing469956e2020-03-04 15:53:52 +08004584#else /* !CONFIG_NET */
4585static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4586{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004587 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004588}
4589
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004590static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4591 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004592{
YueHaibing469956e2020-03-04 15:53:52 +08004593 return -EOPNOTSUPP;
4594}
4595
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004596static int io_send(struct io_kiocb *req, bool force_nonblock,
4597 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004598{
4599 return -EOPNOTSUPP;
4600}
4601
4602static int io_recvmsg_prep(struct io_kiocb *req,
4603 const struct io_uring_sqe *sqe)
4604{
4605 return -EOPNOTSUPP;
4606}
4607
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004608static int io_recvmsg(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
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004614static int io_recv(struct io_kiocb *req, bool force_nonblock,
4615 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004616{
4617 return -EOPNOTSUPP;
4618}
4619
4620static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4621{
4622 return -EOPNOTSUPP;
4623}
4624
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004625static int io_accept(struct io_kiocb *req, bool force_nonblock,
4626 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004627{
4628 return -EOPNOTSUPP;
4629}
4630
4631static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4632{
4633 return -EOPNOTSUPP;
4634}
4635
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004636static int io_connect(struct io_kiocb *req, bool force_nonblock,
4637 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004638{
4639 return -EOPNOTSUPP;
4640}
4641#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004642
Jens Axboed7718a92020-02-14 22:23:12 -07004643struct io_poll_table {
4644 struct poll_table_struct pt;
4645 struct io_kiocb *req;
4646 int error;
4647};
4648
Jens Axboed7718a92020-02-14 22:23:12 -07004649static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4650 __poll_t mask, task_work_func_t func)
4651{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004652 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004653 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004654
4655 /* for instances that support it check for an event match first: */
4656 if (mask && !(mask & poll->events))
4657 return 0;
4658
4659 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4660
4661 list_del_init(&poll->wait.entry);
4662
Jens Axboed7718a92020-02-14 22:23:12 -07004663 req->result = mask;
4664 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004665 percpu_ref_get(&req->ctx->refs);
4666
Jens Axboed7718a92020-02-14 22:23:12 -07004667 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004668 * If we using the signalfd wait_queue_head for this wakeup, then
4669 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4670 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4671 * either, as the normal wakeup will suffice.
4672 */
4673 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4674
4675 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004676 * If this fails, then the task is exiting. When a task exits, the
4677 * work gets canceled, so just cancel this request as well instead
4678 * of executing it. We can't safely execute it anyway, as we may not
4679 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004680 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004681 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004682 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004683 struct task_struct *tsk;
4684
Jens Axboee3aabf92020-05-18 11:04:17 -06004685 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004686 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004687 task_work_add(tsk, &req->task_work, 0);
4688 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004689 }
Jens Axboed7718a92020-02-14 22:23:12 -07004690 return 1;
4691}
4692
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004693static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4694 __acquires(&req->ctx->completion_lock)
4695{
4696 struct io_ring_ctx *ctx = req->ctx;
4697
4698 if (!req->result && !READ_ONCE(poll->canceled)) {
4699 struct poll_table_struct pt = { ._key = poll->events };
4700
4701 req->result = vfs_poll(req->file, &pt) & poll->events;
4702 }
4703
4704 spin_lock_irq(&ctx->completion_lock);
4705 if (!req->result && !READ_ONCE(poll->canceled)) {
4706 add_wait_queue(poll->head, &poll->wait);
4707 return true;
4708 }
4709
4710 return false;
4711}
4712
Jens Axboed4e7cd32020-08-15 11:44:50 -07004713static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004714{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004715 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4716 if (req->opcode == IORING_OP_POLL_ADD)
4717 return (struct io_poll_iocb *) req->io;
4718 return req->apoll->double_poll;
4719}
4720
4721static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4722{
4723 if (req->opcode == IORING_OP_POLL_ADD)
4724 return &req->poll;
4725 return &req->apoll->poll;
4726}
4727
4728static void io_poll_remove_double(struct io_kiocb *req)
4729{
4730 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004731
4732 lockdep_assert_held(&req->ctx->completion_lock);
4733
4734 if (poll && poll->head) {
4735 struct wait_queue_head *head = poll->head;
4736
4737 spin_lock(&head->lock);
4738 list_del_init(&poll->wait.entry);
4739 if (poll->wait.private)
4740 refcount_dec(&req->refs);
4741 poll->head = NULL;
4742 spin_unlock(&head->lock);
4743 }
4744}
4745
4746static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4747{
4748 struct io_ring_ctx *ctx = req->ctx;
4749
Jens Axboed4e7cd32020-08-15 11:44:50 -07004750 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004751 req->poll.done = true;
4752 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4753 io_commit_cqring(ctx);
4754}
4755
4756static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4757{
4758 struct io_ring_ctx *ctx = req->ctx;
4759
4760 if (io_poll_rewait(req, &req->poll)) {
4761 spin_unlock_irq(&ctx->completion_lock);
4762 return;
4763 }
4764
4765 hash_del(&req->hash_node);
4766 io_poll_complete(req, req->result, 0);
4767 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004768 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004769 spin_unlock_irq(&ctx->completion_lock);
4770
4771 io_cqring_ev_posted(ctx);
4772}
4773
4774static void io_poll_task_func(struct callback_head *cb)
4775{
4776 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004777 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004778 struct io_kiocb *nxt = NULL;
4779
4780 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004781 if (nxt)
4782 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004783 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004784}
4785
4786static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4787 int sync, void *key)
4788{
4789 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004790 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004791 __poll_t mask = key_to_poll(key);
4792
4793 /* for instances that support it check for an event match first: */
4794 if (mask && !(mask & poll->events))
4795 return 0;
4796
Jens Axboe8706e042020-09-28 08:38:54 -06004797 list_del_init(&wait->entry);
4798
Jens Axboe807abcb2020-07-17 17:09:27 -06004799 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004800 bool done;
4801
Jens Axboe807abcb2020-07-17 17:09:27 -06004802 spin_lock(&poll->head->lock);
4803 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004804 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004805 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004806 /* make sure double remove sees this as being gone */
4807 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004808 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004809 if (!done)
4810 __io_async_wake(req, poll, mask, io_poll_task_func);
4811 }
4812 refcount_dec(&req->refs);
4813 return 1;
4814}
4815
4816static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4817 wait_queue_func_t wake_func)
4818{
4819 poll->head = NULL;
4820 poll->done = false;
4821 poll->canceled = false;
4822 poll->events = events;
4823 INIT_LIST_HEAD(&poll->wait.entry);
4824 init_waitqueue_func_entry(&poll->wait, wake_func);
4825}
4826
4827static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004828 struct wait_queue_head *head,
4829 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004830{
4831 struct io_kiocb *req = pt->req;
4832
4833 /*
4834 * If poll->head is already set, it's because the file being polled
4835 * uses multiple waitqueues for poll handling (eg one for read, one
4836 * for write). Setup a separate io_poll_iocb if this happens.
4837 */
4838 if (unlikely(poll->head)) {
4839 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004840 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004841 pt->error = -EINVAL;
4842 return;
4843 }
4844 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4845 if (!poll) {
4846 pt->error = -ENOMEM;
4847 return;
4848 }
4849 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4850 refcount_inc(&req->refs);
4851 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004852 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004853 }
4854
4855 pt->error = 0;
4856 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004857
4858 if (poll->events & EPOLLEXCLUSIVE)
4859 add_wait_queue_exclusive(head, &poll->wait);
4860 else
4861 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004862}
4863
4864static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4865 struct poll_table_struct *p)
4866{
4867 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004868 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004869
Jens Axboe807abcb2020-07-17 17:09:27 -06004870 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004871}
4872
Jens Axboed7718a92020-02-14 22:23:12 -07004873static void io_async_task_func(struct callback_head *cb)
4874{
4875 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4876 struct async_poll *apoll = req->apoll;
4877 struct io_ring_ctx *ctx = req->ctx;
4878
4879 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4880
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004881 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004882 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004883 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004884 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004885 }
4886
Jens Axboe31067252020-05-17 17:43:31 -06004887 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004888 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004889 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004890
Jens Axboed4e7cd32020-08-15 11:44:50 -07004891 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004892 spin_unlock_irq(&ctx->completion_lock);
4893
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004894 if (!READ_ONCE(apoll->poll.canceled))
4895 __io_req_task_submit(req);
4896 else
4897 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004898
Jens Axboe6d816e02020-08-11 08:04:14 -06004899 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004900 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004901 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004902}
4903
4904static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4905 void *key)
4906{
4907 struct io_kiocb *req = wait->private;
4908 struct io_poll_iocb *poll = &req->apoll->poll;
4909
4910 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4911 key_to_poll(key));
4912
4913 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4914}
4915
4916static void io_poll_req_insert(struct io_kiocb *req)
4917{
4918 struct io_ring_ctx *ctx = req->ctx;
4919 struct hlist_head *list;
4920
4921 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4922 hlist_add_head(&req->hash_node, list);
4923}
4924
4925static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4926 struct io_poll_iocb *poll,
4927 struct io_poll_table *ipt, __poll_t mask,
4928 wait_queue_func_t wake_func)
4929 __acquires(&ctx->completion_lock)
4930{
4931 struct io_ring_ctx *ctx = req->ctx;
4932 bool cancel = false;
4933
Jens Axboe18bceab2020-05-15 11:56:54 -06004934 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004935 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004936 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004937
4938 ipt->pt._key = mask;
4939 ipt->req = req;
4940 ipt->error = -EINVAL;
4941
Jens Axboed7718a92020-02-14 22:23:12 -07004942 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4943
4944 spin_lock_irq(&ctx->completion_lock);
4945 if (likely(poll->head)) {
4946 spin_lock(&poll->head->lock);
4947 if (unlikely(list_empty(&poll->wait.entry))) {
4948 if (ipt->error)
4949 cancel = true;
4950 ipt->error = 0;
4951 mask = 0;
4952 }
4953 if (mask || ipt->error)
4954 list_del_init(&poll->wait.entry);
4955 else if (cancel)
4956 WRITE_ONCE(poll->canceled, true);
4957 else if (!poll->done) /* actually waiting for an event */
4958 io_poll_req_insert(req);
4959 spin_unlock(&poll->head->lock);
4960 }
4961
4962 return mask;
4963}
4964
4965static bool io_arm_poll_handler(struct io_kiocb *req)
4966{
4967 const struct io_op_def *def = &io_op_defs[req->opcode];
4968 struct io_ring_ctx *ctx = req->ctx;
4969 struct async_poll *apoll;
4970 struct io_poll_table ipt;
4971 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004972 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004973
4974 if (!req->file || !file_can_poll(req->file))
4975 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004976 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004977 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004978 if (def->pollin)
4979 rw = READ;
4980 else if (def->pollout)
4981 rw = WRITE;
4982 else
4983 return false;
4984 /* if we can't nonblock try, then no point in arming a poll handler */
4985 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004986 return false;
4987
4988 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4989 if (unlikely(!apoll))
4990 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004991 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004992
4993 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07004994 req->apoll = apoll;
4995 INIT_HLIST_NODE(&req->hash_node);
4996
Nathan Chancellor8755d972020-03-02 16:01:19 -07004997 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004998 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004999 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005000 if (def->pollout)
5001 mask |= POLLOUT | POLLWRNORM;
5002 mask |= POLLERR | POLLPRI;
5003
5004 ipt.pt._qproc = io_async_queue_proc;
5005
5006 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5007 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005008 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005009 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005010 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005011 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005012 kfree(apoll);
5013 return false;
5014 }
5015 spin_unlock_irq(&ctx->completion_lock);
5016 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5017 apoll->poll.events);
5018 return true;
5019}
5020
5021static bool __io_poll_remove_one(struct io_kiocb *req,
5022 struct io_poll_iocb *poll)
5023{
Jens Axboeb41e9852020-02-17 09:52:41 -07005024 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005025
5026 spin_lock(&poll->head->lock);
5027 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005028 if (!list_empty(&poll->wait.entry)) {
5029 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005030 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005031 }
5032 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005033 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005034 return do_complete;
5035}
5036
5037static bool io_poll_remove_one(struct io_kiocb *req)
5038{
5039 bool do_complete;
5040
Jens Axboed4e7cd32020-08-15 11:44:50 -07005041 io_poll_remove_double(req);
5042
Jens Axboed7718a92020-02-14 22:23:12 -07005043 if (req->opcode == IORING_OP_POLL_ADD) {
5044 do_complete = __io_poll_remove_one(req, &req->poll);
5045 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005046 struct async_poll *apoll = req->apoll;
5047
Jens Axboed7718a92020-02-14 22:23:12 -07005048 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005049 do_complete = __io_poll_remove_one(req, &apoll->poll);
5050 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005051 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005052 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005053 kfree(apoll);
5054 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005055 }
5056
Jens Axboeb41e9852020-02-17 09:52:41 -07005057 if (do_complete) {
5058 io_cqring_fill_event(req, -ECANCELED);
5059 io_commit_cqring(req->ctx);
5060 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005061 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005062 io_put_req(req);
5063 }
5064
5065 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005066}
5067
Jens Axboe76e1b642020-09-26 15:05:03 -06005068/*
5069 * Returns true if we found and killed one or more poll requests
5070 */
5071static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005072{
Jens Axboe78076bb2019-12-04 19:56:40 -07005073 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005074 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005075 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005076
5077 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005078 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5079 struct hlist_head *list;
5080
5081 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005082 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5083 if (io_task_match(req, tsk))
5084 posted += io_poll_remove_one(req);
5085 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005086 }
5087 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005088
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005089 if (posted)
5090 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005091
5092 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005093}
5094
Jens Axboe47f46762019-11-09 17:43:02 -07005095static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5096{
Jens Axboe78076bb2019-12-04 19:56:40 -07005097 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005098 struct io_kiocb *req;
5099
Jens Axboe78076bb2019-12-04 19:56:40 -07005100 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5101 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005102 if (sqe_addr != req->user_data)
5103 continue;
5104 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005105 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005106 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005107 }
5108
5109 return -ENOENT;
5110}
5111
Jens Axboe3529d8c2019-12-19 18:24:38 -07005112static int io_poll_remove_prep(struct io_kiocb *req,
5113 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005114{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005115 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5116 return -EINVAL;
5117 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5118 sqe->poll_events)
5119 return -EINVAL;
5120
Jens Axboe0969e782019-12-17 18:40:57 -07005121 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005122 return 0;
5123}
5124
5125/*
5126 * Find a running poll command that matches one specified in sqe->addr,
5127 * and remove it if found.
5128 */
5129static int io_poll_remove(struct io_kiocb *req)
5130{
5131 struct io_ring_ctx *ctx = req->ctx;
5132 u64 addr;
5133 int ret;
5134
Jens Axboe0969e782019-12-17 18:40:57 -07005135 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005136 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005137 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005138 spin_unlock_irq(&ctx->completion_lock);
5139
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005140 if (ret < 0)
5141 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005142 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005143 return 0;
5144}
5145
Jens Axboe221c5eb2019-01-17 09:41:58 -07005146static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5147 void *key)
5148{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005149 struct io_kiocb *req = wait->private;
5150 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005151
Jens Axboed7718a92020-02-14 22:23:12 -07005152 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005153}
5154
Jens Axboe221c5eb2019-01-17 09:41:58 -07005155static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5156 struct poll_table_struct *p)
5157{
5158 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5159
Jens Axboe807abcb2020-07-17 17:09:27 -06005160 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005161}
5162
Jens Axboe3529d8c2019-12-19 18:24:38 -07005163static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005164{
5165 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005166 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005167
5168 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5169 return -EINVAL;
5170 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5171 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005172 if (!poll->file)
5173 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005174
Jiufei Xue5769a352020-06-17 17:53:55 +08005175 events = READ_ONCE(sqe->poll32_events);
5176#ifdef __BIG_ENDIAN
5177 events = swahw32(events);
5178#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005179 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5180 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005181 return 0;
5182}
5183
Pavel Begunkov014db002020-03-03 21:33:12 +03005184static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005185{
5186 struct io_poll_iocb *poll = &req->poll;
5187 struct io_ring_ctx *ctx = req->ctx;
5188 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005189 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005190
Jens Axboe78076bb2019-12-04 19:56:40 -07005191 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005192 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005193
Jens Axboed7718a92020-02-14 22:23:12 -07005194 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5195 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005196
Jens Axboe8c838782019-03-12 15:48:16 -06005197 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005198 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005199 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005200 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005201 spin_unlock_irq(&ctx->completion_lock);
5202
Jens Axboe8c838782019-03-12 15:48:16 -06005203 if (mask) {
5204 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005205 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005206 }
Jens Axboe8c838782019-03-12 15:48:16 -06005207 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005208}
5209
Jens Axboe5262f562019-09-17 12:26:57 -06005210static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5211{
Jens Axboead8a48a2019-11-15 08:49:11 -07005212 struct io_timeout_data *data = container_of(timer,
5213 struct io_timeout_data, timer);
5214 struct io_kiocb *req = data->req;
5215 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005216 unsigned long flags;
5217
Jens Axboe5262f562019-09-17 12:26:57 -06005218 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005219 atomic_set(&req->ctx->cq_timeouts,
5220 atomic_read(&req->ctx->cq_timeouts) + 1);
5221
zhangyi (F)ef036812019-10-23 15:10:08 +08005222 /*
Jens Axboe11365042019-10-16 09:08:32 -06005223 * We could be racing with timeout deletion. If the list is empty,
5224 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005225 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005226 if (!list_empty(&req->timeout.list))
5227 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005228
Jens Axboe78e19bb2019-11-06 15:21:34 -07005229 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005230 io_commit_cqring(ctx);
5231 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5232
5233 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005234 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005235 io_put_req(req);
5236 return HRTIMER_NORESTART;
5237}
5238
Jens Axboef254ac02020-08-12 17:33:30 -06005239static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005240{
Jens Axboef254ac02020-08-12 17:33:30 -06005241 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005242
Jens Axboef254ac02020-08-12 17:33:30 -06005243 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005244
Jens Axboe2d283902019-12-04 11:08:05 -07005245 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005246 if (ret == -1)
5247 return -EALREADY;
5248
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005249 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005250 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005251 io_cqring_fill_event(req, -ECANCELED);
5252 io_put_req(req);
5253 return 0;
5254}
5255
Jens Axboef254ac02020-08-12 17:33:30 -06005256static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5257{
5258 struct io_kiocb *req;
5259 int ret = -ENOENT;
5260
5261 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5262 if (user_data == req->user_data) {
5263 ret = 0;
5264 break;
5265 }
5266 }
5267
5268 if (ret == -ENOENT)
5269 return ret;
5270
5271 return __io_timeout_cancel(req);
5272}
5273
Jens Axboe3529d8c2019-12-19 18:24:38 -07005274static int io_timeout_remove_prep(struct io_kiocb *req,
5275 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005276{
Jens Axboeb29472e2019-12-17 18:50:29 -07005277 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5278 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005279 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5280 return -EINVAL;
5281 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005282 return -EINVAL;
5283
5284 req->timeout.addr = READ_ONCE(sqe->addr);
5285 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5286 if (req->timeout.flags)
5287 return -EINVAL;
5288
Jens Axboeb29472e2019-12-17 18:50:29 -07005289 return 0;
5290}
5291
Jens Axboe11365042019-10-16 09:08:32 -06005292/*
5293 * Remove or update an existing timeout command
5294 */
Jens Axboefc4df992019-12-10 14:38:45 -07005295static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005296{
5297 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005298 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005299
Jens Axboe11365042019-10-16 09:08:32 -06005300 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005301 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005302
Jens Axboe47f46762019-11-09 17:43:02 -07005303 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005304 io_commit_cqring(ctx);
5305 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005306 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005307 if (ret < 0)
5308 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005309 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005310 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005311}
5312
Jens Axboe3529d8c2019-12-19 18:24:38 -07005313static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005314 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005315{
Jens Axboead8a48a2019-11-15 08:49:11 -07005316 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005317 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005318 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005319
Jens Axboead8a48a2019-11-15 08:49:11 -07005320 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005321 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005322 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005323 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005324 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005325 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005326 flags = READ_ONCE(sqe->timeout_flags);
5327 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005328 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005329
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005330 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005331
Jens Axboe3529d8c2019-12-19 18:24:38 -07005332 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005333 return -ENOMEM;
5334
5335 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005336 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005337
5338 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005339 return -EFAULT;
5340
Jens Axboe11365042019-10-16 09:08:32 -06005341 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005342 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005343 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005344 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005345
Jens Axboead8a48a2019-11-15 08:49:11 -07005346 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5347 return 0;
5348}
5349
Jens Axboefc4df992019-12-10 14:38:45 -07005350static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005351{
Jens Axboead8a48a2019-11-15 08:49:11 -07005352 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005353 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005354 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005355 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005356
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005357 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005358
Jens Axboe5262f562019-09-17 12:26:57 -06005359 /*
5360 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005361 * timeout event to be satisfied. If it isn't set, then this is
5362 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005363 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005364 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005365 entry = ctx->timeout_list.prev;
5366 goto add;
5367 }
Jens Axboe5262f562019-09-17 12:26:57 -06005368
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005369 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5370 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005371
5372 /*
5373 * Insertion sort, ensuring the first entry in the list is always
5374 * the one we need first.
5375 */
Jens Axboe5262f562019-09-17 12:26:57 -06005376 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005377 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5378 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005379
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005380 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005381 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005382 /* nxt.seq is behind @tail, otherwise would've been completed */
5383 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005384 break;
5385 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005386add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005387 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005388 data->timer.function = io_timeout_fn;
5389 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005390 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005391 return 0;
5392}
5393
Jens Axboe62755e32019-10-28 21:49:21 -06005394static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005395{
Jens Axboe62755e32019-10-28 21:49:21 -06005396 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005397
Jens Axboe62755e32019-10-28 21:49:21 -06005398 return req->user_data == (unsigned long) data;
5399}
5400
Jens Axboee977d6d2019-11-05 12:39:45 -07005401static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005402{
Jens Axboe62755e32019-10-28 21:49:21 -06005403 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005404 int ret = 0;
5405
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005406 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005407 switch (cancel_ret) {
5408 case IO_WQ_CANCEL_OK:
5409 ret = 0;
5410 break;
5411 case IO_WQ_CANCEL_RUNNING:
5412 ret = -EALREADY;
5413 break;
5414 case IO_WQ_CANCEL_NOTFOUND:
5415 ret = -ENOENT;
5416 break;
5417 }
5418
Jens Axboee977d6d2019-11-05 12:39:45 -07005419 return ret;
5420}
5421
Jens Axboe47f46762019-11-09 17:43:02 -07005422static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5423 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005424 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005425{
5426 unsigned long flags;
5427 int ret;
5428
5429 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5430 if (ret != -ENOENT) {
5431 spin_lock_irqsave(&ctx->completion_lock, flags);
5432 goto done;
5433 }
5434
5435 spin_lock_irqsave(&ctx->completion_lock, flags);
5436 ret = io_timeout_cancel(ctx, sqe_addr);
5437 if (ret != -ENOENT)
5438 goto done;
5439 ret = io_poll_cancel(ctx, sqe_addr);
5440done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005441 if (!ret)
5442 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005443 io_cqring_fill_event(req, ret);
5444 io_commit_cqring(ctx);
5445 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5446 io_cqring_ev_posted(ctx);
5447
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005448 if (ret < 0)
5449 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005450 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005451}
5452
Jens Axboe3529d8c2019-12-19 18:24:38 -07005453static int io_async_cancel_prep(struct io_kiocb *req,
5454 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005455{
Jens Axboefbf23842019-12-17 18:45:56 -07005456 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005457 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005458 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5459 return -EINVAL;
5460 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005461 return -EINVAL;
5462
Jens Axboefbf23842019-12-17 18:45:56 -07005463 req->cancel.addr = READ_ONCE(sqe->addr);
5464 return 0;
5465}
5466
Pavel Begunkov014db002020-03-03 21:33:12 +03005467static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005468{
5469 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005470
Pavel Begunkov014db002020-03-03 21:33:12 +03005471 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005472 return 0;
5473}
5474
Jens Axboe05f3fb32019-12-09 11:22:50 -07005475static int io_files_update_prep(struct io_kiocb *req,
5476 const struct io_uring_sqe *sqe)
5477{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005478 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5479 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005480 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5481 return -EINVAL;
5482 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005483 return -EINVAL;
5484
5485 req->files_update.offset = READ_ONCE(sqe->off);
5486 req->files_update.nr_args = READ_ONCE(sqe->len);
5487 if (!req->files_update.nr_args)
5488 return -EINVAL;
5489 req->files_update.arg = READ_ONCE(sqe->addr);
5490 return 0;
5491}
5492
Jens Axboe229a7b62020-06-22 10:13:11 -06005493static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5494 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005495{
5496 struct io_ring_ctx *ctx = req->ctx;
5497 struct io_uring_files_update up;
5498 int ret;
5499
Jens Axboef86cd202020-01-29 13:46:44 -07005500 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005501 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005502
5503 up.offset = req->files_update.offset;
5504 up.fds = req->files_update.arg;
5505
5506 mutex_lock(&ctx->uring_lock);
5507 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5508 mutex_unlock(&ctx->uring_lock);
5509
5510 if (ret < 0)
5511 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005512 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005513 return 0;
5514}
5515
Jens Axboe3529d8c2019-12-19 18:24:38 -07005516static int io_req_defer_prep(struct io_kiocb *req,
5517 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005518{
Jens Axboee7815732019-12-17 19:45:06 -07005519 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005520
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005521 if (!sqe)
5522 return 0;
5523
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005524 if (io_alloc_async_ctx(req))
5525 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005526 ret = io_prep_work_files(req);
5527 if (unlikely(ret))
5528 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005529
Jens Axboe202700e12020-09-12 13:18:10 -06005530 io_prep_async_work(req);
5531
Jens Axboed625c6e2019-12-17 19:53:05 -07005532 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005533 case IORING_OP_NOP:
5534 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005535 case IORING_OP_READV:
5536 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005537 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005538 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005539 break;
5540 case IORING_OP_WRITEV:
5541 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005542 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005543 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005544 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005545 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005546 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005547 break;
5548 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005549 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005550 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005551 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005552 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005553 break;
5554 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005555 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005556 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005557 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005558 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005559 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005560 break;
5561 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005562 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005563 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005564 break;
Jens Axboef499a022019-12-02 16:28:46 -07005565 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005566 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005567 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005568 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005569 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005570 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005571 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005572 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005573 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005574 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005575 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005576 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005577 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005578 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005579 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005580 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005581 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005582 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005583 case IORING_OP_FALLOCATE:
5584 ret = io_fallocate_prep(req, sqe);
5585 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005586 case IORING_OP_OPENAT:
5587 ret = io_openat_prep(req, sqe);
5588 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005589 case IORING_OP_CLOSE:
5590 ret = io_close_prep(req, sqe);
5591 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005592 case IORING_OP_FILES_UPDATE:
5593 ret = io_files_update_prep(req, sqe);
5594 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005595 case IORING_OP_STATX:
5596 ret = io_statx_prep(req, sqe);
5597 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005598 case IORING_OP_FADVISE:
5599 ret = io_fadvise_prep(req, sqe);
5600 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005601 case IORING_OP_MADVISE:
5602 ret = io_madvise_prep(req, sqe);
5603 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005604 case IORING_OP_OPENAT2:
5605 ret = io_openat2_prep(req, sqe);
5606 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005607 case IORING_OP_EPOLL_CTL:
5608 ret = io_epoll_ctl_prep(req, sqe);
5609 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005610 case IORING_OP_SPLICE:
5611 ret = io_splice_prep(req, sqe);
5612 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005613 case IORING_OP_PROVIDE_BUFFERS:
5614 ret = io_provide_buffers_prep(req, sqe);
5615 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005616 case IORING_OP_REMOVE_BUFFERS:
5617 ret = io_remove_buffers_prep(req, sqe);
5618 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005619 case IORING_OP_TEE:
5620 ret = io_tee_prep(req, sqe);
5621 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005622 default:
Jens Axboee7815732019-12-17 19:45:06 -07005623 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5624 req->opcode);
5625 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005626 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005627 }
5628
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005629 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005630}
5631
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005632static u32 io_get_sequence(struct io_kiocb *req)
5633{
5634 struct io_kiocb *pos;
5635 struct io_ring_ctx *ctx = req->ctx;
5636 u32 total_submitted, nr_reqs = 1;
5637
5638 if (req->flags & REQ_F_LINK_HEAD)
5639 list_for_each_entry(pos, &req->link_list, link_list)
5640 nr_reqs++;
5641
5642 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5643 return total_submitted - nr_reqs;
5644}
5645
Jens Axboe3529d8c2019-12-19 18:24:38 -07005646static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005647{
Jackie Liua197f662019-11-08 08:09:12 -07005648 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005649 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005650 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005651 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005652
Bob Liu9d858b22019-11-13 18:06:25 +08005653 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005654 if (likely(list_empty_careful(&ctx->defer_list) &&
5655 !(req->flags & REQ_F_IO_DRAIN)))
5656 return 0;
5657
5658 seq = io_get_sequence(req);
5659 /* Still a chance to pass the sequence check */
5660 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005661 return 0;
5662
Pavel Begunkov650b5482020-05-17 14:02:11 +03005663 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005664 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005665 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005666 return ret;
5667 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005668 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005669 de = kmalloc(sizeof(*de), GFP_KERNEL);
5670 if (!de)
5671 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005672
Jens Axboede0617e2019-04-06 21:51:27 -06005673 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005674 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005675 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005676 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005677 io_queue_async_work(req);
5678 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005679 }
5680
Jens Axboe915967f2019-11-21 09:01:20 -07005681 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005682 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005683 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005684 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005685 spin_unlock_irq(&ctx->completion_lock);
5686 return -EIOCBQUEUED;
5687}
5688
Jens Axboef573d382020-09-22 10:19:24 -06005689static void io_req_drop_files(struct io_kiocb *req)
5690{
5691 struct io_ring_ctx *ctx = req->ctx;
5692 unsigned long flags;
5693
5694 spin_lock_irqsave(&ctx->inflight_lock, flags);
5695 list_del(&req->inflight_entry);
5696 if (waitqueue_active(&ctx->inflight_wait))
5697 wake_up(&ctx->inflight_wait);
5698 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5699 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005700 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005701 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005702 req->work.files = NULL;
5703}
5704
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005705static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005706{
5707 struct io_async_ctx *io = req->io;
5708
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005709 if (req->flags & REQ_F_BUFFER_SELECTED) {
5710 switch (req->opcode) {
5711 case IORING_OP_READV:
5712 case IORING_OP_READ_FIXED:
5713 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005714 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005715 break;
5716 case IORING_OP_RECVMSG:
5717 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005718 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005719 break;
5720 }
5721 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005722 }
5723
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005724 if (req->flags & REQ_F_NEED_CLEANUP) {
5725 switch (req->opcode) {
5726 case IORING_OP_READV:
5727 case IORING_OP_READ_FIXED:
5728 case IORING_OP_READ:
5729 case IORING_OP_WRITEV:
5730 case IORING_OP_WRITE_FIXED:
5731 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005732 if (io->rw.free_iovec)
5733 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005734 break;
5735 case IORING_OP_RECVMSG:
5736 case IORING_OP_SENDMSG:
5737 if (io->msg.iov != io->msg.fast_iov)
5738 kfree(io->msg.iov);
5739 break;
5740 case IORING_OP_SPLICE:
5741 case IORING_OP_TEE:
5742 io_put_file(req, req->splice.file_in,
5743 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5744 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005745 case IORING_OP_OPENAT:
5746 case IORING_OP_OPENAT2:
5747 if (req->open.filename)
5748 putname(req->open.filename);
5749 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005750 }
5751 req->flags &= ~REQ_F_NEED_CLEANUP;
5752 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005753
Jens Axboef573d382020-09-22 10:19:24 -06005754 if (req->flags & REQ_F_INFLIGHT)
5755 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005756}
5757
Jens Axboe3529d8c2019-12-19 18:24:38 -07005758static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005759 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005760{
Jackie Liua197f662019-11-08 08:09:12 -07005761 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005762 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763
Jens Axboed625c6e2019-12-17 19:53:05 -07005764 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005765 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005766 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005767 break;
5768 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005769 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005770 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005771 if (sqe) {
5772 ret = io_read_prep(req, sqe, force_nonblock);
5773 if (ret < 0)
5774 break;
5775 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005776 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005777 break;
5778 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005779 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005780 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005781 if (sqe) {
5782 ret = io_write_prep(req, sqe, force_nonblock);
5783 if (ret < 0)
5784 break;
5785 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005786 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005787 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005788 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005789 if (sqe) {
5790 ret = io_prep_fsync(req, sqe);
5791 if (ret < 0)
5792 break;
5793 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005794 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005795 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005796 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005797 if (sqe) {
5798 ret = io_poll_add_prep(req, sqe);
5799 if (ret)
5800 break;
5801 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005802 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005803 break;
5804 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005805 if (sqe) {
5806 ret = io_poll_remove_prep(req, sqe);
5807 if (ret < 0)
5808 break;
5809 }
Jens Axboefc4df992019-12-10 14:38:45 -07005810 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005811 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005812 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005813 if (sqe) {
5814 ret = io_prep_sfr(req, sqe);
5815 if (ret < 0)
5816 break;
5817 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005818 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005819 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005820 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005821 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005822 if (sqe) {
5823 ret = io_sendmsg_prep(req, sqe);
5824 if (ret < 0)
5825 break;
5826 }
Jens Axboefddafac2020-01-04 20:19:44 -07005827 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005828 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005829 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005830 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005831 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005832 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005833 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005834 if (sqe) {
5835 ret = io_recvmsg_prep(req, sqe);
5836 if (ret)
5837 break;
5838 }
Jens Axboefddafac2020-01-04 20:19:44 -07005839 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005840 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005841 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005842 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005843 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005844 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005845 if (sqe) {
5846 ret = io_timeout_prep(req, sqe, false);
5847 if (ret)
5848 break;
5849 }
Jens Axboefc4df992019-12-10 14:38:45 -07005850 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005851 break;
Jens Axboe11365042019-10-16 09:08:32 -06005852 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005853 if (sqe) {
5854 ret = io_timeout_remove_prep(req, sqe);
5855 if (ret)
5856 break;
5857 }
Jens Axboefc4df992019-12-10 14:38:45 -07005858 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005859 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005860 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005861 if (sqe) {
5862 ret = io_accept_prep(req, sqe);
5863 if (ret)
5864 break;
5865 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005866 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005867 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005868 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005869 if (sqe) {
5870 ret = io_connect_prep(req, sqe);
5871 if (ret)
5872 break;
5873 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005874 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005875 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005876 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005877 if (sqe) {
5878 ret = io_async_cancel_prep(req, sqe);
5879 if (ret)
5880 break;
5881 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005882 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005883 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005884 case IORING_OP_FALLOCATE:
5885 if (sqe) {
5886 ret = io_fallocate_prep(req, sqe);
5887 if (ret)
5888 break;
5889 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005890 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005891 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005892 case IORING_OP_OPENAT:
5893 if (sqe) {
5894 ret = io_openat_prep(req, sqe);
5895 if (ret)
5896 break;
5897 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005898 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005899 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005900 case IORING_OP_CLOSE:
5901 if (sqe) {
5902 ret = io_close_prep(req, sqe);
5903 if (ret)
5904 break;
5905 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005906 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005907 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005908 case IORING_OP_FILES_UPDATE:
5909 if (sqe) {
5910 ret = io_files_update_prep(req, sqe);
5911 if (ret)
5912 break;
5913 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005914 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005915 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005916 case IORING_OP_STATX:
5917 if (sqe) {
5918 ret = io_statx_prep(req, sqe);
5919 if (ret)
5920 break;
5921 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005922 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005923 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005924 case IORING_OP_FADVISE:
5925 if (sqe) {
5926 ret = io_fadvise_prep(req, sqe);
5927 if (ret)
5928 break;
5929 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005930 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005931 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005932 case IORING_OP_MADVISE:
5933 if (sqe) {
5934 ret = io_madvise_prep(req, sqe);
5935 if (ret)
5936 break;
5937 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005938 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005939 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005940 case IORING_OP_OPENAT2:
5941 if (sqe) {
5942 ret = io_openat2_prep(req, sqe);
5943 if (ret)
5944 break;
5945 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005946 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005947 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005948 case IORING_OP_EPOLL_CTL:
5949 if (sqe) {
5950 ret = io_epoll_ctl_prep(req, sqe);
5951 if (ret)
5952 break;
5953 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005954 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005955 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005956 case IORING_OP_SPLICE:
5957 if (sqe) {
5958 ret = io_splice_prep(req, sqe);
5959 if (ret < 0)
5960 break;
5961 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005962 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005963 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005964 case IORING_OP_PROVIDE_BUFFERS:
5965 if (sqe) {
5966 ret = io_provide_buffers_prep(req, sqe);
5967 if (ret)
5968 break;
5969 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005970 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005971 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005972 case IORING_OP_REMOVE_BUFFERS:
5973 if (sqe) {
5974 ret = io_remove_buffers_prep(req, sqe);
5975 if (ret)
5976 break;
5977 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005978 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005979 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005980 case IORING_OP_TEE:
5981 if (sqe) {
5982 ret = io_tee_prep(req, sqe);
5983 if (ret < 0)
5984 break;
5985 }
5986 ret = io_tee(req, force_nonblock);
5987 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005988 default:
5989 ret = -EINVAL;
5990 break;
5991 }
5992
5993 if (ret)
5994 return ret;
5995
Jens Axboeb5325762020-05-19 21:20:27 -06005996 /* If the op doesn't have a file, we're not polling for it */
5997 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005998 const bool in_async = io_wq_current_is_worker();
5999
Jens Axboe11ba8202020-01-15 21:51:17 -07006000 /* workqueue context doesn't hold uring_lock, grab it now */
6001 if (in_async)
6002 mutex_lock(&ctx->uring_lock);
6003
Jens Axboe2b188cc2019-01-07 10:46:33 -07006004 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006005
6006 if (in_async)
6007 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006008 }
6009
6010 return 0;
6011}
6012
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006013static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006014{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006015 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006016 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006017 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006018
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006019 timeout = io_prep_linked_timeout(req);
6020 if (timeout)
6021 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006022
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006023 /* if NO_CANCEL is set, we must still run the work */
6024 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6025 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006026 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006027 }
Jens Axboe31b51512019-01-18 22:56:34 -07006028
Jens Axboe561fb042019-10-24 07:25:42 -06006029 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006030 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006031 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006032 /*
6033 * We can get EAGAIN for polled IO even though we're
6034 * forcing a sync submission from here, since we can't
6035 * wait for request slots on the block side.
6036 */
6037 if (ret != -EAGAIN)
6038 break;
6039 cond_resched();
6040 } while (1);
6041 }
Jens Axboe31b51512019-01-18 22:56:34 -07006042
Jens Axboe561fb042019-10-24 07:25:42 -06006043 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006044 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006045 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006046 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006047
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006048 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006049}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006050
Jens Axboe65e19f52019-10-26 07:20:21 -06006051static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6052 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006053{
Jens Axboe65e19f52019-10-26 07:20:21 -06006054 struct fixed_file_table *table;
6055
Jens Axboe05f3fb32019-12-09 11:22:50 -07006056 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006057 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006058}
6059
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006060static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6061 int fd, struct file **out_file, bool fixed)
6062{
6063 struct io_ring_ctx *ctx = req->ctx;
6064 struct file *file;
6065
6066 if (fixed) {
6067 if (unlikely(!ctx->file_data ||
6068 (unsigned) fd >= ctx->nr_user_files))
6069 return -EBADF;
6070 fd = array_index_nospec(fd, ctx->nr_user_files);
6071 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006072 if (file) {
6073 req->fixed_file_refs = ctx->file_data->cur_refs;
6074 percpu_ref_get(req->fixed_file_refs);
6075 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006076 } else {
6077 trace_io_uring_file_get(ctx, fd);
6078 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006079 }
6080
Jens Axboefd2206e2020-06-02 16:40:47 -06006081 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6082 *out_file = file;
6083 return 0;
6084 }
6085 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006086}
6087
Jens Axboe3529d8c2019-12-19 18:24:38 -07006088static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006089 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006090{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006091 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006092
Jens Axboe63ff8222020-05-07 14:56:15 -06006093 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006094 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006095 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006096
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006097 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006098}
6099
Jackie Liua197f662019-11-08 08:09:12 -07006100static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006101{
Jackie Liua197f662019-11-08 08:09:12 -07006102 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006103
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006104 io_req_init_async(req);
6105
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006106 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006107 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006108
Jens Axboe0f212202020-09-13 13:09:39 -06006109 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006110 get_nsproxy(current->nsproxy);
6111 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006112 req->flags |= REQ_F_INFLIGHT;
6113
Jens Axboefcb323c2019-10-24 12:39:47 -06006114 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006115 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006116 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006117 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006118}
6119
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006120static inline int io_prep_work_files(struct io_kiocb *req)
6121{
6122 if (!io_op_defs[req->opcode].file_table)
6123 return 0;
6124 return io_grab_files(req);
6125}
6126
Jens Axboe2665abf2019-11-05 12:40:47 -07006127static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6128{
Jens Axboead8a48a2019-11-15 08:49:11 -07006129 struct io_timeout_data *data = container_of(timer,
6130 struct io_timeout_data, timer);
6131 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006132 struct io_ring_ctx *ctx = req->ctx;
6133 struct io_kiocb *prev = NULL;
6134 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006135
6136 spin_lock_irqsave(&ctx->completion_lock, flags);
6137
6138 /*
6139 * We don't expect the list to be empty, that will only happen if we
6140 * race with the completion of the linked work.
6141 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006142 if (!list_empty(&req->link_list)) {
6143 prev = list_entry(req->link_list.prev, struct io_kiocb,
6144 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006145 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006146 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006147 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6148 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006149 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006150 }
6151
6152 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6153
6154 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006155 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006156 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006157 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006158 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006159 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006160 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006161 return HRTIMER_NORESTART;
6162}
6163
Jens Axboe7271ef32020-08-10 09:55:22 -06006164static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006165{
Jens Axboe76a46e02019-11-10 23:34:16 -07006166 /*
6167 * If the list is now empty, then our linked request finished before
6168 * we got a chance to setup the timer
6169 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006170 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006171 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006172
Jens Axboead8a48a2019-11-15 08:49:11 -07006173 data->timer.function = io_link_timeout_fn;
6174 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6175 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006176 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006177}
6178
6179static void io_queue_linked_timeout(struct io_kiocb *req)
6180{
6181 struct io_ring_ctx *ctx = req->ctx;
6182
6183 spin_lock_irq(&ctx->completion_lock);
6184 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006185 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006186
Jens Axboe2665abf2019-11-05 12:40:47 -07006187 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006188 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006189}
6190
Jens Axboead8a48a2019-11-15 08:49:11 -07006191static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006192{
6193 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006194
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006195 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006196 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006197 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006198 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006199
Pavel Begunkov44932332019-12-05 16:16:35 +03006200 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6201 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006202 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006203 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006204
Jens Axboe76a46e02019-11-10 23:34:16 -07006205 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006206 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006207}
6208
Jens Axboef13fad72020-06-22 09:34:30 -06006209static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6210 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006211{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006212 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006213 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006214 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006215 int ret;
6216
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006217again:
6218 linked_timeout = io_prep_linked_timeout(req);
6219
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006220 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6221 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006222 if (old_creds)
6223 revert_creds(old_creds);
6224 if (old_creds == req->work.creds)
6225 old_creds = NULL; /* restored original creds */
6226 else
6227 old_creds = override_creds(req->work.creds);
6228 }
6229
Jens Axboef13fad72020-06-22 09:34:30 -06006230 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006231
6232 /*
6233 * We async punt it if the file wasn't marked NOWAIT, or if the file
6234 * doesn't support non-blocking read/write attempts
6235 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006236 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006237 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006238punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006239 ret = io_prep_work_files(req);
6240 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006241 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006242 /*
6243 * Queued up for async execution, worker will release
6244 * submit reference when the iocb is actually submitted.
6245 */
6246 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006247 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006248
Pavel Begunkovf063c542020-07-25 14:41:59 +03006249 if (linked_timeout)
6250 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006251 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006252 }
Jens Axboee65ef562019-03-12 10:16:44 -06006253
Pavel Begunkov652532a2020-07-03 22:15:07 +03006254 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006255err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006256 /* un-prep timeout, so it'll be killed as any other linked */
6257 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006258 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006259 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006260 io_req_complete(req, ret);
6261 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006262 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006263
Jens Axboe6c271ce2019-01-10 11:22:30 -07006264 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006265 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006266 if (linked_timeout)
6267 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006268
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006269 if (nxt) {
6270 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006271
6272 if (req->flags & REQ_F_FORCE_ASYNC)
6273 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006274 goto again;
6275 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006276exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006277 if (old_creds)
6278 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006279}
6280
Jens Axboef13fad72020-06-22 09:34:30 -06006281static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6282 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006283{
6284 int ret;
6285
Jens Axboe3529d8c2019-12-19 18:24:38 -07006286 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006287 if (ret) {
6288 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006289fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006290 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006291 io_put_req(req);
6292 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006293 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006294 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006295 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006296 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006297 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006298 goto fail_req;
6299 }
6300
Jens Axboece35a472019-12-17 08:04:44 -07006301 /*
6302 * Never try inline submit of IOSQE_ASYNC is set, go straight
6303 * to async execution.
6304 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006305 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006306 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6307 io_queue_async_work(req);
6308 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006309 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006310 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006311}
6312
Jens Axboef13fad72020-06-22 09:34:30 -06006313static inline void io_queue_link_head(struct io_kiocb *req,
6314 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006315{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006316 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006317 io_put_req(req);
6318 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006319 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006320 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006321}
6322
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006323static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006324 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006325{
Jackie Liua197f662019-11-08 08:09:12 -07006326 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006327 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006328
Jens Axboe9e645e112019-05-10 16:07:28 -06006329 /*
6330 * If we already have a head request, queue this one for async
6331 * submittal once the head completes. If we don't have a head but
6332 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6333 * submitted sync once the chain is complete. If none of those
6334 * conditions are true (normal request), then just queue it.
6335 */
6336 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006337 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006338
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006339 /*
6340 * Taking sequential execution of a link, draining both sides
6341 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6342 * requests in the link. So, it drains the head and the
6343 * next after the link request. The last one is done via
6344 * drain_next flag to persist the effect across calls.
6345 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006346 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006347 head->flags |= REQ_F_IO_DRAIN;
6348 ctx->drain_next = 1;
6349 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006350 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006351 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006352 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006353 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006354 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006355 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006356 trace_io_uring_link(ctx, req, head);
6357 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006358
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006359 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006360 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006361 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006362 *link = NULL;
6363 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006364 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006365 if (unlikely(ctx->drain_next)) {
6366 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006367 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006368 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006369 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006370 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006371 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006372
Pavel Begunkov711be032020-01-17 03:57:59 +03006373 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006374 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006375 req->flags |= REQ_F_FAIL_LINK;
6376 *link = req;
6377 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006378 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006379 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006380 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006381
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006382 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006383}
6384
Jens Axboe9a56a232019-01-09 09:06:50 -07006385/*
6386 * Batched submission is done, ensure local IO is flushed out.
6387 */
6388static void io_submit_state_end(struct io_submit_state *state)
6389{
Jens Axboef13fad72020-06-22 09:34:30 -06006390 if (!list_empty(&state->comp.list))
6391 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006392 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006393 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006394 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006395 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006396}
6397
6398/*
6399 * Start submission side cache.
6400 */
6401static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006402 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006403{
6404 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006405 state->comp.nr = 0;
6406 INIT_LIST_HEAD(&state->comp.list);
6407 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006408 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006409 state->file = NULL;
6410 state->ios_left = max_ios;
6411}
6412
Jens Axboe2b188cc2019-01-07 10:46:33 -07006413static void io_commit_sqring(struct io_ring_ctx *ctx)
6414{
Hristo Venev75b28af2019-08-26 17:23:46 +00006415 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006416
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006417 /*
6418 * Ensure any loads from the SQEs are done at this point,
6419 * since once we write the new head, the application could
6420 * write new data to them.
6421 */
6422 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006423}
6424
6425/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006426 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006427 * that is mapped by userspace. This means that care needs to be taken to
6428 * ensure that reads are stable, as we cannot rely on userspace always
6429 * being a good citizen. If members of the sqe are validated and then later
6430 * used, it's important that those reads are done through READ_ONCE() to
6431 * prevent a re-load down the line.
6432 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006433static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006434{
Hristo Venev75b28af2019-08-26 17:23:46 +00006435 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006436 unsigned head;
6437
6438 /*
6439 * The cached sq head (or cq tail) serves two purposes:
6440 *
6441 * 1) allows us to batch the cost of updating the user visible
6442 * head updates.
6443 * 2) allows the kernel side to track the head on its own, even
6444 * though the application is the one updating it.
6445 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006446 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006447 if (likely(head < ctx->sq_entries))
6448 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006449
6450 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006451 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006452 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006453 return NULL;
6454}
6455
6456static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6457{
6458 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006459}
6460
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006461/*
6462 * Check SQE restrictions (opcode and flags).
6463 *
6464 * Returns 'true' if SQE is allowed, 'false' otherwise.
6465 */
6466static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6467 struct io_kiocb *req,
6468 unsigned int sqe_flags)
6469{
6470 if (!ctx->restricted)
6471 return true;
6472
6473 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6474 return false;
6475
6476 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6477 ctx->restrictions.sqe_flags_required)
6478 return false;
6479
6480 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6481 ctx->restrictions.sqe_flags_required))
6482 return false;
6483
6484 return true;
6485}
6486
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006487#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6488 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6489 IOSQE_BUFFER_SELECT)
6490
6491static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6492 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006493 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006494{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006495 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006496 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006497
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006498 req->opcode = READ_ONCE(sqe->opcode);
6499 req->user_data = READ_ONCE(sqe->user_data);
6500 req->io = NULL;
6501 req->file = NULL;
6502 req->ctx = ctx;
6503 req->flags = 0;
6504 /* one is dropped after submission, the other at completion */
6505 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006506 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006507 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006508 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006509 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006510
6511 if (unlikely(req->opcode >= IORING_OP_LAST))
6512 return -EINVAL;
6513
Jens Axboe9d8426a2020-06-16 18:42:49 -06006514 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6515 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006516
6517 sqe_flags = READ_ONCE(sqe->flags);
6518 /* enforce forwards compatibility on users */
6519 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6520 return -EINVAL;
6521
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006522 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6523 return -EACCES;
6524
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006525 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6526 !io_op_defs[req->opcode].buffer_select)
6527 return -EOPNOTSUPP;
6528
6529 id = READ_ONCE(sqe->personality);
6530 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006531 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006532 req->work.creds = idr_find(&ctx->personality_idr, id);
6533 if (unlikely(!req->work.creds))
6534 return -EINVAL;
6535 get_cred(req->work.creds);
6536 }
6537
6538 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006539 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006540
Jens Axboe63ff8222020-05-07 14:56:15 -06006541 if (!io_op_defs[req->opcode].needs_file)
6542 return 0;
6543
6544 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006545}
6546
Jens Axboe0f212202020-09-13 13:09:39 -06006547static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006548{
Jens Axboeac8691c2020-06-01 08:30:41 -06006549 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006550 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006551 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006552
Jens Axboec4a2ed72019-11-21 21:01:26 -07006553 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006554 if (test_bit(0, &ctx->sq_check_overflow)) {
6555 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006556 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006557 return -EBUSY;
6558 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006559
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006560 /* make sure SQ entry isn't read before tail */
6561 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006562
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006563 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6564 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006565
Jens Axboe013538b2020-06-22 09:29:15 -06006566 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006567
6568 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006569 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006570 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006571 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006572
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006573 sqe = io_get_sqe(ctx);
6574 if (unlikely(!sqe)) {
6575 io_consume_sqe(ctx);
6576 break;
6577 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006578 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006579 if (unlikely(!req)) {
6580 if (!submitted)
6581 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006582 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006583 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006584
Jens Axboeac8691c2020-06-01 08:30:41 -06006585 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006586 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006587 /* will complete beyond this point, count as submitted */
6588 submitted++;
6589
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006590 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006591fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006592 io_put_req(req);
6593 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006594 break;
6595 }
6596
Jens Axboe354420f2020-01-08 18:55:15 -07006597 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006598 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006599 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006600 if (err)
6601 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006602 }
6603
Pavel Begunkov9466f432020-01-25 22:34:01 +03006604 if (unlikely(submitted != nr)) {
6605 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6606
6607 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6608 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006609 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006610 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006611 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006612
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006613 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6614 io_commit_sqring(ctx);
6615
Jens Axboe6c271ce2019-01-10 11:22:30 -07006616 return submitted;
6617}
6618
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006619static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6620{
6621 /* Tell userspace we may need a wakeup call */
6622 spin_lock_irq(&ctx->completion_lock);
6623 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6624 spin_unlock_irq(&ctx->completion_lock);
6625}
6626
6627static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6628{
6629 spin_lock_irq(&ctx->completion_lock);
6630 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6631 spin_unlock_irq(&ctx->completion_lock);
6632}
6633
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006634static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6635 int sync, void *key)
6636{
6637 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6638 int ret;
6639
6640 ret = autoremove_wake_function(wqe, mode, sync, key);
6641 if (ret) {
6642 unsigned long flags;
6643
6644 spin_lock_irqsave(&ctx->completion_lock, flags);
6645 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6646 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6647 }
6648 return ret;
6649}
6650
Jens Axboec8d1ba52020-09-14 11:07:26 -06006651enum sq_ret {
6652 SQT_IDLE = 1,
6653 SQT_SPIN = 2,
6654 SQT_DID_WORK = 4,
6655};
6656
6657static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
6658 unsigned long start_jiffies)
6659{
6660 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006661 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006662 unsigned int to_submit;
6663 int ret = 0;
6664
6665again:
6666 if (!list_empty(&ctx->iopoll_list)) {
6667 unsigned nr_events = 0;
6668
6669 mutex_lock(&ctx->uring_lock);
6670 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6671 io_do_iopoll(ctx, &nr_events, 0);
6672 mutex_unlock(&ctx->uring_lock);
6673 }
6674
6675 to_submit = io_sqring_entries(ctx);
6676
6677 /*
6678 * If submit got -EBUSY, flag us as needing the application
6679 * to enter the kernel to reap and flush events.
6680 */
6681 if (!to_submit || ret == -EBUSY || need_resched()) {
6682 /*
6683 * Drop cur_mm before scheduling, we can't hold it for
6684 * long periods (or over schedule()). Do this before
6685 * adding ourselves to the waitqueue, as the unuse/drop
6686 * may sleep.
6687 */
6688 io_sq_thread_drop_mm();
6689
6690 /*
6691 * We're polling. If we're within the defined idle
6692 * period, then let us spin without work before going
6693 * to sleep. The exception is if we got EBUSY doing
6694 * more IO, we should wait for the application to
6695 * reap events and wake us up.
6696 */
6697 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6698 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6699 !percpu_ref_is_dying(&ctx->refs)))
6700 return SQT_SPIN;
6701
Jens Axboe534ca6d2020-09-02 13:52:19 -06006702 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006703 TASK_INTERRUPTIBLE);
6704
6705 /*
6706 * While doing polled IO, before going to sleep, we need
6707 * to check if there are new reqs added to iopoll_list,
6708 * it is because reqs may have been punted to io worker
6709 * and will be added to iopoll_list later, hence check
6710 * the iopoll_list again.
6711 */
6712 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6713 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006714 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006715 goto again;
6716 }
6717
6718 io_ring_set_wakeup_flag(ctx);
6719
6720 to_submit = io_sqring_entries(ctx);
6721 if (!to_submit || ret == -EBUSY)
6722 return SQT_IDLE;
6723 }
6724
Jens Axboe534ca6d2020-09-02 13:52:19 -06006725 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006726 io_ring_clear_wakeup_flag(ctx);
6727
6728 mutex_lock(&ctx->uring_lock);
6729 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6730 ret = io_submit_sqes(ctx, to_submit);
6731 mutex_unlock(&ctx->uring_lock);
6732 return SQT_DID_WORK;
6733}
6734
Jens Axboe6c271ce2019-01-10 11:22:30 -07006735static int io_sq_thread(void *data)
6736{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006737 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006738 const struct cred *old_cred;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006739 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006740
Jens Axboe6a779382020-09-02 12:21:41 -06006741 init_wait(&ctx->sqo_wait_entry);
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006742 ctx->sqo_wait_entry.func = io_sq_wake_function;
Jens Axboe6a779382020-09-02 12:21:41 -06006743
Jens Axboe0f158b42020-05-14 17:18:39 -06006744 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006745
Jens Axboe181e4482019-11-25 08:52:30 -07006746 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006747
Jens Axboec8d1ba52020-09-14 11:07:26 -06006748 start_jiffies = jiffies;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006749 while (!kthread_should_park()) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006750 enum sq_ret ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006751
Jens Axboec8d1ba52020-09-14 11:07:26 -06006752 ret = __io_sq_thread(ctx, start_jiffies);
6753 switch (ret) {
6754 case SQT_IDLE:
6755 schedule();
6756 start_jiffies = jiffies;
6757 continue;
6758 case SQT_SPIN:
6759 io_run_task_work();
6760 cond_resched();
6761 fallthrough;
6762 case SQT_DID_WORK:
6763 continue;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006764 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006765 }
6766
Jens Axboe4c6e2772020-07-01 11:29:10 -06006767 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006768
Jens Axboe4349f302020-07-09 15:07:01 -06006769 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006770 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006771
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006772 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006773
Jens Axboe6c271ce2019-01-10 11:22:30 -07006774 return 0;
6775}
6776
Jens Axboebda52162019-09-24 13:47:15 -06006777struct io_wait_queue {
6778 struct wait_queue_entry wq;
6779 struct io_ring_ctx *ctx;
6780 unsigned to_wait;
6781 unsigned nr_timeouts;
6782};
6783
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006784static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006785{
6786 struct io_ring_ctx *ctx = iowq->ctx;
6787
6788 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006789 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006790 * started waiting. For timeouts, we always want to return to userspace,
6791 * regardless of event count.
6792 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006793 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006794 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6795}
6796
6797static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6798 int wake_flags, void *key)
6799{
6800 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6801 wq);
6802
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006803 /* use noflush == true, as we can't safely rely on locking context */
6804 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006805 return -1;
6806
6807 return autoremove_wake_function(curr, mode, wake_flags, key);
6808}
6809
Jens Axboe2b188cc2019-01-07 10:46:33 -07006810/*
6811 * Wait until events become available, if we don't already have some. The
6812 * application must reap them itself, as they reside on the shared cq ring.
6813 */
6814static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6815 const sigset_t __user *sig, size_t sigsz)
6816{
Jens Axboebda52162019-09-24 13:47:15 -06006817 struct io_wait_queue iowq = {
6818 .wq = {
6819 .private = current,
6820 .func = io_wake_function,
6821 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6822 },
6823 .ctx = ctx,
6824 .to_wait = min_events,
6825 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006826 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006827 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006828
Jens Axboeb41e9852020-02-17 09:52:41 -07006829 do {
6830 if (io_cqring_events(ctx, false) >= min_events)
6831 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006832 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006833 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006834 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006835
6836 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006837#ifdef CONFIG_COMPAT
6838 if (in_compat_syscall())
6839 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006840 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006841 else
6842#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006843 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006844
Jens Axboe2b188cc2019-01-07 10:46:33 -07006845 if (ret)
6846 return ret;
6847 }
6848
Jens Axboebda52162019-09-24 13:47:15 -06006849 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006850 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006851 do {
6852 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6853 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006854 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006855 if (io_run_task_work())
6856 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006857 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006858 if (current->jobctl & JOBCTL_TASK_WORK) {
6859 spin_lock_irq(&current->sighand->siglock);
6860 current->jobctl &= ~JOBCTL_TASK_WORK;
6861 recalc_sigpending();
6862 spin_unlock_irq(&current->sighand->siglock);
6863 continue;
6864 }
6865 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006866 break;
6867 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006868 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006869 break;
6870 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006871 } while (1);
6872 finish_wait(&ctx->wait, &iowq.wq);
6873
Jens Axboeb7db41c2020-07-04 08:55:50 -06006874 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006875
Hristo Venev75b28af2019-08-26 17:23:46 +00006876 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006877}
6878
Jens Axboe6b063142019-01-10 22:13:58 -07006879static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6880{
6881#if defined(CONFIG_UNIX)
6882 if (ctx->ring_sock) {
6883 struct sock *sock = ctx->ring_sock->sk;
6884 struct sk_buff *skb;
6885
6886 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6887 kfree_skb(skb);
6888 }
6889#else
6890 int i;
6891
Jens Axboe65e19f52019-10-26 07:20:21 -06006892 for (i = 0; i < ctx->nr_user_files; i++) {
6893 struct file *file;
6894
6895 file = io_file_from_index(ctx, i);
6896 if (file)
6897 fput(file);
6898 }
Jens Axboe6b063142019-01-10 22:13:58 -07006899#endif
6900}
6901
Jens Axboe05f3fb32019-12-09 11:22:50 -07006902static void io_file_ref_kill(struct percpu_ref *ref)
6903{
6904 struct fixed_file_data *data;
6905
6906 data = container_of(ref, struct fixed_file_data, refs);
6907 complete(&data->done);
6908}
6909
Jens Axboe6b063142019-01-10 22:13:58 -07006910static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6911{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006912 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006913 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006914 unsigned nr_tables, i;
6915
Jens Axboe05f3fb32019-12-09 11:22:50 -07006916 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006917 return -ENXIO;
6918
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006919 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006920 if (!list_empty(&data->ref_list))
6921 ref_node = list_first_entry(&data->ref_list,
6922 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006923 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006924 if (ref_node)
6925 percpu_ref_kill(&ref_node->refs);
6926
6927 percpu_ref_kill(&data->refs);
6928
6929 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006930 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006931 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006932
Jens Axboe6b063142019-01-10 22:13:58 -07006933 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006934 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6935 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006936 kfree(data->table[i].files);
6937 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006938 percpu_ref_exit(&data->refs);
6939 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006940 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006941 ctx->nr_user_files = 0;
6942 return 0;
6943}
6944
Jens Axboe534ca6d2020-09-02 13:52:19 -06006945static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006946{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006947 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006948 /*
6949 * The park is a bit of a work-around, without it we get
6950 * warning spews on shutdown with SQPOLL set and affinity
6951 * set to a single CPU.
6952 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06006953 if (sqd->thread) {
6954 kthread_park(sqd->thread);
6955 kthread_stop(sqd->thread);
6956 }
6957
6958 kfree(sqd);
6959 }
6960}
6961
6962static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
6963{
6964 struct io_sq_data *sqd;
6965
6966 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
6967 if (!sqd)
6968 return ERR_PTR(-ENOMEM);
6969
6970 refcount_set(&sqd->refs, 1);
6971 init_waitqueue_head(&sqd->wait);
6972 return sqd;
6973}
6974
6975static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6976{
6977 struct io_sq_data *sqd = ctx->sq_data;
6978
6979 if (sqd) {
6980 if (sqd->thread) {
6981 /*
6982 * We may arrive here from the error branch in
6983 * io_sq_offload_create() where the kthread is created
6984 * without being waked up, thus wake it up now to make
6985 * sure the wait will complete.
6986 */
6987 wake_up_process(sqd->thread);
6988 wait_for_completion(&ctx->sq_thread_comp);
6989 }
6990
6991 io_put_sq_data(sqd);
6992 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006993 }
6994}
6995
Jens Axboe6b063142019-01-10 22:13:58 -07006996static void io_finish_async(struct io_ring_ctx *ctx)
6997{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006998 io_sq_thread_stop(ctx);
6999
Jens Axboe561fb042019-10-24 07:25:42 -06007000 if (ctx->io_wq) {
7001 io_wq_destroy(ctx->io_wq);
7002 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007003 }
7004}
7005
7006#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007007/*
7008 * Ensure the UNIX gc is aware of our file set, so we are certain that
7009 * the io_uring can be safely unregistered on process exit, even if we have
7010 * loops in the file referencing.
7011 */
7012static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7013{
7014 struct sock *sk = ctx->ring_sock->sk;
7015 struct scm_fp_list *fpl;
7016 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007017 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007018
Jens Axboe6b063142019-01-10 22:13:58 -07007019 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7020 if (!fpl)
7021 return -ENOMEM;
7022
7023 skb = alloc_skb(0, GFP_KERNEL);
7024 if (!skb) {
7025 kfree(fpl);
7026 return -ENOMEM;
7027 }
7028
7029 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007030
Jens Axboe08a45172019-10-03 08:11:03 -06007031 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007032 fpl->user = get_uid(ctx->user);
7033 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007034 struct file *file = io_file_from_index(ctx, i + offset);
7035
7036 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007037 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007038 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007039 unix_inflight(fpl->user, fpl->fp[nr_files]);
7040 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007041 }
7042
Jens Axboe08a45172019-10-03 08:11:03 -06007043 if (nr_files) {
7044 fpl->max = SCM_MAX_FD;
7045 fpl->count = nr_files;
7046 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007047 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007048 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7049 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007050
Jens Axboe08a45172019-10-03 08:11:03 -06007051 for (i = 0; i < nr_files; i++)
7052 fput(fpl->fp[i]);
7053 } else {
7054 kfree_skb(skb);
7055 kfree(fpl);
7056 }
Jens Axboe6b063142019-01-10 22:13:58 -07007057
7058 return 0;
7059}
7060
7061/*
7062 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7063 * causes regular reference counting to break down. We rely on the UNIX
7064 * garbage collection to take care of this problem for us.
7065 */
7066static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7067{
7068 unsigned left, total;
7069 int ret = 0;
7070
7071 total = 0;
7072 left = ctx->nr_user_files;
7073 while (left) {
7074 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007075
7076 ret = __io_sqe_files_scm(ctx, this_files, total);
7077 if (ret)
7078 break;
7079 left -= this_files;
7080 total += this_files;
7081 }
7082
7083 if (!ret)
7084 return 0;
7085
7086 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007087 struct file *file = io_file_from_index(ctx, total);
7088
7089 if (file)
7090 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007091 total++;
7092 }
7093
7094 return ret;
7095}
7096#else
7097static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7098{
7099 return 0;
7100}
7101#endif
7102
Jens Axboe65e19f52019-10-26 07:20:21 -06007103static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7104 unsigned nr_files)
7105{
7106 int i;
7107
7108 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007109 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007110 unsigned this_files;
7111
7112 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7113 table->files = kcalloc(this_files, sizeof(struct file *),
7114 GFP_KERNEL);
7115 if (!table->files)
7116 break;
7117 nr_files -= this_files;
7118 }
7119
7120 if (i == nr_tables)
7121 return 0;
7122
7123 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007124 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007125 kfree(table->files);
7126 }
7127 return 1;
7128}
7129
Jens Axboe05f3fb32019-12-09 11:22:50 -07007130static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007131{
7132#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007133 struct sock *sock = ctx->ring_sock->sk;
7134 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7135 struct sk_buff *skb;
7136 int i;
7137
7138 __skb_queue_head_init(&list);
7139
7140 /*
7141 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7142 * remove this entry and rearrange the file array.
7143 */
7144 skb = skb_dequeue(head);
7145 while (skb) {
7146 struct scm_fp_list *fp;
7147
7148 fp = UNIXCB(skb).fp;
7149 for (i = 0; i < fp->count; i++) {
7150 int left;
7151
7152 if (fp->fp[i] != file)
7153 continue;
7154
7155 unix_notinflight(fp->user, fp->fp[i]);
7156 left = fp->count - 1 - i;
7157 if (left) {
7158 memmove(&fp->fp[i], &fp->fp[i + 1],
7159 left * sizeof(struct file *));
7160 }
7161 fp->count--;
7162 if (!fp->count) {
7163 kfree_skb(skb);
7164 skb = NULL;
7165 } else {
7166 __skb_queue_tail(&list, skb);
7167 }
7168 fput(file);
7169 file = NULL;
7170 break;
7171 }
7172
7173 if (!file)
7174 break;
7175
7176 __skb_queue_tail(&list, skb);
7177
7178 skb = skb_dequeue(head);
7179 }
7180
7181 if (skb_peek(&list)) {
7182 spin_lock_irq(&head->lock);
7183 while ((skb = __skb_dequeue(&list)) != NULL)
7184 __skb_queue_tail(head, skb);
7185 spin_unlock_irq(&head->lock);
7186 }
7187#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007188 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007189#endif
7190}
7191
Jens Axboe05f3fb32019-12-09 11:22:50 -07007192struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007193 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007194 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007195};
7196
Jens Axboe4a38aed22020-05-14 17:21:15 -06007197static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007198{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007199 struct fixed_file_data *file_data = ref_node->file_data;
7200 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007201 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007202
7203 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007204 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007205 io_ring_file_put(ctx, pfile->file);
7206 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007207 }
7208
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007209 spin_lock(&file_data->lock);
7210 list_del(&ref_node->node);
7211 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007212
Xiaoguang Wang05589552020-03-31 14:05:18 +08007213 percpu_ref_exit(&ref_node->refs);
7214 kfree(ref_node);
7215 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007216}
7217
Jens Axboe4a38aed22020-05-14 17:21:15 -06007218static void io_file_put_work(struct work_struct *work)
7219{
7220 struct io_ring_ctx *ctx;
7221 struct llist_node *node;
7222
7223 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7224 node = llist_del_all(&ctx->file_put_llist);
7225
7226 while (node) {
7227 struct fixed_file_ref_node *ref_node;
7228 struct llist_node *next = node->next;
7229
7230 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7231 __io_file_put_work(ref_node);
7232 node = next;
7233 }
7234}
7235
Jens Axboe05f3fb32019-12-09 11:22:50 -07007236static void io_file_data_ref_zero(struct percpu_ref *ref)
7237{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007238 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007239 struct io_ring_ctx *ctx;
7240 bool first_add;
7241 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007242
Xiaoguang Wang05589552020-03-31 14:05:18 +08007243 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007244 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007245
Jens Axboe4a38aed22020-05-14 17:21:15 -06007246 if (percpu_ref_is_dying(&ctx->file_data->refs))
7247 delay = 0;
7248
7249 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7250 if (!delay)
7251 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7252 else if (first_add)
7253 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007254}
7255
7256static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7257 struct io_ring_ctx *ctx)
7258{
7259 struct fixed_file_ref_node *ref_node;
7260
7261 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7262 if (!ref_node)
7263 return ERR_PTR(-ENOMEM);
7264
7265 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7266 0, GFP_KERNEL)) {
7267 kfree(ref_node);
7268 return ERR_PTR(-ENOMEM);
7269 }
7270 INIT_LIST_HEAD(&ref_node->node);
7271 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007272 ref_node->file_data = ctx->file_data;
7273 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007274}
7275
7276static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7277{
7278 percpu_ref_exit(&ref_node->refs);
7279 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007280}
7281
7282static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7283 unsigned nr_args)
7284{
7285 __s32 __user *fds = (__s32 __user *) arg;
7286 unsigned nr_tables;
7287 struct file *file;
7288 int fd, ret = 0;
7289 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007290 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007291
7292 if (ctx->file_data)
7293 return -EBUSY;
7294 if (!nr_args)
7295 return -EINVAL;
7296 if (nr_args > IORING_MAX_FIXED_FILES)
7297 return -EMFILE;
7298
7299 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7300 if (!ctx->file_data)
7301 return -ENOMEM;
7302 ctx->file_data->ctx = ctx;
7303 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007304 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007305 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007306
7307 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7308 ctx->file_data->table = kcalloc(nr_tables,
7309 sizeof(struct fixed_file_table),
7310 GFP_KERNEL);
7311 if (!ctx->file_data->table) {
7312 kfree(ctx->file_data);
7313 ctx->file_data = NULL;
7314 return -ENOMEM;
7315 }
7316
Xiaoguang Wang05589552020-03-31 14:05:18 +08007317 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007318 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7319 kfree(ctx->file_data->table);
7320 kfree(ctx->file_data);
7321 ctx->file_data = NULL;
7322 return -ENOMEM;
7323 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007324
7325 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7326 percpu_ref_exit(&ctx->file_data->refs);
7327 kfree(ctx->file_data->table);
7328 kfree(ctx->file_data);
7329 ctx->file_data = NULL;
7330 return -ENOMEM;
7331 }
7332
7333 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7334 struct fixed_file_table *table;
7335 unsigned index;
7336
7337 ret = -EFAULT;
7338 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7339 break;
7340 /* allow sparse sets */
7341 if (fd == -1) {
7342 ret = 0;
7343 continue;
7344 }
7345
7346 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7347 index = i & IORING_FILE_TABLE_MASK;
7348 file = fget(fd);
7349
7350 ret = -EBADF;
7351 if (!file)
7352 break;
7353
7354 /*
7355 * Don't allow io_uring instances to be registered. If UNIX
7356 * isn't enabled, then this causes a reference cycle and this
7357 * instance can never get freed. If UNIX is enabled we'll
7358 * handle it just fine, but there's still no point in allowing
7359 * a ring fd as it doesn't support regular read/write anyway.
7360 */
7361 if (file->f_op == &io_uring_fops) {
7362 fput(file);
7363 break;
7364 }
7365 ret = 0;
7366 table->files[index] = file;
7367 }
7368
7369 if (ret) {
7370 for (i = 0; i < ctx->nr_user_files; i++) {
7371 file = io_file_from_index(ctx, i);
7372 if (file)
7373 fput(file);
7374 }
7375 for (i = 0; i < nr_tables; i++)
7376 kfree(ctx->file_data->table[i].files);
7377
Yang Yingliang667e57d2020-07-10 14:14:20 +00007378 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007379 kfree(ctx->file_data->table);
7380 kfree(ctx->file_data);
7381 ctx->file_data = NULL;
7382 ctx->nr_user_files = 0;
7383 return ret;
7384 }
7385
7386 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007387 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007388 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007389 return ret;
7390 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007391
Xiaoguang Wang05589552020-03-31 14:05:18 +08007392 ref_node = alloc_fixed_file_ref_node(ctx);
7393 if (IS_ERR(ref_node)) {
7394 io_sqe_files_unregister(ctx);
7395 return PTR_ERR(ref_node);
7396 }
7397
7398 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007399 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007400 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007401 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007402 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007403 return ret;
7404}
7405
Jens Axboec3a31e62019-10-03 13:59:56 -06007406static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7407 int index)
7408{
7409#if defined(CONFIG_UNIX)
7410 struct sock *sock = ctx->ring_sock->sk;
7411 struct sk_buff_head *head = &sock->sk_receive_queue;
7412 struct sk_buff *skb;
7413
7414 /*
7415 * See if we can merge this file into an existing skb SCM_RIGHTS
7416 * file set. If there's no room, fall back to allocating a new skb
7417 * and filling it in.
7418 */
7419 spin_lock_irq(&head->lock);
7420 skb = skb_peek(head);
7421 if (skb) {
7422 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7423
7424 if (fpl->count < SCM_MAX_FD) {
7425 __skb_unlink(skb, head);
7426 spin_unlock_irq(&head->lock);
7427 fpl->fp[fpl->count] = get_file(file);
7428 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7429 fpl->count++;
7430 spin_lock_irq(&head->lock);
7431 __skb_queue_head(head, skb);
7432 } else {
7433 skb = NULL;
7434 }
7435 }
7436 spin_unlock_irq(&head->lock);
7437
7438 if (skb) {
7439 fput(file);
7440 return 0;
7441 }
7442
7443 return __io_sqe_files_scm(ctx, 1, index);
7444#else
7445 return 0;
7446#endif
7447}
7448
Hillf Dantona5318d32020-03-23 17:47:15 +08007449static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007450 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007451{
Hillf Dantona5318d32020-03-23 17:47:15 +08007452 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007453 struct percpu_ref *refs = data->cur_refs;
7454 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007455
Jens Axboe05f3fb32019-12-09 11:22:50 -07007456 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007457 if (!pfile)
7458 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007459
Xiaoguang Wang05589552020-03-31 14:05:18 +08007460 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007461 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007462 list_add(&pfile->list, &ref_node->file_list);
7463
Hillf Dantona5318d32020-03-23 17:47:15 +08007464 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007465}
7466
7467static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7468 struct io_uring_files_update *up,
7469 unsigned nr_args)
7470{
7471 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007472 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007473 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007474 __s32 __user *fds;
7475 int fd, i, err;
7476 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007477 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007478
Jens Axboe05f3fb32019-12-09 11:22:50 -07007479 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007480 return -EOVERFLOW;
7481 if (done > ctx->nr_user_files)
7482 return -EINVAL;
7483
Xiaoguang Wang05589552020-03-31 14:05:18 +08007484 ref_node = alloc_fixed_file_ref_node(ctx);
7485 if (IS_ERR(ref_node))
7486 return PTR_ERR(ref_node);
7487
Jens Axboec3a31e62019-10-03 13:59:56 -06007488 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007489 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007490 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007491 struct fixed_file_table *table;
7492 unsigned index;
7493
Jens Axboec3a31e62019-10-03 13:59:56 -06007494 err = 0;
7495 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7496 err = -EFAULT;
7497 break;
7498 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007499 i = array_index_nospec(up->offset, ctx->nr_user_files);
7500 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007501 index = i & IORING_FILE_TABLE_MASK;
7502 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007503 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007504 err = io_queue_file_removal(data, file);
7505 if (err)
7506 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007507 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007508 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007509 }
7510 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007511 file = fget(fd);
7512 if (!file) {
7513 err = -EBADF;
7514 break;
7515 }
7516 /*
7517 * Don't allow io_uring instances to be registered. If
7518 * UNIX isn't enabled, then this causes a reference
7519 * cycle and this instance can never get freed. If UNIX
7520 * is enabled we'll handle it just fine, but there's
7521 * still no point in allowing a ring fd as it doesn't
7522 * support regular read/write anyway.
7523 */
7524 if (file->f_op == &io_uring_fops) {
7525 fput(file);
7526 err = -EBADF;
7527 break;
7528 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007529 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007530 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007531 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007532 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007533 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007534 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007535 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007536 }
7537 nr_args--;
7538 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007539 up->offset++;
7540 }
7541
Xiaoguang Wang05589552020-03-31 14:05:18 +08007542 if (needs_switch) {
7543 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007544 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007545 list_add(&ref_node->node, &data->ref_list);
7546 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007547 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007548 percpu_ref_get(&ctx->file_data->refs);
7549 } else
7550 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007551
7552 return done ? done : err;
7553}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007554
Jens Axboe05f3fb32019-12-09 11:22:50 -07007555static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7556 unsigned nr_args)
7557{
7558 struct io_uring_files_update up;
7559
7560 if (!ctx->file_data)
7561 return -ENXIO;
7562 if (!nr_args)
7563 return -EINVAL;
7564 if (copy_from_user(&up, arg, sizeof(up)))
7565 return -EFAULT;
7566 if (up.resv)
7567 return -EINVAL;
7568
7569 return __io_sqe_files_update(ctx, &up, nr_args);
7570}
Jens Axboec3a31e62019-10-03 13:59:56 -06007571
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007572static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007573{
7574 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7575
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007576 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007577 io_put_req(req);
7578}
7579
Pavel Begunkov24369c22020-01-28 03:15:48 +03007580static int io_init_wq_offload(struct io_ring_ctx *ctx,
7581 struct io_uring_params *p)
7582{
7583 struct io_wq_data data;
7584 struct fd f;
7585 struct io_ring_ctx *ctx_attach;
7586 unsigned int concurrency;
7587 int ret = 0;
7588
7589 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007590 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007591 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007592
7593 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7594 /* Do QD, or 4 * CPUS, whatever is smallest */
7595 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7596
7597 ctx->io_wq = io_wq_create(concurrency, &data);
7598 if (IS_ERR(ctx->io_wq)) {
7599 ret = PTR_ERR(ctx->io_wq);
7600 ctx->io_wq = NULL;
7601 }
7602 return ret;
7603 }
7604
7605 f = fdget(p->wq_fd);
7606 if (!f.file)
7607 return -EBADF;
7608
7609 if (f.file->f_op != &io_uring_fops) {
7610 ret = -EINVAL;
7611 goto out_fput;
7612 }
7613
7614 ctx_attach = f.file->private_data;
7615 /* @io_wq is protected by holding the fd */
7616 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7617 ret = -EINVAL;
7618 goto out_fput;
7619 }
7620
7621 ctx->io_wq = ctx_attach->io_wq;
7622out_fput:
7623 fdput(f);
7624 return ret;
7625}
7626
Jens Axboe0f212202020-09-13 13:09:39 -06007627static int io_uring_alloc_task_context(struct task_struct *task)
7628{
7629 struct io_uring_task *tctx;
7630
7631 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7632 if (unlikely(!tctx))
7633 return -ENOMEM;
7634
7635 xa_init(&tctx->xa);
7636 init_waitqueue_head(&tctx->wait);
7637 tctx->last = NULL;
7638 tctx->in_idle = 0;
7639 atomic_long_set(&tctx->req_issue, 0);
7640 atomic_long_set(&tctx->req_complete, 0);
7641 task->io_uring = tctx;
7642 return 0;
7643}
7644
7645void __io_uring_free(struct task_struct *tsk)
7646{
7647 struct io_uring_task *tctx = tsk->io_uring;
7648
7649 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7650 xa_destroy(&tctx->xa);
7651 kfree(tctx);
7652 tsk->io_uring = NULL;
7653}
7654
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007655static int io_sq_offload_create(struct io_ring_ctx *ctx,
7656 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007657{
7658 int ret;
7659
Jens Axboe6c271ce2019-01-10 11:22:30 -07007660 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007661 struct io_sq_data *sqd;
7662
Jens Axboe3ec482d2019-04-08 10:51:01 -06007663 ret = -EPERM;
7664 if (!capable(CAP_SYS_ADMIN))
7665 goto err;
7666
Jens Axboe534ca6d2020-09-02 13:52:19 -06007667 sqd = io_get_sq_data(p);
7668 if (IS_ERR(sqd)) {
7669 ret = PTR_ERR(sqd);
7670 goto err;
7671 }
7672 ctx->sq_data = sqd;
7673
Jens Axboe917257d2019-04-13 09:28:55 -06007674 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7675 if (!ctx->sq_thread_idle)
7676 ctx->sq_thread_idle = HZ;
7677
Jens Axboe6c271ce2019-01-10 11:22:30 -07007678 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007679 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007680
Jens Axboe917257d2019-04-13 09:28:55 -06007681 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007682 if (cpu >= nr_cpu_ids)
7683 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007684 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007685 goto err;
7686
Jens Axboe534ca6d2020-09-02 13:52:19 -06007687 sqd->thread = kthread_create_on_cpu(io_sq_thread, ctx,
7688 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007689 } else {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007690 sqd->thread = kthread_create(io_sq_thread, ctx,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007691 "io_uring-sq");
7692 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007693 if (IS_ERR(sqd->thread)) {
7694 ret = PTR_ERR(sqd->thread);
7695 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007696 goto err;
7697 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007698 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007699 if (ret)
7700 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007701 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7702 /* Can't have SQ_AFF without SQPOLL */
7703 ret = -EINVAL;
7704 goto err;
7705 }
7706
Pavel Begunkov24369c22020-01-28 03:15:48 +03007707 ret = io_init_wq_offload(ctx, p);
7708 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007709 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007710
7711 return 0;
7712err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007713 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007714 return ret;
7715}
7716
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007717static void io_sq_offload_start(struct io_ring_ctx *ctx)
7718{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007719 struct io_sq_data *sqd = ctx->sq_data;
7720
7721 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7722 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007723}
7724
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007725static inline void __io_unaccount_mem(struct user_struct *user,
7726 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007727{
7728 atomic_long_sub(nr_pages, &user->locked_vm);
7729}
7730
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007731static inline int __io_account_mem(struct user_struct *user,
7732 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007733{
7734 unsigned long page_limit, cur_pages, new_pages;
7735
7736 /* Don't allow more pages than we can safely lock */
7737 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7738
7739 do {
7740 cur_pages = atomic_long_read(&user->locked_vm);
7741 new_pages = cur_pages + nr_pages;
7742 if (new_pages > page_limit)
7743 return -ENOMEM;
7744 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7745 new_pages) != cur_pages);
7746
7747 return 0;
7748}
7749
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007750static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7751 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007752{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007753 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007754 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007755
Jens Axboe2aede0e2020-09-14 10:45:53 -06007756 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007757 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007758 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007759 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007760 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007761 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007762}
7763
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007764static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7765 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007766{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007767 int ret;
7768
7769 if (ctx->limit_mem) {
7770 ret = __io_account_mem(ctx->user, nr_pages);
7771 if (ret)
7772 return ret;
7773 }
7774
Jens Axboe2aede0e2020-09-14 10:45:53 -06007775 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007776 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007777 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007778 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007779 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007780 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007781
7782 return 0;
7783}
7784
Jens Axboe2b188cc2019-01-07 10:46:33 -07007785static void io_mem_free(void *ptr)
7786{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007787 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007788
Mark Rutland52e04ef2019-04-30 17:30:21 +01007789 if (!ptr)
7790 return;
7791
7792 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007793 if (put_page_testzero(page))
7794 free_compound_page(page);
7795}
7796
7797static void *io_mem_alloc(size_t size)
7798{
7799 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7800 __GFP_NORETRY;
7801
7802 return (void *) __get_free_pages(gfp_flags, get_order(size));
7803}
7804
Hristo Venev75b28af2019-08-26 17:23:46 +00007805static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7806 size_t *sq_offset)
7807{
7808 struct io_rings *rings;
7809 size_t off, sq_array_size;
7810
7811 off = struct_size(rings, cqes, cq_entries);
7812 if (off == SIZE_MAX)
7813 return SIZE_MAX;
7814
7815#ifdef CONFIG_SMP
7816 off = ALIGN(off, SMP_CACHE_BYTES);
7817 if (off == 0)
7818 return SIZE_MAX;
7819#endif
7820
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007821 if (sq_offset)
7822 *sq_offset = off;
7823
Hristo Venev75b28af2019-08-26 17:23:46 +00007824 sq_array_size = array_size(sizeof(u32), sq_entries);
7825 if (sq_array_size == SIZE_MAX)
7826 return SIZE_MAX;
7827
7828 if (check_add_overflow(off, sq_array_size, &off))
7829 return SIZE_MAX;
7830
Hristo Venev75b28af2019-08-26 17:23:46 +00007831 return off;
7832}
7833
Jens Axboe2b188cc2019-01-07 10:46:33 -07007834static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7835{
Hristo Venev75b28af2019-08-26 17:23:46 +00007836 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007837
Hristo Venev75b28af2019-08-26 17:23:46 +00007838 pages = (size_t)1 << get_order(
7839 rings_size(sq_entries, cq_entries, NULL));
7840 pages += (size_t)1 << get_order(
7841 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007842
Hristo Venev75b28af2019-08-26 17:23:46 +00007843 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007844}
7845
Jens Axboeedafcce2019-01-09 09:16:05 -07007846static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7847{
7848 int i, j;
7849
7850 if (!ctx->user_bufs)
7851 return -ENXIO;
7852
7853 for (i = 0; i < ctx->nr_user_bufs; i++) {
7854 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7855
7856 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007857 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007858
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007859 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007860 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007861 imu->nr_bvecs = 0;
7862 }
7863
7864 kfree(ctx->user_bufs);
7865 ctx->user_bufs = NULL;
7866 ctx->nr_user_bufs = 0;
7867 return 0;
7868}
7869
7870static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7871 void __user *arg, unsigned index)
7872{
7873 struct iovec __user *src;
7874
7875#ifdef CONFIG_COMPAT
7876 if (ctx->compat) {
7877 struct compat_iovec __user *ciovs;
7878 struct compat_iovec ciov;
7879
7880 ciovs = (struct compat_iovec __user *) arg;
7881 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7882 return -EFAULT;
7883
Jens Axboed55e5f52019-12-11 16:12:15 -07007884 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007885 dst->iov_len = ciov.iov_len;
7886 return 0;
7887 }
7888#endif
7889 src = (struct iovec __user *) arg;
7890 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7891 return -EFAULT;
7892 return 0;
7893}
7894
7895static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7896 unsigned nr_args)
7897{
7898 struct vm_area_struct **vmas = NULL;
7899 struct page **pages = NULL;
7900 int i, j, got_pages = 0;
7901 int ret = -EINVAL;
7902
7903 if (ctx->user_bufs)
7904 return -EBUSY;
7905 if (!nr_args || nr_args > UIO_MAXIOV)
7906 return -EINVAL;
7907
7908 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7909 GFP_KERNEL);
7910 if (!ctx->user_bufs)
7911 return -ENOMEM;
7912
7913 for (i = 0; i < nr_args; i++) {
7914 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7915 unsigned long off, start, end, ubuf;
7916 int pret, nr_pages;
7917 struct iovec iov;
7918 size_t size;
7919
7920 ret = io_copy_iov(ctx, &iov, arg, i);
7921 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007922 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007923
7924 /*
7925 * Don't impose further limits on the size and buffer
7926 * constraints here, we'll -EINVAL later when IO is
7927 * submitted if they are wrong.
7928 */
7929 ret = -EFAULT;
7930 if (!iov.iov_base || !iov.iov_len)
7931 goto err;
7932
7933 /* arbitrary limit, but we need something */
7934 if (iov.iov_len > SZ_1G)
7935 goto err;
7936
7937 ubuf = (unsigned long) iov.iov_base;
7938 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7939 start = ubuf >> PAGE_SHIFT;
7940 nr_pages = end - start;
7941
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007942 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007943 if (ret)
7944 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007945
7946 ret = 0;
7947 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007948 kvfree(vmas);
7949 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007950 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007951 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007952 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007953 sizeof(struct vm_area_struct *),
7954 GFP_KERNEL);
7955 if (!pages || !vmas) {
7956 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007957 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007958 goto err;
7959 }
7960 got_pages = nr_pages;
7961 }
7962
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007963 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007964 GFP_KERNEL);
7965 ret = -ENOMEM;
7966 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007967 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007968 goto err;
7969 }
7970
7971 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007972 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007973 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007974 FOLL_WRITE | FOLL_LONGTERM,
7975 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007976 if (pret == nr_pages) {
7977 /* don't support file backed memory */
7978 for (j = 0; j < nr_pages; j++) {
7979 struct vm_area_struct *vma = vmas[j];
7980
7981 if (vma->vm_file &&
7982 !is_file_hugepages(vma->vm_file)) {
7983 ret = -EOPNOTSUPP;
7984 break;
7985 }
7986 }
7987 } else {
7988 ret = pret < 0 ? pret : -EFAULT;
7989 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007990 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007991 if (ret) {
7992 /*
7993 * if we did partial map, or found file backed vmas,
7994 * release any pages we did get
7995 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007996 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007997 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007998 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007999 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008000 goto err;
8001 }
8002
8003 off = ubuf & ~PAGE_MASK;
8004 size = iov.iov_len;
8005 for (j = 0; j < nr_pages; j++) {
8006 size_t vec_len;
8007
8008 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8009 imu->bvec[j].bv_page = pages[j];
8010 imu->bvec[j].bv_len = vec_len;
8011 imu->bvec[j].bv_offset = off;
8012 off = 0;
8013 size -= vec_len;
8014 }
8015 /* store original address for later verification */
8016 imu->ubuf = ubuf;
8017 imu->len = iov.iov_len;
8018 imu->nr_bvecs = nr_pages;
8019
8020 ctx->nr_user_bufs++;
8021 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008022 kvfree(pages);
8023 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008024 return 0;
8025err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008026 kvfree(pages);
8027 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008028 io_sqe_buffer_unregister(ctx);
8029 return ret;
8030}
8031
Jens Axboe9b402842019-04-11 11:45:41 -06008032static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8033{
8034 __s32 __user *fds = arg;
8035 int fd;
8036
8037 if (ctx->cq_ev_fd)
8038 return -EBUSY;
8039
8040 if (copy_from_user(&fd, fds, sizeof(*fds)))
8041 return -EFAULT;
8042
8043 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8044 if (IS_ERR(ctx->cq_ev_fd)) {
8045 int ret = PTR_ERR(ctx->cq_ev_fd);
8046 ctx->cq_ev_fd = NULL;
8047 return ret;
8048 }
8049
8050 return 0;
8051}
8052
8053static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8054{
8055 if (ctx->cq_ev_fd) {
8056 eventfd_ctx_put(ctx->cq_ev_fd);
8057 ctx->cq_ev_fd = NULL;
8058 return 0;
8059 }
8060
8061 return -ENXIO;
8062}
8063
Jens Axboe5a2e7452020-02-23 16:23:11 -07008064static int __io_destroy_buffers(int id, void *p, void *data)
8065{
8066 struct io_ring_ctx *ctx = data;
8067 struct io_buffer *buf = p;
8068
Jens Axboe067524e2020-03-02 16:32:28 -07008069 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008070 return 0;
8071}
8072
8073static void io_destroy_buffers(struct io_ring_ctx *ctx)
8074{
8075 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8076 idr_destroy(&ctx->io_buffer_idr);
8077}
8078
Jens Axboe2b188cc2019-01-07 10:46:33 -07008079static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8080{
Jens Axboe6b063142019-01-10 22:13:58 -07008081 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008082 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008083
8084 if (ctx->sqo_task) {
8085 put_task_struct(ctx->sqo_task);
8086 ctx->sqo_task = NULL;
8087 mmdrop(ctx->mm_account);
8088 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008089 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008090
Jens Axboe6b063142019-01-10 22:13:58 -07008091 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008092 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008093 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008094 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008095
Jens Axboe2b188cc2019-01-07 10:46:33 -07008096#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008097 if (ctx->ring_sock) {
8098 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008099 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008100 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008101#endif
8102
Hristo Venev75b28af2019-08-26 17:23:46 +00008103 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008104 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008105
8106 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008107 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008108 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008109 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008110 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008111 kfree(ctx);
8112}
8113
8114static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8115{
8116 struct io_ring_ctx *ctx = file->private_data;
8117 __poll_t mask = 0;
8118
8119 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008120 /*
8121 * synchronizes with barrier from wq_has_sleeper call in
8122 * io_commit_cqring
8123 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008124 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00008125 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
8126 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008127 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008128 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008129 mask |= EPOLLIN | EPOLLRDNORM;
8130
8131 return mask;
8132}
8133
8134static int io_uring_fasync(int fd, struct file *file, int on)
8135{
8136 struct io_ring_ctx *ctx = file->private_data;
8137
8138 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8139}
8140
Jens Axboe071698e2020-01-28 10:04:42 -07008141static int io_remove_personalities(int id, void *p, void *data)
8142{
8143 struct io_ring_ctx *ctx = data;
8144 const struct cred *cred;
8145
8146 cred = idr_remove(&ctx->personality_idr, id);
8147 if (cred)
8148 put_cred(cred);
8149 return 0;
8150}
8151
Jens Axboe85faa7b2020-04-09 18:14:00 -06008152static void io_ring_exit_work(struct work_struct *work)
8153{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008154 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8155 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008156
Jens Axboe56952e92020-06-17 15:00:04 -06008157 /*
8158 * If we're doing polled IO and end up having requests being
8159 * submitted async (out-of-line), then completions can come in while
8160 * we're waiting for refs to drop. We need to reap these manually,
8161 * as nobody else will be looking for them.
8162 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008163 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008164 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008165 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008166 io_iopoll_try_reap_events(ctx);
8167 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008168 io_ring_ctx_free(ctx);
8169}
8170
Jens Axboe2b188cc2019-01-07 10:46:33 -07008171static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8172{
8173 mutex_lock(&ctx->uring_lock);
8174 percpu_ref_kill(&ctx->refs);
8175 mutex_unlock(&ctx->uring_lock);
8176
Jens Axboef3606e32020-09-22 08:18:24 -06008177 io_kill_timeouts(ctx, NULL);
8178 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008179
8180 if (ctx->io_wq)
8181 io_wq_cancel_all(ctx->io_wq);
8182
Jens Axboe15dff282019-11-13 09:09:23 -07008183 /* if we failed setting up the ctx, we might not have any rings */
8184 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008185 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008186 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008187 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008188
8189 /*
8190 * Do this upfront, so we won't have a grace period where the ring
8191 * is closed but resources aren't reaped yet. This can cause
8192 * spurious failure in setting up a new ring.
8193 */
Jens Axboe760618f2020-07-24 12:53:31 -06008194 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8195 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008196
Jens Axboe85faa7b2020-04-09 18:14:00 -06008197 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008198 /*
8199 * Use system_unbound_wq to avoid spawning tons of event kworkers
8200 * if we're exiting a ton of rings at the same time. It just adds
8201 * noise and overhead, there's no discernable change in runtime
8202 * over using system_wq.
8203 */
8204 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008205}
8206
8207static int io_uring_release(struct inode *inode, struct file *file)
8208{
8209 struct io_ring_ctx *ctx = file->private_data;
8210
8211 file->private_data = NULL;
8212 io_ring_ctx_wait_and_kill(ctx);
8213 return 0;
8214}
8215
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008216static bool io_wq_files_match(struct io_wq_work *work, void *data)
8217{
8218 struct files_struct *files = data;
8219
Jens Axboe0f212202020-09-13 13:09:39 -06008220 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008221}
8222
Jens Axboef254ac02020-08-12 17:33:30 -06008223/*
8224 * Returns true if 'preq' is the link parent of 'req'
8225 */
8226static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8227{
8228 struct io_kiocb *link;
8229
8230 if (!(preq->flags & REQ_F_LINK_HEAD))
8231 return false;
8232
8233 list_for_each_entry(link, &preq->link_list, link_list) {
8234 if (link == req)
8235 return true;
8236 }
8237
8238 return false;
8239}
8240
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008241static bool io_match_link_files(struct io_kiocb *req,
8242 struct files_struct *files)
8243{
8244 struct io_kiocb *link;
8245
8246 if (io_match_files(req, files))
8247 return true;
8248 if (req->flags & REQ_F_LINK_HEAD) {
8249 list_for_each_entry(link, &req->link_list, link_list) {
8250 if (io_match_files(link, files))
8251 return true;
8252 }
8253 }
8254 return false;
8255}
8256
Jens Axboef254ac02020-08-12 17:33:30 -06008257/*
8258 * We're looking to cancel 'req' because it's holding on to our files, but
8259 * 'req' could be a link to another request. See if it is, and cancel that
8260 * parent request if so.
8261 */
8262static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8263{
8264 struct hlist_node *tmp;
8265 struct io_kiocb *preq;
8266 bool found = false;
8267 int i;
8268
8269 spin_lock_irq(&ctx->completion_lock);
8270 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8271 struct hlist_head *list;
8272
8273 list = &ctx->cancel_hash[i];
8274 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8275 found = io_match_link(preq, req);
8276 if (found) {
8277 io_poll_remove_one(preq);
8278 break;
8279 }
8280 }
8281 }
8282 spin_unlock_irq(&ctx->completion_lock);
8283 return found;
8284}
8285
8286static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8287 struct io_kiocb *req)
8288{
8289 struct io_kiocb *preq;
8290 bool found = false;
8291
8292 spin_lock_irq(&ctx->completion_lock);
8293 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8294 found = io_match_link(preq, req);
8295 if (found) {
8296 __io_timeout_cancel(preq);
8297 break;
8298 }
8299 }
8300 spin_unlock_irq(&ctx->completion_lock);
8301 return found;
8302}
8303
Jens Axboeb711d4e2020-08-16 08:23:05 -07008304static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8305{
8306 return io_match_link(container_of(work, struct io_kiocb, work), data);
8307}
8308
8309static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8310{
8311 enum io_wq_cancel cret;
8312
8313 /* cancel this particular work, if it's running */
8314 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8315 if (cret != IO_WQ_CANCEL_NOTFOUND)
8316 return;
8317
8318 /* find links that hold this pending, cancel those */
8319 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8320 if (cret != IO_WQ_CANCEL_NOTFOUND)
8321 return;
8322
8323 /* if we have a poll link holding this pending, cancel that */
8324 if (io_poll_remove_link(ctx, req))
8325 return;
8326
8327 /* final option, timeout link is holding this req pending */
8328 io_timeout_remove_link(ctx, req);
8329}
8330
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008331static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8332 struct files_struct *files)
8333{
8334 struct io_defer_entry *de = NULL;
8335 LIST_HEAD(list);
8336
8337 spin_lock_irq(&ctx->completion_lock);
8338 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008339 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008340 list_cut_position(&list, &ctx->defer_list, &de->list);
8341 break;
8342 }
8343 }
8344 spin_unlock_irq(&ctx->completion_lock);
8345
8346 while (!list_empty(&list)) {
8347 de = list_first_entry(&list, struct io_defer_entry, list);
8348 list_del_init(&de->list);
8349 req_set_fail_links(de->req);
8350 io_put_req(de->req);
8351 io_req_complete(de->req, -ECANCELED);
8352 kfree(de);
8353 }
8354}
8355
Jens Axboe76e1b642020-09-26 15:05:03 -06008356/*
8357 * Returns true if we found and killed one or more files pinning requests
8358 */
8359static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008360 struct files_struct *files)
8361{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008362 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008363 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008364
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008365 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008366 /* cancel all at once, should be faster than doing it one by one*/
8367 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8368
Jens Axboefcb323c2019-10-24 12:39:47 -06008369 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008370 struct io_kiocb *cancel_req = NULL, *req;
8371 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008372
8373 spin_lock_irq(&ctx->inflight_lock);
8374 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008375 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008376 continue;
8377 /* req is being completed, ignore */
8378 if (!refcount_inc_not_zero(&req->refs))
8379 continue;
8380 cancel_req = req;
8381 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008382 }
Jens Axboe768134d2019-11-10 20:30:53 -07008383 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008384 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008385 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008386 spin_unlock_irq(&ctx->inflight_lock);
8387
Jens Axboe768134d2019-11-10 20:30:53 -07008388 /* We need to keep going until we don't find a matching req */
8389 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008390 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008391 /* cancel this request, or head link requests */
8392 io_attempt_cancel(ctx, cancel_req);
8393 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008394 /* cancellations _may_ trigger task work */
8395 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008396 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008397 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008398 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008399
8400 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008401}
8402
Pavel Begunkov801dd572020-06-15 10:33:14 +03008403static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008404{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008405 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8406 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008407
Jens Axboef3606e32020-09-22 08:18:24 -06008408 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008409}
8410
Jens Axboe0f212202020-09-13 13:09:39 -06008411static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8412 struct task_struct *task,
8413 struct files_struct *files)
8414{
8415 bool ret;
8416
8417 ret = io_uring_cancel_files(ctx, files);
8418 if (!files) {
8419 enum io_wq_cancel cret;
8420
8421 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8422 if (cret != IO_WQ_CANCEL_NOTFOUND)
8423 ret = true;
8424
8425 /* SQPOLL thread does its own polling */
8426 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8427 while (!list_empty_careful(&ctx->iopoll_list)) {
8428 io_iopoll_try_reap_events(ctx);
8429 ret = true;
8430 }
8431 }
8432
8433 ret |= io_poll_remove_all(ctx, task);
8434 ret |= io_kill_timeouts(ctx, task);
8435 }
8436
8437 return ret;
8438}
8439
8440/*
8441 * We need to iteratively cancel requests, in case a request has dependent
8442 * hard links. These persist even for failure of cancelations, hence keep
8443 * looping until none are found.
8444 */
8445static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8446 struct files_struct *files)
8447{
8448 struct task_struct *task = current;
8449
Jens Axboe534ca6d2020-09-02 13:52:19 -06008450 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8451 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008452
8453 io_cqring_overflow_flush(ctx, true, task, files);
8454
8455 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8456 io_run_task_work();
8457 cond_resched();
8458 }
8459}
8460
8461/*
8462 * Note that this task has used io_uring. We use it for cancelation purposes.
8463 */
8464static int io_uring_add_task_file(struct file *file)
8465{
8466 if (unlikely(!current->io_uring)) {
8467 int ret;
8468
8469 ret = io_uring_alloc_task_context(current);
8470 if (unlikely(ret))
8471 return ret;
8472 }
8473 if (current->io_uring->last != file) {
8474 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8475 void *old;
8476
8477 rcu_read_lock();
8478 old = xas_load(&xas);
8479 if (old != file) {
8480 get_file(file);
8481 xas_lock(&xas);
8482 xas_store(&xas, file);
8483 xas_unlock(&xas);
8484 }
8485 rcu_read_unlock();
8486 current->io_uring->last = file;
8487 }
8488
8489 return 0;
8490}
8491
8492/*
8493 * Remove this io_uring_file -> task mapping.
8494 */
8495static void io_uring_del_task_file(struct file *file)
8496{
8497 struct io_uring_task *tctx = current->io_uring;
8498 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8499
8500 if (tctx->last == file)
8501 tctx->last = NULL;
8502
8503 xas_lock(&xas);
8504 file = xas_store(&xas, NULL);
8505 xas_unlock(&xas);
8506
8507 if (file)
8508 fput(file);
8509}
8510
8511static void __io_uring_attempt_task_drop(struct file *file)
8512{
8513 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8514 struct file *old;
8515
8516 rcu_read_lock();
8517 old = xas_load(&xas);
8518 rcu_read_unlock();
8519
8520 if (old == file)
8521 io_uring_del_task_file(file);
8522}
8523
8524/*
8525 * Drop task note for this file if we're the only ones that hold it after
8526 * pending fput()
8527 */
8528static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8529{
8530 if (!current->io_uring)
8531 return;
8532 /*
8533 * fput() is pending, will be 2 if the only other ref is our potential
8534 * task file note. If the task is exiting, drop regardless of count.
8535 */
8536 if (!exiting && atomic_long_read(&file->f_count) != 2)
8537 return;
8538
8539 __io_uring_attempt_task_drop(file);
8540}
8541
8542void __io_uring_files_cancel(struct files_struct *files)
8543{
8544 struct io_uring_task *tctx = current->io_uring;
8545 XA_STATE(xas, &tctx->xa, 0);
8546
8547 /* make sure overflow events are dropped */
8548 tctx->in_idle = true;
8549
8550 do {
8551 struct io_ring_ctx *ctx;
8552 struct file *file;
8553
8554 xas_lock(&xas);
8555 file = xas_next_entry(&xas, ULONG_MAX);
8556 xas_unlock(&xas);
8557
8558 if (!file)
8559 break;
8560
8561 ctx = file->private_data;
8562
8563 io_uring_cancel_task_requests(ctx, files);
8564 if (files)
8565 io_uring_del_task_file(file);
8566 } while (1);
8567}
8568
8569static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8570{
8571 return atomic_long_read(&tctx->req_issue) ==
8572 atomic_long_read(&tctx->req_complete);
8573}
8574
8575/*
8576 * Find any io_uring fd that this task has registered or done IO on, and cancel
8577 * requests.
8578 */
8579void __io_uring_task_cancel(void)
8580{
8581 struct io_uring_task *tctx = current->io_uring;
8582 DEFINE_WAIT(wait);
8583 long completions;
8584
8585 /* make sure overflow events are dropped */
8586 tctx->in_idle = true;
8587
8588 while (!io_uring_task_idle(tctx)) {
8589 /* read completions before cancelations */
8590 completions = atomic_long_read(&tctx->req_complete);
8591 __io_uring_files_cancel(NULL);
8592
8593 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8594
8595 /*
8596 * If we've seen completions, retry. This avoids a race where
8597 * a completion comes in before we did prepare_to_wait().
8598 */
8599 if (completions != atomic_long_read(&tctx->req_complete))
8600 continue;
8601 if (io_uring_task_idle(tctx))
8602 break;
8603 schedule();
8604 }
8605
8606 finish_wait(&tctx->wait, &wait);
8607 tctx->in_idle = false;
8608}
8609
Jens Axboefcb323c2019-10-24 12:39:47 -06008610static int io_uring_flush(struct file *file, void *data)
8611{
8612 struct io_ring_ctx *ctx = file->private_data;
8613
Jens Axboe6ab23142020-02-08 20:23:59 -07008614 /*
8615 * If the task is going away, cancel work it may have pending
8616 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008617 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008618 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008619
Jens Axboe0f212202020-09-13 13:09:39 -06008620 io_uring_cancel_task_requests(ctx, data);
8621 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008622 return 0;
8623}
8624
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008625static void *io_uring_validate_mmap_request(struct file *file,
8626 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008627{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008628 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008629 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008630 struct page *page;
8631 void *ptr;
8632
8633 switch (offset) {
8634 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008635 case IORING_OFF_CQ_RING:
8636 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008637 break;
8638 case IORING_OFF_SQES:
8639 ptr = ctx->sq_sqes;
8640 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008641 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008642 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008643 }
8644
8645 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008646 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008647 return ERR_PTR(-EINVAL);
8648
8649 return ptr;
8650}
8651
8652#ifdef CONFIG_MMU
8653
8654static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8655{
8656 size_t sz = vma->vm_end - vma->vm_start;
8657 unsigned long pfn;
8658 void *ptr;
8659
8660 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8661 if (IS_ERR(ptr))
8662 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008663
8664 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8665 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8666}
8667
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008668#else /* !CONFIG_MMU */
8669
8670static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8671{
8672 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8673}
8674
8675static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8676{
8677 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8678}
8679
8680static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8681 unsigned long addr, unsigned long len,
8682 unsigned long pgoff, unsigned long flags)
8683{
8684 void *ptr;
8685
8686 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8687 if (IS_ERR(ptr))
8688 return PTR_ERR(ptr);
8689
8690 return (unsigned long) ptr;
8691}
8692
8693#endif /* !CONFIG_MMU */
8694
Jens Axboe2b188cc2019-01-07 10:46:33 -07008695SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8696 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8697 size_t, sigsz)
8698{
8699 struct io_ring_ctx *ctx;
8700 long ret = -EBADF;
8701 int submitted = 0;
8702 struct fd f;
8703
Jens Axboe4c6e2772020-07-01 11:29:10 -06008704 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008705
Jens Axboe6c271ce2019-01-10 11:22:30 -07008706 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008707 return -EINVAL;
8708
8709 f = fdget(fd);
8710 if (!f.file)
8711 return -EBADF;
8712
8713 ret = -EOPNOTSUPP;
8714 if (f.file->f_op != &io_uring_fops)
8715 goto out_fput;
8716
8717 ret = -ENXIO;
8718 ctx = f.file->private_data;
8719 if (!percpu_ref_tryget(&ctx->refs))
8720 goto out_fput;
8721
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008722 ret = -EBADFD;
8723 if (ctx->flags & IORING_SETUP_R_DISABLED)
8724 goto out;
8725
Jens Axboe6c271ce2019-01-10 11:22:30 -07008726 /*
8727 * For SQ polling, the thread will do all submissions and completions.
8728 * Just return the requested submit count, and wake the thread if
8729 * we were asked to.
8730 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008731 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008732 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008733 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008734 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008735 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008736 wake_up(&ctx->sq_data->wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008737 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008738 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008739 ret = io_uring_add_task_file(f.file);
8740 if (unlikely(ret))
8741 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008742 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008743 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008744 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008745
8746 if (submitted != to_submit)
8747 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008748 }
8749 if (flags & IORING_ENTER_GETEVENTS) {
8750 min_complete = min(min_complete, ctx->cq_entries);
8751
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008752 /*
8753 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8754 * space applications don't need to do io completion events
8755 * polling again, they can rely on io_sq_thread to do polling
8756 * work, which can reduce cpu usage and uring_lock contention.
8757 */
8758 if (ctx->flags & IORING_SETUP_IOPOLL &&
8759 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008760 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008761 } else {
8762 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8763 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008764 }
8765
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008766out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008767 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008768out_fput:
8769 fdput(f);
8770 return submitted ? submitted : ret;
8771}
8772
Tobias Klauserbebdb652020-02-26 18:38:32 +01008773#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008774static int io_uring_show_cred(int id, void *p, void *data)
8775{
8776 const struct cred *cred = p;
8777 struct seq_file *m = data;
8778 struct user_namespace *uns = seq_user_ns(m);
8779 struct group_info *gi;
8780 kernel_cap_t cap;
8781 unsigned __capi;
8782 int g;
8783
8784 seq_printf(m, "%5d\n", id);
8785 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8786 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8787 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8788 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8789 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8790 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8791 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8792 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8793 seq_puts(m, "\n\tGroups:\t");
8794 gi = cred->group_info;
8795 for (g = 0; g < gi->ngroups; g++) {
8796 seq_put_decimal_ull(m, g ? " " : "",
8797 from_kgid_munged(uns, gi->gid[g]));
8798 }
8799 seq_puts(m, "\n\tCapEff:\t");
8800 cap = cred->cap_effective;
8801 CAP_FOR_EACH_U32(__capi)
8802 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8803 seq_putc(m, '\n');
8804 return 0;
8805}
8806
8807static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8808{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008809 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008810 int i;
8811
Jens Axboefad8e0d2020-09-28 08:57:48 -06008812 /*
8813 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8814 * since fdinfo case grabs it in the opposite direction of normal use
8815 * cases. If we fail to get the lock, we just don't iterate any
8816 * structures that could be going away outside the io_uring mutex.
8817 */
8818 has_lock = mutex_trylock(&ctx->uring_lock);
8819
Jens Axboe87ce9552020-01-30 08:25:34 -07008820 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008821 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008822 struct fixed_file_table *table;
8823 struct file *f;
8824
8825 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8826 f = table->files[i & IORING_FILE_TABLE_MASK];
8827 if (f)
8828 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8829 else
8830 seq_printf(m, "%5u: <none>\n", i);
8831 }
8832 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008833 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008834 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8835
8836 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8837 (unsigned int) buf->len);
8838 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008839 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008840 seq_printf(m, "Personalities:\n");
8841 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8842 }
Jens Axboed7718a92020-02-14 22:23:12 -07008843 seq_printf(m, "PollList:\n");
8844 spin_lock_irq(&ctx->completion_lock);
8845 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8846 struct hlist_head *list = &ctx->cancel_hash[i];
8847 struct io_kiocb *req;
8848
8849 hlist_for_each_entry(req, list, hash_node)
8850 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8851 req->task->task_works != NULL);
8852 }
8853 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008854 if (has_lock)
8855 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008856}
8857
8858static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8859{
8860 struct io_ring_ctx *ctx = f->private_data;
8861
8862 if (percpu_ref_tryget(&ctx->refs)) {
8863 __io_uring_show_fdinfo(ctx, m);
8864 percpu_ref_put(&ctx->refs);
8865 }
8866}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008867#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008868
Jens Axboe2b188cc2019-01-07 10:46:33 -07008869static const struct file_operations io_uring_fops = {
8870 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008871 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008872 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008873#ifndef CONFIG_MMU
8874 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8875 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8876#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008877 .poll = io_uring_poll,
8878 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008879#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008880 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008881#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008882};
8883
8884static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8885 struct io_uring_params *p)
8886{
Hristo Venev75b28af2019-08-26 17:23:46 +00008887 struct io_rings *rings;
8888 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008889
Jens Axboebd740482020-08-05 12:58:23 -06008890 /* make sure these are sane, as we already accounted them */
8891 ctx->sq_entries = p->sq_entries;
8892 ctx->cq_entries = p->cq_entries;
8893
Hristo Venev75b28af2019-08-26 17:23:46 +00008894 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8895 if (size == SIZE_MAX)
8896 return -EOVERFLOW;
8897
8898 rings = io_mem_alloc(size);
8899 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008900 return -ENOMEM;
8901
Hristo Venev75b28af2019-08-26 17:23:46 +00008902 ctx->rings = rings;
8903 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8904 rings->sq_ring_mask = p->sq_entries - 1;
8905 rings->cq_ring_mask = p->cq_entries - 1;
8906 rings->sq_ring_entries = p->sq_entries;
8907 rings->cq_ring_entries = p->cq_entries;
8908 ctx->sq_mask = rings->sq_ring_mask;
8909 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008910
8911 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008912 if (size == SIZE_MAX) {
8913 io_mem_free(ctx->rings);
8914 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008915 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008916 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008917
8918 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008919 if (!ctx->sq_sqes) {
8920 io_mem_free(ctx->rings);
8921 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008922 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008923 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008924
Jens Axboe2b188cc2019-01-07 10:46:33 -07008925 return 0;
8926}
8927
8928/*
8929 * Allocate an anonymous fd, this is what constitutes the application
8930 * visible backing of an io_uring instance. The application mmaps this
8931 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8932 * we have to tie this fd to a socket for file garbage collection purposes.
8933 */
8934static int io_uring_get_fd(struct io_ring_ctx *ctx)
8935{
8936 struct file *file;
8937 int ret;
8938
8939#if defined(CONFIG_UNIX)
8940 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8941 &ctx->ring_sock);
8942 if (ret)
8943 return ret;
8944#endif
8945
8946 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8947 if (ret < 0)
8948 goto err;
8949
8950 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8951 O_RDWR | O_CLOEXEC);
8952 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008953err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07008954 put_unused_fd(ret);
8955 ret = PTR_ERR(file);
8956 goto err;
8957 }
8958
8959#if defined(CONFIG_UNIX)
8960 ctx->ring_sock->file = file;
8961#endif
Jens Axboe0f212202020-09-13 13:09:39 -06008962 if (unlikely(io_uring_add_task_file(file))) {
8963 file = ERR_PTR(-ENOMEM);
8964 goto err_fd;
8965 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008966 fd_install(ret, file);
8967 return ret;
8968err:
8969#if defined(CONFIG_UNIX)
8970 sock_release(ctx->ring_sock);
8971 ctx->ring_sock = NULL;
8972#endif
8973 return ret;
8974}
8975
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008976static int io_uring_create(unsigned entries, struct io_uring_params *p,
8977 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008978{
8979 struct user_struct *user = NULL;
8980 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008981 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008982 int ret;
8983
Jens Axboe8110c1a2019-12-28 15:39:54 -07008984 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008985 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008986 if (entries > IORING_MAX_ENTRIES) {
8987 if (!(p->flags & IORING_SETUP_CLAMP))
8988 return -EINVAL;
8989 entries = IORING_MAX_ENTRIES;
8990 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008991
8992 /*
8993 * Use twice as many entries for the CQ ring. It's possible for the
8994 * application to drive a higher depth than the size of the SQ ring,
8995 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008996 * some flexibility in overcommitting a bit. If the application has
8997 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8998 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008999 */
9000 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009001 if (p->flags & IORING_SETUP_CQSIZE) {
9002 /*
9003 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9004 * to a power-of-two, if it isn't already. We do NOT impose
9005 * any cq vs sq ring sizing.
9006 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009007 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009008 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009009 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9010 if (!(p->flags & IORING_SETUP_CLAMP))
9011 return -EINVAL;
9012 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9013 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009014 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9015 } else {
9016 p->cq_entries = 2 * p->sq_entries;
9017 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009018
9019 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009020 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009021
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009022 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009023 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009024 ring_pages(p->sq_entries, p->cq_entries));
9025 if (ret) {
9026 free_uid(user);
9027 return ret;
9028 }
9029 }
9030
9031 ctx = io_ring_ctx_alloc(p);
9032 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009033 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009034 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009035 p->cq_entries));
9036 free_uid(user);
9037 return -ENOMEM;
9038 }
9039 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009040 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009041 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009042
Jens Axboe2aede0e2020-09-14 10:45:53 -06009043 ctx->sqo_task = get_task_struct(current);
9044
9045 /*
9046 * This is just grabbed for accounting purposes. When a process exits,
9047 * the mm is exited and dropped before the files, hence we need to hang
9048 * on to this mm purely for the purposes of being able to unaccount
9049 * memory (locked/pinned vm). It's not used for anything else.
9050 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009051 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009052 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009053
Jens Axboef74441e2020-08-05 13:00:44 -06009054 /*
9055 * Account memory _before_ installing the file descriptor. Once
9056 * the descriptor is installed, it can get closed at any time. Also
9057 * do this before hitting the general error path, as ring freeing
9058 * will un-account as well.
9059 */
9060 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9061 ACCT_LOCKED);
9062 ctx->limit_mem = limit_mem;
9063
Jens Axboe2b188cc2019-01-07 10:46:33 -07009064 ret = io_allocate_scq_urings(ctx, p);
9065 if (ret)
9066 goto err;
9067
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009068 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009069 if (ret)
9070 goto err;
9071
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009072 if (!(p->flags & IORING_SETUP_R_DISABLED))
9073 io_sq_offload_start(ctx);
9074
Jens Axboe2b188cc2019-01-07 10:46:33 -07009075 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009076 p->sq_off.head = offsetof(struct io_rings, sq.head);
9077 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9078 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9079 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9080 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9081 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9082 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009083
9084 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009085 p->cq_off.head = offsetof(struct io_rings, cq.head);
9086 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9087 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9088 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9089 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9090 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009091 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009092
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009093 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9094 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009095 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9096 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009097
9098 if (copy_to_user(params, p, sizeof(*p))) {
9099 ret = -EFAULT;
9100 goto err;
9101 }
Jens Axboed1719f72020-07-30 13:43:53 -06009102
9103 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009104 * Install ring fd as the very last thing, so we don't risk someone
9105 * having closed it before we finish setup
9106 */
9107 ret = io_uring_get_fd(ctx);
9108 if (ret < 0)
9109 goto err;
9110
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009111 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009112 return ret;
9113err:
9114 io_ring_ctx_wait_and_kill(ctx);
9115 return ret;
9116}
9117
9118/*
9119 * Sets up an aio uring context, and returns the fd. Applications asks for a
9120 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9121 * params structure passed in.
9122 */
9123static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9124{
9125 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009126 int i;
9127
9128 if (copy_from_user(&p, params, sizeof(p)))
9129 return -EFAULT;
9130 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9131 if (p.resv[i])
9132 return -EINVAL;
9133 }
9134
Jens Axboe6c271ce2019-01-10 11:22:30 -07009135 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009136 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009137 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9138 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009139 return -EINVAL;
9140
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009141 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009142}
9143
9144SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9145 struct io_uring_params __user *, params)
9146{
9147 return io_uring_setup(entries, params);
9148}
9149
Jens Axboe66f4af92020-01-16 15:36:52 -07009150static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9151{
9152 struct io_uring_probe *p;
9153 size_t size;
9154 int i, ret;
9155
9156 size = struct_size(p, ops, nr_args);
9157 if (size == SIZE_MAX)
9158 return -EOVERFLOW;
9159 p = kzalloc(size, GFP_KERNEL);
9160 if (!p)
9161 return -ENOMEM;
9162
9163 ret = -EFAULT;
9164 if (copy_from_user(p, arg, size))
9165 goto out;
9166 ret = -EINVAL;
9167 if (memchr_inv(p, 0, size))
9168 goto out;
9169
9170 p->last_op = IORING_OP_LAST - 1;
9171 if (nr_args > IORING_OP_LAST)
9172 nr_args = IORING_OP_LAST;
9173
9174 for (i = 0; i < nr_args; i++) {
9175 p->ops[i].op = i;
9176 if (!io_op_defs[i].not_supported)
9177 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9178 }
9179 p->ops_len = i;
9180
9181 ret = 0;
9182 if (copy_to_user(arg, p, size))
9183 ret = -EFAULT;
9184out:
9185 kfree(p);
9186 return ret;
9187}
9188
Jens Axboe071698e2020-01-28 10:04:42 -07009189static int io_register_personality(struct io_ring_ctx *ctx)
9190{
9191 const struct cred *creds = get_current_cred();
9192 int id;
9193
9194 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9195 USHRT_MAX, GFP_KERNEL);
9196 if (id < 0)
9197 put_cred(creds);
9198 return id;
9199}
9200
9201static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9202{
9203 const struct cred *old_creds;
9204
9205 old_creds = idr_remove(&ctx->personality_idr, id);
9206 if (old_creds) {
9207 put_cred(old_creds);
9208 return 0;
9209 }
9210
9211 return -EINVAL;
9212}
9213
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009214static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9215 unsigned int nr_args)
9216{
9217 struct io_uring_restriction *res;
9218 size_t size;
9219 int i, ret;
9220
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009221 /* Restrictions allowed only if rings started disabled */
9222 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9223 return -EBADFD;
9224
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009225 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009226 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009227 return -EBUSY;
9228
9229 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9230 return -EINVAL;
9231
9232 size = array_size(nr_args, sizeof(*res));
9233 if (size == SIZE_MAX)
9234 return -EOVERFLOW;
9235
9236 res = memdup_user(arg, size);
9237 if (IS_ERR(res))
9238 return PTR_ERR(res);
9239
9240 ret = 0;
9241
9242 for (i = 0; i < nr_args; i++) {
9243 switch (res[i].opcode) {
9244 case IORING_RESTRICTION_REGISTER_OP:
9245 if (res[i].register_op >= IORING_REGISTER_LAST) {
9246 ret = -EINVAL;
9247 goto out;
9248 }
9249
9250 __set_bit(res[i].register_op,
9251 ctx->restrictions.register_op);
9252 break;
9253 case IORING_RESTRICTION_SQE_OP:
9254 if (res[i].sqe_op >= IORING_OP_LAST) {
9255 ret = -EINVAL;
9256 goto out;
9257 }
9258
9259 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9260 break;
9261 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9262 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9263 break;
9264 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9265 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9266 break;
9267 default:
9268 ret = -EINVAL;
9269 goto out;
9270 }
9271 }
9272
9273out:
9274 /* Reset all restrictions if an error happened */
9275 if (ret != 0)
9276 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9277 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009278 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009279
9280 kfree(res);
9281 return ret;
9282}
9283
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009284static int io_register_enable_rings(struct io_ring_ctx *ctx)
9285{
9286 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9287 return -EBADFD;
9288
9289 if (ctx->restrictions.registered)
9290 ctx->restricted = 1;
9291
9292 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9293
9294 io_sq_offload_start(ctx);
9295
9296 return 0;
9297}
9298
Jens Axboe071698e2020-01-28 10:04:42 -07009299static bool io_register_op_must_quiesce(int op)
9300{
9301 switch (op) {
9302 case IORING_UNREGISTER_FILES:
9303 case IORING_REGISTER_FILES_UPDATE:
9304 case IORING_REGISTER_PROBE:
9305 case IORING_REGISTER_PERSONALITY:
9306 case IORING_UNREGISTER_PERSONALITY:
9307 return false;
9308 default:
9309 return true;
9310 }
9311}
9312
Jens Axboeedafcce2019-01-09 09:16:05 -07009313static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9314 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009315 __releases(ctx->uring_lock)
9316 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009317{
9318 int ret;
9319
Jens Axboe35fa71a2019-04-22 10:23:23 -06009320 /*
9321 * We're inside the ring mutex, if the ref is already dying, then
9322 * someone else killed the ctx or is already going through
9323 * io_uring_register().
9324 */
9325 if (percpu_ref_is_dying(&ctx->refs))
9326 return -ENXIO;
9327
Jens Axboe071698e2020-01-28 10:04:42 -07009328 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009329 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009330
Jens Axboe05f3fb32019-12-09 11:22:50 -07009331 /*
9332 * Drop uring mutex before waiting for references to exit. If
9333 * another thread is currently inside io_uring_enter() it might
9334 * need to grab the uring_lock to make progress. If we hold it
9335 * here across the drain wait, then we can deadlock. It's safe
9336 * to drop the mutex here, since no new references will come in
9337 * after we've killed the percpu ref.
9338 */
9339 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009340 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009341 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009342 if (ret) {
9343 percpu_ref_resurrect(&ctx->refs);
9344 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009345 goto out_quiesce;
9346 }
9347 }
9348
9349 if (ctx->restricted) {
9350 if (opcode >= IORING_REGISTER_LAST) {
9351 ret = -EINVAL;
9352 goto out;
9353 }
9354
9355 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9356 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009357 goto out;
9358 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009359 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009360
9361 switch (opcode) {
9362 case IORING_REGISTER_BUFFERS:
9363 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9364 break;
9365 case IORING_UNREGISTER_BUFFERS:
9366 ret = -EINVAL;
9367 if (arg || nr_args)
9368 break;
9369 ret = io_sqe_buffer_unregister(ctx);
9370 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009371 case IORING_REGISTER_FILES:
9372 ret = io_sqe_files_register(ctx, arg, nr_args);
9373 break;
9374 case IORING_UNREGISTER_FILES:
9375 ret = -EINVAL;
9376 if (arg || nr_args)
9377 break;
9378 ret = io_sqe_files_unregister(ctx);
9379 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009380 case IORING_REGISTER_FILES_UPDATE:
9381 ret = io_sqe_files_update(ctx, arg, nr_args);
9382 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009383 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009384 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009385 ret = -EINVAL;
9386 if (nr_args != 1)
9387 break;
9388 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009389 if (ret)
9390 break;
9391 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9392 ctx->eventfd_async = 1;
9393 else
9394 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009395 break;
9396 case IORING_UNREGISTER_EVENTFD:
9397 ret = -EINVAL;
9398 if (arg || nr_args)
9399 break;
9400 ret = io_eventfd_unregister(ctx);
9401 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009402 case IORING_REGISTER_PROBE:
9403 ret = -EINVAL;
9404 if (!arg || nr_args > 256)
9405 break;
9406 ret = io_probe(ctx, arg, nr_args);
9407 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009408 case IORING_REGISTER_PERSONALITY:
9409 ret = -EINVAL;
9410 if (arg || nr_args)
9411 break;
9412 ret = io_register_personality(ctx);
9413 break;
9414 case IORING_UNREGISTER_PERSONALITY:
9415 ret = -EINVAL;
9416 if (arg)
9417 break;
9418 ret = io_unregister_personality(ctx, nr_args);
9419 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009420 case IORING_REGISTER_ENABLE_RINGS:
9421 ret = -EINVAL;
9422 if (arg || nr_args)
9423 break;
9424 ret = io_register_enable_rings(ctx);
9425 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009426 case IORING_REGISTER_RESTRICTIONS:
9427 ret = io_register_restrictions(ctx, arg, nr_args);
9428 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009429 default:
9430 ret = -EINVAL;
9431 break;
9432 }
9433
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009434out:
Jens Axboe071698e2020-01-28 10:04:42 -07009435 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009436 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009437 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009438out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009439 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009440 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009441 return ret;
9442}
9443
9444SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9445 void __user *, arg, unsigned int, nr_args)
9446{
9447 struct io_ring_ctx *ctx;
9448 long ret = -EBADF;
9449 struct fd f;
9450
9451 f = fdget(fd);
9452 if (!f.file)
9453 return -EBADF;
9454
9455 ret = -EOPNOTSUPP;
9456 if (f.file->f_op != &io_uring_fops)
9457 goto out_fput;
9458
9459 ctx = f.file->private_data;
9460
9461 mutex_lock(&ctx->uring_lock);
9462 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9463 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009464 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9465 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009466out_fput:
9467 fdput(f);
9468 return ret;
9469}
9470
Jens Axboe2b188cc2019-01-07 10:46:33 -07009471static int __init io_uring_init(void)
9472{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009473#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9474 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9475 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9476} while (0)
9477
9478#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9479 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9480 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9481 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9482 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9483 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9484 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9485 BUILD_BUG_SQE_ELEM(8, __u64, off);
9486 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9487 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009488 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009489 BUILD_BUG_SQE_ELEM(24, __u32, len);
9490 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9491 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9492 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9493 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009494 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9495 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009496 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9497 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9498 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9499 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9500 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9501 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9502 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9503 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009504 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009505 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9506 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9507 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009508 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009509
Jens Axboed3656342019-12-18 09:50:26 -07009510 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009511 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009512 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9513 return 0;
9514};
9515__initcall(io_uring_init);