blob: 6d97767c82d50c328be2f0a1503a4fccdf23fd79 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020084#define CREATE_TRACE_POINTS
85#include <trace/events/io_uring.h>
86
Jens Axboe2b188cc2019-01-07 10:46:33 -070087#include <uapi/linux/io_uring.h>
88
89#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060090#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
Daniel Xu5277dea2019-09-14 14:23:45 -070092#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060093#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060094
95/*
96 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
97 */
98#define IORING_FILE_TABLE_SHIFT 9
99#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
100#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
101#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200102#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
103 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700104
105struct io_uring {
106 u32 head ____cacheline_aligned_in_smp;
107 u32 tail ____cacheline_aligned_in_smp;
108};
109
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000111 * This data is shared with the application through the mmap at offsets
112 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 *
114 * The offsets to the member fields are published through struct
115 * io_sqring_offsets when calling io_uring_setup.
116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
119 * Head and tail offsets into the ring; the offsets need to be
120 * masked to get valid indices.
121 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * The kernel controls head of the sq ring and the tail of the cq ring,
123 * and the application controls tail of the sq ring and the head of the
124 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 * ring_entries - 1)
130 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 u32 sq_ring_mask, cq_ring_mask;
132 /* Ring sizes (constant, power of 2) */
133 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Number of invalid entries dropped by the kernel due to
136 * invalid index stored in array
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application (i.e. get number of "new events" by comparing to
140 * cached value).
141 *
142 * After a new SQ head value was read by the application this
143 * counter includes all submissions that were dropped reaching
144 * the new SQ head (and possibly more).
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200148 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application.
152 *
153 * The application needs a full memory barrier before checking
154 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
155 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000156 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200158 * Runtime CQ flags
159 *
160 * Written by the application, shouldn't be modified by the
161 * kernel.
162 */
163 u32 cq_flags;
164 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * Number of completion events lost because the queue was full;
166 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800167 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 * the completion queue.
169 *
170 * Written by the kernel, shouldn't be modified by the
171 * application (i.e. get number of "new events" by comparing to
172 * cached value).
173 *
174 * As completion events come in out of order this counter is not
175 * ordered with any other data.
176 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000177 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200178 /*
179 * Ring buffer of completion events.
180 *
181 * The kernel writes completion events fresh every time they are
182 * produced, so the application is allowed to modify pending
183 * entries.
184 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000185 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700186};
187
Jens Axboeedafcce2019-01-09 09:16:05 -0700188struct io_mapped_ubuf {
189 u64 ubuf;
190 size_t len;
191 struct bio_vec *bvec;
192 unsigned int nr_bvecs;
193};
194
Jens Axboe65e19f52019-10-26 07:20:21 -0600195struct fixed_file_table {
196 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700197};
198
Xiaoguang Wang05589552020-03-31 14:05:18 +0800199struct fixed_file_ref_node {
200 struct percpu_ref refs;
201 struct list_head node;
202 struct list_head file_list;
203 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600204 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800205};
206
Jens Axboe05f3fb32019-12-09 11:22:50 -0700207struct fixed_file_data {
208 struct fixed_file_table *table;
209 struct io_ring_ctx *ctx;
210
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700212 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800214 struct list_head ref_list;
215 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216};
217
Jens Axboe5a2e7452020-02-23 16:23:11 -0700218struct io_buffer {
219 struct list_head list;
220 __u64 addr;
221 __s32 len;
222 __u16 bid;
223};
224
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200225struct io_restriction {
226 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
227 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
228 u8 sqe_flags_allowed;
229 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200230 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200231};
232
Jens Axboe2b188cc2019-01-07 10:46:33 -0700233struct io_ring_ctx {
234 struct {
235 struct percpu_ref refs;
236 } ____cacheline_aligned_in_smp;
237
238 struct {
239 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800240 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700241 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800242 unsigned int cq_overflow_flushed: 1;
243 unsigned int drain_next: 1;
244 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200245 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246
Hristo Venev75b28af2019-08-26 17:23:46 +0000247 /*
248 * Ring buffer of indices into array of io_uring_sqe, which is
249 * mmapped by the application using the IORING_OFF_SQES offset.
250 *
251 * This indirection could e.g. be used to assign fixed
252 * io_uring_sqe entries to operations and only submit them to
253 * the queue when needed.
254 *
255 * The kernel modifies neither the indices array nor the entries
256 * array.
257 */
258 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 unsigned cached_sq_head;
260 unsigned sq_entries;
261 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700262 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600263 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700264 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700265 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600266
267 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600268 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700269 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270
Jens Axboefcb323c2019-10-24 12:39:47 -0600271 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700272 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700273 } ____cacheline_aligned_in_smp;
274
Hristo Venev75b28af2019-08-26 17:23:46 +0000275 struct io_rings *rings;
276
Jens Axboe2b188cc2019-01-07 10:46:33 -0700277 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600278 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2aede0e2020-09-14 10:45:53 -0600280
281 /*
282 * For SQPOLL usage - we hold a reference to the parent task, so we
283 * have access to the ->files
284 */
285 struct task_struct *sqo_task;
286
287 /* Only used for accounting purposes */
288 struct mm_struct *mm_account;
289
Jens Axboe6a779382020-09-02 12:21:41 -0600290 struct wait_queue_head *sqo_wait;
291 struct wait_queue_head __sqo_wait;
292 struct wait_queue_entry sqo_wait_entry;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700293
Jens Axboe6b063142019-01-10 22:13:58 -0700294 /*
295 * If used, fixed file set. Writers must ensure that ->refs is dead,
296 * readers must ensure that ->refs is alive as long as the file* is
297 * used. Only updated through io_uring_register(2).
298 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700299 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700300 unsigned nr_user_files;
301
Jens Axboeedafcce2019-01-09 09:16:05 -0700302 /* if used, fixed mapped user buffers */
303 unsigned nr_user_bufs;
304 struct io_mapped_ubuf *user_bufs;
305
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306 struct user_struct *user;
307
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700308 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700309
Jens Axboe0f158b42020-05-14 17:18:39 -0600310 struct completion ref_comp;
311 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700312
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700313 /* if all else fails... */
314 struct io_kiocb *fallback_req;
315
Jens Axboe206aefd2019-11-07 18:27:42 -0700316#if defined(CONFIG_UNIX)
317 struct socket *ring_sock;
318#endif
319
Jens Axboe5a2e7452020-02-23 16:23:11 -0700320 struct idr io_buffer_idr;
321
Jens Axboe071698e2020-01-28 10:04:42 -0700322 struct idr personality_idr;
323
Jens Axboe206aefd2019-11-07 18:27:42 -0700324 struct {
325 unsigned cached_cq_tail;
326 unsigned cq_entries;
327 unsigned cq_mask;
328 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700329 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700330 struct wait_queue_head cq_wait;
331 struct fasync_struct *cq_fasync;
332 struct eventfd_ctx *cq_ev_fd;
333 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700334
335 struct {
336 struct mutex uring_lock;
337 wait_queue_head_t wait;
338 } ____cacheline_aligned_in_smp;
339
340 struct {
341 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700342
Jens Axboedef596e2019-01-09 08:59:42 -0700343 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300344 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700345 * io_uring instances that don't use IORING_SETUP_SQPOLL.
346 * For SQPOLL, only the single threaded io_sq_thread() will
347 * manipulate the list, hence no extra locking is needed there.
348 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300349 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700350 struct hlist_head *cancel_hash;
351 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700352 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600353
354 spinlock_t inflight_lock;
355 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600357
Jens Axboe4a38aed22020-05-14 17:21:15 -0600358 struct delayed_work file_put_work;
359 struct llist_head file_put_llist;
360
Jens Axboe85faa7b2020-04-09 18:14:00 -0600361 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200362 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363};
364
Jens Axboe09bb8392019-03-13 12:39:28 -0600365/*
366 * First field must be the file pointer in all the
367 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
368 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700369struct io_poll_iocb {
370 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700371 union {
372 struct wait_queue_head *head;
373 u64 addr;
374 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700375 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600376 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700377 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700378 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700379};
380
Jens Axboeb5dba592019-12-11 14:02:38 -0700381struct io_close {
382 struct file *file;
383 struct file *put_file;
384 int fd;
385};
386
Jens Axboead8a48a2019-11-15 08:49:11 -0700387struct io_timeout_data {
388 struct io_kiocb *req;
389 struct hrtimer timer;
390 struct timespec64 ts;
391 enum hrtimer_mode mode;
392};
393
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700394struct io_accept {
395 struct file *file;
396 struct sockaddr __user *addr;
397 int __user *addr_len;
398 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600399 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700400};
401
402struct io_sync {
403 struct file *file;
404 loff_t len;
405 loff_t off;
406 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700407 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700408};
409
Jens Axboefbf23842019-12-17 18:45:56 -0700410struct io_cancel {
411 struct file *file;
412 u64 addr;
413};
414
Jens Axboeb29472e2019-12-17 18:50:29 -0700415struct io_timeout {
416 struct file *file;
417 u64 addr;
418 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300419 u32 off;
420 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300421 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700422};
423
Jens Axboe9adbd452019-12-20 08:45:55 -0700424struct io_rw {
425 /* NOTE: kiocb has the file as the first member, so don't do it here */
426 struct kiocb kiocb;
427 u64 addr;
428 u64 len;
429};
430
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700431struct io_connect {
432 struct file *file;
433 struct sockaddr __user *addr;
434 int addr_len;
435};
436
Jens Axboee47293f2019-12-20 08:58:21 -0700437struct io_sr_msg {
438 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700439 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300440 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700441 void __user *buf;
442 };
Jens Axboee47293f2019-12-20 08:58:21 -0700443 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700444 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700445 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700446 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700447};
448
Jens Axboe15b71ab2019-12-11 11:20:36 -0700449struct io_open {
450 struct file *file;
451 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700452 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700453 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600454 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700455};
456
Jens Axboe05f3fb32019-12-09 11:22:50 -0700457struct io_files_update {
458 struct file *file;
459 u64 arg;
460 u32 nr_args;
461 u32 offset;
462};
463
Jens Axboe4840e412019-12-25 22:03:45 -0700464struct io_fadvise {
465 struct file *file;
466 u64 offset;
467 u32 len;
468 u32 advice;
469};
470
Jens Axboec1ca7572019-12-25 22:18:28 -0700471struct io_madvise {
472 struct file *file;
473 u64 addr;
474 u32 len;
475 u32 advice;
476};
477
Jens Axboe3e4827b2020-01-08 15:18:09 -0700478struct io_epoll {
479 struct file *file;
480 int epfd;
481 int op;
482 int fd;
483 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700484};
485
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300486struct io_splice {
487 struct file *file_out;
488 struct file *file_in;
489 loff_t off_out;
490 loff_t off_in;
491 u64 len;
492 unsigned int flags;
493};
494
Jens Axboeddf0322d2020-02-23 16:41:33 -0700495struct io_provide_buf {
496 struct file *file;
497 __u64 addr;
498 __s32 len;
499 __u32 bgid;
500 __u16 nbufs;
501 __u16 bid;
502};
503
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700504struct io_statx {
505 struct file *file;
506 int dfd;
507 unsigned int mask;
508 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700509 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700510 struct statx __user *buffer;
511};
512
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300513struct io_completion {
514 struct file *file;
515 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300516 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300517};
518
Jens Axboef499a022019-12-02 16:28:46 -0700519struct io_async_connect {
520 struct sockaddr_storage address;
521};
522
Jens Axboe03b12302019-12-02 18:50:25 -0700523struct io_async_msghdr {
524 struct iovec fast_iov[UIO_FASTIOV];
525 struct iovec *iov;
526 struct sockaddr __user *uaddr;
527 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700528 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700529};
530
Jens Axboef67676d2019-12-02 11:03:47 -0700531struct io_async_rw {
532 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600533 const struct iovec *free_iovec;
534 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600535 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600536 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700537};
538
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700539struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700540 union {
541 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700542 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700543 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700544 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700545 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700546};
547
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300548enum {
549 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
550 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
551 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
552 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
553 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700554 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300555
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300556 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300557 REQ_F_FAIL_LINK_BIT,
558 REQ_F_INFLIGHT_BIT,
559 REQ_F_CUR_POS_BIT,
560 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300561 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300563 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300564 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700565 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700566 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600567 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800568 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700569
570 /* not a real bit, just to check we're not overflowing the space */
571 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300572};
573
574enum {
575 /* ctx owns file */
576 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
577 /* drain existing IO first */
578 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
579 /* linked sqes */
580 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
581 /* doesn't sever on completion < 0 */
582 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
583 /* IOSQE_ASYNC */
584 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700585 /* IOSQE_BUFFER_SELECT */
586 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300587
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300588 /* head of a link */
589 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300590 /* fail rest of links */
591 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
592 /* on inflight list */
593 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
594 /* read/write uses file position */
595 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
596 /* must not punt to workers */
597 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300598 /* has linked timeout */
599 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300600 /* regular file */
601 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300602 /* completion under lock */
603 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300604 /* needs cleanup */
605 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700606 /* already went through poll handler */
607 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700608 /* buffer already selected */
609 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600610 /* doesn't need file table for this request */
611 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800612 /* io_wq_work is initialized */
613 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700614};
615
616struct async_poll {
617 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600618 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300619};
620
Jens Axboe09bb8392019-03-13 12:39:28 -0600621/*
622 * NOTE! Each of the iocb union members has the file pointer
623 * as the first entry in their struct definition. So you can
624 * access the file pointer through any of the sub-structs,
625 * or directly as just 'ki_filp' in this struct.
626 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700627struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700628 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600629 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700630 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700631 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700632 struct io_accept accept;
633 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700634 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700635 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700636 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700637 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700638 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700639 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700640 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700641 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700642 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700643 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300644 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700645 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700646 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300647 /* use only after cleaning per-op data, see io_clean_op() */
648 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700649 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700650
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700651 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700652 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800653 /* polled IO has completed */
654 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700655
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700656 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300657 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700658
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300659 struct io_ring_ctx *ctx;
660 unsigned int flags;
661 refcount_t refs;
662 struct task_struct *task;
663 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700664
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300665 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700666
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300667 /*
668 * 1. used with ctx->iopoll_list with reads/writes
669 * 2. to track reqs with ->files (see io_op_def::file_table)
670 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300671 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600672
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300673 struct percpu_ref *fixed_file_refs;
674 struct callback_head task_work;
675 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
676 struct hlist_node hash_node;
677 struct async_poll *apoll;
678 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700679};
680
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300681struct io_defer_entry {
682 struct list_head list;
683 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300684 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300685};
686
Jens Axboedef596e2019-01-09 08:59:42 -0700687#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700688
Jens Axboe013538b2020-06-22 09:29:15 -0600689struct io_comp_state {
690 unsigned int nr;
691 struct list_head list;
692 struct io_ring_ctx *ctx;
693};
694
Jens Axboe9a56a232019-01-09 09:06:50 -0700695struct io_submit_state {
696 struct blk_plug plug;
697
698 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700699 * io_kiocb alloc cache
700 */
701 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300702 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700703
704 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600705 * Batch completion logic
706 */
707 struct io_comp_state comp;
708
709 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700710 * File reference cache
711 */
712 struct file *file;
713 unsigned int fd;
714 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700715 unsigned int ios_left;
716};
717
Jens Axboed3656342019-12-18 09:50:26 -0700718struct io_op_def {
719 /* needs req->io allocated for deferral/async */
720 unsigned async_ctx : 1;
721 /* needs current->mm setup, does mm access */
722 unsigned needs_mm : 1;
723 /* needs req->file assigned */
724 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600725 /* don't fail if file grab fails */
726 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700727 /* hash wq insertion if file is a regular file */
728 unsigned hash_reg_file : 1;
729 /* unbound wq insertion if file is a non-regular file */
730 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700731 /* opcode is not supported by this kernel */
732 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700733 /* needs file table */
734 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700735 /* needs ->fs */
736 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700737 /* set if opcode supports polled "wait" */
738 unsigned pollin : 1;
739 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700740 /* op supports buffer selection */
741 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300742 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700743};
744
745static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300746 [IORING_OP_NOP] = {},
747 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .async_ctx = 1,
749 .needs_mm = 1,
750 .needs_file = 1,
751 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700752 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700753 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700756 .async_ctx = 1,
757 .needs_mm = 1,
758 .needs_file = 1,
759 .hash_reg_file = 1,
760 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700761 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300762 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700763 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300764 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700765 .needs_file = 1,
766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700768 .needs_file = 1,
769 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700770 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700771 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300772 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700773 .needs_file = 1,
774 .hash_reg_file = 1,
775 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700776 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300777 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700778 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300779 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700780 .needs_file = 1,
781 .unbound_nonreg_file = 1,
782 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300783 [IORING_OP_POLL_REMOVE] = {},
784 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700785 .needs_file = 1,
786 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300787 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700788 .async_ctx = 1,
789 .needs_mm = 1,
790 .needs_file = 1,
791 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700792 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700793 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700796 .async_ctx = 1,
797 .needs_mm = 1,
798 .needs_file = 1,
799 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700800 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700801 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700802 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700803 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300804 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700805 .async_ctx = 1,
806 .needs_mm = 1,
807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_TIMEOUT_REMOVE] = {},
809 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .needs_mm = 1,
811 .needs_file = 1,
812 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700813 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700814 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700815 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_ASYNC_CANCEL] = {},
817 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700818 .async_ctx = 1,
819 .needs_mm = 1,
820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700822 .async_ctx = 1,
823 .needs_mm = 1,
824 .needs_file = 1,
825 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700826 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700829 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300830 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700831 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300832 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700833 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700834 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700835 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300836 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600837 .needs_file = 1,
838 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700839 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700840 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300841 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700842 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700843 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700846 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700847 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600848 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700849 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300850 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700851 .needs_mm = 1,
852 .needs_file = 1,
853 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700854 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700855 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700856 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300857 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700858 .needs_mm = 1,
859 .needs_file = 1,
860 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700861 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300862 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700865 .needs_file = 1,
866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700868 .needs_mm = 1,
869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700871 .needs_mm = 1,
872 .needs_file = 1,
873 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700874 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700875 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300876 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700877 .needs_mm = 1,
878 .needs_file = 1,
879 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700880 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700881 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700882 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300883 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700884 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700885 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700886 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700887 [IORING_OP_EPOLL_CTL] = {
888 .unbound_nonreg_file = 1,
889 .file_table = 1,
890 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300891 [IORING_OP_SPLICE] = {
892 .needs_file = 1,
893 .hash_reg_file = 1,
894 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700895 },
896 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700897 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300898 [IORING_OP_TEE] = {
899 .needs_file = 1,
900 .hash_reg_file = 1,
901 .unbound_nonreg_file = 1,
902 },
Jens Axboed3656342019-12-18 09:50:26 -0700903};
904
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700905enum io_mem_account {
906 ACCT_LOCKED,
907 ACCT_PINNED,
908};
909
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300910static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
911 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700912static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800913static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600914static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700915static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700916static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600917static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700918static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700919static int __io_sqe_files_update(struct io_ring_ctx *ctx,
920 struct io_uring_files_update *ip,
921 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300922static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300923static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700924static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
925 int fd, struct file **out_file, bool fixed);
926static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600927 const struct io_uring_sqe *sqe,
928 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600929static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600930
Jens Axboeb63534c2020-06-04 11:28:00 -0600931static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
932 struct iovec **iovec, struct iov_iter *iter,
933 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600934static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
935 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600936 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937
938static struct kmem_cache *req_cachep;
939
940static const struct file_operations io_uring_fops;
941
942struct sock *io_uring_get_socket(struct file *file)
943{
944#if defined(CONFIG_UNIX)
945 if (file->f_op == &io_uring_fops) {
946 struct io_ring_ctx *ctx = file->private_data;
947
948 return ctx->ring_sock->sk;
949 }
950#endif
951 return NULL;
952}
953EXPORT_SYMBOL(io_uring_get_socket);
954
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300955static inline void io_clean_op(struct io_kiocb *req)
956{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300957 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
958 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300959 __io_clean_op(req);
960}
961
Jens Axboe4349f302020-07-09 15:07:01 -0600962static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600963{
964 struct mm_struct *mm = current->mm;
965
966 if (mm) {
967 kthread_unuse_mm(mm);
968 mmput(mm);
969 }
970}
971
972static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
973{
974 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300975 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600976 !ctx->sqo_task->mm ||
977 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600978 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600979 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600980 }
981
982 return 0;
983}
984
985static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
986 struct io_kiocb *req)
987{
988 if (!io_op_defs[req->opcode].needs_mm)
989 return 0;
990 return __io_sq_thread_acquire_mm(ctx);
991}
992
993static inline void req_set_fail_links(struct io_kiocb *req)
994{
995 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
996 req->flags |= REQ_F_FAIL_LINK;
997}
Jens Axboe4a38aed22020-05-14 17:21:15 -0600998
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800999/*
1000 * Note: must call io_req_init_async() for the first time you
1001 * touch any members of io_wq_work.
1002 */
1003static inline void io_req_init_async(struct io_kiocb *req)
1004{
1005 if (req->flags & REQ_F_WORK_INITIALIZED)
1006 return;
1007
1008 memset(&req->work, 0, sizeof(req->work));
1009 req->flags |= REQ_F_WORK_INITIALIZED;
1010}
1011
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001012static inline bool io_async_submit(struct io_ring_ctx *ctx)
1013{
1014 return ctx->flags & IORING_SETUP_SQPOLL;
1015}
1016
Jens Axboe2b188cc2019-01-07 10:46:33 -07001017static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1018{
1019 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1020
Jens Axboe0f158b42020-05-14 17:18:39 -06001021 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022}
1023
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001024static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1025{
1026 return !req->timeout.off;
1027}
1028
Jens Axboe2b188cc2019-01-07 10:46:33 -07001029static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1030{
1031 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001032 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001033
1034 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1035 if (!ctx)
1036 return NULL;
1037
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001038 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1039 if (!ctx->fallback_req)
1040 goto err;
1041
Jens Axboe78076bb2019-12-04 19:56:40 -07001042 /*
1043 * Use 5 bits less than the max cq entries, that should give us around
1044 * 32 entries per hash list if totally full and uniformly spread.
1045 */
1046 hash_bits = ilog2(p->cq_entries);
1047 hash_bits -= 5;
1048 if (hash_bits <= 0)
1049 hash_bits = 1;
1050 ctx->cancel_hash_bits = hash_bits;
1051 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1052 GFP_KERNEL);
1053 if (!ctx->cancel_hash)
1054 goto err;
1055 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1056
Roman Gushchin21482892019-05-07 10:01:48 -07001057 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001058 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1059 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060
1061 ctx->flags = p->flags;
Jens Axboe6a779382020-09-02 12:21:41 -06001062 init_waitqueue_head(&ctx->__sqo_wait);
1063 ctx->sqo_wait = &ctx->__sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001064 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001065 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001066 init_completion(&ctx->ref_comp);
1067 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001068 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001069 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001070 mutex_init(&ctx->uring_lock);
1071 init_waitqueue_head(&ctx->wait);
1072 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001073 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001074 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001075 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001076 init_waitqueue_head(&ctx->inflight_wait);
1077 spin_lock_init(&ctx->inflight_lock);
1078 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001079 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1080 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001081 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001082err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001083 if (ctx->fallback_req)
1084 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001085 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001086 kfree(ctx);
1087 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001088}
1089
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001090static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001091{
Jens Axboe2bc99302020-07-09 09:43:27 -06001092 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1093 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001094
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001095 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001096 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001097 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001098
Bob Liu9d858b22019-11-13 18:06:25 +08001099 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001100}
1101
Jens Axboede0617e2019-04-06 21:51:27 -06001102static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001103{
Hristo Venev75b28af2019-08-26 17:23:46 +00001104 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105
Pavel Begunkov07910152020-01-17 03:52:46 +03001106 /* order cqe stores with ring update */
1107 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001108
Pavel Begunkov07910152020-01-17 03:52:46 +03001109 if (wq_has_sleeper(&ctx->cq_wait)) {
1110 wake_up_interruptible(&ctx->cq_wait);
1111 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001112 }
1113}
1114
Jens Axboe51a4cc12020-08-10 10:55:56 -06001115/*
1116 * Returns true if we need to defer file table putting. This can only happen
1117 * from the error path with REQ_F_COMP_LOCKED set.
1118 */
1119static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001120{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001121 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001122 return false;
1123
1124 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001125
Jens Axboecccf0ee2020-01-27 16:34:48 -07001126 if (req->work.mm) {
1127 mmdrop(req->work.mm);
1128 req->work.mm = NULL;
1129 }
1130 if (req->work.creds) {
1131 put_cred(req->work.creds);
1132 req->work.creds = NULL;
1133 }
Jens Axboeff002b32020-02-07 16:05:21 -07001134 if (req->work.fs) {
1135 struct fs_struct *fs = req->work.fs;
1136
Jens Axboe51a4cc12020-08-10 10:55:56 -06001137 if (req->flags & REQ_F_COMP_LOCKED)
1138 return true;
1139
Jens Axboeff002b32020-02-07 16:05:21 -07001140 spin_lock(&req->work.fs->lock);
1141 if (--fs->users)
1142 fs = NULL;
1143 spin_unlock(&req->work.fs->lock);
1144 if (fs)
1145 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001146 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001147 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001148
1149 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001150}
1151
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001152static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001153{
Jens Axboed3656342019-12-18 09:50:26 -07001154 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001155
Pavel Begunkov16d59802020-07-12 16:16:47 +03001156 io_req_init_async(req);
1157
Jens Axboed3656342019-12-18 09:50:26 -07001158 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001159 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001160 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001161 } else {
1162 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001163 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001164 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001165 if (!req->work.mm && def->needs_mm) {
1166 mmgrab(current->mm);
1167 req->work.mm = current->mm;
1168 }
1169 if (!req->work.creds)
1170 req->work.creds = get_current_cred();
1171 if (!req->work.fs && def->needs_fs) {
1172 spin_lock(&current->fs->lock);
1173 if (!current->fs->in_exec) {
1174 req->work.fs = current->fs;
1175 req->work.fs->users++;
1176 } else {
1177 req->work.flags |= IO_WQ_WORK_CANCEL;
1178 }
1179 spin_unlock(&current->fs->lock);
1180 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001181 if (def->needs_fsize)
1182 req->work.fsize = rlimit(RLIMIT_FSIZE);
1183 else
1184 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001185}
1186
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001187static void io_prep_async_link(struct io_kiocb *req)
1188{
1189 struct io_kiocb *cur;
1190
1191 io_prep_async_work(req);
1192 if (req->flags & REQ_F_LINK_HEAD)
1193 list_for_each_entry(cur, &req->link_list, link_list)
1194 io_prep_async_work(cur);
1195}
1196
Jens Axboe7271ef32020-08-10 09:55:22 -06001197static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001198{
Jackie Liua197f662019-11-08 08:09:12 -07001199 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001200 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001201
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001202 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1203 &req->work, req->flags);
1204 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001205 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001206}
1207
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001208static void io_queue_async_work(struct io_kiocb *req)
1209{
Jens Axboe7271ef32020-08-10 09:55:22 -06001210 struct io_kiocb *link;
1211
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001212 /* init ->work of the whole link before punting */
1213 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001214 link = __io_queue_async_work(req);
1215
1216 if (link)
1217 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001218}
1219
Jens Axboe5262f562019-09-17 12:26:57 -06001220static void io_kill_timeout(struct io_kiocb *req)
1221{
1222 int ret;
1223
Jens Axboe2d283902019-12-04 11:08:05 -07001224 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001225 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001226 atomic_set(&req->ctx->cq_timeouts,
1227 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001228 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001229 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001230 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001231 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001232 }
1233}
1234
Jens Axboef3606e32020-09-22 08:18:24 -06001235static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1236{
1237 struct io_ring_ctx *ctx = req->ctx;
1238
1239 if (!tsk || req->task == tsk)
1240 return true;
1241 if ((ctx->flags & IORING_SETUP_SQPOLL) && req->task == ctx->sqo_thread)
1242 return true;
1243 return false;
1244}
1245
Jens Axboe76e1b642020-09-26 15:05:03 -06001246/*
1247 * Returns true if we found and killed one or more timeouts
1248 */
1249static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001250{
1251 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001252 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001253
1254 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001255 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001256 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001257 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001258 canceled++;
1259 }
Jens Axboef3606e32020-09-22 08:18:24 -06001260 }
Jens Axboe5262f562019-09-17 12:26:57 -06001261 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001262 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001263}
1264
Pavel Begunkov04518942020-05-26 20:34:05 +03001265static void __io_queue_deferred(struct io_ring_ctx *ctx)
1266{
1267 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001268 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1269 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001270 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001271
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001272 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001273 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001274 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001275 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001276 link = __io_queue_async_work(de->req);
1277 if (link) {
1278 __io_queue_linked_timeout(link);
1279 /* drop submission reference */
1280 link->flags |= REQ_F_COMP_LOCKED;
1281 io_put_req(link);
1282 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001283 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001284 } while (!list_empty(&ctx->defer_list));
1285}
1286
Pavel Begunkov360428f2020-05-30 14:54:17 +03001287static void io_flush_timeouts(struct io_ring_ctx *ctx)
1288{
1289 while (!list_empty(&ctx->timeout_list)) {
1290 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001291 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001292
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001293 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001294 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001295 if (req->timeout.target_seq != ctx->cached_cq_tail
1296 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001297 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001298
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001299 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001300 io_kill_timeout(req);
1301 }
1302}
1303
Jens Axboede0617e2019-04-06 21:51:27 -06001304static void io_commit_cqring(struct io_ring_ctx *ctx)
1305{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001306 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001307 __io_commit_cqring(ctx);
1308
Pavel Begunkov04518942020-05-26 20:34:05 +03001309 if (unlikely(!list_empty(&ctx->defer_list)))
1310 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001311}
1312
Jens Axboe2b188cc2019-01-07 10:46:33 -07001313static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1314{
Hristo Venev75b28af2019-08-26 17:23:46 +00001315 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001316 unsigned tail;
1317
1318 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001319 /*
1320 * writes to the cq entry need to come after reading head; the
1321 * control dependency is enough as we're using WRITE_ONCE to
1322 * fill the cq entry
1323 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001324 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325 return NULL;
1326
1327 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001328 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329}
1330
Jens Axboef2842ab2020-01-08 11:04:00 -07001331static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1332{
Jens Axboef0b493e2020-02-01 21:30:11 -07001333 if (!ctx->cq_ev_fd)
1334 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001335 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1336 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001337 if (!ctx->eventfd_async)
1338 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001339 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001340}
1341
Jens Axboeb41e9852020-02-17 09:52:41 -07001342static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001343{
1344 if (waitqueue_active(&ctx->wait))
1345 wake_up(&ctx->wait);
Jens Axboe6a779382020-09-02 12:21:41 -06001346 if (waitqueue_active(ctx->sqo_wait))
1347 wake_up(ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001348 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001349 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001350}
1351
Pavel Begunkov46930142020-07-30 18:43:49 +03001352static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1353{
1354 if (list_empty(&ctx->cq_overflow_list)) {
1355 clear_bit(0, &ctx->sq_check_overflow);
1356 clear_bit(0, &ctx->cq_check_overflow);
1357 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1358 }
1359}
1360
Jens Axboee6c8aa92020-09-28 13:10:13 -06001361static inline bool io_match_files(struct io_kiocb *req,
1362 struct files_struct *files)
1363{
1364 if (!files)
1365 return true;
1366 if (req->flags & REQ_F_WORK_INITIALIZED)
1367 return req->work.files == files;
1368 return false;
1369}
1370
Jens Axboec4a2ed72019-11-21 21:01:26 -07001371/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001372static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1373 struct task_struct *tsk,
1374 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001375{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001376 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001377 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001378 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001379 unsigned long flags;
1380 LIST_HEAD(list);
1381
1382 if (!force) {
1383 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001384 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001385 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1386 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001387 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001388 }
1389
1390 spin_lock_irqsave(&ctx->completion_lock, flags);
1391
1392 /* if force is set, the ring is going away. always drop after that */
1393 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001394 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001395
Jens Axboec4a2ed72019-11-21 21:01:26 -07001396 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001397 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1398 if (tsk && req->task != tsk)
1399 continue;
1400 if (!io_match_files(req, files))
1401 continue;
1402
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001403 cqe = io_get_cqring(ctx);
1404 if (!cqe && !force)
1405 break;
1406
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001407 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001408 if (cqe) {
1409 WRITE_ONCE(cqe->user_data, req->user_data);
1410 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001411 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001412 } else {
1413 WRITE_ONCE(ctx->rings->cq_overflow,
1414 atomic_inc_return(&ctx->cached_cq_overflow));
1415 }
1416 }
1417
1418 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001419 io_cqring_mark_overflow(ctx);
1420
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001421 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1422 io_cqring_ev_posted(ctx);
1423
1424 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001425 req = list_first_entry(&list, struct io_kiocb, compl.list);
1426 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001427 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001428 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001429
1430 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001431}
1432
Jens Axboebcda7ba2020-02-23 16:42:51 -07001433static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001434{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001435 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436 struct io_uring_cqe *cqe;
1437
Jens Axboe78e19bb2019-11-06 15:21:34 -07001438 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001439
Jens Axboe2b188cc2019-01-07 10:46:33 -07001440 /*
1441 * If we can't get a cq entry, userspace overflowed the
1442 * submission (by quite a lot). Increment the overflow count in
1443 * the ring.
1444 */
1445 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001446 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001447 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001448 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001449 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001450 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1451 /*
1452 * If we're in ring overflow flush mode, or in task cancel mode,
1453 * then we cannot store the request for later flushing, we need
1454 * to drop it on the floor.
1455 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001456 WRITE_ONCE(ctx->rings->cq_overflow,
1457 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001458 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001459 if (list_empty(&ctx->cq_overflow_list)) {
1460 set_bit(0, &ctx->sq_check_overflow);
1461 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001462 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001463 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001464 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001465 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001466 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001467 refcount_inc(&req->refs);
1468 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001469 }
1470}
1471
Jens Axboebcda7ba2020-02-23 16:42:51 -07001472static void io_cqring_fill_event(struct io_kiocb *req, long res)
1473{
1474 __io_cqring_fill_event(req, res, 0);
1475}
1476
Jens Axboee1e16092020-06-22 09:17:17 -06001477static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001478{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001479 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480 unsigned long flags;
1481
1482 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001483 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001484 io_commit_cqring(ctx);
1485 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1486
Jens Axboe8c838782019-03-12 15:48:16 -06001487 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488}
1489
Jens Axboe229a7b62020-06-22 10:13:11 -06001490static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001491{
Jens Axboe229a7b62020-06-22 10:13:11 -06001492 struct io_ring_ctx *ctx = cs->ctx;
1493
1494 spin_lock_irq(&ctx->completion_lock);
1495 while (!list_empty(&cs->list)) {
1496 struct io_kiocb *req;
1497
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001498 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1499 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001500 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001501 if (!(req->flags & REQ_F_LINK_HEAD)) {
1502 req->flags |= REQ_F_COMP_LOCKED;
1503 io_put_req(req);
1504 } else {
1505 spin_unlock_irq(&ctx->completion_lock);
1506 io_put_req(req);
1507 spin_lock_irq(&ctx->completion_lock);
1508 }
1509 }
1510 io_commit_cqring(ctx);
1511 spin_unlock_irq(&ctx->completion_lock);
1512
1513 io_cqring_ev_posted(ctx);
1514 cs->nr = 0;
1515}
1516
1517static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1518 struct io_comp_state *cs)
1519{
1520 if (!cs) {
1521 io_cqring_add_event(req, res, cflags);
1522 io_put_req(req);
1523 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001524 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001525 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001526 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001527 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001528 if (++cs->nr >= 32)
1529 io_submit_flush_completions(cs);
1530 }
Jens Axboee1e16092020-06-22 09:17:17 -06001531}
1532
1533static void io_req_complete(struct io_kiocb *req, long res)
1534{
Jens Axboe229a7b62020-06-22 10:13:11 -06001535 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001536}
1537
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001538static inline bool io_is_fallback_req(struct io_kiocb *req)
1539{
1540 return req == (struct io_kiocb *)
1541 ((unsigned long) req->ctx->fallback_req & ~1UL);
1542}
1543
1544static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1545{
1546 struct io_kiocb *req;
1547
1548 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001549 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001550 return req;
1551
1552 return NULL;
1553}
1554
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001555static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1556 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001557{
Jens Axboefd6fab22019-03-14 16:30:06 -06001558 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001559 struct io_kiocb *req;
1560
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001561 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001562 size_t sz;
1563 int ret;
1564
1565 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001566 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1567
1568 /*
1569 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1570 * retry single alloc to be on the safe side.
1571 */
1572 if (unlikely(ret <= 0)) {
1573 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1574 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001575 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001576 ret = 1;
1577 }
Jens Axboe2579f912019-01-09 09:10:43 -07001578 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001579 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001580 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001581 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001582 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001583 }
1584
Jens Axboe2579f912019-01-09 09:10:43 -07001585 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001586fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001587 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001588}
1589
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001590static inline void io_put_file(struct io_kiocb *req, struct file *file,
1591 bool fixed)
1592{
1593 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001594 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001595 else
1596 fput(file);
1597}
1598
Jens Axboe51a4cc12020-08-10 10:55:56 -06001599static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001600{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001601 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001602
Jens Axboe5acbbc82020-07-08 15:15:26 -06001603 if (req->io)
1604 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001605 if (req->file)
1606 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001607
Jens Axboe51a4cc12020-08-10 10:55:56 -06001608 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001609}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001610
Jens Axboe51a4cc12020-08-10 10:55:56 -06001611static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001612{
Jens Axboe0f212202020-09-13 13:09:39 -06001613 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001614 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001615
Jens Axboe0f212202020-09-13 13:09:39 -06001616 atomic_long_inc(&tctx->req_complete);
1617 if (tctx->in_idle)
1618 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001619 put_task_struct(req->task);
1620
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001621 if (likely(!io_is_fallback_req(req)))
1622 kmem_cache_free(req_cachep, req);
1623 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001624 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1625 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001626}
1627
Jens Axboe51a4cc12020-08-10 10:55:56 -06001628static void io_req_task_file_table_put(struct callback_head *cb)
1629{
1630 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1631 struct fs_struct *fs = req->work.fs;
1632
1633 spin_lock(&req->work.fs->lock);
1634 if (--fs->users)
1635 fs = NULL;
1636 spin_unlock(&req->work.fs->lock);
1637 if (fs)
1638 free_fs_struct(fs);
1639 req->work.fs = NULL;
1640 __io_free_req_finish(req);
1641}
1642
1643static void __io_free_req(struct io_kiocb *req)
1644{
1645 if (!io_dismantle_req(req)) {
1646 __io_free_req_finish(req);
1647 } else {
1648 int ret;
1649
1650 init_task_work(&req->task_work, io_req_task_file_table_put);
1651 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1652 if (unlikely(ret)) {
1653 struct task_struct *tsk;
1654
1655 tsk = io_wq_get_task(req->ctx->io_wq);
1656 task_work_add(tsk, &req->task_work, 0);
1657 }
1658 }
1659}
1660
Jackie Liua197f662019-11-08 08:09:12 -07001661static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001662{
Jackie Liua197f662019-11-08 08:09:12 -07001663 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001664 int ret;
1665
Jens Axboe2d283902019-12-04 11:08:05 -07001666 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001667 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001668 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001669 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001670 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001671 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001672 return true;
1673 }
1674
1675 return false;
1676}
1677
Jens Axboeab0b6452020-06-30 08:43:15 -06001678static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001679{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001680 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001681 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001682
1683 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001684 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001685 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1686 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001687 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001688
1689 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001690 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001691 wake_ev = io_link_cancel_timeout(link);
1692 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001693 return wake_ev;
1694}
1695
1696static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001697{
Jens Axboe2665abf2019-11-05 12:40:47 -07001698 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001699 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001700
Jens Axboeab0b6452020-06-30 08:43:15 -06001701 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1702 unsigned long flags;
1703
1704 spin_lock_irqsave(&ctx->completion_lock, flags);
1705 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001706 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001707 } else {
1708 wake_ev = __io_kill_linked_timeout(req);
1709 }
1710
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001711 if (wake_ev)
1712 io_cqring_ev_posted(ctx);
1713}
1714
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001715static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001716{
1717 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001718
Jens Axboe9e645e112019-05-10 16:07:28 -06001719 /*
1720 * The list should never be empty when we are called here. But could
1721 * potentially happen if the chain is messed up, check to be on the
1722 * safe side.
1723 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001724 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001725 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001726
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001727 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1728 list_del_init(&req->link_list);
1729 if (!list_empty(&nxt->link_list))
1730 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001731 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001732}
1733
1734/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001735 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001736 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001737static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001738{
Jens Axboe2665abf2019-11-05 12:40:47 -07001739 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001740
1741 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001742 struct io_kiocb *link = list_first_entry(&req->link_list,
1743 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001744
Pavel Begunkov44932332019-12-05 16:16:35 +03001745 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001746 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001747
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001748 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001749 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001750 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001751 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001752 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001753
1754 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001755 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001756}
1757
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001758static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001759{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001760 struct io_ring_ctx *ctx = req->ctx;
1761
1762 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1763 unsigned long flags;
1764
1765 spin_lock_irqsave(&ctx->completion_lock, flags);
1766 __io_fail_links(req);
1767 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1768 } else {
1769 __io_fail_links(req);
1770 }
1771
Jens Axboe9e645e112019-05-10 16:07:28 -06001772 io_cqring_ev_posted(ctx);
1773}
1774
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001775static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001776{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001777 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001778 if (req->flags & REQ_F_LINK_TIMEOUT)
1779 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001780
Jens Axboe9e645e112019-05-10 16:07:28 -06001781 /*
1782 * If LINK is set, we have dependent requests in this chain. If we
1783 * didn't fail this request, queue the first one up, moving any other
1784 * dependencies to the next request. In case of failure, fail the rest
1785 * of the chain.
1786 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001787 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1788 return io_req_link_next(req);
1789 io_fail_links(req);
1790 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001791}
Jens Axboe2665abf2019-11-05 12:40:47 -07001792
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001793static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1794{
1795 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1796 return NULL;
1797 return __io_req_find_next(req);
1798}
1799
Jens Axboefd7d6de2020-08-23 11:00:37 -06001800static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1801 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001802{
1803 struct task_struct *tsk = req->task;
1804 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001805 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001806
Jens Axboe6200b0a2020-09-13 14:38:30 -06001807 if (tsk->flags & PF_EXITING)
1808 return -ESRCH;
1809
Jens Axboec2c4c832020-07-01 15:37:11 -06001810 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001811 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1812 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1813 * processing task_work. There's no reliable way to tell if TWA_RESUME
1814 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001815 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001816 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001817 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001818 notify = TWA_SIGNAL;
1819
1820 ret = task_work_add(tsk, cb, notify);
1821 if (!ret)
1822 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001823
Jens Axboec2c4c832020-07-01 15:37:11 -06001824 return ret;
1825}
1826
Jens Axboec40f6372020-06-25 15:39:59 -06001827static void __io_req_task_cancel(struct io_kiocb *req, int error)
1828{
1829 struct io_ring_ctx *ctx = req->ctx;
1830
1831 spin_lock_irq(&ctx->completion_lock);
1832 io_cqring_fill_event(req, error);
1833 io_commit_cqring(ctx);
1834 spin_unlock_irq(&ctx->completion_lock);
1835
1836 io_cqring_ev_posted(ctx);
1837 req_set_fail_links(req);
1838 io_double_put_req(req);
1839}
1840
1841static void io_req_task_cancel(struct callback_head *cb)
1842{
1843 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001844 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001845
1846 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001847 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001848}
1849
1850static void __io_req_task_submit(struct io_kiocb *req)
1851{
1852 struct io_ring_ctx *ctx = req->ctx;
1853
Jens Axboec40f6372020-06-25 15:39:59 -06001854 if (!__io_sq_thread_acquire_mm(ctx)) {
1855 mutex_lock(&ctx->uring_lock);
1856 __io_queue_sqe(req, NULL, NULL);
1857 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001858 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001859 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001860 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001861}
1862
Jens Axboec40f6372020-06-25 15:39:59 -06001863static void io_req_task_submit(struct callback_head *cb)
1864{
1865 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001866 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001867
1868 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001869 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001870}
1871
1872static void io_req_task_queue(struct io_kiocb *req)
1873{
Jens Axboec40f6372020-06-25 15:39:59 -06001874 int ret;
1875
1876 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001877 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001878
Jens Axboefd7d6de2020-08-23 11:00:37 -06001879 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001880 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001881 struct task_struct *tsk;
1882
Jens Axboec40f6372020-06-25 15:39:59 -06001883 init_task_work(&req->task_work, io_req_task_cancel);
1884 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001885 task_work_add(tsk, &req->task_work, 0);
1886 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001887 }
Jens Axboec40f6372020-06-25 15:39:59 -06001888}
1889
Pavel Begunkovc3524382020-06-28 12:52:32 +03001890static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001891{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001892 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001893
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001894 if (nxt)
1895 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001896}
1897
Jens Axboe9e645e112019-05-10 16:07:28 -06001898static void io_free_req(struct io_kiocb *req)
1899{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001900 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001901 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001902}
1903
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001904struct req_batch {
1905 void *reqs[IO_IOPOLL_BATCH];
1906 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001907
1908 struct task_struct *task;
1909 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001910};
1911
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001912static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001913{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001914 rb->to_free = 0;
1915 rb->task_refs = 0;
1916 rb->task = NULL;
1917}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001918
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001919static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1920 struct req_batch *rb)
1921{
1922 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1923 percpu_ref_put_many(&ctx->refs, rb->to_free);
1924 rb->to_free = 0;
1925}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001926
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001927static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1928 struct req_batch *rb)
1929{
1930 if (rb->to_free)
1931 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001932 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001933 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001934 put_task_struct_many(rb->task, rb->task_refs);
1935 rb->task = NULL;
1936 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001937}
1938
1939static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1940{
1941 if (unlikely(io_is_fallback_req(req))) {
1942 io_free_req(req);
1943 return;
1944 }
1945 if (req->flags & REQ_F_LINK_HEAD)
1946 io_queue_next(req);
1947
Jens Axboee3bc8e92020-09-24 08:45:57 -06001948 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001949 if (rb->task) {
1950 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001951 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06001952 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001953 rb->task = req->task;
1954 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001955 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001956 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001957
Jens Axboe51a4cc12020-08-10 10:55:56 -06001958 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001959 rb->reqs[rb->to_free++] = req;
1960 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1961 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001962}
1963
Jens Axboeba816ad2019-09-28 11:36:45 -06001964/*
1965 * Drop reference to request, return next in chain (if there is one) if this
1966 * was the last reference to this request.
1967 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001968static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001969{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001970 struct io_kiocb *nxt = NULL;
1971
Jens Axboe2a44f462020-02-25 13:25:41 -07001972 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001973 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001974 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001975 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001976 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001977}
1978
Jens Axboe2b188cc2019-01-07 10:46:33 -07001979static void io_put_req(struct io_kiocb *req)
1980{
Jens Axboedef596e2019-01-09 08:59:42 -07001981 if (refcount_dec_and_test(&req->refs))
1982 io_free_req(req);
1983}
1984
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001985static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001986{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001987 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001988
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001989 /*
1990 * A ref is owned by io-wq in which context we're. So, if that's the
1991 * last one, it's safe to steal next work. False negatives are Ok,
1992 * it just will be re-punted async in io_put_work()
1993 */
1994 if (refcount_read(&req->refs) != 1)
1995 return NULL;
1996
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001997 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001998 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001999}
2000
Jens Axboe978db572019-11-14 22:39:04 -07002001/*
2002 * Must only be used if we don't need to care about links, usually from
2003 * within the completion handling itself.
2004 */
2005static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002006{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002007 /* drop both submit and complete references */
2008 if (refcount_sub_and_test(2, &req->refs))
2009 __io_free_req(req);
2010}
2011
Jens Axboe978db572019-11-14 22:39:04 -07002012static void io_double_put_req(struct io_kiocb *req)
2013{
2014 /* drop both submit and complete references */
2015 if (refcount_sub_and_test(2, &req->refs))
2016 io_free_req(req);
2017}
2018
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002019static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002020{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002021 struct io_rings *rings = ctx->rings;
2022
Jens Axboead3eb2c2019-12-18 17:12:20 -07002023 if (test_bit(0, &ctx->cq_check_overflow)) {
2024 /*
2025 * noflush == true is from the waitqueue handler, just ensure
2026 * we wake up the task, and the next invocation will flush the
2027 * entries. We cannot safely to it from here.
2028 */
2029 if (noflush && !list_empty(&ctx->cq_overflow_list))
2030 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002031
Jens Axboee6c8aa92020-09-28 13:10:13 -06002032 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002033 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002034
Jens Axboea3a0e432019-08-20 11:03:11 -06002035 /* See comment at the top of this file */
2036 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002037 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002038}
2039
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002040static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2041{
2042 struct io_rings *rings = ctx->rings;
2043
2044 /* make sure SQ entry isn't read before tail */
2045 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2046}
2047
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002048static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002049{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002050 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002051
Jens Axboebcda7ba2020-02-23 16:42:51 -07002052 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2053 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002054 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002055 kfree(kbuf);
2056 return cflags;
2057}
2058
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002059static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2060{
2061 struct io_buffer *kbuf;
2062
2063 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2064 return io_put_kbuf(req, kbuf);
2065}
2066
Jens Axboe4c6e2772020-07-01 11:29:10 -06002067static inline bool io_run_task_work(void)
2068{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002069 /*
2070 * Not safe to run on exiting task, and the task_work handling will
2071 * not add work to such a task.
2072 */
2073 if (unlikely(current->flags & PF_EXITING))
2074 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002075 if (current->task_works) {
2076 __set_current_state(TASK_RUNNING);
2077 task_work_run();
2078 return true;
2079 }
2080
2081 return false;
2082}
2083
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002084static void io_iopoll_queue(struct list_head *again)
2085{
2086 struct io_kiocb *req;
2087
2088 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002089 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2090 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002091 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002092 } while (!list_empty(again));
2093}
2094
Jens Axboedef596e2019-01-09 08:59:42 -07002095/*
2096 * Find and free completed poll iocbs
2097 */
2098static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2099 struct list_head *done)
2100{
Jens Axboe8237e042019-12-28 10:48:22 -07002101 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002102 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002103 LIST_HEAD(again);
2104
2105 /* order with ->result store in io_complete_rw_iopoll() */
2106 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002107
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002108 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002109 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002110 int cflags = 0;
2111
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002112 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002113 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002114 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002115 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002116 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002117 continue;
2118 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002119 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002120
Jens Axboebcda7ba2020-02-23 16:42:51 -07002121 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002122 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002123
2124 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002125 (*nr_events)++;
2126
Pavel Begunkovc3524382020-06-28 12:52:32 +03002127 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002128 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002129 }
Jens Axboedef596e2019-01-09 08:59:42 -07002130
Jens Axboe09bb8392019-03-13 12:39:28 -06002131 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002132 if (ctx->flags & IORING_SETUP_SQPOLL)
2133 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002134 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002135
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002136 if (!list_empty(&again))
2137 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002138}
2139
Jens Axboedef596e2019-01-09 08:59:42 -07002140static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2141 long min)
2142{
2143 struct io_kiocb *req, *tmp;
2144 LIST_HEAD(done);
2145 bool spin;
2146 int ret;
2147
2148 /*
2149 * Only spin for completions if we don't have multiple devices hanging
2150 * off our complete list, and we're under the requested amount.
2151 */
2152 spin = !ctx->poll_multi_file && *nr_events < min;
2153
2154 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002155 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002156 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002157
2158 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002159 * Move completed and retryable entries to our local lists.
2160 * If we find a request that requires polling, break out
2161 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002162 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002163 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002164 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002165 continue;
2166 }
2167 if (!list_empty(&done))
2168 break;
2169
2170 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2171 if (ret < 0)
2172 break;
2173
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002174 /* iopoll may have completed current req */
2175 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002176 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002177
Jens Axboedef596e2019-01-09 08:59:42 -07002178 if (ret && spin)
2179 spin = false;
2180 ret = 0;
2181 }
2182
2183 if (!list_empty(&done))
2184 io_iopoll_complete(ctx, nr_events, &done);
2185
2186 return ret;
2187}
2188
2189/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002190 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002191 * non-spinning poll check - we'll still enter the driver poll loop, but only
2192 * as a non-spinning completion check.
2193 */
2194static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2195 long min)
2196{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002197 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002198 int ret;
2199
2200 ret = io_do_iopoll(ctx, nr_events, min);
2201 if (ret < 0)
2202 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002203 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002204 return 0;
2205 }
2206
2207 return 1;
2208}
2209
2210/*
2211 * We can't just wait for polled events to come to us, we have to actively
2212 * find and complete them.
2213 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002214static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002215{
2216 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2217 return;
2218
2219 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002220 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002221 unsigned int nr_events = 0;
2222
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002223 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002224
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002225 /* let it sleep and repeat later if can't complete a request */
2226 if (nr_events == 0)
2227 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002228 /*
2229 * Ensure we allow local-to-the-cpu processing to take place,
2230 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002231 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002232 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002233 if (need_resched()) {
2234 mutex_unlock(&ctx->uring_lock);
2235 cond_resched();
2236 mutex_lock(&ctx->uring_lock);
2237 }
Jens Axboedef596e2019-01-09 08:59:42 -07002238 }
2239 mutex_unlock(&ctx->uring_lock);
2240}
2241
Pavel Begunkov7668b922020-07-07 16:36:21 +03002242static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002243{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002244 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002245 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002246
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002247 /*
2248 * We disallow the app entering submit/complete with polling, but we
2249 * still need to lock the ring to prevent racing with polled issue
2250 * that got punted to a workqueue.
2251 */
2252 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002253 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002254 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002255 * Don't enter poll loop if we already have events pending.
2256 * If we do, we can potentially be spinning for commands that
2257 * already triggered a CQE (eg in error).
2258 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002259 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002260 break;
2261
2262 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002263 * If a submit got punted to a workqueue, we can have the
2264 * application entering polling for a command before it gets
2265 * issued. That app will hold the uring_lock for the duration
2266 * of the poll right here, so we need to take a breather every
2267 * now and then to ensure that the issue has a chance to add
2268 * the poll to the issued list. Otherwise we can spin here
2269 * forever, while the workqueue is stuck trying to acquire the
2270 * very same mutex.
2271 */
2272 if (!(++iters & 7)) {
2273 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002274 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002275 mutex_lock(&ctx->uring_lock);
2276 }
2277
Pavel Begunkov7668b922020-07-07 16:36:21 +03002278 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002279 if (ret <= 0)
2280 break;
2281 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002282 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002283
Jens Axboe500f9fb2019-08-19 12:15:59 -06002284 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002285 return ret;
2286}
2287
Jens Axboe491381ce2019-10-17 09:20:46 -06002288static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002289{
Jens Axboe491381ce2019-10-17 09:20:46 -06002290 /*
2291 * Tell lockdep we inherited freeze protection from submission
2292 * thread.
2293 */
2294 if (req->flags & REQ_F_ISREG) {
2295 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002296
Jens Axboe491381ce2019-10-17 09:20:46 -06002297 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002298 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002299 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002300}
2301
Jens Axboea1d7c392020-06-22 11:09:46 -06002302static void io_complete_rw_common(struct kiocb *kiocb, long res,
2303 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002304{
Jens Axboe9adbd452019-12-20 08:45:55 -07002305 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002306 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307
Jens Axboe491381ce2019-10-17 09:20:46 -06002308 if (kiocb->ki_flags & IOCB_WRITE)
2309 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002310
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002311 if (res != req->result)
2312 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002313 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002314 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002315 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002316}
2317
Jens Axboeb63534c2020-06-04 11:28:00 -06002318#ifdef CONFIG_BLOCK
2319static bool io_resubmit_prep(struct io_kiocb *req, int error)
2320{
2321 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2322 ssize_t ret = -ECANCELED;
2323 struct iov_iter iter;
2324 int rw;
2325
2326 if (error) {
2327 ret = error;
2328 goto end_req;
2329 }
2330
2331 switch (req->opcode) {
2332 case IORING_OP_READV:
2333 case IORING_OP_READ_FIXED:
2334 case IORING_OP_READ:
2335 rw = READ;
2336 break;
2337 case IORING_OP_WRITEV:
2338 case IORING_OP_WRITE_FIXED:
2339 case IORING_OP_WRITE:
2340 rw = WRITE;
2341 break;
2342 default:
2343 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2344 req->opcode);
2345 goto end_req;
2346 }
2347
Jens Axboe8f3d7492020-09-14 09:28:14 -06002348 if (!req->io) {
2349 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2350 if (ret < 0)
2351 goto end_req;
2352 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2353 if (!ret)
2354 return true;
2355 kfree(iovec);
2356 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002357 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002358 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002359end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002360 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002361 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002362 return false;
2363}
Jens Axboeb63534c2020-06-04 11:28:00 -06002364#endif
2365
2366static bool io_rw_reissue(struct io_kiocb *req, long res)
2367{
2368#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002369 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002370 int ret;
2371
Jens Axboe355afae2020-09-02 09:30:31 -06002372 if (!S_ISBLK(mode) && !S_ISREG(mode))
2373 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002374 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2375 return false;
2376
Jens Axboefdee9462020-08-27 16:46:24 -06002377 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002378
Jens Axboefdee9462020-08-27 16:46:24 -06002379 if (io_resubmit_prep(req, ret)) {
2380 refcount_inc(&req->refs);
2381 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002382 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002383 }
2384
Jens Axboeb63534c2020-06-04 11:28:00 -06002385#endif
2386 return false;
2387}
2388
Jens Axboea1d7c392020-06-22 11:09:46 -06002389static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2390 struct io_comp_state *cs)
2391{
2392 if (!io_rw_reissue(req, res))
2393 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002394}
2395
2396static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2397{
Jens Axboe9adbd452019-12-20 08:45:55 -07002398 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002399
Jens Axboea1d7c392020-06-22 11:09:46 -06002400 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002401}
2402
Jens Axboedef596e2019-01-09 08:59:42 -07002403static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2404{
Jens Axboe9adbd452019-12-20 08:45:55 -07002405 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002406
Jens Axboe491381ce2019-10-17 09:20:46 -06002407 if (kiocb->ki_flags & IOCB_WRITE)
2408 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002409
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002410 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002411 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002412
2413 WRITE_ONCE(req->result, res);
2414 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002415 smp_wmb();
2416 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002417}
2418
2419/*
2420 * After the iocb has been issued, it's safe to be found on the poll list.
2421 * Adding the kiocb to the list AFTER submission ensures that we don't
2422 * find it from a io_iopoll_getevents() thread before the issuer is done
2423 * accessing the kiocb cookie.
2424 */
2425static void io_iopoll_req_issued(struct io_kiocb *req)
2426{
2427 struct io_ring_ctx *ctx = req->ctx;
2428
2429 /*
2430 * Track whether we have multiple files in our lists. This will impact
2431 * how we do polling eventually, not spinning if we're on potentially
2432 * different devices.
2433 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002434 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002435 ctx->poll_multi_file = false;
2436 } else if (!ctx->poll_multi_file) {
2437 struct io_kiocb *list_req;
2438
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002439 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002440 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002441 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002442 ctx->poll_multi_file = true;
2443 }
2444
2445 /*
2446 * For fast devices, IO may have already completed. If it has, add
2447 * it to the front so we find it first.
2448 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002449 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002450 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002451 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002452 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002453
Jens Axboe6a779382020-09-02 12:21:41 -06002454 if ((ctx->flags & IORING_SETUP_SQPOLL) && wq_has_sleeper(ctx->sqo_wait))
2455 wake_up(ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002456}
2457
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002458static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002459{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002460 if (state->has_refs)
2461 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002462 state->file = NULL;
2463}
2464
2465static inline void io_state_file_put(struct io_submit_state *state)
2466{
2467 if (state->file)
2468 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002469}
2470
2471/*
2472 * Get as many references to a file as we have IOs left in this submission,
2473 * assuming most submissions are for one file, or at least that each file
2474 * has more than one submission.
2475 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002476static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002477{
2478 if (!state)
2479 return fget(fd);
2480
2481 if (state->file) {
2482 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002483 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002484 state->ios_left--;
2485 return state->file;
2486 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002487 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002488 }
2489 state->file = fget_many(fd, state->ios_left);
2490 if (!state->file)
2491 return NULL;
2492
2493 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002494 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002495 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002496 return state->file;
2497}
2498
Jens Axboe4503b762020-06-01 10:00:27 -06002499static bool io_bdev_nowait(struct block_device *bdev)
2500{
2501#ifdef CONFIG_BLOCK
2502 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2503#else
2504 return true;
2505#endif
2506}
2507
Jens Axboe2b188cc2019-01-07 10:46:33 -07002508/*
2509 * If we tracked the file through the SCM inflight mechanism, we could support
2510 * any file. For now, just ensure that anything potentially problematic is done
2511 * inline.
2512 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002513static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002514{
2515 umode_t mode = file_inode(file)->i_mode;
2516
Jens Axboe4503b762020-06-01 10:00:27 -06002517 if (S_ISBLK(mode)) {
2518 if (io_bdev_nowait(file->f_inode->i_bdev))
2519 return true;
2520 return false;
2521 }
2522 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002524 if (S_ISREG(mode)) {
2525 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2526 file->f_op != &io_uring_fops)
2527 return true;
2528 return false;
2529 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002530
Jens Axboec5b85622020-06-09 19:23:05 -06002531 /* any ->read/write should understand O_NONBLOCK */
2532 if (file->f_flags & O_NONBLOCK)
2533 return true;
2534
Jens Axboeaf197f52020-04-28 13:15:06 -06002535 if (!(file->f_mode & FMODE_NOWAIT))
2536 return false;
2537
2538 if (rw == READ)
2539 return file->f_op->read_iter != NULL;
2540
2541 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002542}
2543
Jens Axboe3529d8c2019-12-19 18:24:38 -07002544static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2545 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002546{
Jens Axboedef596e2019-01-09 08:59:42 -07002547 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002548 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002549 unsigned ioprio;
2550 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002551
Jens Axboe491381ce2019-10-17 09:20:46 -06002552 if (S_ISREG(file_inode(req->file)->i_mode))
2553 req->flags |= REQ_F_ISREG;
2554
Jens Axboe2b188cc2019-01-07 10:46:33 -07002555 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002556 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2557 req->flags |= REQ_F_CUR_POS;
2558 kiocb->ki_pos = req->file->f_pos;
2559 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002560 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002561 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2562 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2563 if (unlikely(ret))
2564 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002565
2566 ioprio = READ_ONCE(sqe->ioprio);
2567 if (ioprio) {
2568 ret = ioprio_check_cap(ioprio);
2569 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002570 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002571
2572 kiocb->ki_ioprio = ioprio;
2573 } else
2574 kiocb->ki_ioprio = get_current_ioprio();
2575
Stefan Bühler8449eed2019-04-27 20:34:19 +02002576 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002577 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002578 req->flags |= REQ_F_NOWAIT;
2579
2580 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002581 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002582
Jens Axboedef596e2019-01-09 08:59:42 -07002583 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002584 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2585 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002586 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002587
Jens Axboedef596e2019-01-09 08:59:42 -07002588 kiocb->ki_flags |= IOCB_HIPRI;
2589 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002590 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002591 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002592 if (kiocb->ki_flags & IOCB_HIPRI)
2593 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002594 kiocb->ki_complete = io_complete_rw;
2595 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002596
Jens Axboe3529d8c2019-12-19 18:24:38 -07002597 req->rw.addr = READ_ONCE(sqe->addr);
2598 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002599 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002600 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002601}
2602
2603static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2604{
2605 switch (ret) {
2606 case -EIOCBQUEUED:
2607 break;
2608 case -ERESTARTSYS:
2609 case -ERESTARTNOINTR:
2610 case -ERESTARTNOHAND:
2611 case -ERESTART_RESTARTBLOCK:
2612 /*
2613 * We can't just restart the syscall, since previously
2614 * submitted sqes may already be in progress. Just fail this
2615 * IO with EINTR.
2616 */
2617 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002618 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002619 default:
2620 kiocb->ki_complete(kiocb, ret, 0);
2621 }
2622}
2623
Jens Axboea1d7c392020-06-22 11:09:46 -06002624static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2625 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002626{
Jens Axboeba042912019-12-25 16:33:42 -07002627 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2628
Jens Axboe227c0c92020-08-13 11:51:40 -06002629 /* add previously done IO, if any */
2630 if (req->io && req->io->rw.bytes_done > 0) {
2631 if (ret < 0)
2632 ret = req->io->rw.bytes_done;
2633 else
2634 ret += req->io->rw.bytes_done;
2635 }
2636
Jens Axboeba042912019-12-25 16:33:42 -07002637 if (req->flags & REQ_F_CUR_POS)
2638 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002639 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002640 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002641 else
2642 io_rw_done(kiocb, ret);
2643}
2644
Jens Axboe9adbd452019-12-20 08:45:55 -07002645static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002646 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002647{
Jens Axboe9adbd452019-12-20 08:45:55 -07002648 struct io_ring_ctx *ctx = req->ctx;
2649 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002650 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002651 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002652 size_t offset;
2653 u64 buf_addr;
2654
2655 /* attempt to use fixed buffers without having provided iovecs */
2656 if (unlikely(!ctx->user_bufs))
2657 return -EFAULT;
2658
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002659 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002660 if (unlikely(buf_index >= ctx->nr_user_bufs))
2661 return -EFAULT;
2662
2663 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2664 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002665 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002666
2667 /* overflow */
2668 if (buf_addr + len < buf_addr)
2669 return -EFAULT;
2670 /* not inside the mapped region */
2671 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2672 return -EFAULT;
2673
2674 /*
2675 * May not be a start of buffer, set size appropriately
2676 * and advance us to the beginning.
2677 */
2678 offset = buf_addr - imu->ubuf;
2679 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002680
2681 if (offset) {
2682 /*
2683 * Don't use iov_iter_advance() here, as it's really slow for
2684 * using the latter parts of a big fixed buffer - it iterates
2685 * over each segment manually. We can cheat a bit here, because
2686 * we know that:
2687 *
2688 * 1) it's a BVEC iter, we set it up
2689 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2690 * first and last bvec
2691 *
2692 * So just find our index, and adjust the iterator afterwards.
2693 * If the offset is within the first bvec (or the whole first
2694 * bvec, just use iov_iter_advance(). This makes it easier
2695 * since we can just skip the first segment, which may not
2696 * be PAGE_SIZE aligned.
2697 */
2698 const struct bio_vec *bvec = imu->bvec;
2699
2700 if (offset <= bvec->bv_len) {
2701 iov_iter_advance(iter, offset);
2702 } else {
2703 unsigned long seg_skip;
2704
2705 /* skip first vec */
2706 offset -= bvec->bv_len;
2707 seg_skip = 1 + (offset >> PAGE_SHIFT);
2708
2709 iter->bvec = bvec + seg_skip;
2710 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002711 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002712 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002713 }
2714 }
2715
Jens Axboe5e559562019-11-13 16:12:46 -07002716 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002717}
2718
Jens Axboebcda7ba2020-02-23 16:42:51 -07002719static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2720{
2721 if (needs_lock)
2722 mutex_unlock(&ctx->uring_lock);
2723}
2724
2725static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2726{
2727 /*
2728 * "Normal" inline submissions always hold the uring_lock, since we
2729 * grab it from the system call. Same is true for the SQPOLL offload.
2730 * The only exception is when we've detached the request and issue it
2731 * from an async worker thread, grab the lock for that case.
2732 */
2733 if (needs_lock)
2734 mutex_lock(&ctx->uring_lock);
2735}
2736
2737static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2738 int bgid, struct io_buffer *kbuf,
2739 bool needs_lock)
2740{
2741 struct io_buffer *head;
2742
2743 if (req->flags & REQ_F_BUFFER_SELECTED)
2744 return kbuf;
2745
2746 io_ring_submit_lock(req->ctx, needs_lock);
2747
2748 lockdep_assert_held(&req->ctx->uring_lock);
2749
2750 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2751 if (head) {
2752 if (!list_empty(&head->list)) {
2753 kbuf = list_last_entry(&head->list, struct io_buffer,
2754 list);
2755 list_del(&kbuf->list);
2756 } else {
2757 kbuf = head;
2758 idr_remove(&req->ctx->io_buffer_idr, bgid);
2759 }
2760 if (*len > kbuf->len)
2761 *len = kbuf->len;
2762 } else {
2763 kbuf = ERR_PTR(-ENOBUFS);
2764 }
2765
2766 io_ring_submit_unlock(req->ctx, needs_lock);
2767
2768 return kbuf;
2769}
2770
Jens Axboe4d954c22020-02-27 07:31:19 -07002771static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2772 bool needs_lock)
2773{
2774 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002775 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002776
2777 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002778 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002779 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2780 if (IS_ERR(kbuf))
2781 return kbuf;
2782 req->rw.addr = (u64) (unsigned long) kbuf;
2783 req->flags |= REQ_F_BUFFER_SELECTED;
2784 return u64_to_user_ptr(kbuf->addr);
2785}
2786
2787#ifdef CONFIG_COMPAT
2788static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2789 bool needs_lock)
2790{
2791 struct compat_iovec __user *uiov;
2792 compat_ssize_t clen;
2793 void __user *buf;
2794 ssize_t len;
2795
2796 uiov = u64_to_user_ptr(req->rw.addr);
2797 if (!access_ok(uiov, sizeof(*uiov)))
2798 return -EFAULT;
2799 if (__get_user(clen, &uiov->iov_len))
2800 return -EFAULT;
2801 if (clen < 0)
2802 return -EINVAL;
2803
2804 len = clen;
2805 buf = io_rw_buffer_select(req, &len, needs_lock);
2806 if (IS_ERR(buf))
2807 return PTR_ERR(buf);
2808 iov[0].iov_base = buf;
2809 iov[0].iov_len = (compat_size_t) len;
2810 return 0;
2811}
2812#endif
2813
2814static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2815 bool needs_lock)
2816{
2817 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2818 void __user *buf;
2819 ssize_t len;
2820
2821 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2822 return -EFAULT;
2823
2824 len = iov[0].iov_len;
2825 if (len < 0)
2826 return -EINVAL;
2827 buf = io_rw_buffer_select(req, &len, needs_lock);
2828 if (IS_ERR(buf))
2829 return PTR_ERR(buf);
2830 iov[0].iov_base = buf;
2831 iov[0].iov_len = len;
2832 return 0;
2833}
2834
2835static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2836 bool needs_lock)
2837{
Jens Axboedddb3e22020-06-04 11:27:01 -06002838 if (req->flags & REQ_F_BUFFER_SELECTED) {
2839 struct io_buffer *kbuf;
2840
2841 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2842 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2843 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002844 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002845 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002846 if (!req->rw.len)
2847 return 0;
2848 else if (req->rw.len > 1)
2849 return -EINVAL;
2850
2851#ifdef CONFIG_COMPAT
2852 if (req->ctx->compat)
2853 return io_compat_import(req, iov, needs_lock);
2854#endif
2855
2856 return __io_iov_buffer_select(req, iov, needs_lock);
2857}
2858
Jens Axboe8452fd02020-08-18 13:58:33 -07002859static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2860 struct iovec **iovec, struct iov_iter *iter,
2861 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002862{
Jens Axboe9adbd452019-12-20 08:45:55 -07002863 void __user *buf = u64_to_user_ptr(req->rw.addr);
2864 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002865 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002866 u8 opcode;
2867
Jens Axboed625c6e2019-12-17 19:53:05 -07002868 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002869 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002870 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002871 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002872 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002873
Jens Axboebcda7ba2020-02-23 16:42:51 -07002874 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002875 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002876 return -EINVAL;
2877
Jens Axboe3a6820f2019-12-22 15:19:35 -07002878 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002879 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002880 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002881 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002882 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002883 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002884 }
2885
Jens Axboe3a6820f2019-12-22 15:19:35 -07002886 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2887 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002888 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002889 }
2890
Jens Axboe4d954c22020-02-27 07:31:19 -07002891 if (req->flags & REQ_F_BUFFER_SELECT) {
2892 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002893 if (!ret) {
2894 ret = (*iovec)->iov_len;
2895 iov_iter_init(iter, rw, *iovec, 1, ret);
2896 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002897 *iovec = NULL;
2898 return ret;
2899 }
2900
Jens Axboe2b188cc2019-01-07 10:46:33 -07002901#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002902 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002903 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2904 iovec, iter);
2905#endif
2906
2907 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2908}
2909
Jens Axboe8452fd02020-08-18 13:58:33 -07002910static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2911 struct iovec **iovec, struct iov_iter *iter,
2912 bool needs_lock)
2913{
2914 if (!req->io)
2915 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2916 *iovec = NULL;
2917 return iov_iter_count(&req->io->rw.iter);
2918}
2919
Jens Axboe0fef9482020-08-26 10:36:20 -06002920static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2921{
2922 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2923}
2924
Jens Axboe32960612019-09-23 11:05:34 -06002925/*
2926 * For files that don't have ->read_iter() and ->write_iter(), handle them
2927 * by looping over ->read() or ->write() manually.
2928 */
2929static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2930 struct iov_iter *iter)
2931{
2932 ssize_t ret = 0;
2933
2934 /*
2935 * Don't support polled IO through this interface, and we can't
2936 * support non-blocking either. For the latter, this just causes
2937 * the kiocb to be handled from an async context.
2938 */
2939 if (kiocb->ki_flags & IOCB_HIPRI)
2940 return -EOPNOTSUPP;
2941 if (kiocb->ki_flags & IOCB_NOWAIT)
2942 return -EAGAIN;
2943
2944 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002945 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002946 ssize_t nr;
2947
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002948 if (!iov_iter_is_bvec(iter)) {
2949 iovec = iov_iter_iovec(iter);
2950 } else {
2951 /* fixed buffers import bvec */
2952 iovec.iov_base = kmap(iter->bvec->bv_page)
2953 + iter->iov_offset;
2954 iovec.iov_len = min(iter->count,
2955 iter->bvec->bv_len - iter->iov_offset);
2956 }
2957
Jens Axboe32960612019-09-23 11:05:34 -06002958 if (rw == READ) {
2959 nr = file->f_op->read(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 } else {
2962 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002963 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002964 }
2965
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002966 if (iov_iter_is_bvec(iter))
2967 kunmap(iter->bvec->bv_page);
2968
Jens Axboe32960612019-09-23 11:05:34 -06002969 if (nr < 0) {
2970 if (!ret)
2971 ret = nr;
2972 break;
2973 }
2974 ret += nr;
2975 if (nr != iovec.iov_len)
2976 break;
2977 iov_iter_advance(iter, nr);
2978 }
2979
2980 return ret;
2981}
2982
Jens Axboeff6165b2020-08-13 09:47:43 -06002983static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2984 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07002985{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002986 struct io_async_rw *rw = &req->io->rw;
2987
Jens Axboeff6165b2020-08-13 09:47:43 -06002988 memcpy(&rw->iter, iter, sizeof(*iter));
2989 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06002990 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06002991 /* can only be fixed buffers, no need to do anything */
2992 if (iter->type == ITER_BVEC)
2993 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002994 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06002995 unsigned iov_off = 0;
2996
2997 rw->iter.iov = rw->fast_iov;
2998 if (iter->iov != fast_iov) {
2999 iov_off = iter->iov - fast_iov;
3000 rw->iter.iov += iov_off;
3001 }
3002 if (rw->fast_iov != fast_iov)
3003 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003004 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003005 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06003006 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003007 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003008 }
3009}
3010
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003011static inline int __io_alloc_async_ctx(struct io_kiocb *req)
3012{
3013 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
3014 return req->io == NULL;
3015}
3016
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003017static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003018{
Jens Axboed3656342019-12-18 09:50:26 -07003019 if (!io_op_defs[req->opcode].async_ctx)
3020 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003021
3022 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003023}
3024
Jens Axboeff6165b2020-08-13 09:47:43 -06003025static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3026 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003027 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003028{
Jens Axboe227c0c92020-08-13 11:51:40 -06003029 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003030 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003031 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003032 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003033 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003034
Jens Axboeff6165b2020-08-13 09:47:43 -06003035 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003036 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003037 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003038}
3039
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003040static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3041 bool force_nonblock)
3042{
Jens Axboeff6165b2020-08-13 09:47:43 -06003043 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06003044 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003045 ssize_t ret;
3046
Jens Axboec183edf2020-09-04 22:36:52 -06003047 iorw->iter.iov = iov = iorw->fast_iov;
3048 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003049 if (unlikely(ret < 0))
3050 return ret;
3051
Jens Axboec183edf2020-09-04 22:36:52 -06003052 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003053 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003054 return 0;
3055}
3056
Jens Axboe3529d8c2019-12-19 18:24:38 -07003057static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3058 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003059{
3060 ssize_t ret;
3061
Jens Axboe3529d8c2019-12-19 18:24:38 -07003062 ret = io_prep_rw(req, sqe, force_nonblock);
3063 if (ret)
3064 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003065
Jens Axboe3529d8c2019-12-19 18:24:38 -07003066 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3067 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003068
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003069 /* either don't need iovec imported or already have it */
3070 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003071 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003072 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003073}
3074
Jens Axboec1dd91d2020-08-03 16:43:59 -06003075/*
3076 * This is our waitqueue callback handler, registered through lock_page_async()
3077 * when we initially tried to do the IO with the iocb armed our waitqueue.
3078 * This gets called when the page is unlocked, and we generally expect that to
3079 * happen when the page IO is completed and the page is now uptodate. This will
3080 * queue a task_work based retry of the operation, attempting to copy the data
3081 * again. If the latter fails because the page was NOT uptodate, then we will
3082 * do a thread based blocking retry of the operation. That's the unexpected
3083 * slow path.
3084 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003085static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3086 int sync, void *arg)
3087{
3088 struct wait_page_queue *wpq;
3089 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003090 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003091 int ret;
3092
3093 wpq = container_of(wait, struct wait_page_queue, wait);
3094
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003095 if (!wake_page_match(wpq, key))
3096 return 0;
3097
Hao Xuc8d317a2020-09-29 20:00:45 +08003098 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003099 list_del_init(&wait->entry);
3100
Pavel Begunkove7375122020-07-12 20:42:04 +03003101 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003102 percpu_ref_get(&req->ctx->refs);
3103
Jens Axboebcf5a062020-05-22 09:24:42 -06003104 /* submit ref gets dropped, acquire a new one */
3105 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003106 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003107 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003108 struct task_struct *tsk;
3109
Jens Axboebcf5a062020-05-22 09:24:42 -06003110 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003111 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003112 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003113 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003114 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003115 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003116 return 1;
3117}
3118
Jens Axboec1dd91d2020-08-03 16:43:59 -06003119/*
3120 * This controls whether a given IO request should be armed for async page
3121 * based retry. If we return false here, the request is handed to the async
3122 * worker threads for retry. If we're doing buffered reads on a regular file,
3123 * we prepare a private wait_page_queue entry and retry the operation. This
3124 * will either succeed because the page is now uptodate and unlocked, or it
3125 * will register a callback when the page is unlocked at IO completion. Through
3126 * that callback, io_uring uses task_work to setup a retry of the operation.
3127 * That retry will attempt the buffered read again. The retry will generally
3128 * succeed, or in rare cases where it fails, we then fall back to using the
3129 * async worker threads for a blocking retry.
3130 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003131static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003132{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003133 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003134 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003135
3136 /* never retry for NOWAIT, we just complete with -EAGAIN */
3137 if (req->flags & REQ_F_NOWAIT)
3138 return false;
3139
Jens Axboe227c0c92020-08-13 11:51:40 -06003140 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003141 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003142 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003143
Jens Axboebcf5a062020-05-22 09:24:42 -06003144 /*
3145 * just use poll if we can, and don't attempt if the fs doesn't
3146 * support callback based unlocks
3147 */
3148 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3149 return false;
3150
Jens Axboe3b2a4432020-08-16 10:58:43 -07003151 wait->wait.func = io_async_buf_func;
3152 wait->wait.private = req;
3153 wait->wait.flags = 0;
3154 INIT_LIST_HEAD(&wait->wait.entry);
3155 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003156 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003157 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003158 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003159}
3160
3161static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3162{
3163 if (req->file->f_op->read_iter)
3164 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003165 else if (req->file->f_op->read)
3166 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3167 else
3168 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003169}
3170
Jens Axboea1d7c392020-06-22 11:09:46 -06003171static int io_read(struct io_kiocb *req, bool force_nonblock,
3172 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003173{
3174 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003175 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003176 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003177 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003178 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003179 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003180
Jens Axboeff6165b2020-08-13 09:47:43 -06003181 if (req->io)
3182 iter = &req->io->rw.iter;
3183
3184 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003185 if (ret < 0)
3186 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003187 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003188 io_size = ret;
3189 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003190 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003191
Jens Axboefd6c2e42019-12-18 12:19:41 -07003192 /* Ensure we clear previously set non-block flag */
3193 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003194 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003195
Pavel Begunkov24c74672020-06-21 13:09:51 +03003196 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003197 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3198 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003199 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003200
Jens Axboe0fef9482020-08-26 10:36:20 -06003201 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003202 if (unlikely(ret))
3203 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003204
Jens Axboe227c0c92020-08-13 11:51:40 -06003205 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003206
Jens Axboe227c0c92020-08-13 11:51:40 -06003207 if (!ret) {
3208 goto done;
3209 } else if (ret == -EIOCBQUEUED) {
3210 ret = 0;
3211 goto out_free;
3212 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003213 /* IOPOLL retry should happen for io-wq threads */
3214 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003215 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003216 /* no retry on NONBLOCK marked file */
3217 if (req->file->f_flags & O_NONBLOCK)
3218 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003219 /* some cases will consume bytes even on error returns */
3220 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003221 ret = 0;
3222 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003223 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003224 /* make sure -ERESTARTSYS -> -EINTR is done */
3225 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003226 }
3227
3228 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003229 if (!iov_iter_count(iter) || !force_nonblock ||
3230 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003231 goto done;
3232
3233 io_size -= ret;
3234copy_iov:
3235 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3236 if (ret2) {
3237 ret = ret2;
3238 goto out_free;
3239 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003240 if (no_async)
3241 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003242 /* it's copied and will be cleaned with ->io */
3243 iovec = NULL;
3244 /* now use our persistent iterator, if we aren't already */
3245 iter = &req->io->rw.iter;
3246retry:
3247 req->io->rw.bytes_done += ret;
3248 /* if we can retry, do so with the callbacks armed */
3249 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003250 kiocb->ki_flags &= ~IOCB_WAITQ;
3251 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003252 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003253
3254 /*
3255 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3256 * get -EIOCBQUEUED, then we'll get a notification when the desired
3257 * page gets unlocked. We can also get a partial read here, and if we
3258 * do, then just retry at the new offset.
3259 */
3260 ret = io_iter_do_read(req, iter);
3261 if (ret == -EIOCBQUEUED) {
3262 ret = 0;
3263 goto out_free;
3264 } else if (ret > 0 && ret < io_size) {
3265 /* we got some bytes, but not all. retry. */
3266 goto retry;
3267 }
3268done:
3269 kiocb_done(kiocb, ret, cs);
3270 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003271out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003272 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003273 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003274 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003275 return ret;
3276}
3277
Jens Axboe3529d8c2019-12-19 18:24:38 -07003278static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3279 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003280{
3281 ssize_t ret;
3282
Jens Axboe3529d8c2019-12-19 18:24:38 -07003283 ret = io_prep_rw(req, sqe, force_nonblock);
3284 if (ret)
3285 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003286
Jens Axboe3529d8c2019-12-19 18:24:38 -07003287 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3288 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003289
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003290 /* either don't need iovec imported or already have it */
3291 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003292 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003293 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003294}
3295
Jens Axboea1d7c392020-06-22 11:09:46 -06003296static int io_write(struct io_kiocb *req, bool force_nonblock,
3297 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003298{
3299 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003300 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003301 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003302 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003303 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003304
Jens Axboeff6165b2020-08-13 09:47:43 -06003305 if (req->io)
3306 iter = &req->io->rw.iter;
3307
3308 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003309 if (ret < 0)
3310 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003311 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003312 io_size = ret;
3313 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003314
Jens Axboefd6c2e42019-12-18 12:19:41 -07003315 /* Ensure we clear previously set non-block flag */
3316 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003317 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003318
Pavel Begunkov24c74672020-06-21 13:09:51 +03003319 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003320 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003321 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003322
Jens Axboe10d59342019-12-09 20:16:22 -07003323 /* file path doesn't support NOWAIT for non-direct_IO */
3324 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3325 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003326 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003327
Jens Axboe0fef9482020-08-26 10:36:20 -06003328 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003329 if (unlikely(ret))
3330 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003331
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003332 /*
3333 * Open-code file_start_write here to grab freeze protection,
3334 * which will be released by another thread in
3335 * io_complete_rw(). Fool lockdep by telling it the lock got
3336 * released so that it doesn't complain about the held lock when
3337 * we return to userspace.
3338 */
3339 if (req->flags & REQ_F_ISREG) {
3340 __sb_start_write(file_inode(req->file)->i_sb,
3341 SB_FREEZE_WRITE, true);
3342 __sb_writers_release(file_inode(req->file)->i_sb,
3343 SB_FREEZE_WRITE);
3344 }
3345 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003346
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003347 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003348 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003349 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003350 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003351 else
3352 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003353
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003354 /*
3355 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3356 * retry them without IOCB_NOWAIT.
3357 */
3358 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3359 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003360 /* no retry on NONBLOCK marked file */
3361 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3362 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003363 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003364 /* IOPOLL retry should happen for io-wq threads */
3365 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3366 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003367done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003368 kiocb_done(kiocb, ret2, cs);
3369 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003370copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003371 /* some cases will consume bytes even on error returns */
3372 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003373 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003374 if (!ret)
3375 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003376 }
Jens Axboe31b51512019-01-18 22:56:34 -07003377out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003378 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003379 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003380 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003381 return ret;
3382}
3383
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003384static int __io_splice_prep(struct io_kiocb *req,
3385 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003386{
3387 struct io_splice* sp = &req->splice;
3388 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3389 int ret;
3390
3391 if (req->flags & REQ_F_NEED_CLEANUP)
3392 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003393 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3394 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003395
3396 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003397 sp->len = READ_ONCE(sqe->len);
3398 sp->flags = READ_ONCE(sqe->splice_flags);
3399
3400 if (unlikely(sp->flags & ~valid_flags))
3401 return -EINVAL;
3402
3403 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3404 (sp->flags & SPLICE_F_FD_IN_FIXED));
3405 if (ret)
3406 return ret;
3407 req->flags |= REQ_F_NEED_CLEANUP;
3408
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003409 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3410 /*
3411 * Splice operation will be punted aync, and here need to
3412 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3413 */
3414 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003415 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003416 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003417
3418 return 0;
3419}
3420
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003421static int io_tee_prep(struct io_kiocb *req,
3422 const struct io_uring_sqe *sqe)
3423{
3424 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3425 return -EINVAL;
3426 return __io_splice_prep(req, sqe);
3427}
3428
3429static int io_tee(struct io_kiocb *req, bool force_nonblock)
3430{
3431 struct io_splice *sp = &req->splice;
3432 struct file *in = sp->file_in;
3433 struct file *out = sp->file_out;
3434 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3435 long ret = 0;
3436
3437 if (force_nonblock)
3438 return -EAGAIN;
3439 if (sp->len)
3440 ret = do_tee(in, out, sp->len, flags);
3441
3442 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3443 req->flags &= ~REQ_F_NEED_CLEANUP;
3444
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003445 if (ret != sp->len)
3446 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003447 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003448 return 0;
3449}
3450
3451static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3452{
3453 struct io_splice* sp = &req->splice;
3454
3455 sp->off_in = READ_ONCE(sqe->splice_off_in);
3456 sp->off_out = READ_ONCE(sqe->off);
3457 return __io_splice_prep(req, sqe);
3458}
3459
Pavel Begunkov014db002020-03-03 21:33:12 +03003460static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003461{
3462 struct io_splice *sp = &req->splice;
3463 struct file *in = sp->file_in;
3464 struct file *out = sp->file_out;
3465 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3466 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003467 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003468
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003469 if (force_nonblock)
3470 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003471
3472 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3473 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003474
Jens Axboe948a7742020-05-17 14:21:38 -06003475 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003476 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003477
3478 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3479 req->flags &= ~REQ_F_NEED_CLEANUP;
3480
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003481 if (ret != sp->len)
3482 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003483 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003484 return 0;
3485}
3486
Jens Axboe2b188cc2019-01-07 10:46:33 -07003487/*
3488 * IORING_OP_NOP just posts a completion event, nothing else.
3489 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003490static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003491{
3492 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493
Jens Axboedef596e2019-01-09 08:59:42 -07003494 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3495 return -EINVAL;
3496
Jens Axboe229a7b62020-06-22 10:13:11 -06003497 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003498 return 0;
3499}
3500
Jens Axboe3529d8c2019-12-19 18:24:38 -07003501static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003502{
Jens Axboe6b063142019-01-10 22:13:58 -07003503 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003504
Jens Axboe09bb8392019-03-13 12:39:28 -06003505 if (!req->file)
3506 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003507
Jens Axboe6b063142019-01-10 22:13:58 -07003508 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003509 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003510 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003511 return -EINVAL;
3512
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003513 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3514 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3515 return -EINVAL;
3516
3517 req->sync.off = READ_ONCE(sqe->off);
3518 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003519 return 0;
3520}
3521
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003522static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003523{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003524 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003525 int ret;
3526
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003527 /* fsync always requires a blocking context */
3528 if (force_nonblock)
3529 return -EAGAIN;
3530
Jens Axboe9adbd452019-12-20 08:45:55 -07003531 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003532 end > 0 ? end : LLONG_MAX,
3533 req->sync.flags & IORING_FSYNC_DATASYNC);
3534 if (ret < 0)
3535 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003536 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003537 return 0;
3538}
3539
Jens Axboed63d1b52019-12-10 10:38:56 -07003540static int io_fallocate_prep(struct io_kiocb *req,
3541 const struct io_uring_sqe *sqe)
3542{
3543 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3544 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003545 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3546 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003547
3548 req->sync.off = READ_ONCE(sqe->off);
3549 req->sync.len = READ_ONCE(sqe->addr);
3550 req->sync.mode = READ_ONCE(sqe->len);
3551 return 0;
3552}
3553
Pavel Begunkov014db002020-03-03 21:33:12 +03003554static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003555{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003556 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003557
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003558 /* fallocate always requiring blocking context */
3559 if (force_nonblock)
3560 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003561 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3562 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003563 if (ret < 0)
3564 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003565 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003566 return 0;
3567}
3568
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003569static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003570{
Jens Axboef8748882020-01-08 17:47:02 -07003571 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003572 int ret;
3573
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003574 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003575 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003576 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003577 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003578
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003579 /* open.how should be already initialised */
3580 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003581 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003582
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003583 req->open.dfd = READ_ONCE(sqe->fd);
3584 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003585 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003586 if (IS_ERR(req->open.filename)) {
3587 ret = PTR_ERR(req->open.filename);
3588 req->open.filename = NULL;
3589 return ret;
3590 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003591 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003592 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003593 return 0;
3594}
3595
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003596static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3597{
3598 u64 flags, mode;
3599
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003600 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3601 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003602 if (req->flags & REQ_F_NEED_CLEANUP)
3603 return 0;
3604 mode = READ_ONCE(sqe->len);
3605 flags = READ_ONCE(sqe->open_flags);
3606 req->open.how = build_open_how(flags, mode);
3607 return __io_openat_prep(req, sqe);
3608}
3609
Jens Axboecebdb982020-01-08 17:59:24 -07003610static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3611{
3612 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003613 size_t len;
3614 int ret;
3615
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003616 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3617 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003618 if (req->flags & REQ_F_NEED_CLEANUP)
3619 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003620 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3621 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003622 if (len < OPEN_HOW_SIZE_VER0)
3623 return -EINVAL;
3624
3625 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3626 len);
3627 if (ret)
3628 return ret;
3629
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003630 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003631}
3632
Pavel Begunkov014db002020-03-03 21:33:12 +03003633static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003634{
3635 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003636 struct file *file;
3637 int ret;
3638
Jens Axboef86cd202020-01-29 13:46:44 -07003639 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003640 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003641
Jens Axboecebdb982020-01-08 17:59:24 -07003642 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003643 if (ret)
3644 goto err;
3645
Jens Axboe4022e7a2020-03-19 19:23:18 -06003646 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003647 if (ret < 0)
3648 goto err;
3649
3650 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3651 if (IS_ERR(file)) {
3652 put_unused_fd(ret);
3653 ret = PTR_ERR(file);
3654 } else {
3655 fsnotify_open(file);
3656 fd_install(ret, file);
3657 }
3658err:
3659 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003660 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003661 if (ret < 0)
3662 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003663 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003664 return 0;
3665}
3666
Pavel Begunkov014db002020-03-03 21:33:12 +03003667static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003668{
Pavel Begunkov014db002020-03-03 21:33:12 +03003669 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003670}
3671
Jens Axboe067524e2020-03-02 16:32:28 -07003672static int io_remove_buffers_prep(struct io_kiocb *req,
3673 const struct io_uring_sqe *sqe)
3674{
3675 struct io_provide_buf *p = &req->pbuf;
3676 u64 tmp;
3677
3678 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3679 return -EINVAL;
3680
3681 tmp = READ_ONCE(sqe->fd);
3682 if (!tmp || tmp > USHRT_MAX)
3683 return -EINVAL;
3684
3685 memset(p, 0, sizeof(*p));
3686 p->nbufs = tmp;
3687 p->bgid = READ_ONCE(sqe->buf_group);
3688 return 0;
3689}
3690
3691static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3692 int bgid, unsigned nbufs)
3693{
3694 unsigned i = 0;
3695
3696 /* shouldn't happen */
3697 if (!nbufs)
3698 return 0;
3699
3700 /* the head kbuf is the list itself */
3701 while (!list_empty(&buf->list)) {
3702 struct io_buffer *nxt;
3703
3704 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3705 list_del(&nxt->list);
3706 kfree(nxt);
3707 if (++i == nbufs)
3708 return i;
3709 }
3710 i++;
3711 kfree(buf);
3712 idr_remove(&ctx->io_buffer_idr, bgid);
3713
3714 return i;
3715}
3716
Jens Axboe229a7b62020-06-22 10:13:11 -06003717static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3718 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003719{
3720 struct io_provide_buf *p = &req->pbuf;
3721 struct io_ring_ctx *ctx = req->ctx;
3722 struct io_buffer *head;
3723 int ret = 0;
3724
3725 io_ring_submit_lock(ctx, !force_nonblock);
3726
3727 lockdep_assert_held(&ctx->uring_lock);
3728
3729 ret = -ENOENT;
3730 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3731 if (head)
3732 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3733
3734 io_ring_submit_lock(ctx, !force_nonblock);
3735 if (ret < 0)
3736 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003737 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003738 return 0;
3739}
3740
Jens Axboeddf0322d2020-02-23 16:41:33 -07003741static int io_provide_buffers_prep(struct io_kiocb *req,
3742 const struct io_uring_sqe *sqe)
3743{
3744 struct io_provide_buf *p = &req->pbuf;
3745 u64 tmp;
3746
3747 if (sqe->ioprio || sqe->rw_flags)
3748 return -EINVAL;
3749
3750 tmp = READ_ONCE(sqe->fd);
3751 if (!tmp || tmp > USHRT_MAX)
3752 return -E2BIG;
3753 p->nbufs = tmp;
3754 p->addr = READ_ONCE(sqe->addr);
3755 p->len = READ_ONCE(sqe->len);
3756
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003757 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003758 return -EFAULT;
3759
3760 p->bgid = READ_ONCE(sqe->buf_group);
3761 tmp = READ_ONCE(sqe->off);
3762 if (tmp > USHRT_MAX)
3763 return -E2BIG;
3764 p->bid = tmp;
3765 return 0;
3766}
3767
3768static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3769{
3770 struct io_buffer *buf;
3771 u64 addr = pbuf->addr;
3772 int i, bid = pbuf->bid;
3773
3774 for (i = 0; i < pbuf->nbufs; i++) {
3775 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3776 if (!buf)
3777 break;
3778
3779 buf->addr = addr;
3780 buf->len = pbuf->len;
3781 buf->bid = bid;
3782 addr += pbuf->len;
3783 bid++;
3784 if (!*head) {
3785 INIT_LIST_HEAD(&buf->list);
3786 *head = buf;
3787 } else {
3788 list_add_tail(&buf->list, &(*head)->list);
3789 }
3790 }
3791
3792 return i ? i : -ENOMEM;
3793}
3794
Jens Axboe229a7b62020-06-22 10:13:11 -06003795static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3796 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003797{
3798 struct io_provide_buf *p = &req->pbuf;
3799 struct io_ring_ctx *ctx = req->ctx;
3800 struct io_buffer *head, *list;
3801 int ret = 0;
3802
3803 io_ring_submit_lock(ctx, !force_nonblock);
3804
3805 lockdep_assert_held(&ctx->uring_lock);
3806
3807 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3808
3809 ret = io_add_buffers(p, &head);
3810 if (ret < 0)
3811 goto out;
3812
3813 if (!list) {
3814 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3815 GFP_KERNEL);
3816 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003817 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003818 goto out;
3819 }
3820 }
3821out:
3822 io_ring_submit_unlock(ctx, !force_nonblock);
3823 if (ret < 0)
3824 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003825 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003826 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003827}
3828
Jens Axboe3e4827b2020-01-08 15:18:09 -07003829static int io_epoll_ctl_prep(struct io_kiocb *req,
3830 const struct io_uring_sqe *sqe)
3831{
3832#if defined(CONFIG_EPOLL)
3833 if (sqe->ioprio || sqe->buf_index)
3834 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003835 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003836 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003837
3838 req->epoll.epfd = READ_ONCE(sqe->fd);
3839 req->epoll.op = READ_ONCE(sqe->len);
3840 req->epoll.fd = READ_ONCE(sqe->off);
3841
3842 if (ep_op_has_event(req->epoll.op)) {
3843 struct epoll_event __user *ev;
3844
3845 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3846 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3847 return -EFAULT;
3848 }
3849
3850 return 0;
3851#else
3852 return -EOPNOTSUPP;
3853#endif
3854}
3855
Jens Axboe229a7b62020-06-22 10:13:11 -06003856static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3857 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003858{
3859#if defined(CONFIG_EPOLL)
3860 struct io_epoll *ie = &req->epoll;
3861 int ret;
3862
3863 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3864 if (force_nonblock && ret == -EAGAIN)
3865 return -EAGAIN;
3866
3867 if (ret < 0)
3868 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003869 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003870 return 0;
3871#else
3872 return -EOPNOTSUPP;
3873#endif
3874}
3875
Jens Axboec1ca7572019-12-25 22:18:28 -07003876static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3877{
3878#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3879 if (sqe->ioprio || sqe->buf_index || sqe->off)
3880 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003881 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3882 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003883
3884 req->madvise.addr = READ_ONCE(sqe->addr);
3885 req->madvise.len = READ_ONCE(sqe->len);
3886 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3887 return 0;
3888#else
3889 return -EOPNOTSUPP;
3890#endif
3891}
3892
Pavel Begunkov014db002020-03-03 21:33:12 +03003893static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003894{
3895#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3896 struct io_madvise *ma = &req->madvise;
3897 int ret;
3898
3899 if (force_nonblock)
3900 return -EAGAIN;
3901
3902 ret = do_madvise(ma->addr, ma->len, ma->advice);
3903 if (ret < 0)
3904 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003905 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003906 return 0;
3907#else
3908 return -EOPNOTSUPP;
3909#endif
3910}
3911
Jens Axboe4840e412019-12-25 22:03:45 -07003912static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3913{
3914 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3915 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003916 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3917 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003918
3919 req->fadvise.offset = READ_ONCE(sqe->off);
3920 req->fadvise.len = READ_ONCE(sqe->len);
3921 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3922 return 0;
3923}
3924
Pavel Begunkov014db002020-03-03 21:33:12 +03003925static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003926{
3927 struct io_fadvise *fa = &req->fadvise;
3928 int ret;
3929
Jens Axboe3e694262020-02-01 09:22:49 -07003930 if (force_nonblock) {
3931 switch (fa->advice) {
3932 case POSIX_FADV_NORMAL:
3933 case POSIX_FADV_RANDOM:
3934 case POSIX_FADV_SEQUENTIAL:
3935 break;
3936 default:
3937 return -EAGAIN;
3938 }
3939 }
Jens Axboe4840e412019-12-25 22:03:45 -07003940
3941 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3942 if (ret < 0)
3943 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003944 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003945 return 0;
3946}
3947
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003948static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3949{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003950 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003951 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003952 if (sqe->ioprio || sqe->buf_index)
3953 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003954 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003955 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003956
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003957 req->statx.dfd = READ_ONCE(sqe->fd);
3958 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003959 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003960 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3961 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003962
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003963 return 0;
3964}
3965
Pavel Begunkov014db002020-03-03 21:33:12 +03003966static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003967{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003968 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003969 int ret;
3970
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003971 if (force_nonblock) {
3972 /* only need file table for an actual valid fd */
3973 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3974 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003975 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003976 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003977
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003978 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3979 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003980
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003981 if (ret < 0)
3982 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003983 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003984 return 0;
3985}
3986
Jens Axboeb5dba592019-12-11 14:02:38 -07003987static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3988{
3989 /*
3990 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003991 * leave the 'file' in an undeterminate state, and here need to modify
3992 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003993 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003994 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003995 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3996
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003997 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3998 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003999 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4000 sqe->rw_flags || sqe->buf_index)
4001 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004002 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004003 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004004
4005 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004006 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004007 return -EBADF;
4008
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004009 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004010 return 0;
4011}
4012
Jens Axboe229a7b62020-06-22 10:13:11 -06004013static int io_close(struct io_kiocb *req, bool force_nonblock,
4014 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004015{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004016 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004017 int ret;
4018
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004019 /* might be already done during nonblock submission */
4020 if (!close->put_file) {
4021 ret = __close_fd_get_file(close->fd, &close->put_file);
4022 if (ret < 0)
4023 return (ret == -ENOENT) ? -EBADF : ret;
4024 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004025
4026 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004027 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004028 /* was never set, but play safe */
4029 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004030 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004031 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004032 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004033 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004034
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004035 /* No ->flush() or already async, safely close from here */
4036 ret = filp_close(close->put_file, req->work.files);
4037 if (ret < 0)
4038 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004039 fput(close->put_file);
4040 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004041 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004042 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004043}
4044
Jens Axboe3529d8c2019-12-19 18:24:38 -07004045static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004046{
4047 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004048
4049 if (!req->file)
4050 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004051
4052 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4053 return -EINVAL;
4054 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4055 return -EINVAL;
4056
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004057 req->sync.off = READ_ONCE(sqe->off);
4058 req->sync.len = READ_ONCE(sqe->len);
4059 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004060 return 0;
4061}
4062
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004063static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004064{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004065 int ret;
4066
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004067 /* sync_file_range always requires a blocking context */
4068 if (force_nonblock)
4069 return -EAGAIN;
4070
Jens Axboe9adbd452019-12-20 08:45:55 -07004071 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004072 req->sync.flags);
4073 if (ret < 0)
4074 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004075 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004076 return 0;
4077}
4078
YueHaibing469956e2020-03-04 15:53:52 +08004079#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004080static int io_setup_async_msg(struct io_kiocb *req,
4081 struct io_async_msghdr *kmsg)
4082{
4083 if (req->io)
4084 return -EAGAIN;
4085 if (io_alloc_async_ctx(req)) {
4086 if (kmsg->iov != kmsg->fast_iov)
4087 kfree(kmsg->iov);
4088 return -ENOMEM;
4089 }
4090 req->flags |= REQ_F_NEED_CLEANUP;
4091 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4092 return -EAGAIN;
4093}
4094
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004095static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4096 struct io_async_msghdr *iomsg)
4097{
4098 iomsg->iov = iomsg->fast_iov;
4099 iomsg->msg.msg_name = &iomsg->addr;
4100 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4101 req->sr_msg.msg_flags, &iomsg->iov);
4102}
4103
Jens Axboe3529d8c2019-12-19 18:24:38 -07004104static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004105{
Jens Axboee47293f2019-12-20 08:58:21 -07004106 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004107 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004108 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004109
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004110 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4111 return -EINVAL;
4112
Jens Axboee47293f2019-12-20 08:58:21 -07004113 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004114 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004115 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004116
Jens Axboed8768362020-02-27 14:17:49 -07004117#ifdef CONFIG_COMPAT
4118 if (req->ctx->compat)
4119 sr->msg_flags |= MSG_CMSG_COMPAT;
4120#endif
4121
Jens Axboefddafac2020-01-04 20:19:44 -07004122 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004123 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004124 /* iovec is already imported */
4125 if (req->flags & REQ_F_NEED_CLEANUP)
4126 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004127
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004128 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004129 if (!ret)
4130 req->flags |= REQ_F_NEED_CLEANUP;
4131 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004132}
4133
Jens Axboe229a7b62020-06-22 10:13:11 -06004134static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4135 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004136{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004137 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004138 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004139 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004140 int ret;
4141
Jens Axboe03b12302019-12-02 18:50:25 -07004142 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004143 if (unlikely(!sock))
4144 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004145
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004146 if (req->io) {
4147 kmsg = &req->io->msg;
4148 kmsg->msg.msg_name = &req->io->msg.addr;
4149 /* if iov is set, it's allocated already */
4150 if (!kmsg->iov)
4151 kmsg->iov = kmsg->fast_iov;
4152 kmsg->msg.msg_iter.iov = kmsg->iov;
4153 } else {
4154 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004155 if (ret)
4156 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004157 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004158 }
4159
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004160 flags = req->sr_msg.msg_flags;
4161 if (flags & MSG_DONTWAIT)
4162 req->flags |= REQ_F_NOWAIT;
4163 else if (force_nonblock)
4164 flags |= MSG_DONTWAIT;
4165
4166 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4167 if (force_nonblock && ret == -EAGAIN)
4168 return io_setup_async_msg(req, kmsg);
4169 if (ret == -ERESTARTSYS)
4170 ret = -EINTR;
4171
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004172 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004173 kfree(kmsg->iov);
4174 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004175 if (ret < 0)
4176 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004177 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004178 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004179}
4180
Jens Axboe229a7b62020-06-22 10:13:11 -06004181static int io_send(struct io_kiocb *req, bool force_nonblock,
4182 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004183{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004184 struct io_sr_msg *sr = &req->sr_msg;
4185 struct msghdr msg;
4186 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004187 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004188 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004189 int ret;
4190
4191 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004192 if (unlikely(!sock))
4193 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004194
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004195 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4196 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004197 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004198
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004199 msg.msg_name = NULL;
4200 msg.msg_control = NULL;
4201 msg.msg_controllen = 0;
4202 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004203
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004204 flags = req->sr_msg.msg_flags;
4205 if (flags & MSG_DONTWAIT)
4206 req->flags |= REQ_F_NOWAIT;
4207 else if (force_nonblock)
4208 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004209
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004210 msg.msg_flags = flags;
4211 ret = sock_sendmsg(sock, &msg);
4212 if (force_nonblock && ret == -EAGAIN)
4213 return -EAGAIN;
4214 if (ret == -ERESTARTSYS)
4215 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004216
Jens Axboe03b12302019-12-02 18:50:25 -07004217 if (ret < 0)
4218 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004219 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004220 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004221}
4222
Pavel Begunkov1400e692020-07-12 20:41:05 +03004223static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4224 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004225{
4226 struct io_sr_msg *sr = &req->sr_msg;
4227 struct iovec __user *uiov;
4228 size_t iov_len;
4229 int ret;
4230
Pavel Begunkov1400e692020-07-12 20:41:05 +03004231 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4232 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004233 if (ret)
4234 return ret;
4235
4236 if (req->flags & REQ_F_BUFFER_SELECT) {
4237 if (iov_len > 1)
4238 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004239 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004240 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004241 sr->len = iomsg->iov[0].iov_len;
4242 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004243 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004244 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004245 } else {
4246 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004247 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004248 if (ret > 0)
4249 ret = 0;
4250 }
4251
4252 return ret;
4253}
4254
4255#ifdef CONFIG_COMPAT
4256static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004257 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004258{
4259 struct compat_msghdr __user *msg_compat;
4260 struct io_sr_msg *sr = &req->sr_msg;
4261 struct compat_iovec __user *uiov;
4262 compat_uptr_t ptr;
4263 compat_size_t len;
4264 int ret;
4265
Pavel Begunkov270a5942020-07-12 20:41:04 +03004266 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004267 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004268 &ptr, &len);
4269 if (ret)
4270 return ret;
4271
4272 uiov = compat_ptr(ptr);
4273 if (req->flags & REQ_F_BUFFER_SELECT) {
4274 compat_ssize_t clen;
4275
4276 if (len > 1)
4277 return -EINVAL;
4278 if (!access_ok(uiov, sizeof(*uiov)))
4279 return -EFAULT;
4280 if (__get_user(clen, &uiov->iov_len))
4281 return -EFAULT;
4282 if (clen < 0)
4283 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004284 sr->len = iomsg->iov[0].iov_len;
4285 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004286 } else {
4287 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004288 &iomsg->iov,
4289 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004290 if (ret < 0)
4291 return ret;
4292 }
4293
4294 return 0;
4295}
Jens Axboe03b12302019-12-02 18:50:25 -07004296#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004297
Pavel Begunkov1400e692020-07-12 20:41:05 +03004298static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4299 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004300{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004301 iomsg->msg.msg_name = &iomsg->addr;
4302 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004303
4304#ifdef CONFIG_COMPAT
4305 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004306 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004307#endif
4308
Pavel Begunkov1400e692020-07-12 20:41:05 +03004309 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004310}
4311
Jens Axboebcda7ba2020-02-23 16:42:51 -07004312static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004313 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004314{
4315 struct io_sr_msg *sr = &req->sr_msg;
4316 struct io_buffer *kbuf;
4317
Jens Axboebcda7ba2020-02-23 16:42:51 -07004318 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4319 if (IS_ERR(kbuf))
4320 return kbuf;
4321
4322 sr->kbuf = kbuf;
4323 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004324 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004325}
4326
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004327static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4328{
4329 return io_put_kbuf(req, req->sr_msg.kbuf);
4330}
4331
Jens Axboe3529d8c2019-12-19 18:24:38 -07004332static int io_recvmsg_prep(struct io_kiocb *req,
4333 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004334{
Jens Axboee47293f2019-12-20 08:58:21 -07004335 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004337 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004338
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004339 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4340 return -EINVAL;
4341
Jens Axboe3529d8c2019-12-19 18:24:38 -07004342 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004343 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004344 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004345 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004346
Jens Axboed8768362020-02-27 14:17:49 -07004347#ifdef CONFIG_COMPAT
4348 if (req->ctx->compat)
4349 sr->msg_flags |= MSG_CMSG_COMPAT;
4350#endif
4351
Jens Axboefddafac2020-01-04 20:19:44 -07004352 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004353 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004354 /* iovec is already imported */
4355 if (req->flags & REQ_F_NEED_CLEANUP)
4356 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004357
Pavel Begunkov1400e692020-07-12 20:41:05 +03004358 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004359 if (!ret)
4360 req->flags |= REQ_F_NEED_CLEANUP;
4361 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004362}
4363
Jens Axboe229a7b62020-06-22 10:13:11 -06004364static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4365 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004366{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004367 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004368 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004369 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004370 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004371 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004372
Jens Axboe0fa03c62019-04-19 13:34:07 -06004373 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004374 if (unlikely(!sock))
4375 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004376
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004377 if (req->io) {
4378 kmsg = &req->io->msg;
4379 kmsg->msg.msg_name = &req->io->msg.addr;
4380 /* if iov is set, it's allocated already */
4381 if (!kmsg->iov)
4382 kmsg->iov = kmsg->fast_iov;
4383 kmsg->msg.msg_iter.iov = kmsg->iov;
4384 } else {
4385 ret = io_recvmsg_copy_hdr(req, &iomsg);
4386 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004387 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004388 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004389 }
4390
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004391 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004392 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004393 if (IS_ERR(kbuf))
4394 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004395 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4396 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4397 1, req->sr_msg.len);
4398 }
4399
4400 flags = req->sr_msg.msg_flags;
4401 if (flags & MSG_DONTWAIT)
4402 req->flags |= REQ_F_NOWAIT;
4403 else if (force_nonblock)
4404 flags |= MSG_DONTWAIT;
4405
4406 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4407 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004408 if (force_nonblock && ret == -EAGAIN)
4409 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004410 if (ret == -ERESTARTSYS)
4411 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004412
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004413 if (req->flags & REQ_F_BUFFER_SELECTED)
4414 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004415 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004416 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004417 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004418 if (ret < 0)
4419 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004420 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004421 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004422}
4423
Jens Axboe229a7b62020-06-22 10:13:11 -06004424static int io_recv(struct io_kiocb *req, bool force_nonblock,
4425 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004426{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004427 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004428 struct io_sr_msg *sr = &req->sr_msg;
4429 struct msghdr msg;
4430 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004431 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004432 struct iovec iov;
4433 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004434 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004435
Jens Axboefddafac2020-01-04 20:19:44 -07004436 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004437 if (unlikely(!sock))
4438 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004439
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004440 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004441 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004442 if (IS_ERR(kbuf))
4443 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004444 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004445 }
4446
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004447 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004448 if (unlikely(ret))
4449 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004450
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004451 msg.msg_name = NULL;
4452 msg.msg_control = NULL;
4453 msg.msg_controllen = 0;
4454 msg.msg_namelen = 0;
4455 msg.msg_iocb = NULL;
4456 msg.msg_flags = 0;
4457
4458 flags = req->sr_msg.msg_flags;
4459 if (flags & MSG_DONTWAIT)
4460 req->flags |= REQ_F_NOWAIT;
4461 else if (force_nonblock)
4462 flags |= MSG_DONTWAIT;
4463
4464 ret = sock_recvmsg(sock, &msg, flags);
4465 if (force_nonblock && ret == -EAGAIN)
4466 return -EAGAIN;
4467 if (ret == -ERESTARTSYS)
4468 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004469out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004470 if (req->flags & REQ_F_BUFFER_SELECTED)
4471 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004472 if (ret < 0)
4473 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004474 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004475 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004476}
4477
Jens Axboe3529d8c2019-12-19 18:24:38 -07004478static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004479{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004480 struct io_accept *accept = &req->accept;
4481
Jens Axboe17f2fe32019-10-17 14:42:58 -06004482 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4483 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004484 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004485 return -EINVAL;
4486
Jens Axboed55e5f52019-12-11 16:12:15 -07004487 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4488 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004489 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004490 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004491 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004492}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004493
Jens Axboe229a7b62020-06-22 10:13:11 -06004494static int io_accept(struct io_kiocb *req, bool force_nonblock,
4495 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004496{
4497 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004498 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004499 int ret;
4500
Jiufei Xuee697dee2020-06-10 13:41:59 +08004501 if (req->file->f_flags & O_NONBLOCK)
4502 req->flags |= REQ_F_NOWAIT;
4503
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004504 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004505 accept->addr_len, accept->flags,
4506 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004507 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004508 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004509 if (ret < 0) {
4510 if (ret == -ERESTARTSYS)
4511 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004512 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004513 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004514 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004515 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004516}
4517
Jens Axboe3529d8c2019-12-19 18:24:38 -07004518static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004519{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004520 struct io_connect *conn = &req->connect;
4521 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004522
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004523 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4524 return -EINVAL;
4525 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4526 return -EINVAL;
4527
Jens Axboe3529d8c2019-12-19 18:24:38 -07004528 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4529 conn->addr_len = READ_ONCE(sqe->addr2);
4530
4531 if (!io)
4532 return 0;
4533
4534 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004535 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004536}
4537
Jens Axboe229a7b62020-06-22 10:13:11 -06004538static int io_connect(struct io_kiocb *req, bool force_nonblock,
4539 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004540{
Jens Axboef499a022019-12-02 16:28:46 -07004541 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004542 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004543 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004544
Jens Axboef499a022019-12-02 16:28:46 -07004545 if (req->io) {
4546 io = req->io;
4547 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004548 ret = move_addr_to_kernel(req->connect.addr,
4549 req->connect.addr_len,
4550 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004551 if (ret)
4552 goto out;
4553 io = &__io;
4554 }
4555
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004556 file_flags = force_nonblock ? O_NONBLOCK : 0;
4557
4558 ret = __sys_connect_file(req->file, &io->connect.address,
4559 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004560 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004561 if (req->io)
4562 return -EAGAIN;
4563 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004564 ret = -ENOMEM;
4565 goto out;
4566 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004567 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004568 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004569 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004570 if (ret == -ERESTARTSYS)
4571 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004572out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004573 if (ret < 0)
4574 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004575 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004576 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004577}
YueHaibing469956e2020-03-04 15:53:52 +08004578#else /* !CONFIG_NET */
4579static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4580{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004581 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004582}
4583
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004584static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4585 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004586{
YueHaibing469956e2020-03-04 15:53:52 +08004587 return -EOPNOTSUPP;
4588}
4589
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004590static int io_send(struct io_kiocb *req, bool force_nonblock,
4591 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004592{
4593 return -EOPNOTSUPP;
4594}
4595
4596static int io_recvmsg_prep(struct io_kiocb *req,
4597 const struct io_uring_sqe *sqe)
4598{
4599 return -EOPNOTSUPP;
4600}
4601
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004602static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4603 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004604{
4605 return -EOPNOTSUPP;
4606}
4607
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004608static int io_recv(struct io_kiocb *req, bool force_nonblock,
4609 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004610{
4611 return -EOPNOTSUPP;
4612}
4613
4614static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4615{
4616 return -EOPNOTSUPP;
4617}
4618
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004619static int io_accept(struct io_kiocb *req, bool force_nonblock,
4620 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004621{
4622 return -EOPNOTSUPP;
4623}
4624
4625static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4626{
4627 return -EOPNOTSUPP;
4628}
4629
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004630static int io_connect(struct io_kiocb *req, bool force_nonblock,
4631 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004632{
4633 return -EOPNOTSUPP;
4634}
4635#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004636
Jens Axboed7718a92020-02-14 22:23:12 -07004637struct io_poll_table {
4638 struct poll_table_struct pt;
4639 struct io_kiocb *req;
4640 int error;
4641};
4642
Jens Axboed7718a92020-02-14 22:23:12 -07004643static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4644 __poll_t mask, task_work_func_t func)
4645{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004646 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004647 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004648
4649 /* for instances that support it check for an event match first: */
4650 if (mask && !(mask & poll->events))
4651 return 0;
4652
4653 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4654
4655 list_del_init(&poll->wait.entry);
4656
Jens Axboed7718a92020-02-14 22:23:12 -07004657 req->result = mask;
4658 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004659 percpu_ref_get(&req->ctx->refs);
4660
Jens Axboed7718a92020-02-14 22:23:12 -07004661 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004662 * If we using the signalfd wait_queue_head for this wakeup, then
4663 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4664 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4665 * either, as the normal wakeup will suffice.
4666 */
4667 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4668
4669 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004670 * If this fails, then the task is exiting. When a task exits, the
4671 * work gets canceled, so just cancel this request as well instead
4672 * of executing it. We can't safely execute it anyway, as we may not
4673 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004674 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004675 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004676 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004677 struct task_struct *tsk;
4678
Jens Axboee3aabf92020-05-18 11:04:17 -06004679 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004680 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004681 task_work_add(tsk, &req->task_work, 0);
4682 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004683 }
Jens Axboed7718a92020-02-14 22:23:12 -07004684 return 1;
4685}
4686
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004687static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4688 __acquires(&req->ctx->completion_lock)
4689{
4690 struct io_ring_ctx *ctx = req->ctx;
4691
4692 if (!req->result && !READ_ONCE(poll->canceled)) {
4693 struct poll_table_struct pt = { ._key = poll->events };
4694
4695 req->result = vfs_poll(req->file, &pt) & poll->events;
4696 }
4697
4698 spin_lock_irq(&ctx->completion_lock);
4699 if (!req->result && !READ_ONCE(poll->canceled)) {
4700 add_wait_queue(poll->head, &poll->wait);
4701 return true;
4702 }
4703
4704 return false;
4705}
4706
Jens Axboed4e7cd32020-08-15 11:44:50 -07004707static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004708{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004709 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4710 if (req->opcode == IORING_OP_POLL_ADD)
4711 return (struct io_poll_iocb *) req->io;
4712 return req->apoll->double_poll;
4713}
4714
4715static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4716{
4717 if (req->opcode == IORING_OP_POLL_ADD)
4718 return &req->poll;
4719 return &req->apoll->poll;
4720}
4721
4722static void io_poll_remove_double(struct io_kiocb *req)
4723{
4724 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004725
4726 lockdep_assert_held(&req->ctx->completion_lock);
4727
4728 if (poll && poll->head) {
4729 struct wait_queue_head *head = poll->head;
4730
4731 spin_lock(&head->lock);
4732 list_del_init(&poll->wait.entry);
4733 if (poll->wait.private)
4734 refcount_dec(&req->refs);
4735 poll->head = NULL;
4736 spin_unlock(&head->lock);
4737 }
4738}
4739
4740static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4741{
4742 struct io_ring_ctx *ctx = req->ctx;
4743
Jens Axboed4e7cd32020-08-15 11:44:50 -07004744 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004745 req->poll.done = true;
4746 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4747 io_commit_cqring(ctx);
4748}
4749
4750static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4751{
4752 struct io_ring_ctx *ctx = req->ctx;
4753
4754 if (io_poll_rewait(req, &req->poll)) {
4755 spin_unlock_irq(&ctx->completion_lock);
4756 return;
4757 }
4758
4759 hash_del(&req->hash_node);
4760 io_poll_complete(req, req->result, 0);
4761 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004762 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004763 spin_unlock_irq(&ctx->completion_lock);
4764
4765 io_cqring_ev_posted(ctx);
4766}
4767
4768static void io_poll_task_func(struct callback_head *cb)
4769{
4770 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004771 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004772 struct io_kiocb *nxt = NULL;
4773
4774 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004775 if (nxt)
4776 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004777 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004778}
4779
4780static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4781 int sync, void *key)
4782{
4783 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004784 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004785 __poll_t mask = key_to_poll(key);
4786
4787 /* for instances that support it check for an event match first: */
4788 if (mask && !(mask & poll->events))
4789 return 0;
4790
Jens Axboe8706e042020-09-28 08:38:54 -06004791 list_del_init(&wait->entry);
4792
Jens Axboe807abcb2020-07-17 17:09:27 -06004793 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004794 bool done;
4795
Jens Axboe807abcb2020-07-17 17:09:27 -06004796 spin_lock(&poll->head->lock);
4797 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004798 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004799 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004800 /* make sure double remove sees this as being gone */
4801 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004802 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004803 if (!done)
4804 __io_async_wake(req, poll, mask, io_poll_task_func);
4805 }
4806 refcount_dec(&req->refs);
4807 return 1;
4808}
4809
4810static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4811 wait_queue_func_t wake_func)
4812{
4813 poll->head = NULL;
4814 poll->done = false;
4815 poll->canceled = false;
4816 poll->events = events;
4817 INIT_LIST_HEAD(&poll->wait.entry);
4818 init_waitqueue_func_entry(&poll->wait, wake_func);
4819}
4820
4821static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004822 struct wait_queue_head *head,
4823 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004824{
4825 struct io_kiocb *req = pt->req;
4826
4827 /*
4828 * If poll->head is already set, it's because the file being polled
4829 * uses multiple waitqueues for poll handling (eg one for read, one
4830 * for write). Setup a separate io_poll_iocb if this happens.
4831 */
4832 if (unlikely(poll->head)) {
4833 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004834 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004835 pt->error = -EINVAL;
4836 return;
4837 }
4838 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4839 if (!poll) {
4840 pt->error = -ENOMEM;
4841 return;
4842 }
4843 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4844 refcount_inc(&req->refs);
4845 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004846 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004847 }
4848
4849 pt->error = 0;
4850 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004851
4852 if (poll->events & EPOLLEXCLUSIVE)
4853 add_wait_queue_exclusive(head, &poll->wait);
4854 else
4855 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004856}
4857
4858static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4859 struct poll_table_struct *p)
4860{
4861 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004862 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004863
Jens Axboe807abcb2020-07-17 17:09:27 -06004864 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004865}
4866
Jens Axboed7718a92020-02-14 22:23:12 -07004867static void io_async_task_func(struct callback_head *cb)
4868{
4869 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4870 struct async_poll *apoll = req->apoll;
4871 struct io_ring_ctx *ctx = req->ctx;
4872
4873 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4874
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004875 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004876 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004877 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004878 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004879 }
4880
Jens Axboe31067252020-05-17 17:43:31 -06004881 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004882 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004883 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004884
Jens Axboed4e7cd32020-08-15 11:44:50 -07004885 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004886 spin_unlock_irq(&ctx->completion_lock);
4887
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004888 if (!READ_ONCE(apoll->poll.canceled))
4889 __io_req_task_submit(req);
4890 else
4891 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004892
Jens Axboe6d816e02020-08-11 08:04:14 -06004893 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004894 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004895 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004896}
4897
4898static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4899 void *key)
4900{
4901 struct io_kiocb *req = wait->private;
4902 struct io_poll_iocb *poll = &req->apoll->poll;
4903
4904 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4905 key_to_poll(key));
4906
4907 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4908}
4909
4910static void io_poll_req_insert(struct io_kiocb *req)
4911{
4912 struct io_ring_ctx *ctx = req->ctx;
4913 struct hlist_head *list;
4914
4915 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4916 hlist_add_head(&req->hash_node, list);
4917}
4918
4919static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4920 struct io_poll_iocb *poll,
4921 struct io_poll_table *ipt, __poll_t mask,
4922 wait_queue_func_t wake_func)
4923 __acquires(&ctx->completion_lock)
4924{
4925 struct io_ring_ctx *ctx = req->ctx;
4926 bool cancel = false;
4927
Jens Axboe18bceab2020-05-15 11:56:54 -06004928 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004929 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004930 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004931
4932 ipt->pt._key = mask;
4933 ipt->req = req;
4934 ipt->error = -EINVAL;
4935
Jens Axboed7718a92020-02-14 22:23:12 -07004936 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4937
4938 spin_lock_irq(&ctx->completion_lock);
4939 if (likely(poll->head)) {
4940 spin_lock(&poll->head->lock);
4941 if (unlikely(list_empty(&poll->wait.entry))) {
4942 if (ipt->error)
4943 cancel = true;
4944 ipt->error = 0;
4945 mask = 0;
4946 }
4947 if (mask || ipt->error)
4948 list_del_init(&poll->wait.entry);
4949 else if (cancel)
4950 WRITE_ONCE(poll->canceled, true);
4951 else if (!poll->done) /* actually waiting for an event */
4952 io_poll_req_insert(req);
4953 spin_unlock(&poll->head->lock);
4954 }
4955
4956 return mask;
4957}
4958
4959static bool io_arm_poll_handler(struct io_kiocb *req)
4960{
4961 const struct io_op_def *def = &io_op_defs[req->opcode];
4962 struct io_ring_ctx *ctx = req->ctx;
4963 struct async_poll *apoll;
4964 struct io_poll_table ipt;
4965 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004966 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004967
4968 if (!req->file || !file_can_poll(req->file))
4969 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004970 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004971 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004972 if (def->pollin)
4973 rw = READ;
4974 else if (def->pollout)
4975 rw = WRITE;
4976 else
4977 return false;
4978 /* if we can't nonblock try, then no point in arming a poll handler */
4979 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004980 return false;
4981
4982 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4983 if (unlikely(!apoll))
4984 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004985 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004986
4987 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07004988 req->apoll = apoll;
4989 INIT_HLIST_NODE(&req->hash_node);
4990
Nathan Chancellor8755d972020-03-02 16:01:19 -07004991 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004992 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004993 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004994 if (def->pollout)
4995 mask |= POLLOUT | POLLWRNORM;
4996 mask |= POLLERR | POLLPRI;
4997
4998 ipt.pt._qproc = io_async_queue_proc;
4999
5000 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5001 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005002 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005003 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005004 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005005 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005006 kfree(apoll);
5007 return false;
5008 }
5009 spin_unlock_irq(&ctx->completion_lock);
5010 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5011 apoll->poll.events);
5012 return true;
5013}
5014
5015static bool __io_poll_remove_one(struct io_kiocb *req,
5016 struct io_poll_iocb *poll)
5017{
Jens Axboeb41e9852020-02-17 09:52:41 -07005018 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005019
5020 spin_lock(&poll->head->lock);
5021 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005022 if (!list_empty(&poll->wait.entry)) {
5023 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005024 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005025 }
5026 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005027 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005028 return do_complete;
5029}
5030
5031static bool io_poll_remove_one(struct io_kiocb *req)
5032{
5033 bool do_complete;
5034
Jens Axboed4e7cd32020-08-15 11:44:50 -07005035 io_poll_remove_double(req);
5036
Jens Axboed7718a92020-02-14 22:23:12 -07005037 if (req->opcode == IORING_OP_POLL_ADD) {
5038 do_complete = __io_poll_remove_one(req, &req->poll);
5039 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005040 struct async_poll *apoll = req->apoll;
5041
Jens Axboed7718a92020-02-14 22:23:12 -07005042 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005043 do_complete = __io_poll_remove_one(req, &apoll->poll);
5044 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005045 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005046 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005047 kfree(apoll);
5048 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005049 }
5050
Jens Axboeb41e9852020-02-17 09:52:41 -07005051 if (do_complete) {
5052 io_cqring_fill_event(req, -ECANCELED);
5053 io_commit_cqring(req->ctx);
5054 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005055 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005056 io_put_req(req);
5057 }
5058
5059 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005060}
5061
Jens Axboe76e1b642020-09-26 15:05:03 -06005062/*
5063 * Returns true if we found and killed one or more poll requests
5064 */
5065static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005066{
Jens Axboe78076bb2019-12-04 19:56:40 -07005067 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005068 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005069 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005070
5071 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005072 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5073 struct hlist_head *list;
5074
5075 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005076 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5077 if (io_task_match(req, tsk))
5078 posted += io_poll_remove_one(req);
5079 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005080 }
5081 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005082
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005083 if (posted)
5084 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005085
5086 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005087}
5088
Jens Axboe47f46762019-11-09 17:43:02 -07005089static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5090{
Jens Axboe78076bb2019-12-04 19:56:40 -07005091 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005092 struct io_kiocb *req;
5093
Jens Axboe78076bb2019-12-04 19:56:40 -07005094 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5095 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005096 if (sqe_addr != req->user_data)
5097 continue;
5098 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005099 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005100 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005101 }
5102
5103 return -ENOENT;
5104}
5105
Jens Axboe3529d8c2019-12-19 18:24:38 -07005106static int io_poll_remove_prep(struct io_kiocb *req,
5107 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005108{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005109 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5110 return -EINVAL;
5111 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5112 sqe->poll_events)
5113 return -EINVAL;
5114
Jens Axboe0969e782019-12-17 18:40:57 -07005115 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005116 return 0;
5117}
5118
5119/*
5120 * Find a running poll command that matches one specified in sqe->addr,
5121 * and remove it if found.
5122 */
5123static int io_poll_remove(struct io_kiocb *req)
5124{
5125 struct io_ring_ctx *ctx = req->ctx;
5126 u64 addr;
5127 int ret;
5128
Jens Axboe0969e782019-12-17 18:40:57 -07005129 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005130 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005131 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005132 spin_unlock_irq(&ctx->completion_lock);
5133
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005134 if (ret < 0)
5135 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005136 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005137 return 0;
5138}
5139
Jens Axboe221c5eb2019-01-17 09:41:58 -07005140static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5141 void *key)
5142{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005143 struct io_kiocb *req = wait->private;
5144 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005145
Jens Axboed7718a92020-02-14 22:23:12 -07005146 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005147}
5148
Jens Axboe221c5eb2019-01-17 09:41:58 -07005149static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5150 struct poll_table_struct *p)
5151{
5152 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5153
Jens Axboe807abcb2020-07-17 17:09:27 -06005154 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005155}
5156
Jens Axboe3529d8c2019-12-19 18:24:38 -07005157static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005158{
5159 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005160 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005161
5162 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5163 return -EINVAL;
5164 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5165 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005166 if (!poll->file)
5167 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005168
Jiufei Xue5769a352020-06-17 17:53:55 +08005169 events = READ_ONCE(sqe->poll32_events);
5170#ifdef __BIG_ENDIAN
5171 events = swahw32(events);
5172#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005173 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5174 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005175 return 0;
5176}
5177
Pavel Begunkov014db002020-03-03 21:33:12 +03005178static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005179{
5180 struct io_poll_iocb *poll = &req->poll;
5181 struct io_ring_ctx *ctx = req->ctx;
5182 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005183 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005184
Jens Axboe78076bb2019-12-04 19:56:40 -07005185 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005186 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005187
Jens Axboed7718a92020-02-14 22:23:12 -07005188 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5189 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005190
Jens Axboe8c838782019-03-12 15:48:16 -06005191 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005192 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005193 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005194 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005195 spin_unlock_irq(&ctx->completion_lock);
5196
Jens Axboe8c838782019-03-12 15:48:16 -06005197 if (mask) {
5198 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005199 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005200 }
Jens Axboe8c838782019-03-12 15:48:16 -06005201 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005202}
5203
Jens Axboe5262f562019-09-17 12:26:57 -06005204static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5205{
Jens Axboead8a48a2019-11-15 08:49:11 -07005206 struct io_timeout_data *data = container_of(timer,
5207 struct io_timeout_data, timer);
5208 struct io_kiocb *req = data->req;
5209 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005210 unsigned long flags;
5211
Jens Axboe5262f562019-09-17 12:26:57 -06005212 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005213 atomic_set(&req->ctx->cq_timeouts,
5214 atomic_read(&req->ctx->cq_timeouts) + 1);
5215
zhangyi (F)ef036812019-10-23 15:10:08 +08005216 /*
Jens Axboe11365042019-10-16 09:08:32 -06005217 * We could be racing with timeout deletion. If the list is empty,
5218 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005219 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005220 if (!list_empty(&req->timeout.list))
5221 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005222
Jens Axboe78e19bb2019-11-06 15:21:34 -07005223 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005224 io_commit_cqring(ctx);
5225 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5226
5227 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005228 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005229 io_put_req(req);
5230 return HRTIMER_NORESTART;
5231}
5232
Jens Axboef254ac02020-08-12 17:33:30 -06005233static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005234{
Jens Axboef254ac02020-08-12 17:33:30 -06005235 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005236
Jens Axboef254ac02020-08-12 17:33:30 -06005237 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005238
Jens Axboe2d283902019-12-04 11:08:05 -07005239 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005240 if (ret == -1)
5241 return -EALREADY;
5242
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005243 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005244 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005245 io_cqring_fill_event(req, -ECANCELED);
5246 io_put_req(req);
5247 return 0;
5248}
5249
Jens Axboef254ac02020-08-12 17:33:30 -06005250static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5251{
5252 struct io_kiocb *req;
5253 int ret = -ENOENT;
5254
5255 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5256 if (user_data == req->user_data) {
5257 ret = 0;
5258 break;
5259 }
5260 }
5261
5262 if (ret == -ENOENT)
5263 return ret;
5264
5265 return __io_timeout_cancel(req);
5266}
5267
Jens Axboe3529d8c2019-12-19 18:24:38 -07005268static int io_timeout_remove_prep(struct io_kiocb *req,
5269 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005270{
Jens Axboeb29472e2019-12-17 18:50:29 -07005271 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5272 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005273 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5274 return -EINVAL;
5275 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005276 return -EINVAL;
5277
5278 req->timeout.addr = READ_ONCE(sqe->addr);
5279 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5280 if (req->timeout.flags)
5281 return -EINVAL;
5282
Jens Axboeb29472e2019-12-17 18:50:29 -07005283 return 0;
5284}
5285
Jens Axboe11365042019-10-16 09:08:32 -06005286/*
5287 * Remove or update an existing timeout command
5288 */
Jens Axboefc4df992019-12-10 14:38:45 -07005289static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005290{
5291 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005292 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005293
Jens Axboe11365042019-10-16 09:08:32 -06005294 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005295 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005296
Jens Axboe47f46762019-11-09 17:43:02 -07005297 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005298 io_commit_cqring(ctx);
5299 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005300 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005301 if (ret < 0)
5302 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005303 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005304 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005305}
5306
Jens Axboe3529d8c2019-12-19 18:24:38 -07005307static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005308 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005309{
Jens Axboead8a48a2019-11-15 08:49:11 -07005310 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005311 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005312 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005313
Jens Axboead8a48a2019-11-15 08:49:11 -07005314 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005315 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005316 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005317 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005318 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005319 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005320 flags = READ_ONCE(sqe->timeout_flags);
5321 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005322 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005323
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005324 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005325
Jens Axboe3529d8c2019-12-19 18:24:38 -07005326 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005327 return -ENOMEM;
5328
5329 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005330 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005331
5332 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005333 return -EFAULT;
5334
Jens Axboe11365042019-10-16 09:08:32 -06005335 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005336 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005337 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005338 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005339
Jens Axboead8a48a2019-11-15 08:49:11 -07005340 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5341 return 0;
5342}
5343
Jens Axboefc4df992019-12-10 14:38:45 -07005344static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005345{
Jens Axboead8a48a2019-11-15 08:49:11 -07005346 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005347 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005348 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005349 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005350
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005351 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005352
Jens Axboe5262f562019-09-17 12:26:57 -06005353 /*
5354 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005355 * timeout event to be satisfied. If it isn't set, then this is
5356 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005357 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005358 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005359 entry = ctx->timeout_list.prev;
5360 goto add;
5361 }
Jens Axboe5262f562019-09-17 12:26:57 -06005362
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005363 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5364 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005365
5366 /*
5367 * Insertion sort, ensuring the first entry in the list is always
5368 * the one we need first.
5369 */
Jens Axboe5262f562019-09-17 12:26:57 -06005370 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005371 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5372 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005373
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005374 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005375 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005376 /* nxt.seq is behind @tail, otherwise would've been completed */
5377 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005378 break;
5379 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005380add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005381 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005382 data->timer.function = io_timeout_fn;
5383 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005384 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005385 return 0;
5386}
5387
Jens Axboe62755e32019-10-28 21:49:21 -06005388static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005389{
Jens Axboe62755e32019-10-28 21:49:21 -06005390 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005391
Jens Axboe62755e32019-10-28 21:49:21 -06005392 return req->user_data == (unsigned long) data;
5393}
5394
Jens Axboee977d6d2019-11-05 12:39:45 -07005395static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005396{
Jens Axboe62755e32019-10-28 21:49:21 -06005397 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005398 int ret = 0;
5399
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005400 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005401 switch (cancel_ret) {
5402 case IO_WQ_CANCEL_OK:
5403 ret = 0;
5404 break;
5405 case IO_WQ_CANCEL_RUNNING:
5406 ret = -EALREADY;
5407 break;
5408 case IO_WQ_CANCEL_NOTFOUND:
5409 ret = -ENOENT;
5410 break;
5411 }
5412
Jens Axboee977d6d2019-11-05 12:39:45 -07005413 return ret;
5414}
5415
Jens Axboe47f46762019-11-09 17:43:02 -07005416static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5417 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005418 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005419{
5420 unsigned long flags;
5421 int ret;
5422
5423 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5424 if (ret != -ENOENT) {
5425 spin_lock_irqsave(&ctx->completion_lock, flags);
5426 goto done;
5427 }
5428
5429 spin_lock_irqsave(&ctx->completion_lock, flags);
5430 ret = io_timeout_cancel(ctx, sqe_addr);
5431 if (ret != -ENOENT)
5432 goto done;
5433 ret = io_poll_cancel(ctx, sqe_addr);
5434done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005435 if (!ret)
5436 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005437 io_cqring_fill_event(req, ret);
5438 io_commit_cqring(ctx);
5439 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5440 io_cqring_ev_posted(ctx);
5441
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005442 if (ret < 0)
5443 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005444 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005445}
5446
Jens Axboe3529d8c2019-12-19 18:24:38 -07005447static int io_async_cancel_prep(struct io_kiocb *req,
5448 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005449{
Jens Axboefbf23842019-12-17 18:45:56 -07005450 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005451 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005452 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5453 return -EINVAL;
5454 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005455 return -EINVAL;
5456
Jens Axboefbf23842019-12-17 18:45:56 -07005457 req->cancel.addr = READ_ONCE(sqe->addr);
5458 return 0;
5459}
5460
Pavel Begunkov014db002020-03-03 21:33:12 +03005461static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005462{
5463 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005464
Pavel Begunkov014db002020-03-03 21:33:12 +03005465 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005466 return 0;
5467}
5468
Jens Axboe05f3fb32019-12-09 11:22:50 -07005469static int io_files_update_prep(struct io_kiocb *req,
5470 const struct io_uring_sqe *sqe)
5471{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005472 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5473 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005474 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5475 return -EINVAL;
5476 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005477 return -EINVAL;
5478
5479 req->files_update.offset = READ_ONCE(sqe->off);
5480 req->files_update.nr_args = READ_ONCE(sqe->len);
5481 if (!req->files_update.nr_args)
5482 return -EINVAL;
5483 req->files_update.arg = READ_ONCE(sqe->addr);
5484 return 0;
5485}
5486
Jens Axboe229a7b62020-06-22 10:13:11 -06005487static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5488 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005489{
5490 struct io_ring_ctx *ctx = req->ctx;
5491 struct io_uring_files_update up;
5492 int ret;
5493
Jens Axboef86cd202020-01-29 13:46:44 -07005494 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005495 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005496
5497 up.offset = req->files_update.offset;
5498 up.fds = req->files_update.arg;
5499
5500 mutex_lock(&ctx->uring_lock);
5501 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5502 mutex_unlock(&ctx->uring_lock);
5503
5504 if (ret < 0)
5505 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005506 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005507 return 0;
5508}
5509
Jens Axboe3529d8c2019-12-19 18:24:38 -07005510static int io_req_defer_prep(struct io_kiocb *req,
5511 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005512{
Jens Axboee7815732019-12-17 19:45:06 -07005513 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005514
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005515 if (!sqe)
5516 return 0;
5517
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005518 if (io_alloc_async_ctx(req))
5519 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005520 ret = io_prep_work_files(req);
5521 if (unlikely(ret))
5522 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005523
Jens Axboe202700e12020-09-12 13:18:10 -06005524 io_prep_async_work(req);
5525
Jens Axboed625c6e2019-12-17 19:53:05 -07005526 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005527 case IORING_OP_NOP:
5528 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005529 case IORING_OP_READV:
5530 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005531 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005532 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005533 break;
5534 case IORING_OP_WRITEV:
5535 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005536 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005537 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005538 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005539 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005540 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005541 break;
5542 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005543 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005544 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005545 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005546 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005547 break;
5548 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005549 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005550 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005551 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005552 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005553 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005554 break;
5555 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005556 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005557 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005558 break;
Jens Axboef499a022019-12-02 16:28:46 -07005559 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005560 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005561 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005562 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005563 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005564 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005565 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005566 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005567 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005568 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005569 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005570 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005571 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005572 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005573 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005574 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005575 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005576 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005577 case IORING_OP_FALLOCATE:
5578 ret = io_fallocate_prep(req, sqe);
5579 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005580 case IORING_OP_OPENAT:
5581 ret = io_openat_prep(req, sqe);
5582 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005583 case IORING_OP_CLOSE:
5584 ret = io_close_prep(req, sqe);
5585 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005586 case IORING_OP_FILES_UPDATE:
5587 ret = io_files_update_prep(req, sqe);
5588 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005589 case IORING_OP_STATX:
5590 ret = io_statx_prep(req, sqe);
5591 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005592 case IORING_OP_FADVISE:
5593 ret = io_fadvise_prep(req, sqe);
5594 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005595 case IORING_OP_MADVISE:
5596 ret = io_madvise_prep(req, sqe);
5597 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005598 case IORING_OP_OPENAT2:
5599 ret = io_openat2_prep(req, sqe);
5600 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005601 case IORING_OP_EPOLL_CTL:
5602 ret = io_epoll_ctl_prep(req, sqe);
5603 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005604 case IORING_OP_SPLICE:
5605 ret = io_splice_prep(req, sqe);
5606 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005607 case IORING_OP_PROVIDE_BUFFERS:
5608 ret = io_provide_buffers_prep(req, sqe);
5609 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005610 case IORING_OP_REMOVE_BUFFERS:
5611 ret = io_remove_buffers_prep(req, sqe);
5612 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005613 case IORING_OP_TEE:
5614 ret = io_tee_prep(req, sqe);
5615 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005616 default:
Jens Axboee7815732019-12-17 19:45:06 -07005617 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5618 req->opcode);
5619 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005620 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005621 }
5622
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005623 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005624}
5625
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005626static u32 io_get_sequence(struct io_kiocb *req)
5627{
5628 struct io_kiocb *pos;
5629 struct io_ring_ctx *ctx = req->ctx;
5630 u32 total_submitted, nr_reqs = 1;
5631
5632 if (req->flags & REQ_F_LINK_HEAD)
5633 list_for_each_entry(pos, &req->link_list, link_list)
5634 nr_reqs++;
5635
5636 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5637 return total_submitted - nr_reqs;
5638}
5639
Jens Axboe3529d8c2019-12-19 18:24:38 -07005640static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005641{
Jackie Liua197f662019-11-08 08:09:12 -07005642 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005643 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005644 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005645 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005646
Bob Liu9d858b22019-11-13 18:06:25 +08005647 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005648 if (likely(list_empty_careful(&ctx->defer_list) &&
5649 !(req->flags & REQ_F_IO_DRAIN)))
5650 return 0;
5651
5652 seq = io_get_sequence(req);
5653 /* Still a chance to pass the sequence check */
5654 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005655 return 0;
5656
Pavel Begunkov650b5482020-05-17 14:02:11 +03005657 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005658 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005659 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005660 return ret;
5661 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005662 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005663 de = kmalloc(sizeof(*de), GFP_KERNEL);
5664 if (!de)
5665 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005666
Jens Axboede0617e2019-04-06 21:51:27 -06005667 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005668 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005669 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005670 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005671 io_queue_async_work(req);
5672 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005673 }
5674
Jens Axboe915967f2019-11-21 09:01:20 -07005675 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005676 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005677 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005678 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005679 spin_unlock_irq(&ctx->completion_lock);
5680 return -EIOCBQUEUED;
5681}
5682
Jens Axboef573d382020-09-22 10:19:24 -06005683static void io_req_drop_files(struct io_kiocb *req)
5684{
5685 struct io_ring_ctx *ctx = req->ctx;
5686 unsigned long flags;
5687
5688 spin_lock_irqsave(&ctx->inflight_lock, flags);
5689 list_del(&req->inflight_entry);
5690 if (waitqueue_active(&ctx->inflight_wait))
5691 wake_up(&ctx->inflight_wait);
5692 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5693 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005694 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005695 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005696 req->work.files = NULL;
5697}
5698
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005699static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005700{
5701 struct io_async_ctx *io = req->io;
5702
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005703 if (req->flags & REQ_F_BUFFER_SELECTED) {
5704 switch (req->opcode) {
5705 case IORING_OP_READV:
5706 case IORING_OP_READ_FIXED:
5707 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005708 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005709 break;
5710 case IORING_OP_RECVMSG:
5711 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005712 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005713 break;
5714 }
5715 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005716 }
5717
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005718 if (req->flags & REQ_F_NEED_CLEANUP) {
5719 switch (req->opcode) {
5720 case IORING_OP_READV:
5721 case IORING_OP_READ_FIXED:
5722 case IORING_OP_READ:
5723 case IORING_OP_WRITEV:
5724 case IORING_OP_WRITE_FIXED:
5725 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005726 if (io->rw.free_iovec)
5727 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005728 break;
5729 case IORING_OP_RECVMSG:
5730 case IORING_OP_SENDMSG:
5731 if (io->msg.iov != io->msg.fast_iov)
5732 kfree(io->msg.iov);
5733 break;
5734 case IORING_OP_SPLICE:
5735 case IORING_OP_TEE:
5736 io_put_file(req, req->splice.file_in,
5737 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5738 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005739 case IORING_OP_OPENAT:
5740 case IORING_OP_OPENAT2:
5741 if (req->open.filename)
5742 putname(req->open.filename);
5743 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005744 }
5745 req->flags &= ~REQ_F_NEED_CLEANUP;
5746 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005747
Jens Axboef573d382020-09-22 10:19:24 -06005748 if (req->flags & REQ_F_INFLIGHT)
5749 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005750}
5751
Jens Axboe3529d8c2019-12-19 18:24:38 -07005752static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005753 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005754{
Jackie Liua197f662019-11-08 08:09:12 -07005755 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005756 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005757
Jens Axboed625c6e2019-12-17 19:53:05 -07005758 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005759 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005760 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005761 break;
5762 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005763 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005764 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005765 if (sqe) {
5766 ret = io_read_prep(req, sqe, force_nonblock);
5767 if (ret < 0)
5768 break;
5769 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005770 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005771 break;
5772 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005773 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005774 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005775 if (sqe) {
5776 ret = io_write_prep(req, sqe, force_nonblock);
5777 if (ret < 0)
5778 break;
5779 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005780 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005781 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005782 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005783 if (sqe) {
5784 ret = io_prep_fsync(req, sqe);
5785 if (ret < 0)
5786 break;
5787 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005788 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005789 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005790 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005791 if (sqe) {
5792 ret = io_poll_add_prep(req, sqe);
5793 if (ret)
5794 break;
5795 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005796 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005797 break;
5798 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005799 if (sqe) {
5800 ret = io_poll_remove_prep(req, sqe);
5801 if (ret < 0)
5802 break;
5803 }
Jens Axboefc4df992019-12-10 14:38:45 -07005804 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005805 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005806 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005807 if (sqe) {
5808 ret = io_prep_sfr(req, sqe);
5809 if (ret < 0)
5810 break;
5811 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005812 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005813 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005814 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005815 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005816 if (sqe) {
5817 ret = io_sendmsg_prep(req, sqe);
5818 if (ret < 0)
5819 break;
5820 }
Jens Axboefddafac2020-01-04 20:19:44 -07005821 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005822 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005823 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005824 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005825 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005826 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005827 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005828 if (sqe) {
5829 ret = io_recvmsg_prep(req, sqe);
5830 if (ret)
5831 break;
5832 }
Jens Axboefddafac2020-01-04 20:19:44 -07005833 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005834 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005835 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005836 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005837 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005838 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005839 if (sqe) {
5840 ret = io_timeout_prep(req, sqe, false);
5841 if (ret)
5842 break;
5843 }
Jens Axboefc4df992019-12-10 14:38:45 -07005844 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005845 break;
Jens Axboe11365042019-10-16 09:08:32 -06005846 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005847 if (sqe) {
5848 ret = io_timeout_remove_prep(req, sqe);
5849 if (ret)
5850 break;
5851 }
Jens Axboefc4df992019-12-10 14:38:45 -07005852 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005853 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005854 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005855 if (sqe) {
5856 ret = io_accept_prep(req, sqe);
5857 if (ret)
5858 break;
5859 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005860 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005861 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005862 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005863 if (sqe) {
5864 ret = io_connect_prep(req, sqe);
5865 if (ret)
5866 break;
5867 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005868 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005869 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005870 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005871 if (sqe) {
5872 ret = io_async_cancel_prep(req, sqe);
5873 if (ret)
5874 break;
5875 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005876 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005877 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005878 case IORING_OP_FALLOCATE:
5879 if (sqe) {
5880 ret = io_fallocate_prep(req, sqe);
5881 if (ret)
5882 break;
5883 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005884 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005885 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005886 case IORING_OP_OPENAT:
5887 if (sqe) {
5888 ret = io_openat_prep(req, sqe);
5889 if (ret)
5890 break;
5891 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005892 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005893 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005894 case IORING_OP_CLOSE:
5895 if (sqe) {
5896 ret = io_close_prep(req, sqe);
5897 if (ret)
5898 break;
5899 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005900 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005901 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005902 case IORING_OP_FILES_UPDATE:
5903 if (sqe) {
5904 ret = io_files_update_prep(req, sqe);
5905 if (ret)
5906 break;
5907 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005908 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005909 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005910 case IORING_OP_STATX:
5911 if (sqe) {
5912 ret = io_statx_prep(req, sqe);
5913 if (ret)
5914 break;
5915 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005916 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005917 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005918 case IORING_OP_FADVISE:
5919 if (sqe) {
5920 ret = io_fadvise_prep(req, sqe);
5921 if (ret)
5922 break;
5923 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005924 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005925 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005926 case IORING_OP_MADVISE:
5927 if (sqe) {
5928 ret = io_madvise_prep(req, sqe);
5929 if (ret)
5930 break;
5931 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005932 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005933 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005934 case IORING_OP_OPENAT2:
5935 if (sqe) {
5936 ret = io_openat2_prep(req, sqe);
5937 if (ret)
5938 break;
5939 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005940 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005941 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005942 case IORING_OP_EPOLL_CTL:
5943 if (sqe) {
5944 ret = io_epoll_ctl_prep(req, sqe);
5945 if (ret)
5946 break;
5947 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005948 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005949 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005950 case IORING_OP_SPLICE:
5951 if (sqe) {
5952 ret = io_splice_prep(req, sqe);
5953 if (ret < 0)
5954 break;
5955 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005956 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005957 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005958 case IORING_OP_PROVIDE_BUFFERS:
5959 if (sqe) {
5960 ret = io_provide_buffers_prep(req, sqe);
5961 if (ret)
5962 break;
5963 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005964 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005965 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005966 case IORING_OP_REMOVE_BUFFERS:
5967 if (sqe) {
5968 ret = io_remove_buffers_prep(req, sqe);
5969 if (ret)
5970 break;
5971 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005972 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005973 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005974 case IORING_OP_TEE:
5975 if (sqe) {
5976 ret = io_tee_prep(req, sqe);
5977 if (ret < 0)
5978 break;
5979 }
5980 ret = io_tee(req, force_nonblock);
5981 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005982 default:
5983 ret = -EINVAL;
5984 break;
5985 }
5986
5987 if (ret)
5988 return ret;
5989
Jens Axboeb5325762020-05-19 21:20:27 -06005990 /* If the op doesn't have a file, we're not polling for it */
5991 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005992 const bool in_async = io_wq_current_is_worker();
5993
Jens Axboe11ba8202020-01-15 21:51:17 -07005994 /* workqueue context doesn't hold uring_lock, grab it now */
5995 if (in_async)
5996 mutex_lock(&ctx->uring_lock);
5997
Jens Axboe2b188cc2019-01-07 10:46:33 -07005998 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005999
6000 if (in_async)
6001 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006002 }
6003
6004 return 0;
6005}
6006
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006007static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006008{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006009 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006010 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006011 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006012
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006013 timeout = io_prep_linked_timeout(req);
6014 if (timeout)
6015 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006016
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006017 /* if NO_CANCEL is set, we must still run the work */
6018 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6019 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006020 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006021 }
Jens Axboe31b51512019-01-18 22:56:34 -07006022
Jens Axboe561fb042019-10-24 07:25:42 -06006023 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006024 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006025 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006026 /*
6027 * We can get EAGAIN for polled IO even though we're
6028 * forcing a sync submission from here, since we can't
6029 * wait for request slots on the block side.
6030 */
6031 if (ret != -EAGAIN)
6032 break;
6033 cond_resched();
6034 } while (1);
6035 }
Jens Axboe31b51512019-01-18 22:56:34 -07006036
Jens Axboe561fb042019-10-24 07:25:42 -06006037 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006038 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006039 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006040 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006041
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006042 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006043}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006044
Jens Axboe65e19f52019-10-26 07:20:21 -06006045static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6046 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006047{
Jens Axboe65e19f52019-10-26 07:20:21 -06006048 struct fixed_file_table *table;
6049
Jens Axboe05f3fb32019-12-09 11:22:50 -07006050 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006051 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006052}
6053
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006054static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6055 int fd, struct file **out_file, bool fixed)
6056{
6057 struct io_ring_ctx *ctx = req->ctx;
6058 struct file *file;
6059
6060 if (fixed) {
6061 if (unlikely(!ctx->file_data ||
6062 (unsigned) fd >= ctx->nr_user_files))
6063 return -EBADF;
6064 fd = array_index_nospec(fd, ctx->nr_user_files);
6065 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006066 if (file) {
6067 req->fixed_file_refs = ctx->file_data->cur_refs;
6068 percpu_ref_get(req->fixed_file_refs);
6069 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006070 } else {
6071 trace_io_uring_file_get(ctx, fd);
6072 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006073 }
6074
Jens Axboefd2206e2020-06-02 16:40:47 -06006075 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6076 *out_file = file;
6077 return 0;
6078 }
6079 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006080}
6081
Jens Axboe3529d8c2019-12-19 18:24:38 -07006082static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006083 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006084{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006085 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006086
Jens Axboe63ff8222020-05-07 14:56:15 -06006087 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006088 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006089 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006090
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006091 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006092}
6093
Jackie Liua197f662019-11-08 08:09:12 -07006094static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006095{
Jackie Liua197f662019-11-08 08:09:12 -07006096 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006097
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006098 io_req_init_async(req);
6099
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006100 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006101 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006102
Jens Axboe0f212202020-09-13 13:09:39 -06006103 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006104 get_nsproxy(current->nsproxy);
6105 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006106 req->flags |= REQ_F_INFLIGHT;
6107
Jens Axboefcb323c2019-10-24 12:39:47 -06006108 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006109 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006110 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006111 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006112}
6113
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006114static inline int io_prep_work_files(struct io_kiocb *req)
6115{
6116 if (!io_op_defs[req->opcode].file_table)
6117 return 0;
6118 return io_grab_files(req);
6119}
6120
Jens Axboe2665abf2019-11-05 12:40:47 -07006121static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6122{
Jens Axboead8a48a2019-11-15 08:49:11 -07006123 struct io_timeout_data *data = container_of(timer,
6124 struct io_timeout_data, timer);
6125 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006126 struct io_ring_ctx *ctx = req->ctx;
6127 struct io_kiocb *prev = NULL;
6128 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006129
6130 spin_lock_irqsave(&ctx->completion_lock, flags);
6131
6132 /*
6133 * We don't expect the list to be empty, that will only happen if we
6134 * race with the completion of the linked work.
6135 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006136 if (!list_empty(&req->link_list)) {
6137 prev = list_entry(req->link_list.prev, struct io_kiocb,
6138 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006139 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006140 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006141 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6142 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006143 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006144 }
6145
6146 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6147
6148 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006149 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006150 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006151 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006152 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006153 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006154 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006155 return HRTIMER_NORESTART;
6156}
6157
Jens Axboe7271ef32020-08-10 09:55:22 -06006158static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006159{
Jens Axboe76a46e02019-11-10 23:34:16 -07006160 /*
6161 * If the list is now empty, then our linked request finished before
6162 * we got a chance to setup the timer
6163 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006164 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006165 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006166
Jens Axboead8a48a2019-11-15 08:49:11 -07006167 data->timer.function = io_link_timeout_fn;
6168 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6169 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006170 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006171}
6172
6173static void io_queue_linked_timeout(struct io_kiocb *req)
6174{
6175 struct io_ring_ctx *ctx = req->ctx;
6176
6177 spin_lock_irq(&ctx->completion_lock);
6178 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006179 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006180
Jens Axboe2665abf2019-11-05 12:40:47 -07006181 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006182 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006183}
6184
Jens Axboead8a48a2019-11-15 08:49:11 -07006185static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006186{
6187 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006188
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006189 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006190 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006191 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006192 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006193
Pavel Begunkov44932332019-12-05 16:16:35 +03006194 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6195 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006196 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006197 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006198
Jens Axboe76a46e02019-11-10 23:34:16 -07006199 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006200 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006201}
6202
Jens Axboef13fad72020-06-22 09:34:30 -06006203static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6204 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006205{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006206 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006207 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006208 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209 int ret;
6210
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006211again:
6212 linked_timeout = io_prep_linked_timeout(req);
6213
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006214 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6215 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006216 if (old_creds)
6217 revert_creds(old_creds);
6218 if (old_creds == req->work.creds)
6219 old_creds = NULL; /* restored original creds */
6220 else
6221 old_creds = override_creds(req->work.creds);
6222 }
6223
Jens Axboef13fad72020-06-22 09:34:30 -06006224 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006225
6226 /*
6227 * We async punt it if the file wasn't marked NOWAIT, or if the file
6228 * doesn't support non-blocking read/write attempts
6229 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006230 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006231 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006232punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006233 ret = io_prep_work_files(req);
6234 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006235 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006236 /*
6237 * Queued up for async execution, worker will release
6238 * submit reference when the iocb is actually submitted.
6239 */
6240 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006241 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006242
Pavel Begunkovf063c542020-07-25 14:41:59 +03006243 if (linked_timeout)
6244 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006245 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006246 }
Jens Axboee65ef562019-03-12 10:16:44 -06006247
Pavel Begunkov652532a2020-07-03 22:15:07 +03006248 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006249err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006250 /* un-prep timeout, so it'll be killed as any other linked */
6251 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006252 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006253 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006254 io_req_complete(req, ret);
6255 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006256 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006257
Jens Axboe6c271ce2019-01-10 11:22:30 -07006258 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006259 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006260 if (linked_timeout)
6261 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006262
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006263 if (nxt) {
6264 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006265
6266 if (req->flags & REQ_F_FORCE_ASYNC)
6267 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006268 goto again;
6269 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006270exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006271 if (old_creds)
6272 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006273}
6274
Jens Axboef13fad72020-06-22 09:34:30 -06006275static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6276 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006277{
6278 int ret;
6279
Jens Axboe3529d8c2019-12-19 18:24:38 -07006280 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006281 if (ret) {
6282 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006283fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006284 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006285 io_put_req(req);
6286 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006287 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006288 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006289 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006290 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006291 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006292 goto fail_req;
6293 }
6294
Jens Axboece35a472019-12-17 08:04:44 -07006295 /*
6296 * Never try inline submit of IOSQE_ASYNC is set, go straight
6297 * to async execution.
6298 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006299 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006300 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6301 io_queue_async_work(req);
6302 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006303 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006304 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006305}
6306
Jens Axboef13fad72020-06-22 09:34:30 -06006307static inline void io_queue_link_head(struct io_kiocb *req,
6308 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006309{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006310 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006311 io_put_req(req);
6312 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006313 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006314 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006315}
6316
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006317static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006318 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006319{
Jackie Liua197f662019-11-08 08:09:12 -07006320 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006321 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006322
Jens Axboe9e645e112019-05-10 16:07:28 -06006323 /*
6324 * If we already have a head request, queue this one for async
6325 * submittal once the head completes. If we don't have a head but
6326 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6327 * submitted sync once the chain is complete. If none of those
6328 * conditions are true (normal request), then just queue it.
6329 */
6330 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006331 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006332
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006333 /*
6334 * Taking sequential execution of a link, draining both sides
6335 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6336 * requests in the link. So, it drains the head and the
6337 * next after the link request. The last one is done via
6338 * drain_next flag to persist the effect across calls.
6339 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006340 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006341 head->flags |= REQ_F_IO_DRAIN;
6342 ctx->drain_next = 1;
6343 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006344 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006345 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006346 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006347 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006348 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006349 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006350 trace_io_uring_link(ctx, req, head);
6351 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006352
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006353 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006354 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006355 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006356 *link = NULL;
6357 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006358 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006359 if (unlikely(ctx->drain_next)) {
6360 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006361 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006362 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006363 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006364 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006365 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006366
Pavel Begunkov711be032020-01-17 03:57:59 +03006367 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006368 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006369 req->flags |= REQ_F_FAIL_LINK;
6370 *link = req;
6371 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006372 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006373 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006374 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006375
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006376 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006377}
6378
Jens Axboe9a56a232019-01-09 09:06:50 -07006379/*
6380 * Batched submission is done, ensure local IO is flushed out.
6381 */
6382static void io_submit_state_end(struct io_submit_state *state)
6383{
Jens Axboef13fad72020-06-22 09:34:30 -06006384 if (!list_empty(&state->comp.list))
6385 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006386 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006387 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006388 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006389 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006390}
6391
6392/*
6393 * Start submission side cache.
6394 */
6395static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006396 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006397{
6398 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006399 state->comp.nr = 0;
6400 INIT_LIST_HEAD(&state->comp.list);
6401 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006402 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006403 state->file = NULL;
6404 state->ios_left = max_ios;
6405}
6406
Jens Axboe2b188cc2019-01-07 10:46:33 -07006407static void io_commit_sqring(struct io_ring_ctx *ctx)
6408{
Hristo Venev75b28af2019-08-26 17:23:46 +00006409 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006410
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006411 /*
6412 * Ensure any loads from the SQEs are done at this point,
6413 * since once we write the new head, the application could
6414 * write new data to them.
6415 */
6416 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006417}
6418
6419/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006420 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006421 * that is mapped by userspace. This means that care needs to be taken to
6422 * ensure that reads are stable, as we cannot rely on userspace always
6423 * being a good citizen. If members of the sqe are validated and then later
6424 * used, it's important that those reads are done through READ_ONCE() to
6425 * prevent a re-load down the line.
6426 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006427static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006428{
Hristo Venev75b28af2019-08-26 17:23:46 +00006429 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006430 unsigned head;
6431
6432 /*
6433 * The cached sq head (or cq tail) serves two purposes:
6434 *
6435 * 1) allows us to batch the cost of updating the user visible
6436 * head updates.
6437 * 2) allows the kernel side to track the head on its own, even
6438 * though the application is the one updating it.
6439 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006440 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006441 if (likely(head < ctx->sq_entries))
6442 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006443
6444 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006445 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006446 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006447 return NULL;
6448}
6449
6450static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6451{
6452 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006453}
6454
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006455/*
6456 * Check SQE restrictions (opcode and flags).
6457 *
6458 * Returns 'true' if SQE is allowed, 'false' otherwise.
6459 */
6460static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6461 struct io_kiocb *req,
6462 unsigned int sqe_flags)
6463{
6464 if (!ctx->restricted)
6465 return true;
6466
6467 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6468 return false;
6469
6470 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6471 ctx->restrictions.sqe_flags_required)
6472 return false;
6473
6474 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6475 ctx->restrictions.sqe_flags_required))
6476 return false;
6477
6478 return true;
6479}
6480
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006481#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6482 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6483 IOSQE_BUFFER_SELECT)
6484
6485static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6486 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006487 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006488{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006489 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006490 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006491
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006492 req->opcode = READ_ONCE(sqe->opcode);
6493 req->user_data = READ_ONCE(sqe->user_data);
6494 req->io = NULL;
6495 req->file = NULL;
6496 req->ctx = ctx;
6497 req->flags = 0;
6498 /* one is dropped after submission, the other at completion */
6499 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006500 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006501 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006502 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006503 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006504
6505 if (unlikely(req->opcode >= IORING_OP_LAST))
6506 return -EINVAL;
6507
Jens Axboe9d8426a2020-06-16 18:42:49 -06006508 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6509 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006510
6511 sqe_flags = READ_ONCE(sqe->flags);
6512 /* enforce forwards compatibility on users */
6513 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6514 return -EINVAL;
6515
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006516 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6517 return -EACCES;
6518
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006519 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6520 !io_op_defs[req->opcode].buffer_select)
6521 return -EOPNOTSUPP;
6522
6523 id = READ_ONCE(sqe->personality);
6524 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006525 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006526 req->work.creds = idr_find(&ctx->personality_idr, id);
6527 if (unlikely(!req->work.creds))
6528 return -EINVAL;
6529 get_cred(req->work.creds);
6530 }
6531
6532 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006533 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006534
Jens Axboe63ff8222020-05-07 14:56:15 -06006535 if (!io_op_defs[req->opcode].needs_file)
6536 return 0;
6537
6538 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006539}
6540
Jens Axboe0f212202020-09-13 13:09:39 -06006541static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006542{
Jens Axboeac8691c2020-06-01 08:30:41 -06006543 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006544 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006545 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006546
Jens Axboec4a2ed72019-11-21 21:01:26 -07006547 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006548 if (test_bit(0, &ctx->sq_check_overflow)) {
6549 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006550 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006551 return -EBUSY;
6552 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006553
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006554 /* make sure SQ entry isn't read before tail */
6555 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006556
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006557 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6558 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006559
Jens Axboe013538b2020-06-22 09:29:15 -06006560 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006561
6562 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006563 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006564 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006565 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006566
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006567 sqe = io_get_sqe(ctx);
6568 if (unlikely(!sqe)) {
6569 io_consume_sqe(ctx);
6570 break;
6571 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006572 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006573 if (unlikely(!req)) {
6574 if (!submitted)
6575 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006576 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006577 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006578
Jens Axboeac8691c2020-06-01 08:30:41 -06006579 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006580 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006581 /* will complete beyond this point, count as submitted */
6582 submitted++;
6583
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006584 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006585fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006586 io_put_req(req);
6587 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006588 break;
6589 }
6590
Jens Axboe354420f2020-01-08 18:55:15 -07006591 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006592 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006593 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006594 if (err)
6595 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006596 }
6597
Pavel Begunkov9466f432020-01-25 22:34:01 +03006598 if (unlikely(submitted != nr)) {
6599 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6600
6601 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6602 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006603 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006604 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006605 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006606
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006607 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6608 io_commit_sqring(ctx);
6609
Jens Axboe6c271ce2019-01-10 11:22:30 -07006610 return submitted;
6611}
6612
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006613static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6614{
6615 /* Tell userspace we may need a wakeup call */
6616 spin_lock_irq(&ctx->completion_lock);
6617 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6618 spin_unlock_irq(&ctx->completion_lock);
6619}
6620
6621static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6622{
6623 spin_lock_irq(&ctx->completion_lock);
6624 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6625 spin_unlock_irq(&ctx->completion_lock);
6626}
6627
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006628static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6629 int sync, void *key)
6630{
6631 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6632 int ret;
6633
6634 ret = autoremove_wake_function(wqe, mode, sync, key);
6635 if (ret) {
6636 unsigned long flags;
6637
6638 spin_lock_irqsave(&ctx->completion_lock, flags);
6639 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6640 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6641 }
6642 return ret;
6643}
6644
Jens Axboe6c271ce2019-01-10 11:22:30 -07006645static int io_sq_thread(void *data)
6646{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006647 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006648 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006649 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006650 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006651
Jens Axboe6a779382020-09-02 12:21:41 -06006652 init_wait(&ctx->sqo_wait_entry);
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006653 ctx->sqo_wait_entry.func = io_sq_wake_function;
Jens Axboe6a779382020-09-02 12:21:41 -06006654
Jens Axboe0f158b42020-05-14 17:18:39 -06006655 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006656
Jens Axboe181e4482019-11-25 08:52:30 -07006657 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006658
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006659 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006660 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006661 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006662
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006663 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006664 unsigned nr_events = 0;
6665
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006666 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006667 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006668 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006669 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006670 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006671 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006672 }
6673
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006674 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006675
6676 /*
6677 * If submit got -EBUSY, flag us as needing the application
6678 * to enter the kernel to reap and flush events.
6679 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006680 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006681 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006682 * Drop cur_mm before scheduling, we can't hold it for
6683 * long periods (or over schedule()). Do this before
6684 * adding ourselves to the waitqueue, as the unuse/drop
6685 * may sleep.
6686 */
Jens Axboe4349f302020-07-09 15:07:01 -06006687 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006688
6689 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006690 * We're polling. If we're within the defined idle
6691 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006692 * to sleep. The exception is if we got EBUSY doing
6693 * more IO, we should wait for the application to
6694 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006695 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006696 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006697 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6698 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006699 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006700 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006701 continue;
6702 }
6703
Jens Axboe6a779382020-09-02 12:21:41 -06006704 prepare_to_wait(ctx->sqo_wait, &ctx->sqo_wait_entry,
Jens Axboe6c271ce2019-01-10 11:22:30 -07006705 TASK_INTERRUPTIBLE);
6706
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006707 /*
6708 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006709 * to check if there are new reqs added to iopoll_list,
6710 * it is because reqs may have been punted to io worker
6711 * and will be added to iopoll_list later, hence check
6712 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006713 */
6714 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006715 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe6a779382020-09-02 12:21:41 -06006716 finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006717 continue;
6718 }
6719
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006720 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006721
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006722 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006723 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006724 if (kthread_should_park()) {
Jens Axboe6a779382020-09-02 12:21:41 -06006725 finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006726 break;
6727 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006728 if (io_run_task_work()) {
Jens Axboe6a779382020-09-02 12:21:41 -06006729 finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006730 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006731 continue;
6732 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006733 schedule();
Jens Axboe6a779382020-09-02 12:21:41 -06006734 finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006735
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006736 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006737 continue;
6738 }
Jens Axboe6a779382020-09-02 12:21:41 -06006739 finish_wait(ctx->sqo_wait, &ctx->sqo_wait_entry);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006740
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006741 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006742 }
6743
Jens Axboe8a4955f2019-12-09 14:52:35 -07006744 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006745 if (likely(!percpu_ref_is_dying(&ctx->refs)))
Jens Axboe0f212202020-09-13 13:09:39 -06006746 ret = io_submit_sqes(ctx, to_submit);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006747 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006748 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006749 }
6750
Jens Axboe4c6e2772020-07-01 11:29:10 -06006751 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006752
Jens Axboe4349f302020-07-09 15:07:01 -06006753 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006754 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006755
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006756 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006757
Jens Axboe6c271ce2019-01-10 11:22:30 -07006758 return 0;
6759}
6760
Jens Axboebda52162019-09-24 13:47:15 -06006761struct io_wait_queue {
6762 struct wait_queue_entry wq;
6763 struct io_ring_ctx *ctx;
6764 unsigned to_wait;
6765 unsigned nr_timeouts;
6766};
6767
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006768static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006769{
6770 struct io_ring_ctx *ctx = iowq->ctx;
6771
6772 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006773 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006774 * started waiting. For timeouts, we always want to return to userspace,
6775 * regardless of event count.
6776 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006777 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006778 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6779}
6780
6781static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6782 int wake_flags, void *key)
6783{
6784 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6785 wq);
6786
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006787 /* use noflush == true, as we can't safely rely on locking context */
6788 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006789 return -1;
6790
6791 return autoremove_wake_function(curr, mode, wake_flags, key);
6792}
6793
Jens Axboe2b188cc2019-01-07 10:46:33 -07006794/*
6795 * Wait until events become available, if we don't already have some. The
6796 * application must reap them itself, as they reside on the shared cq ring.
6797 */
6798static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6799 const sigset_t __user *sig, size_t sigsz)
6800{
Jens Axboebda52162019-09-24 13:47:15 -06006801 struct io_wait_queue iowq = {
6802 .wq = {
6803 .private = current,
6804 .func = io_wake_function,
6805 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6806 },
6807 .ctx = ctx,
6808 .to_wait = min_events,
6809 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006810 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006811 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006812
Jens Axboeb41e9852020-02-17 09:52:41 -07006813 do {
6814 if (io_cqring_events(ctx, false) >= min_events)
6815 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006816 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006817 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006818 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006819
6820 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006821#ifdef CONFIG_COMPAT
6822 if (in_compat_syscall())
6823 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006824 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006825 else
6826#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006827 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006828
Jens Axboe2b188cc2019-01-07 10:46:33 -07006829 if (ret)
6830 return ret;
6831 }
6832
Jens Axboebda52162019-09-24 13:47:15 -06006833 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006834 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006835 do {
6836 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6837 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006838 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006839 if (io_run_task_work())
6840 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006841 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006842 if (current->jobctl & JOBCTL_TASK_WORK) {
6843 spin_lock_irq(&current->sighand->siglock);
6844 current->jobctl &= ~JOBCTL_TASK_WORK;
6845 recalc_sigpending();
6846 spin_unlock_irq(&current->sighand->siglock);
6847 continue;
6848 }
6849 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006850 break;
6851 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006852 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006853 break;
6854 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006855 } while (1);
6856 finish_wait(&ctx->wait, &iowq.wq);
6857
Jens Axboeb7db41c2020-07-04 08:55:50 -06006858 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006859
Hristo Venev75b28af2019-08-26 17:23:46 +00006860 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006861}
6862
Jens Axboe6b063142019-01-10 22:13:58 -07006863static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6864{
6865#if defined(CONFIG_UNIX)
6866 if (ctx->ring_sock) {
6867 struct sock *sock = ctx->ring_sock->sk;
6868 struct sk_buff *skb;
6869
6870 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6871 kfree_skb(skb);
6872 }
6873#else
6874 int i;
6875
Jens Axboe65e19f52019-10-26 07:20:21 -06006876 for (i = 0; i < ctx->nr_user_files; i++) {
6877 struct file *file;
6878
6879 file = io_file_from_index(ctx, i);
6880 if (file)
6881 fput(file);
6882 }
Jens Axboe6b063142019-01-10 22:13:58 -07006883#endif
6884}
6885
Jens Axboe05f3fb32019-12-09 11:22:50 -07006886static void io_file_ref_kill(struct percpu_ref *ref)
6887{
6888 struct fixed_file_data *data;
6889
6890 data = container_of(ref, struct fixed_file_data, refs);
6891 complete(&data->done);
6892}
6893
Jens Axboe6b063142019-01-10 22:13:58 -07006894static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6895{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006896 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006897 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006898 unsigned nr_tables, i;
6899
Jens Axboe05f3fb32019-12-09 11:22:50 -07006900 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006901 return -ENXIO;
6902
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006903 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006904 if (!list_empty(&data->ref_list))
6905 ref_node = list_first_entry(&data->ref_list,
6906 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006907 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006908 if (ref_node)
6909 percpu_ref_kill(&ref_node->refs);
6910
6911 percpu_ref_kill(&data->refs);
6912
6913 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006914 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006915 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006916
Jens Axboe6b063142019-01-10 22:13:58 -07006917 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006918 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6919 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006920 kfree(data->table[i].files);
6921 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006922 percpu_ref_exit(&data->refs);
6923 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006924 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006925 ctx->nr_user_files = 0;
6926 return 0;
6927}
6928
Jens Axboe6c271ce2019-01-10 11:22:30 -07006929static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6930{
6931 if (ctx->sqo_thread) {
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02006932 /*
6933 * We may arrive here from the error branch in
6934 * io_sq_offload_create() where the kthread is created
6935 * without being waked up, thus wake it up now to make
6936 * sure the wait will complete.
6937 */
6938 wake_up_process(ctx->sqo_thread);
6939
Jens Axboe0f158b42020-05-14 17:18:39 -06006940 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006941 /*
6942 * The park is a bit of a work-around, without it we get
6943 * warning spews on shutdown with SQPOLL set and affinity
6944 * set to a single CPU.
6945 */
Jens Axboe06058632019-04-13 09:26:03 -06006946 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006947 kthread_stop(ctx->sqo_thread);
6948 ctx->sqo_thread = NULL;
6949 }
6950}
6951
Jens Axboe6b063142019-01-10 22:13:58 -07006952static void io_finish_async(struct io_ring_ctx *ctx)
6953{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006954 io_sq_thread_stop(ctx);
6955
Jens Axboe561fb042019-10-24 07:25:42 -06006956 if (ctx->io_wq) {
6957 io_wq_destroy(ctx->io_wq);
6958 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006959 }
6960}
6961
6962#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006963/*
6964 * Ensure the UNIX gc is aware of our file set, so we are certain that
6965 * the io_uring can be safely unregistered on process exit, even if we have
6966 * loops in the file referencing.
6967 */
6968static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6969{
6970 struct sock *sk = ctx->ring_sock->sk;
6971 struct scm_fp_list *fpl;
6972 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006973 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006974
Jens Axboe6b063142019-01-10 22:13:58 -07006975 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6976 if (!fpl)
6977 return -ENOMEM;
6978
6979 skb = alloc_skb(0, GFP_KERNEL);
6980 if (!skb) {
6981 kfree(fpl);
6982 return -ENOMEM;
6983 }
6984
6985 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006986
Jens Axboe08a45172019-10-03 08:11:03 -06006987 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006988 fpl->user = get_uid(ctx->user);
6989 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006990 struct file *file = io_file_from_index(ctx, i + offset);
6991
6992 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006993 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006994 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006995 unix_inflight(fpl->user, fpl->fp[nr_files]);
6996 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006997 }
6998
Jens Axboe08a45172019-10-03 08:11:03 -06006999 if (nr_files) {
7000 fpl->max = SCM_MAX_FD;
7001 fpl->count = nr_files;
7002 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007003 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007004 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7005 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007006
Jens Axboe08a45172019-10-03 08:11:03 -06007007 for (i = 0; i < nr_files; i++)
7008 fput(fpl->fp[i]);
7009 } else {
7010 kfree_skb(skb);
7011 kfree(fpl);
7012 }
Jens Axboe6b063142019-01-10 22:13:58 -07007013
7014 return 0;
7015}
7016
7017/*
7018 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7019 * causes regular reference counting to break down. We rely on the UNIX
7020 * garbage collection to take care of this problem for us.
7021 */
7022static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7023{
7024 unsigned left, total;
7025 int ret = 0;
7026
7027 total = 0;
7028 left = ctx->nr_user_files;
7029 while (left) {
7030 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007031
7032 ret = __io_sqe_files_scm(ctx, this_files, total);
7033 if (ret)
7034 break;
7035 left -= this_files;
7036 total += this_files;
7037 }
7038
7039 if (!ret)
7040 return 0;
7041
7042 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007043 struct file *file = io_file_from_index(ctx, total);
7044
7045 if (file)
7046 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007047 total++;
7048 }
7049
7050 return ret;
7051}
7052#else
7053static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7054{
7055 return 0;
7056}
7057#endif
7058
Jens Axboe65e19f52019-10-26 07:20:21 -06007059static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7060 unsigned nr_files)
7061{
7062 int i;
7063
7064 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007065 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007066 unsigned this_files;
7067
7068 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7069 table->files = kcalloc(this_files, sizeof(struct file *),
7070 GFP_KERNEL);
7071 if (!table->files)
7072 break;
7073 nr_files -= this_files;
7074 }
7075
7076 if (i == nr_tables)
7077 return 0;
7078
7079 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007080 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007081 kfree(table->files);
7082 }
7083 return 1;
7084}
7085
Jens Axboe05f3fb32019-12-09 11:22:50 -07007086static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007087{
7088#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007089 struct sock *sock = ctx->ring_sock->sk;
7090 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7091 struct sk_buff *skb;
7092 int i;
7093
7094 __skb_queue_head_init(&list);
7095
7096 /*
7097 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7098 * remove this entry and rearrange the file array.
7099 */
7100 skb = skb_dequeue(head);
7101 while (skb) {
7102 struct scm_fp_list *fp;
7103
7104 fp = UNIXCB(skb).fp;
7105 for (i = 0; i < fp->count; i++) {
7106 int left;
7107
7108 if (fp->fp[i] != file)
7109 continue;
7110
7111 unix_notinflight(fp->user, fp->fp[i]);
7112 left = fp->count - 1 - i;
7113 if (left) {
7114 memmove(&fp->fp[i], &fp->fp[i + 1],
7115 left * sizeof(struct file *));
7116 }
7117 fp->count--;
7118 if (!fp->count) {
7119 kfree_skb(skb);
7120 skb = NULL;
7121 } else {
7122 __skb_queue_tail(&list, skb);
7123 }
7124 fput(file);
7125 file = NULL;
7126 break;
7127 }
7128
7129 if (!file)
7130 break;
7131
7132 __skb_queue_tail(&list, skb);
7133
7134 skb = skb_dequeue(head);
7135 }
7136
7137 if (skb_peek(&list)) {
7138 spin_lock_irq(&head->lock);
7139 while ((skb = __skb_dequeue(&list)) != NULL)
7140 __skb_queue_tail(head, skb);
7141 spin_unlock_irq(&head->lock);
7142 }
7143#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007144 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007145#endif
7146}
7147
Jens Axboe05f3fb32019-12-09 11:22:50 -07007148struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007149 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007150 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007151};
7152
Jens Axboe4a38aed22020-05-14 17:21:15 -06007153static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007154{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007155 struct fixed_file_data *file_data = ref_node->file_data;
7156 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007157 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007158
7159 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007160 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007161 io_ring_file_put(ctx, pfile->file);
7162 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007163 }
7164
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007165 spin_lock(&file_data->lock);
7166 list_del(&ref_node->node);
7167 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007168
Xiaoguang Wang05589552020-03-31 14:05:18 +08007169 percpu_ref_exit(&ref_node->refs);
7170 kfree(ref_node);
7171 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007172}
7173
Jens Axboe4a38aed22020-05-14 17:21:15 -06007174static void io_file_put_work(struct work_struct *work)
7175{
7176 struct io_ring_ctx *ctx;
7177 struct llist_node *node;
7178
7179 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7180 node = llist_del_all(&ctx->file_put_llist);
7181
7182 while (node) {
7183 struct fixed_file_ref_node *ref_node;
7184 struct llist_node *next = node->next;
7185
7186 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7187 __io_file_put_work(ref_node);
7188 node = next;
7189 }
7190}
7191
Jens Axboe05f3fb32019-12-09 11:22:50 -07007192static void io_file_data_ref_zero(struct percpu_ref *ref)
7193{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007194 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007195 struct io_ring_ctx *ctx;
7196 bool first_add;
7197 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007198
Xiaoguang Wang05589552020-03-31 14:05:18 +08007199 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007200 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007201
Jens Axboe4a38aed22020-05-14 17:21:15 -06007202 if (percpu_ref_is_dying(&ctx->file_data->refs))
7203 delay = 0;
7204
7205 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7206 if (!delay)
7207 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7208 else if (first_add)
7209 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007210}
7211
7212static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7213 struct io_ring_ctx *ctx)
7214{
7215 struct fixed_file_ref_node *ref_node;
7216
7217 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7218 if (!ref_node)
7219 return ERR_PTR(-ENOMEM);
7220
7221 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7222 0, GFP_KERNEL)) {
7223 kfree(ref_node);
7224 return ERR_PTR(-ENOMEM);
7225 }
7226 INIT_LIST_HEAD(&ref_node->node);
7227 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007228 ref_node->file_data = ctx->file_data;
7229 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007230}
7231
7232static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7233{
7234 percpu_ref_exit(&ref_node->refs);
7235 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007236}
7237
7238static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7239 unsigned nr_args)
7240{
7241 __s32 __user *fds = (__s32 __user *) arg;
7242 unsigned nr_tables;
7243 struct file *file;
7244 int fd, ret = 0;
7245 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007246 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007247
7248 if (ctx->file_data)
7249 return -EBUSY;
7250 if (!nr_args)
7251 return -EINVAL;
7252 if (nr_args > IORING_MAX_FIXED_FILES)
7253 return -EMFILE;
7254
7255 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7256 if (!ctx->file_data)
7257 return -ENOMEM;
7258 ctx->file_data->ctx = ctx;
7259 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007260 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007261 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007262
7263 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7264 ctx->file_data->table = kcalloc(nr_tables,
7265 sizeof(struct fixed_file_table),
7266 GFP_KERNEL);
7267 if (!ctx->file_data->table) {
7268 kfree(ctx->file_data);
7269 ctx->file_data = NULL;
7270 return -ENOMEM;
7271 }
7272
Xiaoguang Wang05589552020-03-31 14:05:18 +08007273 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007274 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7275 kfree(ctx->file_data->table);
7276 kfree(ctx->file_data);
7277 ctx->file_data = NULL;
7278 return -ENOMEM;
7279 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007280
7281 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7282 percpu_ref_exit(&ctx->file_data->refs);
7283 kfree(ctx->file_data->table);
7284 kfree(ctx->file_data);
7285 ctx->file_data = NULL;
7286 return -ENOMEM;
7287 }
7288
7289 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7290 struct fixed_file_table *table;
7291 unsigned index;
7292
7293 ret = -EFAULT;
7294 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7295 break;
7296 /* allow sparse sets */
7297 if (fd == -1) {
7298 ret = 0;
7299 continue;
7300 }
7301
7302 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7303 index = i & IORING_FILE_TABLE_MASK;
7304 file = fget(fd);
7305
7306 ret = -EBADF;
7307 if (!file)
7308 break;
7309
7310 /*
7311 * Don't allow io_uring instances to be registered. If UNIX
7312 * isn't enabled, then this causes a reference cycle and this
7313 * instance can never get freed. If UNIX is enabled we'll
7314 * handle it just fine, but there's still no point in allowing
7315 * a ring fd as it doesn't support regular read/write anyway.
7316 */
7317 if (file->f_op == &io_uring_fops) {
7318 fput(file);
7319 break;
7320 }
7321 ret = 0;
7322 table->files[index] = file;
7323 }
7324
7325 if (ret) {
7326 for (i = 0; i < ctx->nr_user_files; i++) {
7327 file = io_file_from_index(ctx, i);
7328 if (file)
7329 fput(file);
7330 }
7331 for (i = 0; i < nr_tables; i++)
7332 kfree(ctx->file_data->table[i].files);
7333
Yang Yingliang667e57d2020-07-10 14:14:20 +00007334 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007335 kfree(ctx->file_data->table);
7336 kfree(ctx->file_data);
7337 ctx->file_data = NULL;
7338 ctx->nr_user_files = 0;
7339 return ret;
7340 }
7341
7342 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007343 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007344 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007345 return ret;
7346 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007347
Xiaoguang Wang05589552020-03-31 14:05:18 +08007348 ref_node = alloc_fixed_file_ref_node(ctx);
7349 if (IS_ERR(ref_node)) {
7350 io_sqe_files_unregister(ctx);
7351 return PTR_ERR(ref_node);
7352 }
7353
7354 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007355 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007356 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007357 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007358 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007359 return ret;
7360}
7361
Jens Axboec3a31e62019-10-03 13:59:56 -06007362static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7363 int index)
7364{
7365#if defined(CONFIG_UNIX)
7366 struct sock *sock = ctx->ring_sock->sk;
7367 struct sk_buff_head *head = &sock->sk_receive_queue;
7368 struct sk_buff *skb;
7369
7370 /*
7371 * See if we can merge this file into an existing skb SCM_RIGHTS
7372 * file set. If there's no room, fall back to allocating a new skb
7373 * and filling it in.
7374 */
7375 spin_lock_irq(&head->lock);
7376 skb = skb_peek(head);
7377 if (skb) {
7378 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7379
7380 if (fpl->count < SCM_MAX_FD) {
7381 __skb_unlink(skb, head);
7382 spin_unlock_irq(&head->lock);
7383 fpl->fp[fpl->count] = get_file(file);
7384 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7385 fpl->count++;
7386 spin_lock_irq(&head->lock);
7387 __skb_queue_head(head, skb);
7388 } else {
7389 skb = NULL;
7390 }
7391 }
7392 spin_unlock_irq(&head->lock);
7393
7394 if (skb) {
7395 fput(file);
7396 return 0;
7397 }
7398
7399 return __io_sqe_files_scm(ctx, 1, index);
7400#else
7401 return 0;
7402#endif
7403}
7404
Hillf Dantona5318d32020-03-23 17:47:15 +08007405static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007406 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007407{
Hillf Dantona5318d32020-03-23 17:47:15 +08007408 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007409 struct percpu_ref *refs = data->cur_refs;
7410 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007411
Jens Axboe05f3fb32019-12-09 11:22:50 -07007412 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007413 if (!pfile)
7414 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007415
Xiaoguang Wang05589552020-03-31 14:05:18 +08007416 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007417 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007418 list_add(&pfile->list, &ref_node->file_list);
7419
Hillf Dantona5318d32020-03-23 17:47:15 +08007420 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007421}
7422
7423static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7424 struct io_uring_files_update *up,
7425 unsigned nr_args)
7426{
7427 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007428 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007429 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007430 __s32 __user *fds;
7431 int fd, i, err;
7432 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007433 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007434
Jens Axboe05f3fb32019-12-09 11:22:50 -07007435 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007436 return -EOVERFLOW;
7437 if (done > ctx->nr_user_files)
7438 return -EINVAL;
7439
Xiaoguang Wang05589552020-03-31 14:05:18 +08007440 ref_node = alloc_fixed_file_ref_node(ctx);
7441 if (IS_ERR(ref_node))
7442 return PTR_ERR(ref_node);
7443
Jens Axboec3a31e62019-10-03 13:59:56 -06007444 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007445 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007446 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007447 struct fixed_file_table *table;
7448 unsigned index;
7449
Jens Axboec3a31e62019-10-03 13:59:56 -06007450 err = 0;
7451 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7452 err = -EFAULT;
7453 break;
7454 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007455 i = array_index_nospec(up->offset, ctx->nr_user_files);
7456 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007457 index = i & IORING_FILE_TABLE_MASK;
7458 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007459 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007460 err = io_queue_file_removal(data, file);
7461 if (err)
7462 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007463 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007464 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007465 }
7466 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007467 file = fget(fd);
7468 if (!file) {
7469 err = -EBADF;
7470 break;
7471 }
7472 /*
7473 * Don't allow io_uring instances to be registered. If
7474 * UNIX isn't enabled, then this causes a reference
7475 * cycle and this instance can never get freed. If UNIX
7476 * is enabled we'll handle it just fine, but there's
7477 * still no point in allowing a ring fd as it doesn't
7478 * support regular read/write anyway.
7479 */
7480 if (file->f_op == &io_uring_fops) {
7481 fput(file);
7482 err = -EBADF;
7483 break;
7484 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007485 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007486 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007487 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007488 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007489 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007490 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007491 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007492 }
7493 nr_args--;
7494 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007495 up->offset++;
7496 }
7497
Xiaoguang Wang05589552020-03-31 14:05:18 +08007498 if (needs_switch) {
7499 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007500 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007501 list_add(&ref_node->node, &data->ref_list);
7502 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007503 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007504 percpu_ref_get(&ctx->file_data->refs);
7505 } else
7506 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007507
7508 return done ? done : err;
7509}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007510
Jens Axboe05f3fb32019-12-09 11:22:50 -07007511static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7512 unsigned nr_args)
7513{
7514 struct io_uring_files_update up;
7515
7516 if (!ctx->file_data)
7517 return -ENXIO;
7518 if (!nr_args)
7519 return -EINVAL;
7520 if (copy_from_user(&up, arg, sizeof(up)))
7521 return -EFAULT;
7522 if (up.resv)
7523 return -EINVAL;
7524
7525 return __io_sqe_files_update(ctx, &up, nr_args);
7526}
Jens Axboec3a31e62019-10-03 13:59:56 -06007527
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007528static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007529{
7530 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7531
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007532 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007533 io_put_req(req);
7534}
7535
Pavel Begunkov24369c22020-01-28 03:15:48 +03007536static int io_init_wq_offload(struct io_ring_ctx *ctx,
7537 struct io_uring_params *p)
7538{
7539 struct io_wq_data data;
7540 struct fd f;
7541 struct io_ring_ctx *ctx_attach;
7542 unsigned int concurrency;
7543 int ret = 0;
7544
7545 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007546 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007547 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007548
7549 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7550 /* Do QD, or 4 * CPUS, whatever is smallest */
7551 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7552
7553 ctx->io_wq = io_wq_create(concurrency, &data);
7554 if (IS_ERR(ctx->io_wq)) {
7555 ret = PTR_ERR(ctx->io_wq);
7556 ctx->io_wq = NULL;
7557 }
7558 return ret;
7559 }
7560
7561 f = fdget(p->wq_fd);
7562 if (!f.file)
7563 return -EBADF;
7564
7565 if (f.file->f_op != &io_uring_fops) {
7566 ret = -EINVAL;
7567 goto out_fput;
7568 }
7569
7570 ctx_attach = f.file->private_data;
7571 /* @io_wq is protected by holding the fd */
7572 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7573 ret = -EINVAL;
7574 goto out_fput;
7575 }
7576
7577 ctx->io_wq = ctx_attach->io_wq;
7578out_fput:
7579 fdput(f);
7580 return ret;
7581}
7582
Jens Axboe0f212202020-09-13 13:09:39 -06007583static int io_uring_alloc_task_context(struct task_struct *task)
7584{
7585 struct io_uring_task *tctx;
7586
7587 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7588 if (unlikely(!tctx))
7589 return -ENOMEM;
7590
7591 xa_init(&tctx->xa);
7592 init_waitqueue_head(&tctx->wait);
7593 tctx->last = NULL;
7594 tctx->in_idle = 0;
7595 atomic_long_set(&tctx->req_issue, 0);
7596 atomic_long_set(&tctx->req_complete, 0);
7597 task->io_uring = tctx;
7598 return 0;
7599}
7600
7601void __io_uring_free(struct task_struct *tsk)
7602{
7603 struct io_uring_task *tctx = tsk->io_uring;
7604
7605 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7606 xa_destroy(&tctx->xa);
7607 kfree(tctx);
7608 tsk->io_uring = NULL;
7609}
7610
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007611static int io_sq_offload_create(struct io_ring_ctx *ctx,
7612 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007613{
7614 int ret;
7615
Jens Axboe6c271ce2019-01-10 11:22:30 -07007616 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007617 ret = -EPERM;
7618 if (!capable(CAP_SYS_ADMIN))
7619 goto err;
7620
Jens Axboe917257d2019-04-13 09:28:55 -06007621 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7622 if (!ctx->sq_thread_idle)
7623 ctx->sq_thread_idle = HZ;
7624
Jens Axboe6c271ce2019-01-10 11:22:30 -07007625 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007626 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007627
Jens Axboe917257d2019-04-13 09:28:55 -06007628 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007629 if (cpu >= nr_cpu_ids)
7630 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007631 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007632 goto err;
7633
Jens Axboe6c271ce2019-01-10 11:22:30 -07007634 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7635 ctx, cpu,
7636 "io_uring-sq");
7637 } else {
7638 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7639 "io_uring-sq");
7640 }
7641 if (IS_ERR(ctx->sqo_thread)) {
7642 ret = PTR_ERR(ctx->sqo_thread);
7643 ctx->sqo_thread = NULL;
7644 goto err;
7645 }
Jens Axboe0f212202020-09-13 13:09:39 -06007646 ret = io_uring_alloc_task_context(ctx->sqo_thread);
7647 if (ret)
7648 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007649 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7650 /* Can't have SQ_AFF without SQPOLL */
7651 ret = -EINVAL;
7652 goto err;
7653 }
7654
Pavel Begunkov24369c22020-01-28 03:15:48 +03007655 ret = io_init_wq_offload(ctx, p);
7656 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007657 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007658
7659 return 0;
7660err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007661 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007662 return ret;
7663}
7664
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007665static void io_sq_offload_start(struct io_ring_ctx *ctx)
7666{
7667 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sqo_thread)
7668 wake_up_process(ctx->sqo_thread);
7669}
7670
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007671static inline void __io_unaccount_mem(struct user_struct *user,
7672 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007673{
7674 atomic_long_sub(nr_pages, &user->locked_vm);
7675}
7676
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007677static inline int __io_account_mem(struct user_struct *user,
7678 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007679{
7680 unsigned long page_limit, cur_pages, new_pages;
7681
7682 /* Don't allow more pages than we can safely lock */
7683 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7684
7685 do {
7686 cur_pages = atomic_long_read(&user->locked_vm);
7687 new_pages = cur_pages + nr_pages;
7688 if (new_pages > page_limit)
7689 return -ENOMEM;
7690 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7691 new_pages) != cur_pages);
7692
7693 return 0;
7694}
7695
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007696static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7697 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007698{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007699 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007700 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007701
Jens Axboe2aede0e2020-09-14 10:45:53 -06007702 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007703 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007704 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007705 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007706 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007707 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007708}
7709
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007710static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7711 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007712{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007713 int ret;
7714
7715 if (ctx->limit_mem) {
7716 ret = __io_account_mem(ctx->user, nr_pages);
7717 if (ret)
7718 return ret;
7719 }
7720
Jens Axboe2aede0e2020-09-14 10:45:53 -06007721 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007722 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007723 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007724 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007725 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007726 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007727
7728 return 0;
7729}
7730
Jens Axboe2b188cc2019-01-07 10:46:33 -07007731static void io_mem_free(void *ptr)
7732{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007733 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007734
Mark Rutland52e04ef2019-04-30 17:30:21 +01007735 if (!ptr)
7736 return;
7737
7738 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007739 if (put_page_testzero(page))
7740 free_compound_page(page);
7741}
7742
7743static void *io_mem_alloc(size_t size)
7744{
7745 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7746 __GFP_NORETRY;
7747
7748 return (void *) __get_free_pages(gfp_flags, get_order(size));
7749}
7750
Hristo Venev75b28af2019-08-26 17:23:46 +00007751static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7752 size_t *sq_offset)
7753{
7754 struct io_rings *rings;
7755 size_t off, sq_array_size;
7756
7757 off = struct_size(rings, cqes, cq_entries);
7758 if (off == SIZE_MAX)
7759 return SIZE_MAX;
7760
7761#ifdef CONFIG_SMP
7762 off = ALIGN(off, SMP_CACHE_BYTES);
7763 if (off == 0)
7764 return SIZE_MAX;
7765#endif
7766
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007767 if (sq_offset)
7768 *sq_offset = off;
7769
Hristo Venev75b28af2019-08-26 17:23:46 +00007770 sq_array_size = array_size(sizeof(u32), sq_entries);
7771 if (sq_array_size == SIZE_MAX)
7772 return SIZE_MAX;
7773
7774 if (check_add_overflow(off, sq_array_size, &off))
7775 return SIZE_MAX;
7776
Hristo Venev75b28af2019-08-26 17:23:46 +00007777 return off;
7778}
7779
Jens Axboe2b188cc2019-01-07 10:46:33 -07007780static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7781{
Hristo Venev75b28af2019-08-26 17:23:46 +00007782 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007783
Hristo Venev75b28af2019-08-26 17:23:46 +00007784 pages = (size_t)1 << get_order(
7785 rings_size(sq_entries, cq_entries, NULL));
7786 pages += (size_t)1 << get_order(
7787 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007788
Hristo Venev75b28af2019-08-26 17:23:46 +00007789 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007790}
7791
Jens Axboeedafcce2019-01-09 09:16:05 -07007792static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7793{
7794 int i, j;
7795
7796 if (!ctx->user_bufs)
7797 return -ENXIO;
7798
7799 for (i = 0; i < ctx->nr_user_bufs; i++) {
7800 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7801
7802 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007803 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007804
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007805 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007806 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007807 imu->nr_bvecs = 0;
7808 }
7809
7810 kfree(ctx->user_bufs);
7811 ctx->user_bufs = NULL;
7812 ctx->nr_user_bufs = 0;
7813 return 0;
7814}
7815
7816static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7817 void __user *arg, unsigned index)
7818{
7819 struct iovec __user *src;
7820
7821#ifdef CONFIG_COMPAT
7822 if (ctx->compat) {
7823 struct compat_iovec __user *ciovs;
7824 struct compat_iovec ciov;
7825
7826 ciovs = (struct compat_iovec __user *) arg;
7827 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7828 return -EFAULT;
7829
Jens Axboed55e5f52019-12-11 16:12:15 -07007830 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007831 dst->iov_len = ciov.iov_len;
7832 return 0;
7833 }
7834#endif
7835 src = (struct iovec __user *) arg;
7836 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7837 return -EFAULT;
7838 return 0;
7839}
7840
7841static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7842 unsigned nr_args)
7843{
7844 struct vm_area_struct **vmas = NULL;
7845 struct page **pages = NULL;
7846 int i, j, got_pages = 0;
7847 int ret = -EINVAL;
7848
7849 if (ctx->user_bufs)
7850 return -EBUSY;
7851 if (!nr_args || nr_args > UIO_MAXIOV)
7852 return -EINVAL;
7853
7854 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7855 GFP_KERNEL);
7856 if (!ctx->user_bufs)
7857 return -ENOMEM;
7858
7859 for (i = 0; i < nr_args; i++) {
7860 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7861 unsigned long off, start, end, ubuf;
7862 int pret, nr_pages;
7863 struct iovec iov;
7864 size_t size;
7865
7866 ret = io_copy_iov(ctx, &iov, arg, i);
7867 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007868 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007869
7870 /*
7871 * Don't impose further limits on the size and buffer
7872 * constraints here, we'll -EINVAL later when IO is
7873 * submitted if they are wrong.
7874 */
7875 ret = -EFAULT;
7876 if (!iov.iov_base || !iov.iov_len)
7877 goto err;
7878
7879 /* arbitrary limit, but we need something */
7880 if (iov.iov_len > SZ_1G)
7881 goto err;
7882
7883 ubuf = (unsigned long) iov.iov_base;
7884 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7885 start = ubuf >> PAGE_SHIFT;
7886 nr_pages = end - start;
7887
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007888 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007889 if (ret)
7890 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007891
7892 ret = 0;
7893 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007894 kvfree(vmas);
7895 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007896 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007897 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007898 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007899 sizeof(struct vm_area_struct *),
7900 GFP_KERNEL);
7901 if (!pages || !vmas) {
7902 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007903 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007904 goto err;
7905 }
7906 got_pages = nr_pages;
7907 }
7908
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007909 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007910 GFP_KERNEL);
7911 ret = -ENOMEM;
7912 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007913 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007914 goto err;
7915 }
7916
7917 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007918 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007919 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007920 FOLL_WRITE | FOLL_LONGTERM,
7921 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007922 if (pret == nr_pages) {
7923 /* don't support file backed memory */
7924 for (j = 0; j < nr_pages; j++) {
7925 struct vm_area_struct *vma = vmas[j];
7926
7927 if (vma->vm_file &&
7928 !is_file_hugepages(vma->vm_file)) {
7929 ret = -EOPNOTSUPP;
7930 break;
7931 }
7932 }
7933 } else {
7934 ret = pret < 0 ? pret : -EFAULT;
7935 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007936 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007937 if (ret) {
7938 /*
7939 * if we did partial map, or found file backed vmas,
7940 * release any pages we did get
7941 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007942 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007943 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007944 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007945 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007946 goto err;
7947 }
7948
7949 off = ubuf & ~PAGE_MASK;
7950 size = iov.iov_len;
7951 for (j = 0; j < nr_pages; j++) {
7952 size_t vec_len;
7953
7954 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7955 imu->bvec[j].bv_page = pages[j];
7956 imu->bvec[j].bv_len = vec_len;
7957 imu->bvec[j].bv_offset = off;
7958 off = 0;
7959 size -= vec_len;
7960 }
7961 /* store original address for later verification */
7962 imu->ubuf = ubuf;
7963 imu->len = iov.iov_len;
7964 imu->nr_bvecs = nr_pages;
7965
7966 ctx->nr_user_bufs++;
7967 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007968 kvfree(pages);
7969 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007970 return 0;
7971err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007972 kvfree(pages);
7973 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007974 io_sqe_buffer_unregister(ctx);
7975 return ret;
7976}
7977
Jens Axboe9b402842019-04-11 11:45:41 -06007978static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7979{
7980 __s32 __user *fds = arg;
7981 int fd;
7982
7983 if (ctx->cq_ev_fd)
7984 return -EBUSY;
7985
7986 if (copy_from_user(&fd, fds, sizeof(*fds)))
7987 return -EFAULT;
7988
7989 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7990 if (IS_ERR(ctx->cq_ev_fd)) {
7991 int ret = PTR_ERR(ctx->cq_ev_fd);
7992 ctx->cq_ev_fd = NULL;
7993 return ret;
7994 }
7995
7996 return 0;
7997}
7998
7999static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8000{
8001 if (ctx->cq_ev_fd) {
8002 eventfd_ctx_put(ctx->cq_ev_fd);
8003 ctx->cq_ev_fd = NULL;
8004 return 0;
8005 }
8006
8007 return -ENXIO;
8008}
8009
Jens Axboe5a2e7452020-02-23 16:23:11 -07008010static int __io_destroy_buffers(int id, void *p, void *data)
8011{
8012 struct io_ring_ctx *ctx = data;
8013 struct io_buffer *buf = p;
8014
Jens Axboe067524e2020-03-02 16:32:28 -07008015 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008016 return 0;
8017}
8018
8019static void io_destroy_buffers(struct io_ring_ctx *ctx)
8020{
8021 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8022 idr_destroy(&ctx->io_buffer_idr);
8023}
8024
Jens Axboe2b188cc2019-01-07 10:46:33 -07008025static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8026{
Jens Axboe6b063142019-01-10 22:13:58 -07008027 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008028 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008029
8030 if (ctx->sqo_task) {
8031 put_task_struct(ctx->sqo_task);
8032 ctx->sqo_task = NULL;
8033 mmdrop(ctx->mm_account);
8034 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008035 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008036
Jens Axboe6b063142019-01-10 22:13:58 -07008037 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008038 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008039 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008040 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008041
Jens Axboe2b188cc2019-01-07 10:46:33 -07008042#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008043 if (ctx->ring_sock) {
8044 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008045 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008046 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008047#endif
8048
Hristo Venev75b28af2019-08-26 17:23:46 +00008049 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008050 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008051
8052 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008053 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008054 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008055 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008056 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008057 kfree(ctx);
8058}
8059
8060static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8061{
8062 struct io_ring_ctx *ctx = file->private_data;
8063 __poll_t mask = 0;
8064
8065 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008066 /*
8067 * synchronizes with barrier from wq_has_sleeper call in
8068 * io_commit_cqring
8069 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008070 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00008071 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
8072 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008073 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008074 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008075 mask |= EPOLLIN | EPOLLRDNORM;
8076
8077 return mask;
8078}
8079
8080static int io_uring_fasync(int fd, struct file *file, int on)
8081{
8082 struct io_ring_ctx *ctx = file->private_data;
8083
8084 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8085}
8086
Jens Axboe071698e2020-01-28 10:04:42 -07008087static int io_remove_personalities(int id, void *p, void *data)
8088{
8089 struct io_ring_ctx *ctx = data;
8090 const struct cred *cred;
8091
8092 cred = idr_remove(&ctx->personality_idr, id);
8093 if (cred)
8094 put_cred(cred);
8095 return 0;
8096}
8097
Jens Axboe85faa7b2020-04-09 18:14:00 -06008098static void io_ring_exit_work(struct work_struct *work)
8099{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008100 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8101 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008102
Jens Axboe56952e92020-06-17 15:00:04 -06008103 /*
8104 * If we're doing polled IO and end up having requests being
8105 * submitted async (out-of-line), then completions can come in while
8106 * we're waiting for refs to drop. We need to reap these manually,
8107 * as nobody else will be looking for them.
8108 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008109 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008110 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008111 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008112 io_iopoll_try_reap_events(ctx);
8113 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008114 io_ring_ctx_free(ctx);
8115}
8116
Jens Axboe2b188cc2019-01-07 10:46:33 -07008117static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8118{
8119 mutex_lock(&ctx->uring_lock);
8120 percpu_ref_kill(&ctx->refs);
8121 mutex_unlock(&ctx->uring_lock);
8122
Jens Axboef3606e32020-09-22 08:18:24 -06008123 io_kill_timeouts(ctx, NULL);
8124 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008125
8126 if (ctx->io_wq)
8127 io_wq_cancel_all(ctx->io_wq);
8128
Jens Axboe15dff282019-11-13 09:09:23 -07008129 /* if we failed setting up the ctx, we might not have any rings */
8130 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008131 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008132 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008133 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008134
8135 /*
8136 * Do this upfront, so we won't have a grace period where the ring
8137 * is closed but resources aren't reaped yet. This can cause
8138 * spurious failure in setting up a new ring.
8139 */
Jens Axboe760618f2020-07-24 12:53:31 -06008140 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8141 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008142
Jens Axboe85faa7b2020-04-09 18:14:00 -06008143 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008144 /*
8145 * Use system_unbound_wq to avoid spawning tons of event kworkers
8146 * if we're exiting a ton of rings at the same time. It just adds
8147 * noise and overhead, there's no discernable change in runtime
8148 * over using system_wq.
8149 */
8150 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008151}
8152
8153static int io_uring_release(struct inode *inode, struct file *file)
8154{
8155 struct io_ring_ctx *ctx = file->private_data;
8156
8157 file->private_data = NULL;
8158 io_ring_ctx_wait_and_kill(ctx);
8159 return 0;
8160}
8161
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008162static bool io_wq_files_match(struct io_wq_work *work, void *data)
8163{
8164 struct files_struct *files = data;
8165
Jens Axboe0f212202020-09-13 13:09:39 -06008166 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008167}
8168
Jens Axboef254ac02020-08-12 17:33:30 -06008169/*
8170 * Returns true if 'preq' is the link parent of 'req'
8171 */
8172static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8173{
8174 struct io_kiocb *link;
8175
8176 if (!(preq->flags & REQ_F_LINK_HEAD))
8177 return false;
8178
8179 list_for_each_entry(link, &preq->link_list, link_list) {
8180 if (link == req)
8181 return true;
8182 }
8183
8184 return false;
8185}
8186
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008187static bool io_match_link_files(struct io_kiocb *req,
8188 struct files_struct *files)
8189{
8190 struct io_kiocb *link;
8191
8192 if (io_match_files(req, files))
8193 return true;
8194 if (req->flags & REQ_F_LINK_HEAD) {
8195 list_for_each_entry(link, &req->link_list, link_list) {
8196 if (io_match_files(link, files))
8197 return true;
8198 }
8199 }
8200 return false;
8201}
8202
Jens Axboef254ac02020-08-12 17:33:30 -06008203/*
8204 * We're looking to cancel 'req' because it's holding on to our files, but
8205 * 'req' could be a link to another request. See if it is, and cancel that
8206 * parent request if so.
8207 */
8208static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8209{
8210 struct hlist_node *tmp;
8211 struct io_kiocb *preq;
8212 bool found = false;
8213 int i;
8214
8215 spin_lock_irq(&ctx->completion_lock);
8216 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8217 struct hlist_head *list;
8218
8219 list = &ctx->cancel_hash[i];
8220 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8221 found = io_match_link(preq, req);
8222 if (found) {
8223 io_poll_remove_one(preq);
8224 break;
8225 }
8226 }
8227 }
8228 spin_unlock_irq(&ctx->completion_lock);
8229 return found;
8230}
8231
8232static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8233 struct io_kiocb *req)
8234{
8235 struct io_kiocb *preq;
8236 bool found = false;
8237
8238 spin_lock_irq(&ctx->completion_lock);
8239 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8240 found = io_match_link(preq, req);
8241 if (found) {
8242 __io_timeout_cancel(preq);
8243 break;
8244 }
8245 }
8246 spin_unlock_irq(&ctx->completion_lock);
8247 return found;
8248}
8249
Jens Axboeb711d4e2020-08-16 08:23:05 -07008250static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8251{
8252 return io_match_link(container_of(work, struct io_kiocb, work), data);
8253}
8254
8255static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8256{
8257 enum io_wq_cancel cret;
8258
8259 /* cancel this particular work, if it's running */
8260 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8261 if (cret != IO_WQ_CANCEL_NOTFOUND)
8262 return;
8263
8264 /* find links that hold this pending, cancel those */
8265 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8266 if (cret != IO_WQ_CANCEL_NOTFOUND)
8267 return;
8268
8269 /* if we have a poll link holding this pending, cancel that */
8270 if (io_poll_remove_link(ctx, req))
8271 return;
8272
8273 /* final option, timeout link is holding this req pending */
8274 io_timeout_remove_link(ctx, req);
8275}
8276
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008277static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8278 struct files_struct *files)
8279{
8280 struct io_defer_entry *de = NULL;
8281 LIST_HEAD(list);
8282
8283 spin_lock_irq(&ctx->completion_lock);
8284 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008285 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008286 list_cut_position(&list, &ctx->defer_list, &de->list);
8287 break;
8288 }
8289 }
8290 spin_unlock_irq(&ctx->completion_lock);
8291
8292 while (!list_empty(&list)) {
8293 de = list_first_entry(&list, struct io_defer_entry, list);
8294 list_del_init(&de->list);
8295 req_set_fail_links(de->req);
8296 io_put_req(de->req);
8297 io_req_complete(de->req, -ECANCELED);
8298 kfree(de);
8299 }
8300}
8301
Jens Axboe76e1b642020-09-26 15:05:03 -06008302/*
8303 * Returns true if we found and killed one or more files pinning requests
8304 */
8305static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008306 struct files_struct *files)
8307{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008308 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008309 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008310
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008311 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008312 /* cancel all at once, should be faster than doing it one by one*/
8313 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8314
Jens Axboefcb323c2019-10-24 12:39:47 -06008315 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008316 struct io_kiocb *cancel_req = NULL, *req;
8317 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008318
8319 spin_lock_irq(&ctx->inflight_lock);
8320 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008321 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008322 continue;
8323 /* req is being completed, ignore */
8324 if (!refcount_inc_not_zero(&req->refs))
8325 continue;
8326 cancel_req = req;
8327 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008328 }
Jens Axboe768134d2019-11-10 20:30:53 -07008329 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008330 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008331 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008332 spin_unlock_irq(&ctx->inflight_lock);
8333
Jens Axboe768134d2019-11-10 20:30:53 -07008334 /* We need to keep going until we don't find a matching req */
8335 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008336 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008337 /* cancel this request, or head link requests */
8338 io_attempt_cancel(ctx, cancel_req);
8339 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008340 /* cancellations _may_ trigger task work */
8341 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008342 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008343 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008344 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008345
8346 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008347}
8348
Pavel Begunkov801dd572020-06-15 10:33:14 +03008349static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008350{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008351 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8352 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008353
Jens Axboef3606e32020-09-22 08:18:24 -06008354 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008355}
8356
Jens Axboe0f212202020-09-13 13:09:39 -06008357static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8358 struct task_struct *task,
8359 struct files_struct *files)
8360{
8361 bool ret;
8362
8363 ret = io_uring_cancel_files(ctx, files);
8364 if (!files) {
8365 enum io_wq_cancel cret;
8366
8367 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8368 if (cret != IO_WQ_CANCEL_NOTFOUND)
8369 ret = true;
8370
8371 /* SQPOLL thread does its own polling */
8372 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8373 while (!list_empty_careful(&ctx->iopoll_list)) {
8374 io_iopoll_try_reap_events(ctx);
8375 ret = true;
8376 }
8377 }
8378
8379 ret |= io_poll_remove_all(ctx, task);
8380 ret |= io_kill_timeouts(ctx, task);
8381 }
8382
8383 return ret;
8384}
8385
8386/*
8387 * We need to iteratively cancel requests, in case a request has dependent
8388 * hard links. These persist even for failure of cancelations, hence keep
8389 * looping until none are found.
8390 */
8391static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8392 struct files_struct *files)
8393{
8394 struct task_struct *task = current;
8395
8396 if (ctx->flags & IORING_SETUP_SQPOLL)
8397 task = ctx->sqo_thread;
8398
8399 io_cqring_overflow_flush(ctx, true, task, files);
8400
8401 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8402 io_run_task_work();
8403 cond_resched();
8404 }
8405}
8406
8407/*
8408 * Note that this task has used io_uring. We use it for cancelation purposes.
8409 */
8410static int io_uring_add_task_file(struct file *file)
8411{
8412 if (unlikely(!current->io_uring)) {
8413 int ret;
8414
8415 ret = io_uring_alloc_task_context(current);
8416 if (unlikely(ret))
8417 return ret;
8418 }
8419 if (current->io_uring->last != file) {
8420 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8421 void *old;
8422
8423 rcu_read_lock();
8424 old = xas_load(&xas);
8425 if (old != file) {
8426 get_file(file);
8427 xas_lock(&xas);
8428 xas_store(&xas, file);
8429 xas_unlock(&xas);
8430 }
8431 rcu_read_unlock();
8432 current->io_uring->last = file;
8433 }
8434
8435 return 0;
8436}
8437
8438/*
8439 * Remove this io_uring_file -> task mapping.
8440 */
8441static void io_uring_del_task_file(struct file *file)
8442{
8443 struct io_uring_task *tctx = current->io_uring;
8444 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8445
8446 if (tctx->last == file)
8447 tctx->last = NULL;
8448
8449 xas_lock(&xas);
8450 file = xas_store(&xas, NULL);
8451 xas_unlock(&xas);
8452
8453 if (file)
8454 fput(file);
8455}
8456
8457static void __io_uring_attempt_task_drop(struct file *file)
8458{
8459 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8460 struct file *old;
8461
8462 rcu_read_lock();
8463 old = xas_load(&xas);
8464 rcu_read_unlock();
8465
8466 if (old == file)
8467 io_uring_del_task_file(file);
8468}
8469
8470/*
8471 * Drop task note for this file if we're the only ones that hold it after
8472 * pending fput()
8473 */
8474static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8475{
8476 if (!current->io_uring)
8477 return;
8478 /*
8479 * fput() is pending, will be 2 if the only other ref is our potential
8480 * task file note. If the task is exiting, drop regardless of count.
8481 */
8482 if (!exiting && atomic_long_read(&file->f_count) != 2)
8483 return;
8484
8485 __io_uring_attempt_task_drop(file);
8486}
8487
8488void __io_uring_files_cancel(struct files_struct *files)
8489{
8490 struct io_uring_task *tctx = current->io_uring;
8491 XA_STATE(xas, &tctx->xa, 0);
8492
8493 /* make sure overflow events are dropped */
8494 tctx->in_idle = true;
8495
8496 do {
8497 struct io_ring_ctx *ctx;
8498 struct file *file;
8499
8500 xas_lock(&xas);
8501 file = xas_next_entry(&xas, ULONG_MAX);
8502 xas_unlock(&xas);
8503
8504 if (!file)
8505 break;
8506
8507 ctx = file->private_data;
8508
8509 io_uring_cancel_task_requests(ctx, files);
8510 if (files)
8511 io_uring_del_task_file(file);
8512 } while (1);
8513}
8514
8515static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8516{
8517 return atomic_long_read(&tctx->req_issue) ==
8518 atomic_long_read(&tctx->req_complete);
8519}
8520
8521/*
8522 * Find any io_uring fd that this task has registered or done IO on, and cancel
8523 * requests.
8524 */
8525void __io_uring_task_cancel(void)
8526{
8527 struct io_uring_task *tctx = current->io_uring;
8528 DEFINE_WAIT(wait);
8529 long completions;
8530
8531 /* make sure overflow events are dropped */
8532 tctx->in_idle = true;
8533
8534 while (!io_uring_task_idle(tctx)) {
8535 /* read completions before cancelations */
8536 completions = atomic_long_read(&tctx->req_complete);
8537 __io_uring_files_cancel(NULL);
8538
8539 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8540
8541 /*
8542 * If we've seen completions, retry. This avoids a race where
8543 * a completion comes in before we did prepare_to_wait().
8544 */
8545 if (completions != atomic_long_read(&tctx->req_complete))
8546 continue;
8547 if (io_uring_task_idle(tctx))
8548 break;
8549 schedule();
8550 }
8551
8552 finish_wait(&tctx->wait, &wait);
8553 tctx->in_idle = false;
8554}
8555
Jens Axboefcb323c2019-10-24 12:39:47 -06008556static int io_uring_flush(struct file *file, void *data)
8557{
8558 struct io_ring_ctx *ctx = file->private_data;
8559
Jens Axboe6ab23142020-02-08 20:23:59 -07008560 /*
8561 * If the task is going away, cancel work it may have pending
8562 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008563 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008564 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008565
Jens Axboe0f212202020-09-13 13:09:39 -06008566 io_uring_cancel_task_requests(ctx, data);
8567 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008568 return 0;
8569}
8570
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008571static void *io_uring_validate_mmap_request(struct file *file,
8572 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008573{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008574 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008575 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008576 struct page *page;
8577 void *ptr;
8578
8579 switch (offset) {
8580 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008581 case IORING_OFF_CQ_RING:
8582 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008583 break;
8584 case IORING_OFF_SQES:
8585 ptr = ctx->sq_sqes;
8586 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008587 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008588 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008589 }
8590
8591 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008592 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008593 return ERR_PTR(-EINVAL);
8594
8595 return ptr;
8596}
8597
8598#ifdef CONFIG_MMU
8599
8600static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8601{
8602 size_t sz = vma->vm_end - vma->vm_start;
8603 unsigned long pfn;
8604 void *ptr;
8605
8606 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8607 if (IS_ERR(ptr))
8608 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008609
8610 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8611 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8612}
8613
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008614#else /* !CONFIG_MMU */
8615
8616static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8617{
8618 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8619}
8620
8621static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8622{
8623 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8624}
8625
8626static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8627 unsigned long addr, unsigned long len,
8628 unsigned long pgoff, unsigned long flags)
8629{
8630 void *ptr;
8631
8632 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8633 if (IS_ERR(ptr))
8634 return PTR_ERR(ptr);
8635
8636 return (unsigned long) ptr;
8637}
8638
8639#endif /* !CONFIG_MMU */
8640
Jens Axboe2b188cc2019-01-07 10:46:33 -07008641SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8642 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8643 size_t, sigsz)
8644{
8645 struct io_ring_ctx *ctx;
8646 long ret = -EBADF;
8647 int submitted = 0;
8648 struct fd f;
8649
Jens Axboe4c6e2772020-07-01 11:29:10 -06008650 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008651
Jens Axboe6c271ce2019-01-10 11:22:30 -07008652 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008653 return -EINVAL;
8654
8655 f = fdget(fd);
8656 if (!f.file)
8657 return -EBADF;
8658
8659 ret = -EOPNOTSUPP;
8660 if (f.file->f_op != &io_uring_fops)
8661 goto out_fput;
8662
8663 ret = -ENXIO;
8664 ctx = f.file->private_data;
8665 if (!percpu_ref_tryget(&ctx->refs))
8666 goto out_fput;
8667
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008668 ret = -EBADFD;
8669 if (ctx->flags & IORING_SETUP_R_DISABLED)
8670 goto out;
8671
Jens Axboe6c271ce2019-01-10 11:22:30 -07008672 /*
8673 * For SQ polling, the thread will do all submissions and completions.
8674 * Just return the requested submit count, and wake the thread if
8675 * we were asked to.
8676 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008677 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008678 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008679 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008680 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008681 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe6a779382020-09-02 12:21:41 -06008682 wake_up(ctx->sqo_wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008683 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008684 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008685 ret = io_uring_add_task_file(f.file);
8686 if (unlikely(ret))
8687 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008688 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008689 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008690 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008691
8692 if (submitted != to_submit)
8693 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008694 }
8695 if (flags & IORING_ENTER_GETEVENTS) {
8696 min_complete = min(min_complete, ctx->cq_entries);
8697
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008698 /*
8699 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8700 * space applications don't need to do io completion events
8701 * polling again, they can rely on io_sq_thread to do polling
8702 * work, which can reduce cpu usage and uring_lock contention.
8703 */
8704 if (ctx->flags & IORING_SETUP_IOPOLL &&
8705 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008706 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008707 } else {
8708 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8709 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008710 }
8711
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008712out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008713 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008714out_fput:
8715 fdput(f);
8716 return submitted ? submitted : ret;
8717}
8718
Tobias Klauserbebdb652020-02-26 18:38:32 +01008719#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008720static int io_uring_show_cred(int id, void *p, void *data)
8721{
8722 const struct cred *cred = p;
8723 struct seq_file *m = data;
8724 struct user_namespace *uns = seq_user_ns(m);
8725 struct group_info *gi;
8726 kernel_cap_t cap;
8727 unsigned __capi;
8728 int g;
8729
8730 seq_printf(m, "%5d\n", id);
8731 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8732 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8733 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8734 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8735 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8736 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8737 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8738 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8739 seq_puts(m, "\n\tGroups:\t");
8740 gi = cred->group_info;
8741 for (g = 0; g < gi->ngroups; g++) {
8742 seq_put_decimal_ull(m, g ? " " : "",
8743 from_kgid_munged(uns, gi->gid[g]));
8744 }
8745 seq_puts(m, "\n\tCapEff:\t");
8746 cap = cred->cap_effective;
8747 CAP_FOR_EACH_U32(__capi)
8748 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8749 seq_putc(m, '\n');
8750 return 0;
8751}
8752
8753static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8754{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008755 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008756 int i;
8757
Jens Axboefad8e0d2020-09-28 08:57:48 -06008758 /*
8759 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8760 * since fdinfo case grabs it in the opposite direction of normal use
8761 * cases. If we fail to get the lock, we just don't iterate any
8762 * structures that could be going away outside the io_uring mutex.
8763 */
8764 has_lock = mutex_trylock(&ctx->uring_lock);
8765
Jens Axboe87ce9552020-01-30 08:25:34 -07008766 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008767 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008768 struct fixed_file_table *table;
8769 struct file *f;
8770
8771 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8772 f = table->files[i & IORING_FILE_TABLE_MASK];
8773 if (f)
8774 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8775 else
8776 seq_printf(m, "%5u: <none>\n", i);
8777 }
8778 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008779 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008780 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8781
8782 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8783 (unsigned int) buf->len);
8784 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008785 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008786 seq_printf(m, "Personalities:\n");
8787 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8788 }
Jens Axboed7718a92020-02-14 22:23:12 -07008789 seq_printf(m, "PollList:\n");
8790 spin_lock_irq(&ctx->completion_lock);
8791 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8792 struct hlist_head *list = &ctx->cancel_hash[i];
8793 struct io_kiocb *req;
8794
8795 hlist_for_each_entry(req, list, hash_node)
8796 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8797 req->task->task_works != NULL);
8798 }
8799 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008800 if (has_lock)
8801 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008802}
8803
8804static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8805{
8806 struct io_ring_ctx *ctx = f->private_data;
8807
8808 if (percpu_ref_tryget(&ctx->refs)) {
8809 __io_uring_show_fdinfo(ctx, m);
8810 percpu_ref_put(&ctx->refs);
8811 }
8812}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008813#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008814
Jens Axboe2b188cc2019-01-07 10:46:33 -07008815static const struct file_operations io_uring_fops = {
8816 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008817 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008818 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008819#ifndef CONFIG_MMU
8820 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8821 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8822#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008823 .poll = io_uring_poll,
8824 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008825#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008826 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008827#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008828};
8829
8830static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8831 struct io_uring_params *p)
8832{
Hristo Venev75b28af2019-08-26 17:23:46 +00008833 struct io_rings *rings;
8834 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008835
Jens Axboebd740482020-08-05 12:58:23 -06008836 /* make sure these are sane, as we already accounted them */
8837 ctx->sq_entries = p->sq_entries;
8838 ctx->cq_entries = p->cq_entries;
8839
Hristo Venev75b28af2019-08-26 17:23:46 +00008840 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8841 if (size == SIZE_MAX)
8842 return -EOVERFLOW;
8843
8844 rings = io_mem_alloc(size);
8845 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008846 return -ENOMEM;
8847
Hristo Venev75b28af2019-08-26 17:23:46 +00008848 ctx->rings = rings;
8849 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8850 rings->sq_ring_mask = p->sq_entries - 1;
8851 rings->cq_ring_mask = p->cq_entries - 1;
8852 rings->sq_ring_entries = p->sq_entries;
8853 rings->cq_ring_entries = p->cq_entries;
8854 ctx->sq_mask = rings->sq_ring_mask;
8855 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008856
8857 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008858 if (size == SIZE_MAX) {
8859 io_mem_free(ctx->rings);
8860 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008861 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008862 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008863
8864 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008865 if (!ctx->sq_sqes) {
8866 io_mem_free(ctx->rings);
8867 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008868 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008869 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008870
Jens Axboe2b188cc2019-01-07 10:46:33 -07008871 return 0;
8872}
8873
8874/*
8875 * Allocate an anonymous fd, this is what constitutes the application
8876 * visible backing of an io_uring instance. The application mmaps this
8877 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8878 * we have to tie this fd to a socket for file garbage collection purposes.
8879 */
8880static int io_uring_get_fd(struct io_ring_ctx *ctx)
8881{
8882 struct file *file;
8883 int ret;
8884
8885#if defined(CONFIG_UNIX)
8886 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8887 &ctx->ring_sock);
8888 if (ret)
8889 return ret;
8890#endif
8891
8892 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8893 if (ret < 0)
8894 goto err;
8895
8896 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8897 O_RDWR | O_CLOEXEC);
8898 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008899err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07008900 put_unused_fd(ret);
8901 ret = PTR_ERR(file);
8902 goto err;
8903 }
8904
8905#if defined(CONFIG_UNIX)
8906 ctx->ring_sock->file = file;
8907#endif
Jens Axboe0f212202020-09-13 13:09:39 -06008908 if (unlikely(io_uring_add_task_file(file))) {
8909 file = ERR_PTR(-ENOMEM);
8910 goto err_fd;
8911 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008912 fd_install(ret, file);
8913 return ret;
8914err:
8915#if defined(CONFIG_UNIX)
8916 sock_release(ctx->ring_sock);
8917 ctx->ring_sock = NULL;
8918#endif
8919 return ret;
8920}
8921
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008922static int io_uring_create(unsigned entries, struct io_uring_params *p,
8923 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008924{
8925 struct user_struct *user = NULL;
8926 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008927 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008928 int ret;
8929
Jens Axboe8110c1a2019-12-28 15:39:54 -07008930 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008931 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008932 if (entries > IORING_MAX_ENTRIES) {
8933 if (!(p->flags & IORING_SETUP_CLAMP))
8934 return -EINVAL;
8935 entries = IORING_MAX_ENTRIES;
8936 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008937
8938 /*
8939 * Use twice as many entries for the CQ ring. It's possible for the
8940 * application to drive a higher depth than the size of the SQ ring,
8941 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008942 * some flexibility in overcommitting a bit. If the application has
8943 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8944 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008945 */
8946 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008947 if (p->flags & IORING_SETUP_CQSIZE) {
8948 /*
8949 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8950 * to a power-of-two, if it isn't already. We do NOT impose
8951 * any cq vs sq ring sizing.
8952 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008953 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008954 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008955 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8956 if (!(p->flags & IORING_SETUP_CLAMP))
8957 return -EINVAL;
8958 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8959 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008960 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8961 } else {
8962 p->cq_entries = 2 * p->sq_entries;
8963 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008964
8965 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008966 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008967
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008968 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008969 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008970 ring_pages(p->sq_entries, p->cq_entries));
8971 if (ret) {
8972 free_uid(user);
8973 return ret;
8974 }
8975 }
8976
8977 ctx = io_ring_ctx_alloc(p);
8978 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008979 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008980 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008981 p->cq_entries));
8982 free_uid(user);
8983 return -ENOMEM;
8984 }
8985 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008986 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008987 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008988
Jens Axboe2aede0e2020-09-14 10:45:53 -06008989 ctx->sqo_task = get_task_struct(current);
8990
8991 /*
8992 * This is just grabbed for accounting purposes. When a process exits,
8993 * the mm is exited and dropped before the files, hence we need to hang
8994 * on to this mm purely for the purposes of being able to unaccount
8995 * memory (locked/pinned vm). It's not used for anything else.
8996 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06008997 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008998 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06008999
Jens Axboef74441e2020-08-05 13:00:44 -06009000 /*
9001 * Account memory _before_ installing the file descriptor. Once
9002 * the descriptor is installed, it can get closed at any time. Also
9003 * do this before hitting the general error path, as ring freeing
9004 * will un-account as well.
9005 */
9006 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9007 ACCT_LOCKED);
9008 ctx->limit_mem = limit_mem;
9009
Jens Axboe2b188cc2019-01-07 10:46:33 -07009010 ret = io_allocate_scq_urings(ctx, p);
9011 if (ret)
9012 goto err;
9013
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009014 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009015 if (ret)
9016 goto err;
9017
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009018 if (!(p->flags & IORING_SETUP_R_DISABLED))
9019 io_sq_offload_start(ctx);
9020
Jens Axboe2b188cc2019-01-07 10:46:33 -07009021 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009022 p->sq_off.head = offsetof(struct io_rings, sq.head);
9023 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9024 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9025 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9026 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9027 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9028 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009029
9030 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009031 p->cq_off.head = offsetof(struct io_rings, cq.head);
9032 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9033 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9034 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9035 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9036 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009037 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009038
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009039 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9040 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009041 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9042 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009043
9044 if (copy_to_user(params, p, sizeof(*p))) {
9045 ret = -EFAULT;
9046 goto err;
9047 }
Jens Axboed1719f72020-07-30 13:43:53 -06009048
9049 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009050 * Install ring fd as the very last thing, so we don't risk someone
9051 * having closed it before we finish setup
9052 */
9053 ret = io_uring_get_fd(ctx);
9054 if (ret < 0)
9055 goto err;
9056
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009057 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009058 return ret;
9059err:
9060 io_ring_ctx_wait_and_kill(ctx);
9061 return ret;
9062}
9063
9064/*
9065 * Sets up an aio uring context, and returns the fd. Applications asks for a
9066 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9067 * params structure passed in.
9068 */
9069static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9070{
9071 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009072 int i;
9073
9074 if (copy_from_user(&p, params, sizeof(p)))
9075 return -EFAULT;
9076 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9077 if (p.resv[i])
9078 return -EINVAL;
9079 }
9080
Jens Axboe6c271ce2019-01-10 11:22:30 -07009081 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009082 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009083 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9084 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009085 return -EINVAL;
9086
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009087 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009088}
9089
9090SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9091 struct io_uring_params __user *, params)
9092{
9093 return io_uring_setup(entries, params);
9094}
9095
Jens Axboe66f4af92020-01-16 15:36:52 -07009096static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9097{
9098 struct io_uring_probe *p;
9099 size_t size;
9100 int i, ret;
9101
9102 size = struct_size(p, ops, nr_args);
9103 if (size == SIZE_MAX)
9104 return -EOVERFLOW;
9105 p = kzalloc(size, GFP_KERNEL);
9106 if (!p)
9107 return -ENOMEM;
9108
9109 ret = -EFAULT;
9110 if (copy_from_user(p, arg, size))
9111 goto out;
9112 ret = -EINVAL;
9113 if (memchr_inv(p, 0, size))
9114 goto out;
9115
9116 p->last_op = IORING_OP_LAST - 1;
9117 if (nr_args > IORING_OP_LAST)
9118 nr_args = IORING_OP_LAST;
9119
9120 for (i = 0; i < nr_args; i++) {
9121 p->ops[i].op = i;
9122 if (!io_op_defs[i].not_supported)
9123 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9124 }
9125 p->ops_len = i;
9126
9127 ret = 0;
9128 if (copy_to_user(arg, p, size))
9129 ret = -EFAULT;
9130out:
9131 kfree(p);
9132 return ret;
9133}
9134
Jens Axboe071698e2020-01-28 10:04:42 -07009135static int io_register_personality(struct io_ring_ctx *ctx)
9136{
9137 const struct cred *creds = get_current_cred();
9138 int id;
9139
9140 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9141 USHRT_MAX, GFP_KERNEL);
9142 if (id < 0)
9143 put_cred(creds);
9144 return id;
9145}
9146
9147static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9148{
9149 const struct cred *old_creds;
9150
9151 old_creds = idr_remove(&ctx->personality_idr, id);
9152 if (old_creds) {
9153 put_cred(old_creds);
9154 return 0;
9155 }
9156
9157 return -EINVAL;
9158}
9159
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009160static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9161 unsigned int nr_args)
9162{
9163 struct io_uring_restriction *res;
9164 size_t size;
9165 int i, ret;
9166
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009167 /* Restrictions allowed only if rings started disabled */
9168 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9169 return -EBADFD;
9170
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009171 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009172 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009173 return -EBUSY;
9174
9175 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9176 return -EINVAL;
9177
9178 size = array_size(nr_args, sizeof(*res));
9179 if (size == SIZE_MAX)
9180 return -EOVERFLOW;
9181
9182 res = memdup_user(arg, size);
9183 if (IS_ERR(res))
9184 return PTR_ERR(res);
9185
9186 ret = 0;
9187
9188 for (i = 0; i < nr_args; i++) {
9189 switch (res[i].opcode) {
9190 case IORING_RESTRICTION_REGISTER_OP:
9191 if (res[i].register_op >= IORING_REGISTER_LAST) {
9192 ret = -EINVAL;
9193 goto out;
9194 }
9195
9196 __set_bit(res[i].register_op,
9197 ctx->restrictions.register_op);
9198 break;
9199 case IORING_RESTRICTION_SQE_OP:
9200 if (res[i].sqe_op >= IORING_OP_LAST) {
9201 ret = -EINVAL;
9202 goto out;
9203 }
9204
9205 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9206 break;
9207 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9208 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9209 break;
9210 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9211 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9212 break;
9213 default:
9214 ret = -EINVAL;
9215 goto out;
9216 }
9217 }
9218
9219out:
9220 /* Reset all restrictions if an error happened */
9221 if (ret != 0)
9222 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9223 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009224 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009225
9226 kfree(res);
9227 return ret;
9228}
9229
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009230static int io_register_enable_rings(struct io_ring_ctx *ctx)
9231{
9232 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9233 return -EBADFD;
9234
9235 if (ctx->restrictions.registered)
9236 ctx->restricted = 1;
9237
9238 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9239
9240 io_sq_offload_start(ctx);
9241
9242 return 0;
9243}
9244
Jens Axboe071698e2020-01-28 10:04:42 -07009245static bool io_register_op_must_quiesce(int op)
9246{
9247 switch (op) {
9248 case IORING_UNREGISTER_FILES:
9249 case IORING_REGISTER_FILES_UPDATE:
9250 case IORING_REGISTER_PROBE:
9251 case IORING_REGISTER_PERSONALITY:
9252 case IORING_UNREGISTER_PERSONALITY:
9253 return false;
9254 default:
9255 return true;
9256 }
9257}
9258
Jens Axboeedafcce2019-01-09 09:16:05 -07009259static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9260 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009261 __releases(ctx->uring_lock)
9262 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009263{
9264 int ret;
9265
Jens Axboe35fa71a2019-04-22 10:23:23 -06009266 /*
9267 * We're inside the ring mutex, if the ref is already dying, then
9268 * someone else killed the ctx or is already going through
9269 * io_uring_register().
9270 */
9271 if (percpu_ref_is_dying(&ctx->refs))
9272 return -ENXIO;
9273
Jens Axboe071698e2020-01-28 10:04:42 -07009274 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009275 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009276
Jens Axboe05f3fb32019-12-09 11:22:50 -07009277 /*
9278 * Drop uring mutex before waiting for references to exit. If
9279 * another thread is currently inside io_uring_enter() it might
9280 * need to grab the uring_lock to make progress. If we hold it
9281 * here across the drain wait, then we can deadlock. It's safe
9282 * to drop the mutex here, since no new references will come in
9283 * after we've killed the percpu ref.
9284 */
9285 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009286 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009287 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009288 if (ret) {
9289 percpu_ref_resurrect(&ctx->refs);
9290 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009291 goto out_quiesce;
9292 }
9293 }
9294
9295 if (ctx->restricted) {
9296 if (opcode >= IORING_REGISTER_LAST) {
9297 ret = -EINVAL;
9298 goto out;
9299 }
9300
9301 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9302 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009303 goto out;
9304 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009305 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009306
9307 switch (opcode) {
9308 case IORING_REGISTER_BUFFERS:
9309 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9310 break;
9311 case IORING_UNREGISTER_BUFFERS:
9312 ret = -EINVAL;
9313 if (arg || nr_args)
9314 break;
9315 ret = io_sqe_buffer_unregister(ctx);
9316 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009317 case IORING_REGISTER_FILES:
9318 ret = io_sqe_files_register(ctx, arg, nr_args);
9319 break;
9320 case IORING_UNREGISTER_FILES:
9321 ret = -EINVAL;
9322 if (arg || nr_args)
9323 break;
9324 ret = io_sqe_files_unregister(ctx);
9325 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009326 case IORING_REGISTER_FILES_UPDATE:
9327 ret = io_sqe_files_update(ctx, arg, nr_args);
9328 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009329 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009330 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009331 ret = -EINVAL;
9332 if (nr_args != 1)
9333 break;
9334 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009335 if (ret)
9336 break;
9337 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9338 ctx->eventfd_async = 1;
9339 else
9340 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009341 break;
9342 case IORING_UNREGISTER_EVENTFD:
9343 ret = -EINVAL;
9344 if (arg || nr_args)
9345 break;
9346 ret = io_eventfd_unregister(ctx);
9347 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009348 case IORING_REGISTER_PROBE:
9349 ret = -EINVAL;
9350 if (!arg || nr_args > 256)
9351 break;
9352 ret = io_probe(ctx, arg, nr_args);
9353 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009354 case IORING_REGISTER_PERSONALITY:
9355 ret = -EINVAL;
9356 if (arg || nr_args)
9357 break;
9358 ret = io_register_personality(ctx);
9359 break;
9360 case IORING_UNREGISTER_PERSONALITY:
9361 ret = -EINVAL;
9362 if (arg)
9363 break;
9364 ret = io_unregister_personality(ctx, nr_args);
9365 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009366 case IORING_REGISTER_ENABLE_RINGS:
9367 ret = -EINVAL;
9368 if (arg || nr_args)
9369 break;
9370 ret = io_register_enable_rings(ctx);
9371 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009372 case IORING_REGISTER_RESTRICTIONS:
9373 ret = io_register_restrictions(ctx, arg, nr_args);
9374 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009375 default:
9376 ret = -EINVAL;
9377 break;
9378 }
9379
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009380out:
Jens Axboe071698e2020-01-28 10:04:42 -07009381 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009382 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009383 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009384out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009385 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009386 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009387 return ret;
9388}
9389
9390SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9391 void __user *, arg, unsigned int, nr_args)
9392{
9393 struct io_ring_ctx *ctx;
9394 long ret = -EBADF;
9395 struct fd f;
9396
9397 f = fdget(fd);
9398 if (!f.file)
9399 return -EBADF;
9400
9401 ret = -EOPNOTSUPP;
9402 if (f.file->f_op != &io_uring_fops)
9403 goto out_fput;
9404
9405 ctx = f.file->private_data;
9406
9407 mutex_lock(&ctx->uring_lock);
9408 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9409 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009410 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9411 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009412out_fput:
9413 fdput(f);
9414 return ret;
9415}
9416
Jens Axboe2b188cc2019-01-07 10:46:33 -07009417static int __init io_uring_init(void)
9418{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009419#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9420 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9421 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9422} while (0)
9423
9424#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9425 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9426 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9427 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9428 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9429 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9430 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9431 BUILD_BUG_SQE_ELEM(8, __u64, off);
9432 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9433 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009434 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009435 BUILD_BUG_SQE_ELEM(24, __u32, len);
9436 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9437 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9438 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9439 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009440 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9441 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009442 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9443 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9444 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9445 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9446 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9447 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9448 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9449 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009450 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009451 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9452 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9453 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009454 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009455
Jens Axboed3656342019-12-18 09:50:26 -07009456 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009457 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009458 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9459 return 0;
9460};
9461__initcall(io_uring_init);