blob: c4855cecc8f30a32fd3077a340ad139e5a89167f [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;
230};
231
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232struct io_ring_ctx {
233 struct {
234 struct percpu_ref refs;
235 } ____cacheline_aligned_in_smp;
236
237 struct {
238 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800239 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700240 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800241 unsigned int cq_overflow_flushed: 1;
242 unsigned int drain_next: 1;
243 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200244 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245
Hristo Venev75b28af2019-08-26 17:23:46 +0000246 /*
247 * Ring buffer of indices into array of io_uring_sqe, which is
248 * mmapped by the application using the IORING_OFF_SQES offset.
249 *
250 * This indirection could e.g. be used to assign fixed
251 * io_uring_sqe entries to operations and only submit them to
252 * the queue when needed.
253 *
254 * The kernel modifies neither the indices array nor the entries
255 * array.
256 */
257 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258 unsigned cached_sq_head;
259 unsigned sq_entries;
260 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700261 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600262 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700263 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700264 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600265
266 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600267 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700268 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700269
Jens Axboefcb323c2019-10-24 12:39:47 -0600270 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700271 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700272 } ____cacheline_aligned_in_smp;
273
Hristo Venev75b28af2019-08-26 17:23:46 +0000274 struct io_rings *rings;
275
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600277 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2aede0e2020-09-14 10:45:53 -0600279
280 /*
281 * For SQPOLL usage - we hold a reference to the parent task, so we
282 * have access to the ->files
283 */
284 struct task_struct *sqo_task;
285
286 /* Only used for accounting purposes */
287 struct mm_struct *mm_account;
288
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290
Jens Axboe6b063142019-01-10 22:13:58 -0700291 /*
292 * If used, fixed file set. Writers must ensure that ->refs is dead,
293 * readers must ensure that ->refs is alive as long as the file* is
294 * used. Only updated through io_uring_register(2).
295 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700296 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700297 unsigned nr_user_files;
298
Jens Axboeedafcce2019-01-09 09:16:05 -0700299 /* if used, fixed mapped user buffers */
300 unsigned nr_user_bufs;
301 struct io_mapped_ubuf *user_bufs;
302
Jens Axboe2b188cc2019-01-07 10:46:33 -0700303 struct user_struct *user;
304
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700305 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700306
Jens Axboe0f158b42020-05-14 17:18:39 -0600307 struct completion ref_comp;
308 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700309
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700310 /* if all else fails... */
311 struct io_kiocb *fallback_req;
312
Jens Axboe206aefd2019-11-07 18:27:42 -0700313#if defined(CONFIG_UNIX)
314 struct socket *ring_sock;
315#endif
316
Jens Axboe5a2e7452020-02-23 16:23:11 -0700317 struct idr io_buffer_idr;
318
Jens Axboe071698e2020-01-28 10:04:42 -0700319 struct idr personality_idr;
320
Jens Axboe206aefd2019-11-07 18:27:42 -0700321 struct {
322 unsigned cached_cq_tail;
323 unsigned cq_entries;
324 unsigned cq_mask;
325 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700326 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700327 struct wait_queue_head cq_wait;
328 struct fasync_struct *cq_fasync;
329 struct eventfd_ctx *cq_ev_fd;
330 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331
332 struct {
333 struct mutex uring_lock;
334 wait_queue_head_t wait;
335 } ____cacheline_aligned_in_smp;
336
337 struct {
338 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700339
Jens Axboedef596e2019-01-09 08:59:42 -0700340 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300341 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700342 * io_uring instances that don't use IORING_SETUP_SQPOLL.
343 * For SQPOLL, only the single threaded io_sq_thread() will
344 * manipulate the list, hence no extra locking is needed there.
345 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300346 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700347 struct hlist_head *cancel_hash;
348 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700349 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600350
351 spinlock_t inflight_lock;
352 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700353 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600354
Jens Axboe4a38aed22020-05-14 17:21:15 -0600355 struct delayed_work file_put_work;
356 struct llist_head file_put_llist;
357
Jens Axboe85faa7b2020-04-09 18:14:00 -0600358 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200359 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700360};
361
Jens Axboe09bb8392019-03-13 12:39:28 -0600362/*
363 * First field must be the file pointer in all the
364 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
365 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700366struct io_poll_iocb {
367 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700368 union {
369 struct wait_queue_head *head;
370 u64 addr;
371 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700372 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600373 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700374 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700375 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700376};
377
Jens Axboeb5dba592019-12-11 14:02:38 -0700378struct io_close {
379 struct file *file;
380 struct file *put_file;
381 int fd;
382};
383
Jens Axboead8a48a2019-11-15 08:49:11 -0700384struct io_timeout_data {
385 struct io_kiocb *req;
386 struct hrtimer timer;
387 struct timespec64 ts;
388 enum hrtimer_mode mode;
389};
390
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700391struct io_accept {
392 struct file *file;
393 struct sockaddr __user *addr;
394 int __user *addr_len;
395 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600396 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700397};
398
399struct io_sync {
400 struct file *file;
401 loff_t len;
402 loff_t off;
403 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700404 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700405};
406
Jens Axboefbf23842019-12-17 18:45:56 -0700407struct io_cancel {
408 struct file *file;
409 u64 addr;
410};
411
Jens Axboeb29472e2019-12-17 18:50:29 -0700412struct io_timeout {
413 struct file *file;
414 u64 addr;
415 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300416 u32 off;
417 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300418 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700419};
420
Jens Axboe9adbd452019-12-20 08:45:55 -0700421struct io_rw {
422 /* NOTE: kiocb has the file as the first member, so don't do it here */
423 struct kiocb kiocb;
424 u64 addr;
425 u64 len;
426};
427
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700428struct io_connect {
429 struct file *file;
430 struct sockaddr __user *addr;
431 int addr_len;
432};
433
Jens Axboee47293f2019-12-20 08:58:21 -0700434struct io_sr_msg {
435 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700436 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300437 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700438 void __user *buf;
439 };
Jens Axboee47293f2019-12-20 08:58:21 -0700440 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700441 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700442 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700443 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700444};
445
Jens Axboe15b71ab2019-12-11 11:20:36 -0700446struct io_open {
447 struct file *file;
448 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700449 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700450 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600451 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700452};
453
Jens Axboe05f3fb32019-12-09 11:22:50 -0700454struct io_files_update {
455 struct file *file;
456 u64 arg;
457 u32 nr_args;
458 u32 offset;
459};
460
Jens Axboe4840e412019-12-25 22:03:45 -0700461struct io_fadvise {
462 struct file *file;
463 u64 offset;
464 u32 len;
465 u32 advice;
466};
467
Jens Axboec1ca7572019-12-25 22:18:28 -0700468struct io_madvise {
469 struct file *file;
470 u64 addr;
471 u32 len;
472 u32 advice;
473};
474
Jens Axboe3e4827b2020-01-08 15:18:09 -0700475struct io_epoll {
476 struct file *file;
477 int epfd;
478 int op;
479 int fd;
480 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700481};
482
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300483struct io_splice {
484 struct file *file_out;
485 struct file *file_in;
486 loff_t off_out;
487 loff_t off_in;
488 u64 len;
489 unsigned int flags;
490};
491
Jens Axboeddf0322d2020-02-23 16:41:33 -0700492struct io_provide_buf {
493 struct file *file;
494 __u64 addr;
495 __s32 len;
496 __u32 bgid;
497 __u16 nbufs;
498 __u16 bid;
499};
500
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700501struct io_statx {
502 struct file *file;
503 int dfd;
504 unsigned int mask;
505 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700506 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700507 struct statx __user *buffer;
508};
509
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300510struct io_completion {
511 struct file *file;
512 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300513 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300514};
515
Jens Axboef499a022019-12-02 16:28:46 -0700516struct io_async_connect {
517 struct sockaddr_storage address;
518};
519
Jens Axboe03b12302019-12-02 18:50:25 -0700520struct io_async_msghdr {
521 struct iovec fast_iov[UIO_FASTIOV];
522 struct iovec *iov;
523 struct sockaddr __user *uaddr;
524 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700525 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700526};
527
Jens Axboef67676d2019-12-02 11:03:47 -0700528struct io_async_rw {
529 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600530 const struct iovec *free_iovec;
531 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600532 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600533 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700534};
535
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700536struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700537 union {
538 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700539 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700540 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700541 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700542 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700543};
544
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300545enum {
546 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
547 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
548 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
549 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
550 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700551 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300552
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300553 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300554 REQ_F_FAIL_LINK_BIT,
555 REQ_F_INFLIGHT_BIT,
556 REQ_F_CUR_POS_BIT,
557 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300558 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300559 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300560 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300561 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700562 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700563 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600564 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800565 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700566
567 /* not a real bit, just to check we're not overflowing the space */
568 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300569};
570
571enum {
572 /* ctx owns file */
573 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
574 /* drain existing IO first */
575 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
576 /* linked sqes */
577 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
578 /* doesn't sever on completion < 0 */
579 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
580 /* IOSQE_ASYNC */
581 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700582 /* IOSQE_BUFFER_SELECT */
583 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300584
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300585 /* head of a link */
586 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300587 /* fail rest of links */
588 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
589 /* on inflight list */
590 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
591 /* read/write uses file position */
592 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
593 /* must not punt to workers */
594 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300595 /* has linked timeout */
596 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300597 /* regular file */
598 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300599 /* completion under lock */
600 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300601 /* needs cleanup */
602 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700603 /* already went through poll handler */
604 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700605 /* buffer already selected */
606 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600607 /* doesn't need file table for this request */
608 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800609 /* io_wq_work is initialized */
610 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700611};
612
613struct async_poll {
614 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600615 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300616};
617
Jens Axboe09bb8392019-03-13 12:39:28 -0600618/*
619 * NOTE! Each of the iocb union members has the file pointer
620 * as the first entry in their struct definition. So you can
621 * access the file pointer through any of the sub-structs,
622 * or directly as just 'ki_filp' in this struct.
623 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700624struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700625 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600626 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700627 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700628 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700629 struct io_accept accept;
630 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700631 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700632 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700633 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700634 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700635 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700636 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700637 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700638 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700639 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700640 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300641 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700642 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700643 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300644 /* use only after cleaning per-op data, see io_clean_op() */
645 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700646 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700647
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700648 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700649 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800650 /* polled IO has completed */
651 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700652
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700653 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300654 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700655
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300656 struct io_ring_ctx *ctx;
657 unsigned int flags;
658 refcount_t refs;
659 struct task_struct *task;
660 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700661
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300662 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700663
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300664 /*
665 * 1. used with ctx->iopoll_list with reads/writes
666 * 2. to track reqs with ->files (see io_op_def::file_table)
667 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300668 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600669
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300670 struct percpu_ref *fixed_file_refs;
671 struct callback_head task_work;
672 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
673 struct hlist_node hash_node;
674 struct async_poll *apoll;
675 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700676};
677
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300678struct io_defer_entry {
679 struct list_head list;
680 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300681 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300682};
683
Jens Axboedef596e2019-01-09 08:59:42 -0700684#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700685
Jens Axboe013538b2020-06-22 09:29:15 -0600686struct io_comp_state {
687 unsigned int nr;
688 struct list_head list;
689 struct io_ring_ctx *ctx;
690};
691
Jens Axboe9a56a232019-01-09 09:06:50 -0700692struct io_submit_state {
693 struct blk_plug plug;
694
695 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700696 * io_kiocb alloc cache
697 */
698 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300699 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700700
701 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600702 * Batch completion logic
703 */
704 struct io_comp_state comp;
705
706 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700707 * File reference cache
708 */
709 struct file *file;
710 unsigned int fd;
711 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700712 unsigned int ios_left;
713};
714
Jens Axboed3656342019-12-18 09:50:26 -0700715struct io_op_def {
716 /* needs req->io allocated for deferral/async */
717 unsigned async_ctx : 1;
718 /* needs current->mm setup, does mm access */
719 unsigned needs_mm : 1;
720 /* needs req->file assigned */
721 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600722 /* don't fail if file grab fails */
723 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700724 /* hash wq insertion if file is a regular file */
725 unsigned hash_reg_file : 1;
726 /* unbound wq insertion if file is a non-regular file */
727 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700728 /* opcode is not supported by this kernel */
729 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700730 /* needs file table */
731 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700732 /* needs ->fs */
733 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700734 /* set if opcode supports polled "wait" */
735 unsigned pollin : 1;
736 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700737 /* op supports buffer selection */
738 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300739 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700740};
741
742static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300743 [IORING_OP_NOP] = {},
744 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700745 .async_ctx = 1,
746 .needs_mm = 1,
747 .needs_file = 1,
748 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700749 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700750 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700751 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300752 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .async_ctx = 1,
754 .needs_mm = 1,
755 .needs_file = 1,
756 .hash_reg_file = 1,
757 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700758 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300759 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700760 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300761 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700762 .needs_file = 1,
763 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300764 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700765 .needs_file = 1,
766 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700767 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .needs_file = 1,
771 .hash_reg_file = 1,
772 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700773 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300774 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700777 .needs_file = 1,
778 .unbound_nonreg_file = 1,
779 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300780 [IORING_OP_POLL_REMOVE] = {},
781 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700782 .needs_file = 1,
783 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300784 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700785 .async_ctx = 1,
786 .needs_mm = 1,
787 .needs_file = 1,
788 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700789 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700790 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_RECVMSG] = {
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 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700799 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700802 .async_ctx = 1,
803 .needs_mm = 1,
804 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300805 [IORING_OP_TIMEOUT_REMOVE] = {},
806 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700807 .needs_mm = 1,
808 .needs_file = 1,
809 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700810 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700811 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700812 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300813 [IORING_OP_ASYNC_CANCEL] = {},
814 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700815 .async_ctx = 1,
816 .needs_mm = 1,
817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .async_ctx = 1,
820 .needs_mm = 1,
821 .needs_file = 1,
822 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700823 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700826 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300827 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700828 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300829 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700830 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700831 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700832 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300833 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600834 .needs_file = 1,
835 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700836 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700837 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300838 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700839 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700840 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700843 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700844 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600845 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700846 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300847 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700848 .needs_mm = 1,
849 .needs_file = 1,
850 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700851 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700852 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700855 .needs_mm = 1,
856 .needs_file = 1,
857 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700858 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300859 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700860 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300861 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700862 .needs_file = 1,
863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700865 .needs_mm = 1,
866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700868 .needs_mm = 1,
869 .needs_file = 1,
870 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700871 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700872 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300873 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700874 .needs_mm = 1,
875 .needs_file = 1,
876 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700877 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700878 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700879 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300880 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700881 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700882 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700883 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700884 [IORING_OP_EPOLL_CTL] = {
885 .unbound_nonreg_file = 1,
886 .file_table = 1,
887 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300888 [IORING_OP_SPLICE] = {
889 .needs_file = 1,
890 .hash_reg_file = 1,
891 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700892 },
893 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700894 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300895 [IORING_OP_TEE] = {
896 .needs_file = 1,
897 .hash_reg_file = 1,
898 .unbound_nonreg_file = 1,
899 },
Jens Axboed3656342019-12-18 09:50:26 -0700900};
901
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700902enum io_mem_account {
903 ACCT_LOCKED,
904 ACCT_PINNED,
905};
906
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300907static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
908 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700909static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800910static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600911static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700912static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700913static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600914static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700915static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700916static int __io_sqe_files_update(struct io_ring_ctx *ctx,
917 struct io_uring_files_update *ip,
918 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300919static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300920static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700921static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
922 int fd, struct file **out_file, bool fixed);
923static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600924 const struct io_uring_sqe *sqe,
925 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600926static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600927
Jens Axboeb63534c2020-06-04 11:28:00 -0600928static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
929 struct iovec **iovec, struct iov_iter *iter,
930 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600931static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
932 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600933 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700934
935static struct kmem_cache *req_cachep;
936
937static const struct file_operations io_uring_fops;
938
939struct sock *io_uring_get_socket(struct file *file)
940{
941#if defined(CONFIG_UNIX)
942 if (file->f_op == &io_uring_fops) {
943 struct io_ring_ctx *ctx = file->private_data;
944
945 return ctx->ring_sock->sk;
946 }
947#endif
948 return NULL;
949}
950EXPORT_SYMBOL(io_uring_get_socket);
951
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300952static inline void io_clean_op(struct io_kiocb *req)
953{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300954 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
955 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300956 __io_clean_op(req);
957}
958
Jens Axboe4349f302020-07-09 15:07:01 -0600959static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600960{
961 struct mm_struct *mm = current->mm;
962
963 if (mm) {
964 kthread_unuse_mm(mm);
965 mmput(mm);
966 }
967}
968
969static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
970{
971 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300972 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600973 !ctx->sqo_task->mm ||
974 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600975 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600976 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600977 }
978
979 return 0;
980}
981
982static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
983 struct io_kiocb *req)
984{
985 if (!io_op_defs[req->opcode].needs_mm)
986 return 0;
987 return __io_sq_thread_acquire_mm(ctx);
988}
989
990static inline void req_set_fail_links(struct io_kiocb *req)
991{
992 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
993 req->flags |= REQ_F_FAIL_LINK;
994}
Jens Axboe4a38aed22020-05-14 17:21:15 -0600995
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800996/*
997 * Note: must call io_req_init_async() for the first time you
998 * touch any members of io_wq_work.
999 */
1000static inline void io_req_init_async(struct io_kiocb *req)
1001{
1002 if (req->flags & REQ_F_WORK_INITIALIZED)
1003 return;
1004
1005 memset(&req->work, 0, sizeof(req->work));
1006 req->flags |= REQ_F_WORK_INITIALIZED;
1007}
1008
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001009static inline bool io_async_submit(struct io_ring_ctx *ctx)
1010{
1011 return ctx->flags & IORING_SETUP_SQPOLL;
1012}
1013
Jens Axboe2b188cc2019-01-07 10:46:33 -07001014static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1015{
1016 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1017
Jens Axboe0f158b42020-05-14 17:18:39 -06001018 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001019}
1020
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001021static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1022{
1023 return !req->timeout.off;
1024}
1025
Jens Axboe2b188cc2019-01-07 10:46:33 -07001026static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1027{
1028 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001029 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030
1031 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1032 if (!ctx)
1033 return NULL;
1034
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001035 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1036 if (!ctx->fallback_req)
1037 goto err;
1038
Jens Axboe78076bb2019-12-04 19:56:40 -07001039 /*
1040 * Use 5 bits less than the max cq entries, that should give us around
1041 * 32 entries per hash list if totally full and uniformly spread.
1042 */
1043 hash_bits = ilog2(p->cq_entries);
1044 hash_bits -= 5;
1045 if (hash_bits <= 0)
1046 hash_bits = 1;
1047 ctx->cancel_hash_bits = hash_bits;
1048 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1049 GFP_KERNEL);
1050 if (!ctx->cancel_hash)
1051 goto err;
1052 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1053
Roman Gushchin21482892019-05-07 10:01:48 -07001054 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001055 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1056 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001057
1058 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001059 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001061 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001062 init_completion(&ctx->ref_comp);
1063 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001064 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001065 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001066 mutex_init(&ctx->uring_lock);
1067 init_waitqueue_head(&ctx->wait);
1068 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001069 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001070 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001071 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001072 init_waitqueue_head(&ctx->inflight_wait);
1073 spin_lock_init(&ctx->inflight_lock);
1074 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001075 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1076 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001078err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001079 if (ctx->fallback_req)
1080 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001081 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001082 kfree(ctx);
1083 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084}
1085
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001086static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001087{
Jens Axboe2bc99302020-07-09 09:43:27 -06001088 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1089 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001090
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001091 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001092 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001093 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001094
Bob Liu9d858b22019-11-13 18:06:25 +08001095 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001096}
1097
Jens Axboede0617e2019-04-06 21:51:27 -06001098static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001099{
Hristo Venev75b28af2019-08-26 17:23:46 +00001100 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101
Pavel Begunkov07910152020-01-17 03:52:46 +03001102 /* order cqe stores with ring update */
1103 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104
Pavel Begunkov07910152020-01-17 03:52:46 +03001105 if (wq_has_sleeper(&ctx->cq_wait)) {
1106 wake_up_interruptible(&ctx->cq_wait);
1107 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001108 }
1109}
1110
Jens Axboe51a4cc12020-08-10 10:55:56 -06001111/*
1112 * Returns true if we need to defer file table putting. This can only happen
1113 * from the error path with REQ_F_COMP_LOCKED set.
1114 */
1115static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001116{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001117 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001118 return false;
1119
1120 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001121
Jens Axboecccf0ee2020-01-27 16:34:48 -07001122 if (req->work.mm) {
1123 mmdrop(req->work.mm);
1124 req->work.mm = NULL;
1125 }
1126 if (req->work.creds) {
1127 put_cred(req->work.creds);
1128 req->work.creds = NULL;
1129 }
Jens Axboeff002b32020-02-07 16:05:21 -07001130 if (req->work.fs) {
1131 struct fs_struct *fs = req->work.fs;
1132
Jens Axboe51a4cc12020-08-10 10:55:56 -06001133 if (req->flags & REQ_F_COMP_LOCKED)
1134 return true;
1135
Jens Axboeff002b32020-02-07 16:05:21 -07001136 spin_lock(&req->work.fs->lock);
1137 if (--fs->users)
1138 fs = NULL;
1139 spin_unlock(&req->work.fs->lock);
1140 if (fs)
1141 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001142 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001143 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001144
1145 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001146}
1147
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001148static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001149{
Jens Axboed3656342019-12-18 09:50:26 -07001150 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001151
Pavel Begunkov16d59802020-07-12 16:16:47 +03001152 io_req_init_async(req);
1153
Jens Axboed3656342019-12-18 09:50:26 -07001154 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001155 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001156 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001157 } else {
1158 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001159 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001160 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001161 if (!req->work.mm && def->needs_mm) {
1162 mmgrab(current->mm);
1163 req->work.mm = current->mm;
1164 }
1165 if (!req->work.creds)
1166 req->work.creds = get_current_cred();
1167 if (!req->work.fs && def->needs_fs) {
1168 spin_lock(&current->fs->lock);
1169 if (!current->fs->in_exec) {
1170 req->work.fs = current->fs;
1171 req->work.fs->users++;
1172 } else {
1173 req->work.flags |= IO_WQ_WORK_CANCEL;
1174 }
1175 spin_unlock(&current->fs->lock);
1176 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001177 if (def->needs_fsize)
1178 req->work.fsize = rlimit(RLIMIT_FSIZE);
1179 else
1180 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001181}
1182
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001183static void io_prep_async_link(struct io_kiocb *req)
1184{
1185 struct io_kiocb *cur;
1186
1187 io_prep_async_work(req);
1188 if (req->flags & REQ_F_LINK_HEAD)
1189 list_for_each_entry(cur, &req->link_list, link_list)
1190 io_prep_async_work(cur);
1191}
1192
Jens Axboe7271ef32020-08-10 09:55:22 -06001193static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001194{
Jackie Liua197f662019-11-08 08:09:12 -07001195 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001196 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001197
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001198 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1199 &req->work, req->flags);
1200 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001201 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001202}
1203
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001204static void io_queue_async_work(struct io_kiocb *req)
1205{
Jens Axboe7271ef32020-08-10 09:55:22 -06001206 struct io_kiocb *link;
1207
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001208 /* init ->work of the whole link before punting */
1209 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001210 link = __io_queue_async_work(req);
1211
1212 if (link)
1213 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001214}
1215
Jens Axboe5262f562019-09-17 12:26:57 -06001216static void io_kill_timeout(struct io_kiocb *req)
1217{
1218 int ret;
1219
Jens Axboe2d283902019-12-04 11:08:05 -07001220 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001221 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001222 atomic_set(&req->ctx->cq_timeouts,
1223 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001224 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001225 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001226 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001227 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001228 }
1229}
1230
Jens Axboef3606e32020-09-22 08:18:24 -06001231static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1232{
1233 struct io_ring_ctx *ctx = req->ctx;
1234
1235 if (!tsk || req->task == tsk)
1236 return true;
1237 if ((ctx->flags & IORING_SETUP_SQPOLL) && req->task == ctx->sqo_thread)
1238 return true;
1239 return false;
1240}
1241
Jens Axboe76e1b642020-09-26 15:05:03 -06001242/*
1243 * Returns true if we found and killed one or more timeouts
1244 */
1245static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001246{
1247 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001248 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001249
1250 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001251 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001252 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001253 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001254 canceled++;
1255 }
Jens Axboef3606e32020-09-22 08:18:24 -06001256 }
Jens Axboe5262f562019-09-17 12:26:57 -06001257 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001258 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001259}
1260
Pavel Begunkov04518942020-05-26 20:34:05 +03001261static void __io_queue_deferred(struct io_ring_ctx *ctx)
1262{
1263 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001264 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1265 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001266 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001267
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001268 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001269 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001270 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001271 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001272 link = __io_queue_async_work(de->req);
1273 if (link) {
1274 __io_queue_linked_timeout(link);
1275 /* drop submission reference */
1276 link->flags |= REQ_F_COMP_LOCKED;
1277 io_put_req(link);
1278 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001279 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001280 } while (!list_empty(&ctx->defer_list));
1281}
1282
Pavel Begunkov360428f2020-05-30 14:54:17 +03001283static void io_flush_timeouts(struct io_ring_ctx *ctx)
1284{
1285 while (!list_empty(&ctx->timeout_list)) {
1286 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001287 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001288
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001289 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001290 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001291 if (req->timeout.target_seq != ctx->cached_cq_tail
1292 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001293 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001294
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001295 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001296 io_kill_timeout(req);
1297 }
1298}
1299
Jens Axboede0617e2019-04-06 21:51:27 -06001300static void io_commit_cqring(struct io_ring_ctx *ctx)
1301{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001302 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001303 __io_commit_cqring(ctx);
1304
Pavel Begunkov04518942020-05-26 20:34:05 +03001305 if (unlikely(!list_empty(&ctx->defer_list)))
1306 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001307}
1308
Jens Axboe2b188cc2019-01-07 10:46:33 -07001309static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1310{
Hristo Venev75b28af2019-08-26 17:23:46 +00001311 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001312 unsigned tail;
1313
1314 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001315 /*
1316 * writes to the cq entry need to come after reading head; the
1317 * control dependency is enough as we're using WRITE_ONCE to
1318 * fill the cq entry
1319 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001320 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001321 return NULL;
1322
1323 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001324 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325}
1326
Jens Axboef2842ab2020-01-08 11:04:00 -07001327static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1328{
Jens Axboef0b493e2020-02-01 21:30:11 -07001329 if (!ctx->cq_ev_fd)
1330 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001331 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1332 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001333 if (!ctx->eventfd_async)
1334 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001335 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001336}
1337
Jens Axboeb41e9852020-02-17 09:52:41 -07001338static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001339{
1340 if (waitqueue_active(&ctx->wait))
1341 wake_up(&ctx->wait);
1342 if (waitqueue_active(&ctx->sqo_wait))
1343 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001344 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001345 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001346}
1347
Pavel Begunkov46930142020-07-30 18:43:49 +03001348static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1349{
1350 if (list_empty(&ctx->cq_overflow_list)) {
1351 clear_bit(0, &ctx->sq_check_overflow);
1352 clear_bit(0, &ctx->cq_check_overflow);
1353 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1354 }
1355}
1356
Jens Axboee6c8aa92020-09-28 13:10:13 -06001357static inline bool io_match_files(struct io_kiocb *req,
1358 struct files_struct *files)
1359{
1360 if (!files)
1361 return true;
1362 if (req->flags & REQ_F_WORK_INITIALIZED)
1363 return req->work.files == files;
1364 return false;
1365}
1366
Jens Axboec4a2ed72019-11-21 21:01:26 -07001367/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001368static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1369 struct task_struct *tsk,
1370 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001371{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001372 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001373 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001374 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001375 unsigned long flags;
1376 LIST_HEAD(list);
1377
1378 if (!force) {
1379 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001380 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001381 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1382 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001383 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001384 }
1385
1386 spin_lock_irqsave(&ctx->completion_lock, flags);
1387
1388 /* if force is set, the ring is going away. always drop after that */
1389 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001390 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001391
Jens Axboec4a2ed72019-11-21 21:01:26 -07001392 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001393 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1394 if (tsk && req->task != tsk)
1395 continue;
1396 if (!io_match_files(req, files))
1397 continue;
1398
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001399 cqe = io_get_cqring(ctx);
1400 if (!cqe && !force)
1401 break;
1402
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001403 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001404 if (cqe) {
1405 WRITE_ONCE(cqe->user_data, req->user_data);
1406 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001407 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001408 } else {
1409 WRITE_ONCE(ctx->rings->cq_overflow,
1410 atomic_inc_return(&ctx->cached_cq_overflow));
1411 }
1412 }
1413
1414 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001415 io_cqring_mark_overflow(ctx);
1416
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001417 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1418 io_cqring_ev_posted(ctx);
1419
1420 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001421 req = list_first_entry(&list, struct io_kiocb, compl.list);
1422 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001423 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001424 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001425
1426 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001427}
1428
Jens Axboebcda7ba2020-02-23 16:42:51 -07001429static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001431 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001432 struct io_uring_cqe *cqe;
1433
Jens Axboe78e19bb2019-11-06 15:21:34 -07001434 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001435
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436 /*
1437 * If we can't get a cq entry, userspace overflowed the
1438 * submission (by quite a lot). Increment the overflow count in
1439 * the ring.
1440 */
1441 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001442 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001443 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001444 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001445 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001446 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1447 /*
1448 * If we're in ring overflow flush mode, or in task cancel mode,
1449 * then we cannot store the request for later flushing, we need
1450 * to drop it on the floor.
1451 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452 WRITE_ONCE(ctx->rings->cq_overflow,
1453 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001454 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001455 if (list_empty(&ctx->cq_overflow_list)) {
1456 set_bit(0, &ctx->sq_check_overflow);
1457 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001458 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001459 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001460 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001461 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001462 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001463 refcount_inc(&req->refs);
1464 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465 }
1466}
1467
Jens Axboebcda7ba2020-02-23 16:42:51 -07001468static void io_cqring_fill_event(struct io_kiocb *req, long res)
1469{
1470 __io_cqring_fill_event(req, res, 0);
1471}
1472
Jens Axboee1e16092020-06-22 09:17:17 -06001473static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001474{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001475 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001476 unsigned long flags;
1477
1478 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001479 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480 io_commit_cqring(ctx);
1481 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1482
Jens Axboe8c838782019-03-12 15:48:16 -06001483 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001484}
1485
Jens Axboe229a7b62020-06-22 10:13:11 -06001486static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001487{
Jens Axboe229a7b62020-06-22 10:13:11 -06001488 struct io_ring_ctx *ctx = cs->ctx;
1489
1490 spin_lock_irq(&ctx->completion_lock);
1491 while (!list_empty(&cs->list)) {
1492 struct io_kiocb *req;
1493
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001494 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1495 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001496 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001497 if (!(req->flags & REQ_F_LINK_HEAD)) {
1498 req->flags |= REQ_F_COMP_LOCKED;
1499 io_put_req(req);
1500 } else {
1501 spin_unlock_irq(&ctx->completion_lock);
1502 io_put_req(req);
1503 spin_lock_irq(&ctx->completion_lock);
1504 }
1505 }
1506 io_commit_cqring(ctx);
1507 spin_unlock_irq(&ctx->completion_lock);
1508
1509 io_cqring_ev_posted(ctx);
1510 cs->nr = 0;
1511}
1512
1513static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1514 struct io_comp_state *cs)
1515{
1516 if (!cs) {
1517 io_cqring_add_event(req, res, cflags);
1518 io_put_req(req);
1519 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001520 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001521 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001522 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001523 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001524 if (++cs->nr >= 32)
1525 io_submit_flush_completions(cs);
1526 }
Jens Axboee1e16092020-06-22 09:17:17 -06001527}
1528
1529static void io_req_complete(struct io_kiocb *req, long res)
1530{
Jens Axboe229a7b62020-06-22 10:13:11 -06001531 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001532}
1533
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001534static inline bool io_is_fallback_req(struct io_kiocb *req)
1535{
1536 return req == (struct io_kiocb *)
1537 ((unsigned long) req->ctx->fallback_req & ~1UL);
1538}
1539
1540static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1541{
1542 struct io_kiocb *req;
1543
1544 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001545 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001546 return req;
1547
1548 return NULL;
1549}
1550
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001551static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1552 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553{
Jens Axboefd6fab22019-03-14 16:30:06 -06001554 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001555 struct io_kiocb *req;
1556
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001557 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001558 size_t sz;
1559 int ret;
1560
1561 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001562 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1563
1564 /*
1565 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1566 * retry single alloc to be on the safe side.
1567 */
1568 if (unlikely(ret <= 0)) {
1569 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1570 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001571 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001572 ret = 1;
1573 }
Jens Axboe2579f912019-01-09 09:10:43 -07001574 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001575 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001576 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001577 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001578 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579 }
1580
Jens Axboe2579f912019-01-09 09:10:43 -07001581 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001582fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001583 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001584}
1585
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001586static inline void io_put_file(struct io_kiocb *req, struct file *file,
1587 bool fixed)
1588{
1589 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001590 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001591 else
1592 fput(file);
1593}
1594
Jens Axboe51a4cc12020-08-10 10:55:56 -06001595static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001596{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001597 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001598
Jens Axboe5acbbc82020-07-08 15:15:26 -06001599 if (req->io)
1600 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001601 if (req->file)
1602 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001603
Jens Axboe51a4cc12020-08-10 10:55:56 -06001604 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001605}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001606
Jens Axboe51a4cc12020-08-10 10:55:56 -06001607static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001608{
Jens Axboe0f212202020-09-13 13:09:39 -06001609 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001610 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001611
Jens Axboe0f212202020-09-13 13:09:39 -06001612 atomic_long_inc(&tctx->req_complete);
1613 if (tctx->in_idle)
1614 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001615 put_task_struct(req->task);
1616
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001617 if (likely(!io_is_fallback_req(req)))
1618 kmem_cache_free(req_cachep, req);
1619 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001620 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1621 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001622}
1623
Jens Axboe51a4cc12020-08-10 10:55:56 -06001624static void io_req_task_file_table_put(struct callback_head *cb)
1625{
1626 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1627 struct fs_struct *fs = req->work.fs;
1628
1629 spin_lock(&req->work.fs->lock);
1630 if (--fs->users)
1631 fs = NULL;
1632 spin_unlock(&req->work.fs->lock);
1633 if (fs)
1634 free_fs_struct(fs);
1635 req->work.fs = NULL;
1636 __io_free_req_finish(req);
1637}
1638
1639static void __io_free_req(struct io_kiocb *req)
1640{
1641 if (!io_dismantle_req(req)) {
1642 __io_free_req_finish(req);
1643 } else {
1644 int ret;
1645
1646 init_task_work(&req->task_work, io_req_task_file_table_put);
1647 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1648 if (unlikely(ret)) {
1649 struct task_struct *tsk;
1650
1651 tsk = io_wq_get_task(req->ctx->io_wq);
1652 task_work_add(tsk, &req->task_work, 0);
1653 }
1654 }
1655}
1656
Jackie Liua197f662019-11-08 08:09:12 -07001657static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001658{
Jackie Liua197f662019-11-08 08:09:12 -07001659 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001660 int ret;
1661
Jens Axboe2d283902019-12-04 11:08:05 -07001662 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001663 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001664 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001665 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001666 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001667 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001668 return true;
1669 }
1670
1671 return false;
1672}
1673
Jens Axboeab0b6452020-06-30 08:43:15 -06001674static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001675{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001676 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001677 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001678
1679 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001680 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001681 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1682 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001683 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001684
1685 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001686 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001687 wake_ev = io_link_cancel_timeout(link);
1688 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001689 return wake_ev;
1690}
1691
1692static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001693{
Jens Axboe2665abf2019-11-05 12:40:47 -07001694 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001695 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001696
Jens Axboeab0b6452020-06-30 08:43:15 -06001697 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1698 unsigned long flags;
1699
1700 spin_lock_irqsave(&ctx->completion_lock, flags);
1701 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001702 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001703 } else {
1704 wake_ev = __io_kill_linked_timeout(req);
1705 }
1706
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001707 if (wake_ev)
1708 io_cqring_ev_posted(ctx);
1709}
1710
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001711static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001712{
1713 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001714
Jens Axboe9e645e112019-05-10 16:07:28 -06001715 /*
1716 * The list should never be empty when we are called here. But could
1717 * potentially happen if the chain is messed up, check to be on the
1718 * safe side.
1719 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001720 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001721 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001722
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001723 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1724 list_del_init(&req->link_list);
1725 if (!list_empty(&nxt->link_list))
1726 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001727 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001728}
1729
1730/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001731 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001732 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001733static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001734{
Jens Axboe2665abf2019-11-05 12:40:47 -07001735 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001736
1737 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001738 struct io_kiocb *link = list_first_entry(&req->link_list,
1739 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001740
Pavel Begunkov44932332019-12-05 16:16:35 +03001741 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001742 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001743
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001744 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001745 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001746 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001747 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001748 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001749
1750 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001751 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001752}
1753
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001754static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001755{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001756 struct io_ring_ctx *ctx = req->ctx;
1757
1758 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1759 unsigned long flags;
1760
1761 spin_lock_irqsave(&ctx->completion_lock, flags);
1762 __io_fail_links(req);
1763 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1764 } else {
1765 __io_fail_links(req);
1766 }
1767
Jens Axboe9e645e112019-05-10 16:07:28 -06001768 io_cqring_ev_posted(ctx);
1769}
1770
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001771static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001772{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001773 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001774 if (req->flags & REQ_F_LINK_TIMEOUT)
1775 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001776
Jens Axboe9e645e112019-05-10 16:07:28 -06001777 /*
1778 * If LINK is set, we have dependent requests in this chain. If we
1779 * didn't fail this request, queue the first one up, moving any other
1780 * dependencies to the next request. In case of failure, fail the rest
1781 * of the chain.
1782 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001783 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1784 return io_req_link_next(req);
1785 io_fail_links(req);
1786 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001787}
Jens Axboe2665abf2019-11-05 12:40:47 -07001788
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001789static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1790{
1791 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1792 return NULL;
1793 return __io_req_find_next(req);
1794}
1795
Jens Axboefd7d6de2020-08-23 11:00:37 -06001796static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1797 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001798{
1799 struct task_struct *tsk = req->task;
1800 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001801 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001802
Jens Axboe6200b0a2020-09-13 14:38:30 -06001803 if (tsk->flags & PF_EXITING)
1804 return -ESRCH;
1805
Jens Axboec2c4c832020-07-01 15:37:11 -06001806 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001807 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1808 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1809 * processing task_work. There's no reliable way to tell if TWA_RESUME
1810 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001811 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001812 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001813 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001814 notify = TWA_SIGNAL;
1815
1816 ret = task_work_add(tsk, cb, notify);
1817 if (!ret)
1818 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001819
Jens Axboec2c4c832020-07-01 15:37:11 -06001820 return ret;
1821}
1822
Jens Axboec40f6372020-06-25 15:39:59 -06001823static void __io_req_task_cancel(struct io_kiocb *req, int error)
1824{
1825 struct io_ring_ctx *ctx = req->ctx;
1826
1827 spin_lock_irq(&ctx->completion_lock);
1828 io_cqring_fill_event(req, error);
1829 io_commit_cqring(ctx);
1830 spin_unlock_irq(&ctx->completion_lock);
1831
1832 io_cqring_ev_posted(ctx);
1833 req_set_fail_links(req);
1834 io_double_put_req(req);
1835}
1836
1837static void io_req_task_cancel(struct callback_head *cb)
1838{
1839 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001840 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001841
1842 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001843 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001844}
1845
1846static void __io_req_task_submit(struct io_kiocb *req)
1847{
1848 struct io_ring_ctx *ctx = req->ctx;
1849
Jens Axboec40f6372020-06-25 15:39:59 -06001850 if (!__io_sq_thread_acquire_mm(ctx)) {
1851 mutex_lock(&ctx->uring_lock);
1852 __io_queue_sqe(req, NULL, NULL);
1853 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001854 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001855 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001856 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001857}
1858
Jens Axboec40f6372020-06-25 15:39:59 -06001859static void io_req_task_submit(struct callback_head *cb)
1860{
1861 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001862 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001863
1864 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001865 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001866}
1867
1868static void io_req_task_queue(struct io_kiocb *req)
1869{
Jens Axboec40f6372020-06-25 15:39:59 -06001870 int ret;
1871
1872 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001873 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001874
Jens Axboefd7d6de2020-08-23 11:00:37 -06001875 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001876 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001877 struct task_struct *tsk;
1878
Jens Axboec40f6372020-06-25 15:39:59 -06001879 init_task_work(&req->task_work, io_req_task_cancel);
1880 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001881 task_work_add(tsk, &req->task_work, 0);
1882 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001883 }
Jens Axboec40f6372020-06-25 15:39:59 -06001884}
1885
Pavel Begunkovc3524382020-06-28 12:52:32 +03001886static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001887{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001888 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001889
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001890 if (nxt)
1891 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001892}
1893
Jens Axboe9e645e112019-05-10 16:07:28 -06001894static void io_free_req(struct io_kiocb *req)
1895{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001896 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001897 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001898}
1899
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001900struct req_batch {
1901 void *reqs[IO_IOPOLL_BATCH];
1902 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001903
1904 struct task_struct *task;
1905 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001906};
1907
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001908static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001909{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001910 rb->to_free = 0;
1911 rb->task_refs = 0;
1912 rb->task = NULL;
1913}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001914
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001915static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1916 struct req_batch *rb)
1917{
1918 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1919 percpu_ref_put_many(&ctx->refs, rb->to_free);
1920 rb->to_free = 0;
1921}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001922
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001923static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1924 struct req_batch *rb)
1925{
1926 if (rb->to_free)
1927 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001928 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001929 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001930 put_task_struct_many(rb->task, rb->task_refs);
1931 rb->task = NULL;
1932 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001933}
1934
1935static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1936{
1937 if (unlikely(io_is_fallback_req(req))) {
1938 io_free_req(req);
1939 return;
1940 }
1941 if (req->flags & REQ_F_LINK_HEAD)
1942 io_queue_next(req);
1943
Jens Axboee3bc8e92020-09-24 08:45:57 -06001944 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001945 if (rb->task) {
1946 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001947 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06001948 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001949 rb->task = req->task;
1950 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001951 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001952 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001953
Jens Axboe51a4cc12020-08-10 10:55:56 -06001954 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001955 rb->reqs[rb->to_free++] = req;
1956 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1957 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001958}
1959
Jens Axboeba816ad2019-09-28 11:36:45 -06001960/*
1961 * Drop reference to request, return next in chain (if there is one) if this
1962 * was the last reference to this request.
1963 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001964static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001965{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001966 struct io_kiocb *nxt = NULL;
1967
Jens Axboe2a44f462020-02-25 13:25:41 -07001968 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001969 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001970 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001971 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001972 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001973}
1974
Jens Axboe2b188cc2019-01-07 10:46:33 -07001975static void io_put_req(struct io_kiocb *req)
1976{
Jens Axboedef596e2019-01-09 08:59:42 -07001977 if (refcount_dec_and_test(&req->refs))
1978 io_free_req(req);
1979}
1980
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001981static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001982{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001983 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001984
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001985 /*
1986 * A ref is owned by io-wq in which context we're. So, if that's the
1987 * last one, it's safe to steal next work. False negatives are Ok,
1988 * it just will be re-punted async in io_put_work()
1989 */
1990 if (refcount_read(&req->refs) != 1)
1991 return NULL;
1992
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001993 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001994 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001995}
1996
Jens Axboe978db572019-11-14 22:39:04 -07001997/*
1998 * Must only be used if we don't need to care about links, usually from
1999 * within the completion handling itself.
2000 */
2001static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002002{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002003 /* drop both submit and complete references */
2004 if (refcount_sub_and_test(2, &req->refs))
2005 __io_free_req(req);
2006}
2007
Jens Axboe978db572019-11-14 22:39:04 -07002008static void io_double_put_req(struct io_kiocb *req)
2009{
2010 /* drop both submit and complete references */
2011 if (refcount_sub_and_test(2, &req->refs))
2012 io_free_req(req);
2013}
2014
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002015static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002016{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002017 struct io_rings *rings = ctx->rings;
2018
Jens Axboead3eb2c2019-12-18 17:12:20 -07002019 if (test_bit(0, &ctx->cq_check_overflow)) {
2020 /*
2021 * noflush == true is from the waitqueue handler, just ensure
2022 * we wake up the task, and the next invocation will flush the
2023 * entries. We cannot safely to it from here.
2024 */
2025 if (noflush && !list_empty(&ctx->cq_overflow_list))
2026 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002027
Jens Axboee6c8aa92020-09-28 13:10:13 -06002028 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002029 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002030
Jens Axboea3a0e432019-08-20 11:03:11 -06002031 /* See comment at the top of this file */
2032 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002033 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002034}
2035
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002036static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2037{
2038 struct io_rings *rings = ctx->rings;
2039
2040 /* make sure SQ entry isn't read before tail */
2041 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2042}
2043
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002044static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002045{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002046 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002047
Jens Axboebcda7ba2020-02-23 16:42:51 -07002048 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2049 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002050 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002051 kfree(kbuf);
2052 return cflags;
2053}
2054
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002055static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2056{
2057 struct io_buffer *kbuf;
2058
2059 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2060 return io_put_kbuf(req, kbuf);
2061}
2062
Jens Axboe4c6e2772020-07-01 11:29:10 -06002063static inline bool io_run_task_work(void)
2064{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002065 /*
2066 * Not safe to run on exiting task, and the task_work handling will
2067 * not add work to such a task.
2068 */
2069 if (unlikely(current->flags & PF_EXITING))
2070 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002071 if (current->task_works) {
2072 __set_current_state(TASK_RUNNING);
2073 task_work_run();
2074 return true;
2075 }
2076
2077 return false;
2078}
2079
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002080static void io_iopoll_queue(struct list_head *again)
2081{
2082 struct io_kiocb *req;
2083
2084 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002085 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2086 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002087 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002088 } while (!list_empty(again));
2089}
2090
Jens Axboedef596e2019-01-09 08:59:42 -07002091/*
2092 * Find and free completed poll iocbs
2093 */
2094static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2095 struct list_head *done)
2096{
Jens Axboe8237e042019-12-28 10:48:22 -07002097 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002098 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002099 LIST_HEAD(again);
2100
2101 /* order with ->result store in io_complete_rw_iopoll() */
2102 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002103
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002104 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002105 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002106 int cflags = 0;
2107
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002108 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002109 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002110 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002111 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002112 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002113 continue;
2114 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002115 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002116
Jens Axboebcda7ba2020-02-23 16:42:51 -07002117 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002118 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002119
2120 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002121 (*nr_events)++;
2122
Pavel Begunkovc3524382020-06-28 12:52:32 +03002123 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002124 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002125 }
Jens Axboedef596e2019-01-09 08:59:42 -07002126
Jens Axboe09bb8392019-03-13 12:39:28 -06002127 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002128 if (ctx->flags & IORING_SETUP_SQPOLL)
2129 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002130 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002131
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002132 if (!list_empty(&again))
2133 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002134}
2135
Jens Axboedef596e2019-01-09 08:59:42 -07002136static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2137 long min)
2138{
2139 struct io_kiocb *req, *tmp;
2140 LIST_HEAD(done);
2141 bool spin;
2142 int ret;
2143
2144 /*
2145 * Only spin for completions if we don't have multiple devices hanging
2146 * off our complete list, and we're under the requested amount.
2147 */
2148 spin = !ctx->poll_multi_file && *nr_events < min;
2149
2150 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002151 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002152 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002153
2154 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002155 * Move completed and retryable entries to our local lists.
2156 * If we find a request that requires polling, break out
2157 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002158 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002159 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002160 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002161 continue;
2162 }
2163 if (!list_empty(&done))
2164 break;
2165
2166 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2167 if (ret < 0)
2168 break;
2169
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002170 /* iopoll may have completed current req */
2171 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002172 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002173
Jens Axboedef596e2019-01-09 08:59:42 -07002174 if (ret && spin)
2175 spin = false;
2176 ret = 0;
2177 }
2178
2179 if (!list_empty(&done))
2180 io_iopoll_complete(ctx, nr_events, &done);
2181
2182 return ret;
2183}
2184
2185/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002186 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002187 * non-spinning poll check - we'll still enter the driver poll loop, but only
2188 * as a non-spinning completion check.
2189 */
2190static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2191 long min)
2192{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002193 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002194 int ret;
2195
2196 ret = io_do_iopoll(ctx, nr_events, min);
2197 if (ret < 0)
2198 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002199 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002200 return 0;
2201 }
2202
2203 return 1;
2204}
2205
2206/*
2207 * We can't just wait for polled events to come to us, we have to actively
2208 * find and complete them.
2209 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002210static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002211{
2212 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2213 return;
2214
2215 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002216 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002217 unsigned int nr_events = 0;
2218
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002219 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002220
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002221 /* let it sleep and repeat later if can't complete a request */
2222 if (nr_events == 0)
2223 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002224 /*
2225 * Ensure we allow local-to-the-cpu processing to take place,
2226 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002227 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002228 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002229 if (need_resched()) {
2230 mutex_unlock(&ctx->uring_lock);
2231 cond_resched();
2232 mutex_lock(&ctx->uring_lock);
2233 }
Jens Axboedef596e2019-01-09 08:59:42 -07002234 }
2235 mutex_unlock(&ctx->uring_lock);
2236}
2237
Pavel Begunkov7668b922020-07-07 16:36:21 +03002238static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002239{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002240 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002241 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002242
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002243 /*
2244 * We disallow the app entering submit/complete with polling, but we
2245 * still need to lock the ring to prevent racing with polled issue
2246 * that got punted to a workqueue.
2247 */
2248 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002249 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002250 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002251 * Don't enter poll loop if we already have events pending.
2252 * If we do, we can potentially be spinning for commands that
2253 * already triggered a CQE (eg in error).
2254 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002255 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002256 break;
2257
2258 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002259 * If a submit got punted to a workqueue, we can have the
2260 * application entering polling for a command before it gets
2261 * issued. That app will hold the uring_lock for the duration
2262 * of the poll right here, so we need to take a breather every
2263 * now and then to ensure that the issue has a chance to add
2264 * the poll to the issued list. Otherwise we can spin here
2265 * forever, while the workqueue is stuck trying to acquire the
2266 * very same mutex.
2267 */
2268 if (!(++iters & 7)) {
2269 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002270 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002271 mutex_lock(&ctx->uring_lock);
2272 }
2273
Pavel Begunkov7668b922020-07-07 16:36:21 +03002274 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002275 if (ret <= 0)
2276 break;
2277 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002278 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002279
Jens Axboe500f9fb2019-08-19 12:15:59 -06002280 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002281 return ret;
2282}
2283
Jens Axboe491381ce2019-10-17 09:20:46 -06002284static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002285{
Jens Axboe491381ce2019-10-17 09:20:46 -06002286 /*
2287 * Tell lockdep we inherited freeze protection from submission
2288 * thread.
2289 */
2290 if (req->flags & REQ_F_ISREG) {
2291 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292
Jens Axboe491381ce2019-10-17 09:20:46 -06002293 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002294 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002295 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002296}
2297
Jens Axboea1d7c392020-06-22 11:09:46 -06002298static void io_complete_rw_common(struct kiocb *kiocb, long res,
2299 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002300{
Jens Axboe9adbd452019-12-20 08:45:55 -07002301 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002302 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303
Jens Axboe491381ce2019-10-17 09:20:46 -06002304 if (kiocb->ki_flags & IOCB_WRITE)
2305 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002306
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002307 if (res != req->result)
2308 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002309 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002310 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002311 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002312}
2313
Jens Axboeb63534c2020-06-04 11:28:00 -06002314#ifdef CONFIG_BLOCK
2315static bool io_resubmit_prep(struct io_kiocb *req, int error)
2316{
2317 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2318 ssize_t ret = -ECANCELED;
2319 struct iov_iter iter;
2320 int rw;
2321
2322 if (error) {
2323 ret = error;
2324 goto end_req;
2325 }
2326
2327 switch (req->opcode) {
2328 case IORING_OP_READV:
2329 case IORING_OP_READ_FIXED:
2330 case IORING_OP_READ:
2331 rw = READ;
2332 break;
2333 case IORING_OP_WRITEV:
2334 case IORING_OP_WRITE_FIXED:
2335 case IORING_OP_WRITE:
2336 rw = WRITE;
2337 break;
2338 default:
2339 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2340 req->opcode);
2341 goto end_req;
2342 }
2343
Jens Axboe8f3d7492020-09-14 09:28:14 -06002344 if (!req->io) {
2345 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2346 if (ret < 0)
2347 goto end_req;
2348 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2349 if (!ret)
2350 return true;
2351 kfree(iovec);
2352 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002353 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002354 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002355end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002356 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002357 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002358 return false;
2359}
Jens Axboeb63534c2020-06-04 11:28:00 -06002360#endif
2361
2362static bool io_rw_reissue(struct io_kiocb *req, long res)
2363{
2364#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002365 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002366 int ret;
2367
Jens Axboe355afae2020-09-02 09:30:31 -06002368 if (!S_ISBLK(mode) && !S_ISREG(mode))
2369 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002370 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2371 return false;
2372
Jens Axboefdee9462020-08-27 16:46:24 -06002373 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002374
Jens Axboefdee9462020-08-27 16:46:24 -06002375 if (io_resubmit_prep(req, ret)) {
2376 refcount_inc(&req->refs);
2377 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002378 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002379 }
2380
Jens Axboeb63534c2020-06-04 11:28:00 -06002381#endif
2382 return false;
2383}
2384
Jens Axboea1d7c392020-06-22 11:09:46 -06002385static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2386 struct io_comp_state *cs)
2387{
2388 if (!io_rw_reissue(req, res))
2389 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002390}
2391
2392static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2393{
Jens Axboe9adbd452019-12-20 08:45:55 -07002394 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002395
Jens Axboea1d7c392020-06-22 11:09:46 -06002396 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002397}
2398
Jens Axboedef596e2019-01-09 08:59:42 -07002399static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2400{
Jens Axboe9adbd452019-12-20 08:45:55 -07002401 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002402
Jens Axboe491381ce2019-10-17 09:20:46 -06002403 if (kiocb->ki_flags & IOCB_WRITE)
2404 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002405
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002406 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002407 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002408
2409 WRITE_ONCE(req->result, res);
2410 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002411 smp_wmb();
2412 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002413}
2414
2415/*
2416 * After the iocb has been issued, it's safe to be found on the poll list.
2417 * Adding the kiocb to the list AFTER submission ensures that we don't
2418 * find it from a io_iopoll_getevents() thread before the issuer is done
2419 * accessing the kiocb cookie.
2420 */
2421static void io_iopoll_req_issued(struct io_kiocb *req)
2422{
2423 struct io_ring_ctx *ctx = req->ctx;
2424
2425 /*
2426 * Track whether we have multiple files in our lists. This will impact
2427 * how we do polling eventually, not spinning if we're on potentially
2428 * different devices.
2429 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002430 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002431 ctx->poll_multi_file = false;
2432 } else if (!ctx->poll_multi_file) {
2433 struct io_kiocb *list_req;
2434
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002435 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002436 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002437 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002438 ctx->poll_multi_file = true;
2439 }
2440
2441 /*
2442 * For fast devices, IO may have already completed. If it has, add
2443 * it to the front so we find it first.
2444 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002445 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002446 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002447 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002448 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002449
2450 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2451 wq_has_sleeper(&ctx->sqo_wait))
2452 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002453}
2454
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002455static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002456{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002457 if (state->has_refs)
2458 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002459 state->file = NULL;
2460}
2461
2462static inline void io_state_file_put(struct io_submit_state *state)
2463{
2464 if (state->file)
2465 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002466}
2467
2468/*
2469 * Get as many references to a file as we have IOs left in this submission,
2470 * assuming most submissions are for one file, or at least that each file
2471 * has more than one submission.
2472 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002473static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002474{
2475 if (!state)
2476 return fget(fd);
2477
2478 if (state->file) {
2479 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002480 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002481 state->ios_left--;
2482 return state->file;
2483 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002484 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002485 }
2486 state->file = fget_many(fd, state->ios_left);
2487 if (!state->file)
2488 return NULL;
2489
2490 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002491 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002492 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002493 return state->file;
2494}
2495
Jens Axboe4503b762020-06-01 10:00:27 -06002496static bool io_bdev_nowait(struct block_device *bdev)
2497{
2498#ifdef CONFIG_BLOCK
2499 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2500#else
2501 return true;
2502#endif
2503}
2504
Jens Axboe2b188cc2019-01-07 10:46:33 -07002505/*
2506 * If we tracked the file through the SCM inflight mechanism, we could support
2507 * any file. For now, just ensure that anything potentially problematic is done
2508 * inline.
2509 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002510static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002511{
2512 umode_t mode = file_inode(file)->i_mode;
2513
Jens Axboe4503b762020-06-01 10:00:27 -06002514 if (S_ISBLK(mode)) {
2515 if (io_bdev_nowait(file->f_inode->i_bdev))
2516 return true;
2517 return false;
2518 }
2519 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002521 if (S_ISREG(mode)) {
2522 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2523 file->f_op != &io_uring_fops)
2524 return true;
2525 return false;
2526 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002527
Jens Axboec5b85622020-06-09 19:23:05 -06002528 /* any ->read/write should understand O_NONBLOCK */
2529 if (file->f_flags & O_NONBLOCK)
2530 return true;
2531
Jens Axboeaf197f52020-04-28 13:15:06 -06002532 if (!(file->f_mode & FMODE_NOWAIT))
2533 return false;
2534
2535 if (rw == READ)
2536 return file->f_op->read_iter != NULL;
2537
2538 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002539}
2540
Jens Axboe3529d8c2019-12-19 18:24:38 -07002541static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2542 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002543{
Jens Axboedef596e2019-01-09 08:59:42 -07002544 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002545 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002546 unsigned ioprio;
2547 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002548
Jens Axboe491381ce2019-10-17 09:20:46 -06002549 if (S_ISREG(file_inode(req->file)->i_mode))
2550 req->flags |= REQ_F_ISREG;
2551
Jens Axboe2b188cc2019-01-07 10:46:33 -07002552 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002553 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2554 req->flags |= REQ_F_CUR_POS;
2555 kiocb->ki_pos = req->file->f_pos;
2556 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002557 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002558 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2559 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2560 if (unlikely(ret))
2561 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002562
2563 ioprio = READ_ONCE(sqe->ioprio);
2564 if (ioprio) {
2565 ret = ioprio_check_cap(ioprio);
2566 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002567 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002568
2569 kiocb->ki_ioprio = ioprio;
2570 } else
2571 kiocb->ki_ioprio = get_current_ioprio();
2572
Stefan Bühler8449eed2019-04-27 20:34:19 +02002573 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002574 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002575 req->flags |= REQ_F_NOWAIT;
2576
2577 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002578 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002579
Jens Axboedef596e2019-01-09 08:59:42 -07002580 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002581 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2582 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002583 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584
Jens Axboedef596e2019-01-09 08:59:42 -07002585 kiocb->ki_flags |= IOCB_HIPRI;
2586 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002587 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002588 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002589 if (kiocb->ki_flags & IOCB_HIPRI)
2590 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002591 kiocb->ki_complete = io_complete_rw;
2592 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002593
Jens Axboe3529d8c2019-12-19 18:24:38 -07002594 req->rw.addr = READ_ONCE(sqe->addr);
2595 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002596 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002597 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002598}
2599
2600static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2601{
2602 switch (ret) {
2603 case -EIOCBQUEUED:
2604 break;
2605 case -ERESTARTSYS:
2606 case -ERESTARTNOINTR:
2607 case -ERESTARTNOHAND:
2608 case -ERESTART_RESTARTBLOCK:
2609 /*
2610 * We can't just restart the syscall, since previously
2611 * submitted sqes may already be in progress. Just fail this
2612 * IO with EINTR.
2613 */
2614 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002615 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002616 default:
2617 kiocb->ki_complete(kiocb, ret, 0);
2618 }
2619}
2620
Jens Axboea1d7c392020-06-22 11:09:46 -06002621static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2622 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002623{
Jens Axboeba042912019-12-25 16:33:42 -07002624 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2625
Jens Axboe227c0c92020-08-13 11:51:40 -06002626 /* add previously done IO, if any */
2627 if (req->io && req->io->rw.bytes_done > 0) {
2628 if (ret < 0)
2629 ret = req->io->rw.bytes_done;
2630 else
2631 ret += req->io->rw.bytes_done;
2632 }
2633
Jens Axboeba042912019-12-25 16:33:42 -07002634 if (req->flags & REQ_F_CUR_POS)
2635 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002636 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002637 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002638 else
2639 io_rw_done(kiocb, ret);
2640}
2641
Jens Axboe9adbd452019-12-20 08:45:55 -07002642static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002643 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002644{
Jens Axboe9adbd452019-12-20 08:45:55 -07002645 struct io_ring_ctx *ctx = req->ctx;
2646 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002647 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002648 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002649 size_t offset;
2650 u64 buf_addr;
2651
2652 /* attempt to use fixed buffers without having provided iovecs */
2653 if (unlikely(!ctx->user_bufs))
2654 return -EFAULT;
2655
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002656 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002657 if (unlikely(buf_index >= ctx->nr_user_bufs))
2658 return -EFAULT;
2659
2660 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2661 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002662 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002663
2664 /* overflow */
2665 if (buf_addr + len < buf_addr)
2666 return -EFAULT;
2667 /* not inside the mapped region */
2668 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2669 return -EFAULT;
2670
2671 /*
2672 * May not be a start of buffer, set size appropriately
2673 * and advance us to the beginning.
2674 */
2675 offset = buf_addr - imu->ubuf;
2676 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002677
2678 if (offset) {
2679 /*
2680 * Don't use iov_iter_advance() here, as it's really slow for
2681 * using the latter parts of a big fixed buffer - it iterates
2682 * over each segment manually. We can cheat a bit here, because
2683 * we know that:
2684 *
2685 * 1) it's a BVEC iter, we set it up
2686 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2687 * first and last bvec
2688 *
2689 * So just find our index, and adjust the iterator afterwards.
2690 * If the offset is within the first bvec (or the whole first
2691 * bvec, just use iov_iter_advance(). This makes it easier
2692 * since we can just skip the first segment, which may not
2693 * be PAGE_SIZE aligned.
2694 */
2695 const struct bio_vec *bvec = imu->bvec;
2696
2697 if (offset <= bvec->bv_len) {
2698 iov_iter_advance(iter, offset);
2699 } else {
2700 unsigned long seg_skip;
2701
2702 /* skip first vec */
2703 offset -= bvec->bv_len;
2704 seg_skip = 1 + (offset >> PAGE_SHIFT);
2705
2706 iter->bvec = bvec + seg_skip;
2707 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002708 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002709 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002710 }
2711 }
2712
Jens Axboe5e559562019-11-13 16:12:46 -07002713 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002714}
2715
Jens Axboebcda7ba2020-02-23 16:42:51 -07002716static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2717{
2718 if (needs_lock)
2719 mutex_unlock(&ctx->uring_lock);
2720}
2721
2722static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2723{
2724 /*
2725 * "Normal" inline submissions always hold the uring_lock, since we
2726 * grab it from the system call. Same is true for the SQPOLL offload.
2727 * The only exception is when we've detached the request and issue it
2728 * from an async worker thread, grab the lock for that case.
2729 */
2730 if (needs_lock)
2731 mutex_lock(&ctx->uring_lock);
2732}
2733
2734static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2735 int bgid, struct io_buffer *kbuf,
2736 bool needs_lock)
2737{
2738 struct io_buffer *head;
2739
2740 if (req->flags & REQ_F_BUFFER_SELECTED)
2741 return kbuf;
2742
2743 io_ring_submit_lock(req->ctx, needs_lock);
2744
2745 lockdep_assert_held(&req->ctx->uring_lock);
2746
2747 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2748 if (head) {
2749 if (!list_empty(&head->list)) {
2750 kbuf = list_last_entry(&head->list, struct io_buffer,
2751 list);
2752 list_del(&kbuf->list);
2753 } else {
2754 kbuf = head;
2755 idr_remove(&req->ctx->io_buffer_idr, bgid);
2756 }
2757 if (*len > kbuf->len)
2758 *len = kbuf->len;
2759 } else {
2760 kbuf = ERR_PTR(-ENOBUFS);
2761 }
2762
2763 io_ring_submit_unlock(req->ctx, needs_lock);
2764
2765 return kbuf;
2766}
2767
Jens Axboe4d954c22020-02-27 07:31:19 -07002768static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2769 bool needs_lock)
2770{
2771 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002772 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002773
2774 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002775 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002776 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2777 if (IS_ERR(kbuf))
2778 return kbuf;
2779 req->rw.addr = (u64) (unsigned long) kbuf;
2780 req->flags |= REQ_F_BUFFER_SELECTED;
2781 return u64_to_user_ptr(kbuf->addr);
2782}
2783
2784#ifdef CONFIG_COMPAT
2785static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2786 bool needs_lock)
2787{
2788 struct compat_iovec __user *uiov;
2789 compat_ssize_t clen;
2790 void __user *buf;
2791 ssize_t len;
2792
2793 uiov = u64_to_user_ptr(req->rw.addr);
2794 if (!access_ok(uiov, sizeof(*uiov)))
2795 return -EFAULT;
2796 if (__get_user(clen, &uiov->iov_len))
2797 return -EFAULT;
2798 if (clen < 0)
2799 return -EINVAL;
2800
2801 len = clen;
2802 buf = io_rw_buffer_select(req, &len, needs_lock);
2803 if (IS_ERR(buf))
2804 return PTR_ERR(buf);
2805 iov[0].iov_base = buf;
2806 iov[0].iov_len = (compat_size_t) len;
2807 return 0;
2808}
2809#endif
2810
2811static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2812 bool needs_lock)
2813{
2814 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2815 void __user *buf;
2816 ssize_t len;
2817
2818 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2819 return -EFAULT;
2820
2821 len = iov[0].iov_len;
2822 if (len < 0)
2823 return -EINVAL;
2824 buf = io_rw_buffer_select(req, &len, needs_lock);
2825 if (IS_ERR(buf))
2826 return PTR_ERR(buf);
2827 iov[0].iov_base = buf;
2828 iov[0].iov_len = len;
2829 return 0;
2830}
2831
2832static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2833 bool needs_lock)
2834{
Jens Axboedddb3e22020-06-04 11:27:01 -06002835 if (req->flags & REQ_F_BUFFER_SELECTED) {
2836 struct io_buffer *kbuf;
2837
2838 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2839 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2840 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002841 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002842 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002843 if (!req->rw.len)
2844 return 0;
2845 else if (req->rw.len > 1)
2846 return -EINVAL;
2847
2848#ifdef CONFIG_COMPAT
2849 if (req->ctx->compat)
2850 return io_compat_import(req, iov, needs_lock);
2851#endif
2852
2853 return __io_iov_buffer_select(req, iov, needs_lock);
2854}
2855
Jens Axboe8452fd02020-08-18 13:58:33 -07002856static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2857 struct iovec **iovec, struct iov_iter *iter,
2858 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002859{
Jens Axboe9adbd452019-12-20 08:45:55 -07002860 void __user *buf = u64_to_user_ptr(req->rw.addr);
2861 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002862 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002863 u8 opcode;
2864
Jens Axboed625c6e2019-12-17 19:53:05 -07002865 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002866 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002867 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002868 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002869 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002870
Jens Axboebcda7ba2020-02-23 16:42:51 -07002871 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002872 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002873 return -EINVAL;
2874
Jens Axboe3a6820f2019-12-22 15:19:35 -07002875 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002876 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002877 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002878 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002879 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002880 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002881 }
2882
Jens Axboe3a6820f2019-12-22 15:19:35 -07002883 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2884 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002885 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002886 }
2887
Jens Axboe4d954c22020-02-27 07:31:19 -07002888 if (req->flags & REQ_F_BUFFER_SELECT) {
2889 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002890 if (!ret) {
2891 ret = (*iovec)->iov_len;
2892 iov_iter_init(iter, rw, *iovec, 1, ret);
2893 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002894 *iovec = NULL;
2895 return ret;
2896 }
2897
Jens Axboe2b188cc2019-01-07 10:46:33 -07002898#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002899 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002900 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2901 iovec, iter);
2902#endif
2903
2904 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2905}
2906
Jens Axboe8452fd02020-08-18 13:58:33 -07002907static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2908 struct iovec **iovec, struct iov_iter *iter,
2909 bool needs_lock)
2910{
2911 if (!req->io)
2912 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2913 *iovec = NULL;
2914 return iov_iter_count(&req->io->rw.iter);
2915}
2916
Jens Axboe0fef9482020-08-26 10:36:20 -06002917static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2918{
2919 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2920}
2921
Jens Axboe32960612019-09-23 11:05:34 -06002922/*
2923 * For files that don't have ->read_iter() and ->write_iter(), handle them
2924 * by looping over ->read() or ->write() manually.
2925 */
2926static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2927 struct iov_iter *iter)
2928{
2929 ssize_t ret = 0;
2930
2931 /*
2932 * Don't support polled IO through this interface, and we can't
2933 * support non-blocking either. For the latter, this just causes
2934 * the kiocb to be handled from an async context.
2935 */
2936 if (kiocb->ki_flags & IOCB_HIPRI)
2937 return -EOPNOTSUPP;
2938 if (kiocb->ki_flags & IOCB_NOWAIT)
2939 return -EAGAIN;
2940
2941 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002942 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002943 ssize_t nr;
2944
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002945 if (!iov_iter_is_bvec(iter)) {
2946 iovec = iov_iter_iovec(iter);
2947 } else {
2948 /* fixed buffers import bvec */
2949 iovec.iov_base = kmap(iter->bvec->bv_page)
2950 + iter->iov_offset;
2951 iovec.iov_len = min(iter->count,
2952 iter->bvec->bv_len - iter->iov_offset);
2953 }
2954
Jens Axboe32960612019-09-23 11:05:34 -06002955 if (rw == READ) {
2956 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002957 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002958 } else {
2959 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002960 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002961 }
2962
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002963 if (iov_iter_is_bvec(iter))
2964 kunmap(iter->bvec->bv_page);
2965
Jens Axboe32960612019-09-23 11:05:34 -06002966 if (nr < 0) {
2967 if (!ret)
2968 ret = nr;
2969 break;
2970 }
2971 ret += nr;
2972 if (nr != iovec.iov_len)
2973 break;
2974 iov_iter_advance(iter, nr);
2975 }
2976
2977 return ret;
2978}
2979
Jens Axboeff6165b2020-08-13 09:47:43 -06002980static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2981 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07002982{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002983 struct io_async_rw *rw = &req->io->rw;
2984
Jens Axboeff6165b2020-08-13 09:47:43 -06002985 memcpy(&rw->iter, iter, sizeof(*iter));
2986 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06002987 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06002988 /* can only be fixed buffers, no need to do anything */
2989 if (iter->type == ITER_BVEC)
2990 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002991 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06002992 unsigned iov_off = 0;
2993
2994 rw->iter.iov = rw->fast_iov;
2995 if (iter->iov != fast_iov) {
2996 iov_off = iter->iov - fast_iov;
2997 rw->iter.iov += iov_off;
2998 }
2999 if (rw->fast_iov != fast_iov)
3000 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003001 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003002 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06003003 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003004 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003005 }
3006}
3007
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003008static inline int __io_alloc_async_ctx(struct io_kiocb *req)
3009{
3010 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
3011 return req->io == NULL;
3012}
3013
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003014static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003015{
Jens Axboed3656342019-12-18 09:50:26 -07003016 if (!io_op_defs[req->opcode].async_ctx)
3017 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003018
3019 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003020}
3021
Jens Axboeff6165b2020-08-13 09:47:43 -06003022static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3023 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003024 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003025{
Jens Axboe227c0c92020-08-13 11:51:40 -06003026 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003027 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003028 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003029 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003030 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003031
Jens Axboeff6165b2020-08-13 09:47:43 -06003032 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003033 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003034 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003035}
3036
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003037static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3038 bool force_nonblock)
3039{
Jens Axboeff6165b2020-08-13 09:47:43 -06003040 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06003041 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003042 ssize_t ret;
3043
Jens Axboec183edf2020-09-04 22:36:52 -06003044 iorw->iter.iov = iov = iorw->fast_iov;
3045 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003046 if (unlikely(ret < 0))
3047 return ret;
3048
Jens Axboec183edf2020-09-04 22:36:52 -06003049 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003050 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003051 return 0;
3052}
3053
Jens Axboe3529d8c2019-12-19 18:24:38 -07003054static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3055 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003056{
3057 ssize_t ret;
3058
Jens Axboe3529d8c2019-12-19 18:24:38 -07003059 ret = io_prep_rw(req, sqe, force_nonblock);
3060 if (ret)
3061 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003062
Jens Axboe3529d8c2019-12-19 18:24:38 -07003063 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3064 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003065
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003066 /* either don't need iovec imported or already have it */
3067 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003068 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003069 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003070}
3071
Jens Axboec1dd91d2020-08-03 16:43:59 -06003072/*
3073 * This is our waitqueue callback handler, registered through lock_page_async()
3074 * when we initially tried to do the IO with the iocb armed our waitqueue.
3075 * This gets called when the page is unlocked, and we generally expect that to
3076 * happen when the page IO is completed and the page is now uptodate. This will
3077 * queue a task_work based retry of the operation, attempting to copy the data
3078 * again. If the latter fails because the page was NOT uptodate, then we will
3079 * do a thread based blocking retry of the operation. That's the unexpected
3080 * slow path.
3081 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003082static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3083 int sync, void *arg)
3084{
3085 struct wait_page_queue *wpq;
3086 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003087 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003088 int ret;
3089
3090 wpq = container_of(wait, struct wait_page_queue, wait);
3091
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003092 if (!wake_page_match(wpq, key))
3093 return 0;
3094
Hao Xuc8d317a2020-09-29 20:00:45 +08003095 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003096 list_del_init(&wait->entry);
3097
Pavel Begunkove7375122020-07-12 20:42:04 +03003098 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003099 percpu_ref_get(&req->ctx->refs);
3100
Jens Axboebcf5a062020-05-22 09:24:42 -06003101 /* submit ref gets dropped, acquire a new one */
3102 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003103 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003104 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003105 struct task_struct *tsk;
3106
Jens Axboebcf5a062020-05-22 09:24:42 -06003107 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003108 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003109 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003110 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003111 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003112 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003113 return 1;
3114}
3115
Jens Axboec1dd91d2020-08-03 16:43:59 -06003116/*
3117 * This controls whether a given IO request should be armed for async page
3118 * based retry. If we return false here, the request is handed to the async
3119 * worker threads for retry. If we're doing buffered reads on a regular file,
3120 * we prepare a private wait_page_queue entry and retry the operation. This
3121 * will either succeed because the page is now uptodate and unlocked, or it
3122 * will register a callback when the page is unlocked at IO completion. Through
3123 * that callback, io_uring uses task_work to setup a retry of the operation.
3124 * That retry will attempt the buffered read again. The retry will generally
3125 * succeed, or in rare cases where it fails, we then fall back to using the
3126 * async worker threads for a blocking retry.
3127 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003128static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003129{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003130 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003131 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003132
3133 /* never retry for NOWAIT, we just complete with -EAGAIN */
3134 if (req->flags & REQ_F_NOWAIT)
3135 return false;
3136
Jens Axboe227c0c92020-08-13 11:51:40 -06003137 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003138 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003139 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003140
Jens Axboebcf5a062020-05-22 09:24:42 -06003141 /*
3142 * just use poll if we can, and don't attempt if the fs doesn't
3143 * support callback based unlocks
3144 */
3145 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3146 return false;
3147
Jens Axboe3b2a4432020-08-16 10:58:43 -07003148 wait->wait.func = io_async_buf_func;
3149 wait->wait.private = req;
3150 wait->wait.flags = 0;
3151 INIT_LIST_HEAD(&wait->wait.entry);
3152 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003153 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003154 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003155 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003156}
3157
3158static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3159{
3160 if (req->file->f_op->read_iter)
3161 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003162 else if (req->file->f_op->read)
3163 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3164 else
3165 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003166}
3167
Jens Axboea1d7c392020-06-22 11:09:46 -06003168static int io_read(struct io_kiocb *req, bool force_nonblock,
3169 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003170{
3171 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003172 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003173 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003174 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003175 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003176 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003177
Jens Axboeff6165b2020-08-13 09:47:43 -06003178 if (req->io)
3179 iter = &req->io->rw.iter;
3180
3181 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003182 if (ret < 0)
3183 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003184 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003185 io_size = ret;
3186 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003187 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003188
Jens Axboefd6c2e42019-12-18 12:19:41 -07003189 /* Ensure we clear previously set non-block flag */
3190 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003191 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003192
Pavel Begunkov24c74672020-06-21 13:09:51 +03003193 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003194 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3195 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003196 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003197
Jens Axboe0fef9482020-08-26 10:36:20 -06003198 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003199 if (unlikely(ret))
3200 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003201
Jens Axboe227c0c92020-08-13 11:51:40 -06003202 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003203
Jens Axboe227c0c92020-08-13 11:51:40 -06003204 if (!ret) {
3205 goto done;
3206 } else if (ret == -EIOCBQUEUED) {
3207 ret = 0;
3208 goto out_free;
3209 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003210 /* IOPOLL retry should happen for io-wq threads */
3211 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003212 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003213 /* no retry on NONBLOCK marked file */
3214 if (req->file->f_flags & O_NONBLOCK)
3215 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003216 /* some cases will consume bytes even on error returns */
3217 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003218 ret = 0;
3219 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003220 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003221 /* make sure -ERESTARTSYS -> -EINTR is done */
3222 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003223 }
3224
3225 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003226 if (!iov_iter_count(iter) || !force_nonblock ||
3227 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003228 goto done;
3229
3230 io_size -= ret;
3231copy_iov:
3232 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3233 if (ret2) {
3234 ret = ret2;
3235 goto out_free;
3236 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003237 if (no_async)
3238 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003239 /* it's copied and will be cleaned with ->io */
3240 iovec = NULL;
3241 /* now use our persistent iterator, if we aren't already */
3242 iter = &req->io->rw.iter;
3243retry:
3244 req->io->rw.bytes_done += ret;
3245 /* if we can retry, do so with the callbacks armed */
3246 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003247 kiocb->ki_flags &= ~IOCB_WAITQ;
3248 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003249 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003250
3251 /*
3252 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3253 * get -EIOCBQUEUED, then we'll get a notification when the desired
3254 * page gets unlocked. We can also get a partial read here, and if we
3255 * do, then just retry at the new offset.
3256 */
3257 ret = io_iter_do_read(req, iter);
3258 if (ret == -EIOCBQUEUED) {
3259 ret = 0;
3260 goto out_free;
3261 } else if (ret > 0 && ret < io_size) {
3262 /* we got some bytes, but not all. retry. */
3263 goto retry;
3264 }
3265done:
3266 kiocb_done(kiocb, ret, cs);
3267 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003268out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003269 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003270 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003271 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003272 return ret;
3273}
3274
Jens Axboe3529d8c2019-12-19 18:24:38 -07003275static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3276 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003277{
3278 ssize_t ret;
3279
Jens Axboe3529d8c2019-12-19 18:24:38 -07003280 ret = io_prep_rw(req, sqe, force_nonblock);
3281 if (ret)
3282 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003283
Jens Axboe3529d8c2019-12-19 18:24:38 -07003284 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3285 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003286
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003287 /* either don't need iovec imported or already have it */
3288 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003289 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003290 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003291}
3292
Jens Axboea1d7c392020-06-22 11:09:46 -06003293static int io_write(struct io_kiocb *req, bool force_nonblock,
3294 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003295{
3296 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003297 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003298 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003299 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003300 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003301
Jens Axboeff6165b2020-08-13 09:47:43 -06003302 if (req->io)
3303 iter = &req->io->rw.iter;
3304
3305 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003306 if (ret < 0)
3307 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003308 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003309 io_size = ret;
3310 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003311
Jens Axboefd6c2e42019-12-18 12:19:41 -07003312 /* Ensure we clear previously set non-block flag */
3313 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003314 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003315
Pavel Begunkov24c74672020-06-21 13:09:51 +03003316 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003317 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003318 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003319
Jens Axboe10d59342019-12-09 20:16:22 -07003320 /* file path doesn't support NOWAIT for non-direct_IO */
3321 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3322 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003323 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003324
Jens Axboe0fef9482020-08-26 10:36:20 -06003325 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003326 if (unlikely(ret))
3327 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003328
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003329 /*
3330 * Open-code file_start_write here to grab freeze protection,
3331 * which will be released by another thread in
3332 * io_complete_rw(). Fool lockdep by telling it the lock got
3333 * released so that it doesn't complain about the held lock when
3334 * we return to userspace.
3335 */
3336 if (req->flags & REQ_F_ISREG) {
3337 __sb_start_write(file_inode(req->file)->i_sb,
3338 SB_FREEZE_WRITE, true);
3339 __sb_writers_release(file_inode(req->file)->i_sb,
3340 SB_FREEZE_WRITE);
3341 }
3342 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003343
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003344 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003345 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003346 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003347 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003348 else
3349 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003350
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003351 /*
3352 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3353 * retry them without IOCB_NOWAIT.
3354 */
3355 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3356 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003357 /* no retry on NONBLOCK marked file */
3358 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3359 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003360 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003361 /* IOPOLL retry should happen for io-wq threads */
3362 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3363 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003364done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003365 kiocb_done(kiocb, ret2, cs);
3366 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003367copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003368 /* some cases will consume bytes even on error returns */
3369 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003370 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003371 if (!ret)
3372 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003373 }
Jens Axboe31b51512019-01-18 22:56:34 -07003374out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003375 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003376 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003377 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003378 return ret;
3379}
3380
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003381static int __io_splice_prep(struct io_kiocb *req,
3382 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003383{
3384 struct io_splice* sp = &req->splice;
3385 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3386 int ret;
3387
3388 if (req->flags & REQ_F_NEED_CLEANUP)
3389 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003390 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3391 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003392
3393 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003394 sp->len = READ_ONCE(sqe->len);
3395 sp->flags = READ_ONCE(sqe->splice_flags);
3396
3397 if (unlikely(sp->flags & ~valid_flags))
3398 return -EINVAL;
3399
3400 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3401 (sp->flags & SPLICE_F_FD_IN_FIXED));
3402 if (ret)
3403 return ret;
3404 req->flags |= REQ_F_NEED_CLEANUP;
3405
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003406 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3407 /*
3408 * Splice operation will be punted aync, and here need to
3409 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3410 */
3411 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003412 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003413 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003414
3415 return 0;
3416}
3417
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003418static int io_tee_prep(struct io_kiocb *req,
3419 const struct io_uring_sqe *sqe)
3420{
3421 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3422 return -EINVAL;
3423 return __io_splice_prep(req, sqe);
3424}
3425
3426static int io_tee(struct io_kiocb *req, bool force_nonblock)
3427{
3428 struct io_splice *sp = &req->splice;
3429 struct file *in = sp->file_in;
3430 struct file *out = sp->file_out;
3431 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3432 long ret = 0;
3433
3434 if (force_nonblock)
3435 return -EAGAIN;
3436 if (sp->len)
3437 ret = do_tee(in, out, sp->len, flags);
3438
3439 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3440 req->flags &= ~REQ_F_NEED_CLEANUP;
3441
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003442 if (ret != sp->len)
3443 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003444 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003445 return 0;
3446}
3447
3448static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3449{
3450 struct io_splice* sp = &req->splice;
3451
3452 sp->off_in = READ_ONCE(sqe->splice_off_in);
3453 sp->off_out = READ_ONCE(sqe->off);
3454 return __io_splice_prep(req, sqe);
3455}
3456
Pavel Begunkov014db002020-03-03 21:33:12 +03003457static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003458{
3459 struct io_splice *sp = &req->splice;
3460 struct file *in = sp->file_in;
3461 struct file *out = sp->file_out;
3462 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3463 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003464 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003465
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003466 if (force_nonblock)
3467 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003468
3469 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3470 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003471
Jens Axboe948a7742020-05-17 14:21:38 -06003472 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003473 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003474
3475 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3476 req->flags &= ~REQ_F_NEED_CLEANUP;
3477
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003478 if (ret != sp->len)
3479 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003480 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003481 return 0;
3482}
3483
Jens Axboe2b188cc2019-01-07 10:46:33 -07003484/*
3485 * IORING_OP_NOP just posts a completion event, nothing else.
3486 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003487static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003488{
3489 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003490
Jens Axboedef596e2019-01-09 08:59:42 -07003491 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3492 return -EINVAL;
3493
Jens Axboe229a7b62020-06-22 10:13:11 -06003494 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003495 return 0;
3496}
3497
Jens Axboe3529d8c2019-12-19 18:24:38 -07003498static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003499{
Jens Axboe6b063142019-01-10 22:13:58 -07003500 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003501
Jens Axboe09bb8392019-03-13 12:39:28 -06003502 if (!req->file)
3503 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003504
Jens Axboe6b063142019-01-10 22:13:58 -07003505 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003506 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003507 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003508 return -EINVAL;
3509
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003510 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3511 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3512 return -EINVAL;
3513
3514 req->sync.off = READ_ONCE(sqe->off);
3515 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003516 return 0;
3517}
3518
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003519static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003520{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003521 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003522 int ret;
3523
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003524 /* fsync always requires a blocking context */
3525 if (force_nonblock)
3526 return -EAGAIN;
3527
Jens Axboe9adbd452019-12-20 08:45:55 -07003528 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003529 end > 0 ? end : LLONG_MAX,
3530 req->sync.flags & IORING_FSYNC_DATASYNC);
3531 if (ret < 0)
3532 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003533 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003534 return 0;
3535}
3536
Jens Axboed63d1b52019-12-10 10:38:56 -07003537static int io_fallocate_prep(struct io_kiocb *req,
3538 const struct io_uring_sqe *sqe)
3539{
3540 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3541 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003542 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3543 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003544
3545 req->sync.off = READ_ONCE(sqe->off);
3546 req->sync.len = READ_ONCE(sqe->addr);
3547 req->sync.mode = READ_ONCE(sqe->len);
3548 return 0;
3549}
3550
Pavel Begunkov014db002020-03-03 21:33:12 +03003551static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003552{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003553 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003554
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003555 /* fallocate always requiring blocking context */
3556 if (force_nonblock)
3557 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003558 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3559 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003560 if (ret < 0)
3561 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003562 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003563 return 0;
3564}
3565
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003566static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003567{
Jens Axboef8748882020-01-08 17:47:02 -07003568 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003569 int ret;
3570
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003571 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003572 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003573 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003574 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003575
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003576 /* open.how should be already initialised */
3577 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003578 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003579
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003580 req->open.dfd = READ_ONCE(sqe->fd);
3581 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003582 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003583 if (IS_ERR(req->open.filename)) {
3584 ret = PTR_ERR(req->open.filename);
3585 req->open.filename = NULL;
3586 return ret;
3587 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003588 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003589 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003590 return 0;
3591}
3592
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003593static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3594{
3595 u64 flags, mode;
3596
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003597 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3598 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003599 if (req->flags & REQ_F_NEED_CLEANUP)
3600 return 0;
3601 mode = READ_ONCE(sqe->len);
3602 flags = READ_ONCE(sqe->open_flags);
3603 req->open.how = build_open_how(flags, mode);
3604 return __io_openat_prep(req, sqe);
3605}
3606
Jens Axboecebdb982020-01-08 17:59:24 -07003607static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3608{
3609 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003610 size_t len;
3611 int ret;
3612
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003613 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3614 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003615 if (req->flags & REQ_F_NEED_CLEANUP)
3616 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003617 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3618 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003619 if (len < OPEN_HOW_SIZE_VER0)
3620 return -EINVAL;
3621
3622 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3623 len);
3624 if (ret)
3625 return ret;
3626
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003627 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003628}
3629
Pavel Begunkov014db002020-03-03 21:33:12 +03003630static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003631{
3632 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003633 struct file *file;
3634 int ret;
3635
Jens Axboef86cd202020-01-29 13:46:44 -07003636 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003637 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003638
Jens Axboecebdb982020-01-08 17:59:24 -07003639 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003640 if (ret)
3641 goto err;
3642
Jens Axboe4022e7a2020-03-19 19:23:18 -06003643 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003644 if (ret < 0)
3645 goto err;
3646
3647 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3648 if (IS_ERR(file)) {
3649 put_unused_fd(ret);
3650 ret = PTR_ERR(file);
3651 } else {
3652 fsnotify_open(file);
3653 fd_install(ret, file);
3654 }
3655err:
3656 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003657 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003658 if (ret < 0)
3659 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003660 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003661 return 0;
3662}
3663
Pavel Begunkov014db002020-03-03 21:33:12 +03003664static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003665{
Pavel Begunkov014db002020-03-03 21:33:12 +03003666 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003667}
3668
Jens Axboe067524e2020-03-02 16:32:28 -07003669static int io_remove_buffers_prep(struct io_kiocb *req,
3670 const struct io_uring_sqe *sqe)
3671{
3672 struct io_provide_buf *p = &req->pbuf;
3673 u64 tmp;
3674
3675 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3676 return -EINVAL;
3677
3678 tmp = READ_ONCE(sqe->fd);
3679 if (!tmp || tmp > USHRT_MAX)
3680 return -EINVAL;
3681
3682 memset(p, 0, sizeof(*p));
3683 p->nbufs = tmp;
3684 p->bgid = READ_ONCE(sqe->buf_group);
3685 return 0;
3686}
3687
3688static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3689 int bgid, unsigned nbufs)
3690{
3691 unsigned i = 0;
3692
3693 /* shouldn't happen */
3694 if (!nbufs)
3695 return 0;
3696
3697 /* the head kbuf is the list itself */
3698 while (!list_empty(&buf->list)) {
3699 struct io_buffer *nxt;
3700
3701 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3702 list_del(&nxt->list);
3703 kfree(nxt);
3704 if (++i == nbufs)
3705 return i;
3706 }
3707 i++;
3708 kfree(buf);
3709 idr_remove(&ctx->io_buffer_idr, bgid);
3710
3711 return i;
3712}
3713
Jens Axboe229a7b62020-06-22 10:13:11 -06003714static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3715 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003716{
3717 struct io_provide_buf *p = &req->pbuf;
3718 struct io_ring_ctx *ctx = req->ctx;
3719 struct io_buffer *head;
3720 int ret = 0;
3721
3722 io_ring_submit_lock(ctx, !force_nonblock);
3723
3724 lockdep_assert_held(&ctx->uring_lock);
3725
3726 ret = -ENOENT;
3727 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3728 if (head)
3729 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3730
3731 io_ring_submit_lock(ctx, !force_nonblock);
3732 if (ret < 0)
3733 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003734 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003735 return 0;
3736}
3737
Jens Axboeddf0322d2020-02-23 16:41:33 -07003738static int io_provide_buffers_prep(struct io_kiocb *req,
3739 const struct io_uring_sqe *sqe)
3740{
3741 struct io_provide_buf *p = &req->pbuf;
3742 u64 tmp;
3743
3744 if (sqe->ioprio || sqe->rw_flags)
3745 return -EINVAL;
3746
3747 tmp = READ_ONCE(sqe->fd);
3748 if (!tmp || tmp > USHRT_MAX)
3749 return -E2BIG;
3750 p->nbufs = tmp;
3751 p->addr = READ_ONCE(sqe->addr);
3752 p->len = READ_ONCE(sqe->len);
3753
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003754 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003755 return -EFAULT;
3756
3757 p->bgid = READ_ONCE(sqe->buf_group);
3758 tmp = READ_ONCE(sqe->off);
3759 if (tmp > USHRT_MAX)
3760 return -E2BIG;
3761 p->bid = tmp;
3762 return 0;
3763}
3764
3765static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3766{
3767 struct io_buffer *buf;
3768 u64 addr = pbuf->addr;
3769 int i, bid = pbuf->bid;
3770
3771 for (i = 0; i < pbuf->nbufs; i++) {
3772 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3773 if (!buf)
3774 break;
3775
3776 buf->addr = addr;
3777 buf->len = pbuf->len;
3778 buf->bid = bid;
3779 addr += pbuf->len;
3780 bid++;
3781 if (!*head) {
3782 INIT_LIST_HEAD(&buf->list);
3783 *head = buf;
3784 } else {
3785 list_add_tail(&buf->list, &(*head)->list);
3786 }
3787 }
3788
3789 return i ? i : -ENOMEM;
3790}
3791
Jens Axboe229a7b62020-06-22 10:13:11 -06003792static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3793 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003794{
3795 struct io_provide_buf *p = &req->pbuf;
3796 struct io_ring_ctx *ctx = req->ctx;
3797 struct io_buffer *head, *list;
3798 int ret = 0;
3799
3800 io_ring_submit_lock(ctx, !force_nonblock);
3801
3802 lockdep_assert_held(&ctx->uring_lock);
3803
3804 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3805
3806 ret = io_add_buffers(p, &head);
3807 if (ret < 0)
3808 goto out;
3809
3810 if (!list) {
3811 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3812 GFP_KERNEL);
3813 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003814 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003815 goto out;
3816 }
3817 }
3818out:
3819 io_ring_submit_unlock(ctx, !force_nonblock);
3820 if (ret < 0)
3821 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003822 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003823 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003824}
3825
Jens Axboe3e4827b2020-01-08 15:18:09 -07003826static int io_epoll_ctl_prep(struct io_kiocb *req,
3827 const struct io_uring_sqe *sqe)
3828{
3829#if defined(CONFIG_EPOLL)
3830 if (sqe->ioprio || sqe->buf_index)
3831 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003832 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003833 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003834
3835 req->epoll.epfd = READ_ONCE(sqe->fd);
3836 req->epoll.op = READ_ONCE(sqe->len);
3837 req->epoll.fd = READ_ONCE(sqe->off);
3838
3839 if (ep_op_has_event(req->epoll.op)) {
3840 struct epoll_event __user *ev;
3841
3842 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3843 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3844 return -EFAULT;
3845 }
3846
3847 return 0;
3848#else
3849 return -EOPNOTSUPP;
3850#endif
3851}
3852
Jens Axboe229a7b62020-06-22 10:13:11 -06003853static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3854 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003855{
3856#if defined(CONFIG_EPOLL)
3857 struct io_epoll *ie = &req->epoll;
3858 int ret;
3859
3860 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3861 if (force_nonblock && ret == -EAGAIN)
3862 return -EAGAIN;
3863
3864 if (ret < 0)
3865 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003866 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003867 return 0;
3868#else
3869 return -EOPNOTSUPP;
3870#endif
3871}
3872
Jens Axboec1ca7572019-12-25 22:18:28 -07003873static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3874{
3875#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3876 if (sqe->ioprio || sqe->buf_index || sqe->off)
3877 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003878 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3879 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003880
3881 req->madvise.addr = READ_ONCE(sqe->addr);
3882 req->madvise.len = READ_ONCE(sqe->len);
3883 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3884 return 0;
3885#else
3886 return -EOPNOTSUPP;
3887#endif
3888}
3889
Pavel Begunkov014db002020-03-03 21:33:12 +03003890static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003891{
3892#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3893 struct io_madvise *ma = &req->madvise;
3894 int ret;
3895
3896 if (force_nonblock)
3897 return -EAGAIN;
3898
3899 ret = do_madvise(ma->addr, ma->len, ma->advice);
3900 if (ret < 0)
3901 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003902 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003903 return 0;
3904#else
3905 return -EOPNOTSUPP;
3906#endif
3907}
3908
Jens Axboe4840e412019-12-25 22:03:45 -07003909static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3910{
3911 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3912 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003913 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3914 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003915
3916 req->fadvise.offset = READ_ONCE(sqe->off);
3917 req->fadvise.len = READ_ONCE(sqe->len);
3918 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3919 return 0;
3920}
3921
Pavel Begunkov014db002020-03-03 21:33:12 +03003922static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003923{
3924 struct io_fadvise *fa = &req->fadvise;
3925 int ret;
3926
Jens Axboe3e694262020-02-01 09:22:49 -07003927 if (force_nonblock) {
3928 switch (fa->advice) {
3929 case POSIX_FADV_NORMAL:
3930 case POSIX_FADV_RANDOM:
3931 case POSIX_FADV_SEQUENTIAL:
3932 break;
3933 default:
3934 return -EAGAIN;
3935 }
3936 }
Jens Axboe4840e412019-12-25 22:03:45 -07003937
3938 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3939 if (ret < 0)
3940 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003941 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003942 return 0;
3943}
3944
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003945static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3946{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003947 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003948 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003949 if (sqe->ioprio || sqe->buf_index)
3950 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003951 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003952 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003953
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003954 req->statx.dfd = READ_ONCE(sqe->fd);
3955 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003956 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003957 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3958 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003959
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003960 return 0;
3961}
3962
Pavel Begunkov014db002020-03-03 21:33:12 +03003963static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003964{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003965 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003966 int ret;
3967
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003968 if (force_nonblock) {
3969 /* only need file table for an actual valid fd */
3970 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3971 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003972 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003973 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003974
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003975 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3976 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003977
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003978 if (ret < 0)
3979 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003980 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003981 return 0;
3982}
3983
Jens Axboeb5dba592019-12-11 14:02:38 -07003984static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3985{
3986 /*
3987 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003988 * leave the 'file' in an undeterminate state, and here need to modify
3989 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003990 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003991 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003992 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3993
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003994 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3995 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003996 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3997 sqe->rw_flags || sqe->buf_index)
3998 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003999 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004000 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004001
4002 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004003 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004004 return -EBADF;
4005
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004006 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004007 return 0;
4008}
4009
Jens Axboe229a7b62020-06-22 10:13:11 -06004010static int io_close(struct io_kiocb *req, bool force_nonblock,
4011 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004012{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004013 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004014 int ret;
4015
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004016 /* might be already done during nonblock submission */
4017 if (!close->put_file) {
4018 ret = __close_fd_get_file(close->fd, &close->put_file);
4019 if (ret < 0)
4020 return (ret == -ENOENT) ? -EBADF : ret;
4021 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004022
4023 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004024 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004025 /* was never set, but play safe */
4026 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004027 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004028 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004029 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004030 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004031
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004032 /* No ->flush() or already async, safely close from here */
4033 ret = filp_close(close->put_file, req->work.files);
4034 if (ret < 0)
4035 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004036 fput(close->put_file);
4037 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004038 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004039 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004040}
4041
Jens Axboe3529d8c2019-12-19 18:24:38 -07004042static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004043{
4044 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004045
4046 if (!req->file)
4047 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004048
4049 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4050 return -EINVAL;
4051 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4052 return -EINVAL;
4053
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004054 req->sync.off = READ_ONCE(sqe->off);
4055 req->sync.len = READ_ONCE(sqe->len);
4056 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004057 return 0;
4058}
4059
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004060static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004061{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004062 int ret;
4063
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004064 /* sync_file_range always requires a blocking context */
4065 if (force_nonblock)
4066 return -EAGAIN;
4067
Jens Axboe9adbd452019-12-20 08:45:55 -07004068 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004069 req->sync.flags);
4070 if (ret < 0)
4071 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004072 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004073 return 0;
4074}
4075
YueHaibing469956e2020-03-04 15:53:52 +08004076#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004077static int io_setup_async_msg(struct io_kiocb *req,
4078 struct io_async_msghdr *kmsg)
4079{
4080 if (req->io)
4081 return -EAGAIN;
4082 if (io_alloc_async_ctx(req)) {
4083 if (kmsg->iov != kmsg->fast_iov)
4084 kfree(kmsg->iov);
4085 return -ENOMEM;
4086 }
4087 req->flags |= REQ_F_NEED_CLEANUP;
4088 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4089 return -EAGAIN;
4090}
4091
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004092static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4093 struct io_async_msghdr *iomsg)
4094{
4095 iomsg->iov = iomsg->fast_iov;
4096 iomsg->msg.msg_name = &iomsg->addr;
4097 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4098 req->sr_msg.msg_flags, &iomsg->iov);
4099}
4100
Jens Axboe3529d8c2019-12-19 18:24:38 -07004101static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004102{
Jens Axboee47293f2019-12-20 08:58:21 -07004103 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004104 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004105 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004106
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004107 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4108 return -EINVAL;
4109
Jens Axboee47293f2019-12-20 08:58:21 -07004110 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004111 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004112 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004113
Jens Axboed8768362020-02-27 14:17:49 -07004114#ifdef CONFIG_COMPAT
4115 if (req->ctx->compat)
4116 sr->msg_flags |= MSG_CMSG_COMPAT;
4117#endif
4118
Jens Axboefddafac2020-01-04 20:19:44 -07004119 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004120 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004121 /* iovec is already imported */
4122 if (req->flags & REQ_F_NEED_CLEANUP)
4123 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004124
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004125 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004126 if (!ret)
4127 req->flags |= REQ_F_NEED_CLEANUP;
4128 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004129}
4130
Jens Axboe229a7b62020-06-22 10:13:11 -06004131static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4132 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004133{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004134 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004135 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004136 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004137 int ret;
4138
Jens Axboe03b12302019-12-02 18:50:25 -07004139 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004140 if (unlikely(!sock))
4141 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004142
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004143 if (req->io) {
4144 kmsg = &req->io->msg;
4145 kmsg->msg.msg_name = &req->io->msg.addr;
4146 /* if iov is set, it's allocated already */
4147 if (!kmsg->iov)
4148 kmsg->iov = kmsg->fast_iov;
4149 kmsg->msg.msg_iter.iov = kmsg->iov;
4150 } else {
4151 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004152 if (ret)
4153 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004154 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004155 }
4156
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004157 flags = req->sr_msg.msg_flags;
4158 if (flags & MSG_DONTWAIT)
4159 req->flags |= REQ_F_NOWAIT;
4160 else if (force_nonblock)
4161 flags |= MSG_DONTWAIT;
4162
4163 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4164 if (force_nonblock && ret == -EAGAIN)
4165 return io_setup_async_msg(req, kmsg);
4166 if (ret == -ERESTARTSYS)
4167 ret = -EINTR;
4168
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004169 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004170 kfree(kmsg->iov);
4171 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004172 if (ret < 0)
4173 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004174 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004175 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004176}
4177
Jens Axboe229a7b62020-06-22 10:13:11 -06004178static int io_send(struct io_kiocb *req, bool force_nonblock,
4179 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004180{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004181 struct io_sr_msg *sr = &req->sr_msg;
4182 struct msghdr msg;
4183 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004184 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004185 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004186 int ret;
4187
4188 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004189 if (unlikely(!sock))
4190 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004191
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004192 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4193 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004194 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004195
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004196 msg.msg_name = NULL;
4197 msg.msg_control = NULL;
4198 msg.msg_controllen = 0;
4199 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004200
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004201 flags = req->sr_msg.msg_flags;
4202 if (flags & MSG_DONTWAIT)
4203 req->flags |= REQ_F_NOWAIT;
4204 else if (force_nonblock)
4205 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004206
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004207 msg.msg_flags = flags;
4208 ret = sock_sendmsg(sock, &msg);
4209 if (force_nonblock && ret == -EAGAIN)
4210 return -EAGAIN;
4211 if (ret == -ERESTARTSYS)
4212 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004213
Jens Axboe03b12302019-12-02 18:50:25 -07004214 if (ret < 0)
4215 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004216 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004217 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004218}
4219
Pavel Begunkov1400e692020-07-12 20:41:05 +03004220static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4221 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004222{
4223 struct io_sr_msg *sr = &req->sr_msg;
4224 struct iovec __user *uiov;
4225 size_t iov_len;
4226 int ret;
4227
Pavel Begunkov1400e692020-07-12 20:41:05 +03004228 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4229 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004230 if (ret)
4231 return ret;
4232
4233 if (req->flags & REQ_F_BUFFER_SELECT) {
4234 if (iov_len > 1)
4235 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004236 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004237 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004238 sr->len = iomsg->iov[0].iov_len;
4239 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004240 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004241 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004242 } else {
4243 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004244 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004245 if (ret > 0)
4246 ret = 0;
4247 }
4248
4249 return ret;
4250}
4251
4252#ifdef CONFIG_COMPAT
4253static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004254 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004255{
4256 struct compat_msghdr __user *msg_compat;
4257 struct io_sr_msg *sr = &req->sr_msg;
4258 struct compat_iovec __user *uiov;
4259 compat_uptr_t ptr;
4260 compat_size_t len;
4261 int ret;
4262
Pavel Begunkov270a5942020-07-12 20:41:04 +03004263 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004264 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004265 &ptr, &len);
4266 if (ret)
4267 return ret;
4268
4269 uiov = compat_ptr(ptr);
4270 if (req->flags & REQ_F_BUFFER_SELECT) {
4271 compat_ssize_t clen;
4272
4273 if (len > 1)
4274 return -EINVAL;
4275 if (!access_ok(uiov, sizeof(*uiov)))
4276 return -EFAULT;
4277 if (__get_user(clen, &uiov->iov_len))
4278 return -EFAULT;
4279 if (clen < 0)
4280 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004281 sr->len = iomsg->iov[0].iov_len;
4282 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004283 } else {
4284 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004285 &iomsg->iov,
4286 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004287 if (ret < 0)
4288 return ret;
4289 }
4290
4291 return 0;
4292}
Jens Axboe03b12302019-12-02 18:50:25 -07004293#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004294
Pavel Begunkov1400e692020-07-12 20:41:05 +03004295static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4296 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004297{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004298 iomsg->msg.msg_name = &iomsg->addr;
4299 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004300
4301#ifdef CONFIG_COMPAT
4302 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004303 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004304#endif
4305
Pavel Begunkov1400e692020-07-12 20:41:05 +03004306 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004307}
4308
Jens Axboebcda7ba2020-02-23 16:42:51 -07004309static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004310 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004311{
4312 struct io_sr_msg *sr = &req->sr_msg;
4313 struct io_buffer *kbuf;
4314
Jens Axboebcda7ba2020-02-23 16:42:51 -07004315 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4316 if (IS_ERR(kbuf))
4317 return kbuf;
4318
4319 sr->kbuf = kbuf;
4320 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004321 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004322}
4323
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004324static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4325{
4326 return io_put_kbuf(req, req->sr_msg.kbuf);
4327}
4328
Jens Axboe3529d8c2019-12-19 18:24:38 -07004329static int io_recvmsg_prep(struct io_kiocb *req,
4330 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004331{
Jens Axboee47293f2019-12-20 08:58:21 -07004332 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004333 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004334 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004335
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004336 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4337 return -EINVAL;
4338
Jens Axboe3529d8c2019-12-19 18:24:38 -07004339 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004340 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004341 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004342 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004343
Jens Axboed8768362020-02-27 14:17:49 -07004344#ifdef CONFIG_COMPAT
4345 if (req->ctx->compat)
4346 sr->msg_flags |= MSG_CMSG_COMPAT;
4347#endif
4348
Jens Axboefddafac2020-01-04 20:19:44 -07004349 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004350 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004351 /* iovec is already imported */
4352 if (req->flags & REQ_F_NEED_CLEANUP)
4353 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004354
Pavel Begunkov1400e692020-07-12 20:41:05 +03004355 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004356 if (!ret)
4357 req->flags |= REQ_F_NEED_CLEANUP;
4358 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004359}
4360
Jens Axboe229a7b62020-06-22 10:13:11 -06004361static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4362 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004363{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004364 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004365 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004366 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004367 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004368 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004369
Jens Axboe0fa03c62019-04-19 13:34:07 -06004370 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004371 if (unlikely(!sock))
4372 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004373
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004374 if (req->io) {
4375 kmsg = &req->io->msg;
4376 kmsg->msg.msg_name = &req->io->msg.addr;
4377 /* if iov is set, it's allocated already */
4378 if (!kmsg->iov)
4379 kmsg->iov = kmsg->fast_iov;
4380 kmsg->msg.msg_iter.iov = kmsg->iov;
4381 } else {
4382 ret = io_recvmsg_copy_hdr(req, &iomsg);
4383 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004384 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004385 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004386 }
4387
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004388 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004389 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004390 if (IS_ERR(kbuf))
4391 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004392 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4393 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4394 1, req->sr_msg.len);
4395 }
4396
4397 flags = req->sr_msg.msg_flags;
4398 if (flags & MSG_DONTWAIT)
4399 req->flags |= REQ_F_NOWAIT;
4400 else if (force_nonblock)
4401 flags |= MSG_DONTWAIT;
4402
4403 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4404 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004405 if (force_nonblock && ret == -EAGAIN)
4406 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004407 if (ret == -ERESTARTSYS)
4408 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004409
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004410 if (req->flags & REQ_F_BUFFER_SELECTED)
4411 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004412 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004413 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004414 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004415 if (ret < 0)
4416 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004417 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004418 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004419}
4420
Jens Axboe229a7b62020-06-22 10:13:11 -06004421static int io_recv(struct io_kiocb *req, bool force_nonblock,
4422 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004423{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004424 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004425 struct io_sr_msg *sr = &req->sr_msg;
4426 struct msghdr msg;
4427 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004428 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004429 struct iovec iov;
4430 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004431 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004432
Jens Axboefddafac2020-01-04 20:19:44 -07004433 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004434 if (unlikely(!sock))
4435 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004436
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004437 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004438 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004439 if (IS_ERR(kbuf))
4440 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004441 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004442 }
4443
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004444 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004445 if (unlikely(ret))
4446 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004447
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004448 msg.msg_name = NULL;
4449 msg.msg_control = NULL;
4450 msg.msg_controllen = 0;
4451 msg.msg_namelen = 0;
4452 msg.msg_iocb = NULL;
4453 msg.msg_flags = 0;
4454
4455 flags = req->sr_msg.msg_flags;
4456 if (flags & MSG_DONTWAIT)
4457 req->flags |= REQ_F_NOWAIT;
4458 else if (force_nonblock)
4459 flags |= MSG_DONTWAIT;
4460
4461 ret = sock_recvmsg(sock, &msg, flags);
4462 if (force_nonblock && ret == -EAGAIN)
4463 return -EAGAIN;
4464 if (ret == -ERESTARTSYS)
4465 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004466out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004467 if (req->flags & REQ_F_BUFFER_SELECTED)
4468 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004469 if (ret < 0)
4470 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004471 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004472 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004473}
4474
Jens Axboe3529d8c2019-12-19 18:24:38 -07004475static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004476{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004477 struct io_accept *accept = &req->accept;
4478
Jens Axboe17f2fe32019-10-17 14:42:58 -06004479 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4480 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004481 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004482 return -EINVAL;
4483
Jens Axboed55e5f52019-12-11 16:12:15 -07004484 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4485 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004486 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004487 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004488 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004489}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004490
Jens Axboe229a7b62020-06-22 10:13:11 -06004491static int io_accept(struct io_kiocb *req, bool force_nonblock,
4492 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004493{
4494 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004495 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004496 int ret;
4497
Jiufei Xuee697dee2020-06-10 13:41:59 +08004498 if (req->file->f_flags & O_NONBLOCK)
4499 req->flags |= REQ_F_NOWAIT;
4500
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004501 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004502 accept->addr_len, accept->flags,
4503 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004504 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004505 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004506 if (ret < 0) {
4507 if (ret == -ERESTARTSYS)
4508 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004509 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004510 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004511 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004512 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004513}
4514
Jens Axboe3529d8c2019-12-19 18:24:38 -07004515static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004516{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004517 struct io_connect *conn = &req->connect;
4518 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004519
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004520 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4521 return -EINVAL;
4522 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4523 return -EINVAL;
4524
Jens Axboe3529d8c2019-12-19 18:24:38 -07004525 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4526 conn->addr_len = READ_ONCE(sqe->addr2);
4527
4528 if (!io)
4529 return 0;
4530
4531 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004532 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004533}
4534
Jens Axboe229a7b62020-06-22 10:13:11 -06004535static int io_connect(struct io_kiocb *req, bool force_nonblock,
4536 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004537{
Jens Axboef499a022019-12-02 16:28:46 -07004538 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004539 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004540 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004541
Jens Axboef499a022019-12-02 16:28:46 -07004542 if (req->io) {
4543 io = req->io;
4544 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004545 ret = move_addr_to_kernel(req->connect.addr,
4546 req->connect.addr_len,
4547 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004548 if (ret)
4549 goto out;
4550 io = &__io;
4551 }
4552
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004553 file_flags = force_nonblock ? O_NONBLOCK : 0;
4554
4555 ret = __sys_connect_file(req->file, &io->connect.address,
4556 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004557 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004558 if (req->io)
4559 return -EAGAIN;
4560 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004561 ret = -ENOMEM;
4562 goto out;
4563 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004564 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004565 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004566 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004567 if (ret == -ERESTARTSYS)
4568 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004569out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004570 if (ret < 0)
4571 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004572 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004573 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004574}
YueHaibing469956e2020-03-04 15:53:52 +08004575#else /* !CONFIG_NET */
4576static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4577{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004578 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004579}
4580
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004581static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4582 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004583{
YueHaibing469956e2020-03-04 15:53:52 +08004584 return -EOPNOTSUPP;
4585}
4586
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004587static int io_send(struct io_kiocb *req, bool force_nonblock,
4588 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004589{
4590 return -EOPNOTSUPP;
4591}
4592
4593static int io_recvmsg_prep(struct io_kiocb *req,
4594 const struct io_uring_sqe *sqe)
4595{
4596 return -EOPNOTSUPP;
4597}
4598
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004599static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4600 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004601{
4602 return -EOPNOTSUPP;
4603}
4604
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004605static int io_recv(struct io_kiocb *req, bool force_nonblock,
4606 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004607{
4608 return -EOPNOTSUPP;
4609}
4610
4611static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4612{
4613 return -EOPNOTSUPP;
4614}
4615
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004616static int io_accept(struct io_kiocb *req, bool force_nonblock,
4617 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004618{
4619 return -EOPNOTSUPP;
4620}
4621
4622static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4623{
4624 return -EOPNOTSUPP;
4625}
4626
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004627static int io_connect(struct io_kiocb *req, bool force_nonblock,
4628 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004629{
4630 return -EOPNOTSUPP;
4631}
4632#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004633
Jens Axboed7718a92020-02-14 22:23:12 -07004634struct io_poll_table {
4635 struct poll_table_struct pt;
4636 struct io_kiocb *req;
4637 int error;
4638};
4639
Jens Axboed7718a92020-02-14 22:23:12 -07004640static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4641 __poll_t mask, task_work_func_t func)
4642{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004643 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004644 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004645
4646 /* for instances that support it check for an event match first: */
4647 if (mask && !(mask & poll->events))
4648 return 0;
4649
4650 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4651
4652 list_del_init(&poll->wait.entry);
4653
Jens Axboed7718a92020-02-14 22:23:12 -07004654 req->result = mask;
4655 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004656 percpu_ref_get(&req->ctx->refs);
4657
Jens Axboed7718a92020-02-14 22:23:12 -07004658 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004659 * If we using the signalfd wait_queue_head for this wakeup, then
4660 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4661 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4662 * either, as the normal wakeup will suffice.
4663 */
4664 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4665
4666 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004667 * If this fails, then the task is exiting. When a task exits, the
4668 * work gets canceled, so just cancel this request as well instead
4669 * of executing it. We can't safely execute it anyway, as we may not
4670 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004671 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004672 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004673 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004674 struct task_struct *tsk;
4675
Jens Axboee3aabf92020-05-18 11:04:17 -06004676 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004677 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004678 task_work_add(tsk, &req->task_work, 0);
4679 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004680 }
Jens Axboed7718a92020-02-14 22:23:12 -07004681 return 1;
4682}
4683
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004684static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4685 __acquires(&req->ctx->completion_lock)
4686{
4687 struct io_ring_ctx *ctx = req->ctx;
4688
4689 if (!req->result && !READ_ONCE(poll->canceled)) {
4690 struct poll_table_struct pt = { ._key = poll->events };
4691
4692 req->result = vfs_poll(req->file, &pt) & poll->events;
4693 }
4694
4695 spin_lock_irq(&ctx->completion_lock);
4696 if (!req->result && !READ_ONCE(poll->canceled)) {
4697 add_wait_queue(poll->head, &poll->wait);
4698 return true;
4699 }
4700
4701 return false;
4702}
4703
Jens Axboed4e7cd32020-08-15 11:44:50 -07004704static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004705{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004706 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4707 if (req->opcode == IORING_OP_POLL_ADD)
4708 return (struct io_poll_iocb *) req->io;
4709 return req->apoll->double_poll;
4710}
4711
4712static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4713{
4714 if (req->opcode == IORING_OP_POLL_ADD)
4715 return &req->poll;
4716 return &req->apoll->poll;
4717}
4718
4719static void io_poll_remove_double(struct io_kiocb *req)
4720{
4721 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004722
4723 lockdep_assert_held(&req->ctx->completion_lock);
4724
4725 if (poll && poll->head) {
4726 struct wait_queue_head *head = poll->head;
4727
4728 spin_lock(&head->lock);
4729 list_del_init(&poll->wait.entry);
4730 if (poll->wait.private)
4731 refcount_dec(&req->refs);
4732 poll->head = NULL;
4733 spin_unlock(&head->lock);
4734 }
4735}
4736
4737static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4738{
4739 struct io_ring_ctx *ctx = req->ctx;
4740
Jens Axboed4e7cd32020-08-15 11:44:50 -07004741 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004742 req->poll.done = true;
4743 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4744 io_commit_cqring(ctx);
4745}
4746
4747static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4748{
4749 struct io_ring_ctx *ctx = req->ctx;
4750
4751 if (io_poll_rewait(req, &req->poll)) {
4752 spin_unlock_irq(&ctx->completion_lock);
4753 return;
4754 }
4755
4756 hash_del(&req->hash_node);
4757 io_poll_complete(req, req->result, 0);
4758 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03004759 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004760 spin_unlock_irq(&ctx->completion_lock);
4761
4762 io_cqring_ev_posted(ctx);
4763}
4764
4765static void io_poll_task_func(struct callback_head *cb)
4766{
4767 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004768 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004769 struct io_kiocb *nxt = NULL;
4770
4771 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004772 if (nxt)
4773 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004774 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004775}
4776
4777static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4778 int sync, void *key)
4779{
4780 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004781 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004782 __poll_t mask = key_to_poll(key);
4783
4784 /* for instances that support it check for an event match first: */
4785 if (mask && !(mask & poll->events))
4786 return 0;
4787
Jens Axboe8706e042020-09-28 08:38:54 -06004788 list_del_init(&wait->entry);
4789
Jens Axboe807abcb2020-07-17 17:09:27 -06004790 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004791 bool done;
4792
Jens Axboe807abcb2020-07-17 17:09:27 -06004793 spin_lock(&poll->head->lock);
4794 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004795 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004796 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004797 /* make sure double remove sees this as being gone */
4798 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004799 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004800 if (!done)
4801 __io_async_wake(req, poll, mask, io_poll_task_func);
4802 }
4803 refcount_dec(&req->refs);
4804 return 1;
4805}
4806
4807static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4808 wait_queue_func_t wake_func)
4809{
4810 poll->head = NULL;
4811 poll->done = false;
4812 poll->canceled = false;
4813 poll->events = events;
4814 INIT_LIST_HEAD(&poll->wait.entry);
4815 init_waitqueue_func_entry(&poll->wait, wake_func);
4816}
4817
4818static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004819 struct wait_queue_head *head,
4820 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004821{
4822 struct io_kiocb *req = pt->req;
4823
4824 /*
4825 * If poll->head is already set, it's because the file being polled
4826 * uses multiple waitqueues for poll handling (eg one for read, one
4827 * for write). Setup a separate io_poll_iocb if this happens.
4828 */
4829 if (unlikely(poll->head)) {
4830 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004831 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004832 pt->error = -EINVAL;
4833 return;
4834 }
4835 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4836 if (!poll) {
4837 pt->error = -ENOMEM;
4838 return;
4839 }
4840 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4841 refcount_inc(&req->refs);
4842 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004843 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004844 }
4845
4846 pt->error = 0;
4847 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004848
4849 if (poll->events & EPOLLEXCLUSIVE)
4850 add_wait_queue_exclusive(head, &poll->wait);
4851 else
4852 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004853}
4854
4855static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4856 struct poll_table_struct *p)
4857{
4858 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004859 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004860
Jens Axboe807abcb2020-07-17 17:09:27 -06004861 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004862}
4863
Jens Axboed7718a92020-02-14 22:23:12 -07004864static void io_async_task_func(struct callback_head *cb)
4865{
4866 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4867 struct async_poll *apoll = req->apoll;
4868 struct io_ring_ctx *ctx = req->ctx;
4869
4870 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4871
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004872 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004873 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004874 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004875 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004876 }
4877
Jens Axboe31067252020-05-17 17:43:31 -06004878 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004879 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004880 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004881
Jens Axboed4e7cd32020-08-15 11:44:50 -07004882 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004883 spin_unlock_irq(&ctx->completion_lock);
4884
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004885 if (!READ_ONCE(apoll->poll.canceled))
4886 __io_req_task_submit(req);
4887 else
4888 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004889
Jens Axboe6d816e02020-08-11 08:04:14 -06004890 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004891 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004892 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004893}
4894
4895static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4896 void *key)
4897{
4898 struct io_kiocb *req = wait->private;
4899 struct io_poll_iocb *poll = &req->apoll->poll;
4900
4901 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4902 key_to_poll(key));
4903
4904 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4905}
4906
4907static void io_poll_req_insert(struct io_kiocb *req)
4908{
4909 struct io_ring_ctx *ctx = req->ctx;
4910 struct hlist_head *list;
4911
4912 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4913 hlist_add_head(&req->hash_node, list);
4914}
4915
4916static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4917 struct io_poll_iocb *poll,
4918 struct io_poll_table *ipt, __poll_t mask,
4919 wait_queue_func_t wake_func)
4920 __acquires(&ctx->completion_lock)
4921{
4922 struct io_ring_ctx *ctx = req->ctx;
4923 bool cancel = false;
4924
Jens Axboe18bceab2020-05-15 11:56:54 -06004925 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004926 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004927 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004928
4929 ipt->pt._key = mask;
4930 ipt->req = req;
4931 ipt->error = -EINVAL;
4932
Jens Axboed7718a92020-02-14 22:23:12 -07004933 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4934
4935 spin_lock_irq(&ctx->completion_lock);
4936 if (likely(poll->head)) {
4937 spin_lock(&poll->head->lock);
4938 if (unlikely(list_empty(&poll->wait.entry))) {
4939 if (ipt->error)
4940 cancel = true;
4941 ipt->error = 0;
4942 mask = 0;
4943 }
4944 if (mask || ipt->error)
4945 list_del_init(&poll->wait.entry);
4946 else if (cancel)
4947 WRITE_ONCE(poll->canceled, true);
4948 else if (!poll->done) /* actually waiting for an event */
4949 io_poll_req_insert(req);
4950 spin_unlock(&poll->head->lock);
4951 }
4952
4953 return mask;
4954}
4955
4956static bool io_arm_poll_handler(struct io_kiocb *req)
4957{
4958 const struct io_op_def *def = &io_op_defs[req->opcode];
4959 struct io_ring_ctx *ctx = req->ctx;
4960 struct async_poll *apoll;
4961 struct io_poll_table ipt;
4962 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004963 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004964
4965 if (!req->file || !file_can_poll(req->file))
4966 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004967 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004968 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004969 if (def->pollin)
4970 rw = READ;
4971 else if (def->pollout)
4972 rw = WRITE;
4973 else
4974 return false;
4975 /* if we can't nonblock try, then no point in arming a poll handler */
4976 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004977 return false;
4978
4979 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4980 if (unlikely(!apoll))
4981 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004982 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004983
4984 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07004985 req->apoll = apoll;
4986 INIT_HLIST_NODE(&req->hash_node);
4987
Nathan Chancellor8755d972020-03-02 16:01:19 -07004988 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004989 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004990 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004991 if (def->pollout)
4992 mask |= POLLOUT | POLLWRNORM;
4993 mask |= POLLERR | POLLPRI;
4994
4995 ipt.pt._qproc = io_async_queue_proc;
4996
4997 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4998 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06004999 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005000 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005001 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005002 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005003 kfree(apoll);
5004 return false;
5005 }
5006 spin_unlock_irq(&ctx->completion_lock);
5007 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5008 apoll->poll.events);
5009 return true;
5010}
5011
5012static bool __io_poll_remove_one(struct io_kiocb *req,
5013 struct io_poll_iocb *poll)
5014{
Jens Axboeb41e9852020-02-17 09:52:41 -07005015 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005016
5017 spin_lock(&poll->head->lock);
5018 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005019 if (!list_empty(&poll->wait.entry)) {
5020 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005021 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005022 }
5023 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005024 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005025 return do_complete;
5026}
5027
5028static bool io_poll_remove_one(struct io_kiocb *req)
5029{
5030 bool do_complete;
5031
Jens Axboed4e7cd32020-08-15 11:44:50 -07005032 io_poll_remove_double(req);
5033
Jens Axboed7718a92020-02-14 22:23:12 -07005034 if (req->opcode == IORING_OP_POLL_ADD) {
5035 do_complete = __io_poll_remove_one(req, &req->poll);
5036 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005037 struct async_poll *apoll = req->apoll;
5038
Jens Axboed7718a92020-02-14 22:23:12 -07005039 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005040 do_complete = __io_poll_remove_one(req, &apoll->poll);
5041 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005042 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005043 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005044 kfree(apoll);
5045 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005046 }
5047
Jens Axboeb41e9852020-02-17 09:52:41 -07005048 if (do_complete) {
5049 io_cqring_fill_event(req, -ECANCELED);
5050 io_commit_cqring(req->ctx);
5051 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005052 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005053 io_put_req(req);
5054 }
5055
5056 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005057}
5058
Jens Axboe76e1b642020-09-26 15:05:03 -06005059/*
5060 * Returns true if we found and killed one or more poll requests
5061 */
5062static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005063{
Jens Axboe78076bb2019-12-04 19:56:40 -07005064 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005065 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005066 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005067
5068 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005069 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5070 struct hlist_head *list;
5071
5072 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005073 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5074 if (io_task_match(req, tsk))
5075 posted += io_poll_remove_one(req);
5076 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005077 }
5078 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005079
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005080 if (posted)
5081 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005082
5083 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005084}
5085
Jens Axboe47f46762019-11-09 17:43:02 -07005086static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5087{
Jens Axboe78076bb2019-12-04 19:56:40 -07005088 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005089 struct io_kiocb *req;
5090
Jens Axboe78076bb2019-12-04 19:56:40 -07005091 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5092 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005093 if (sqe_addr != req->user_data)
5094 continue;
5095 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005096 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005097 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005098 }
5099
5100 return -ENOENT;
5101}
5102
Jens Axboe3529d8c2019-12-19 18:24:38 -07005103static int io_poll_remove_prep(struct io_kiocb *req,
5104 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005105{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005106 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5107 return -EINVAL;
5108 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5109 sqe->poll_events)
5110 return -EINVAL;
5111
Jens Axboe0969e782019-12-17 18:40:57 -07005112 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005113 return 0;
5114}
5115
5116/*
5117 * Find a running poll command that matches one specified in sqe->addr,
5118 * and remove it if found.
5119 */
5120static int io_poll_remove(struct io_kiocb *req)
5121{
5122 struct io_ring_ctx *ctx = req->ctx;
5123 u64 addr;
5124 int ret;
5125
Jens Axboe0969e782019-12-17 18:40:57 -07005126 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005127 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005128 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005129 spin_unlock_irq(&ctx->completion_lock);
5130
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005131 if (ret < 0)
5132 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005133 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005134 return 0;
5135}
5136
Jens Axboe221c5eb2019-01-17 09:41:58 -07005137static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5138 void *key)
5139{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005140 struct io_kiocb *req = wait->private;
5141 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005142
Jens Axboed7718a92020-02-14 22:23:12 -07005143 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005144}
5145
Jens Axboe221c5eb2019-01-17 09:41:58 -07005146static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5147 struct poll_table_struct *p)
5148{
5149 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5150
Jens Axboe807abcb2020-07-17 17:09:27 -06005151 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005152}
5153
Jens Axboe3529d8c2019-12-19 18:24:38 -07005154static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005155{
5156 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005157 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005158
5159 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5160 return -EINVAL;
5161 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5162 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005163 if (!poll->file)
5164 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005165
Jiufei Xue5769a352020-06-17 17:53:55 +08005166 events = READ_ONCE(sqe->poll32_events);
5167#ifdef __BIG_ENDIAN
5168 events = swahw32(events);
5169#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005170 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5171 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005172 return 0;
5173}
5174
Pavel Begunkov014db002020-03-03 21:33:12 +03005175static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005176{
5177 struct io_poll_iocb *poll = &req->poll;
5178 struct io_ring_ctx *ctx = req->ctx;
5179 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005180 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005181
Jens Axboe78076bb2019-12-04 19:56:40 -07005182 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005183 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005184
Jens Axboed7718a92020-02-14 22:23:12 -07005185 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5186 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005187
Jens Axboe8c838782019-03-12 15:48:16 -06005188 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005189 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005190 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005191 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005192 spin_unlock_irq(&ctx->completion_lock);
5193
Jens Axboe8c838782019-03-12 15:48:16 -06005194 if (mask) {
5195 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005196 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005197 }
Jens Axboe8c838782019-03-12 15:48:16 -06005198 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005199}
5200
Jens Axboe5262f562019-09-17 12:26:57 -06005201static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5202{
Jens Axboead8a48a2019-11-15 08:49:11 -07005203 struct io_timeout_data *data = container_of(timer,
5204 struct io_timeout_data, timer);
5205 struct io_kiocb *req = data->req;
5206 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005207 unsigned long flags;
5208
Jens Axboe5262f562019-09-17 12:26:57 -06005209 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005210 atomic_set(&req->ctx->cq_timeouts,
5211 atomic_read(&req->ctx->cq_timeouts) + 1);
5212
zhangyi (F)ef036812019-10-23 15:10:08 +08005213 /*
Jens Axboe11365042019-10-16 09:08:32 -06005214 * We could be racing with timeout deletion. If the list is empty,
5215 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005216 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005217 if (!list_empty(&req->timeout.list))
5218 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005219
Jens Axboe78e19bb2019-11-06 15:21:34 -07005220 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005221 io_commit_cqring(ctx);
5222 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5223
5224 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005225 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005226 io_put_req(req);
5227 return HRTIMER_NORESTART;
5228}
5229
Jens Axboef254ac02020-08-12 17:33:30 -06005230static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005231{
Jens Axboef254ac02020-08-12 17:33:30 -06005232 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005233
Jens Axboef254ac02020-08-12 17:33:30 -06005234 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005235
Jens Axboe2d283902019-12-04 11:08:05 -07005236 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005237 if (ret == -1)
5238 return -EALREADY;
5239
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005240 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005241 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005242 io_cqring_fill_event(req, -ECANCELED);
5243 io_put_req(req);
5244 return 0;
5245}
5246
Jens Axboef254ac02020-08-12 17:33:30 -06005247static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5248{
5249 struct io_kiocb *req;
5250 int ret = -ENOENT;
5251
5252 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5253 if (user_data == req->user_data) {
5254 ret = 0;
5255 break;
5256 }
5257 }
5258
5259 if (ret == -ENOENT)
5260 return ret;
5261
5262 return __io_timeout_cancel(req);
5263}
5264
Jens Axboe3529d8c2019-12-19 18:24:38 -07005265static int io_timeout_remove_prep(struct io_kiocb *req,
5266 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005267{
Jens Axboeb29472e2019-12-17 18:50:29 -07005268 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5269 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005270 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5271 return -EINVAL;
5272 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005273 return -EINVAL;
5274
5275 req->timeout.addr = READ_ONCE(sqe->addr);
5276 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5277 if (req->timeout.flags)
5278 return -EINVAL;
5279
Jens Axboeb29472e2019-12-17 18:50:29 -07005280 return 0;
5281}
5282
Jens Axboe11365042019-10-16 09:08:32 -06005283/*
5284 * Remove or update an existing timeout command
5285 */
Jens Axboefc4df992019-12-10 14:38:45 -07005286static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005287{
5288 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005289 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005290
Jens Axboe11365042019-10-16 09:08:32 -06005291 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005292 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005293
Jens Axboe47f46762019-11-09 17:43:02 -07005294 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005295 io_commit_cqring(ctx);
5296 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005297 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005298 if (ret < 0)
5299 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005300 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005301 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005302}
5303
Jens Axboe3529d8c2019-12-19 18:24:38 -07005304static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005305 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005306{
Jens Axboead8a48a2019-11-15 08:49:11 -07005307 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005308 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005309 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005310
Jens Axboead8a48a2019-11-15 08:49:11 -07005311 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005312 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005313 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005314 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005315 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005316 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005317 flags = READ_ONCE(sqe->timeout_flags);
5318 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005319 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005320
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005321 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005322
Jens Axboe3529d8c2019-12-19 18:24:38 -07005323 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005324 return -ENOMEM;
5325
5326 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005327 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005328
5329 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005330 return -EFAULT;
5331
Jens Axboe11365042019-10-16 09:08:32 -06005332 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005333 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005334 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005335 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005336
Jens Axboead8a48a2019-11-15 08:49:11 -07005337 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5338 return 0;
5339}
5340
Jens Axboefc4df992019-12-10 14:38:45 -07005341static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005342{
Jens Axboead8a48a2019-11-15 08:49:11 -07005343 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005344 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005345 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005346 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005347
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005348 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005349
Jens Axboe5262f562019-09-17 12:26:57 -06005350 /*
5351 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005352 * timeout event to be satisfied. If it isn't set, then this is
5353 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005354 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005355 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005356 entry = ctx->timeout_list.prev;
5357 goto add;
5358 }
Jens Axboe5262f562019-09-17 12:26:57 -06005359
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005360 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5361 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005362
5363 /*
5364 * Insertion sort, ensuring the first entry in the list is always
5365 * the one we need first.
5366 */
Jens Axboe5262f562019-09-17 12:26:57 -06005367 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005368 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5369 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005370
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005371 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005372 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005373 /* nxt.seq is behind @tail, otherwise would've been completed */
5374 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005375 break;
5376 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005377add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005378 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005379 data->timer.function = io_timeout_fn;
5380 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005381 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005382 return 0;
5383}
5384
Jens Axboe62755e32019-10-28 21:49:21 -06005385static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005386{
Jens Axboe62755e32019-10-28 21:49:21 -06005387 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005388
Jens Axboe62755e32019-10-28 21:49:21 -06005389 return req->user_data == (unsigned long) data;
5390}
5391
Jens Axboee977d6d2019-11-05 12:39:45 -07005392static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005393{
Jens Axboe62755e32019-10-28 21:49:21 -06005394 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005395 int ret = 0;
5396
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005397 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005398 switch (cancel_ret) {
5399 case IO_WQ_CANCEL_OK:
5400 ret = 0;
5401 break;
5402 case IO_WQ_CANCEL_RUNNING:
5403 ret = -EALREADY;
5404 break;
5405 case IO_WQ_CANCEL_NOTFOUND:
5406 ret = -ENOENT;
5407 break;
5408 }
5409
Jens Axboee977d6d2019-11-05 12:39:45 -07005410 return ret;
5411}
5412
Jens Axboe47f46762019-11-09 17:43:02 -07005413static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5414 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005415 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005416{
5417 unsigned long flags;
5418 int ret;
5419
5420 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5421 if (ret != -ENOENT) {
5422 spin_lock_irqsave(&ctx->completion_lock, flags);
5423 goto done;
5424 }
5425
5426 spin_lock_irqsave(&ctx->completion_lock, flags);
5427 ret = io_timeout_cancel(ctx, sqe_addr);
5428 if (ret != -ENOENT)
5429 goto done;
5430 ret = io_poll_cancel(ctx, sqe_addr);
5431done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005432 if (!ret)
5433 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005434 io_cqring_fill_event(req, ret);
5435 io_commit_cqring(ctx);
5436 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5437 io_cqring_ev_posted(ctx);
5438
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005439 if (ret < 0)
5440 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005441 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005442}
5443
Jens Axboe3529d8c2019-12-19 18:24:38 -07005444static int io_async_cancel_prep(struct io_kiocb *req,
5445 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005446{
Jens Axboefbf23842019-12-17 18:45:56 -07005447 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005448 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005449 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5450 return -EINVAL;
5451 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005452 return -EINVAL;
5453
Jens Axboefbf23842019-12-17 18:45:56 -07005454 req->cancel.addr = READ_ONCE(sqe->addr);
5455 return 0;
5456}
5457
Pavel Begunkov014db002020-03-03 21:33:12 +03005458static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005459{
5460 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005461
Pavel Begunkov014db002020-03-03 21:33:12 +03005462 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005463 return 0;
5464}
5465
Jens Axboe05f3fb32019-12-09 11:22:50 -07005466static int io_files_update_prep(struct io_kiocb *req,
5467 const struct io_uring_sqe *sqe)
5468{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005469 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5470 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005471 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5472 return -EINVAL;
5473 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005474 return -EINVAL;
5475
5476 req->files_update.offset = READ_ONCE(sqe->off);
5477 req->files_update.nr_args = READ_ONCE(sqe->len);
5478 if (!req->files_update.nr_args)
5479 return -EINVAL;
5480 req->files_update.arg = READ_ONCE(sqe->addr);
5481 return 0;
5482}
5483
Jens Axboe229a7b62020-06-22 10:13:11 -06005484static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5485 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005486{
5487 struct io_ring_ctx *ctx = req->ctx;
5488 struct io_uring_files_update up;
5489 int ret;
5490
Jens Axboef86cd202020-01-29 13:46:44 -07005491 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005492 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005493
5494 up.offset = req->files_update.offset;
5495 up.fds = req->files_update.arg;
5496
5497 mutex_lock(&ctx->uring_lock);
5498 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5499 mutex_unlock(&ctx->uring_lock);
5500
5501 if (ret < 0)
5502 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005503 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005504 return 0;
5505}
5506
Jens Axboe3529d8c2019-12-19 18:24:38 -07005507static int io_req_defer_prep(struct io_kiocb *req,
5508 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005509{
Jens Axboee7815732019-12-17 19:45:06 -07005510 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005511
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005512 if (!sqe)
5513 return 0;
5514
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005515 if (io_alloc_async_ctx(req))
5516 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005517 ret = io_prep_work_files(req);
5518 if (unlikely(ret))
5519 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005520
Jens Axboe202700e12020-09-12 13:18:10 -06005521 io_prep_async_work(req);
5522
Jens Axboed625c6e2019-12-17 19:53:05 -07005523 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005524 case IORING_OP_NOP:
5525 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005526 case IORING_OP_READV:
5527 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005528 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005529 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005530 break;
5531 case IORING_OP_WRITEV:
5532 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005533 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005534 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005535 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005536 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005537 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005538 break;
5539 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005540 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005541 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005542 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005543 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005544 break;
5545 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005546 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005547 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005548 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005549 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005550 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005551 break;
5552 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005553 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005554 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005555 break;
Jens Axboef499a022019-12-02 16:28:46 -07005556 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005557 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005558 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005559 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005560 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005561 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005562 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005563 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005564 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005565 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005566 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005567 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005568 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005569 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005570 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005571 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005572 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005573 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005574 case IORING_OP_FALLOCATE:
5575 ret = io_fallocate_prep(req, sqe);
5576 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005577 case IORING_OP_OPENAT:
5578 ret = io_openat_prep(req, sqe);
5579 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005580 case IORING_OP_CLOSE:
5581 ret = io_close_prep(req, sqe);
5582 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005583 case IORING_OP_FILES_UPDATE:
5584 ret = io_files_update_prep(req, sqe);
5585 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005586 case IORING_OP_STATX:
5587 ret = io_statx_prep(req, sqe);
5588 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005589 case IORING_OP_FADVISE:
5590 ret = io_fadvise_prep(req, sqe);
5591 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005592 case IORING_OP_MADVISE:
5593 ret = io_madvise_prep(req, sqe);
5594 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005595 case IORING_OP_OPENAT2:
5596 ret = io_openat2_prep(req, sqe);
5597 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005598 case IORING_OP_EPOLL_CTL:
5599 ret = io_epoll_ctl_prep(req, sqe);
5600 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005601 case IORING_OP_SPLICE:
5602 ret = io_splice_prep(req, sqe);
5603 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005604 case IORING_OP_PROVIDE_BUFFERS:
5605 ret = io_provide_buffers_prep(req, sqe);
5606 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005607 case IORING_OP_REMOVE_BUFFERS:
5608 ret = io_remove_buffers_prep(req, sqe);
5609 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005610 case IORING_OP_TEE:
5611 ret = io_tee_prep(req, sqe);
5612 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005613 default:
Jens Axboee7815732019-12-17 19:45:06 -07005614 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5615 req->opcode);
5616 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005617 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005618 }
5619
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005620 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005621}
5622
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005623static u32 io_get_sequence(struct io_kiocb *req)
5624{
5625 struct io_kiocb *pos;
5626 struct io_ring_ctx *ctx = req->ctx;
5627 u32 total_submitted, nr_reqs = 1;
5628
5629 if (req->flags & REQ_F_LINK_HEAD)
5630 list_for_each_entry(pos, &req->link_list, link_list)
5631 nr_reqs++;
5632
5633 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5634 return total_submitted - nr_reqs;
5635}
5636
Jens Axboe3529d8c2019-12-19 18:24:38 -07005637static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005638{
Jackie Liua197f662019-11-08 08:09:12 -07005639 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005640 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005641 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005642 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005643
Bob Liu9d858b22019-11-13 18:06:25 +08005644 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005645 if (likely(list_empty_careful(&ctx->defer_list) &&
5646 !(req->flags & REQ_F_IO_DRAIN)))
5647 return 0;
5648
5649 seq = io_get_sequence(req);
5650 /* Still a chance to pass the sequence check */
5651 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005652 return 0;
5653
Pavel Begunkov650b5482020-05-17 14:02:11 +03005654 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005655 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005656 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005657 return ret;
5658 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005659 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005660 de = kmalloc(sizeof(*de), GFP_KERNEL);
5661 if (!de)
5662 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005663
Jens Axboede0617e2019-04-06 21:51:27 -06005664 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005665 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005666 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005667 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005668 io_queue_async_work(req);
5669 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005670 }
5671
Jens Axboe915967f2019-11-21 09:01:20 -07005672 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005673 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005674 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005675 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005676 spin_unlock_irq(&ctx->completion_lock);
5677 return -EIOCBQUEUED;
5678}
5679
Jens Axboef573d382020-09-22 10:19:24 -06005680static void io_req_drop_files(struct io_kiocb *req)
5681{
5682 struct io_ring_ctx *ctx = req->ctx;
5683 unsigned long flags;
5684
5685 spin_lock_irqsave(&ctx->inflight_lock, flags);
5686 list_del(&req->inflight_entry);
5687 if (waitqueue_active(&ctx->inflight_wait))
5688 wake_up(&ctx->inflight_wait);
5689 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5690 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005691 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005692 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005693 req->work.files = NULL;
5694}
5695
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005696static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005697{
5698 struct io_async_ctx *io = req->io;
5699
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005700 if (req->flags & REQ_F_BUFFER_SELECTED) {
5701 switch (req->opcode) {
5702 case IORING_OP_READV:
5703 case IORING_OP_READ_FIXED:
5704 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005705 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005706 break;
5707 case IORING_OP_RECVMSG:
5708 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005709 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005710 break;
5711 }
5712 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005713 }
5714
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005715 if (req->flags & REQ_F_NEED_CLEANUP) {
5716 switch (req->opcode) {
5717 case IORING_OP_READV:
5718 case IORING_OP_READ_FIXED:
5719 case IORING_OP_READ:
5720 case IORING_OP_WRITEV:
5721 case IORING_OP_WRITE_FIXED:
5722 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005723 if (io->rw.free_iovec)
5724 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005725 break;
5726 case IORING_OP_RECVMSG:
5727 case IORING_OP_SENDMSG:
5728 if (io->msg.iov != io->msg.fast_iov)
5729 kfree(io->msg.iov);
5730 break;
5731 case IORING_OP_SPLICE:
5732 case IORING_OP_TEE:
5733 io_put_file(req, req->splice.file_in,
5734 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5735 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005736 case IORING_OP_OPENAT:
5737 case IORING_OP_OPENAT2:
5738 if (req->open.filename)
5739 putname(req->open.filename);
5740 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005741 }
5742 req->flags &= ~REQ_F_NEED_CLEANUP;
5743 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005744
Jens Axboef573d382020-09-22 10:19:24 -06005745 if (req->flags & REQ_F_INFLIGHT)
5746 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005747}
5748
Jens Axboe3529d8c2019-12-19 18:24:38 -07005749static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005750 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005751{
Jackie Liua197f662019-11-08 08:09:12 -07005752 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005753 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005754
Jens Axboed625c6e2019-12-17 19:53:05 -07005755 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005756 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005757 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005758 break;
5759 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005760 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005761 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005762 if (sqe) {
5763 ret = io_read_prep(req, sqe, force_nonblock);
5764 if (ret < 0)
5765 break;
5766 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005767 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005768 break;
5769 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005770 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005771 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005772 if (sqe) {
5773 ret = io_write_prep(req, sqe, force_nonblock);
5774 if (ret < 0)
5775 break;
5776 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005777 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005778 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005779 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005780 if (sqe) {
5781 ret = io_prep_fsync(req, sqe);
5782 if (ret < 0)
5783 break;
5784 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005785 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005786 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005787 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005788 if (sqe) {
5789 ret = io_poll_add_prep(req, sqe);
5790 if (ret)
5791 break;
5792 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005793 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005794 break;
5795 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005796 if (sqe) {
5797 ret = io_poll_remove_prep(req, sqe);
5798 if (ret < 0)
5799 break;
5800 }
Jens Axboefc4df992019-12-10 14:38:45 -07005801 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005802 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005803 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005804 if (sqe) {
5805 ret = io_prep_sfr(req, sqe);
5806 if (ret < 0)
5807 break;
5808 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005809 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005810 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005811 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005812 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005813 if (sqe) {
5814 ret = io_sendmsg_prep(req, sqe);
5815 if (ret < 0)
5816 break;
5817 }
Jens Axboefddafac2020-01-04 20:19:44 -07005818 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005819 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005820 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005821 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005822 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005823 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005824 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005825 if (sqe) {
5826 ret = io_recvmsg_prep(req, sqe);
5827 if (ret)
5828 break;
5829 }
Jens Axboefddafac2020-01-04 20:19:44 -07005830 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005831 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005832 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005833 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005834 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005835 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005836 if (sqe) {
5837 ret = io_timeout_prep(req, sqe, false);
5838 if (ret)
5839 break;
5840 }
Jens Axboefc4df992019-12-10 14:38:45 -07005841 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005842 break;
Jens Axboe11365042019-10-16 09:08:32 -06005843 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005844 if (sqe) {
5845 ret = io_timeout_remove_prep(req, sqe);
5846 if (ret)
5847 break;
5848 }
Jens Axboefc4df992019-12-10 14:38:45 -07005849 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005850 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005851 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005852 if (sqe) {
5853 ret = io_accept_prep(req, sqe);
5854 if (ret)
5855 break;
5856 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005857 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005858 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005859 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005860 if (sqe) {
5861 ret = io_connect_prep(req, sqe);
5862 if (ret)
5863 break;
5864 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005865 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005866 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005867 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005868 if (sqe) {
5869 ret = io_async_cancel_prep(req, sqe);
5870 if (ret)
5871 break;
5872 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005873 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005874 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005875 case IORING_OP_FALLOCATE:
5876 if (sqe) {
5877 ret = io_fallocate_prep(req, sqe);
5878 if (ret)
5879 break;
5880 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005881 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005882 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005883 case IORING_OP_OPENAT:
5884 if (sqe) {
5885 ret = io_openat_prep(req, sqe);
5886 if (ret)
5887 break;
5888 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005889 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005890 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005891 case IORING_OP_CLOSE:
5892 if (sqe) {
5893 ret = io_close_prep(req, sqe);
5894 if (ret)
5895 break;
5896 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005897 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005898 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005899 case IORING_OP_FILES_UPDATE:
5900 if (sqe) {
5901 ret = io_files_update_prep(req, sqe);
5902 if (ret)
5903 break;
5904 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005905 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005906 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005907 case IORING_OP_STATX:
5908 if (sqe) {
5909 ret = io_statx_prep(req, sqe);
5910 if (ret)
5911 break;
5912 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005913 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005914 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005915 case IORING_OP_FADVISE:
5916 if (sqe) {
5917 ret = io_fadvise_prep(req, sqe);
5918 if (ret)
5919 break;
5920 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005921 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005922 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005923 case IORING_OP_MADVISE:
5924 if (sqe) {
5925 ret = io_madvise_prep(req, sqe);
5926 if (ret)
5927 break;
5928 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005929 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005930 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005931 case IORING_OP_OPENAT2:
5932 if (sqe) {
5933 ret = io_openat2_prep(req, sqe);
5934 if (ret)
5935 break;
5936 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005937 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005938 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005939 case IORING_OP_EPOLL_CTL:
5940 if (sqe) {
5941 ret = io_epoll_ctl_prep(req, sqe);
5942 if (ret)
5943 break;
5944 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005945 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005946 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005947 case IORING_OP_SPLICE:
5948 if (sqe) {
5949 ret = io_splice_prep(req, sqe);
5950 if (ret < 0)
5951 break;
5952 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005953 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005954 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005955 case IORING_OP_PROVIDE_BUFFERS:
5956 if (sqe) {
5957 ret = io_provide_buffers_prep(req, sqe);
5958 if (ret)
5959 break;
5960 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005961 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005962 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005963 case IORING_OP_REMOVE_BUFFERS:
5964 if (sqe) {
5965 ret = io_remove_buffers_prep(req, sqe);
5966 if (ret)
5967 break;
5968 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005969 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005970 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005971 case IORING_OP_TEE:
5972 if (sqe) {
5973 ret = io_tee_prep(req, sqe);
5974 if (ret < 0)
5975 break;
5976 }
5977 ret = io_tee(req, force_nonblock);
5978 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005979 default:
5980 ret = -EINVAL;
5981 break;
5982 }
5983
5984 if (ret)
5985 return ret;
5986
Jens Axboeb5325762020-05-19 21:20:27 -06005987 /* If the op doesn't have a file, we're not polling for it */
5988 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005989 const bool in_async = io_wq_current_is_worker();
5990
Jens Axboe11ba8202020-01-15 21:51:17 -07005991 /* workqueue context doesn't hold uring_lock, grab it now */
5992 if (in_async)
5993 mutex_lock(&ctx->uring_lock);
5994
Jens Axboe2b188cc2019-01-07 10:46:33 -07005995 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005996
5997 if (in_async)
5998 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005999 }
6000
6001 return 0;
6002}
6003
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006004static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006005{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006006 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006007 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006008 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006009
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006010 timeout = io_prep_linked_timeout(req);
6011 if (timeout)
6012 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006013
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006014 /* if NO_CANCEL is set, we must still run the work */
6015 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6016 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006017 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006018 }
Jens Axboe31b51512019-01-18 22:56:34 -07006019
Jens Axboe561fb042019-10-24 07:25:42 -06006020 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006021 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006022 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006023 /*
6024 * We can get EAGAIN for polled IO even though we're
6025 * forcing a sync submission from here, since we can't
6026 * wait for request slots on the block side.
6027 */
6028 if (ret != -EAGAIN)
6029 break;
6030 cond_resched();
6031 } while (1);
6032 }
Jens Axboe31b51512019-01-18 22:56:34 -07006033
Jens Axboe561fb042019-10-24 07:25:42 -06006034 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006035 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006036 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006037 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006038
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006039 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006040}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006041
Jens Axboe65e19f52019-10-26 07:20:21 -06006042static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6043 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006044{
Jens Axboe65e19f52019-10-26 07:20:21 -06006045 struct fixed_file_table *table;
6046
Jens Axboe05f3fb32019-12-09 11:22:50 -07006047 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006048 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006049}
6050
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006051static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6052 int fd, struct file **out_file, bool fixed)
6053{
6054 struct io_ring_ctx *ctx = req->ctx;
6055 struct file *file;
6056
6057 if (fixed) {
6058 if (unlikely(!ctx->file_data ||
6059 (unsigned) fd >= ctx->nr_user_files))
6060 return -EBADF;
6061 fd = array_index_nospec(fd, ctx->nr_user_files);
6062 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006063 if (file) {
6064 req->fixed_file_refs = ctx->file_data->cur_refs;
6065 percpu_ref_get(req->fixed_file_refs);
6066 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006067 } else {
6068 trace_io_uring_file_get(ctx, fd);
6069 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006070 }
6071
Jens Axboefd2206e2020-06-02 16:40:47 -06006072 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6073 *out_file = file;
6074 return 0;
6075 }
6076 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006077}
6078
Jens Axboe3529d8c2019-12-19 18:24:38 -07006079static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006080 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006081{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006082 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006083
Jens Axboe63ff8222020-05-07 14:56:15 -06006084 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006085 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006086 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006087
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006088 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006089}
6090
Jackie Liua197f662019-11-08 08:09:12 -07006091static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006092{
Jackie Liua197f662019-11-08 08:09:12 -07006093 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006094
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006095 io_req_init_async(req);
6096
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006097 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006098 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006099
Jens Axboe0f212202020-09-13 13:09:39 -06006100 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006101 get_nsproxy(current->nsproxy);
6102 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006103 req->flags |= REQ_F_INFLIGHT;
6104
Jens Axboefcb323c2019-10-24 12:39:47 -06006105 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006106 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006107 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006108 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006109}
6110
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006111static inline int io_prep_work_files(struct io_kiocb *req)
6112{
6113 if (!io_op_defs[req->opcode].file_table)
6114 return 0;
6115 return io_grab_files(req);
6116}
6117
Jens Axboe2665abf2019-11-05 12:40:47 -07006118static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6119{
Jens Axboead8a48a2019-11-15 08:49:11 -07006120 struct io_timeout_data *data = container_of(timer,
6121 struct io_timeout_data, timer);
6122 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006123 struct io_ring_ctx *ctx = req->ctx;
6124 struct io_kiocb *prev = NULL;
6125 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006126
6127 spin_lock_irqsave(&ctx->completion_lock, flags);
6128
6129 /*
6130 * We don't expect the list to be empty, that will only happen if we
6131 * race with the completion of the linked work.
6132 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006133 if (!list_empty(&req->link_list)) {
6134 prev = list_entry(req->link_list.prev, struct io_kiocb,
6135 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006136 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006137 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006138 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6139 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006140 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006141 }
6142
6143 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6144
6145 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006146 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006147 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006148 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006149 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006150 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006151 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006152 return HRTIMER_NORESTART;
6153}
6154
Jens Axboe7271ef32020-08-10 09:55:22 -06006155static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006156{
Jens Axboe76a46e02019-11-10 23:34:16 -07006157 /*
6158 * If the list is now empty, then our linked request finished before
6159 * we got a chance to setup the timer
6160 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006161 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006162 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006163
Jens Axboead8a48a2019-11-15 08:49:11 -07006164 data->timer.function = io_link_timeout_fn;
6165 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6166 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006167 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006168}
6169
6170static void io_queue_linked_timeout(struct io_kiocb *req)
6171{
6172 struct io_ring_ctx *ctx = req->ctx;
6173
6174 spin_lock_irq(&ctx->completion_lock);
6175 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006176 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006177
Jens Axboe2665abf2019-11-05 12:40:47 -07006178 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006179 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006180}
6181
Jens Axboead8a48a2019-11-15 08:49:11 -07006182static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006183{
6184 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006185
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006186 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006187 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006188 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006189 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006190
Pavel Begunkov44932332019-12-05 16:16:35 +03006191 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6192 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006193 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006194 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006195
Jens Axboe76a46e02019-11-10 23:34:16 -07006196 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006197 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006198}
6199
Jens Axboef13fad72020-06-22 09:34:30 -06006200static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6201 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006202{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006203 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006204 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006205 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206 int ret;
6207
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006208again:
6209 linked_timeout = io_prep_linked_timeout(req);
6210
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006211 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6212 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006213 if (old_creds)
6214 revert_creds(old_creds);
6215 if (old_creds == req->work.creds)
6216 old_creds = NULL; /* restored original creds */
6217 else
6218 old_creds = override_creds(req->work.creds);
6219 }
6220
Jens Axboef13fad72020-06-22 09:34:30 -06006221 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006222
6223 /*
6224 * We async punt it if the file wasn't marked NOWAIT, or if the file
6225 * doesn't support non-blocking read/write attempts
6226 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006227 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006228 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006229punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006230 ret = io_prep_work_files(req);
6231 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006232 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006233 /*
6234 * Queued up for async execution, worker will release
6235 * submit reference when the iocb is actually submitted.
6236 */
6237 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006238 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006239
Pavel Begunkovf063c542020-07-25 14:41:59 +03006240 if (linked_timeout)
6241 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006242 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006243 }
Jens Axboee65ef562019-03-12 10:16:44 -06006244
Pavel Begunkov652532a2020-07-03 22:15:07 +03006245 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006246err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006247 /* un-prep timeout, so it'll be killed as any other linked */
6248 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006249 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006250 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006251 io_req_complete(req, ret);
6252 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006253 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006254
Jens Axboe6c271ce2019-01-10 11:22:30 -07006255 /* drop submission reference */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03006256 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006257 if (linked_timeout)
6258 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006259
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006260 if (nxt) {
6261 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006262
6263 if (req->flags & REQ_F_FORCE_ASYNC)
6264 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006265 goto again;
6266 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006267exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006268 if (old_creds)
6269 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006270}
6271
Jens Axboef13fad72020-06-22 09:34:30 -06006272static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6273 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006274{
6275 int ret;
6276
Jens Axboe3529d8c2019-12-19 18:24:38 -07006277 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006278 if (ret) {
6279 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006280fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006281 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006282 io_put_req(req);
6283 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006284 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006285 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006286 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006287 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006288 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006289 goto fail_req;
6290 }
6291
Jens Axboece35a472019-12-17 08:04:44 -07006292 /*
6293 * Never try inline submit of IOSQE_ASYNC is set, go straight
6294 * to async execution.
6295 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006296 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006297 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6298 io_queue_async_work(req);
6299 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006300 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006301 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006302}
6303
Jens Axboef13fad72020-06-22 09:34:30 -06006304static inline void io_queue_link_head(struct io_kiocb *req,
6305 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006306{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006307 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006308 io_put_req(req);
6309 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006310 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006311 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006312}
6313
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006314static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006315 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006316{
Jackie Liua197f662019-11-08 08:09:12 -07006317 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006318 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006319
Jens Axboe9e645e112019-05-10 16:07:28 -06006320 /*
6321 * If we already have a head request, queue this one for async
6322 * submittal once the head completes. If we don't have a head but
6323 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6324 * submitted sync once the chain is complete. If none of those
6325 * conditions are true (normal request), then just queue it.
6326 */
6327 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006328 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006329
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006330 /*
6331 * Taking sequential execution of a link, draining both sides
6332 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6333 * requests in the link. So, it drains the head and the
6334 * next after the link request. The last one is done via
6335 * drain_next flag to persist the effect across calls.
6336 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006337 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006338 head->flags |= REQ_F_IO_DRAIN;
6339 ctx->drain_next = 1;
6340 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006341 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006342 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006343 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006344 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006345 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006346 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006347 trace_io_uring_link(ctx, req, head);
6348 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006349
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006350 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006351 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006352 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006353 *link = NULL;
6354 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006355 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006356 if (unlikely(ctx->drain_next)) {
6357 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006358 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006359 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006360 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006361 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006362 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006363
Pavel Begunkov711be032020-01-17 03:57:59 +03006364 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006365 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006366 req->flags |= REQ_F_FAIL_LINK;
6367 *link = req;
6368 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006369 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006370 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006371 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006372
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006373 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006374}
6375
Jens Axboe9a56a232019-01-09 09:06:50 -07006376/*
6377 * Batched submission is done, ensure local IO is flushed out.
6378 */
6379static void io_submit_state_end(struct io_submit_state *state)
6380{
Jens Axboef13fad72020-06-22 09:34:30 -06006381 if (!list_empty(&state->comp.list))
6382 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006383 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006384 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006385 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006386 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006387}
6388
6389/*
6390 * Start submission side cache.
6391 */
6392static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006393 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006394{
6395 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006396 state->comp.nr = 0;
6397 INIT_LIST_HEAD(&state->comp.list);
6398 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006399 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006400 state->file = NULL;
6401 state->ios_left = max_ios;
6402}
6403
Jens Axboe2b188cc2019-01-07 10:46:33 -07006404static void io_commit_sqring(struct io_ring_ctx *ctx)
6405{
Hristo Venev75b28af2019-08-26 17:23:46 +00006406 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006407
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006408 /*
6409 * Ensure any loads from the SQEs are done at this point,
6410 * since once we write the new head, the application could
6411 * write new data to them.
6412 */
6413 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006414}
6415
6416/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006417 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006418 * that is mapped by userspace. This means that care needs to be taken to
6419 * ensure that reads are stable, as we cannot rely on userspace always
6420 * being a good citizen. If members of the sqe are validated and then later
6421 * used, it's important that those reads are done through READ_ONCE() to
6422 * prevent a re-load down the line.
6423 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006424static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006425{
Hristo Venev75b28af2019-08-26 17:23:46 +00006426 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006427 unsigned head;
6428
6429 /*
6430 * The cached sq head (or cq tail) serves two purposes:
6431 *
6432 * 1) allows us to batch the cost of updating the user visible
6433 * head updates.
6434 * 2) allows the kernel side to track the head on its own, even
6435 * though the application is the one updating it.
6436 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006437 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006438 if (likely(head < ctx->sq_entries))
6439 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006440
6441 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006442 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006443 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006444 return NULL;
6445}
6446
6447static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6448{
6449 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006450}
6451
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006452/*
6453 * Check SQE restrictions (opcode and flags).
6454 *
6455 * Returns 'true' if SQE is allowed, 'false' otherwise.
6456 */
6457static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6458 struct io_kiocb *req,
6459 unsigned int sqe_flags)
6460{
6461 if (!ctx->restricted)
6462 return true;
6463
6464 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6465 return false;
6466
6467 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6468 ctx->restrictions.sqe_flags_required)
6469 return false;
6470
6471 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6472 ctx->restrictions.sqe_flags_required))
6473 return false;
6474
6475 return true;
6476}
6477
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006478#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6479 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6480 IOSQE_BUFFER_SELECT)
6481
6482static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6483 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006484 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006485{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006486 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006487 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006488
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006489 req->opcode = READ_ONCE(sqe->opcode);
6490 req->user_data = READ_ONCE(sqe->user_data);
6491 req->io = NULL;
6492 req->file = NULL;
6493 req->ctx = ctx;
6494 req->flags = 0;
6495 /* one is dropped after submission, the other at completion */
6496 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006497 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006498 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006499 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006500 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006501
6502 if (unlikely(req->opcode >= IORING_OP_LAST))
6503 return -EINVAL;
6504
Jens Axboe9d8426a2020-06-16 18:42:49 -06006505 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6506 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006507
6508 sqe_flags = READ_ONCE(sqe->flags);
6509 /* enforce forwards compatibility on users */
6510 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6511 return -EINVAL;
6512
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006513 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6514 return -EACCES;
6515
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006516 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6517 !io_op_defs[req->opcode].buffer_select)
6518 return -EOPNOTSUPP;
6519
6520 id = READ_ONCE(sqe->personality);
6521 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006522 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006523 req->work.creds = idr_find(&ctx->personality_idr, id);
6524 if (unlikely(!req->work.creds))
6525 return -EINVAL;
6526 get_cred(req->work.creds);
6527 }
6528
6529 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006530 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006531
Jens Axboe63ff8222020-05-07 14:56:15 -06006532 if (!io_op_defs[req->opcode].needs_file)
6533 return 0;
6534
6535 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006536}
6537
Jens Axboe0f212202020-09-13 13:09:39 -06006538static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006539{
Jens Axboeac8691c2020-06-01 08:30:41 -06006540 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006541 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006542 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006543
Jens Axboec4a2ed72019-11-21 21:01:26 -07006544 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006545 if (test_bit(0, &ctx->sq_check_overflow)) {
6546 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006547 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006548 return -EBUSY;
6549 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006550
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006551 /* make sure SQ entry isn't read before tail */
6552 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006553
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006554 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6555 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006556
Jens Axboe013538b2020-06-22 09:29:15 -06006557 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006558
6559 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006560 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006561 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006562 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006563
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006564 sqe = io_get_sqe(ctx);
6565 if (unlikely(!sqe)) {
6566 io_consume_sqe(ctx);
6567 break;
6568 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006569 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006570 if (unlikely(!req)) {
6571 if (!submitted)
6572 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006573 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006574 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006575
Jens Axboeac8691c2020-06-01 08:30:41 -06006576 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006577 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006578 /* will complete beyond this point, count as submitted */
6579 submitted++;
6580
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006581 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006582fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006583 io_put_req(req);
6584 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006585 break;
6586 }
6587
Jens Axboe354420f2020-01-08 18:55:15 -07006588 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006589 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006590 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006591 if (err)
6592 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006593 }
6594
Pavel Begunkov9466f432020-01-25 22:34:01 +03006595 if (unlikely(submitted != nr)) {
6596 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6597
6598 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6599 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006600 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006601 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006602 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006603
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006604 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6605 io_commit_sqring(ctx);
6606
Jens Axboe6c271ce2019-01-10 11:22:30 -07006607 return submitted;
6608}
6609
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006610static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6611{
6612 /* Tell userspace we may need a wakeup call */
6613 spin_lock_irq(&ctx->completion_lock);
6614 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6615 spin_unlock_irq(&ctx->completion_lock);
6616}
6617
6618static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6619{
6620 spin_lock_irq(&ctx->completion_lock);
6621 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6622 spin_unlock_irq(&ctx->completion_lock);
6623}
6624
Jens Axboe6c271ce2019-01-10 11:22:30 -07006625static int io_sq_thread(void *data)
6626{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006627 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006628 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006629 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006630 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006631 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006632
Jens Axboe0f158b42020-05-14 17:18:39 -06006633 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006634
Jens Axboe181e4482019-11-25 08:52:30 -07006635 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006636
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006637 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006638 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006639 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006640
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006641 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006642 unsigned nr_events = 0;
6643
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006644 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006645 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006646 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006647 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006648 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006649 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006650 }
6651
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006652 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006653
6654 /*
6655 * If submit got -EBUSY, flag us as needing the application
6656 * to enter the kernel to reap and flush events.
6657 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006658 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006659 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006660 * Drop cur_mm before scheduling, we can't hold it for
6661 * long periods (or over schedule()). Do this before
6662 * adding ourselves to the waitqueue, as the unuse/drop
6663 * may sleep.
6664 */
Jens Axboe4349f302020-07-09 15:07:01 -06006665 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006666
6667 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006668 * We're polling. If we're within the defined idle
6669 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006670 * to sleep. The exception is if we got EBUSY doing
6671 * more IO, we should wait for the application to
6672 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006673 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006674 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006675 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6676 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006677 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006678 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006679 continue;
6680 }
6681
Jens Axboe6c271ce2019-01-10 11:22:30 -07006682 prepare_to_wait(&ctx->sqo_wait, &wait,
6683 TASK_INTERRUPTIBLE);
6684
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006685 /*
6686 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006687 * to check if there are new reqs added to iopoll_list,
6688 * it is because reqs may have been punted to io worker
6689 * and will be added to iopoll_list later, hence check
6690 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006691 */
6692 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006693 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006694 finish_wait(&ctx->sqo_wait, &wait);
6695 continue;
6696 }
6697
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006698 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006699
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006700 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006701 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006702 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006703 finish_wait(&ctx->sqo_wait, &wait);
6704 break;
6705 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006706 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006707 finish_wait(&ctx->sqo_wait, &wait);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006708 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006709 continue;
6710 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006711 if (signal_pending(current))
6712 flush_signals(current);
6713 schedule();
6714 finish_wait(&ctx->sqo_wait, &wait);
6715
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006716 io_ring_clear_wakeup_flag(ctx);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006717 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006718 continue;
6719 }
6720 finish_wait(&ctx->sqo_wait, &wait);
6721
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006722 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006723 }
6724
Jens Axboe8a4955f2019-12-09 14:52:35 -07006725 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006726 if (likely(!percpu_ref_is_dying(&ctx->refs)))
Jens Axboe0f212202020-09-13 13:09:39 -06006727 ret = io_submit_sqes(ctx, to_submit);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006728 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006729 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006730 }
6731
Jens Axboe4c6e2772020-07-01 11:29:10 -06006732 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006733
Jens Axboe4349f302020-07-09 15:07:01 -06006734 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006735 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006736
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006737 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006738
Jens Axboe6c271ce2019-01-10 11:22:30 -07006739 return 0;
6740}
6741
Jens Axboebda52162019-09-24 13:47:15 -06006742struct io_wait_queue {
6743 struct wait_queue_entry wq;
6744 struct io_ring_ctx *ctx;
6745 unsigned to_wait;
6746 unsigned nr_timeouts;
6747};
6748
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006749static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006750{
6751 struct io_ring_ctx *ctx = iowq->ctx;
6752
6753 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006754 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006755 * started waiting. For timeouts, we always want to return to userspace,
6756 * regardless of event count.
6757 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006758 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006759 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6760}
6761
6762static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6763 int wake_flags, void *key)
6764{
6765 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6766 wq);
6767
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006768 /* use noflush == true, as we can't safely rely on locking context */
6769 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006770 return -1;
6771
6772 return autoremove_wake_function(curr, mode, wake_flags, key);
6773}
6774
Jens Axboe2b188cc2019-01-07 10:46:33 -07006775/*
6776 * Wait until events become available, if we don't already have some. The
6777 * application must reap them itself, as they reside on the shared cq ring.
6778 */
6779static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6780 const sigset_t __user *sig, size_t sigsz)
6781{
Jens Axboebda52162019-09-24 13:47:15 -06006782 struct io_wait_queue iowq = {
6783 .wq = {
6784 .private = current,
6785 .func = io_wake_function,
6786 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6787 },
6788 .ctx = ctx,
6789 .to_wait = min_events,
6790 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006791 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006792 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006793
Jens Axboeb41e9852020-02-17 09:52:41 -07006794 do {
6795 if (io_cqring_events(ctx, false) >= min_events)
6796 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006797 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006798 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006799 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006800
6801 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006802#ifdef CONFIG_COMPAT
6803 if (in_compat_syscall())
6804 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006805 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006806 else
6807#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006808 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006809
Jens Axboe2b188cc2019-01-07 10:46:33 -07006810 if (ret)
6811 return ret;
6812 }
6813
Jens Axboebda52162019-09-24 13:47:15 -06006814 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006815 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006816 do {
6817 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6818 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006819 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006820 if (io_run_task_work())
6821 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006822 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006823 if (current->jobctl & JOBCTL_TASK_WORK) {
6824 spin_lock_irq(&current->sighand->siglock);
6825 current->jobctl &= ~JOBCTL_TASK_WORK;
6826 recalc_sigpending();
6827 spin_unlock_irq(&current->sighand->siglock);
6828 continue;
6829 }
6830 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006831 break;
6832 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006833 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006834 break;
6835 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006836 } while (1);
6837 finish_wait(&ctx->wait, &iowq.wq);
6838
Jens Axboeb7db41c2020-07-04 08:55:50 -06006839 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006840
Hristo Venev75b28af2019-08-26 17:23:46 +00006841 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006842}
6843
Jens Axboe6b063142019-01-10 22:13:58 -07006844static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6845{
6846#if defined(CONFIG_UNIX)
6847 if (ctx->ring_sock) {
6848 struct sock *sock = ctx->ring_sock->sk;
6849 struct sk_buff *skb;
6850
6851 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6852 kfree_skb(skb);
6853 }
6854#else
6855 int i;
6856
Jens Axboe65e19f52019-10-26 07:20:21 -06006857 for (i = 0; i < ctx->nr_user_files; i++) {
6858 struct file *file;
6859
6860 file = io_file_from_index(ctx, i);
6861 if (file)
6862 fput(file);
6863 }
Jens Axboe6b063142019-01-10 22:13:58 -07006864#endif
6865}
6866
Jens Axboe05f3fb32019-12-09 11:22:50 -07006867static void io_file_ref_kill(struct percpu_ref *ref)
6868{
6869 struct fixed_file_data *data;
6870
6871 data = container_of(ref, struct fixed_file_data, refs);
6872 complete(&data->done);
6873}
6874
Jens Axboe6b063142019-01-10 22:13:58 -07006875static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6876{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006877 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006878 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006879 unsigned nr_tables, i;
6880
Jens Axboe05f3fb32019-12-09 11:22:50 -07006881 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006882 return -ENXIO;
6883
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006884 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006885 if (!list_empty(&data->ref_list))
6886 ref_node = list_first_entry(&data->ref_list,
6887 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006888 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006889 if (ref_node)
6890 percpu_ref_kill(&ref_node->refs);
6891
6892 percpu_ref_kill(&data->refs);
6893
6894 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006895 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006896 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006897
Jens Axboe6b063142019-01-10 22:13:58 -07006898 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006899 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6900 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006901 kfree(data->table[i].files);
6902 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006903 percpu_ref_exit(&data->refs);
6904 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006905 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006906 ctx->nr_user_files = 0;
6907 return 0;
6908}
6909
Jens Axboe6c271ce2019-01-10 11:22:30 -07006910static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6911{
6912 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006913 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006914 /*
6915 * The park is a bit of a work-around, without it we get
6916 * warning spews on shutdown with SQPOLL set and affinity
6917 * set to a single CPU.
6918 */
Jens Axboe06058632019-04-13 09:26:03 -06006919 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006920 kthread_stop(ctx->sqo_thread);
6921 ctx->sqo_thread = NULL;
6922 }
6923}
6924
Jens Axboe6b063142019-01-10 22:13:58 -07006925static void io_finish_async(struct io_ring_ctx *ctx)
6926{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006927 io_sq_thread_stop(ctx);
6928
Jens Axboe561fb042019-10-24 07:25:42 -06006929 if (ctx->io_wq) {
6930 io_wq_destroy(ctx->io_wq);
6931 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006932 }
6933}
6934
6935#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006936/*
6937 * Ensure the UNIX gc is aware of our file set, so we are certain that
6938 * the io_uring can be safely unregistered on process exit, even if we have
6939 * loops in the file referencing.
6940 */
6941static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6942{
6943 struct sock *sk = ctx->ring_sock->sk;
6944 struct scm_fp_list *fpl;
6945 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006946 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006947
Jens Axboe6b063142019-01-10 22:13:58 -07006948 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6949 if (!fpl)
6950 return -ENOMEM;
6951
6952 skb = alloc_skb(0, GFP_KERNEL);
6953 if (!skb) {
6954 kfree(fpl);
6955 return -ENOMEM;
6956 }
6957
6958 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006959
Jens Axboe08a45172019-10-03 08:11:03 -06006960 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006961 fpl->user = get_uid(ctx->user);
6962 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006963 struct file *file = io_file_from_index(ctx, i + offset);
6964
6965 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006966 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006967 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006968 unix_inflight(fpl->user, fpl->fp[nr_files]);
6969 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006970 }
6971
Jens Axboe08a45172019-10-03 08:11:03 -06006972 if (nr_files) {
6973 fpl->max = SCM_MAX_FD;
6974 fpl->count = nr_files;
6975 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006976 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006977 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6978 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006979
Jens Axboe08a45172019-10-03 08:11:03 -06006980 for (i = 0; i < nr_files; i++)
6981 fput(fpl->fp[i]);
6982 } else {
6983 kfree_skb(skb);
6984 kfree(fpl);
6985 }
Jens Axboe6b063142019-01-10 22:13:58 -07006986
6987 return 0;
6988}
6989
6990/*
6991 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6992 * causes regular reference counting to break down. We rely on the UNIX
6993 * garbage collection to take care of this problem for us.
6994 */
6995static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6996{
6997 unsigned left, total;
6998 int ret = 0;
6999
7000 total = 0;
7001 left = ctx->nr_user_files;
7002 while (left) {
7003 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007004
7005 ret = __io_sqe_files_scm(ctx, this_files, total);
7006 if (ret)
7007 break;
7008 left -= this_files;
7009 total += this_files;
7010 }
7011
7012 if (!ret)
7013 return 0;
7014
7015 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007016 struct file *file = io_file_from_index(ctx, total);
7017
7018 if (file)
7019 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007020 total++;
7021 }
7022
7023 return ret;
7024}
7025#else
7026static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7027{
7028 return 0;
7029}
7030#endif
7031
Jens Axboe65e19f52019-10-26 07:20:21 -06007032static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7033 unsigned nr_files)
7034{
7035 int i;
7036
7037 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007038 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007039 unsigned this_files;
7040
7041 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7042 table->files = kcalloc(this_files, sizeof(struct file *),
7043 GFP_KERNEL);
7044 if (!table->files)
7045 break;
7046 nr_files -= this_files;
7047 }
7048
7049 if (i == nr_tables)
7050 return 0;
7051
7052 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007053 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007054 kfree(table->files);
7055 }
7056 return 1;
7057}
7058
Jens Axboe05f3fb32019-12-09 11:22:50 -07007059static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007060{
7061#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007062 struct sock *sock = ctx->ring_sock->sk;
7063 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7064 struct sk_buff *skb;
7065 int i;
7066
7067 __skb_queue_head_init(&list);
7068
7069 /*
7070 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7071 * remove this entry and rearrange the file array.
7072 */
7073 skb = skb_dequeue(head);
7074 while (skb) {
7075 struct scm_fp_list *fp;
7076
7077 fp = UNIXCB(skb).fp;
7078 for (i = 0; i < fp->count; i++) {
7079 int left;
7080
7081 if (fp->fp[i] != file)
7082 continue;
7083
7084 unix_notinflight(fp->user, fp->fp[i]);
7085 left = fp->count - 1 - i;
7086 if (left) {
7087 memmove(&fp->fp[i], &fp->fp[i + 1],
7088 left * sizeof(struct file *));
7089 }
7090 fp->count--;
7091 if (!fp->count) {
7092 kfree_skb(skb);
7093 skb = NULL;
7094 } else {
7095 __skb_queue_tail(&list, skb);
7096 }
7097 fput(file);
7098 file = NULL;
7099 break;
7100 }
7101
7102 if (!file)
7103 break;
7104
7105 __skb_queue_tail(&list, skb);
7106
7107 skb = skb_dequeue(head);
7108 }
7109
7110 if (skb_peek(&list)) {
7111 spin_lock_irq(&head->lock);
7112 while ((skb = __skb_dequeue(&list)) != NULL)
7113 __skb_queue_tail(head, skb);
7114 spin_unlock_irq(&head->lock);
7115 }
7116#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007117 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007118#endif
7119}
7120
Jens Axboe05f3fb32019-12-09 11:22:50 -07007121struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007122 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007123 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007124};
7125
Jens Axboe4a38aed22020-05-14 17:21:15 -06007126static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007127{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007128 struct fixed_file_data *file_data = ref_node->file_data;
7129 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007130 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007131
7132 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007133 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007134 io_ring_file_put(ctx, pfile->file);
7135 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007136 }
7137
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007138 spin_lock(&file_data->lock);
7139 list_del(&ref_node->node);
7140 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007141
Xiaoguang Wang05589552020-03-31 14:05:18 +08007142 percpu_ref_exit(&ref_node->refs);
7143 kfree(ref_node);
7144 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007145}
7146
Jens Axboe4a38aed22020-05-14 17:21:15 -06007147static void io_file_put_work(struct work_struct *work)
7148{
7149 struct io_ring_ctx *ctx;
7150 struct llist_node *node;
7151
7152 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7153 node = llist_del_all(&ctx->file_put_llist);
7154
7155 while (node) {
7156 struct fixed_file_ref_node *ref_node;
7157 struct llist_node *next = node->next;
7158
7159 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7160 __io_file_put_work(ref_node);
7161 node = next;
7162 }
7163}
7164
Jens Axboe05f3fb32019-12-09 11:22:50 -07007165static void io_file_data_ref_zero(struct percpu_ref *ref)
7166{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007167 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007168 struct io_ring_ctx *ctx;
7169 bool first_add;
7170 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007171
Xiaoguang Wang05589552020-03-31 14:05:18 +08007172 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007173 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007174
Jens Axboe4a38aed22020-05-14 17:21:15 -06007175 if (percpu_ref_is_dying(&ctx->file_data->refs))
7176 delay = 0;
7177
7178 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7179 if (!delay)
7180 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7181 else if (first_add)
7182 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007183}
7184
7185static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7186 struct io_ring_ctx *ctx)
7187{
7188 struct fixed_file_ref_node *ref_node;
7189
7190 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7191 if (!ref_node)
7192 return ERR_PTR(-ENOMEM);
7193
7194 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7195 0, GFP_KERNEL)) {
7196 kfree(ref_node);
7197 return ERR_PTR(-ENOMEM);
7198 }
7199 INIT_LIST_HEAD(&ref_node->node);
7200 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007201 ref_node->file_data = ctx->file_data;
7202 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007203}
7204
7205static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7206{
7207 percpu_ref_exit(&ref_node->refs);
7208 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007209}
7210
7211static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7212 unsigned nr_args)
7213{
7214 __s32 __user *fds = (__s32 __user *) arg;
7215 unsigned nr_tables;
7216 struct file *file;
7217 int fd, ret = 0;
7218 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007219 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007220
7221 if (ctx->file_data)
7222 return -EBUSY;
7223 if (!nr_args)
7224 return -EINVAL;
7225 if (nr_args > IORING_MAX_FIXED_FILES)
7226 return -EMFILE;
7227
7228 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7229 if (!ctx->file_data)
7230 return -ENOMEM;
7231 ctx->file_data->ctx = ctx;
7232 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007233 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007234 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007235
7236 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7237 ctx->file_data->table = kcalloc(nr_tables,
7238 sizeof(struct fixed_file_table),
7239 GFP_KERNEL);
7240 if (!ctx->file_data->table) {
7241 kfree(ctx->file_data);
7242 ctx->file_data = NULL;
7243 return -ENOMEM;
7244 }
7245
Xiaoguang Wang05589552020-03-31 14:05:18 +08007246 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007247 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7248 kfree(ctx->file_data->table);
7249 kfree(ctx->file_data);
7250 ctx->file_data = NULL;
7251 return -ENOMEM;
7252 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007253
7254 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7255 percpu_ref_exit(&ctx->file_data->refs);
7256 kfree(ctx->file_data->table);
7257 kfree(ctx->file_data);
7258 ctx->file_data = NULL;
7259 return -ENOMEM;
7260 }
7261
7262 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7263 struct fixed_file_table *table;
7264 unsigned index;
7265
7266 ret = -EFAULT;
7267 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7268 break;
7269 /* allow sparse sets */
7270 if (fd == -1) {
7271 ret = 0;
7272 continue;
7273 }
7274
7275 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7276 index = i & IORING_FILE_TABLE_MASK;
7277 file = fget(fd);
7278
7279 ret = -EBADF;
7280 if (!file)
7281 break;
7282
7283 /*
7284 * Don't allow io_uring instances to be registered. If UNIX
7285 * isn't enabled, then this causes a reference cycle and this
7286 * instance can never get freed. If UNIX is enabled we'll
7287 * handle it just fine, but there's still no point in allowing
7288 * a ring fd as it doesn't support regular read/write anyway.
7289 */
7290 if (file->f_op == &io_uring_fops) {
7291 fput(file);
7292 break;
7293 }
7294 ret = 0;
7295 table->files[index] = file;
7296 }
7297
7298 if (ret) {
7299 for (i = 0; i < ctx->nr_user_files; i++) {
7300 file = io_file_from_index(ctx, i);
7301 if (file)
7302 fput(file);
7303 }
7304 for (i = 0; i < nr_tables; i++)
7305 kfree(ctx->file_data->table[i].files);
7306
Yang Yingliang667e57d2020-07-10 14:14:20 +00007307 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007308 kfree(ctx->file_data->table);
7309 kfree(ctx->file_data);
7310 ctx->file_data = NULL;
7311 ctx->nr_user_files = 0;
7312 return ret;
7313 }
7314
7315 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007316 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007317 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007318 return ret;
7319 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007320
Xiaoguang Wang05589552020-03-31 14:05:18 +08007321 ref_node = alloc_fixed_file_ref_node(ctx);
7322 if (IS_ERR(ref_node)) {
7323 io_sqe_files_unregister(ctx);
7324 return PTR_ERR(ref_node);
7325 }
7326
7327 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007328 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007329 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007330 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007331 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007332 return ret;
7333}
7334
Jens Axboec3a31e62019-10-03 13:59:56 -06007335static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7336 int index)
7337{
7338#if defined(CONFIG_UNIX)
7339 struct sock *sock = ctx->ring_sock->sk;
7340 struct sk_buff_head *head = &sock->sk_receive_queue;
7341 struct sk_buff *skb;
7342
7343 /*
7344 * See if we can merge this file into an existing skb SCM_RIGHTS
7345 * file set. If there's no room, fall back to allocating a new skb
7346 * and filling it in.
7347 */
7348 spin_lock_irq(&head->lock);
7349 skb = skb_peek(head);
7350 if (skb) {
7351 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7352
7353 if (fpl->count < SCM_MAX_FD) {
7354 __skb_unlink(skb, head);
7355 spin_unlock_irq(&head->lock);
7356 fpl->fp[fpl->count] = get_file(file);
7357 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7358 fpl->count++;
7359 spin_lock_irq(&head->lock);
7360 __skb_queue_head(head, skb);
7361 } else {
7362 skb = NULL;
7363 }
7364 }
7365 spin_unlock_irq(&head->lock);
7366
7367 if (skb) {
7368 fput(file);
7369 return 0;
7370 }
7371
7372 return __io_sqe_files_scm(ctx, 1, index);
7373#else
7374 return 0;
7375#endif
7376}
7377
Hillf Dantona5318d32020-03-23 17:47:15 +08007378static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007379 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007380{
Hillf Dantona5318d32020-03-23 17:47:15 +08007381 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007382 struct percpu_ref *refs = data->cur_refs;
7383 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007384
Jens Axboe05f3fb32019-12-09 11:22:50 -07007385 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007386 if (!pfile)
7387 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007388
Xiaoguang Wang05589552020-03-31 14:05:18 +08007389 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007390 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007391 list_add(&pfile->list, &ref_node->file_list);
7392
Hillf Dantona5318d32020-03-23 17:47:15 +08007393 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007394}
7395
7396static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7397 struct io_uring_files_update *up,
7398 unsigned nr_args)
7399{
7400 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007401 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007402 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007403 __s32 __user *fds;
7404 int fd, i, err;
7405 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007406 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007407
Jens Axboe05f3fb32019-12-09 11:22:50 -07007408 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007409 return -EOVERFLOW;
7410 if (done > ctx->nr_user_files)
7411 return -EINVAL;
7412
Xiaoguang Wang05589552020-03-31 14:05:18 +08007413 ref_node = alloc_fixed_file_ref_node(ctx);
7414 if (IS_ERR(ref_node))
7415 return PTR_ERR(ref_node);
7416
Jens Axboec3a31e62019-10-03 13:59:56 -06007417 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007418 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007419 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007420 struct fixed_file_table *table;
7421 unsigned index;
7422
Jens Axboec3a31e62019-10-03 13:59:56 -06007423 err = 0;
7424 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7425 err = -EFAULT;
7426 break;
7427 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007428 i = array_index_nospec(up->offset, ctx->nr_user_files);
7429 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007430 index = i & IORING_FILE_TABLE_MASK;
7431 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007432 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007433 err = io_queue_file_removal(data, file);
7434 if (err)
7435 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007436 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007437 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007438 }
7439 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007440 file = fget(fd);
7441 if (!file) {
7442 err = -EBADF;
7443 break;
7444 }
7445 /*
7446 * Don't allow io_uring instances to be registered. If
7447 * UNIX isn't enabled, then this causes a reference
7448 * cycle and this instance can never get freed. If UNIX
7449 * is enabled we'll handle it just fine, but there's
7450 * still no point in allowing a ring fd as it doesn't
7451 * support regular read/write anyway.
7452 */
7453 if (file->f_op == &io_uring_fops) {
7454 fput(file);
7455 err = -EBADF;
7456 break;
7457 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007458 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007459 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007460 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007461 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007462 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007463 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007464 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007465 }
7466 nr_args--;
7467 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007468 up->offset++;
7469 }
7470
Xiaoguang Wang05589552020-03-31 14:05:18 +08007471 if (needs_switch) {
7472 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007473 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007474 list_add(&ref_node->node, &data->ref_list);
7475 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007476 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007477 percpu_ref_get(&ctx->file_data->refs);
7478 } else
7479 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007480
7481 return done ? done : err;
7482}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007483
Jens Axboe05f3fb32019-12-09 11:22:50 -07007484static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7485 unsigned nr_args)
7486{
7487 struct io_uring_files_update up;
7488
7489 if (!ctx->file_data)
7490 return -ENXIO;
7491 if (!nr_args)
7492 return -EINVAL;
7493 if (copy_from_user(&up, arg, sizeof(up)))
7494 return -EFAULT;
7495 if (up.resv)
7496 return -EINVAL;
7497
7498 return __io_sqe_files_update(ctx, &up, nr_args);
7499}
Jens Axboec3a31e62019-10-03 13:59:56 -06007500
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007501static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007502{
7503 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7504
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007505 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007506 io_put_req(req);
7507}
7508
Pavel Begunkov24369c22020-01-28 03:15:48 +03007509static int io_init_wq_offload(struct io_ring_ctx *ctx,
7510 struct io_uring_params *p)
7511{
7512 struct io_wq_data data;
7513 struct fd f;
7514 struct io_ring_ctx *ctx_attach;
7515 unsigned int concurrency;
7516 int ret = 0;
7517
7518 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007519 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007520 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007521
7522 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7523 /* Do QD, or 4 * CPUS, whatever is smallest */
7524 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7525
7526 ctx->io_wq = io_wq_create(concurrency, &data);
7527 if (IS_ERR(ctx->io_wq)) {
7528 ret = PTR_ERR(ctx->io_wq);
7529 ctx->io_wq = NULL;
7530 }
7531 return ret;
7532 }
7533
7534 f = fdget(p->wq_fd);
7535 if (!f.file)
7536 return -EBADF;
7537
7538 if (f.file->f_op != &io_uring_fops) {
7539 ret = -EINVAL;
7540 goto out_fput;
7541 }
7542
7543 ctx_attach = f.file->private_data;
7544 /* @io_wq is protected by holding the fd */
7545 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7546 ret = -EINVAL;
7547 goto out_fput;
7548 }
7549
7550 ctx->io_wq = ctx_attach->io_wq;
7551out_fput:
7552 fdput(f);
7553 return ret;
7554}
7555
Jens Axboe0f212202020-09-13 13:09:39 -06007556static int io_uring_alloc_task_context(struct task_struct *task)
7557{
7558 struct io_uring_task *tctx;
7559
7560 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7561 if (unlikely(!tctx))
7562 return -ENOMEM;
7563
7564 xa_init(&tctx->xa);
7565 init_waitqueue_head(&tctx->wait);
7566 tctx->last = NULL;
7567 tctx->in_idle = 0;
7568 atomic_long_set(&tctx->req_issue, 0);
7569 atomic_long_set(&tctx->req_complete, 0);
7570 task->io_uring = tctx;
7571 return 0;
7572}
7573
7574void __io_uring_free(struct task_struct *tsk)
7575{
7576 struct io_uring_task *tctx = tsk->io_uring;
7577
7578 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7579 xa_destroy(&tctx->xa);
7580 kfree(tctx);
7581 tsk->io_uring = NULL;
7582}
7583
Jens Axboe6c271ce2019-01-10 11:22:30 -07007584static int io_sq_offload_start(struct io_ring_ctx *ctx,
7585 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007586{
7587 int ret;
7588
Jens Axboe6c271ce2019-01-10 11:22:30 -07007589 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007590 ret = -EPERM;
7591 if (!capable(CAP_SYS_ADMIN))
7592 goto err;
7593
Jens Axboe917257d2019-04-13 09:28:55 -06007594 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7595 if (!ctx->sq_thread_idle)
7596 ctx->sq_thread_idle = HZ;
7597
Jens Axboe6c271ce2019-01-10 11:22:30 -07007598 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007599 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007600
Jens Axboe917257d2019-04-13 09:28:55 -06007601 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007602 if (cpu >= nr_cpu_ids)
7603 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007604 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007605 goto err;
7606
Jens Axboe6c271ce2019-01-10 11:22:30 -07007607 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7608 ctx, cpu,
7609 "io_uring-sq");
7610 } else {
7611 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7612 "io_uring-sq");
7613 }
7614 if (IS_ERR(ctx->sqo_thread)) {
7615 ret = PTR_ERR(ctx->sqo_thread);
7616 ctx->sqo_thread = NULL;
7617 goto err;
7618 }
Jens Axboe0f212202020-09-13 13:09:39 -06007619 ret = io_uring_alloc_task_context(ctx->sqo_thread);
7620 if (ret)
7621 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007622 wake_up_process(ctx->sqo_thread);
7623 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7624 /* Can't have SQ_AFF without SQPOLL */
7625 ret = -EINVAL;
7626 goto err;
7627 }
7628
Pavel Begunkov24369c22020-01-28 03:15:48 +03007629 ret = io_init_wq_offload(ctx, p);
7630 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007631 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007632
7633 return 0;
7634err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007635 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007636 return ret;
7637}
7638
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007639static inline void __io_unaccount_mem(struct user_struct *user,
7640 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007641{
7642 atomic_long_sub(nr_pages, &user->locked_vm);
7643}
7644
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007645static inline int __io_account_mem(struct user_struct *user,
7646 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007647{
7648 unsigned long page_limit, cur_pages, new_pages;
7649
7650 /* Don't allow more pages than we can safely lock */
7651 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7652
7653 do {
7654 cur_pages = atomic_long_read(&user->locked_vm);
7655 new_pages = cur_pages + nr_pages;
7656 if (new_pages > page_limit)
7657 return -ENOMEM;
7658 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7659 new_pages) != cur_pages);
7660
7661 return 0;
7662}
7663
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007664static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7665 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007666{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007667 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007668 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007669
Jens Axboe2aede0e2020-09-14 10:45:53 -06007670 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007671 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007672 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007673 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007674 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007675 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007676}
7677
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007678static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7679 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007680{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007681 int ret;
7682
7683 if (ctx->limit_mem) {
7684 ret = __io_account_mem(ctx->user, nr_pages);
7685 if (ret)
7686 return ret;
7687 }
7688
Jens Axboe2aede0e2020-09-14 10:45:53 -06007689 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007690 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007691 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007692 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007693 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007694 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007695
7696 return 0;
7697}
7698
Jens Axboe2b188cc2019-01-07 10:46:33 -07007699static void io_mem_free(void *ptr)
7700{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007701 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007702
Mark Rutland52e04ef2019-04-30 17:30:21 +01007703 if (!ptr)
7704 return;
7705
7706 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007707 if (put_page_testzero(page))
7708 free_compound_page(page);
7709}
7710
7711static void *io_mem_alloc(size_t size)
7712{
7713 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7714 __GFP_NORETRY;
7715
7716 return (void *) __get_free_pages(gfp_flags, get_order(size));
7717}
7718
Hristo Venev75b28af2019-08-26 17:23:46 +00007719static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7720 size_t *sq_offset)
7721{
7722 struct io_rings *rings;
7723 size_t off, sq_array_size;
7724
7725 off = struct_size(rings, cqes, cq_entries);
7726 if (off == SIZE_MAX)
7727 return SIZE_MAX;
7728
7729#ifdef CONFIG_SMP
7730 off = ALIGN(off, SMP_CACHE_BYTES);
7731 if (off == 0)
7732 return SIZE_MAX;
7733#endif
7734
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007735 if (sq_offset)
7736 *sq_offset = off;
7737
Hristo Venev75b28af2019-08-26 17:23:46 +00007738 sq_array_size = array_size(sizeof(u32), sq_entries);
7739 if (sq_array_size == SIZE_MAX)
7740 return SIZE_MAX;
7741
7742 if (check_add_overflow(off, sq_array_size, &off))
7743 return SIZE_MAX;
7744
Hristo Venev75b28af2019-08-26 17:23:46 +00007745 return off;
7746}
7747
Jens Axboe2b188cc2019-01-07 10:46:33 -07007748static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7749{
Hristo Venev75b28af2019-08-26 17:23:46 +00007750 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007751
Hristo Venev75b28af2019-08-26 17:23:46 +00007752 pages = (size_t)1 << get_order(
7753 rings_size(sq_entries, cq_entries, NULL));
7754 pages += (size_t)1 << get_order(
7755 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007756
Hristo Venev75b28af2019-08-26 17:23:46 +00007757 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007758}
7759
Jens Axboeedafcce2019-01-09 09:16:05 -07007760static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7761{
7762 int i, j;
7763
7764 if (!ctx->user_bufs)
7765 return -ENXIO;
7766
7767 for (i = 0; i < ctx->nr_user_bufs; i++) {
7768 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7769
7770 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007771 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007772
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007773 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007774 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007775 imu->nr_bvecs = 0;
7776 }
7777
7778 kfree(ctx->user_bufs);
7779 ctx->user_bufs = NULL;
7780 ctx->nr_user_bufs = 0;
7781 return 0;
7782}
7783
7784static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7785 void __user *arg, unsigned index)
7786{
7787 struct iovec __user *src;
7788
7789#ifdef CONFIG_COMPAT
7790 if (ctx->compat) {
7791 struct compat_iovec __user *ciovs;
7792 struct compat_iovec ciov;
7793
7794 ciovs = (struct compat_iovec __user *) arg;
7795 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7796 return -EFAULT;
7797
Jens Axboed55e5f52019-12-11 16:12:15 -07007798 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007799 dst->iov_len = ciov.iov_len;
7800 return 0;
7801 }
7802#endif
7803 src = (struct iovec __user *) arg;
7804 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7805 return -EFAULT;
7806 return 0;
7807}
7808
7809static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7810 unsigned nr_args)
7811{
7812 struct vm_area_struct **vmas = NULL;
7813 struct page **pages = NULL;
7814 int i, j, got_pages = 0;
7815 int ret = -EINVAL;
7816
7817 if (ctx->user_bufs)
7818 return -EBUSY;
7819 if (!nr_args || nr_args > UIO_MAXIOV)
7820 return -EINVAL;
7821
7822 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7823 GFP_KERNEL);
7824 if (!ctx->user_bufs)
7825 return -ENOMEM;
7826
7827 for (i = 0; i < nr_args; i++) {
7828 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7829 unsigned long off, start, end, ubuf;
7830 int pret, nr_pages;
7831 struct iovec iov;
7832 size_t size;
7833
7834 ret = io_copy_iov(ctx, &iov, arg, i);
7835 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007836 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007837
7838 /*
7839 * Don't impose further limits on the size and buffer
7840 * constraints here, we'll -EINVAL later when IO is
7841 * submitted if they are wrong.
7842 */
7843 ret = -EFAULT;
7844 if (!iov.iov_base || !iov.iov_len)
7845 goto err;
7846
7847 /* arbitrary limit, but we need something */
7848 if (iov.iov_len > SZ_1G)
7849 goto err;
7850
7851 ubuf = (unsigned long) iov.iov_base;
7852 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7853 start = ubuf >> PAGE_SHIFT;
7854 nr_pages = end - start;
7855
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007856 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007857 if (ret)
7858 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007859
7860 ret = 0;
7861 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007862 kvfree(vmas);
7863 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007864 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007865 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007866 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007867 sizeof(struct vm_area_struct *),
7868 GFP_KERNEL);
7869 if (!pages || !vmas) {
7870 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007871 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007872 goto err;
7873 }
7874 got_pages = nr_pages;
7875 }
7876
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007877 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007878 GFP_KERNEL);
7879 ret = -ENOMEM;
7880 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007881 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007882 goto err;
7883 }
7884
7885 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007886 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007887 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007888 FOLL_WRITE | FOLL_LONGTERM,
7889 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007890 if (pret == nr_pages) {
7891 /* don't support file backed memory */
7892 for (j = 0; j < nr_pages; j++) {
7893 struct vm_area_struct *vma = vmas[j];
7894
7895 if (vma->vm_file &&
7896 !is_file_hugepages(vma->vm_file)) {
7897 ret = -EOPNOTSUPP;
7898 break;
7899 }
7900 }
7901 } else {
7902 ret = pret < 0 ? pret : -EFAULT;
7903 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007904 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007905 if (ret) {
7906 /*
7907 * if we did partial map, or found file backed vmas,
7908 * release any pages we did get
7909 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007910 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007911 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007912 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007913 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007914 goto err;
7915 }
7916
7917 off = ubuf & ~PAGE_MASK;
7918 size = iov.iov_len;
7919 for (j = 0; j < nr_pages; j++) {
7920 size_t vec_len;
7921
7922 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7923 imu->bvec[j].bv_page = pages[j];
7924 imu->bvec[j].bv_len = vec_len;
7925 imu->bvec[j].bv_offset = off;
7926 off = 0;
7927 size -= vec_len;
7928 }
7929 /* store original address for later verification */
7930 imu->ubuf = ubuf;
7931 imu->len = iov.iov_len;
7932 imu->nr_bvecs = nr_pages;
7933
7934 ctx->nr_user_bufs++;
7935 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007936 kvfree(pages);
7937 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007938 return 0;
7939err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007940 kvfree(pages);
7941 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007942 io_sqe_buffer_unregister(ctx);
7943 return ret;
7944}
7945
Jens Axboe9b402842019-04-11 11:45:41 -06007946static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7947{
7948 __s32 __user *fds = arg;
7949 int fd;
7950
7951 if (ctx->cq_ev_fd)
7952 return -EBUSY;
7953
7954 if (copy_from_user(&fd, fds, sizeof(*fds)))
7955 return -EFAULT;
7956
7957 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7958 if (IS_ERR(ctx->cq_ev_fd)) {
7959 int ret = PTR_ERR(ctx->cq_ev_fd);
7960 ctx->cq_ev_fd = NULL;
7961 return ret;
7962 }
7963
7964 return 0;
7965}
7966
7967static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7968{
7969 if (ctx->cq_ev_fd) {
7970 eventfd_ctx_put(ctx->cq_ev_fd);
7971 ctx->cq_ev_fd = NULL;
7972 return 0;
7973 }
7974
7975 return -ENXIO;
7976}
7977
Jens Axboe5a2e7452020-02-23 16:23:11 -07007978static int __io_destroy_buffers(int id, void *p, void *data)
7979{
7980 struct io_ring_ctx *ctx = data;
7981 struct io_buffer *buf = p;
7982
Jens Axboe067524e2020-03-02 16:32:28 -07007983 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007984 return 0;
7985}
7986
7987static void io_destroy_buffers(struct io_ring_ctx *ctx)
7988{
7989 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7990 idr_destroy(&ctx->io_buffer_idr);
7991}
7992
Jens Axboe2b188cc2019-01-07 10:46:33 -07007993static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7994{
Jens Axboe6b063142019-01-10 22:13:58 -07007995 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007996 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06007997
7998 if (ctx->sqo_task) {
7999 put_task_struct(ctx->sqo_task);
8000 ctx->sqo_task = NULL;
8001 mmdrop(ctx->mm_account);
8002 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008003 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008004
Jens Axboe6b063142019-01-10 22:13:58 -07008005 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008006 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008007 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008008 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008009
Jens Axboe2b188cc2019-01-07 10:46:33 -07008010#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008011 if (ctx->ring_sock) {
8012 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008013 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008014 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008015#endif
8016
Hristo Venev75b28af2019-08-26 17:23:46 +00008017 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008018 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008019
8020 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008021 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008022 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008023 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008024 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008025 kfree(ctx);
8026}
8027
8028static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8029{
8030 struct io_ring_ctx *ctx = file->private_data;
8031 __poll_t mask = 0;
8032
8033 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008034 /*
8035 * synchronizes with barrier from wq_has_sleeper call in
8036 * io_commit_cqring
8037 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008038 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00008039 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
8040 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008041 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008042 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008043 mask |= EPOLLIN | EPOLLRDNORM;
8044
8045 return mask;
8046}
8047
8048static int io_uring_fasync(int fd, struct file *file, int on)
8049{
8050 struct io_ring_ctx *ctx = file->private_data;
8051
8052 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8053}
8054
Jens Axboe071698e2020-01-28 10:04:42 -07008055static int io_remove_personalities(int id, void *p, void *data)
8056{
8057 struct io_ring_ctx *ctx = data;
8058 const struct cred *cred;
8059
8060 cred = idr_remove(&ctx->personality_idr, id);
8061 if (cred)
8062 put_cred(cred);
8063 return 0;
8064}
8065
Jens Axboe85faa7b2020-04-09 18:14:00 -06008066static void io_ring_exit_work(struct work_struct *work)
8067{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008068 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8069 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008070
Jens Axboe56952e92020-06-17 15:00:04 -06008071 /*
8072 * If we're doing polled IO and end up having requests being
8073 * submitted async (out-of-line), then completions can come in while
8074 * we're waiting for refs to drop. We need to reap these manually,
8075 * as nobody else will be looking for them.
8076 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008077 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008078 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008079 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008080 io_iopoll_try_reap_events(ctx);
8081 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008082 io_ring_ctx_free(ctx);
8083}
8084
Jens Axboe2b188cc2019-01-07 10:46:33 -07008085static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8086{
8087 mutex_lock(&ctx->uring_lock);
8088 percpu_ref_kill(&ctx->refs);
8089 mutex_unlock(&ctx->uring_lock);
8090
Jens Axboef3606e32020-09-22 08:18:24 -06008091 io_kill_timeouts(ctx, NULL);
8092 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008093
8094 if (ctx->io_wq)
8095 io_wq_cancel_all(ctx->io_wq);
8096
Jens Axboe15dff282019-11-13 09:09:23 -07008097 /* if we failed setting up the ctx, we might not have any rings */
8098 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008099 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008100 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008101 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008102
8103 /*
8104 * Do this upfront, so we won't have a grace period where the ring
8105 * is closed but resources aren't reaped yet. This can cause
8106 * spurious failure in setting up a new ring.
8107 */
Jens Axboe760618f2020-07-24 12:53:31 -06008108 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8109 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008110
Jens Axboe85faa7b2020-04-09 18:14:00 -06008111 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008112 /*
8113 * Use system_unbound_wq to avoid spawning tons of event kworkers
8114 * if we're exiting a ton of rings at the same time. It just adds
8115 * noise and overhead, there's no discernable change in runtime
8116 * over using system_wq.
8117 */
8118 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008119}
8120
8121static int io_uring_release(struct inode *inode, struct file *file)
8122{
8123 struct io_ring_ctx *ctx = file->private_data;
8124
8125 file->private_data = NULL;
8126 io_ring_ctx_wait_and_kill(ctx);
8127 return 0;
8128}
8129
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008130static bool io_wq_files_match(struct io_wq_work *work, void *data)
8131{
8132 struct files_struct *files = data;
8133
Jens Axboe0f212202020-09-13 13:09:39 -06008134 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008135}
8136
Jens Axboef254ac02020-08-12 17:33:30 -06008137/*
8138 * Returns true if 'preq' is the link parent of 'req'
8139 */
8140static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8141{
8142 struct io_kiocb *link;
8143
8144 if (!(preq->flags & REQ_F_LINK_HEAD))
8145 return false;
8146
8147 list_for_each_entry(link, &preq->link_list, link_list) {
8148 if (link == req)
8149 return true;
8150 }
8151
8152 return false;
8153}
8154
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008155static bool io_match_link_files(struct io_kiocb *req,
8156 struct files_struct *files)
8157{
8158 struct io_kiocb *link;
8159
8160 if (io_match_files(req, files))
8161 return true;
8162 if (req->flags & REQ_F_LINK_HEAD) {
8163 list_for_each_entry(link, &req->link_list, link_list) {
8164 if (io_match_files(link, files))
8165 return true;
8166 }
8167 }
8168 return false;
8169}
8170
Jens Axboef254ac02020-08-12 17:33:30 -06008171/*
8172 * We're looking to cancel 'req' because it's holding on to our files, but
8173 * 'req' could be a link to another request. See if it is, and cancel that
8174 * parent request if so.
8175 */
8176static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8177{
8178 struct hlist_node *tmp;
8179 struct io_kiocb *preq;
8180 bool found = false;
8181 int i;
8182
8183 spin_lock_irq(&ctx->completion_lock);
8184 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8185 struct hlist_head *list;
8186
8187 list = &ctx->cancel_hash[i];
8188 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8189 found = io_match_link(preq, req);
8190 if (found) {
8191 io_poll_remove_one(preq);
8192 break;
8193 }
8194 }
8195 }
8196 spin_unlock_irq(&ctx->completion_lock);
8197 return found;
8198}
8199
8200static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8201 struct io_kiocb *req)
8202{
8203 struct io_kiocb *preq;
8204 bool found = false;
8205
8206 spin_lock_irq(&ctx->completion_lock);
8207 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8208 found = io_match_link(preq, req);
8209 if (found) {
8210 __io_timeout_cancel(preq);
8211 break;
8212 }
8213 }
8214 spin_unlock_irq(&ctx->completion_lock);
8215 return found;
8216}
8217
Jens Axboeb711d4e2020-08-16 08:23:05 -07008218static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8219{
8220 return io_match_link(container_of(work, struct io_kiocb, work), data);
8221}
8222
8223static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8224{
8225 enum io_wq_cancel cret;
8226
8227 /* cancel this particular work, if it's running */
8228 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8229 if (cret != IO_WQ_CANCEL_NOTFOUND)
8230 return;
8231
8232 /* find links that hold this pending, cancel those */
8233 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8234 if (cret != IO_WQ_CANCEL_NOTFOUND)
8235 return;
8236
8237 /* if we have a poll link holding this pending, cancel that */
8238 if (io_poll_remove_link(ctx, req))
8239 return;
8240
8241 /* final option, timeout link is holding this req pending */
8242 io_timeout_remove_link(ctx, req);
8243}
8244
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008245static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8246 struct files_struct *files)
8247{
8248 struct io_defer_entry *de = NULL;
8249 LIST_HEAD(list);
8250
8251 spin_lock_irq(&ctx->completion_lock);
8252 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008253 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008254 list_cut_position(&list, &ctx->defer_list, &de->list);
8255 break;
8256 }
8257 }
8258 spin_unlock_irq(&ctx->completion_lock);
8259
8260 while (!list_empty(&list)) {
8261 de = list_first_entry(&list, struct io_defer_entry, list);
8262 list_del_init(&de->list);
8263 req_set_fail_links(de->req);
8264 io_put_req(de->req);
8265 io_req_complete(de->req, -ECANCELED);
8266 kfree(de);
8267 }
8268}
8269
Jens Axboe76e1b642020-09-26 15:05:03 -06008270/*
8271 * Returns true if we found and killed one or more files pinning requests
8272 */
8273static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008274 struct files_struct *files)
8275{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008276 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008277 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008278
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008279 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008280 /* cancel all at once, should be faster than doing it one by one*/
8281 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8282
Jens Axboefcb323c2019-10-24 12:39:47 -06008283 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008284 struct io_kiocb *cancel_req = NULL, *req;
8285 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008286
8287 spin_lock_irq(&ctx->inflight_lock);
8288 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008289 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008290 continue;
8291 /* req is being completed, ignore */
8292 if (!refcount_inc_not_zero(&req->refs))
8293 continue;
8294 cancel_req = req;
8295 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008296 }
Jens Axboe768134d2019-11-10 20:30:53 -07008297 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008298 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008299 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008300 spin_unlock_irq(&ctx->inflight_lock);
8301
Jens Axboe768134d2019-11-10 20:30:53 -07008302 /* We need to keep going until we don't find a matching req */
8303 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008304 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008305 /* cancel this request, or head link requests */
8306 io_attempt_cancel(ctx, cancel_req);
8307 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008308 /* cancellations _may_ trigger task work */
8309 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008310 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008311 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008312 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008313
8314 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008315}
8316
Pavel Begunkov801dd572020-06-15 10:33:14 +03008317static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008318{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008319 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8320 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008321
Jens Axboef3606e32020-09-22 08:18:24 -06008322 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008323}
8324
Jens Axboe0f212202020-09-13 13:09:39 -06008325static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8326 struct task_struct *task,
8327 struct files_struct *files)
8328{
8329 bool ret;
8330
8331 ret = io_uring_cancel_files(ctx, files);
8332 if (!files) {
8333 enum io_wq_cancel cret;
8334
8335 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8336 if (cret != IO_WQ_CANCEL_NOTFOUND)
8337 ret = true;
8338
8339 /* SQPOLL thread does its own polling */
8340 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8341 while (!list_empty_careful(&ctx->iopoll_list)) {
8342 io_iopoll_try_reap_events(ctx);
8343 ret = true;
8344 }
8345 }
8346
8347 ret |= io_poll_remove_all(ctx, task);
8348 ret |= io_kill_timeouts(ctx, task);
8349 }
8350
8351 return ret;
8352}
8353
8354/*
8355 * We need to iteratively cancel requests, in case a request has dependent
8356 * hard links. These persist even for failure of cancelations, hence keep
8357 * looping until none are found.
8358 */
8359static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8360 struct files_struct *files)
8361{
8362 struct task_struct *task = current;
8363
8364 if (ctx->flags & IORING_SETUP_SQPOLL)
8365 task = ctx->sqo_thread;
8366
8367 io_cqring_overflow_flush(ctx, true, task, files);
8368
8369 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8370 io_run_task_work();
8371 cond_resched();
8372 }
8373}
8374
8375/*
8376 * Note that this task has used io_uring. We use it for cancelation purposes.
8377 */
8378static int io_uring_add_task_file(struct file *file)
8379{
8380 if (unlikely(!current->io_uring)) {
8381 int ret;
8382
8383 ret = io_uring_alloc_task_context(current);
8384 if (unlikely(ret))
8385 return ret;
8386 }
8387 if (current->io_uring->last != file) {
8388 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8389 void *old;
8390
8391 rcu_read_lock();
8392 old = xas_load(&xas);
8393 if (old != file) {
8394 get_file(file);
8395 xas_lock(&xas);
8396 xas_store(&xas, file);
8397 xas_unlock(&xas);
8398 }
8399 rcu_read_unlock();
8400 current->io_uring->last = file;
8401 }
8402
8403 return 0;
8404}
8405
8406/*
8407 * Remove this io_uring_file -> task mapping.
8408 */
8409static void io_uring_del_task_file(struct file *file)
8410{
8411 struct io_uring_task *tctx = current->io_uring;
8412 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8413
8414 if (tctx->last == file)
8415 tctx->last = NULL;
8416
8417 xas_lock(&xas);
8418 file = xas_store(&xas, NULL);
8419 xas_unlock(&xas);
8420
8421 if (file)
8422 fput(file);
8423}
8424
8425static void __io_uring_attempt_task_drop(struct file *file)
8426{
8427 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8428 struct file *old;
8429
8430 rcu_read_lock();
8431 old = xas_load(&xas);
8432 rcu_read_unlock();
8433
8434 if (old == file)
8435 io_uring_del_task_file(file);
8436}
8437
8438/*
8439 * Drop task note for this file if we're the only ones that hold it after
8440 * pending fput()
8441 */
8442static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8443{
8444 if (!current->io_uring)
8445 return;
8446 /*
8447 * fput() is pending, will be 2 if the only other ref is our potential
8448 * task file note. If the task is exiting, drop regardless of count.
8449 */
8450 if (!exiting && atomic_long_read(&file->f_count) != 2)
8451 return;
8452
8453 __io_uring_attempt_task_drop(file);
8454}
8455
8456void __io_uring_files_cancel(struct files_struct *files)
8457{
8458 struct io_uring_task *tctx = current->io_uring;
8459 XA_STATE(xas, &tctx->xa, 0);
8460
8461 /* make sure overflow events are dropped */
8462 tctx->in_idle = true;
8463
8464 do {
8465 struct io_ring_ctx *ctx;
8466 struct file *file;
8467
8468 xas_lock(&xas);
8469 file = xas_next_entry(&xas, ULONG_MAX);
8470 xas_unlock(&xas);
8471
8472 if (!file)
8473 break;
8474
8475 ctx = file->private_data;
8476
8477 io_uring_cancel_task_requests(ctx, files);
8478 if (files)
8479 io_uring_del_task_file(file);
8480 } while (1);
8481}
8482
8483static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8484{
8485 return atomic_long_read(&tctx->req_issue) ==
8486 atomic_long_read(&tctx->req_complete);
8487}
8488
8489/*
8490 * Find any io_uring fd that this task has registered or done IO on, and cancel
8491 * requests.
8492 */
8493void __io_uring_task_cancel(void)
8494{
8495 struct io_uring_task *tctx = current->io_uring;
8496 DEFINE_WAIT(wait);
8497 long completions;
8498
8499 /* make sure overflow events are dropped */
8500 tctx->in_idle = true;
8501
8502 while (!io_uring_task_idle(tctx)) {
8503 /* read completions before cancelations */
8504 completions = atomic_long_read(&tctx->req_complete);
8505 __io_uring_files_cancel(NULL);
8506
8507 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8508
8509 /*
8510 * If we've seen completions, retry. This avoids a race where
8511 * a completion comes in before we did prepare_to_wait().
8512 */
8513 if (completions != atomic_long_read(&tctx->req_complete))
8514 continue;
8515 if (io_uring_task_idle(tctx))
8516 break;
8517 schedule();
8518 }
8519
8520 finish_wait(&tctx->wait, &wait);
8521 tctx->in_idle = false;
8522}
8523
Jens Axboefcb323c2019-10-24 12:39:47 -06008524static int io_uring_flush(struct file *file, void *data)
8525{
8526 struct io_ring_ctx *ctx = file->private_data;
8527
Jens Axboe6ab23142020-02-08 20:23:59 -07008528 /*
8529 * If the task is going away, cancel work it may have pending
8530 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008531 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008532 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008533
Jens Axboe0f212202020-09-13 13:09:39 -06008534 io_uring_cancel_task_requests(ctx, data);
8535 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008536 return 0;
8537}
8538
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008539static void *io_uring_validate_mmap_request(struct file *file,
8540 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008541{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008542 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008543 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008544 struct page *page;
8545 void *ptr;
8546
8547 switch (offset) {
8548 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008549 case IORING_OFF_CQ_RING:
8550 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008551 break;
8552 case IORING_OFF_SQES:
8553 ptr = ctx->sq_sqes;
8554 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008555 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008556 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008557 }
8558
8559 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008560 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008561 return ERR_PTR(-EINVAL);
8562
8563 return ptr;
8564}
8565
8566#ifdef CONFIG_MMU
8567
8568static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8569{
8570 size_t sz = vma->vm_end - vma->vm_start;
8571 unsigned long pfn;
8572 void *ptr;
8573
8574 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8575 if (IS_ERR(ptr))
8576 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008577
8578 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8579 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8580}
8581
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008582#else /* !CONFIG_MMU */
8583
8584static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8585{
8586 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8587}
8588
8589static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8590{
8591 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8592}
8593
8594static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8595 unsigned long addr, unsigned long len,
8596 unsigned long pgoff, unsigned long flags)
8597{
8598 void *ptr;
8599
8600 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8601 if (IS_ERR(ptr))
8602 return PTR_ERR(ptr);
8603
8604 return (unsigned long) ptr;
8605}
8606
8607#endif /* !CONFIG_MMU */
8608
Jens Axboe2b188cc2019-01-07 10:46:33 -07008609SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8610 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8611 size_t, sigsz)
8612{
8613 struct io_ring_ctx *ctx;
8614 long ret = -EBADF;
8615 int submitted = 0;
8616 struct fd f;
8617
Jens Axboe4c6e2772020-07-01 11:29:10 -06008618 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008619
Jens Axboe6c271ce2019-01-10 11:22:30 -07008620 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008621 return -EINVAL;
8622
8623 f = fdget(fd);
8624 if (!f.file)
8625 return -EBADF;
8626
8627 ret = -EOPNOTSUPP;
8628 if (f.file->f_op != &io_uring_fops)
8629 goto out_fput;
8630
8631 ret = -ENXIO;
8632 ctx = f.file->private_data;
8633 if (!percpu_ref_tryget(&ctx->refs))
8634 goto out_fput;
8635
Jens Axboe6c271ce2019-01-10 11:22:30 -07008636 /*
8637 * For SQ polling, the thread will do all submissions and completions.
8638 * Just return the requested submit count, and wake the thread if
8639 * we were asked to.
8640 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008641 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008642 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008643 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008644 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008645 if (flags & IORING_ENTER_SQ_WAKEUP)
8646 wake_up(&ctx->sqo_wait);
8647 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008648 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008649 ret = io_uring_add_task_file(f.file);
8650 if (unlikely(ret))
8651 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008652 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008653 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008654 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008655
8656 if (submitted != to_submit)
8657 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008658 }
8659 if (flags & IORING_ENTER_GETEVENTS) {
8660 min_complete = min(min_complete, ctx->cq_entries);
8661
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008662 /*
8663 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8664 * space applications don't need to do io completion events
8665 * polling again, they can rely on io_sq_thread to do polling
8666 * work, which can reduce cpu usage and uring_lock contention.
8667 */
8668 if (ctx->flags & IORING_SETUP_IOPOLL &&
8669 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008670 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008671 } else {
8672 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8673 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008674 }
8675
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008676out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008677 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008678out_fput:
8679 fdput(f);
8680 return submitted ? submitted : ret;
8681}
8682
Tobias Klauserbebdb652020-02-26 18:38:32 +01008683#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008684static int io_uring_show_cred(int id, void *p, void *data)
8685{
8686 const struct cred *cred = p;
8687 struct seq_file *m = data;
8688 struct user_namespace *uns = seq_user_ns(m);
8689 struct group_info *gi;
8690 kernel_cap_t cap;
8691 unsigned __capi;
8692 int g;
8693
8694 seq_printf(m, "%5d\n", id);
8695 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8696 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8697 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8698 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8699 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8700 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8701 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8702 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8703 seq_puts(m, "\n\tGroups:\t");
8704 gi = cred->group_info;
8705 for (g = 0; g < gi->ngroups; g++) {
8706 seq_put_decimal_ull(m, g ? " " : "",
8707 from_kgid_munged(uns, gi->gid[g]));
8708 }
8709 seq_puts(m, "\n\tCapEff:\t");
8710 cap = cred->cap_effective;
8711 CAP_FOR_EACH_U32(__capi)
8712 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8713 seq_putc(m, '\n');
8714 return 0;
8715}
8716
8717static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8718{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008719 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008720 int i;
8721
Jens Axboefad8e0d2020-09-28 08:57:48 -06008722 /*
8723 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8724 * since fdinfo case grabs it in the opposite direction of normal use
8725 * cases. If we fail to get the lock, we just don't iterate any
8726 * structures that could be going away outside the io_uring mutex.
8727 */
8728 has_lock = mutex_trylock(&ctx->uring_lock);
8729
Jens Axboe87ce9552020-01-30 08:25:34 -07008730 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008731 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008732 struct fixed_file_table *table;
8733 struct file *f;
8734
8735 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8736 f = table->files[i & IORING_FILE_TABLE_MASK];
8737 if (f)
8738 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8739 else
8740 seq_printf(m, "%5u: <none>\n", i);
8741 }
8742 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008743 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008744 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8745
8746 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8747 (unsigned int) buf->len);
8748 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008749 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008750 seq_printf(m, "Personalities:\n");
8751 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8752 }
Jens Axboed7718a92020-02-14 22:23:12 -07008753 seq_printf(m, "PollList:\n");
8754 spin_lock_irq(&ctx->completion_lock);
8755 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8756 struct hlist_head *list = &ctx->cancel_hash[i];
8757 struct io_kiocb *req;
8758
8759 hlist_for_each_entry(req, list, hash_node)
8760 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8761 req->task->task_works != NULL);
8762 }
8763 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008764 if (has_lock)
8765 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008766}
8767
8768static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8769{
8770 struct io_ring_ctx *ctx = f->private_data;
8771
8772 if (percpu_ref_tryget(&ctx->refs)) {
8773 __io_uring_show_fdinfo(ctx, m);
8774 percpu_ref_put(&ctx->refs);
8775 }
8776}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008777#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008778
Jens Axboe2b188cc2019-01-07 10:46:33 -07008779static const struct file_operations io_uring_fops = {
8780 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008781 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008782 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008783#ifndef CONFIG_MMU
8784 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8785 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8786#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008787 .poll = io_uring_poll,
8788 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008789#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008790 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008791#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008792};
8793
8794static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8795 struct io_uring_params *p)
8796{
Hristo Venev75b28af2019-08-26 17:23:46 +00008797 struct io_rings *rings;
8798 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008799
Jens Axboebd740482020-08-05 12:58:23 -06008800 /* make sure these are sane, as we already accounted them */
8801 ctx->sq_entries = p->sq_entries;
8802 ctx->cq_entries = p->cq_entries;
8803
Hristo Venev75b28af2019-08-26 17:23:46 +00008804 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8805 if (size == SIZE_MAX)
8806 return -EOVERFLOW;
8807
8808 rings = io_mem_alloc(size);
8809 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008810 return -ENOMEM;
8811
Hristo Venev75b28af2019-08-26 17:23:46 +00008812 ctx->rings = rings;
8813 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8814 rings->sq_ring_mask = p->sq_entries - 1;
8815 rings->cq_ring_mask = p->cq_entries - 1;
8816 rings->sq_ring_entries = p->sq_entries;
8817 rings->cq_ring_entries = p->cq_entries;
8818 ctx->sq_mask = rings->sq_ring_mask;
8819 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008820
8821 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008822 if (size == SIZE_MAX) {
8823 io_mem_free(ctx->rings);
8824 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008825 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008826 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008827
8828 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008829 if (!ctx->sq_sqes) {
8830 io_mem_free(ctx->rings);
8831 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008832 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008833 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008834
Jens Axboe2b188cc2019-01-07 10:46:33 -07008835 return 0;
8836}
8837
8838/*
8839 * Allocate an anonymous fd, this is what constitutes the application
8840 * visible backing of an io_uring instance. The application mmaps this
8841 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8842 * we have to tie this fd to a socket for file garbage collection purposes.
8843 */
8844static int io_uring_get_fd(struct io_ring_ctx *ctx)
8845{
8846 struct file *file;
8847 int ret;
8848
8849#if defined(CONFIG_UNIX)
8850 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8851 &ctx->ring_sock);
8852 if (ret)
8853 return ret;
8854#endif
8855
8856 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8857 if (ret < 0)
8858 goto err;
8859
8860 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8861 O_RDWR | O_CLOEXEC);
8862 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008863err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07008864 put_unused_fd(ret);
8865 ret = PTR_ERR(file);
8866 goto err;
8867 }
8868
8869#if defined(CONFIG_UNIX)
8870 ctx->ring_sock->file = file;
8871#endif
Jens Axboe0f212202020-09-13 13:09:39 -06008872 if (unlikely(io_uring_add_task_file(file))) {
8873 file = ERR_PTR(-ENOMEM);
8874 goto err_fd;
8875 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008876 fd_install(ret, file);
8877 return ret;
8878err:
8879#if defined(CONFIG_UNIX)
8880 sock_release(ctx->ring_sock);
8881 ctx->ring_sock = NULL;
8882#endif
8883 return ret;
8884}
8885
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008886static int io_uring_create(unsigned entries, struct io_uring_params *p,
8887 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008888{
8889 struct user_struct *user = NULL;
8890 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008891 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008892 int ret;
8893
Jens Axboe8110c1a2019-12-28 15:39:54 -07008894 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008895 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008896 if (entries > IORING_MAX_ENTRIES) {
8897 if (!(p->flags & IORING_SETUP_CLAMP))
8898 return -EINVAL;
8899 entries = IORING_MAX_ENTRIES;
8900 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008901
8902 /*
8903 * Use twice as many entries for the CQ ring. It's possible for the
8904 * application to drive a higher depth than the size of the SQ ring,
8905 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008906 * some flexibility in overcommitting a bit. If the application has
8907 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8908 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008909 */
8910 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008911 if (p->flags & IORING_SETUP_CQSIZE) {
8912 /*
8913 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8914 * to a power-of-two, if it isn't already. We do NOT impose
8915 * any cq vs sq ring sizing.
8916 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008917 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008918 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008919 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8920 if (!(p->flags & IORING_SETUP_CLAMP))
8921 return -EINVAL;
8922 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8923 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008924 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8925 } else {
8926 p->cq_entries = 2 * p->sq_entries;
8927 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008928
8929 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008930 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008931
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008932 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008933 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008934 ring_pages(p->sq_entries, p->cq_entries));
8935 if (ret) {
8936 free_uid(user);
8937 return ret;
8938 }
8939 }
8940
8941 ctx = io_ring_ctx_alloc(p);
8942 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008943 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008944 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008945 p->cq_entries));
8946 free_uid(user);
8947 return -ENOMEM;
8948 }
8949 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008950 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008951 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008952
Jens Axboe2aede0e2020-09-14 10:45:53 -06008953 ctx->sqo_task = get_task_struct(current);
8954
8955 /*
8956 * This is just grabbed for accounting purposes. When a process exits,
8957 * the mm is exited and dropped before the files, hence we need to hang
8958 * on to this mm purely for the purposes of being able to unaccount
8959 * memory (locked/pinned vm). It's not used for anything else.
8960 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06008961 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008962 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06008963
Jens Axboef74441e2020-08-05 13:00:44 -06008964 /*
8965 * Account memory _before_ installing the file descriptor. Once
8966 * the descriptor is installed, it can get closed at any time. Also
8967 * do this before hitting the general error path, as ring freeing
8968 * will un-account as well.
8969 */
8970 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8971 ACCT_LOCKED);
8972 ctx->limit_mem = limit_mem;
8973
Jens Axboe2b188cc2019-01-07 10:46:33 -07008974 ret = io_allocate_scq_urings(ctx, p);
8975 if (ret)
8976 goto err;
8977
Jens Axboe6c271ce2019-01-10 11:22:30 -07008978 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008979 if (ret)
8980 goto err;
8981
Jens Axboe2b188cc2019-01-07 10:46:33 -07008982 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008983 p->sq_off.head = offsetof(struct io_rings, sq.head);
8984 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8985 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8986 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8987 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8988 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8989 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008990
8991 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008992 p->cq_off.head = offsetof(struct io_rings, cq.head);
8993 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8994 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8995 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8996 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8997 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008998 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008999
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009000 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9001 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009002 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9003 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009004
9005 if (copy_to_user(params, p, sizeof(*p))) {
9006 ret = -EFAULT;
9007 goto err;
9008 }
Jens Axboed1719f72020-07-30 13:43:53 -06009009
9010 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009011 * Install ring fd as the very last thing, so we don't risk someone
9012 * having closed it before we finish setup
9013 */
9014 ret = io_uring_get_fd(ctx);
9015 if (ret < 0)
9016 goto err;
9017
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009018 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009019 return ret;
9020err:
9021 io_ring_ctx_wait_and_kill(ctx);
9022 return ret;
9023}
9024
9025/*
9026 * Sets up an aio uring context, and returns the fd. Applications asks for a
9027 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9028 * params structure passed in.
9029 */
9030static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9031{
9032 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009033 int i;
9034
9035 if (copy_from_user(&p, params, sizeof(p)))
9036 return -EFAULT;
9037 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9038 if (p.resv[i])
9039 return -EINVAL;
9040 }
9041
Jens Axboe6c271ce2019-01-10 11:22:30 -07009042 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009043 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03009044 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009045 return -EINVAL;
9046
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009047 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009048}
9049
9050SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9051 struct io_uring_params __user *, params)
9052{
9053 return io_uring_setup(entries, params);
9054}
9055
Jens Axboe66f4af92020-01-16 15:36:52 -07009056static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9057{
9058 struct io_uring_probe *p;
9059 size_t size;
9060 int i, ret;
9061
9062 size = struct_size(p, ops, nr_args);
9063 if (size == SIZE_MAX)
9064 return -EOVERFLOW;
9065 p = kzalloc(size, GFP_KERNEL);
9066 if (!p)
9067 return -ENOMEM;
9068
9069 ret = -EFAULT;
9070 if (copy_from_user(p, arg, size))
9071 goto out;
9072 ret = -EINVAL;
9073 if (memchr_inv(p, 0, size))
9074 goto out;
9075
9076 p->last_op = IORING_OP_LAST - 1;
9077 if (nr_args > IORING_OP_LAST)
9078 nr_args = IORING_OP_LAST;
9079
9080 for (i = 0; i < nr_args; i++) {
9081 p->ops[i].op = i;
9082 if (!io_op_defs[i].not_supported)
9083 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9084 }
9085 p->ops_len = i;
9086
9087 ret = 0;
9088 if (copy_to_user(arg, p, size))
9089 ret = -EFAULT;
9090out:
9091 kfree(p);
9092 return ret;
9093}
9094
Jens Axboe071698e2020-01-28 10:04:42 -07009095static int io_register_personality(struct io_ring_ctx *ctx)
9096{
9097 const struct cred *creds = get_current_cred();
9098 int id;
9099
9100 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9101 USHRT_MAX, GFP_KERNEL);
9102 if (id < 0)
9103 put_cred(creds);
9104 return id;
9105}
9106
9107static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9108{
9109 const struct cred *old_creds;
9110
9111 old_creds = idr_remove(&ctx->personality_idr, id);
9112 if (old_creds) {
9113 put_cred(old_creds);
9114 return 0;
9115 }
9116
9117 return -EINVAL;
9118}
9119
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009120static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9121 unsigned int nr_args)
9122{
9123 struct io_uring_restriction *res;
9124 size_t size;
9125 int i, ret;
9126
9127 /* We allow only a single restrictions registration */
9128 if (ctx->restricted)
9129 return -EBUSY;
9130
9131 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9132 return -EINVAL;
9133
9134 size = array_size(nr_args, sizeof(*res));
9135 if (size == SIZE_MAX)
9136 return -EOVERFLOW;
9137
9138 res = memdup_user(arg, size);
9139 if (IS_ERR(res))
9140 return PTR_ERR(res);
9141
9142 ret = 0;
9143
9144 for (i = 0; i < nr_args; i++) {
9145 switch (res[i].opcode) {
9146 case IORING_RESTRICTION_REGISTER_OP:
9147 if (res[i].register_op >= IORING_REGISTER_LAST) {
9148 ret = -EINVAL;
9149 goto out;
9150 }
9151
9152 __set_bit(res[i].register_op,
9153 ctx->restrictions.register_op);
9154 break;
9155 case IORING_RESTRICTION_SQE_OP:
9156 if (res[i].sqe_op >= IORING_OP_LAST) {
9157 ret = -EINVAL;
9158 goto out;
9159 }
9160
9161 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9162 break;
9163 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9164 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9165 break;
9166 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9167 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9168 break;
9169 default:
9170 ret = -EINVAL;
9171 goto out;
9172 }
9173 }
9174
9175out:
9176 /* Reset all restrictions if an error happened */
9177 if (ret != 0)
9178 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9179 else
9180 ctx->restricted = 1;
9181
9182 kfree(res);
9183 return ret;
9184}
9185
Jens Axboe071698e2020-01-28 10:04:42 -07009186static bool io_register_op_must_quiesce(int op)
9187{
9188 switch (op) {
9189 case IORING_UNREGISTER_FILES:
9190 case IORING_REGISTER_FILES_UPDATE:
9191 case IORING_REGISTER_PROBE:
9192 case IORING_REGISTER_PERSONALITY:
9193 case IORING_UNREGISTER_PERSONALITY:
9194 return false;
9195 default:
9196 return true;
9197 }
9198}
9199
Jens Axboeedafcce2019-01-09 09:16:05 -07009200static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9201 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009202 __releases(ctx->uring_lock)
9203 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009204{
9205 int ret;
9206
Jens Axboe35fa71a2019-04-22 10:23:23 -06009207 /*
9208 * We're inside the ring mutex, if the ref is already dying, then
9209 * someone else killed the ctx or is already going through
9210 * io_uring_register().
9211 */
9212 if (percpu_ref_is_dying(&ctx->refs))
9213 return -ENXIO;
9214
Jens Axboe071698e2020-01-28 10:04:42 -07009215 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009216 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009217
Jens Axboe05f3fb32019-12-09 11:22:50 -07009218 /*
9219 * Drop uring mutex before waiting for references to exit. If
9220 * another thread is currently inside io_uring_enter() it might
9221 * need to grab the uring_lock to make progress. If we hold it
9222 * here across the drain wait, then we can deadlock. It's safe
9223 * to drop the mutex here, since no new references will come in
9224 * after we've killed the percpu ref.
9225 */
9226 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009227 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009228 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009229 if (ret) {
9230 percpu_ref_resurrect(&ctx->refs);
9231 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009232 goto out_quiesce;
9233 }
9234 }
9235
9236 if (ctx->restricted) {
9237 if (opcode >= IORING_REGISTER_LAST) {
9238 ret = -EINVAL;
9239 goto out;
9240 }
9241
9242 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9243 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009244 goto out;
9245 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009246 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009247
9248 switch (opcode) {
9249 case IORING_REGISTER_BUFFERS:
9250 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9251 break;
9252 case IORING_UNREGISTER_BUFFERS:
9253 ret = -EINVAL;
9254 if (arg || nr_args)
9255 break;
9256 ret = io_sqe_buffer_unregister(ctx);
9257 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009258 case IORING_REGISTER_FILES:
9259 ret = io_sqe_files_register(ctx, arg, nr_args);
9260 break;
9261 case IORING_UNREGISTER_FILES:
9262 ret = -EINVAL;
9263 if (arg || nr_args)
9264 break;
9265 ret = io_sqe_files_unregister(ctx);
9266 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009267 case IORING_REGISTER_FILES_UPDATE:
9268 ret = io_sqe_files_update(ctx, arg, nr_args);
9269 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009270 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009271 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009272 ret = -EINVAL;
9273 if (nr_args != 1)
9274 break;
9275 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009276 if (ret)
9277 break;
9278 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9279 ctx->eventfd_async = 1;
9280 else
9281 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009282 break;
9283 case IORING_UNREGISTER_EVENTFD:
9284 ret = -EINVAL;
9285 if (arg || nr_args)
9286 break;
9287 ret = io_eventfd_unregister(ctx);
9288 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009289 case IORING_REGISTER_PROBE:
9290 ret = -EINVAL;
9291 if (!arg || nr_args > 256)
9292 break;
9293 ret = io_probe(ctx, arg, nr_args);
9294 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009295 case IORING_REGISTER_PERSONALITY:
9296 ret = -EINVAL;
9297 if (arg || nr_args)
9298 break;
9299 ret = io_register_personality(ctx);
9300 break;
9301 case IORING_UNREGISTER_PERSONALITY:
9302 ret = -EINVAL;
9303 if (arg)
9304 break;
9305 ret = io_unregister_personality(ctx, nr_args);
9306 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009307 case IORING_REGISTER_RESTRICTIONS:
9308 ret = io_register_restrictions(ctx, arg, nr_args);
9309 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009310 default:
9311 ret = -EINVAL;
9312 break;
9313 }
9314
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009315out:
Jens Axboe071698e2020-01-28 10:04:42 -07009316 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009317 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009318 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009319out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009320 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009321 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009322 return ret;
9323}
9324
9325SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9326 void __user *, arg, unsigned int, nr_args)
9327{
9328 struct io_ring_ctx *ctx;
9329 long ret = -EBADF;
9330 struct fd f;
9331
9332 f = fdget(fd);
9333 if (!f.file)
9334 return -EBADF;
9335
9336 ret = -EOPNOTSUPP;
9337 if (f.file->f_op != &io_uring_fops)
9338 goto out_fput;
9339
9340 ctx = f.file->private_data;
9341
9342 mutex_lock(&ctx->uring_lock);
9343 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9344 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009345 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9346 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009347out_fput:
9348 fdput(f);
9349 return ret;
9350}
9351
Jens Axboe2b188cc2019-01-07 10:46:33 -07009352static int __init io_uring_init(void)
9353{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009354#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9355 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9356 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9357} while (0)
9358
9359#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9360 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9361 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9362 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9363 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9364 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9365 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9366 BUILD_BUG_SQE_ELEM(8, __u64, off);
9367 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9368 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009369 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009370 BUILD_BUG_SQE_ELEM(24, __u32, len);
9371 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9372 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9373 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9374 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009375 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9376 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009377 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9378 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9379 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9380 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9381 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9382 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9383 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9384 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009385 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009386 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9387 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9388 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009389 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009390
Jens Axboed3656342019-12-18 09:50:26 -07009391 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009392 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009393 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9394 return 0;
9395};
9396__initcall(io_uring_init);