blob: 0680fa385353f525b821eb667e67ea956bc519d6 [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>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020085#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
Jens Axboe2b188cc2019-01-07 10:46:33 -070088#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060091#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
Daniel Xu5277dea2019-09-14 14:23:45 -070093#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060094#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060095
96/*
97 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
98 */
99#define IORING_FILE_TABLE_SHIFT 9
100#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
101#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
102#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200103#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
104 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700105
106struct io_uring {
107 u32 head ____cacheline_aligned_in_smp;
108 u32 tail ____cacheline_aligned_in_smp;
109};
110
Stefan Bühler1e84b972019-04-24 23:54:16 +0200111/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000112 * This data is shared with the application through the mmap at offsets
113 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 *
115 * The offsets to the member fields are published through struct
116 * io_sqring_offsets when calling io_uring_setup.
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 /*
120 * Head and tail offsets into the ring; the offsets need to be
121 * masked to get valid indices.
122 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 * The kernel controls head of the sq ring and the tail of the cq ring,
124 * and the application controls tail of the sq ring and the head of the
125 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000129 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 * ring_entries - 1)
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 u32 sq_ring_mask, cq_ring_mask;
133 /* Ring sizes (constant, power of 2) */
134 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Number of invalid entries dropped by the kernel due to
137 * invalid index stored in array
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * After a new SQ head value was read by the application this
144 * counter includes all submissions that were dropped reaching
145 * the new SQ head (and possibly more).
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200149 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application.
153 *
154 * The application needs a full memory barrier before checking
155 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200159 * Runtime CQ flags
160 *
161 * Written by the application, shouldn't be modified by the
162 * kernel.
163 */
164 u32 cq_flags;
165 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200166 * Number of completion events lost because the queue was full;
167 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800168 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200169 * the completion queue.
170 *
171 * Written by the kernel, shouldn't be modified by the
172 * application (i.e. get number of "new events" by comparing to
173 * cached value).
174 *
175 * As completion events come in out of order this counter is not
176 * ordered with any other data.
177 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000178 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200179 /*
180 * Ring buffer of completion events.
181 *
182 * The kernel writes completion events fresh every time they are
183 * produced, so the application is allowed to modify pending
184 * entries.
185 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000186 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700187};
188
Jens Axboeedafcce2019-01-09 09:16:05 -0700189struct io_mapped_ubuf {
190 u64 ubuf;
191 size_t len;
192 struct bio_vec *bvec;
193 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600194 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700195};
196
Jens Axboe65e19f52019-10-26 07:20:21 -0600197struct fixed_file_table {
198 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700199};
200
Xiaoguang Wang05589552020-03-31 14:05:18 +0800201struct fixed_file_ref_node {
202 struct percpu_ref refs;
203 struct list_head node;
204 struct list_head file_list;
205 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600206 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800207};
208
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209struct fixed_file_data {
210 struct fixed_file_table *table;
211 struct io_ring_ctx *ctx;
212
Pavel Begunkovb2e96852020-10-10 18:34:16 +0100213 struct fixed_file_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700214 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700215 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800216 struct list_head ref_list;
217 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700218};
219
Jens Axboe5a2e7452020-02-23 16:23:11 -0700220struct io_buffer {
221 struct list_head list;
222 __u64 addr;
223 __s32 len;
224 __u16 bid;
225};
226
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200227struct io_restriction {
228 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
229 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
230 u8 sqe_flags_allowed;
231 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200232 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200233};
234
Jens Axboe534ca6d2020-09-02 13:52:19 -0600235struct io_sq_data {
236 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600237 struct mutex lock;
238
239 /* ctx's that are using this sqd */
240 struct list_head ctx_list;
241 struct list_head ctx_new_list;
242 struct mutex ctx_lock;
243
Jens Axboe534ca6d2020-09-02 13:52:19 -0600244 struct task_struct *thread;
245 struct wait_queue_head wait;
246};
247
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248struct io_ring_ctx {
249 struct {
250 struct percpu_ref refs;
251 } ____cacheline_aligned_in_smp;
252
253 struct {
254 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800255 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700256 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800257 unsigned int cq_overflow_flushed: 1;
258 unsigned int drain_next: 1;
259 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200260 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261
Hristo Venev75b28af2019-08-26 17:23:46 +0000262 /*
263 * Ring buffer of indices into array of io_uring_sqe, which is
264 * mmapped by the application using the IORING_OFF_SQES offset.
265 *
266 * This indirection could e.g. be used to assign fixed
267 * io_uring_sqe entries to operations and only submit them to
268 * the queue when needed.
269 *
270 * The kernel modifies neither the indices array nor the entries
271 * array.
272 */
273 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700274 unsigned cached_sq_head;
275 unsigned sq_entries;
276 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700277 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600278 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700280 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600281
282 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600283 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700284 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285
Jens Axboefcb323c2019-10-24 12:39:47 -0600286 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700287 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700288 } ____cacheline_aligned_in_smp;
289
Hristo Venev75b28af2019-08-26 17:23:46 +0000290 struct io_rings *rings;
291
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600293 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600294
295 /*
296 * For SQPOLL usage - we hold a reference to the parent task, so we
297 * have access to the ->files
298 */
299 struct task_struct *sqo_task;
300
301 /* Only used for accounting purposes */
302 struct mm_struct *mm_account;
303
Dennis Zhou91d8f512020-09-16 13:41:05 -0700304#ifdef CONFIG_BLK_CGROUP
305 struct cgroup_subsys_state *sqo_blkcg_css;
306#endif
307
Jens Axboe534ca6d2020-09-02 13:52:19 -0600308 struct io_sq_data *sq_data; /* if using sq thread polling */
309
Jens Axboe90554202020-09-03 12:12:41 -0600310 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600311 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600312 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
Jens Axboe6b063142019-01-10 22:13:58 -0700314 /*
315 * If used, fixed file set. Writers must ensure that ->refs is dead,
316 * readers must ensure that ->refs is alive as long as the file* is
317 * used. Only updated through io_uring_register(2).
318 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700319 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700320 unsigned nr_user_files;
321
Jens Axboeedafcce2019-01-09 09:16:05 -0700322 /* if used, fixed mapped user buffers */
323 unsigned nr_user_bufs;
324 struct io_mapped_ubuf *user_bufs;
325
Jens Axboe2b188cc2019-01-07 10:46:33 -0700326 struct user_struct *user;
327
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700328 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700329
Jens Axboe0f158b42020-05-14 17:18:39 -0600330 struct completion ref_comp;
331 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700332
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700333 /* if all else fails... */
334 struct io_kiocb *fallback_req;
335
Jens Axboe206aefd2019-11-07 18:27:42 -0700336#if defined(CONFIG_UNIX)
337 struct socket *ring_sock;
338#endif
339
Jens Axboe5a2e7452020-02-23 16:23:11 -0700340 struct idr io_buffer_idr;
341
Jens Axboe071698e2020-01-28 10:04:42 -0700342 struct idr personality_idr;
343
Jens Axboe206aefd2019-11-07 18:27:42 -0700344 struct {
345 unsigned cached_cq_tail;
346 unsigned cq_entries;
347 unsigned cq_mask;
348 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700349 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700350 struct wait_queue_head cq_wait;
351 struct fasync_struct *cq_fasync;
352 struct eventfd_ctx *cq_ev_fd;
353 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700354
355 struct {
356 struct mutex uring_lock;
357 wait_queue_head_t wait;
358 } ____cacheline_aligned_in_smp;
359
360 struct {
361 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700362
Jens Axboedef596e2019-01-09 08:59:42 -0700363 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300364 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700365 * io_uring instances that don't use IORING_SETUP_SQPOLL.
366 * For SQPOLL, only the single threaded io_sq_thread() will
367 * manipulate the list, hence no extra locking is needed there.
368 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300369 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700370 struct hlist_head *cancel_hash;
371 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700372 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600373
374 spinlock_t inflight_lock;
375 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600377
Jens Axboe4a38aed22020-05-14 17:21:15 -0600378 struct delayed_work file_put_work;
379 struct llist_head file_put_llist;
380
Jens Axboe85faa7b2020-04-09 18:14:00 -0600381 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200382 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383};
384
Jens Axboe09bb8392019-03-13 12:39:28 -0600385/*
386 * First field must be the file pointer in all the
387 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
388 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700389struct io_poll_iocb {
390 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700391 union {
392 struct wait_queue_head *head;
393 u64 addr;
394 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700395 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600396 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700397 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700398 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700399};
400
Jens Axboeb5dba592019-12-11 14:02:38 -0700401struct io_close {
402 struct file *file;
403 struct file *put_file;
404 int fd;
405};
406
Jens Axboead8a48a2019-11-15 08:49:11 -0700407struct io_timeout_data {
408 struct io_kiocb *req;
409 struct hrtimer timer;
410 struct timespec64 ts;
411 enum hrtimer_mode mode;
412};
413
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700414struct io_accept {
415 struct file *file;
416 struct sockaddr __user *addr;
417 int __user *addr_len;
418 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600419 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700420};
421
422struct io_sync {
423 struct file *file;
424 loff_t len;
425 loff_t off;
426 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700427 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700428};
429
Jens Axboefbf23842019-12-17 18:45:56 -0700430struct io_cancel {
431 struct file *file;
432 u64 addr;
433};
434
Jens Axboeb29472e2019-12-17 18:50:29 -0700435struct io_timeout {
436 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300437 u32 off;
438 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300439 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700440};
441
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100442struct io_timeout_rem {
443 struct file *file;
444 u64 addr;
445};
446
Jens Axboe9adbd452019-12-20 08:45:55 -0700447struct io_rw {
448 /* NOTE: kiocb has the file as the first member, so don't do it here */
449 struct kiocb kiocb;
450 u64 addr;
451 u64 len;
452};
453
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700454struct io_connect {
455 struct file *file;
456 struct sockaddr __user *addr;
457 int addr_len;
458};
459
Jens Axboee47293f2019-12-20 08:58:21 -0700460struct io_sr_msg {
461 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700462 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300463 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700464 void __user *buf;
465 };
Jens Axboee47293f2019-12-20 08:58:21 -0700466 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700467 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700468 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700469 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700470};
471
Jens Axboe15b71ab2019-12-11 11:20:36 -0700472struct io_open {
473 struct file *file;
474 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700475 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700476 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600477 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700478};
479
Jens Axboe05f3fb32019-12-09 11:22:50 -0700480struct io_files_update {
481 struct file *file;
482 u64 arg;
483 u32 nr_args;
484 u32 offset;
485};
486
Jens Axboe4840e412019-12-25 22:03:45 -0700487struct io_fadvise {
488 struct file *file;
489 u64 offset;
490 u32 len;
491 u32 advice;
492};
493
Jens Axboec1ca7572019-12-25 22:18:28 -0700494struct io_madvise {
495 struct file *file;
496 u64 addr;
497 u32 len;
498 u32 advice;
499};
500
Jens Axboe3e4827b2020-01-08 15:18:09 -0700501struct io_epoll {
502 struct file *file;
503 int epfd;
504 int op;
505 int fd;
506 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700507};
508
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300509struct io_splice {
510 struct file *file_out;
511 struct file *file_in;
512 loff_t off_out;
513 loff_t off_in;
514 u64 len;
515 unsigned int flags;
516};
517
Jens Axboeddf0322d2020-02-23 16:41:33 -0700518struct io_provide_buf {
519 struct file *file;
520 __u64 addr;
521 __s32 len;
522 __u32 bgid;
523 __u16 nbufs;
524 __u16 bid;
525};
526
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700527struct io_statx {
528 struct file *file;
529 int dfd;
530 unsigned int mask;
531 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700532 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700533 struct statx __user *buffer;
534};
535
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300536struct io_completion {
537 struct file *file;
538 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300539 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300540};
541
Jens Axboef499a022019-12-02 16:28:46 -0700542struct io_async_connect {
543 struct sockaddr_storage address;
544};
545
Jens Axboe03b12302019-12-02 18:50:25 -0700546struct io_async_msghdr {
547 struct iovec fast_iov[UIO_FASTIOV];
548 struct iovec *iov;
549 struct sockaddr __user *uaddr;
550 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700551 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700552};
553
Jens Axboef67676d2019-12-02 11:03:47 -0700554struct io_async_rw {
555 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600556 const struct iovec *free_iovec;
557 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600558 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600559 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700560};
561
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562enum {
563 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
564 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
565 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
566 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
567 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700568 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300569
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300570 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300571 REQ_F_FAIL_LINK_BIT,
572 REQ_F_INFLIGHT_BIT,
573 REQ_F_CUR_POS_BIT,
574 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300576 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300578 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700579 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700580 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600581 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800582 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700583
584 /* not a real bit, just to check we're not overflowing the space */
585 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300586};
587
588enum {
589 /* ctx owns file */
590 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
591 /* drain existing IO first */
592 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
593 /* linked sqes */
594 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
595 /* doesn't sever on completion < 0 */
596 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
597 /* IOSQE_ASYNC */
598 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700599 /* IOSQE_BUFFER_SELECT */
600 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300601
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300602 /* head of a link */
603 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300604 /* fail rest of links */
605 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
606 /* on inflight list */
607 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
608 /* read/write uses file position */
609 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
610 /* must not punt to workers */
611 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300612 /* has linked timeout */
613 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300614 /* regular file */
615 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300616 /* completion under lock */
617 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300618 /* needs cleanup */
619 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700620 /* already went through poll handler */
621 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700622 /* buffer already selected */
623 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600624 /* doesn't need file table for this request */
625 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800626 /* io_wq_work is initialized */
627 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700628};
629
630struct async_poll {
631 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600632 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300633};
634
Jens Axboe09bb8392019-03-13 12:39:28 -0600635/*
636 * NOTE! Each of the iocb union members has the file pointer
637 * as the first entry in their struct definition. So you can
638 * access the file pointer through any of the sub-structs,
639 * or directly as just 'ki_filp' in this struct.
640 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700642 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600643 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700644 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700645 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700646 struct io_accept accept;
647 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700648 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700649 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100650 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700651 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700652 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700653 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700654 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700655 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700656 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700657 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700658 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300659 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700660 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700661 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300662 /* use only after cleaning per-op data, see io_clean_op() */
663 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700664 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700665
Jens Axboee8c2bc12020-08-15 18:44:09 -0700666 /* opcode allocated if it needs to store data for async defer */
667 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700668 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800669 /* polled IO has completed */
670 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700671
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700672 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300673 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700674
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300675 struct io_ring_ctx *ctx;
676 unsigned int flags;
677 refcount_t refs;
678 struct task_struct *task;
679 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700680
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300681 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700682
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300683 /*
684 * 1. used with ctx->iopoll_list with reads/writes
685 * 2. to track reqs with ->files (see io_op_def::file_table)
686 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300687 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600688
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300689 struct percpu_ref *fixed_file_refs;
690 struct callback_head task_work;
691 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
692 struct hlist_node hash_node;
693 struct async_poll *apoll;
694 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700695};
696
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300697struct io_defer_entry {
698 struct list_head list;
699 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300700 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300701};
702
Jens Axboedef596e2019-01-09 08:59:42 -0700703#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700704
Jens Axboe013538b2020-06-22 09:29:15 -0600705struct io_comp_state {
706 unsigned int nr;
707 struct list_head list;
708 struct io_ring_ctx *ctx;
709};
710
Jens Axboe9a56a232019-01-09 09:06:50 -0700711struct io_submit_state {
712 struct blk_plug plug;
713
714 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700715 * io_kiocb alloc cache
716 */
717 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300718 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700719
720 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600721 * Batch completion logic
722 */
723 struct io_comp_state comp;
724
725 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700726 * File reference cache
727 */
728 struct file *file;
729 unsigned int fd;
730 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700731 unsigned int ios_left;
732};
733
Jens Axboed3656342019-12-18 09:50:26 -0700734struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700735 /* needs current->mm setup, does mm access */
736 unsigned needs_mm : 1;
737 /* needs req->file assigned */
738 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600739 /* don't fail if file grab fails */
740 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700741 /* hash wq insertion if file is a regular file */
742 unsigned hash_reg_file : 1;
743 /* unbound wq insertion if file is a non-regular file */
744 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700745 /* opcode is not supported by this kernel */
746 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700747 /* needs file table */
748 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700749 /* needs ->fs */
750 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700751 /* set if opcode supports polled "wait" */
752 unsigned pollin : 1;
753 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700754 /* op supports buffer selection */
755 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700756 /* needs rlimit(RLIMIT_FSIZE) assigned */
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300757 unsigned needs_fsize : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700758 /* must always have async data allocated */
759 unsigned needs_async_data : 1;
Dennis Zhou91d8f512020-09-16 13:41:05 -0700760 /* needs blkcg context, issues async io potentially */
761 unsigned needs_blkcg : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700762 /* size of async data needed, if any */
763 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700764};
765
Jens Axboe738277a2020-09-03 05:54:56 -0600766static const struct io_op_def io_op_defs[] __read_mostly = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_NOP] = {},
768 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700769 .needs_mm = 1,
770 .needs_file = 1,
771 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700772 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700773 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700774 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700775 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700776 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700779 .needs_mm = 1,
780 .needs_file = 1,
781 .hash_reg_file = 1,
782 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700783 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300784 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700785 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700786 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700787 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700790 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700791 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700792 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300793 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700794 .needs_file = 1,
795 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700796 .pollin = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700797 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700798 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700799 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300800 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700801 .needs_file = 1,
802 .hash_reg_file = 1,
803 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700804 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300805 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700806 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700807 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .needs_file = 1,
811 .unbound_nonreg_file = 1,
812 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300813 [IORING_OP_POLL_REMOVE] = {},
814 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700815 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700816 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .needs_mm = 1,
820 .needs_file = 1,
821 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700822 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700823 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700824 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700825 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700826 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700829 .needs_mm = 1,
830 .needs_file = 1,
831 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700832 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700833 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700834 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700835 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700836 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700837 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700838 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300839 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700840 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700841 .needs_async_data = 1,
842 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_TIMEOUT_REMOVE] = {},
845 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700846 .needs_mm = 1,
847 .needs_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700849 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700850 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_ASYNC_CANCEL] = {},
853 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700854 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700855 .needs_async_data = 1,
856 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700857 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300858 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700859 .needs_mm = 1,
860 .needs_file = 1,
861 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700862 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700863 .needs_async_data = 1,
864 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700867 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300868 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700869 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700872 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700873 .needs_fs = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700874 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700875 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300876 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600877 .needs_file = 1,
878 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700879 .file_table = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700880 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700883 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700884 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700885 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300886 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700887 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700888 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600889 .file_table = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700890 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700891 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300892 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700893 .needs_mm = 1,
894 .needs_file = 1,
895 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700896 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700897 .buffer_select = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700898 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700899 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700900 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300901 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700902 .needs_mm = 1,
903 .needs_file = 1,
904 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700905 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300906 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700907 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700908 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700909 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300910 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700911 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700912 .needs_blkcg = 1,
Jens Axboe4840e412019-12-25 22:03:45 -0700913 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300914 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700915 .needs_mm = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700916 .needs_blkcg = 1,
Jens Axboec1ca7572019-12-25 22:18:28 -0700917 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300918 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700919 .needs_mm = 1,
920 .needs_file = 1,
921 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700922 .pollout = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700923 .needs_blkcg = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700924 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300925 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700926 .needs_mm = 1,
927 .needs_file = 1,
928 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700929 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700930 .buffer_select = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700931 .needs_blkcg = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700932 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300933 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700934 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700935 .needs_fs = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700936 .needs_blkcg = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700937 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700938 [IORING_OP_EPOLL_CTL] = {
939 .unbound_nonreg_file = 1,
940 .file_table = 1,
941 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300942 [IORING_OP_SPLICE] = {
943 .needs_file = 1,
944 .hash_reg_file = 1,
945 .unbound_nonreg_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700946 .needs_blkcg = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700947 },
948 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700949 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300950 [IORING_OP_TEE] = {
951 .needs_file = 1,
952 .hash_reg_file = 1,
953 .unbound_nonreg_file = 1,
954 },
Jens Axboed3656342019-12-18 09:50:26 -0700955};
956
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700957enum io_mem_account {
958 ACCT_LOCKED,
959 ACCT_PINNED,
960};
961
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300962static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
963 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700964static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800965static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600966static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700967static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700968static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600969static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700970static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700971static int __io_sqe_files_update(struct io_ring_ctx *ctx,
972 struct io_uring_files_update *ip,
973 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300974static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +0100975static struct file *io_file_get(struct io_submit_state *state,
976 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +0300977static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600978static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600979
Jens Axboeb63534c2020-06-04 11:28:00 -0600980static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
981 struct iovec **iovec, struct iov_iter *iter,
982 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600983static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
984 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600985 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700986
987static struct kmem_cache *req_cachep;
988
Jens Axboe738277a2020-09-03 05:54:56 -0600989static const struct file_operations io_uring_fops __read_mostly;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700990
991struct sock *io_uring_get_socket(struct file *file)
992{
993#if defined(CONFIG_UNIX)
994 if (file->f_op == &io_uring_fops) {
995 struct io_ring_ctx *ctx = file->private_data;
996
997 return ctx->ring_sock->sk;
998 }
999#endif
1000 return NULL;
1001}
1002EXPORT_SYMBOL(io_uring_get_socket);
1003
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001004static inline void io_clean_op(struct io_kiocb *req)
1005{
Pavel Begunkovbb175342020-08-20 11:33:35 +03001006 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
1007 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001008 __io_clean_op(req);
1009}
1010
Jens Axboe4349f302020-07-09 15:07:01 -06001011static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001012{
1013 struct mm_struct *mm = current->mm;
1014
1015 if (mm) {
1016 kthread_unuse_mm(mm);
1017 mmput(mm);
1018 }
1019}
1020
1021static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1022{
1023 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +03001024 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -06001025 !ctx->sqo_task->mm ||
1026 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -06001027 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -06001028 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -06001029 }
1030
1031 return 0;
1032}
1033
1034static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1035 struct io_kiocb *req)
1036{
1037 if (!io_op_defs[req->opcode].needs_mm)
1038 return 0;
1039 return __io_sq_thread_acquire_mm(ctx);
1040}
1041
Dennis Zhou91d8f512020-09-16 13:41:05 -07001042static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1043 struct cgroup_subsys_state **cur_css)
1044
1045{
1046#ifdef CONFIG_BLK_CGROUP
1047 /* puts the old one when swapping */
1048 if (*cur_css != ctx->sqo_blkcg_css) {
1049 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1050 *cur_css = ctx->sqo_blkcg_css;
1051 }
1052#endif
1053}
1054
1055static void io_sq_thread_unassociate_blkcg(void)
1056{
1057#ifdef CONFIG_BLK_CGROUP
1058 kthread_associate_blkcg(NULL);
1059#endif
1060}
1061
Jens Axboec40f6372020-06-25 15:39:59 -06001062static inline void req_set_fail_links(struct io_kiocb *req)
1063{
1064 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1065 req->flags |= REQ_F_FAIL_LINK;
1066}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001067
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001068/*
1069 * Note: must call io_req_init_async() for the first time you
1070 * touch any members of io_wq_work.
1071 */
1072static inline void io_req_init_async(struct io_kiocb *req)
1073{
1074 if (req->flags & REQ_F_WORK_INITIALIZED)
1075 return;
1076
1077 memset(&req->work, 0, sizeof(req->work));
1078 req->flags |= REQ_F_WORK_INITIALIZED;
1079}
1080
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001081static inline bool io_async_submit(struct io_ring_ctx *ctx)
1082{
1083 return ctx->flags & IORING_SETUP_SQPOLL;
1084}
1085
Jens Axboe2b188cc2019-01-07 10:46:33 -07001086static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1087{
1088 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1089
Jens Axboe0f158b42020-05-14 17:18:39 -06001090 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091}
1092
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001093static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1094{
1095 return !req->timeout.off;
1096}
1097
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1099{
1100 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001101 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102
1103 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1104 if (!ctx)
1105 return NULL;
1106
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001107 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1108 if (!ctx->fallback_req)
1109 goto err;
1110
Jens Axboe78076bb2019-12-04 19:56:40 -07001111 /*
1112 * Use 5 bits less than the max cq entries, that should give us around
1113 * 32 entries per hash list if totally full and uniformly spread.
1114 */
1115 hash_bits = ilog2(p->cq_entries);
1116 hash_bits -= 5;
1117 if (hash_bits <= 0)
1118 hash_bits = 1;
1119 ctx->cancel_hash_bits = hash_bits;
1120 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1121 GFP_KERNEL);
1122 if (!ctx->cancel_hash)
1123 goto err;
1124 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1125
Roman Gushchin21482892019-05-07 10:01:48 -07001126 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001127 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1128 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129
1130 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001131 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001132 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001134 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001135 init_completion(&ctx->ref_comp);
1136 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001137 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001138 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001139 mutex_init(&ctx->uring_lock);
1140 init_waitqueue_head(&ctx->wait);
1141 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001142 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001143 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001144 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001145 init_waitqueue_head(&ctx->inflight_wait);
1146 spin_lock_init(&ctx->inflight_lock);
1147 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001148 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1149 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001151err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001152 if (ctx->fallback_req)
1153 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001154 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001155 kfree(ctx);
1156 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157}
1158
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001159static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001160{
Jens Axboe2bc99302020-07-09 09:43:27 -06001161 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1162 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001163
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001164 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001165 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001166 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001167
Bob Liu9d858b22019-11-13 18:06:25 +08001168 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001169}
1170
Jens Axboede0617e2019-04-06 21:51:27 -06001171static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001172{
Hristo Venev75b28af2019-08-26 17:23:46 +00001173 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001174
Pavel Begunkov07910152020-01-17 03:52:46 +03001175 /* order cqe stores with ring update */
1176 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177
Pavel Begunkov07910152020-01-17 03:52:46 +03001178 if (wq_has_sleeper(&ctx->cq_wait)) {
1179 wake_up_interruptible(&ctx->cq_wait);
1180 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001181 }
1182}
1183
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001184static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001185{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001186 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001187 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001188
1189 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001190
Jens Axboecccf0ee2020-01-27 16:34:48 -07001191 if (req->work.mm) {
1192 mmdrop(req->work.mm);
1193 req->work.mm = NULL;
1194 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001195#ifdef CONFIG_BLK_CGROUP
1196 if (req->work.blkcg_css)
1197 css_put(req->work.blkcg_css);
1198#endif
Jens Axboecccf0ee2020-01-27 16:34:48 -07001199 if (req->work.creds) {
1200 put_cred(req->work.creds);
1201 req->work.creds = NULL;
1202 }
Jens Axboeff002b32020-02-07 16:05:21 -07001203 if (req->work.fs) {
1204 struct fs_struct *fs = req->work.fs;
1205
1206 spin_lock(&req->work.fs->lock);
1207 if (--fs->users)
1208 fs = NULL;
1209 spin_unlock(&req->work.fs->lock);
1210 if (fs)
1211 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001212 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001213 }
Jens Axboe561fb042019-10-24 07:25:42 -06001214}
1215
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001216static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001217{
Jens Axboed3656342019-12-18 09:50:26 -07001218 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001219 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001220
Pavel Begunkov16d59802020-07-12 16:16:47 +03001221 io_req_init_async(req);
1222
Jens Axboed3656342019-12-18 09:50:26 -07001223 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001224 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001225 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001226 } else {
1227 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001228 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001229 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001230 if (!req->work.files && io_op_defs[req->opcode].file_table &&
1231 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1232 req->work.files = get_files_struct(current);
1233 get_nsproxy(current->nsproxy);
1234 req->work.nsproxy = current->nsproxy;
1235 req->flags |= REQ_F_INFLIGHT;
1236
1237 spin_lock_irq(&ctx->inflight_lock);
1238 list_add(&req->inflight_entry, &ctx->inflight_list);
1239 spin_unlock_irq(&ctx->inflight_lock);
1240 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001241 if (!req->work.mm && def->needs_mm) {
1242 mmgrab(current->mm);
1243 req->work.mm = current->mm;
1244 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001245#ifdef CONFIG_BLK_CGROUP
1246 if (!req->work.blkcg_css && def->needs_blkcg) {
1247 rcu_read_lock();
1248 req->work.blkcg_css = blkcg_css();
1249 /*
1250 * This should be rare, either the cgroup is dying or the task
1251 * is moving cgroups. Just punt to root for the handful of ios.
1252 */
1253 if (!css_tryget_online(req->work.blkcg_css))
1254 req->work.blkcg_css = NULL;
1255 rcu_read_unlock();
1256 }
1257#endif
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001258 if (!req->work.creds)
1259 req->work.creds = get_current_cred();
1260 if (!req->work.fs && def->needs_fs) {
1261 spin_lock(&current->fs->lock);
1262 if (!current->fs->in_exec) {
1263 req->work.fs = current->fs;
1264 req->work.fs->users++;
1265 } else {
1266 req->work.flags |= IO_WQ_WORK_CANCEL;
1267 }
1268 spin_unlock(&current->fs->lock);
1269 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001270 if (def->needs_fsize)
1271 req->work.fsize = rlimit(RLIMIT_FSIZE);
1272 else
1273 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001274}
1275
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001276static void io_prep_async_link(struct io_kiocb *req)
1277{
1278 struct io_kiocb *cur;
1279
1280 io_prep_async_work(req);
1281 if (req->flags & REQ_F_LINK_HEAD)
1282 list_for_each_entry(cur, &req->link_list, link_list)
1283 io_prep_async_work(cur);
1284}
1285
Jens Axboe7271ef32020-08-10 09:55:22 -06001286static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001287{
Jackie Liua197f662019-11-08 08:09:12 -07001288 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001289 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001290
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001291 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1292 &req->work, req->flags);
1293 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001294 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001295}
1296
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001297static void io_queue_async_work(struct io_kiocb *req)
1298{
Jens Axboe7271ef32020-08-10 09:55:22 -06001299 struct io_kiocb *link;
1300
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001301 /* init ->work of the whole link before punting */
1302 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001303 link = __io_queue_async_work(req);
1304
1305 if (link)
1306 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001307}
1308
Jens Axboe5262f562019-09-17 12:26:57 -06001309static void io_kill_timeout(struct io_kiocb *req)
1310{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001311 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001312 int ret;
1313
Jens Axboee8c2bc12020-08-15 18:44:09 -07001314 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001315 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001316 atomic_set(&req->ctx->cq_timeouts,
1317 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001318 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001319 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001320 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001321 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001322 }
1323}
1324
Jens Axboef3606e32020-09-22 08:18:24 -06001325static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1326{
1327 struct io_ring_ctx *ctx = req->ctx;
1328
1329 if (!tsk || req->task == tsk)
1330 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001331 if (ctx->flags & IORING_SETUP_SQPOLL) {
1332 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1333 return true;
1334 }
Jens Axboef3606e32020-09-22 08:18:24 -06001335 return false;
1336}
1337
Jens Axboe76e1b642020-09-26 15:05:03 -06001338/*
1339 * Returns true if we found and killed one or more timeouts
1340 */
1341static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001342{
1343 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001344 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001345
1346 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001347 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001348 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001349 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001350 canceled++;
1351 }
Jens Axboef3606e32020-09-22 08:18:24 -06001352 }
Jens Axboe5262f562019-09-17 12:26:57 -06001353 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001354 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001355}
1356
Pavel Begunkov04518942020-05-26 20:34:05 +03001357static void __io_queue_deferred(struct io_ring_ctx *ctx)
1358{
1359 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001360 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1361 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001362 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001363
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001364 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001365 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001366 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001367 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001368 link = __io_queue_async_work(de->req);
1369 if (link) {
1370 __io_queue_linked_timeout(link);
1371 /* drop submission reference */
1372 link->flags |= REQ_F_COMP_LOCKED;
1373 io_put_req(link);
1374 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001375 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001376 } while (!list_empty(&ctx->defer_list));
1377}
1378
Pavel Begunkov360428f2020-05-30 14:54:17 +03001379static void io_flush_timeouts(struct io_ring_ctx *ctx)
1380{
1381 while (!list_empty(&ctx->timeout_list)) {
1382 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001383 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001384
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001385 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001386 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001387 if (req->timeout.target_seq != ctx->cached_cq_tail
1388 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001389 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001390
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001391 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001392 io_kill_timeout(req);
1393 }
1394}
1395
Jens Axboede0617e2019-04-06 21:51:27 -06001396static void io_commit_cqring(struct io_ring_ctx *ctx)
1397{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001398 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001399 __io_commit_cqring(ctx);
1400
Pavel Begunkov04518942020-05-26 20:34:05 +03001401 if (unlikely(!list_empty(&ctx->defer_list)))
1402 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001403}
1404
Jens Axboe90554202020-09-03 12:12:41 -06001405static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1406{
1407 struct io_rings *r = ctx->rings;
1408
1409 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1410}
1411
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1413{
Hristo Venev75b28af2019-08-26 17:23:46 +00001414 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415 unsigned tail;
1416
1417 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001418 /*
1419 * writes to the cq entry need to come after reading head; the
1420 * control dependency is enough as we're using WRITE_ONCE to
1421 * fill the cq entry
1422 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001423 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424 return NULL;
1425
1426 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001427 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001428}
1429
Jens Axboef2842ab2020-01-08 11:04:00 -07001430static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1431{
Jens Axboef0b493e2020-02-01 21:30:11 -07001432 if (!ctx->cq_ev_fd)
1433 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001434 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1435 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001436 if (!ctx->eventfd_async)
1437 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001438 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001439}
1440
Jens Axboeb41e9852020-02-17 09:52:41 -07001441static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001442{
1443 if (waitqueue_active(&ctx->wait))
1444 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001445 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1446 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001447 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001448 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001449}
1450
Pavel Begunkov46930142020-07-30 18:43:49 +03001451static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1452{
1453 if (list_empty(&ctx->cq_overflow_list)) {
1454 clear_bit(0, &ctx->sq_check_overflow);
1455 clear_bit(0, &ctx->cq_check_overflow);
1456 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1457 }
1458}
1459
Jens Axboee6c8aa92020-09-28 13:10:13 -06001460static inline bool io_match_files(struct io_kiocb *req,
1461 struct files_struct *files)
1462{
1463 if (!files)
1464 return true;
1465 if (req->flags & REQ_F_WORK_INITIALIZED)
1466 return req->work.files == files;
1467 return false;
1468}
1469
Jens Axboec4a2ed72019-11-21 21:01:26 -07001470/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001471static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1472 struct task_struct *tsk,
1473 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001474{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001475 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001476 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001477 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001478 unsigned long flags;
1479 LIST_HEAD(list);
1480
1481 if (!force) {
1482 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001483 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001484 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1485 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001486 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001487 }
1488
1489 spin_lock_irqsave(&ctx->completion_lock, flags);
1490
1491 /* if force is set, the ring is going away. always drop after that */
1492 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001493 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001494
Jens Axboec4a2ed72019-11-21 21:01:26 -07001495 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001496 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1497 if (tsk && req->task != tsk)
1498 continue;
1499 if (!io_match_files(req, files))
1500 continue;
1501
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001502 cqe = io_get_cqring(ctx);
1503 if (!cqe && !force)
1504 break;
1505
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001506 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001507 if (cqe) {
1508 WRITE_ONCE(cqe->user_data, req->user_data);
1509 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001510 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001511 } else {
1512 WRITE_ONCE(ctx->rings->cq_overflow,
1513 atomic_inc_return(&ctx->cached_cq_overflow));
1514 }
1515 }
1516
1517 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001518 io_cqring_mark_overflow(ctx);
1519
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001520 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1521 io_cqring_ev_posted(ctx);
1522
1523 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001524 req = list_first_entry(&list, struct io_kiocb, compl.list);
1525 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001526 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001527 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001528
1529 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001530}
1531
Jens Axboebcda7ba2020-02-23 16:42:51 -07001532static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001533{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001534 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001535 struct io_uring_cqe *cqe;
1536
Jens Axboe78e19bb2019-11-06 15:21:34 -07001537 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001538
Jens Axboe2b188cc2019-01-07 10:46:33 -07001539 /*
1540 * If we can't get a cq entry, userspace overflowed the
1541 * submission (by quite a lot). Increment the overflow count in
1542 * the ring.
1543 */
1544 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001545 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001546 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001547 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001548 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001549 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1550 /*
1551 * If we're in ring overflow flush mode, or in task cancel mode,
1552 * then we cannot store the request for later flushing, we need
1553 * to drop it on the floor.
1554 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001555 WRITE_ONCE(ctx->rings->cq_overflow,
1556 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001557 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001558 if (list_empty(&ctx->cq_overflow_list)) {
1559 set_bit(0, &ctx->sq_check_overflow);
1560 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001561 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001562 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001563 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001564 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001565 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001566 refcount_inc(&req->refs);
1567 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001568 }
1569}
1570
Jens Axboebcda7ba2020-02-23 16:42:51 -07001571static void io_cqring_fill_event(struct io_kiocb *req, long res)
1572{
1573 __io_cqring_fill_event(req, res, 0);
1574}
1575
Jens Axboee1e16092020-06-22 09:17:17 -06001576static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001578 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579 unsigned long flags;
1580
1581 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001582 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001583 io_commit_cqring(ctx);
1584 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1585
Jens Axboe8c838782019-03-12 15:48:16 -06001586 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001587}
1588
Jens Axboe229a7b62020-06-22 10:13:11 -06001589static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001590{
Jens Axboe229a7b62020-06-22 10:13:11 -06001591 struct io_ring_ctx *ctx = cs->ctx;
1592
1593 spin_lock_irq(&ctx->completion_lock);
1594 while (!list_empty(&cs->list)) {
1595 struct io_kiocb *req;
1596
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001597 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1598 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001599 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001600 if (!(req->flags & REQ_F_LINK_HEAD)) {
1601 req->flags |= REQ_F_COMP_LOCKED;
1602 io_put_req(req);
1603 } else {
1604 spin_unlock_irq(&ctx->completion_lock);
1605 io_put_req(req);
1606 spin_lock_irq(&ctx->completion_lock);
1607 }
1608 }
1609 io_commit_cqring(ctx);
1610 spin_unlock_irq(&ctx->completion_lock);
1611
1612 io_cqring_ev_posted(ctx);
1613 cs->nr = 0;
1614}
1615
1616static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1617 struct io_comp_state *cs)
1618{
1619 if (!cs) {
1620 io_cqring_add_event(req, res, cflags);
1621 io_put_req(req);
1622 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001623 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001624 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001625 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001626 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001627 if (++cs->nr >= 32)
1628 io_submit_flush_completions(cs);
1629 }
Jens Axboee1e16092020-06-22 09:17:17 -06001630}
1631
1632static void io_req_complete(struct io_kiocb *req, long res)
1633{
Jens Axboe229a7b62020-06-22 10:13:11 -06001634 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001635}
1636
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001637static inline bool io_is_fallback_req(struct io_kiocb *req)
1638{
1639 return req == (struct io_kiocb *)
1640 ((unsigned long) req->ctx->fallback_req & ~1UL);
1641}
1642
1643static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1644{
1645 struct io_kiocb *req;
1646
1647 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001648 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001649 return req;
1650
1651 return NULL;
1652}
1653
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001654static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1655 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001656{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001657 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001658 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001659 size_t sz;
1660 int ret;
1661
1662 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001663 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1664
1665 /*
1666 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1667 * retry single alloc to be on the safe side.
1668 */
1669 if (unlikely(ret <= 0)) {
1670 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1671 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001672 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001673 ret = 1;
1674 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001675 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676 }
1677
Pavel Begunkov291b2822020-09-30 22:57:01 +03001678 state->free_reqs--;
1679 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001680fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001681 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682}
1683
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001684static inline void io_put_file(struct io_kiocb *req, struct file *file,
1685 bool fixed)
1686{
1687 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001688 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001689 else
1690 fput(file);
1691}
1692
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001693static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001694{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001695 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001696
Jens Axboee8c2bc12020-08-15 18:44:09 -07001697 if (req->async_data)
1698 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001699 if (req->file)
1700 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001701
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001702 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001703}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001704
Jens Axboe51a4cc12020-08-10 10:55:56 -06001705static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001706{
Jens Axboe0f212202020-09-13 13:09:39 -06001707 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001708 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001709
Jens Axboe0f212202020-09-13 13:09:39 -06001710 atomic_long_inc(&tctx->req_complete);
1711 if (tctx->in_idle)
1712 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001713 put_task_struct(req->task);
1714
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001715 if (likely(!io_is_fallback_req(req)))
1716 kmem_cache_free(req_cachep, req);
1717 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001718 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1719 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001720}
1721
Jens Axboe51a4cc12020-08-10 10:55:56 -06001722static void io_req_task_file_table_put(struct callback_head *cb)
1723{
1724 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe51a4cc12020-08-10 10:55:56 -06001725
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001726 io_dismantle_req(req);
Jens Axboe51a4cc12020-08-10 10:55:56 -06001727 __io_free_req_finish(req);
1728}
1729
1730static void __io_free_req(struct io_kiocb *req)
1731{
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001732 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1733 io_dismantle_req(req);
Jens Axboe51a4cc12020-08-10 10:55:56 -06001734 __io_free_req_finish(req);
1735 } else {
1736 int ret;
1737
1738 init_task_work(&req->task_work, io_req_task_file_table_put);
1739 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1740 if (unlikely(ret)) {
1741 struct task_struct *tsk;
1742
1743 tsk = io_wq_get_task(req->ctx->io_wq);
1744 task_work_add(tsk, &req->task_work, 0);
1745 }
1746 }
1747}
1748
Jackie Liua197f662019-11-08 08:09:12 -07001749static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001750{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001751 struct io_timeout_data *io = req->async_data;
Jackie Liua197f662019-11-08 08:09:12 -07001752 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001753 int ret;
1754
Jens Axboee8c2bc12020-08-15 18:44:09 -07001755 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001756 if (ret != -1) {
Pavel Begunkov368c5482020-10-13 09:43:56 +01001757 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001758 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001759 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001760 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001761 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001762 return true;
1763 }
1764
1765 return false;
1766}
1767
Jens Axboeab0b6452020-06-30 08:43:15 -06001768static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001769{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001770 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001771 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001772
1773 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001774 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001775 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1776 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001777 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001778
1779 list_del_init(&link->link_list);
1780 wake_ev = io_link_cancel_timeout(link);
1781 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001782 return wake_ev;
1783}
1784
1785static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001786{
Jens Axboe2665abf2019-11-05 12:40:47 -07001787 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001788 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001789
Jens Axboeab0b6452020-06-30 08:43:15 -06001790 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1791 unsigned long flags;
1792
1793 spin_lock_irqsave(&ctx->completion_lock, flags);
1794 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001795 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001796 } else {
1797 wake_ev = __io_kill_linked_timeout(req);
1798 }
1799
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001800 if (wake_ev)
1801 io_cqring_ev_posted(ctx);
1802}
1803
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001804static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001805{
1806 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001807
Jens Axboe9e645e112019-05-10 16:07:28 -06001808 /*
1809 * The list should never be empty when we are called here. But could
1810 * potentially happen if the chain is messed up, check to be on the
1811 * safe side.
1812 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001813 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001814 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001815
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001816 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1817 list_del_init(&req->link_list);
1818 if (!list_empty(&nxt->link_list))
1819 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001820 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001821}
1822
1823/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001824 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001825 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001826static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001827{
Jens Axboe2665abf2019-11-05 12:40:47 -07001828 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001829
1830 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001831 struct io_kiocb *link = list_first_entry(&req->link_list,
1832 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001833
Pavel Begunkov44932332019-12-05 16:16:35 +03001834 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001835 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001836
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001837 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001838 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001839 __io_double_put_req(link);
Jens Axboe9e645e112019-05-10 16:07:28 -06001840 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001841
1842 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001843 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001844}
1845
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001846static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001847{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001848 struct io_ring_ctx *ctx = req->ctx;
1849
1850 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1851 unsigned long flags;
1852
1853 spin_lock_irqsave(&ctx->completion_lock, flags);
1854 __io_fail_links(req);
1855 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1856 } else {
1857 __io_fail_links(req);
1858 }
1859
Jens Axboe9e645e112019-05-10 16:07:28 -06001860 io_cqring_ev_posted(ctx);
1861}
1862
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001863static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001864{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001865 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001866 if (req->flags & REQ_F_LINK_TIMEOUT)
1867 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001868
Jens Axboe9e645e112019-05-10 16:07:28 -06001869 /*
1870 * If LINK is set, we have dependent requests in this chain. If we
1871 * didn't fail this request, queue the first one up, moving any other
1872 * dependencies to the next request. In case of failure, fail the rest
1873 * of the chain.
1874 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001875 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1876 return io_req_link_next(req);
1877 io_fail_links(req);
1878 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001879}
Jens Axboe2665abf2019-11-05 12:40:47 -07001880
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001881static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1882{
1883 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1884 return NULL;
1885 return __io_req_find_next(req);
1886}
1887
Jens Axboe87c43112020-09-30 21:00:14 -06001888static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001889{
1890 struct task_struct *tsk = req->task;
1891 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001892 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001893
Jens Axboe6200b0a2020-09-13 14:38:30 -06001894 if (tsk->flags & PF_EXITING)
1895 return -ESRCH;
1896
Jens Axboec2c4c832020-07-01 15:37:11 -06001897 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001898 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1899 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1900 * processing task_work. There's no reliable way to tell if TWA_RESUME
1901 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001902 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001903 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001904 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001905 notify = TWA_SIGNAL;
1906
Jens Axboe87c43112020-09-30 21:00:14 -06001907 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001908 if (!ret)
1909 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001910
Jens Axboec2c4c832020-07-01 15:37:11 -06001911 return ret;
1912}
1913
Jens Axboec40f6372020-06-25 15:39:59 -06001914static void __io_req_task_cancel(struct io_kiocb *req, int error)
1915{
1916 struct io_ring_ctx *ctx = req->ctx;
1917
1918 spin_lock_irq(&ctx->completion_lock);
1919 io_cqring_fill_event(req, error);
1920 io_commit_cqring(ctx);
1921 spin_unlock_irq(&ctx->completion_lock);
1922
1923 io_cqring_ev_posted(ctx);
1924 req_set_fail_links(req);
1925 io_double_put_req(req);
1926}
1927
1928static void io_req_task_cancel(struct callback_head *cb)
1929{
1930 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001931 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001932
1933 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001934 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001935}
1936
1937static void __io_req_task_submit(struct io_kiocb *req)
1938{
1939 struct io_ring_ctx *ctx = req->ctx;
1940
Jens Axboec40f6372020-06-25 15:39:59 -06001941 if (!__io_sq_thread_acquire_mm(ctx)) {
1942 mutex_lock(&ctx->uring_lock);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001943 __io_queue_sqe(req, NULL);
Jens Axboec40f6372020-06-25 15:39:59 -06001944 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001945 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001946 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001947 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001948}
1949
Jens Axboec40f6372020-06-25 15:39:59 -06001950static void io_req_task_submit(struct callback_head *cb)
1951{
1952 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001953 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001954
1955 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001956 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001957}
1958
1959static void io_req_task_queue(struct io_kiocb *req)
1960{
Jens Axboec40f6372020-06-25 15:39:59 -06001961 int ret;
1962
1963 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001964 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001965
Jens Axboe87c43112020-09-30 21:00:14 -06001966 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001967 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001968 struct task_struct *tsk;
1969
Jens Axboec40f6372020-06-25 15:39:59 -06001970 init_task_work(&req->task_work, io_req_task_cancel);
1971 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001972 task_work_add(tsk, &req->task_work, 0);
1973 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001974 }
Jens Axboec40f6372020-06-25 15:39:59 -06001975}
1976
Pavel Begunkovc3524382020-06-28 12:52:32 +03001977static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001978{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001979 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001980
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001981 if (nxt)
1982 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001983}
1984
Jens Axboe9e645e112019-05-10 16:07:28 -06001985static void io_free_req(struct io_kiocb *req)
1986{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001987 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001988 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001989}
1990
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001991struct req_batch {
1992 void *reqs[IO_IOPOLL_BATCH];
1993 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001994
1995 struct task_struct *task;
1996 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001997};
1998
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001999static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002000{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002001 rb->to_free = 0;
2002 rb->task_refs = 0;
2003 rb->task = NULL;
2004}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002005
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002006static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2007 struct req_batch *rb)
2008{
2009 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2010 percpu_ref_put_many(&ctx->refs, rb->to_free);
2011 rb->to_free = 0;
2012}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002013
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002014static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2015 struct req_batch *rb)
2016{
2017 if (rb->to_free)
2018 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002019 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002020 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002021 put_task_struct_many(rb->task, rb->task_refs);
2022 rb->task = NULL;
2023 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002024}
2025
2026static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2027{
2028 if (unlikely(io_is_fallback_req(req))) {
2029 io_free_req(req);
2030 return;
2031 }
2032 if (req->flags & REQ_F_LINK_HEAD)
2033 io_queue_next(req);
2034
Jens Axboee3bc8e92020-09-24 08:45:57 -06002035 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002036 if (rb->task) {
2037 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002038 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002039 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002040 rb->task = req->task;
2041 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002042 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002043 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002044
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002045 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002046 rb->reqs[rb->to_free++] = req;
2047 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2048 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002049}
2050
Jens Axboeba816ad2019-09-28 11:36:45 -06002051/*
2052 * Drop reference to request, return next in chain (if there is one) if this
2053 * was the last reference to this request.
2054 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002055static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002056{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002057 struct io_kiocb *nxt = NULL;
2058
Jens Axboe2a44f462020-02-25 13:25:41 -07002059 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002060 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002061 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002062 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002063 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002064}
2065
Jens Axboe2b188cc2019-01-07 10:46:33 -07002066static void io_put_req(struct io_kiocb *req)
2067{
Jens Axboedef596e2019-01-09 08:59:42 -07002068 if (refcount_dec_and_test(&req->refs))
2069 io_free_req(req);
2070}
2071
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002072static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002073{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002074 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002075
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002076 /*
2077 * A ref is owned by io-wq in which context we're. So, if that's the
2078 * last one, it's safe to steal next work. False negatives are Ok,
2079 * it just will be re-punted async in io_put_work()
2080 */
2081 if (refcount_read(&req->refs) != 1)
2082 return NULL;
2083
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002084 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002085 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002086}
2087
Jens Axboe978db572019-11-14 22:39:04 -07002088/*
2089 * Must only be used if we don't need to care about links, usually from
2090 * within the completion handling itself.
2091 */
2092static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002093{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002094 /* drop both submit and complete references */
2095 if (refcount_sub_and_test(2, &req->refs))
2096 __io_free_req(req);
2097}
2098
Jens Axboe978db572019-11-14 22:39:04 -07002099static void io_double_put_req(struct io_kiocb *req)
2100{
2101 /* drop both submit and complete references */
2102 if (refcount_sub_and_test(2, &req->refs))
2103 io_free_req(req);
2104}
2105
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002106static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002107{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002108 struct io_rings *rings = ctx->rings;
2109
Jens Axboead3eb2c2019-12-18 17:12:20 -07002110 if (test_bit(0, &ctx->cq_check_overflow)) {
2111 /*
2112 * noflush == true is from the waitqueue handler, just ensure
2113 * we wake up the task, and the next invocation will flush the
2114 * entries. We cannot safely to it from here.
2115 */
2116 if (noflush && !list_empty(&ctx->cq_overflow_list))
2117 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002118
Jens Axboee6c8aa92020-09-28 13:10:13 -06002119 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002120 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002121
Jens Axboea3a0e432019-08-20 11:03:11 -06002122 /* See comment at the top of this file */
2123 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002124 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002125}
2126
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002127static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2128{
2129 struct io_rings *rings = ctx->rings;
2130
2131 /* make sure SQ entry isn't read before tail */
2132 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2133}
2134
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002135static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002136{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002137 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002138
Jens Axboebcda7ba2020-02-23 16:42:51 -07002139 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2140 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002141 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002142 kfree(kbuf);
2143 return cflags;
2144}
2145
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002146static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2147{
2148 struct io_buffer *kbuf;
2149
2150 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2151 return io_put_kbuf(req, kbuf);
2152}
2153
Jens Axboe4c6e2772020-07-01 11:29:10 -06002154static inline bool io_run_task_work(void)
2155{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002156 /*
2157 * Not safe to run on exiting task, and the task_work handling will
2158 * not add work to such a task.
2159 */
2160 if (unlikely(current->flags & PF_EXITING))
2161 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002162 if (current->task_works) {
2163 __set_current_state(TASK_RUNNING);
2164 task_work_run();
2165 return true;
2166 }
2167
2168 return false;
2169}
2170
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002171static void io_iopoll_queue(struct list_head *again)
2172{
2173 struct io_kiocb *req;
2174
2175 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002176 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2177 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002178 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002179 } while (!list_empty(again));
2180}
2181
Jens Axboedef596e2019-01-09 08:59:42 -07002182/*
2183 * Find and free completed poll iocbs
2184 */
2185static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2186 struct list_head *done)
2187{
Jens Axboe8237e042019-12-28 10:48:22 -07002188 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002189 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002190 LIST_HEAD(again);
2191
2192 /* order with ->result store in io_complete_rw_iopoll() */
2193 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002194
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002195 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002196 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002197 int cflags = 0;
2198
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002199 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002200 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002201 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002202 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002203 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002204 continue;
2205 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002206 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002207
Jens Axboebcda7ba2020-02-23 16:42:51 -07002208 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002209 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002210
2211 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002212 (*nr_events)++;
2213
Pavel Begunkovc3524382020-06-28 12:52:32 +03002214 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002215 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002216 }
Jens Axboedef596e2019-01-09 08:59:42 -07002217
Jens Axboe09bb8392019-03-13 12:39:28 -06002218 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002219 if (ctx->flags & IORING_SETUP_SQPOLL)
2220 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002221 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002222
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002223 if (!list_empty(&again))
2224 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002225}
2226
Jens Axboedef596e2019-01-09 08:59:42 -07002227static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2228 long min)
2229{
2230 struct io_kiocb *req, *tmp;
2231 LIST_HEAD(done);
2232 bool spin;
2233 int ret;
2234
2235 /*
2236 * Only spin for completions if we don't have multiple devices hanging
2237 * off our complete list, and we're under the requested amount.
2238 */
2239 spin = !ctx->poll_multi_file && *nr_events < min;
2240
2241 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002242 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002243 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002244
2245 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002246 * Move completed and retryable entries to our local lists.
2247 * If we find a request that requires polling, break out
2248 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002249 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002250 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002251 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002252 continue;
2253 }
2254 if (!list_empty(&done))
2255 break;
2256
2257 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2258 if (ret < 0)
2259 break;
2260
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002261 /* iopoll may have completed current req */
2262 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002263 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002264
Jens Axboedef596e2019-01-09 08:59:42 -07002265 if (ret && spin)
2266 spin = false;
2267 ret = 0;
2268 }
2269
2270 if (!list_empty(&done))
2271 io_iopoll_complete(ctx, nr_events, &done);
2272
2273 return ret;
2274}
2275
2276/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002277 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002278 * non-spinning poll check - we'll still enter the driver poll loop, but only
2279 * as a non-spinning completion check.
2280 */
2281static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2282 long min)
2283{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002284 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002285 int ret;
2286
2287 ret = io_do_iopoll(ctx, nr_events, min);
2288 if (ret < 0)
2289 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002290 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002291 return 0;
2292 }
2293
2294 return 1;
2295}
2296
2297/*
2298 * We can't just wait for polled events to come to us, we have to actively
2299 * find and complete them.
2300 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002301static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002302{
2303 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2304 return;
2305
2306 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002307 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002308 unsigned int nr_events = 0;
2309
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002310 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002311
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002312 /* let it sleep and repeat later if can't complete a request */
2313 if (nr_events == 0)
2314 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002315 /*
2316 * Ensure we allow local-to-the-cpu processing to take place,
2317 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002318 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002319 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002320 if (need_resched()) {
2321 mutex_unlock(&ctx->uring_lock);
2322 cond_resched();
2323 mutex_lock(&ctx->uring_lock);
2324 }
Jens Axboedef596e2019-01-09 08:59:42 -07002325 }
2326 mutex_unlock(&ctx->uring_lock);
2327}
2328
Pavel Begunkov7668b922020-07-07 16:36:21 +03002329static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002330{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002331 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002332 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002333
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002334 /*
2335 * We disallow the app entering submit/complete with polling, but we
2336 * still need to lock the ring to prevent racing with polled issue
2337 * that got punted to a workqueue.
2338 */
2339 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002340 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002341 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002342 * Don't enter poll loop if we already have events pending.
2343 * If we do, we can potentially be spinning for commands that
2344 * already triggered a CQE (eg in error).
2345 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002346 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002347 break;
2348
2349 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002350 * If a submit got punted to a workqueue, we can have the
2351 * application entering polling for a command before it gets
2352 * issued. That app will hold the uring_lock for the duration
2353 * of the poll right here, so we need to take a breather every
2354 * now and then to ensure that the issue has a chance to add
2355 * the poll to the issued list. Otherwise we can spin here
2356 * forever, while the workqueue is stuck trying to acquire the
2357 * very same mutex.
2358 */
2359 if (!(++iters & 7)) {
2360 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002361 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002362 mutex_lock(&ctx->uring_lock);
2363 }
2364
Pavel Begunkov7668b922020-07-07 16:36:21 +03002365 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002366 if (ret <= 0)
2367 break;
2368 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002369 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002370
Jens Axboe500f9fb2019-08-19 12:15:59 -06002371 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002372 return ret;
2373}
2374
Jens Axboe491381ce2019-10-17 09:20:46 -06002375static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002376{
Jens Axboe491381ce2019-10-17 09:20:46 -06002377 /*
2378 * Tell lockdep we inherited freeze protection from submission
2379 * thread.
2380 */
2381 if (req->flags & REQ_F_ISREG) {
2382 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002383
Jens Axboe491381ce2019-10-17 09:20:46 -06002384 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002385 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002386 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002387}
2388
Jens Axboea1d7c392020-06-22 11:09:46 -06002389static void io_complete_rw_common(struct kiocb *kiocb, long res,
2390 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002391{
Jens Axboe9adbd452019-12-20 08:45:55 -07002392 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002393 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002394
Jens Axboe491381ce2019-10-17 09:20:46 -06002395 if (kiocb->ki_flags & IOCB_WRITE)
2396 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002397
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002398 if (res != req->result)
2399 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002400 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002401 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002402 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002403}
2404
Jens Axboeb63534c2020-06-04 11:28:00 -06002405#ifdef CONFIG_BLOCK
2406static bool io_resubmit_prep(struct io_kiocb *req, int error)
2407{
2408 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2409 ssize_t ret = -ECANCELED;
2410 struct iov_iter iter;
2411 int rw;
2412
2413 if (error) {
2414 ret = error;
2415 goto end_req;
2416 }
2417
2418 switch (req->opcode) {
2419 case IORING_OP_READV:
2420 case IORING_OP_READ_FIXED:
2421 case IORING_OP_READ:
2422 rw = READ;
2423 break;
2424 case IORING_OP_WRITEV:
2425 case IORING_OP_WRITE_FIXED:
2426 case IORING_OP_WRITE:
2427 rw = WRITE;
2428 break;
2429 default:
2430 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2431 req->opcode);
2432 goto end_req;
2433 }
2434
Jens Axboee8c2bc12020-08-15 18:44:09 -07002435 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002436 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2437 if (ret < 0)
2438 goto end_req;
2439 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2440 if (!ret)
2441 return true;
2442 kfree(iovec);
2443 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002444 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002445 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002446end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002447 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002448 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002449 return false;
2450}
Jens Axboeb63534c2020-06-04 11:28:00 -06002451#endif
2452
2453static bool io_rw_reissue(struct io_kiocb *req, long res)
2454{
2455#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002456 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002457 int ret;
2458
Jens Axboe355afae2020-09-02 09:30:31 -06002459 if (!S_ISBLK(mode) && !S_ISREG(mode))
2460 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002461 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2462 return false;
2463
Jens Axboefdee9462020-08-27 16:46:24 -06002464 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002465
Jens Axboefdee9462020-08-27 16:46:24 -06002466 if (io_resubmit_prep(req, ret)) {
2467 refcount_inc(&req->refs);
2468 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002469 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002470 }
2471
Jens Axboeb63534c2020-06-04 11:28:00 -06002472#endif
2473 return false;
2474}
2475
Jens Axboea1d7c392020-06-22 11:09:46 -06002476static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2477 struct io_comp_state *cs)
2478{
2479 if (!io_rw_reissue(req, res))
2480 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002481}
2482
2483static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2484{
Jens Axboe9adbd452019-12-20 08:45:55 -07002485 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002486
Jens Axboea1d7c392020-06-22 11:09:46 -06002487 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002488}
2489
Jens Axboedef596e2019-01-09 08:59:42 -07002490static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2491{
Jens Axboe9adbd452019-12-20 08:45:55 -07002492 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002493
Jens Axboe491381ce2019-10-17 09:20:46 -06002494 if (kiocb->ki_flags & IOCB_WRITE)
2495 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002496
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002497 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002498 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002499
2500 WRITE_ONCE(req->result, res);
2501 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002502 smp_wmb();
2503 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002504}
2505
2506/*
2507 * After the iocb has been issued, it's safe to be found on the poll list.
2508 * Adding the kiocb to the list AFTER submission ensures that we don't
2509 * find it from a io_iopoll_getevents() thread before the issuer is done
2510 * accessing the kiocb cookie.
2511 */
2512static void io_iopoll_req_issued(struct io_kiocb *req)
2513{
2514 struct io_ring_ctx *ctx = req->ctx;
2515
2516 /*
2517 * Track whether we have multiple files in our lists. This will impact
2518 * how we do polling eventually, not spinning if we're on potentially
2519 * different devices.
2520 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002521 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002522 ctx->poll_multi_file = false;
2523 } else if (!ctx->poll_multi_file) {
2524 struct io_kiocb *list_req;
2525
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002526 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002527 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002528 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002529 ctx->poll_multi_file = true;
2530 }
2531
2532 /*
2533 * For fast devices, IO may have already completed. If it has, add
2534 * it to the front so we find it first.
2535 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002536 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002537 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002538 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002539 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002540
2541 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002542 wq_has_sleeper(&ctx->sq_data->wait))
2543 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002544}
2545
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002546static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002547{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002548 if (state->has_refs)
2549 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002550 state->file = NULL;
2551}
2552
2553static inline void io_state_file_put(struct io_submit_state *state)
2554{
2555 if (state->file)
2556 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002557}
2558
2559/*
2560 * Get as many references to a file as we have IOs left in this submission,
2561 * assuming most submissions are for one file, or at least that each file
2562 * has more than one submission.
2563 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002564static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002565{
2566 if (!state)
2567 return fget(fd);
2568
2569 if (state->file) {
2570 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002571 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002572 return state->file;
2573 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002574 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002575 }
2576 state->file = fget_many(fd, state->ios_left);
2577 if (!state->file)
2578 return NULL;
2579
2580 state->fd = fd;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01002581 state->has_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002582 return state->file;
2583}
2584
Jens Axboe4503b762020-06-01 10:00:27 -06002585static bool io_bdev_nowait(struct block_device *bdev)
2586{
2587#ifdef CONFIG_BLOCK
2588 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2589#else
2590 return true;
2591#endif
2592}
2593
Jens Axboe2b188cc2019-01-07 10:46:33 -07002594/*
2595 * If we tracked the file through the SCM inflight mechanism, we could support
2596 * any file. For now, just ensure that anything potentially problematic is done
2597 * inline.
2598 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002599static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002600{
2601 umode_t mode = file_inode(file)->i_mode;
2602
Jens Axboe4503b762020-06-01 10:00:27 -06002603 if (S_ISBLK(mode)) {
2604 if (io_bdev_nowait(file->f_inode->i_bdev))
2605 return true;
2606 return false;
2607 }
2608 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002609 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002610 if (S_ISREG(mode)) {
2611 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2612 file->f_op != &io_uring_fops)
2613 return true;
2614 return false;
2615 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002616
Jens Axboec5b85622020-06-09 19:23:05 -06002617 /* any ->read/write should understand O_NONBLOCK */
2618 if (file->f_flags & O_NONBLOCK)
2619 return true;
2620
Jens Axboeaf197f52020-04-28 13:15:06 -06002621 if (!(file->f_mode & FMODE_NOWAIT))
2622 return false;
2623
2624 if (rw == READ)
2625 return file->f_op->read_iter != NULL;
2626
2627 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002628}
2629
Pavel Begunkova88fc402020-09-30 22:57:53 +03002630static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631{
Jens Axboedef596e2019-01-09 08:59:42 -07002632 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002633 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002634 unsigned ioprio;
2635 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002636
Jens Axboe491381ce2019-10-17 09:20:46 -06002637 if (S_ISREG(file_inode(req->file)->i_mode))
2638 req->flags |= REQ_F_ISREG;
2639
Jens Axboe2b188cc2019-01-07 10:46:33 -07002640 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002641 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2642 req->flags |= REQ_F_CUR_POS;
2643 kiocb->ki_pos = req->file->f_pos;
2644 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002645 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002646 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2647 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2648 if (unlikely(ret))
2649 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650
2651 ioprio = READ_ONCE(sqe->ioprio);
2652 if (ioprio) {
2653 ret = ioprio_check_cap(ioprio);
2654 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002655 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002656
2657 kiocb->ki_ioprio = ioprio;
2658 } else
2659 kiocb->ki_ioprio = get_current_ioprio();
2660
Stefan Bühler8449eed2019-04-27 20:34:19 +02002661 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002662 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002663 req->flags |= REQ_F_NOWAIT;
2664
Jens Axboedef596e2019-01-09 08:59:42 -07002665 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002666 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2667 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002668 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002669
Jens Axboedef596e2019-01-09 08:59:42 -07002670 kiocb->ki_flags |= IOCB_HIPRI;
2671 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002672 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002673 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002674 if (kiocb->ki_flags & IOCB_HIPRI)
2675 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002676 kiocb->ki_complete = io_complete_rw;
2677 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002678
Jens Axboe3529d8c2019-12-19 18:24:38 -07002679 req->rw.addr = READ_ONCE(sqe->addr);
2680 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002681 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002682 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683}
2684
2685static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2686{
2687 switch (ret) {
2688 case -EIOCBQUEUED:
2689 break;
2690 case -ERESTARTSYS:
2691 case -ERESTARTNOINTR:
2692 case -ERESTARTNOHAND:
2693 case -ERESTART_RESTARTBLOCK:
2694 /*
2695 * We can't just restart the syscall, since previously
2696 * submitted sqes may already be in progress. Just fail this
2697 * IO with EINTR.
2698 */
2699 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002700 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002701 default:
2702 kiocb->ki_complete(kiocb, ret, 0);
2703 }
2704}
2705
Jens Axboea1d7c392020-06-22 11:09:46 -06002706static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2707 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002708{
Jens Axboeba042912019-12-25 16:33:42 -07002709 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002710 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002711
Jens Axboe227c0c92020-08-13 11:51:40 -06002712 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002713 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002714 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002715 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002716 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002717 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002718 }
2719
Jens Axboeba042912019-12-25 16:33:42 -07002720 if (req->flags & REQ_F_CUR_POS)
2721 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002722 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002723 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002724 else
2725 io_rw_done(kiocb, ret);
2726}
2727
Jens Axboe9adbd452019-12-20 08:45:55 -07002728static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002729 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002730{
Jens Axboe9adbd452019-12-20 08:45:55 -07002731 struct io_ring_ctx *ctx = req->ctx;
2732 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002733 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002734 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002735 size_t offset;
2736 u64 buf_addr;
2737
Jens Axboeedafcce2019-01-09 09:16:05 -07002738 if (unlikely(buf_index >= ctx->nr_user_bufs))
2739 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002740 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2741 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002742 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002743
2744 /* overflow */
2745 if (buf_addr + len < buf_addr)
2746 return -EFAULT;
2747 /* not inside the mapped region */
2748 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2749 return -EFAULT;
2750
2751 /*
2752 * May not be a start of buffer, set size appropriately
2753 * and advance us to the beginning.
2754 */
2755 offset = buf_addr - imu->ubuf;
2756 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002757
2758 if (offset) {
2759 /*
2760 * Don't use iov_iter_advance() here, as it's really slow for
2761 * using the latter parts of a big fixed buffer - it iterates
2762 * over each segment manually. We can cheat a bit here, because
2763 * we know that:
2764 *
2765 * 1) it's a BVEC iter, we set it up
2766 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2767 * first and last bvec
2768 *
2769 * So just find our index, and adjust the iterator afterwards.
2770 * If the offset is within the first bvec (or the whole first
2771 * bvec, just use iov_iter_advance(). This makes it easier
2772 * since we can just skip the first segment, which may not
2773 * be PAGE_SIZE aligned.
2774 */
2775 const struct bio_vec *bvec = imu->bvec;
2776
2777 if (offset <= bvec->bv_len) {
2778 iov_iter_advance(iter, offset);
2779 } else {
2780 unsigned long seg_skip;
2781
2782 /* skip first vec */
2783 offset -= bvec->bv_len;
2784 seg_skip = 1 + (offset >> PAGE_SHIFT);
2785
2786 iter->bvec = bvec + seg_skip;
2787 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002788 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002789 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002790 }
2791 }
2792
Jens Axboe5e559562019-11-13 16:12:46 -07002793 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002794}
2795
Jens Axboebcda7ba2020-02-23 16:42:51 -07002796static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2797{
2798 if (needs_lock)
2799 mutex_unlock(&ctx->uring_lock);
2800}
2801
2802static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2803{
2804 /*
2805 * "Normal" inline submissions always hold the uring_lock, since we
2806 * grab it from the system call. Same is true for the SQPOLL offload.
2807 * The only exception is when we've detached the request and issue it
2808 * from an async worker thread, grab the lock for that case.
2809 */
2810 if (needs_lock)
2811 mutex_lock(&ctx->uring_lock);
2812}
2813
2814static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2815 int bgid, struct io_buffer *kbuf,
2816 bool needs_lock)
2817{
2818 struct io_buffer *head;
2819
2820 if (req->flags & REQ_F_BUFFER_SELECTED)
2821 return kbuf;
2822
2823 io_ring_submit_lock(req->ctx, needs_lock);
2824
2825 lockdep_assert_held(&req->ctx->uring_lock);
2826
2827 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2828 if (head) {
2829 if (!list_empty(&head->list)) {
2830 kbuf = list_last_entry(&head->list, struct io_buffer,
2831 list);
2832 list_del(&kbuf->list);
2833 } else {
2834 kbuf = head;
2835 idr_remove(&req->ctx->io_buffer_idr, bgid);
2836 }
2837 if (*len > kbuf->len)
2838 *len = kbuf->len;
2839 } else {
2840 kbuf = ERR_PTR(-ENOBUFS);
2841 }
2842
2843 io_ring_submit_unlock(req->ctx, needs_lock);
2844
2845 return kbuf;
2846}
2847
Jens Axboe4d954c22020-02-27 07:31:19 -07002848static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2849 bool needs_lock)
2850{
2851 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002852 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002853
2854 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002855 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002856 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2857 if (IS_ERR(kbuf))
2858 return kbuf;
2859 req->rw.addr = (u64) (unsigned long) kbuf;
2860 req->flags |= REQ_F_BUFFER_SELECTED;
2861 return u64_to_user_ptr(kbuf->addr);
2862}
2863
2864#ifdef CONFIG_COMPAT
2865static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2866 bool needs_lock)
2867{
2868 struct compat_iovec __user *uiov;
2869 compat_ssize_t clen;
2870 void __user *buf;
2871 ssize_t len;
2872
2873 uiov = u64_to_user_ptr(req->rw.addr);
2874 if (!access_ok(uiov, sizeof(*uiov)))
2875 return -EFAULT;
2876 if (__get_user(clen, &uiov->iov_len))
2877 return -EFAULT;
2878 if (clen < 0)
2879 return -EINVAL;
2880
2881 len = clen;
2882 buf = io_rw_buffer_select(req, &len, needs_lock);
2883 if (IS_ERR(buf))
2884 return PTR_ERR(buf);
2885 iov[0].iov_base = buf;
2886 iov[0].iov_len = (compat_size_t) len;
2887 return 0;
2888}
2889#endif
2890
2891static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2892 bool needs_lock)
2893{
2894 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2895 void __user *buf;
2896 ssize_t len;
2897
2898 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2899 return -EFAULT;
2900
2901 len = iov[0].iov_len;
2902 if (len < 0)
2903 return -EINVAL;
2904 buf = io_rw_buffer_select(req, &len, needs_lock);
2905 if (IS_ERR(buf))
2906 return PTR_ERR(buf);
2907 iov[0].iov_base = buf;
2908 iov[0].iov_len = len;
2909 return 0;
2910}
2911
2912static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2913 bool needs_lock)
2914{
Jens Axboedddb3e22020-06-04 11:27:01 -06002915 if (req->flags & REQ_F_BUFFER_SELECTED) {
2916 struct io_buffer *kbuf;
2917
2918 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2919 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2920 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002921 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002922 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002923 if (!req->rw.len)
2924 return 0;
2925 else if (req->rw.len > 1)
2926 return -EINVAL;
2927
2928#ifdef CONFIG_COMPAT
2929 if (req->ctx->compat)
2930 return io_compat_import(req, iov, needs_lock);
2931#endif
2932
2933 return __io_iov_buffer_select(req, iov, needs_lock);
2934}
2935
Jens Axboe8452fd02020-08-18 13:58:33 -07002936static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2937 struct iovec **iovec, struct iov_iter *iter,
2938 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939{
Jens Axboe9adbd452019-12-20 08:45:55 -07002940 void __user *buf = u64_to_user_ptr(req->rw.addr);
2941 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002942 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002943 u8 opcode;
2944
Jens Axboed625c6e2019-12-17 19:53:05 -07002945 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002946 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002947 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002948 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002949 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950
Jens Axboebcda7ba2020-02-23 16:42:51 -07002951 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002952 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002953 return -EINVAL;
2954
Jens Axboe3a6820f2019-12-22 15:19:35 -07002955 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002956 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002957 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002958 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002959 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002960 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002961 }
2962
Jens Axboe3a6820f2019-12-22 15:19:35 -07002963 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2964 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002965 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002966 }
2967
Jens Axboe4d954c22020-02-27 07:31:19 -07002968 if (req->flags & REQ_F_BUFFER_SELECT) {
2969 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002970 if (!ret) {
2971 ret = (*iovec)->iov_len;
2972 iov_iter_init(iter, rw, *iovec, 1, ret);
2973 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002974 *iovec = NULL;
2975 return ret;
2976 }
2977
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002978 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
2979 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002980}
2981
Jens Axboe8452fd02020-08-18 13:58:33 -07002982static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2983 struct iovec **iovec, struct iov_iter *iter,
2984 bool needs_lock)
2985{
Jens Axboee8c2bc12020-08-15 18:44:09 -07002986 struct io_async_rw *iorw = req->async_data;
2987
2988 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07002989 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2990 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07002991 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07002992}
2993
Jens Axboe0fef9482020-08-26 10:36:20 -06002994static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2995{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03002996 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06002997}
2998
Jens Axboe32960612019-09-23 11:05:34 -06002999/*
3000 * For files that don't have ->read_iter() and ->write_iter(), handle them
3001 * by looping over ->read() or ->write() manually.
3002 */
3003static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
3004 struct iov_iter *iter)
3005{
3006 ssize_t ret = 0;
3007
3008 /*
3009 * Don't support polled IO through this interface, and we can't
3010 * support non-blocking either. For the latter, this just causes
3011 * the kiocb to be handled from an async context.
3012 */
3013 if (kiocb->ki_flags & IOCB_HIPRI)
3014 return -EOPNOTSUPP;
3015 if (kiocb->ki_flags & IOCB_NOWAIT)
3016 return -EAGAIN;
3017
3018 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003019 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003020 ssize_t nr;
3021
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003022 if (!iov_iter_is_bvec(iter)) {
3023 iovec = iov_iter_iovec(iter);
3024 } else {
3025 /* fixed buffers import bvec */
3026 iovec.iov_base = kmap(iter->bvec->bv_page)
3027 + iter->iov_offset;
3028 iovec.iov_len = min(iter->count,
3029 iter->bvec->bv_len - iter->iov_offset);
3030 }
3031
Jens Axboe32960612019-09-23 11:05:34 -06003032 if (rw == READ) {
3033 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003034 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003035 } else {
3036 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003037 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003038 }
3039
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003040 if (iov_iter_is_bvec(iter))
3041 kunmap(iter->bvec->bv_page);
3042
Jens Axboe32960612019-09-23 11:05:34 -06003043 if (nr < 0) {
3044 if (!ret)
3045 ret = nr;
3046 break;
3047 }
3048 ret += nr;
3049 if (nr != iovec.iov_len)
3050 break;
3051 iov_iter_advance(iter, nr);
3052 }
3053
3054 return ret;
3055}
3056
Jens Axboeff6165b2020-08-13 09:47:43 -06003057static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3058 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003059{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003060 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003061
Jens Axboeff6165b2020-08-13 09:47:43 -06003062 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003063 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003064 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003065 /* can only be fixed buffers, no need to do anything */
3066 if (iter->type == ITER_BVEC)
3067 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003068 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003069 unsigned iov_off = 0;
3070
3071 rw->iter.iov = rw->fast_iov;
3072 if (iter->iov != fast_iov) {
3073 iov_off = iter->iov - fast_iov;
3074 rw->iter.iov += iov_off;
3075 }
3076 if (rw->fast_iov != fast_iov)
3077 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003078 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003079 } else {
3080 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003081 }
3082}
3083
Jens Axboee8c2bc12020-08-15 18:44:09 -07003084static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003085{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003086 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3087 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3088 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003089}
3090
Jens Axboee8c2bc12020-08-15 18:44:09 -07003091static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003092{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003093 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003094 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003095
Jens Axboee8c2bc12020-08-15 18:44:09 -07003096 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003097}
3098
Jens Axboeff6165b2020-08-13 09:47:43 -06003099static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3100 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003101 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003102{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003103 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003104 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003105 if (!req->async_data) {
3106 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003107 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003108
Jens Axboeff6165b2020-08-13 09:47:43 -06003109 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003110 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003111 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003112}
3113
Pavel Begunkov73debe62020-09-30 22:57:54 +03003114static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003115{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003116 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003117 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003118 ssize_t ret;
3119
Pavel Begunkov73debe62020-09-30 22:57:54 +03003120 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003121 if (unlikely(ret < 0))
3122 return ret;
3123
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003124 iorw->bytes_done = 0;
3125 iorw->free_iovec = iov;
3126 if (iov)
3127 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003128 return 0;
3129}
3130
Pavel Begunkov73debe62020-09-30 22:57:54 +03003131static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003132{
3133 ssize_t ret;
3134
Pavel Begunkova88fc402020-09-30 22:57:53 +03003135 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003136 if (ret)
3137 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003138
Jens Axboe3529d8c2019-12-19 18:24:38 -07003139 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3140 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003141
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003142 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003143 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003144 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003145 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003146}
3147
Jens Axboec1dd91d2020-08-03 16:43:59 -06003148/*
3149 * This is our waitqueue callback handler, registered through lock_page_async()
3150 * when we initially tried to do the IO with the iocb armed our waitqueue.
3151 * This gets called when the page is unlocked, and we generally expect that to
3152 * happen when the page IO is completed and the page is now uptodate. This will
3153 * queue a task_work based retry of the operation, attempting to copy the data
3154 * again. If the latter fails because the page was NOT uptodate, then we will
3155 * do a thread based blocking retry of the operation. That's the unexpected
3156 * slow path.
3157 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003158static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3159 int sync, void *arg)
3160{
3161 struct wait_page_queue *wpq;
3162 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003163 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003164 int ret;
3165
3166 wpq = container_of(wait, struct wait_page_queue, wait);
3167
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003168 if (!wake_page_match(wpq, key))
3169 return 0;
3170
Hao Xuc8d317a2020-09-29 20:00:45 +08003171 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003172 list_del_init(&wait->entry);
3173
Pavel Begunkove7375122020-07-12 20:42:04 +03003174 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003175 percpu_ref_get(&req->ctx->refs);
3176
Jens Axboebcf5a062020-05-22 09:24:42 -06003177 /* submit ref gets dropped, acquire a new one */
3178 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003179 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003180 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003181 struct task_struct *tsk;
3182
Jens Axboebcf5a062020-05-22 09:24:42 -06003183 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003184 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003185 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003186 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003187 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003188 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003189 return 1;
3190}
3191
Jens Axboec1dd91d2020-08-03 16:43:59 -06003192/*
3193 * This controls whether a given IO request should be armed for async page
3194 * based retry. If we return false here, the request is handed to the async
3195 * worker threads for retry. If we're doing buffered reads on a regular file,
3196 * we prepare a private wait_page_queue entry and retry the operation. This
3197 * will either succeed because the page is now uptodate and unlocked, or it
3198 * will register a callback when the page is unlocked at IO completion. Through
3199 * that callback, io_uring uses task_work to setup a retry of the operation.
3200 * That retry will attempt the buffered read again. The retry will generally
3201 * succeed, or in rare cases where it fails, we then fall back to using the
3202 * async worker threads for a blocking retry.
3203 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003204static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003205{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003206 struct io_async_rw *rw = req->async_data;
3207 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003208 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003209
3210 /* never retry for NOWAIT, we just complete with -EAGAIN */
3211 if (req->flags & REQ_F_NOWAIT)
3212 return false;
3213
Jens Axboe227c0c92020-08-13 11:51:40 -06003214 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003215 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003216 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003217
Jens Axboebcf5a062020-05-22 09:24:42 -06003218 /*
3219 * just use poll if we can, and don't attempt if the fs doesn't
3220 * support callback based unlocks
3221 */
3222 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3223 return false;
3224
Jens Axboe3b2a4432020-08-16 10:58:43 -07003225 wait->wait.func = io_async_buf_func;
3226 wait->wait.private = req;
3227 wait->wait.flags = 0;
3228 INIT_LIST_HEAD(&wait->wait.entry);
3229 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003230 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003231 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003232 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003233}
3234
3235static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3236{
3237 if (req->file->f_op->read_iter)
3238 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003239 else if (req->file->f_op->read)
3240 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3241 else
3242 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003243}
3244
Jens Axboea1d7c392020-06-22 11:09:46 -06003245static int io_read(struct io_kiocb *req, bool force_nonblock,
3246 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003247{
3248 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003249 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003250 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003251 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003252 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003253 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003254 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003255
Jens Axboee8c2bc12020-08-15 18:44:09 -07003256 if (rw)
3257 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003258
3259 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003260 if (ret < 0)
3261 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003262 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003263 io_size = ret;
3264 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003265 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266
Jens Axboefd6c2e42019-12-18 12:19:41 -07003267 /* Ensure we clear previously set non-block flag */
3268 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003269 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003270 else
3271 kiocb->ki_flags |= IOCB_NOWAIT;
3272
Jens Axboefd6c2e42019-12-18 12:19:41 -07003273
Pavel Begunkov24c74672020-06-21 13:09:51 +03003274 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003275 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3276 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003277 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003278
Jens Axboe0fef9482020-08-26 10:36:20 -06003279 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003280 if (unlikely(ret))
3281 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003282
Jens Axboe227c0c92020-08-13 11:51:40 -06003283 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003284
Jens Axboe227c0c92020-08-13 11:51:40 -06003285 if (!ret) {
3286 goto done;
3287 } else if (ret == -EIOCBQUEUED) {
3288 ret = 0;
3289 goto out_free;
3290 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003291 /* IOPOLL retry should happen for io-wq threads */
3292 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003293 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003294 /* no retry on NONBLOCK marked file */
3295 if (req->file->f_flags & O_NONBLOCK)
3296 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003297 /* some cases will consume bytes even on error returns */
3298 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003299 ret = 0;
3300 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003301 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003302 /* make sure -ERESTARTSYS -> -EINTR is done */
3303 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003304 }
3305
3306 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003307 if (!iov_iter_count(iter) || !force_nonblock ||
3308 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003309 goto done;
3310
3311 io_size -= ret;
3312copy_iov:
3313 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3314 if (ret2) {
3315 ret = ret2;
3316 goto out_free;
3317 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003318 if (no_async)
3319 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003320 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003321 /* it's copied and will be cleaned with ->io */
3322 iovec = NULL;
3323 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003324 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003325retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003326 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003327 /* if we can retry, do so with the callbacks armed */
3328 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003329 kiocb->ki_flags &= ~IOCB_WAITQ;
3330 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003331 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003332
3333 /*
3334 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3335 * get -EIOCBQUEUED, then we'll get a notification when the desired
3336 * page gets unlocked. We can also get a partial read here, and if we
3337 * do, then just retry at the new offset.
3338 */
3339 ret = io_iter_do_read(req, iter);
3340 if (ret == -EIOCBQUEUED) {
3341 ret = 0;
3342 goto out_free;
3343 } else if (ret > 0 && ret < io_size) {
3344 /* we got some bytes, but not all. retry. */
3345 goto retry;
3346 }
3347done:
3348 kiocb_done(kiocb, ret, cs);
3349 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003350out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003351 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003352 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003353 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003354 return ret;
3355}
3356
Pavel Begunkov73debe62020-09-30 22:57:54 +03003357static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003358{
3359 ssize_t ret;
3360
Pavel Begunkova88fc402020-09-30 22:57:53 +03003361 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003362 if (ret)
3363 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003364
Jens Axboe3529d8c2019-12-19 18:24:38 -07003365 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3366 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003367
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003368 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003369 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003370 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003371 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003372}
3373
Jens Axboea1d7c392020-06-22 11:09:46 -06003374static int io_write(struct io_kiocb *req, bool force_nonblock,
3375 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003376{
3377 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003378 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003379 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003380 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003381 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003382 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003383
Jens Axboee8c2bc12020-08-15 18:44:09 -07003384 if (rw)
3385 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003386
3387 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003388 if (ret < 0)
3389 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003390 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003391 io_size = ret;
3392 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003393
Jens Axboefd6c2e42019-12-18 12:19:41 -07003394 /* Ensure we clear previously set non-block flag */
3395 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003396 kiocb->ki_flags &= ~IOCB_NOWAIT;
3397 else
3398 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003399
Pavel Begunkov24c74672020-06-21 13:09:51 +03003400 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003401 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003402 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003403
Jens Axboe10d59342019-12-09 20:16:22 -07003404 /* file path doesn't support NOWAIT for non-direct_IO */
3405 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3406 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003407 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003408
Jens Axboe0fef9482020-08-26 10:36:20 -06003409 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003410 if (unlikely(ret))
3411 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003412
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003413 /*
3414 * Open-code file_start_write here to grab freeze protection,
3415 * which will be released by another thread in
3416 * io_complete_rw(). Fool lockdep by telling it the lock got
3417 * released so that it doesn't complain about the held lock when
3418 * we return to userspace.
3419 */
3420 if (req->flags & REQ_F_ISREG) {
3421 __sb_start_write(file_inode(req->file)->i_sb,
3422 SB_FREEZE_WRITE, true);
3423 __sb_writers_release(file_inode(req->file)->i_sb,
3424 SB_FREEZE_WRITE);
3425 }
3426 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003427
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003428 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003429 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003430 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003431 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003432 else
3433 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003434
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003435 /*
3436 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3437 * retry them without IOCB_NOWAIT.
3438 */
3439 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3440 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003441 /* no retry on NONBLOCK marked file */
3442 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3443 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003444 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003445 /* IOPOLL retry should happen for io-wq threads */
3446 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3447 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003448done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003449 kiocb_done(kiocb, ret2, cs);
3450 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003451copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003452 /* some cases will consume bytes even on error returns */
3453 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003454 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003455 if (!ret)
3456 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003457 }
Jens Axboe31b51512019-01-18 22:56:34 -07003458out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003459 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003460 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003461 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003462 return ret;
3463}
3464
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003465static int __io_splice_prep(struct io_kiocb *req,
3466 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003467{
3468 struct io_splice* sp = &req->splice;
3469 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003470
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003471 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3472 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003473
3474 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003475 sp->len = READ_ONCE(sqe->len);
3476 sp->flags = READ_ONCE(sqe->splice_flags);
3477
3478 if (unlikely(sp->flags & ~valid_flags))
3479 return -EINVAL;
3480
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003481 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3482 (sp->flags & SPLICE_F_FD_IN_FIXED));
3483 if (!sp->file_in)
3484 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003485 req->flags |= REQ_F_NEED_CLEANUP;
3486
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003487 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3488 /*
3489 * Splice operation will be punted aync, and here need to
3490 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3491 */
3492 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003493 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003494 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003495
3496 return 0;
3497}
3498
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003499static int io_tee_prep(struct io_kiocb *req,
3500 const struct io_uring_sqe *sqe)
3501{
3502 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3503 return -EINVAL;
3504 return __io_splice_prep(req, sqe);
3505}
3506
3507static int io_tee(struct io_kiocb *req, bool force_nonblock)
3508{
3509 struct io_splice *sp = &req->splice;
3510 struct file *in = sp->file_in;
3511 struct file *out = sp->file_out;
3512 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3513 long ret = 0;
3514
3515 if (force_nonblock)
3516 return -EAGAIN;
3517 if (sp->len)
3518 ret = do_tee(in, out, sp->len, flags);
3519
3520 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3521 req->flags &= ~REQ_F_NEED_CLEANUP;
3522
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003523 if (ret != sp->len)
3524 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003525 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003526 return 0;
3527}
3528
3529static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3530{
3531 struct io_splice* sp = &req->splice;
3532
3533 sp->off_in = READ_ONCE(sqe->splice_off_in);
3534 sp->off_out = READ_ONCE(sqe->off);
3535 return __io_splice_prep(req, sqe);
3536}
3537
Pavel Begunkov014db002020-03-03 21:33:12 +03003538static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003539{
3540 struct io_splice *sp = &req->splice;
3541 struct file *in = sp->file_in;
3542 struct file *out = sp->file_out;
3543 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3544 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003545 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003546
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003547 if (force_nonblock)
3548 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003549
3550 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3551 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003552
Jens Axboe948a7742020-05-17 14:21:38 -06003553 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003554 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003555
3556 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3557 req->flags &= ~REQ_F_NEED_CLEANUP;
3558
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003559 if (ret != sp->len)
3560 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003561 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003562 return 0;
3563}
3564
Jens Axboe2b188cc2019-01-07 10:46:33 -07003565/*
3566 * IORING_OP_NOP just posts a completion event, nothing else.
3567 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003568static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003569{
3570 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003571
Jens Axboedef596e2019-01-09 08:59:42 -07003572 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3573 return -EINVAL;
3574
Jens Axboe229a7b62020-06-22 10:13:11 -06003575 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003576 return 0;
3577}
3578
Jens Axboe3529d8c2019-12-19 18:24:38 -07003579static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003580{
Jens Axboe6b063142019-01-10 22:13:58 -07003581 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003582
Jens Axboe09bb8392019-03-13 12:39:28 -06003583 if (!req->file)
3584 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003585
Jens Axboe6b063142019-01-10 22:13:58 -07003586 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003587 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003588 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003589 return -EINVAL;
3590
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003591 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3592 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3593 return -EINVAL;
3594
3595 req->sync.off = READ_ONCE(sqe->off);
3596 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003597 return 0;
3598}
3599
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003600static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003601{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003602 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003603 int ret;
3604
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003605 /* fsync always requires a blocking context */
3606 if (force_nonblock)
3607 return -EAGAIN;
3608
Jens Axboe9adbd452019-12-20 08:45:55 -07003609 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003610 end > 0 ? end : LLONG_MAX,
3611 req->sync.flags & IORING_FSYNC_DATASYNC);
3612 if (ret < 0)
3613 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003614 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003615 return 0;
3616}
3617
Jens Axboed63d1b52019-12-10 10:38:56 -07003618static int io_fallocate_prep(struct io_kiocb *req,
3619 const struct io_uring_sqe *sqe)
3620{
3621 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3622 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003623 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3624 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003625
3626 req->sync.off = READ_ONCE(sqe->off);
3627 req->sync.len = READ_ONCE(sqe->addr);
3628 req->sync.mode = READ_ONCE(sqe->len);
3629 return 0;
3630}
3631
Pavel Begunkov014db002020-03-03 21:33:12 +03003632static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003633{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003634 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003635
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003636 /* fallocate always requiring blocking context */
3637 if (force_nonblock)
3638 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003639 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3640 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003641 if (ret < 0)
3642 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003643 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003644 return 0;
3645}
3646
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003647static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003648{
Jens Axboef8748882020-01-08 17:47:02 -07003649 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003650 int ret;
3651
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003652 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003653 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003654 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003655 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003656
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003657 /* open.how should be already initialised */
3658 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003659 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003660
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003661 req->open.dfd = READ_ONCE(sqe->fd);
3662 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003663 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003664 if (IS_ERR(req->open.filename)) {
3665 ret = PTR_ERR(req->open.filename);
3666 req->open.filename = NULL;
3667 return ret;
3668 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003669 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003670 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003671 return 0;
3672}
3673
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003674static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3675{
3676 u64 flags, mode;
3677
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003678 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3679 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003680 mode = READ_ONCE(sqe->len);
3681 flags = READ_ONCE(sqe->open_flags);
3682 req->open.how = build_open_how(flags, mode);
3683 return __io_openat_prep(req, sqe);
3684}
3685
Jens Axboecebdb982020-01-08 17:59:24 -07003686static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3687{
3688 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003689 size_t len;
3690 int ret;
3691
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003692 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3693 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003694 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3695 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003696 if (len < OPEN_HOW_SIZE_VER0)
3697 return -EINVAL;
3698
3699 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3700 len);
3701 if (ret)
3702 return ret;
3703
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003704 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003705}
3706
Pavel Begunkov014db002020-03-03 21:33:12 +03003707static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003708{
3709 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003710 struct file *file;
3711 int ret;
3712
Jens Axboef86cd202020-01-29 13:46:44 -07003713 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003714 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003715
Jens Axboecebdb982020-01-08 17:59:24 -07003716 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003717 if (ret)
3718 goto err;
3719
Jens Axboe4022e7a2020-03-19 19:23:18 -06003720 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003721 if (ret < 0)
3722 goto err;
3723
3724 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3725 if (IS_ERR(file)) {
3726 put_unused_fd(ret);
3727 ret = PTR_ERR(file);
3728 } else {
3729 fsnotify_open(file);
3730 fd_install(ret, file);
3731 }
3732err:
3733 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003734 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003735 if (ret < 0)
3736 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003737 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003738 return 0;
3739}
3740
Pavel Begunkov014db002020-03-03 21:33:12 +03003741static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003742{
Pavel Begunkov014db002020-03-03 21:33:12 +03003743 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003744}
3745
Jens Axboe067524e2020-03-02 16:32:28 -07003746static int io_remove_buffers_prep(struct io_kiocb *req,
3747 const struct io_uring_sqe *sqe)
3748{
3749 struct io_provide_buf *p = &req->pbuf;
3750 u64 tmp;
3751
3752 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3753 return -EINVAL;
3754
3755 tmp = READ_ONCE(sqe->fd);
3756 if (!tmp || tmp > USHRT_MAX)
3757 return -EINVAL;
3758
3759 memset(p, 0, sizeof(*p));
3760 p->nbufs = tmp;
3761 p->bgid = READ_ONCE(sqe->buf_group);
3762 return 0;
3763}
3764
3765static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3766 int bgid, unsigned nbufs)
3767{
3768 unsigned i = 0;
3769
3770 /* shouldn't happen */
3771 if (!nbufs)
3772 return 0;
3773
3774 /* the head kbuf is the list itself */
3775 while (!list_empty(&buf->list)) {
3776 struct io_buffer *nxt;
3777
3778 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3779 list_del(&nxt->list);
3780 kfree(nxt);
3781 if (++i == nbufs)
3782 return i;
3783 }
3784 i++;
3785 kfree(buf);
3786 idr_remove(&ctx->io_buffer_idr, bgid);
3787
3788 return i;
3789}
3790
Jens Axboe229a7b62020-06-22 10:13:11 -06003791static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3792 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003793{
3794 struct io_provide_buf *p = &req->pbuf;
3795 struct io_ring_ctx *ctx = req->ctx;
3796 struct io_buffer *head;
3797 int ret = 0;
3798
3799 io_ring_submit_lock(ctx, !force_nonblock);
3800
3801 lockdep_assert_held(&ctx->uring_lock);
3802
3803 ret = -ENOENT;
3804 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3805 if (head)
3806 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3807
3808 io_ring_submit_lock(ctx, !force_nonblock);
3809 if (ret < 0)
3810 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003811 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003812 return 0;
3813}
3814
Jens Axboeddf0322d2020-02-23 16:41:33 -07003815static int io_provide_buffers_prep(struct io_kiocb *req,
3816 const struct io_uring_sqe *sqe)
3817{
3818 struct io_provide_buf *p = &req->pbuf;
3819 u64 tmp;
3820
3821 if (sqe->ioprio || sqe->rw_flags)
3822 return -EINVAL;
3823
3824 tmp = READ_ONCE(sqe->fd);
3825 if (!tmp || tmp > USHRT_MAX)
3826 return -E2BIG;
3827 p->nbufs = tmp;
3828 p->addr = READ_ONCE(sqe->addr);
3829 p->len = READ_ONCE(sqe->len);
3830
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003831 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003832 return -EFAULT;
3833
3834 p->bgid = READ_ONCE(sqe->buf_group);
3835 tmp = READ_ONCE(sqe->off);
3836 if (tmp > USHRT_MAX)
3837 return -E2BIG;
3838 p->bid = tmp;
3839 return 0;
3840}
3841
3842static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3843{
3844 struct io_buffer *buf;
3845 u64 addr = pbuf->addr;
3846 int i, bid = pbuf->bid;
3847
3848 for (i = 0; i < pbuf->nbufs; i++) {
3849 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3850 if (!buf)
3851 break;
3852
3853 buf->addr = addr;
3854 buf->len = pbuf->len;
3855 buf->bid = bid;
3856 addr += pbuf->len;
3857 bid++;
3858 if (!*head) {
3859 INIT_LIST_HEAD(&buf->list);
3860 *head = buf;
3861 } else {
3862 list_add_tail(&buf->list, &(*head)->list);
3863 }
3864 }
3865
3866 return i ? i : -ENOMEM;
3867}
3868
Jens Axboe229a7b62020-06-22 10:13:11 -06003869static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3870 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003871{
3872 struct io_provide_buf *p = &req->pbuf;
3873 struct io_ring_ctx *ctx = req->ctx;
3874 struct io_buffer *head, *list;
3875 int ret = 0;
3876
3877 io_ring_submit_lock(ctx, !force_nonblock);
3878
3879 lockdep_assert_held(&ctx->uring_lock);
3880
3881 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3882
3883 ret = io_add_buffers(p, &head);
3884 if (ret < 0)
3885 goto out;
3886
3887 if (!list) {
3888 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3889 GFP_KERNEL);
3890 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003891 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003892 goto out;
3893 }
3894 }
3895out:
3896 io_ring_submit_unlock(ctx, !force_nonblock);
3897 if (ret < 0)
3898 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003899 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003900 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003901}
3902
Jens Axboe3e4827b2020-01-08 15:18:09 -07003903static int io_epoll_ctl_prep(struct io_kiocb *req,
3904 const struct io_uring_sqe *sqe)
3905{
3906#if defined(CONFIG_EPOLL)
3907 if (sqe->ioprio || sqe->buf_index)
3908 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003909 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003910 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003911
3912 req->epoll.epfd = READ_ONCE(sqe->fd);
3913 req->epoll.op = READ_ONCE(sqe->len);
3914 req->epoll.fd = READ_ONCE(sqe->off);
3915
3916 if (ep_op_has_event(req->epoll.op)) {
3917 struct epoll_event __user *ev;
3918
3919 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3920 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3921 return -EFAULT;
3922 }
3923
3924 return 0;
3925#else
3926 return -EOPNOTSUPP;
3927#endif
3928}
3929
Jens Axboe229a7b62020-06-22 10:13:11 -06003930static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3931 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003932{
3933#if defined(CONFIG_EPOLL)
3934 struct io_epoll *ie = &req->epoll;
3935 int ret;
3936
3937 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3938 if (force_nonblock && ret == -EAGAIN)
3939 return -EAGAIN;
3940
3941 if (ret < 0)
3942 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003943 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003944 return 0;
3945#else
3946 return -EOPNOTSUPP;
3947#endif
3948}
3949
Jens Axboec1ca7572019-12-25 22:18:28 -07003950static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3951{
3952#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3953 if (sqe->ioprio || sqe->buf_index || sqe->off)
3954 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003955 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3956 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003957
3958 req->madvise.addr = READ_ONCE(sqe->addr);
3959 req->madvise.len = READ_ONCE(sqe->len);
3960 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3961 return 0;
3962#else
3963 return -EOPNOTSUPP;
3964#endif
3965}
3966
Pavel Begunkov014db002020-03-03 21:33:12 +03003967static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003968{
3969#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3970 struct io_madvise *ma = &req->madvise;
3971 int ret;
3972
3973 if (force_nonblock)
3974 return -EAGAIN;
3975
3976 ret = do_madvise(ma->addr, ma->len, ma->advice);
3977 if (ret < 0)
3978 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003979 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003980 return 0;
3981#else
3982 return -EOPNOTSUPP;
3983#endif
3984}
3985
Jens Axboe4840e412019-12-25 22:03:45 -07003986static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3987{
3988 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3989 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003990 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3991 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003992
3993 req->fadvise.offset = READ_ONCE(sqe->off);
3994 req->fadvise.len = READ_ONCE(sqe->len);
3995 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3996 return 0;
3997}
3998
Pavel Begunkov014db002020-03-03 21:33:12 +03003999static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004000{
4001 struct io_fadvise *fa = &req->fadvise;
4002 int ret;
4003
Jens Axboe3e694262020-02-01 09:22:49 -07004004 if (force_nonblock) {
4005 switch (fa->advice) {
4006 case POSIX_FADV_NORMAL:
4007 case POSIX_FADV_RANDOM:
4008 case POSIX_FADV_SEQUENTIAL:
4009 break;
4010 default:
4011 return -EAGAIN;
4012 }
4013 }
Jens Axboe4840e412019-12-25 22:03:45 -07004014
4015 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4016 if (ret < 0)
4017 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004018 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004019 return 0;
4020}
4021
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004022static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4023{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004024 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004025 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004026 if (sqe->ioprio || sqe->buf_index)
4027 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004028 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004029 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004030
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004031 req->statx.dfd = READ_ONCE(sqe->fd);
4032 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004033 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004034 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4035 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004036
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004037 return 0;
4038}
4039
Pavel Begunkov014db002020-03-03 21:33:12 +03004040static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004041{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004042 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004043 int ret;
4044
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004045 if (force_nonblock) {
4046 /* only need file table for an actual valid fd */
4047 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4048 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004049 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004050 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004051
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004052 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4053 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004054
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004055 if (ret < 0)
4056 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004057 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004058 return 0;
4059}
4060
Jens Axboeb5dba592019-12-11 14:02:38 -07004061static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4062{
4063 /*
4064 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004065 * leave the 'file' in an undeterminate state, and here need to modify
4066 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004067 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004068 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004069 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4070
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004071 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4072 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004073 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4074 sqe->rw_flags || sqe->buf_index)
4075 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004076 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004077 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004078
4079 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004080 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004081 return -EBADF;
4082
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004083 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004084 return 0;
4085}
4086
Jens Axboe229a7b62020-06-22 10:13:11 -06004087static int io_close(struct io_kiocb *req, bool force_nonblock,
4088 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004089{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004090 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004091 int ret;
4092
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004093 /* might be already done during nonblock submission */
4094 if (!close->put_file) {
4095 ret = __close_fd_get_file(close->fd, &close->put_file);
4096 if (ret < 0)
4097 return (ret == -ENOENT) ? -EBADF : ret;
4098 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004099
4100 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004101 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004102 /* was never set, but play safe */
4103 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004104 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004105 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004106 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004107 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004108
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004109 /* No ->flush() or already async, safely close from here */
4110 ret = filp_close(close->put_file, req->work.files);
4111 if (ret < 0)
4112 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004113 fput(close->put_file);
4114 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004115 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004116 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004117}
4118
Jens Axboe3529d8c2019-12-19 18:24:38 -07004119static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004120{
4121 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004122
4123 if (!req->file)
4124 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004125
4126 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4127 return -EINVAL;
4128 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4129 return -EINVAL;
4130
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004131 req->sync.off = READ_ONCE(sqe->off);
4132 req->sync.len = READ_ONCE(sqe->len);
4133 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004134 return 0;
4135}
4136
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004137static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004138{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004139 int ret;
4140
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004141 /* sync_file_range always requires a blocking context */
4142 if (force_nonblock)
4143 return -EAGAIN;
4144
Jens Axboe9adbd452019-12-20 08:45:55 -07004145 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004146 req->sync.flags);
4147 if (ret < 0)
4148 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004149 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004150 return 0;
4151}
4152
YueHaibing469956e2020-03-04 15:53:52 +08004153#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004154static int io_setup_async_msg(struct io_kiocb *req,
4155 struct io_async_msghdr *kmsg)
4156{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004157 struct io_async_msghdr *async_msg = req->async_data;
4158
4159 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004160 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004161 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004162 if (kmsg->iov != kmsg->fast_iov)
4163 kfree(kmsg->iov);
4164 return -ENOMEM;
4165 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004166 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004167 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004168 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004169 return -EAGAIN;
4170}
4171
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004172static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4173 struct io_async_msghdr *iomsg)
4174{
4175 iomsg->iov = iomsg->fast_iov;
4176 iomsg->msg.msg_name = &iomsg->addr;
4177 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4178 req->sr_msg.msg_flags, &iomsg->iov);
4179}
4180
Jens Axboe3529d8c2019-12-19 18:24:38 -07004181static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004182{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004183 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004184 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004185 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004186
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004187 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4188 return -EINVAL;
4189
Jens Axboee47293f2019-12-20 08:58:21 -07004190 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004191 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004192 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004193
Jens Axboed8768362020-02-27 14:17:49 -07004194#ifdef CONFIG_COMPAT
4195 if (req->ctx->compat)
4196 sr->msg_flags |= MSG_CMSG_COMPAT;
4197#endif
4198
Jens Axboee8c2bc12020-08-15 18:44:09 -07004199 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004200 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004201 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004202 if (!ret)
4203 req->flags |= REQ_F_NEED_CLEANUP;
4204 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004205}
4206
Jens Axboe229a7b62020-06-22 10:13:11 -06004207static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4208 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004209{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004210 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004211 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004212 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004213 int ret;
4214
Jens Axboe03b12302019-12-02 18:50:25 -07004215 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004216 if (unlikely(!sock))
4217 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004218
Jens Axboee8c2bc12020-08-15 18:44:09 -07004219 if (req->async_data) {
4220 kmsg = req->async_data;
4221 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004222 /* if iov is set, it's allocated already */
4223 if (!kmsg->iov)
4224 kmsg->iov = kmsg->fast_iov;
4225 kmsg->msg.msg_iter.iov = kmsg->iov;
4226 } else {
4227 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004228 if (ret)
4229 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004230 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004231 }
4232
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004233 flags = req->sr_msg.msg_flags;
4234 if (flags & MSG_DONTWAIT)
4235 req->flags |= REQ_F_NOWAIT;
4236 else if (force_nonblock)
4237 flags |= MSG_DONTWAIT;
4238
4239 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4240 if (force_nonblock && ret == -EAGAIN)
4241 return io_setup_async_msg(req, kmsg);
4242 if (ret == -ERESTARTSYS)
4243 ret = -EINTR;
4244
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004245 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004246 kfree(kmsg->iov);
4247 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004248 if (ret < 0)
4249 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004250 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004251 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004252}
4253
Jens Axboe229a7b62020-06-22 10:13:11 -06004254static int io_send(struct io_kiocb *req, bool force_nonblock,
4255 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004256{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004257 struct io_sr_msg *sr = &req->sr_msg;
4258 struct msghdr msg;
4259 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004260 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004261 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004262 int ret;
4263
4264 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004265 if (unlikely(!sock))
4266 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004267
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004268 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4269 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004270 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004271
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004272 msg.msg_name = NULL;
4273 msg.msg_control = NULL;
4274 msg.msg_controllen = 0;
4275 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004276
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004277 flags = req->sr_msg.msg_flags;
4278 if (flags & MSG_DONTWAIT)
4279 req->flags |= REQ_F_NOWAIT;
4280 else if (force_nonblock)
4281 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004282
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004283 msg.msg_flags = flags;
4284 ret = sock_sendmsg(sock, &msg);
4285 if (force_nonblock && ret == -EAGAIN)
4286 return -EAGAIN;
4287 if (ret == -ERESTARTSYS)
4288 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004289
Jens Axboe03b12302019-12-02 18:50:25 -07004290 if (ret < 0)
4291 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004292 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004293 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004294}
4295
Pavel Begunkov1400e692020-07-12 20:41:05 +03004296static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4297 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004298{
4299 struct io_sr_msg *sr = &req->sr_msg;
4300 struct iovec __user *uiov;
4301 size_t iov_len;
4302 int ret;
4303
Pavel Begunkov1400e692020-07-12 20:41:05 +03004304 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4305 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004306 if (ret)
4307 return ret;
4308
4309 if (req->flags & REQ_F_BUFFER_SELECT) {
4310 if (iov_len > 1)
4311 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004312 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004313 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004314 sr->len = iomsg->iov[0].iov_len;
4315 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004316 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004317 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004318 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004319 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4320 &iomsg->iov, &iomsg->msg.msg_iter,
4321 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004322 if (ret > 0)
4323 ret = 0;
4324 }
4325
4326 return ret;
4327}
4328
4329#ifdef CONFIG_COMPAT
4330static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004331 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004332{
4333 struct compat_msghdr __user *msg_compat;
4334 struct io_sr_msg *sr = &req->sr_msg;
4335 struct compat_iovec __user *uiov;
4336 compat_uptr_t ptr;
4337 compat_size_t len;
4338 int ret;
4339
Pavel Begunkov270a5942020-07-12 20:41:04 +03004340 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004341 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004342 &ptr, &len);
4343 if (ret)
4344 return ret;
4345
4346 uiov = compat_ptr(ptr);
4347 if (req->flags & REQ_F_BUFFER_SELECT) {
4348 compat_ssize_t clen;
4349
4350 if (len > 1)
4351 return -EINVAL;
4352 if (!access_ok(uiov, sizeof(*uiov)))
4353 return -EFAULT;
4354 if (__get_user(clen, &uiov->iov_len))
4355 return -EFAULT;
4356 if (clen < 0)
4357 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004358 sr->len = iomsg->iov[0].iov_len;
4359 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004360 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004361 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4362 UIO_FASTIOV, &iomsg->iov,
4363 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004364 if (ret < 0)
4365 return ret;
4366 }
4367
4368 return 0;
4369}
Jens Axboe03b12302019-12-02 18:50:25 -07004370#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004371
Pavel Begunkov1400e692020-07-12 20:41:05 +03004372static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4373 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004374{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004375 iomsg->msg.msg_name = &iomsg->addr;
4376 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004377
4378#ifdef CONFIG_COMPAT
4379 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004380 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004381#endif
4382
Pavel Begunkov1400e692020-07-12 20:41:05 +03004383 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004384}
4385
Jens Axboebcda7ba2020-02-23 16:42:51 -07004386static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004387 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004388{
4389 struct io_sr_msg *sr = &req->sr_msg;
4390 struct io_buffer *kbuf;
4391
Jens Axboebcda7ba2020-02-23 16:42:51 -07004392 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4393 if (IS_ERR(kbuf))
4394 return kbuf;
4395
4396 sr->kbuf = kbuf;
4397 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004398 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004399}
4400
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004401static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4402{
4403 return io_put_kbuf(req, req->sr_msg.kbuf);
4404}
4405
Jens Axboe3529d8c2019-12-19 18:24:38 -07004406static int io_recvmsg_prep(struct io_kiocb *req,
4407 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004408{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004409 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004410 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004411 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004412
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004413 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4414 return -EINVAL;
4415
Jens Axboe3529d8c2019-12-19 18:24:38 -07004416 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004417 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004418 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004419 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004420
Jens Axboed8768362020-02-27 14:17:49 -07004421#ifdef CONFIG_COMPAT
4422 if (req->ctx->compat)
4423 sr->msg_flags |= MSG_CMSG_COMPAT;
4424#endif
4425
Jens Axboee8c2bc12020-08-15 18:44:09 -07004426 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004427 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004428 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004429 if (!ret)
4430 req->flags |= REQ_F_NEED_CLEANUP;
4431 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004432}
4433
Jens Axboe229a7b62020-06-22 10:13:11 -06004434static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4435 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004436{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004437 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004438 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004439 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004440 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004441 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004442
Jens Axboe0fa03c62019-04-19 13:34:07 -06004443 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004444 if (unlikely(!sock))
4445 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004446
Jens Axboee8c2bc12020-08-15 18:44:09 -07004447 if (req->async_data) {
4448 kmsg = req->async_data;
4449 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004450 /* if iov is set, it's allocated already */
4451 if (!kmsg->iov)
4452 kmsg->iov = kmsg->fast_iov;
4453 kmsg->msg.msg_iter.iov = kmsg->iov;
4454 } else {
4455 ret = io_recvmsg_copy_hdr(req, &iomsg);
4456 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004457 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004458 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004459 }
4460
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004461 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004462 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004463 if (IS_ERR(kbuf))
4464 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004465 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4466 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4467 1, req->sr_msg.len);
4468 }
4469
4470 flags = req->sr_msg.msg_flags;
4471 if (flags & MSG_DONTWAIT)
4472 req->flags |= REQ_F_NOWAIT;
4473 else if (force_nonblock)
4474 flags |= MSG_DONTWAIT;
4475
4476 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4477 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004478 if (force_nonblock && ret == -EAGAIN)
4479 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004480 if (ret == -ERESTARTSYS)
4481 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004482
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004483 if (req->flags & REQ_F_BUFFER_SELECTED)
4484 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004485 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004486 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004487 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004488 if (ret < 0)
4489 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004490 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004491 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004492}
4493
Jens Axboe229a7b62020-06-22 10:13:11 -06004494static int io_recv(struct io_kiocb *req, bool force_nonblock,
4495 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004496{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004497 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004498 struct io_sr_msg *sr = &req->sr_msg;
4499 struct msghdr msg;
4500 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004501 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004502 struct iovec iov;
4503 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004504 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004505
Jens Axboefddafac2020-01-04 20:19:44 -07004506 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004507 if (unlikely(!sock))
4508 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004509
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004510 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004511 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004512 if (IS_ERR(kbuf))
4513 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004514 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004515 }
4516
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004517 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004518 if (unlikely(ret))
4519 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004520
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004521 msg.msg_name = NULL;
4522 msg.msg_control = NULL;
4523 msg.msg_controllen = 0;
4524 msg.msg_namelen = 0;
4525 msg.msg_iocb = NULL;
4526 msg.msg_flags = 0;
4527
4528 flags = req->sr_msg.msg_flags;
4529 if (flags & MSG_DONTWAIT)
4530 req->flags |= REQ_F_NOWAIT;
4531 else if (force_nonblock)
4532 flags |= MSG_DONTWAIT;
4533
4534 ret = sock_recvmsg(sock, &msg, flags);
4535 if (force_nonblock && ret == -EAGAIN)
4536 return -EAGAIN;
4537 if (ret == -ERESTARTSYS)
4538 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004539out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004540 if (req->flags & REQ_F_BUFFER_SELECTED)
4541 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004542 if (ret < 0)
4543 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004544 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004545 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004546}
4547
Jens Axboe3529d8c2019-12-19 18:24:38 -07004548static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004549{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004550 struct io_accept *accept = &req->accept;
4551
Jens Axboe17f2fe32019-10-17 14:42:58 -06004552 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4553 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004554 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004555 return -EINVAL;
4556
Jens Axboed55e5f52019-12-11 16:12:15 -07004557 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4558 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004559 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004560 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004561 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004562}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004563
Jens Axboe229a7b62020-06-22 10:13:11 -06004564static int io_accept(struct io_kiocb *req, bool force_nonblock,
4565 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004566{
4567 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004568 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004569 int ret;
4570
Jiufei Xuee697dee2020-06-10 13:41:59 +08004571 if (req->file->f_flags & O_NONBLOCK)
4572 req->flags |= REQ_F_NOWAIT;
4573
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004574 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004575 accept->addr_len, accept->flags,
4576 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004577 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004578 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004579 if (ret < 0) {
4580 if (ret == -ERESTARTSYS)
4581 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004582 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004583 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004584 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004585 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004586}
4587
Jens Axboe3529d8c2019-12-19 18:24:38 -07004588static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004589{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004590 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004591 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004592
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004593 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4594 return -EINVAL;
4595 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4596 return -EINVAL;
4597
Jens Axboe3529d8c2019-12-19 18:24:38 -07004598 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4599 conn->addr_len = READ_ONCE(sqe->addr2);
4600
4601 if (!io)
4602 return 0;
4603
4604 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004605 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004606}
4607
Jens Axboe229a7b62020-06-22 10:13:11 -06004608static int io_connect(struct io_kiocb *req, bool force_nonblock,
4609 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004610{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004611 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004612 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004613 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004614
Jens Axboee8c2bc12020-08-15 18:44:09 -07004615 if (req->async_data) {
4616 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004617 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004618 ret = move_addr_to_kernel(req->connect.addr,
4619 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004620 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004621 if (ret)
4622 goto out;
4623 io = &__io;
4624 }
4625
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004626 file_flags = force_nonblock ? O_NONBLOCK : 0;
4627
Jens Axboee8c2bc12020-08-15 18:44:09 -07004628 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004629 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004630 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004631 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004632 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004633 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004634 ret = -ENOMEM;
4635 goto out;
4636 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004637 io = req->async_data;
4638 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004639 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004640 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004641 if (ret == -ERESTARTSYS)
4642 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004643out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004644 if (ret < 0)
4645 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004646 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004647 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004648}
YueHaibing469956e2020-03-04 15:53:52 +08004649#else /* !CONFIG_NET */
4650static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4651{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004652 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004653}
4654
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004655static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4656 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004657{
YueHaibing469956e2020-03-04 15:53:52 +08004658 return -EOPNOTSUPP;
4659}
4660
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004661static int io_send(struct io_kiocb *req, bool force_nonblock,
4662 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004663{
4664 return -EOPNOTSUPP;
4665}
4666
4667static int io_recvmsg_prep(struct io_kiocb *req,
4668 const struct io_uring_sqe *sqe)
4669{
4670 return -EOPNOTSUPP;
4671}
4672
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004673static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4674 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004675{
4676 return -EOPNOTSUPP;
4677}
4678
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004679static int io_recv(struct io_kiocb *req, bool force_nonblock,
4680 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004681{
4682 return -EOPNOTSUPP;
4683}
4684
4685static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4686{
4687 return -EOPNOTSUPP;
4688}
4689
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004690static int io_accept(struct io_kiocb *req, bool force_nonblock,
4691 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004692{
4693 return -EOPNOTSUPP;
4694}
4695
4696static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4697{
4698 return -EOPNOTSUPP;
4699}
4700
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004701static int io_connect(struct io_kiocb *req, bool force_nonblock,
4702 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004703{
4704 return -EOPNOTSUPP;
4705}
4706#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004707
Jens Axboed7718a92020-02-14 22:23:12 -07004708struct io_poll_table {
4709 struct poll_table_struct pt;
4710 struct io_kiocb *req;
4711 int error;
4712};
4713
Jens Axboed7718a92020-02-14 22:23:12 -07004714static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4715 __poll_t mask, task_work_func_t func)
4716{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004717 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004718 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004719
4720 /* for instances that support it check for an event match first: */
4721 if (mask && !(mask & poll->events))
4722 return 0;
4723
4724 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4725
4726 list_del_init(&poll->wait.entry);
4727
Jens Axboed7718a92020-02-14 22:23:12 -07004728 req->result = mask;
4729 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004730 percpu_ref_get(&req->ctx->refs);
4731
Jens Axboed7718a92020-02-14 22:23:12 -07004732 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004733 * If we using the signalfd wait_queue_head for this wakeup, then
4734 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4735 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4736 * either, as the normal wakeup will suffice.
4737 */
4738 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4739
4740 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004741 * If this fails, then the task is exiting. When a task exits, the
4742 * work gets canceled, so just cancel this request as well instead
4743 * of executing it. We can't safely execute it anyway, as we may not
4744 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004745 */
Jens Axboe87c43112020-09-30 21:00:14 -06004746 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004747 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004748 struct task_struct *tsk;
4749
Jens Axboee3aabf92020-05-18 11:04:17 -06004750 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004751 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004752 task_work_add(tsk, &req->task_work, 0);
4753 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004754 }
Jens Axboed7718a92020-02-14 22:23:12 -07004755 return 1;
4756}
4757
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004758static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4759 __acquires(&req->ctx->completion_lock)
4760{
4761 struct io_ring_ctx *ctx = req->ctx;
4762
4763 if (!req->result && !READ_ONCE(poll->canceled)) {
4764 struct poll_table_struct pt = { ._key = poll->events };
4765
4766 req->result = vfs_poll(req->file, &pt) & poll->events;
4767 }
4768
4769 spin_lock_irq(&ctx->completion_lock);
4770 if (!req->result && !READ_ONCE(poll->canceled)) {
4771 add_wait_queue(poll->head, &poll->wait);
4772 return true;
4773 }
4774
4775 return false;
4776}
4777
Jens Axboed4e7cd32020-08-15 11:44:50 -07004778static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004779{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004780 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004781 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004782 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004783 return req->apoll->double_poll;
4784}
4785
4786static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4787{
4788 if (req->opcode == IORING_OP_POLL_ADD)
4789 return &req->poll;
4790 return &req->apoll->poll;
4791}
4792
4793static void io_poll_remove_double(struct io_kiocb *req)
4794{
4795 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004796
4797 lockdep_assert_held(&req->ctx->completion_lock);
4798
4799 if (poll && poll->head) {
4800 struct wait_queue_head *head = poll->head;
4801
4802 spin_lock(&head->lock);
4803 list_del_init(&poll->wait.entry);
4804 if (poll->wait.private)
4805 refcount_dec(&req->refs);
4806 poll->head = NULL;
4807 spin_unlock(&head->lock);
4808 }
4809}
4810
4811static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4812{
4813 struct io_ring_ctx *ctx = req->ctx;
4814
Jens Axboed4e7cd32020-08-15 11:44:50 -07004815 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004816 req->poll.done = true;
4817 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4818 io_commit_cqring(ctx);
4819}
4820
4821static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4822{
4823 struct io_ring_ctx *ctx = req->ctx;
4824
4825 if (io_poll_rewait(req, &req->poll)) {
4826 spin_unlock_irq(&ctx->completion_lock);
4827 return;
4828 }
4829
4830 hash_del(&req->hash_node);
4831 io_poll_complete(req, req->result, 0);
Jens Axboe18bceab2020-05-15 11:56:54 -06004832 spin_unlock_irq(&ctx->completion_lock);
4833
Pavel Begunkov6a0af222020-10-13 09:43:58 +01004834 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004835 io_cqring_ev_posted(ctx);
4836}
4837
4838static void io_poll_task_func(struct callback_head *cb)
4839{
4840 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004841 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004842 struct io_kiocb *nxt = NULL;
4843
4844 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004845 if (nxt)
4846 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004847 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004848}
4849
4850static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4851 int sync, void *key)
4852{
4853 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004854 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004855 __poll_t mask = key_to_poll(key);
4856
4857 /* for instances that support it check for an event match first: */
4858 if (mask && !(mask & poll->events))
4859 return 0;
4860
Jens Axboe8706e042020-09-28 08:38:54 -06004861 list_del_init(&wait->entry);
4862
Jens Axboe807abcb2020-07-17 17:09:27 -06004863 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004864 bool done;
4865
Jens Axboe807abcb2020-07-17 17:09:27 -06004866 spin_lock(&poll->head->lock);
4867 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004868 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004869 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004870 /* make sure double remove sees this as being gone */
4871 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004872 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004873 if (!done)
4874 __io_async_wake(req, poll, mask, io_poll_task_func);
4875 }
4876 refcount_dec(&req->refs);
4877 return 1;
4878}
4879
4880static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4881 wait_queue_func_t wake_func)
4882{
4883 poll->head = NULL;
4884 poll->done = false;
4885 poll->canceled = false;
4886 poll->events = events;
4887 INIT_LIST_HEAD(&poll->wait.entry);
4888 init_waitqueue_func_entry(&poll->wait, wake_func);
4889}
4890
4891static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004892 struct wait_queue_head *head,
4893 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004894{
4895 struct io_kiocb *req = pt->req;
4896
4897 /*
4898 * If poll->head is already set, it's because the file being polled
4899 * uses multiple waitqueues for poll handling (eg one for read, one
4900 * for write). Setup a separate io_poll_iocb if this happens.
4901 */
4902 if (unlikely(poll->head)) {
4903 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004904 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004905 pt->error = -EINVAL;
4906 return;
4907 }
4908 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4909 if (!poll) {
4910 pt->error = -ENOMEM;
4911 return;
4912 }
4913 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4914 refcount_inc(&req->refs);
4915 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004916 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004917 }
4918
4919 pt->error = 0;
4920 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004921
4922 if (poll->events & EPOLLEXCLUSIVE)
4923 add_wait_queue_exclusive(head, &poll->wait);
4924 else
4925 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004926}
4927
4928static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4929 struct poll_table_struct *p)
4930{
4931 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004932 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004933
Jens Axboe807abcb2020-07-17 17:09:27 -06004934 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004935}
4936
Jens Axboed7718a92020-02-14 22:23:12 -07004937static void io_async_task_func(struct callback_head *cb)
4938{
4939 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4940 struct async_poll *apoll = req->apoll;
4941 struct io_ring_ctx *ctx = req->ctx;
4942
4943 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4944
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004945 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004946 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004947 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004948 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004949 }
4950
Jens Axboe31067252020-05-17 17:43:31 -06004951 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004952 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004953 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004954
Jens Axboed4e7cd32020-08-15 11:44:50 -07004955 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004956 spin_unlock_irq(&ctx->completion_lock);
4957
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004958 if (!READ_ONCE(apoll->poll.canceled))
4959 __io_req_task_submit(req);
4960 else
4961 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004962
Jens Axboe6d816e02020-08-11 08:04:14 -06004963 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004964 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004965 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004966}
4967
4968static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4969 void *key)
4970{
4971 struct io_kiocb *req = wait->private;
4972 struct io_poll_iocb *poll = &req->apoll->poll;
4973
4974 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4975 key_to_poll(key));
4976
4977 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4978}
4979
4980static void io_poll_req_insert(struct io_kiocb *req)
4981{
4982 struct io_ring_ctx *ctx = req->ctx;
4983 struct hlist_head *list;
4984
4985 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4986 hlist_add_head(&req->hash_node, list);
4987}
4988
4989static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4990 struct io_poll_iocb *poll,
4991 struct io_poll_table *ipt, __poll_t mask,
4992 wait_queue_func_t wake_func)
4993 __acquires(&ctx->completion_lock)
4994{
4995 struct io_ring_ctx *ctx = req->ctx;
4996 bool cancel = false;
4997
Jens Axboe18bceab2020-05-15 11:56:54 -06004998 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004999 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005000 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005001
5002 ipt->pt._key = mask;
5003 ipt->req = req;
5004 ipt->error = -EINVAL;
5005
Jens Axboed7718a92020-02-14 22:23:12 -07005006 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5007
5008 spin_lock_irq(&ctx->completion_lock);
5009 if (likely(poll->head)) {
5010 spin_lock(&poll->head->lock);
5011 if (unlikely(list_empty(&poll->wait.entry))) {
5012 if (ipt->error)
5013 cancel = true;
5014 ipt->error = 0;
5015 mask = 0;
5016 }
5017 if (mask || ipt->error)
5018 list_del_init(&poll->wait.entry);
5019 else if (cancel)
5020 WRITE_ONCE(poll->canceled, true);
5021 else if (!poll->done) /* actually waiting for an event */
5022 io_poll_req_insert(req);
5023 spin_unlock(&poll->head->lock);
5024 }
5025
5026 return mask;
5027}
5028
5029static bool io_arm_poll_handler(struct io_kiocb *req)
5030{
5031 const struct io_op_def *def = &io_op_defs[req->opcode];
5032 struct io_ring_ctx *ctx = req->ctx;
5033 struct async_poll *apoll;
5034 struct io_poll_table ipt;
5035 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005036 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005037
5038 if (!req->file || !file_can_poll(req->file))
5039 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005040 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005041 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005042 if (def->pollin)
5043 rw = READ;
5044 else if (def->pollout)
5045 rw = WRITE;
5046 else
5047 return false;
5048 /* if we can't nonblock try, then no point in arming a poll handler */
5049 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005050 return false;
5051
5052 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5053 if (unlikely(!apoll))
5054 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005055 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005056
5057 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005058 req->apoll = apoll;
5059 INIT_HLIST_NODE(&req->hash_node);
5060
Nathan Chancellor8755d972020-03-02 16:01:19 -07005061 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005062 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005063 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005064 if (def->pollout)
5065 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005066
5067 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5068 if ((req->opcode == IORING_OP_RECVMSG) &&
5069 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5070 mask &= ~POLLIN;
5071
Jens Axboed7718a92020-02-14 22:23:12 -07005072 mask |= POLLERR | POLLPRI;
5073
5074 ipt.pt._qproc = io_async_queue_proc;
5075
5076 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5077 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005078 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005079 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005080 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005081 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005082 kfree(apoll);
5083 return false;
5084 }
5085 spin_unlock_irq(&ctx->completion_lock);
5086 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5087 apoll->poll.events);
5088 return true;
5089}
5090
5091static bool __io_poll_remove_one(struct io_kiocb *req,
5092 struct io_poll_iocb *poll)
5093{
Jens Axboeb41e9852020-02-17 09:52:41 -07005094 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005095
5096 spin_lock(&poll->head->lock);
5097 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005098 if (!list_empty(&poll->wait.entry)) {
5099 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005100 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005101 }
5102 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005103 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005104 return do_complete;
5105}
5106
5107static bool io_poll_remove_one(struct io_kiocb *req)
5108{
5109 bool do_complete;
5110
Jens Axboed4e7cd32020-08-15 11:44:50 -07005111 io_poll_remove_double(req);
5112
Jens Axboed7718a92020-02-14 22:23:12 -07005113 if (req->opcode == IORING_OP_POLL_ADD) {
5114 do_complete = __io_poll_remove_one(req, &req->poll);
5115 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005116 struct async_poll *apoll = req->apoll;
5117
Jens Axboed7718a92020-02-14 22:23:12 -07005118 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005119 do_complete = __io_poll_remove_one(req, &apoll->poll);
5120 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005121 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005122 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005123 kfree(apoll);
5124 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005125 }
5126
Jens Axboeb41e9852020-02-17 09:52:41 -07005127 if (do_complete) {
5128 io_cqring_fill_event(req, -ECANCELED);
5129 io_commit_cqring(req->ctx);
5130 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005131 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005132 io_put_req(req);
5133 }
5134
5135 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005136}
5137
Jens Axboe76e1b642020-09-26 15:05:03 -06005138/*
5139 * Returns true if we found and killed one or more poll requests
5140 */
5141static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005142{
Jens Axboe78076bb2019-12-04 19:56:40 -07005143 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005144 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005145 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005146
5147 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005148 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5149 struct hlist_head *list;
5150
5151 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005152 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5153 if (io_task_match(req, tsk))
5154 posted += io_poll_remove_one(req);
5155 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005156 }
5157 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005158
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005159 if (posted)
5160 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005161
5162 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005163}
5164
Jens Axboe47f46762019-11-09 17:43:02 -07005165static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5166{
Jens Axboe78076bb2019-12-04 19:56:40 -07005167 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005168 struct io_kiocb *req;
5169
Jens Axboe78076bb2019-12-04 19:56:40 -07005170 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5171 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005172 if (sqe_addr != req->user_data)
5173 continue;
5174 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005175 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005176 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005177 }
5178
5179 return -ENOENT;
5180}
5181
Jens Axboe3529d8c2019-12-19 18:24:38 -07005182static int io_poll_remove_prep(struct io_kiocb *req,
5183 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005184{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005185 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5186 return -EINVAL;
5187 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5188 sqe->poll_events)
5189 return -EINVAL;
5190
Jens Axboe0969e782019-12-17 18:40:57 -07005191 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005192 return 0;
5193}
5194
5195/*
5196 * Find a running poll command that matches one specified in sqe->addr,
5197 * and remove it if found.
5198 */
5199static int io_poll_remove(struct io_kiocb *req)
5200{
5201 struct io_ring_ctx *ctx = req->ctx;
5202 u64 addr;
5203 int ret;
5204
Jens Axboe0969e782019-12-17 18:40:57 -07005205 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005206 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005207 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005208 spin_unlock_irq(&ctx->completion_lock);
5209
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005210 if (ret < 0)
5211 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005212 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005213 return 0;
5214}
5215
Jens Axboe221c5eb2019-01-17 09:41:58 -07005216static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5217 void *key)
5218{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005219 struct io_kiocb *req = wait->private;
5220 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005221
Jens Axboed7718a92020-02-14 22:23:12 -07005222 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005223}
5224
Jens Axboe221c5eb2019-01-17 09:41:58 -07005225static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5226 struct poll_table_struct *p)
5227{
5228 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5229
Jens Axboee8c2bc12020-08-15 18:44:09 -07005230 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005231}
5232
Jens Axboe3529d8c2019-12-19 18:24:38 -07005233static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005234{
5235 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005236 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005237
5238 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5239 return -EINVAL;
5240 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5241 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005242 if (!poll->file)
5243 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005244
Jiufei Xue5769a352020-06-17 17:53:55 +08005245 events = READ_ONCE(sqe->poll32_events);
5246#ifdef __BIG_ENDIAN
5247 events = swahw32(events);
5248#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005249 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5250 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005251 return 0;
5252}
5253
Pavel Begunkov014db002020-03-03 21:33:12 +03005254static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005255{
5256 struct io_poll_iocb *poll = &req->poll;
5257 struct io_ring_ctx *ctx = req->ctx;
5258 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005259 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005260
Jens Axboe78076bb2019-12-04 19:56:40 -07005261 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005262 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005263
Jens Axboed7718a92020-02-14 22:23:12 -07005264 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5265 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005266
Jens Axboe8c838782019-03-12 15:48:16 -06005267 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005268 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005269 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005270 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005271 spin_unlock_irq(&ctx->completion_lock);
5272
Jens Axboe8c838782019-03-12 15:48:16 -06005273 if (mask) {
5274 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005275 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005276 }
Jens Axboe8c838782019-03-12 15:48:16 -06005277 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005278}
5279
Jens Axboe5262f562019-09-17 12:26:57 -06005280static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5281{
Jens Axboead8a48a2019-11-15 08:49:11 -07005282 struct io_timeout_data *data = container_of(timer,
5283 struct io_timeout_data, timer);
5284 struct io_kiocb *req = data->req;
5285 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005286 unsigned long flags;
5287
Jens Axboe5262f562019-09-17 12:26:57 -06005288 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005289 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005290 atomic_set(&req->ctx->cq_timeouts,
5291 atomic_read(&req->ctx->cq_timeouts) + 1);
5292
Jens Axboe78e19bb2019-11-06 15:21:34 -07005293 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005294 io_commit_cqring(ctx);
5295 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5296
5297 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005298 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005299 io_put_req(req);
5300 return HRTIMER_NORESTART;
5301}
5302
Jens Axboef254ac02020-08-12 17:33:30 -06005303static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005304{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005305 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005306 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005307
Jens Axboee8c2bc12020-08-15 18:44:09 -07005308 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005309 if (ret == -1)
5310 return -EALREADY;
Pavel Begunkova71976f2020-10-10 18:34:11 +01005311 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005312
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005313 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005314 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005315 io_cqring_fill_event(req, -ECANCELED);
5316 io_put_req(req);
5317 return 0;
5318}
5319
Jens Axboef254ac02020-08-12 17:33:30 -06005320static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5321{
5322 struct io_kiocb *req;
5323 int ret = -ENOENT;
5324
5325 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5326 if (user_data == req->user_data) {
5327 ret = 0;
5328 break;
5329 }
5330 }
5331
5332 if (ret == -ENOENT)
5333 return ret;
5334
5335 return __io_timeout_cancel(req);
5336}
5337
Jens Axboe3529d8c2019-12-19 18:24:38 -07005338static int io_timeout_remove_prep(struct io_kiocb *req,
5339 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005340{
Jens Axboeb29472e2019-12-17 18:50:29 -07005341 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5342 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005343 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5344 return -EINVAL;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005345 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags)
Jens Axboeb29472e2019-12-17 18:50:29 -07005346 return -EINVAL;
5347
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005348 req->timeout_rem.addr = READ_ONCE(sqe->addr);
Jens Axboeb29472e2019-12-17 18:50:29 -07005349 return 0;
5350}
5351
Jens Axboe11365042019-10-16 09:08:32 -06005352/*
5353 * Remove or update an existing timeout command
5354 */
Jens Axboefc4df992019-12-10 14:38:45 -07005355static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005356{
5357 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005358 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005359
Jens Axboe11365042019-10-16 09:08:32 -06005360 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005361 ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005362
Jens Axboe47f46762019-11-09 17:43:02 -07005363 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005364 io_commit_cqring(ctx);
5365 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005366 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005367 if (ret < 0)
5368 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005369 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005370 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005371}
5372
Jens Axboe3529d8c2019-12-19 18:24:38 -07005373static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005374 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005375{
Jens Axboead8a48a2019-11-15 08:49:11 -07005376 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005377 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005378 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005379
Jens Axboead8a48a2019-11-15 08:49:11 -07005380 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005381 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005382 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005383 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005384 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005385 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005386 flags = READ_ONCE(sqe->timeout_flags);
5387 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005388 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005389
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005390 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005391
Jens Axboee8c2bc12020-08-15 18:44:09 -07005392 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005393 return -ENOMEM;
5394
Jens Axboee8c2bc12020-08-15 18:44:09 -07005395 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005396 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005397
5398 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005399 return -EFAULT;
5400
Jens Axboe11365042019-10-16 09:08:32 -06005401 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005402 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005403 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005404 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005405
Jens Axboead8a48a2019-11-15 08:49:11 -07005406 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5407 return 0;
5408}
5409
Jens Axboefc4df992019-12-10 14:38:45 -07005410static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005411{
Jens Axboead8a48a2019-11-15 08:49:11 -07005412 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005413 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005414 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005415 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005416
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005417 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005418
Jens Axboe5262f562019-09-17 12:26:57 -06005419 /*
5420 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005421 * timeout event to be satisfied. If it isn't set, then this is
5422 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005423 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005424 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005425 entry = ctx->timeout_list.prev;
5426 goto add;
5427 }
Jens Axboe5262f562019-09-17 12:26:57 -06005428
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005429 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5430 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005431
5432 /*
5433 * Insertion sort, ensuring the first entry in the list is always
5434 * the one we need first.
5435 */
Jens Axboe5262f562019-09-17 12:26:57 -06005436 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005437 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5438 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005439
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005440 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005441 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005442 /* nxt.seq is behind @tail, otherwise would've been completed */
5443 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005444 break;
5445 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005446add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005447 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005448 data->timer.function = io_timeout_fn;
5449 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005450 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005451 return 0;
5452}
5453
Jens Axboe62755e32019-10-28 21:49:21 -06005454static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005455{
Jens Axboe62755e32019-10-28 21:49:21 -06005456 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005457
Jens Axboe62755e32019-10-28 21:49:21 -06005458 return req->user_data == (unsigned long) data;
5459}
5460
Jens Axboee977d6d2019-11-05 12:39:45 -07005461static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005462{
Jens Axboe62755e32019-10-28 21:49:21 -06005463 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005464 int ret = 0;
5465
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005466 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005467 switch (cancel_ret) {
5468 case IO_WQ_CANCEL_OK:
5469 ret = 0;
5470 break;
5471 case IO_WQ_CANCEL_RUNNING:
5472 ret = -EALREADY;
5473 break;
5474 case IO_WQ_CANCEL_NOTFOUND:
5475 ret = -ENOENT;
5476 break;
5477 }
5478
Jens Axboee977d6d2019-11-05 12:39:45 -07005479 return ret;
5480}
5481
Jens Axboe47f46762019-11-09 17:43:02 -07005482static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5483 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005484 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005485{
5486 unsigned long flags;
5487 int ret;
5488
5489 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5490 if (ret != -ENOENT) {
5491 spin_lock_irqsave(&ctx->completion_lock, flags);
5492 goto done;
5493 }
5494
5495 spin_lock_irqsave(&ctx->completion_lock, flags);
5496 ret = io_timeout_cancel(ctx, sqe_addr);
5497 if (ret != -ENOENT)
5498 goto done;
5499 ret = io_poll_cancel(ctx, sqe_addr);
5500done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005501 if (!ret)
5502 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005503 io_cqring_fill_event(req, ret);
5504 io_commit_cqring(ctx);
5505 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5506 io_cqring_ev_posted(ctx);
5507
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005508 if (ret < 0)
5509 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005510 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005511}
5512
Jens Axboe3529d8c2019-12-19 18:24:38 -07005513static int io_async_cancel_prep(struct io_kiocb *req,
5514 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005515{
Jens Axboefbf23842019-12-17 18:45:56 -07005516 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005517 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005518 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5519 return -EINVAL;
5520 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005521 return -EINVAL;
5522
Jens Axboefbf23842019-12-17 18:45:56 -07005523 req->cancel.addr = READ_ONCE(sqe->addr);
5524 return 0;
5525}
5526
Pavel Begunkov014db002020-03-03 21:33:12 +03005527static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005528{
5529 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005530
Pavel Begunkov014db002020-03-03 21:33:12 +03005531 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005532 return 0;
5533}
5534
Jens Axboe05f3fb32019-12-09 11:22:50 -07005535static int io_files_update_prep(struct io_kiocb *req,
5536 const struct io_uring_sqe *sqe)
5537{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005538 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5539 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005540 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5541 return -EINVAL;
5542 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005543 return -EINVAL;
5544
5545 req->files_update.offset = READ_ONCE(sqe->off);
5546 req->files_update.nr_args = READ_ONCE(sqe->len);
5547 if (!req->files_update.nr_args)
5548 return -EINVAL;
5549 req->files_update.arg = READ_ONCE(sqe->addr);
5550 return 0;
5551}
5552
Jens Axboe229a7b62020-06-22 10:13:11 -06005553static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5554 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005555{
5556 struct io_ring_ctx *ctx = req->ctx;
5557 struct io_uring_files_update up;
5558 int ret;
5559
Jens Axboef86cd202020-01-29 13:46:44 -07005560 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005561 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005562
5563 up.offset = req->files_update.offset;
5564 up.fds = req->files_update.arg;
5565
5566 mutex_lock(&ctx->uring_lock);
5567 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5568 mutex_unlock(&ctx->uring_lock);
5569
5570 if (ret < 0)
5571 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005572 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005573 return 0;
5574}
5575
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005576static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005577{
Jens Axboed625c6e2019-12-17 19:53:05 -07005578 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005579 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005580 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005581 case IORING_OP_READV:
5582 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005583 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005584 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005585 case IORING_OP_WRITEV:
5586 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005587 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005588 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005589 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005590 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005591 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005592 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005593 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005594 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005595 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005596 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005597 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005598 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005599 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005600 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005601 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005602 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005603 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005604 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005605 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005606 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005607 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005608 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005609 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005610 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005611 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005612 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005613 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005614 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005615 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005616 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005617 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005618 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005619 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005620 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005621 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005622 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005623 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005624 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005625 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005626 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005627 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005628 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005629 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005630 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005631 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005632 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005633 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005634 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005635 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005636 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005637 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005638 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005639 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005640 return io_tee_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005641 }
5642
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005643 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5644 req->opcode);
5645 return-EINVAL;
5646}
5647
Jens Axboedef596e2019-01-09 08:59:42 -07005648static int io_req_defer_prep(struct io_kiocb *req,
5649 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07005650{
Jens Axboedef596e2019-01-09 08:59:42 -07005651 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005652 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005653 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005654 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005655 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005656}
5657
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005658static u32 io_get_sequence(struct io_kiocb *req)
5659{
5660 struct io_kiocb *pos;
5661 struct io_ring_ctx *ctx = req->ctx;
5662 u32 total_submitted, nr_reqs = 1;
5663
5664 if (req->flags & REQ_F_LINK_HEAD)
5665 list_for_each_entry(pos, &req->link_list, link_list)
5666 nr_reqs++;
5667
5668 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5669 return total_submitted - nr_reqs;
5670}
5671
Jens Axboe3529d8c2019-12-19 18:24:38 -07005672static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005673{
5674 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005675 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005676 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005677 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005678
5679 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005680 if (likely(list_empty_careful(&ctx->defer_list) &&
5681 !(req->flags & REQ_F_IO_DRAIN)))
5682 return 0;
5683
5684 seq = io_get_sequence(req);
5685 /* Still a chance to pass the sequence check */
5686 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005687 return 0;
5688
Jens Axboee8c2bc12020-08-15 18:44:09 -07005689 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005690 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005691 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005692 return ret;
5693 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005694 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005695 de = kmalloc(sizeof(*de), GFP_KERNEL);
5696 if (!de)
5697 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005698
5699 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005700 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005701 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005702 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005703 io_queue_async_work(req);
5704 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005705 }
5706
5707 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005708 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005709 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005710 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005711 spin_unlock_irq(&ctx->completion_lock);
5712 return -EIOCBQUEUED;
5713}
Jens Axboeedafcce2019-01-09 09:16:05 -07005714
Jens Axboef573d382020-09-22 10:19:24 -06005715static void io_req_drop_files(struct io_kiocb *req)
5716{
5717 struct io_ring_ctx *ctx = req->ctx;
5718 unsigned long flags;
5719
5720 spin_lock_irqsave(&ctx->inflight_lock, flags);
5721 list_del(&req->inflight_entry);
5722 if (waitqueue_active(&ctx->inflight_wait))
5723 wake_up(&ctx->inflight_wait);
5724 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5725 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005726 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005727 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005728 req->work.files = NULL;
5729}
5730
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005731static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005732{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005733 if (req->flags & REQ_F_BUFFER_SELECTED) {
5734 switch (req->opcode) {
5735 case IORING_OP_READV:
5736 case IORING_OP_READ_FIXED:
5737 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005738 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005739 break;
5740 case IORING_OP_RECVMSG:
5741 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005742 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005743 break;
5744 }
5745 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005746 }
5747
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005748 if (req->flags & REQ_F_NEED_CLEANUP) {
5749 switch (req->opcode) {
5750 case IORING_OP_READV:
5751 case IORING_OP_READ_FIXED:
5752 case IORING_OP_READ:
5753 case IORING_OP_WRITEV:
5754 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005755 case IORING_OP_WRITE: {
5756 struct io_async_rw *io = req->async_data;
5757 if (io->free_iovec)
5758 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005759 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005760 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005761 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005762 case IORING_OP_SENDMSG: {
5763 struct io_async_msghdr *io = req->async_data;
5764 if (io->iov != io->fast_iov)
5765 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005766 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005767 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005768 case IORING_OP_SPLICE:
5769 case IORING_OP_TEE:
5770 io_put_file(req, req->splice.file_in,
5771 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5772 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005773 case IORING_OP_OPENAT:
5774 case IORING_OP_OPENAT2:
5775 if (req->open.filename)
5776 putname(req->open.filename);
5777 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005778 }
5779 req->flags &= ~REQ_F_NEED_CLEANUP;
5780 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005781
Jens Axboef573d382020-09-22 10:19:24 -06005782 if (req->flags & REQ_F_INFLIGHT)
5783 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005784}
5785
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005786static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
5787 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07005788{
Jens Axboeedafcce2019-01-09 09:16:05 -07005789 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005790 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005791
Jens Axboed625c6e2019-12-17 19:53:05 -07005792 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005793 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005794 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005795 break;
5796 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005797 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005798 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06005799 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005800 break;
5801 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005802 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005803 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06005804 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005805 break;
5806 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03005807 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005808 break;
5809 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03005810 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005811 break;
5812 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07005813 ret = io_poll_remove(req);
5814 break;
5815 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005816 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07005817 break;
5818 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005819 ret = io_sendmsg(req, force_nonblock, cs);
5820 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005821 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005822 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07005823 break;
5824 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005825 ret = io_recvmsg(req, force_nonblock, cs);
5826 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005827 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005828 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005829 break;
5830 case IORING_OP_TIMEOUT:
5831 ret = io_timeout(req);
5832 break;
5833 case IORING_OP_TIMEOUT_REMOVE:
5834 ret = io_timeout_remove(req);
5835 break;
5836 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005837 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005838 break;
5839 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005840 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005841 break;
5842 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03005843 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005844 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005845 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005846 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005847 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005848 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03005849 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005850 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005851 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06005852 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005853 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005854 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06005855 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005856 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005857 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03005858 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005859 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005860 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005861 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005862 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005863 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005864 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005865 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005866 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03005867 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005868 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005869 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06005870 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005871 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005872 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005873 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005874 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005875 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06005876 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005877 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005878 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06005879 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005880 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005881 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005882 ret = io_tee(req, force_nonblock);
5883 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005884 default:
5885 ret = -EINVAL;
5886 break;
Jens Axboe31b51512019-01-18 22:56:34 -07005887 }
5888
5889 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07005890 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005891
Jens Axboeb5325762020-05-19 21:20:27 -06005892 /* If the op doesn't have a file, we're not polling for it */
5893 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005894 const bool in_async = io_wq_current_is_worker();
5895
Jens Axboe11ba8202020-01-15 21:51:17 -07005896 /* workqueue context doesn't hold uring_lock, grab it now */
5897 if (in_async)
5898 mutex_lock(&ctx->uring_lock);
5899
Jens Axboe2b188cc2019-01-07 10:46:33 -07005900 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005901
5902 if (in_async)
5903 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005904 }
5905
5906 return 0;
5907}
5908
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005909static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005910{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005911 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005912 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005913 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005914
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005915 timeout = io_prep_linked_timeout(req);
5916 if (timeout)
5917 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005918
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005919 /* if NO_CANCEL is set, we must still run the work */
5920 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5921 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005922 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005923 }
Jens Axboe31b51512019-01-18 22:56:34 -07005924
Jens Axboe561fb042019-10-24 07:25:42 -06005925 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005926 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005927 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005928 /*
5929 * We can get EAGAIN for polled IO even though we're
5930 * forcing a sync submission from here, since we can't
5931 * wait for request slots on the block side.
5932 */
5933 if (ret != -EAGAIN)
5934 break;
5935 cond_resched();
5936 } while (1);
5937 }
Jens Axboe31b51512019-01-18 22:56:34 -07005938
Jens Axboe561fb042019-10-24 07:25:42 -06005939 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005940 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005941 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005942 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005943
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005944 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005945}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005946
Jens Axboe65e19f52019-10-26 07:20:21 -06005947static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5948 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005949{
Jens Axboe65e19f52019-10-26 07:20:21 -06005950 struct fixed_file_table *table;
5951
Jens Axboe05f3fb32019-12-09 11:22:50 -07005952 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005953 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005954}
5955
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005956static struct file *io_file_get(struct io_submit_state *state,
5957 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005958{
5959 struct io_ring_ctx *ctx = req->ctx;
5960 struct file *file;
5961
5962 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01005963 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005964 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005965 fd = array_index_nospec(fd, ctx->nr_user_files);
5966 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005967 if (file) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01005968 req->fixed_file_refs = &ctx->file_data->node->refs;
Jens Axboefd2206e2020-06-02 16:40:47 -06005969 percpu_ref_get(req->fixed_file_refs);
5970 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005971 } else {
5972 trace_io_uring_file_get(ctx, fd);
5973 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005974 }
5975
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005976 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005977}
5978
Jens Axboe3529d8c2019-12-19 18:24:38 -07005979static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005980 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005981{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005982 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005983
Jens Axboe63ff8222020-05-07 14:56:15 -06005984 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005985 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005986 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005987
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005988 req->file = io_file_get(state, req, fd, fixed);
5989 if (req->file || io_op_defs[req->opcode].needs_file_no_error)
Jens Axboef86cd202020-01-29 13:46:44 -07005990 return 0;
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005991 return -EBADF;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005992}
5993
Jens Axboe2665abf2019-11-05 12:40:47 -07005994static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5995{
Jens Axboead8a48a2019-11-15 08:49:11 -07005996 struct io_timeout_data *data = container_of(timer,
5997 struct io_timeout_data, timer);
5998 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005999 struct io_ring_ctx *ctx = req->ctx;
6000 struct io_kiocb *prev = NULL;
6001 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006002
6003 spin_lock_irqsave(&ctx->completion_lock, flags);
6004
6005 /*
6006 * We don't expect the list to be empty, that will only happen if we
6007 * race with the completion of the linked work.
6008 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006009 if (!list_empty(&req->link_list)) {
6010 prev = list_entry(req->link_list.prev, struct io_kiocb,
6011 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006012 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006013 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006014 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6015 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006016 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006017 }
6018
6019 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6020
6021 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006022 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006023 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006024 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006025 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006026 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006027 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006028 return HRTIMER_NORESTART;
6029}
6030
Jens Axboe7271ef32020-08-10 09:55:22 -06006031static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006032{
Jens Axboe76a46e02019-11-10 23:34:16 -07006033 /*
6034 * If the list is now empty, then our linked request finished before
6035 * we got a chance to setup the timer
6036 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006037 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006038 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006039
Jens Axboead8a48a2019-11-15 08:49:11 -07006040 data->timer.function = io_link_timeout_fn;
6041 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6042 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006043 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006044}
6045
6046static void io_queue_linked_timeout(struct io_kiocb *req)
6047{
6048 struct io_ring_ctx *ctx = req->ctx;
6049
6050 spin_lock_irq(&ctx->completion_lock);
6051 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006052 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006053
Jens Axboe2665abf2019-11-05 12:40:47 -07006054 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006055 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006056}
6057
Jens Axboead8a48a2019-11-15 08:49:11 -07006058static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006059{
6060 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006061
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006062 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006063 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006064 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006065 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006066
Pavel Begunkov44932332019-12-05 16:16:35 +03006067 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6068 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006069 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006070 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006071
Jens Axboe76a46e02019-11-10 23:34:16 -07006072 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006073 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006074}
6075
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006076static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006077{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006078 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006079 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006080 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006081 int ret;
6082
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006083again:
6084 linked_timeout = io_prep_linked_timeout(req);
6085
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006086 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6087 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006088 if (old_creds)
6089 revert_creds(old_creds);
6090 if (old_creds == req->work.creds)
6091 old_creds = NULL; /* restored original creds */
6092 else
6093 old_creds = override_creds(req->work.creds);
6094 }
6095
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006096 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006097
6098 /*
6099 * We async punt it if the file wasn't marked NOWAIT, or if the file
6100 * doesn't support non-blocking read/write attempts
6101 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006102 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006103 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006104punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006105 /*
6106 * Queued up for async execution, worker will release
6107 * submit reference when the iocb is actually submitted.
6108 */
6109 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006111
Pavel Begunkovf063c542020-07-25 14:41:59 +03006112 if (linked_timeout)
6113 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006114 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006115 }
Jens Axboee65ef562019-03-12 10:16:44 -06006116
Pavel Begunkov652532a2020-07-03 22:15:07 +03006117 if (unlikely(ret)) {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006118 /* un-prep timeout, so it'll be killed as any other linked */
6119 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006120 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006121 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006122 io_req_complete(req, ret);
6123 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006124 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006125
Jens Axboe6c271ce2019-01-10 11:22:30 -07006126 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006127 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006128 if (linked_timeout)
6129 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006130
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006131 if (nxt) {
6132 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006133
6134 if (req->flags & REQ_F_FORCE_ASYNC)
6135 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006136 goto again;
6137 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006138exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006139 if (old_creds)
6140 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006141}
6142
Jens Axboef13fad72020-06-22 09:34:30 -06006143static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6144 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006145{
6146 int ret;
6147
Jens Axboe3529d8c2019-12-19 18:24:38 -07006148 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006149 if (ret) {
6150 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006151fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006152 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006153 io_put_req(req);
6154 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006155 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006156 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006157 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006158 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006159 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006160 goto fail_req;
6161 }
6162
Jens Axboece35a472019-12-17 08:04:44 -07006163 /*
6164 * Never try inline submit of IOSQE_ASYNC is set, go straight
6165 * to async execution.
6166 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006167 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006168 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6169 io_queue_async_work(req);
6170 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006171 if (sqe) {
6172 ret = io_req_prep(req, sqe);
6173 if (unlikely(ret))
6174 goto fail_req;
6175 }
6176 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006177 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006178}
6179
Jens Axboef13fad72020-06-22 09:34:30 -06006180static inline void io_queue_link_head(struct io_kiocb *req,
6181 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006182{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006183 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006184 io_put_req(req);
6185 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006186 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006187 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006188}
6189
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006190static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006191 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006192{
Jackie Liua197f662019-11-08 08:09:12 -07006193 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006194 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006195
Jens Axboe9e645e112019-05-10 16:07:28 -06006196 /*
6197 * If we already have a head request, queue this one for async
6198 * submittal once the head completes. If we don't have a head but
6199 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6200 * submitted sync once the chain is complete. If none of those
6201 * conditions are true (normal request), then just queue it.
6202 */
6203 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006204 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006205
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006206 /*
6207 * Taking sequential execution of a link, draining both sides
6208 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6209 * requests in the link. So, it drains the head and the
6210 * next after the link request. The last one is done via
6211 * drain_next flag to persist the effect across calls.
6212 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006213 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006214 head->flags |= REQ_F_IO_DRAIN;
6215 ctx->drain_next = 1;
6216 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006217 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006218 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006219 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006220 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006221 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006222 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006223 trace_io_uring_link(ctx, req, head);
6224 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006225
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006226 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006227 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006228 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006229 *link = NULL;
6230 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006231 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006232 if (unlikely(ctx->drain_next)) {
6233 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006234 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006235 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006236 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006237 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006238 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006239
Pavel Begunkov711be032020-01-17 03:57:59 +03006240 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006241 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006242 req->flags |= REQ_F_FAIL_LINK;
6243 *link = req;
6244 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006245 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006246 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006247 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006248
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006249 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006250}
6251
Jens Axboe9a56a232019-01-09 09:06:50 -07006252/*
6253 * Batched submission is done, ensure local IO is flushed out.
6254 */
6255static void io_submit_state_end(struct io_submit_state *state)
6256{
Jens Axboef13fad72020-06-22 09:34:30 -06006257 if (!list_empty(&state->comp.list))
6258 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006259 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006260 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006261 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006262 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006263}
6264
6265/*
6266 * Start submission side cache.
6267 */
6268static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006269 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006270{
6271 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006272 state->comp.nr = 0;
6273 INIT_LIST_HEAD(&state->comp.list);
6274 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006275 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006276 state->file = NULL;
6277 state->ios_left = max_ios;
6278}
6279
Jens Axboe2b188cc2019-01-07 10:46:33 -07006280static void io_commit_sqring(struct io_ring_ctx *ctx)
6281{
Hristo Venev75b28af2019-08-26 17:23:46 +00006282 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006283
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006284 /*
6285 * Ensure any loads from the SQEs are done at this point,
6286 * since once we write the new head, the application could
6287 * write new data to them.
6288 */
6289 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290}
6291
6292/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006293 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006294 * that is mapped by userspace. This means that care needs to be taken to
6295 * ensure that reads are stable, as we cannot rely on userspace always
6296 * being a good citizen. If members of the sqe are validated and then later
6297 * used, it's important that those reads are done through READ_ONCE() to
6298 * prevent a re-load down the line.
6299 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006300static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301{
Hristo Venev75b28af2019-08-26 17:23:46 +00006302 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303 unsigned head;
6304
6305 /*
6306 * The cached sq head (or cq tail) serves two purposes:
6307 *
6308 * 1) allows us to batch the cost of updating the user visible
6309 * head updates.
6310 * 2) allows the kernel side to track the head on its own, even
6311 * though the application is the one updating it.
6312 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006313 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006314 if (likely(head < ctx->sq_entries))
6315 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316
6317 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006318 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006319 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006320 return NULL;
6321}
6322
6323static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6324{
6325 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006326}
6327
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006328/*
6329 * Check SQE restrictions (opcode and flags).
6330 *
6331 * Returns 'true' if SQE is allowed, 'false' otherwise.
6332 */
6333static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6334 struct io_kiocb *req,
6335 unsigned int sqe_flags)
6336{
6337 if (!ctx->restricted)
6338 return true;
6339
6340 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6341 return false;
6342
6343 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6344 ctx->restrictions.sqe_flags_required)
6345 return false;
6346
6347 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6348 ctx->restrictions.sqe_flags_required))
6349 return false;
6350
6351 return true;
6352}
6353
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006354#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6355 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6356 IOSQE_BUFFER_SELECT)
6357
6358static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6359 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006360 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006361{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006362 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006363 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006364
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006365 req->opcode = READ_ONCE(sqe->opcode);
6366 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006367 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006368 req->file = NULL;
6369 req->ctx = ctx;
6370 req->flags = 0;
6371 /* one is dropped after submission, the other at completion */
6372 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006373 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006374 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006375
6376 if (unlikely(req->opcode >= IORING_OP_LAST))
6377 return -EINVAL;
6378
Jens Axboe9d8426a2020-06-16 18:42:49 -06006379 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6380 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006381
6382 sqe_flags = READ_ONCE(sqe->flags);
6383 /* enforce forwards compatibility on users */
6384 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6385 return -EINVAL;
6386
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006387 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6388 return -EACCES;
6389
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006390 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6391 !io_op_defs[req->opcode].buffer_select)
6392 return -EOPNOTSUPP;
6393
6394 id = READ_ONCE(sqe->personality);
6395 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006396 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006397 req->work.creds = idr_find(&ctx->personality_idr, id);
6398 if (unlikely(!req->work.creds))
6399 return -EINVAL;
6400 get_cred(req->work.creds);
6401 }
6402
6403 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006404 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006405
Jens Axboe63ff8222020-05-07 14:56:15 -06006406 if (!io_op_defs[req->opcode].needs_file)
6407 return 0;
6408
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006409 ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
6410 state->ios_left--;
6411 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006412}
6413
Jens Axboe0f212202020-09-13 13:09:39 -06006414static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006415{
Jens Axboeac8691c2020-06-01 08:30:41 -06006416 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006417 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006418 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006419
Jens Axboec4a2ed72019-11-21 21:01:26 -07006420 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006421 if (test_bit(0, &ctx->sq_check_overflow)) {
6422 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006423 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006424 return -EBUSY;
6425 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006426
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006427 /* make sure SQ entry isn't read before tail */
6428 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006429
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006430 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6431 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006432
Jens Axboefaf7b512020-10-07 12:48:53 -06006433 atomic_long_add(nr, &current->io_uring->req_issue);
6434 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006435
Jens Axboe6c271ce2019-01-10 11:22:30 -07006436 io_submit_state_start(&state, ctx, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006437
Jens Axboe6c271ce2019-01-10 11:22:30 -07006438 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006439 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006440 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006441 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006442
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006443 sqe = io_get_sqe(ctx);
6444 if (unlikely(!sqe)) {
6445 io_consume_sqe(ctx);
6446 break;
6447 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006448 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006449 if (unlikely(!req)) {
6450 if (!submitted)
6451 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006452 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006453 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006454 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006455 /* will complete beyond this point, count as submitted */
6456 submitted++;
6457
Pavel Begunkov692d8362020-10-10 18:34:13 +01006458 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006459 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006460fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006461 io_put_req(req);
6462 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006463 break;
6464 }
6465
Jens Axboe354420f2020-01-08 18:55:15 -07006466 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006467 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006468 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006469 if (err)
6470 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006471 }
6472
Pavel Begunkov9466f432020-01-25 22:34:01 +03006473 if (unlikely(submitted != nr)) {
6474 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6475
6476 percpu_ref_put_many(&ctx->refs, nr - ref_used);
Jens Axboefaf7b512020-10-07 12:48:53 -06006477 atomic_long_sub(nr - ref_used, &current->io_uring->req_issue);
6478 put_task_struct_many(current, nr - ref_used);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006479 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006480 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006481 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006482 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006483
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006484 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6485 io_commit_sqring(ctx);
6486
Jens Axboe6c271ce2019-01-10 11:22:30 -07006487 return submitted;
6488}
6489
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006490static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6491{
6492 /* Tell userspace we may need a wakeup call */
6493 spin_lock_irq(&ctx->completion_lock);
6494 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6495 spin_unlock_irq(&ctx->completion_lock);
6496}
6497
6498static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6499{
6500 spin_lock_irq(&ctx->completion_lock);
6501 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6502 spin_unlock_irq(&ctx->completion_lock);
6503}
6504
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006505static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6506 int sync, void *key)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006507{
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006508 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6509 int ret;
6510
6511 ret = autoremove_wake_function(wqe, mode, sync, key);
6512 if (ret) {
6513 unsigned long flags;
6514
6515 spin_lock_irqsave(&ctx->completion_lock, flags);
6516 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6517 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6518 }
6519 return ret;
6520}
6521
Jens Axboec8d1ba52020-09-14 11:07:26 -06006522enum sq_ret {
6523 SQT_IDLE = 1,
6524 SQT_SPIN = 2,
6525 SQT_DID_WORK = 4,
6526};
6527
6528static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006529 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006530{
6531 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006532 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006533 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006534 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006535
Jens Axboec8d1ba52020-09-14 11:07:26 -06006536again:
6537 if (!list_empty(&ctx->iopoll_list)) {
6538 unsigned nr_events = 0;
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006539
Jens Axboec8d1ba52020-09-14 11:07:26 -06006540 mutex_lock(&ctx->uring_lock);
6541 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6542 io_do_iopoll(ctx, &nr_events, 0);
6543 mutex_unlock(&ctx->uring_lock);
6544 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006545
Jens Axboec8d1ba52020-09-14 11:07:26 -06006546 to_submit = io_sqring_entries(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006547
Jens Axboec8d1ba52020-09-14 11:07:26 -06006548 /*
6549 * If submit got -EBUSY, flag us as needing the application
6550 * to enter the kernel to reap and flush events.
6551 */
6552 if (!to_submit || ret == -EBUSY || need_resched()) {
6553 /*
6554 * Drop cur_mm before scheduling, we can't hold it for
6555 * long periods (or over schedule()). Do this before
6556 * adding ourselves to the waitqueue, as the unuse/drop
6557 * may sleep.
6558 */
6559 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006560
Jens Axboec8d1ba52020-09-14 11:07:26 -06006561 /*
6562 * We're polling. If we're within the defined idle
6563 * period, then let us spin without work before going
6564 * to sleep. The exception is if we got EBUSY doing
6565 * more IO, we should wait for the application to
6566 * reap events and wake us up.
6567 */
6568 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6569 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6570 !percpu_ref_is_dying(&ctx->refs)))
6571 return SQT_SPIN;
6572
Jens Axboe534ca6d2020-09-02 13:52:19 -06006573 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006574 TASK_INTERRUPTIBLE);
6575
6576 /*
6577 * While doing polled IO, before going to sleep, we need
6578 * to check if there are new reqs added to iopoll_list,
6579 * it is because reqs may have been punted to io worker
6580 * and will be added to iopoll_list later, hence check
6581 * the iopoll_list again.
6582 */
6583 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6584 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006585 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006586 goto again;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006587 }
6588
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006589 to_submit = io_sqring_entries(ctx);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006590 if (!to_submit || ret == -EBUSY)
6591 return SQT_IDLE;
6592 }
6593
Jens Axboe534ca6d2020-09-02 13:52:19 -06006594 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006595 io_ring_clear_wakeup_flag(ctx);
6596
Jens Axboee95eee22020-09-08 09:11:32 -06006597 /* if we're handling multiple rings, cap submit size for fairness */
6598 if (cap_entries && to_submit > 8)
6599 to_submit = 8;
6600
Jens Axboec8d1ba52020-09-14 11:07:26 -06006601 mutex_lock(&ctx->uring_lock);
6602 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6603 ret = io_submit_sqes(ctx, to_submit);
6604 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006605
6606 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6607 wake_up(&ctx->sqo_sq_wait);
6608
Jens Axboec8d1ba52020-09-14 11:07:26 -06006609 return SQT_DID_WORK;
6610}
6611
Jens Axboe69fb2132020-09-14 11:16:23 -06006612static void io_sqd_init_new(struct io_sq_data *sqd)
6613{
6614 struct io_ring_ctx *ctx;
6615
6616 while (!list_empty(&sqd->ctx_new_list)) {
6617 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6618 init_wait(&ctx->sqo_wait_entry);
6619 ctx->sqo_wait_entry.func = io_sq_wake_function;
6620 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6621 complete(&ctx->sq_thread_comp);
6622 }
6623}
6624
Jens Axboe6c271ce2019-01-10 11:22:30 -07006625static int io_sq_thread(void *data)
6626{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006627 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006628 const struct cred *old_cred = NULL;
6629 struct io_sq_data *sqd = data;
6630 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006631 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006632
Jens Axboec8d1ba52020-09-14 11:07:26 -06006633 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006634 while (!kthread_should_stop()) {
6635 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006636 bool cap_entries;
Jens Axboec1edbf52019-11-10 16:56:04 -07006637
6638 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006639 * Any changes to the sqd lists are synchronized through the
6640 * kthread parking. This synchronizes the thread vs users,
6641 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006642 */
Jens Axboe69fb2132020-09-14 11:16:23 -06006643 if (kthread_should_park())
6644 kthread_parkme();
6645
6646 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6647 io_sqd_init_new(sqd);
6648
Jens Axboee95eee22020-09-08 09:11:32 -06006649 cap_entries = !list_is_singular(&sqd->ctx_list);
6650
Jens Axboe69fb2132020-09-14 11:16:23 -06006651 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6652 if (current->cred != ctx->creds) {
6653 if (old_cred)
6654 revert_creds(old_cred);
6655 old_cred = override_creds(ctx->creds);
6656 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006657 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe69fb2132020-09-14 11:16:23 -06006658
Jens Axboee95eee22020-09-08 09:11:32 -06006659 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006660
Jens Axboe4349f302020-07-09 15:07:01 -06006661 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006662 }
6663
Jens Axboe69fb2132020-09-14 11:16:23 -06006664 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006665 io_run_task_work();
6666 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006667 } else if (ret == SQT_IDLE) {
6668 if (kthread_should_park())
6669 continue;
6670 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6671 io_ring_set_wakeup_flag(ctx);
6672 schedule();
6673 start_jiffies = jiffies;
6674 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6675 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006676 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006677 }
6678
Jens Axboe4c6e2772020-07-01 11:29:10 -06006679 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006680
Dennis Zhou91d8f512020-09-16 13:41:05 -07006681 if (cur_css)
6682 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006683 if (old_cred)
6684 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006685
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006686 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006687
Jens Axboe6c271ce2019-01-10 11:22:30 -07006688 return 0;
6689}
6690
Jens Axboebda52162019-09-24 13:47:15 -06006691struct io_wait_queue {
6692 struct wait_queue_entry wq;
6693 struct io_ring_ctx *ctx;
6694 unsigned to_wait;
6695 unsigned nr_timeouts;
6696};
6697
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006698static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006699{
6700 struct io_ring_ctx *ctx = iowq->ctx;
6701
6702 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006703 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006704 * started waiting. For timeouts, we always want to return to userspace,
6705 * regardless of event count.
6706 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006707 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006708 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6709}
6710
6711static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6712 int wake_flags, void *key)
6713{
6714 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6715 wq);
6716
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006717 /* use noflush == true, as we can't safely rely on locking context */
6718 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006719 return -1;
6720
6721 return autoremove_wake_function(curr, mode, wake_flags, key);
6722}
6723
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006724static int io_run_task_work_sig(void)
6725{
6726 if (io_run_task_work())
6727 return 1;
6728 if (!signal_pending(current))
6729 return 0;
6730 if (current->jobctl & JOBCTL_TASK_WORK) {
6731 spin_lock_irq(&current->sighand->siglock);
6732 current->jobctl &= ~JOBCTL_TASK_WORK;
6733 recalc_sigpending();
6734 spin_unlock_irq(&current->sighand->siglock);
6735 return 1;
6736 }
6737 return -EINTR;
6738}
6739
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740/*
6741 * Wait until events become available, if we don't already have some. The
6742 * application must reap them itself, as they reside on the shared cq ring.
6743 */
6744static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6745 const sigset_t __user *sig, size_t sigsz)
6746{
Jens Axboebda52162019-09-24 13:47:15 -06006747 struct io_wait_queue iowq = {
6748 .wq = {
6749 .private = current,
6750 .func = io_wake_function,
6751 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6752 },
6753 .ctx = ctx,
6754 .to_wait = min_events,
6755 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006756 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006757 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006758
Jens Axboeb41e9852020-02-17 09:52:41 -07006759 do {
6760 if (io_cqring_events(ctx, false) >= min_events)
6761 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006762 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006763 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006764 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765
6766 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006767#ifdef CONFIG_COMPAT
6768 if (in_compat_syscall())
6769 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006770 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006771 else
6772#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006773 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006774
Jens Axboe2b188cc2019-01-07 10:46:33 -07006775 if (ret)
6776 return ret;
6777 }
6778
Jens Axboebda52162019-09-24 13:47:15 -06006779 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006780 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006781 do {
6782 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6783 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006784 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006785 ret = io_run_task_work_sig();
6786 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06006787 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006788 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06006789 break;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006790 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006791 break;
6792 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006793 } while (1);
6794 finish_wait(&ctx->wait, &iowq.wq);
6795
Jens Axboeb7db41c2020-07-04 08:55:50 -06006796 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797
Hristo Venev75b28af2019-08-26 17:23:46 +00006798 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006799}
6800
Jens Axboe6b063142019-01-10 22:13:58 -07006801static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6802{
6803#if defined(CONFIG_UNIX)
6804 if (ctx->ring_sock) {
6805 struct sock *sock = ctx->ring_sock->sk;
6806 struct sk_buff *skb;
6807
6808 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6809 kfree_skb(skb);
6810 }
6811#else
6812 int i;
6813
Jens Axboe65e19f52019-10-26 07:20:21 -06006814 for (i = 0; i < ctx->nr_user_files; i++) {
6815 struct file *file;
6816
6817 file = io_file_from_index(ctx, i);
6818 if (file)
6819 fput(file);
6820 }
Jens Axboe6b063142019-01-10 22:13:58 -07006821#endif
6822}
6823
Jens Axboe05f3fb32019-12-09 11:22:50 -07006824static void io_file_ref_kill(struct percpu_ref *ref)
6825{
6826 struct fixed_file_data *data;
6827
6828 data = container_of(ref, struct fixed_file_data, refs);
6829 complete(&data->done);
6830}
6831
Jens Axboe6b063142019-01-10 22:13:58 -07006832static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6833{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006834 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006835 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006836 unsigned nr_tables, i;
6837
Jens Axboe05f3fb32019-12-09 11:22:50 -07006838 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006839 return -ENXIO;
6840
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006841 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006842 if (!list_empty(&data->ref_list))
6843 ref_node = list_first_entry(&data->ref_list,
6844 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006845 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006846 if (ref_node)
6847 percpu_ref_kill(&ref_node->refs);
6848
6849 percpu_ref_kill(&data->refs);
6850
6851 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006852 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006853 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006854
Jens Axboe6b063142019-01-10 22:13:58 -07006855 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006856 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6857 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006858 kfree(data->table[i].files);
6859 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006860 percpu_ref_exit(&data->refs);
6861 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006862 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006863 ctx->nr_user_files = 0;
6864 return 0;
6865}
6866
Jens Axboe534ca6d2020-09-02 13:52:19 -06006867static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006868{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006869 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006870 /*
6871 * The park is a bit of a work-around, without it we get
6872 * warning spews on shutdown with SQPOLL set and affinity
6873 * set to a single CPU.
6874 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06006875 if (sqd->thread) {
6876 kthread_park(sqd->thread);
6877 kthread_stop(sqd->thread);
6878 }
6879
6880 kfree(sqd);
6881 }
6882}
6883
Jens Axboeaa061652020-09-02 14:50:27 -06006884static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
6885{
6886 struct io_ring_ctx *ctx_attach;
6887 struct io_sq_data *sqd;
6888 struct fd f;
6889
6890 f = fdget(p->wq_fd);
6891 if (!f.file)
6892 return ERR_PTR(-ENXIO);
6893 if (f.file->f_op != &io_uring_fops) {
6894 fdput(f);
6895 return ERR_PTR(-EINVAL);
6896 }
6897
6898 ctx_attach = f.file->private_data;
6899 sqd = ctx_attach->sq_data;
6900 if (!sqd) {
6901 fdput(f);
6902 return ERR_PTR(-EINVAL);
6903 }
6904
6905 refcount_inc(&sqd->refs);
6906 fdput(f);
6907 return sqd;
6908}
6909
Jens Axboe534ca6d2020-09-02 13:52:19 -06006910static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
6911{
6912 struct io_sq_data *sqd;
6913
Jens Axboeaa061652020-09-02 14:50:27 -06006914 if (p->flags & IORING_SETUP_ATTACH_WQ)
6915 return io_attach_sq_data(p);
6916
Jens Axboe534ca6d2020-09-02 13:52:19 -06006917 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
6918 if (!sqd)
6919 return ERR_PTR(-ENOMEM);
6920
6921 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06006922 INIT_LIST_HEAD(&sqd->ctx_list);
6923 INIT_LIST_HEAD(&sqd->ctx_new_list);
6924 mutex_init(&sqd->ctx_lock);
6925 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06006926 init_waitqueue_head(&sqd->wait);
6927 return sqd;
6928}
6929
Jens Axboe69fb2132020-09-14 11:16:23 -06006930static void io_sq_thread_unpark(struct io_sq_data *sqd)
6931 __releases(&sqd->lock)
6932{
6933 if (!sqd->thread)
6934 return;
6935 kthread_unpark(sqd->thread);
6936 mutex_unlock(&sqd->lock);
6937}
6938
6939static void io_sq_thread_park(struct io_sq_data *sqd)
6940 __acquires(&sqd->lock)
6941{
6942 if (!sqd->thread)
6943 return;
6944 mutex_lock(&sqd->lock);
6945 kthread_park(sqd->thread);
6946}
6947
Jens Axboe534ca6d2020-09-02 13:52:19 -06006948static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6949{
6950 struct io_sq_data *sqd = ctx->sq_data;
6951
6952 if (sqd) {
6953 if (sqd->thread) {
6954 /*
6955 * We may arrive here from the error branch in
6956 * io_sq_offload_create() where the kthread is created
6957 * without being waked up, thus wake it up now to make
6958 * sure the wait will complete.
6959 */
6960 wake_up_process(sqd->thread);
6961 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06006962
6963 io_sq_thread_park(sqd);
6964 }
6965
6966 mutex_lock(&sqd->ctx_lock);
6967 list_del(&ctx->sqd_list);
6968 mutex_unlock(&sqd->ctx_lock);
6969
6970 if (sqd->thread) {
6971 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
6972 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06006973 }
6974
6975 io_put_sq_data(sqd);
6976 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006977 }
6978}
6979
Jens Axboe6b063142019-01-10 22:13:58 -07006980static void io_finish_async(struct io_ring_ctx *ctx)
6981{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006982 io_sq_thread_stop(ctx);
6983
Jens Axboe561fb042019-10-24 07:25:42 -06006984 if (ctx->io_wq) {
6985 io_wq_destroy(ctx->io_wq);
6986 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006987 }
6988}
6989
6990#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006991/*
6992 * Ensure the UNIX gc is aware of our file set, so we are certain that
6993 * the io_uring can be safely unregistered on process exit, even if we have
6994 * loops in the file referencing.
6995 */
6996static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6997{
6998 struct sock *sk = ctx->ring_sock->sk;
6999 struct scm_fp_list *fpl;
7000 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007001 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007002
Jens Axboe6b063142019-01-10 22:13:58 -07007003 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7004 if (!fpl)
7005 return -ENOMEM;
7006
7007 skb = alloc_skb(0, GFP_KERNEL);
7008 if (!skb) {
7009 kfree(fpl);
7010 return -ENOMEM;
7011 }
7012
7013 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007014
Jens Axboe08a45172019-10-03 08:11:03 -06007015 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007016 fpl->user = get_uid(ctx->user);
7017 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007018 struct file *file = io_file_from_index(ctx, i + offset);
7019
7020 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007021 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007022 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007023 unix_inflight(fpl->user, fpl->fp[nr_files]);
7024 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007025 }
7026
Jens Axboe08a45172019-10-03 08:11:03 -06007027 if (nr_files) {
7028 fpl->max = SCM_MAX_FD;
7029 fpl->count = nr_files;
7030 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007031 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007032 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7033 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007034
Jens Axboe08a45172019-10-03 08:11:03 -06007035 for (i = 0; i < nr_files; i++)
7036 fput(fpl->fp[i]);
7037 } else {
7038 kfree_skb(skb);
7039 kfree(fpl);
7040 }
Jens Axboe6b063142019-01-10 22:13:58 -07007041
7042 return 0;
7043}
7044
7045/*
7046 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7047 * causes regular reference counting to break down. We rely on the UNIX
7048 * garbage collection to take care of this problem for us.
7049 */
7050static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7051{
7052 unsigned left, total;
7053 int ret = 0;
7054
7055 total = 0;
7056 left = ctx->nr_user_files;
7057 while (left) {
7058 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007059
7060 ret = __io_sqe_files_scm(ctx, this_files, total);
7061 if (ret)
7062 break;
7063 left -= this_files;
7064 total += this_files;
7065 }
7066
7067 if (!ret)
7068 return 0;
7069
7070 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007071 struct file *file = io_file_from_index(ctx, total);
7072
7073 if (file)
7074 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007075 total++;
7076 }
7077
7078 return ret;
7079}
7080#else
7081static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7082{
7083 return 0;
7084}
7085#endif
7086
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007087static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7088 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007089{
7090 int i;
7091
7092 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007093 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007094 unsigned this_files;
7095
7096 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7097 table->files = kcalloc(this_files, sizeof(struct file *),
7098 GFP_KERNEL);
7099 if (!table->files)
7100 break;
7101 nr_files -= this_files;
7102 }
7103
7104 if (i == nr_tables)
7105 return 0;
7106
7107 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007108 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007109 kfree(table->files);
7110 }
7111 return 1;
7112}
7113
Jens Axboe05f3fb32019-12-09 11:22:50 -07007114static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007115{
7116#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007117 struct sock *sock = ctx->ring_sock->sk;
7118 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7119 struct sk_buff *skb;
7120 int i;
7121
7122 __skb_queue_head_init(&list);
7123
7124 /*
7125 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7126 * remove this entry and rearrange the file array.
7127 */
7128 skb = skb_dequeue(head);
7129 while (skb) {
7130 struct scm_fp_list *fp;
7131
7132 fp = UNIXCB(skb).fp;
7133 for (i = 0; i < fp->count; i++) {
7134 int left;
7135
7136 if (fp->fp[i] != file)
7137 continue;
7138
7139 unix_notinflight(fp->user, fp->fp[i]);
7140 left = fp->count - 1 - i;
7141 if (left) {
7142 memmove(&fp->fp[i], &fp->fp[i + 1],
7143 left * sizeof(struct file *));
7144 }
7145 fp->count--;
7146 if (!fp->count) {
7147 kfree_skb(skb);
7148 skb = NULL;
7149 } else {
7150 __skb_queue_tail(&list, skb);
7151 }
7152 fput(file);
7153 file = NULL;
7154 break;
7155 }
7156
7157 if (!file)
7158 break;
7159
7160 __skb_queue_tail(&list, skb);
7161
7162 skb = skb_dequeue(head);
7163 }
7164
7165 if (skb_peek(&list)) {
7166 spin_lock_irq(&head->lock);
7167 while ((skb = __skb_dequeue(&list)) != NULL)
7168 __skb_queue_tail(head, skb);
7169 spin_unlock_irq(&head->lock);
7170 }
7171#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007172 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007173#endif
7174}
7175
Jens Axboe05f3fb32019-12-09 11:22:50 -07007176struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007177 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007178 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007179};
7180
Jens Axboe4a38aed22020-05-14 17:21:15 -06007181static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007182{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007183 struct fixed_file_data *file_data = ref_node->file_data;
7184 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007185 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007186
7187 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007188 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007189 io_ring_file_put(ctx, pfile->file);
7190 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007191 }
7192
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007193 spin_lock(&file_data->lock);
7194 list_del(&ref_node->node);
7195 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007196
Xiaoguang Wang05589552020-03-31 14:05:18 +08007197 percpu_ref_exit(&ref_node->refs);
7198 kfree(ref_node);
7199 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007200}
7201
Jens Axboe4a38aed22020-05-14 17:21:15 -06007202static void io_file_put_work(struct work_struct *work)
7203{
7204 struct io_ring_ctx *ctx;
7205 struct llist_node *node;
7206
7207 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7208 node = llist_del_all(&ctx->file_put_llist);
7209
7210 while (node) {
7211 struct fixed_file_ref_node *ref_node;
7212 struct llist_node *next = node->next;
7213
7214 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7215 __io_file_put_work(ref_node);
7216 node = next;
7217 }
7218}
7219
Jens Axboe05f3fb32019-12-09 11:22:50 -07007220static void io_file_data_ref_zero(struct percpu_ref *ref)
7221{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007222 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007223 struct io_ring_ctx *ctx;
7224 bool first_add;
7225 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007226
Xiaoguang Wang05589552020-03-31 14:05:18 +08007227 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007228 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007229
Jens Axboe4a38aed22020-05-14 17:21:15 -06007230 if (percpu_ref_is_dying(&ctx->file_data->refs))
7231 delay = 0;
7232
7233 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7234 if (!delay)
7235 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7236 else if (first_add)
7237 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007238}
7239
7240static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7241 struct io_ring_ctx *ctx)
7242{
7243 struct fixed_file_ref_node *ref_node;
7244
7245 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7246 if (!ref_node)
7247 return ERR_PTR(-ENOMEM);
7248
7249 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7250 0, GFP_KERNEL)) {
7251 kfree(ref_node);
7252 return ERR_PTR(-ENOMEM);
7253 }
7254 INIT_LIST_HEAD(&ref_node->node);
7255 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007256 ref_node->file_data = ctx->file_data;
7257 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007258}
7259
7260static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7261{
7262 percpu_ref_exit(&ref_node->refs);
7263 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007264}
7265
7266static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7267 unsigned nr_args)
7268{
7269 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007270 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007271 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007272 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007273 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007274 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007275
7276 if (ctx->file_data)
7277 return -EBUSY;
7278 if (!nr_args)
7279 return -EINVAL;
7280 if (nr_args > IORING_MAX_FIXED_FILES)
7281 return -EMFILE;
7282
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007283 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7284 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007285 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007286 file_data->ctx = ctx;
7287 init_completion(&file_data->done);
7288 INIT_LIST_HEAD(&file_data->ref_list);
7289 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007290
7291 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007292 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007293 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007294 if (!file_data->table)
7295 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007296
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007297 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007298 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7299 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007300
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007301 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7302 goto out_ref;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007303
7304 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7305 struct fixed_file_table *table;
7306 unsigned index;
7307
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007308 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7309 ret = -EFAULT;
7310 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007311 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007312 /* allow sparse sets */
7313 if (fd == -1)
7314 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007315
Jens Axboe05f3fb32019-12-09 11:22:50 -07007316 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007317 ret = -EBADF;
7318 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007319 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007320
7321 /*
7322 * Don't allow io_uring instances to be registered. If UNIX
7323 * isn't enabled, then this causes a reference cycle and this
7324 * instance can never get freed. If UNIX is enabled we'll
7325 * handle it just fine, but there's still no point in allowing
7326 * a ring fd as it doesn't support regular read/write anyway.
7327 */
7328 if (file->f_op == &io_uring_fops) {
7329 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007330 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007331 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007332 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7333 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007334 table->files[index] = file;
7335 }
7336
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007337 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007338 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007339 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007340 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007341 return ret;
7342 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007343
Xiaoguang Wang05589552020-03-31 14:05:18 +08007344 ref_node = alloc_fixed_file_ref_node(ctx);
7345 if (IS_ERR(ref_node)) {
7346 io_sqe_files_unregister(ctx);
7347 return PTR_ERR(ref_node);
7348 }
7349
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007350 file_data->node = ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007351 spin_lock(&file_data->lock);
7352 list_add(&ref_node->node, &file_data->ref_list);
7353 spin_unlock(&file_data->lock);
7354 percpu_ref_get(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007355 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007356out_fput:
7357 for (i = 0; i < ctx->nr_user_files; i++) {
7358 file = io_file_from_index(ctx, i);
7359 if (file)
7360 fput(file);
7361 }
7362 for (i = 0; i < nr_tables; i++)
7363 kfree(file_data->table[i].files);
7364 ctx->nr_user_files = 0;
7365out_ref:
7366 percpu_ref_exit(&file_data->refs);
7367out_free:
7368 kfree(file_data->table);
7369 kfree(file_data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007370 return ret;
7371}
7372
Jens Axboec3a31e62019-10-03 13:59:56 -06007373static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7374 int index)
7375{
7376#if defined(CONFIG_UNIX)
7377 struct sock *sock = ctx->ring_sock->sk;
7378 struct sk_buff_head *head = &sock->sk_receive_queue;
7379 struct sk_buff *skb;
7380
7381 /*
7382 * See if we can merge this file into an existing skb SCM_RIGHTS
7383 * file set. If there's no room, fall back to allocating a new skb
7384 * and filling it in.
7385 */
7386 spin_lock_irq(&head->lock);
7387 skb = skb_peek(head);
7388 if (skb) {
7389 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7390
7391 if (fpl->count < SCM_MAX_FD) {
7392 __skb_unlink(skb, head);
7393 spin_unlock_irq(&head->lock);
7394 fpl->fp[fpl->count] = get_file(file);
7395 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7396 fpl->count++;
7397 spin_lock_irq(&head->lock);
7398 __skb_queue_head(head, skb);
7399 } else {
7400 skb = NULL;
7401 }
7402 }
7403 spin_unlock_irq(&head->lock);
7404
7405 if (skb) {
7406 fput(file);
7407 return 0;
7408 }
7409
7410 return __io_sqe_files_scm(ctx, 1, index);
7411#else
7412 return 0;
7413#endif
7414}
7415
Hillf Dantona5318d32020-03-23 17:47:15 +08007416static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007417 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007418{
Hillf Dantona5318d32020-03-23 17:47:15 +08007419 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007420 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007421
Jens Axboe05f3fb32019-12-09 11:22:50 -07007422 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007423 if (!pfile)
7424 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007425
7426 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007427 list_add(&pfile->list, &ref_node->file_list);
7428
Hillf Dantona5318d32020-03-23 17:47:15 +08007429 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007430}
7431
7432static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7433 struct io_uring_files_update *up,
7434 unsigned nr_args)
7435{
7436 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007437 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007438 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007439 __s32 __user *fds;
7440 int fd, i, err;
7441 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007442 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007443
Jens Axboe05f3fb32019-12-09 11:22:50 -07007444 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007445 return -EOVERFLOW;
7446 if (done > ctx->nr_user_files)
7447 return -EINVAL;
7448
Xiaoguang Wang05589552020-03-31 14:05:18 +08007449 ref_node = alloc_fixed_file_ref_node(ctx);
7450 if (IS_ERR(ref_node))
7451 return PTR_ERR(ref_node);
7452
Jens Axboec3a31e62019-10-03 13:59:56 -06007453 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007454 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007455 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007456 struct fixed_file_table *table;
7457 unsigned index;
7458
Jens Axboec3a31e62019-10-03 13:59:56 -06007459 err = 0;
7460 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7461 err = -EFAULT;
7462 break;
7463 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007464 i = array_index_nospec(up->offset, ctx->nr_user_files);
7465 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007466 index = i & IORING_FILE_TABLE_MASK;
7467 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007468 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007469 err = io_queue_file_removal(data, file);
7470 if (err)
7471 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007472 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007473 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007474 }
7475 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007476 file = fget(fd);
7477 if (!file) {
7478 err = -EBADF;
7479 break;
7480 }
7481 /*
7482 * Don't allow io_uring instances to be registered. If
7483 * UNIX isn't enabled, then this causes a reference
7484 * cycle and this instance can never get freed. If UNIX
7485 * is enabled we'll handle it just fine, but there's
7486 * still no point in allowing a ring fd as it doesn't
7487 * support regular read/write anyway.
7488 */
7489 if (file->f_op == &io_uring_fops) {
7490 fput(file);
7491 err = -EBADF;
7492 break;
7493 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007494 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007495 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007496 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007497 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007498 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007499 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007500 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007501 }
7502 nr_args--;
7503 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007504 up->offset++;
7505 }
7506
Xiaoguang Wang05589552020-03-31 14:05:18 +08007507 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007508 percpu_ref_kill(&data->node->refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007509 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007510 list_add(&ref_node->node, &data->ref_list);
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007511 data->node = ref_node;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007512 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007513 percpu_ref_get(&ctx->file_data->refs);
7514 } else
7515 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007516
7517 return done ? done : err;
7518}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007519
Jens Axboe05f3fb32019-12-09 11:22:50 -07007520static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7521 unsigned nr_args)
7522{
7523 struct io_uring_files_update up;
7524
7525 if (!ctx->file_data)
7526 return -ENXIO;
7527 if (!nr_args)
7528 return -EINVAL;
7529 if (copy_from_user(&up, arg, sizeof(up)))
7530 return -EFAULT;
7531 if (up.resv)
7532 return -EINVAL;
7533
7534 return __io_sqe_files_update(ctx, &up, nr_args);
7535}
Jens Axboec3a31e62019-10-03 13:59:56 -06007536
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007537static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007538{
7539 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7540
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007541 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007542 io_put_req(req);
7543}
7544
Pavel Begunkov24369c22020-01-28 03:15:48 +03007545static int io_init_wq_offload(struct io_ring_ctx *ctx,
7546 struct io_uring_params *p)
7547{
7548 struct io_wq_data data;
7549 struct fd f;
7550 struct io_ring_ctx *ctx_attach;
7551 unsigned int concurrency;
7552 int ret = 0;
7553
7554 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007555 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007556 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007557
7558 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7559 /* Do QD, or 4 * CPUS, whatever is smallest */
7560 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7561
7562 ctx->io_wq = io_wq_create(concurrency, &data);
7563 if (IS_ERR(ctx->io_wq)) {
7564 ret = PTR_ERR(ctx->io_wq);
7565 ctx->io_wq = NULL;
7566 }
7567 return ret;
7568 }
7569
7570 f = fdget(p->wq_fd);
7571 if (!f.file)
7572 return -EBADF;
7573
7574 if (f.file->f_op != &io_uring_fops) {
7575 ret = -EINVAL;
7576 goto out_fput;
7577 }
7578
7579 ctx_attach = f.file->private_data;
7580 /* @io_wq is protected by holding the fd */
7581 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7582 ret = -EINVAL;
7583 goto out_fput;
7584 }
7585
7586 ctx->io_wq = ctx_attach->io_wq;
7587out_fput:
7588 fdput(f);
7589 return ret;
7590}
7591
Jens Axboe0f212202020-09-13 13:09:39 -06007592static int io_uring_alloc_task_context(struct task_struct *task)
7593{
7594 struct io_uring_task *tctx;
7595
7596 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7597 if (unlikely(!tctx))
7598 return -ENOMEM;
7599
7600 xa_init(&tctx->xa);
7601 init_waitqueue_head(&tctx->wait);
7602 tctx->last = NULL;
7603 tctx->in_idle = 0;
7604 atomic_long_set(&tctx->req_issue, 0);
7605 atomic_long_set(&tctx->req_complete, 0);
7606 task->io_uring = tctx;
7607 return 0;
7608}
7609
7610void __io_uring_free(struct task_struct *tsk)
7611{
7612 struct io_uring_task *tctx = tsk->io_uring;
7613
7614 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe0f212202020-09-13 13:09:39 -06007615 kfree(tctx);
7616 tsk->io_uring = NULL;
7617}
7618
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007619static int io_sq_offload_create(struct io_ring_ctx *ctx,
7620 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007621{
7622 int ret;
7623
Jens Axboe6c271ce2019-01-10 11:22:30 -07007624 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007625 struct io_sq_data *sqd;
7626
Jens Axboe3ec482d2019-04-08 10:51:01 -06007627 ret = -EPERM;
7628 if (!capable(CAP_SYS_ADMIN))
7629 goto err;
7630
Jens Axboe534ca6d2020-09-02 13:52:19 -06007631 sqd = io_get_sq_data(p);
7632 if (IS_ERR(sqd)) {
7633 ret = PTR_ERR(sqd);
7634 goto err;
7635 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007636
Jens Axboe534ca6d2020-09-02 13:52:19 -06007637 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007638 io_sq_thread_park(sqd);
7639 mutex_lock(&sqd->ctx_lock);
7640 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7641 mutex_unlock(&sqd->ctx_lock);
7642 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007643
Jens Axboe917257d2019-04-13 09:28:55 -06007644 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7645 if (!ctx->sq_thread_idle)
7646 ctx->sq_thread_idle = HZ;
7647
Jens Axboeaa061652020-09-02 14:50:27 -06007648 if (sqd->thread)
7649 goto done;
7650
Jens Axboe6c271ce2019-01-10 11:22:30 -07007651 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007652 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007653
Jens Axboe917257d2019-04-13 09:28:55 -06007654 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007655 if (cpu >= nr_cpu_ids)
7656 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007657 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007658 goto err;
7659
Jens Axboe69fb2132020-09-14 11:16:23 -06007660 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007661 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007662 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007663 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007664 "io_uring-sq");
7665 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007666 if (IS_ERR(sqd->thread)) {
7667 ret = PTR_ERR(sqd->thread);
7668 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007669 goto err;
7670 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007671 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007672 if (ret)
7673 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007674 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7675 /* Can't have SQ_AFF without SQPOLL */
7676 ret = -EINVAL;
7677 goto err;
7678 }
7679
Jens Axboeaa061652020-09-02 14:50:27 -06007680done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007681 ret = io_init_wq_offload(ctx, p);
7682 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007683 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007684
7685 return 0;
7686err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007687 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007688 return ret;
7689}
7690
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007691static void io_sq_offload_start(struct io_ring_ctx *ctx)
7692{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007693 struct io_sq_data *sqd = ctx->sq_data;
7694
7695 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7696 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007697}
7698
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007699static inline void __io_unaccount_mem(struct user_struct *user,
7700 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007701{
7702 atomic_long_sub(nr_pages, &user->locked_vm);
7703}
7704
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007705static inline int __io_account_mem(struct user_struct *user,
7706 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007707{
7708 unsigned long page_limit, cur_pages, new_pages;
7709
7710 /* Don't allow more pages than we can safely lock */
7711 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7712
7713 do {
7714 cur_pages = atomic_long_read(&user->locked_vm);
7715 new_pages = cur_pages + nr_pages;
7716 if (new_pages > page_limit)
7717 return -ENOMEM;
7718 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7719 new_pages) != cur_pages);
7720
7721 return 0;
7722}
7723
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007724static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7725 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007726{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007727 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007728 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007729
Jens Axboe2aede0e2020-09-14 10:45:53 -06007730 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007731 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007732 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007733 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007734 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007735 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007736}
7737
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007738static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7739 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007740{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007741 int ret;
7742
7743 if (ctx->limit_mem) {
7744 ret = __io_account_mem(ctx->user, nr_pages);
7745 if (ret)
7746 return ret;
7747 }
7748
Jens Axboe2aede0e2020-09-14 10:45:53 -06007749 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007750 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007751 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007752 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007753 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007754 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007755
7756 return 0;
7757}
7758
Jens Axboe2b188cc2019-01-07 10:46:33 -07007759static void io_mem_free(void *ptr)
7760{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007761 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007762
Mark Rutland52e04ef2019-04-30 17:30:21 +01007763 if (!ptr)
7764 return;
7765
7766 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007767 if (put_page_testzero(page))
7768 free_compound_page(page);
7769}
7770
7771static void *io_mem_alloc(size_t size)
7772{
7773 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7774 __GFP_NORETRY;
7775
7776 return (void *) __get_free_pages(gfp_flags, get_order(size));
7777}
7778
Hristo Venev75b28af2019-08-26 17:23:46 +00007779static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7780 size_t *sq_offset)
7781{
7782 struct io_rings *rings;
7783 size_t off, sq_array_size;
7784
7785 off = struct_size(rings, cqes, cq_entries);
7786 if (off == SIZE_MAX)
7787 return SIZE_MAX;
7788
7789#ifdef CONFIG_SMP
7790 off = ALIGN(off, SMP_CACHE_BYTES);
7791 if (off == 0)
7792 return SIZE_MAX;
7793#endif
7794
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007795 if (sq_offset)
7796 *sq_offset = off;
7797
Hristo Venev75b28af2019-08-26 17:23:46 +00007798 sq_array_size = array_size(sizeof(u32), sq_entries);
7799 if (sq_array_size == SIZE_MAX)
7800 return SIZE_MAX;
7801
7802 if (check_add_overflow(off, sq_array_size, &off))
7803 return SIZE_MAX;
7804
Hristo Venev75b28af2019-08-26 17:23:46 +00007805 return off;
7806}
7807
Jens Axboe2b188cc2019-01-07 10:46:33 -07007808static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7809{
Hristo Venev75b28af2019-08-26 17:23:46 +00007810 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007811
Hristo Venev75b28af2019-08-26 17:23:46 +00007812 pages = (size_t)1 << get_order(
7813 rings_size(sq_entries, cq_entries, NULL));
7814 pages += (size_t)1 << get_order(
7815 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007816
Hristo Venev75b28af2019-08-26 17:23:46 +00007817 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007818}
7819
Jens Axboeedafcce2019-01-09 09:16:05 -07007820static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7821{
7822 int i, j;
7823
7824 if (!ctx->user_bufs)
7825 return -ENXIO;
7826
7827 for (i = 0; i < ctx->nr_user_bufs; i++) {
7828 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7829
7830 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007831 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007832
Jens Axboede293932020-09-17 16:19:16 -06007833 if (imu->acct_pages)
7834 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007835 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007836 imu->nr_bvecs = 0;
7837 }
7838
7839 kfree(ctx->user_bufs);
7840 ctx->user_bufs = NULL;
7841 ctx->nr_user_bufs = 0;
7842 return 0;
7843}
7844
7845static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7846 void __user *arg, unsigned index)
7847{
7848 struct iovec __user *src;
7849
7850#ifdef CONFIG_COMPAT
7851 if (ctx->compat) {
7852 struct compat_iovec __user *ciovs;
7853 struct compat_iovec ciov;
7854
7855 ciovs = (struct compat_iovec __user *) arg;
7856 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7857 return -EFAULT;
7858
Jens Axboed55e5f52019-12-11 16:12:15 -07007859 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007860 dst->iov_len = ciov.iov_len;
7861 return 0;
7862 }
7863#endif
7864 src = (struct iovec __user *) arg;
7865 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7866 return -EFAULT;
7867 return 0;
7868}
7869
Jens Axboede293932020-09-17 16:19:16 -06007870/*
7871 * Not super efficient, but this is just a registration time. And we do cache
7872 * the last compound head, so generally we'll only do a full search if we don't
7873 * match that one.
7874 *
7875 * We check if the given compound head page has already been accounted, to
7876 * avoid double accounting it. This allows us to account the full size of the
7877 * page, not just the constituent pages of a huge page.
7878 */
7879static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
7880 int nr_pages, struct page *hpage)
7881{
7882 int i, j;
7883
7884 /* check current page array */
7885 for (i = 0; i < nr_pages; i++) {
7886 if (!PageCompound(pages[i]))
7887 continue;
7888 if (compound_head(pages[i]) == hpage)
7889 return true;
7890 }
7891
7892 /* check previously registered pages */
7893 for (i = 0; i < ctx->nr_user_bufs; i++) {
7894 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7895
7896 for (j = 0; j < imu->nr_bvecs; j++) {
7897 if (!PageCompound(imu->bvec[j].bv_page))
7898 continue;
7899 if (compound_head(imu->bvec[j].bv_page) == hpage)
7900 return true;
7901 }
7902 }
7903
7904 return false;
7905}
7906
7907static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
7908 int nr_pages, struct io_mapped_ubuf *imu,
7909 struct page **last_hpage)
7910{
7911 int i, ret;
7912
7913 for (i = 0; i < nr_pages; i++) {
7914 if (!PageCompound(pages[i])) {
7915 imu->acct_pages++;
7916 } else {
7917 struct page *hpage;
7918
7919 hpage = compound_head(pages[i]);
7920 if (hpage == *last_hpage)
7921 continue;
7922 *last_hpage = hpage;
7923 if (headpage_already_acct(ctx, pages, i, hpage))
7924 continue;
7925 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
7926 }
7927 }
7928
7929 if (!imu->acct_pages)
7930 return 0;
7931
7932 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
7933 if (ret)
7934 imu->acct_pages = 0;
7935 return ret;
7936}
7937
Jens Axboeedafcce2019-01-09 09:16:05 -07007938static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7939 unsigned nr_args)
7940{
7941 struct vm_area_struct **vmas = NULL;
7942 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06007943 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07007944 int i, j, got_pages = 0;
7945 int ret = -EINVAL;
7946
7947 if (ctx->user_bufs)
7948 return -EBUSY;
7949 if (!nr_args || nr_args > UIO_MAXIOV)
7950 return -EINVAL;
7951
7952 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7953 GFP_KERNEL);
7954 if (!ctx->user_bufs)
7955 return -ENOMEM;
7956
7957 for (i = 0; i < nr_args; i++) {
7958 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7959 unsigned long off, start, end, ubuf;
7960 int pret, nr_pages;
7961 struct iovec iov;
7962 size_t size;
7963
7964 ret = io_copy_iov(ctx, &iov, arg, i);
7965 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007966 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007967
7968 /*
7969 * Don't impose further limits on the size and buffer
7970 * constraints here, we'll -EINVAL later when IO is
7971 * submitted if they are wrong.
7972 */
7973 ret = -EFAULT;
7974 if (!iov.iov_base || !iov.iov_len)
7975 goto err;
7976
7977 /* arbitrary limit, but we need something */
7978 if (iov.iov_len > SZ_1G)
7979 goto err;
7980
7981 ubuf = (unsigned long) iov.iov_base;
7982 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7983 start = ubuf >> PAGE_SHIFT;
7984 nr_pages = end - start;
7985
Jens Axboeedafcce2019-01-09 09:16:05 -07007986 ret = 0;
7987 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007988 kvfree(vmas);
7989 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007990 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007991 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007992 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007993 sizeof(struct vm_area_struct *),
7994 GFP_KERNEL);
7995 if (!pages || !vmas) {
7996 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07007997 goto err;
7998 }
7999 got_pages = nr_pages;
8000 }
8001
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008002 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008003 GFP_KERNEL);
8004 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008005 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008006 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008007
8008 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008009 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008010 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008011 FOLL_WRITE | FOLL_LONGTERM,
8012 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008013 if (pret == nr_pages) {
8014 /* don't support file backed memory */
8015 for (j = 0; j < nr_pages; j++) {
8016 struct vm_area_struct *vma = vmas[j];
8017
8018 if (vma->vm_file &&
8019 !is_file_hugepages(vma->vm_file)) {
8020 ret = -EOPNOTSUPP;
8021 break;
8022 }
8023 }
8024 } else {
8025 ret = pret < 0 ? pret : -EFAULT;
8026 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008027 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008028 if (ret) {
8029 /*
8030 * if we did partial map, or found file backed vmas,
8031 * release any pages we did get
8032 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008033 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008034 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008035 kvfree(imu->bvec);
8036 goto err;
8037 }
8038
8039 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8040 if (ret) {
8041 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008042 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008043 goto err;
8044 }
8045
8046 off = ubuf & ~PAGE_MASK;
8047 size = iov.iov_len;
8048 for (j = 0; j < nr_pages; j++) {
8049 size_t vec_len;
8050
8051 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8052 imu->bvec[j].bv_page = pages[j];
8053 imu->bvec[j].bv_len = vec_len;
8054 imu->bvec[j].bv_offset = off;
8055 off = 0;
8056 size -= vec_len;
8057 }
8058 /* store original address for later verification */
8059 imu->ubuf = ubuf;
8060 imu->len = iov.iov_len;
8061 imu->nr_bvecs = nr_pages;
8062
8063 ctx->nr_user_bufs++;
8064 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008065 kvfree(pages);
8066 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008067 return 0;
8068err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008069 kvfree(pages);
8070 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008071 io_sqe_buffer_unregister(ctx);
8072 return ret;
8073}
8074
Jens Axboe9b402842019-04-11 11:45:41 -06008075static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8076{
8077 __s32 __user *fds = arg;
8078 int fd;
8079
8080 if (ctx->cq_ev_fd)
8081 return -EBUSY;
8082
8083 if (copy_from_user(&fd, fds, sizeof(*fds)))
8084 return -EFAULT;
8085
8086 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8087 if (IS_ERR(ctx->cq_ev_fd)) {
8088 int ret = PTR_ERR(ctx->cq_ev_fd);
8089 ctx->cq_ev_fd = NULL;
8090 return ret;
8091 }
8092
8093 return 0;
8094}
8095
8096static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8097{
8098 if (ctx->cq_ev_fd) {
8099 eventfd_ctx_put(ctx->cq_ev_fd);
8100 ctx->cq_ev_fd = NULL;
8101 return 0;
8102 }
8103
8104 return -ENXIO;
8105}
8106
Jens Axboe5a2e7452020-02-23 16:23:11 -07008107static int __io_destroy_buffers(int id, void *p, void *data)
8108{
8109 struct io_ring_ctx *ctx = data;
8110 struct io_buffer *buf = p;
8111
Jens Axboe067524e2020-03-02 16:32:28 -07008112 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008113 return 0;
8114}
8115
8116static void io_destroy_buffers(struct io_ring_ctx *ctx)
8117{
8118 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8119 idr_destroy(&ctx->io_buffer_idr);
8120}
8121
Jens Axboe2b188cc2019-01-07 10:46:33 -07008122static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8123{
Jens Axboe6b063142019-01-10 22:13:58 -07008124 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008125 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008126
8127 if (ctx->sqo_task) {
8128 put_task_struct(ctx->sqo_task);
8129 ctx->sqo_task = NULL;
8130 mmdrop(ctx->mm_account);
8131 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008132 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008133
Dennis Zhou91d8f512020-09-16 13:41:05 -07008134#ifdef CONFIG_BLK_CGROUP
8135 if (ctx->sqo_blkcg_css)
8136 css_put(ctx->sqo_blkcg_css);
8137#endif
8138
Jens Axboe6b063142019-01-10 22:13:58 -07008139 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008140 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008141 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008142 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008143
Jens Axboe2b188cc2019-01-07 10:46:33 -07008144#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008145 if (ctx->ring_sock) {
8146 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008147 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008148 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008149#endif
8150
Hristo Venev75b28af2019-08-26 17:23:46 +00008151 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008152 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008153
8154 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008155 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008156 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008157 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008158 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008159 kfree(ctx);
8160}
8161
8162static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8163{
8164 struct io_ring_ctx *ctx = file->private_data;
8165 __poll_t mask = 0;
8166
8167 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008168 /*
8169 * synchronizes with barrier from wq_has_sleeper call in
8170 * io_commit_cqring
8171 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008172 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008173 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008174 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008175 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008176 mask |= EPOLLIN | EPOLLRDNORM;
8177
8178 return mask;
8179}
8180
8181static int io_uring_fasync(int fd, struct file *file, int on)
8182{
8183 struct io_ring_ctx *ctx = file->private_data;
8184
8185 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8186}
8187
Jens Axboe071698e2020-01-28 10:04:42 -07008188static int io_remove_personalities(int id, void *p, void *data)
8189{
8190 struct io_ring_ctx *ctx = data;
8191 const struct cred *cred;
8192
8193 cred = idr_remove(&ctx->personality_idr, id);
8194 if (cred)
8195 put_cred(cred);
8196 return 0;
8197}
8198
Jens Axboe85faa7b2020-04-09 18:14:00 -06008199static void io_ring_exit_work(struct work_struct *work)
8200{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008201 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8202 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008203
Jens Axboe56952e92020-06-17 15:00:04 -06008204 /*
8205 * If we're doing polled IO and end up having requests being
8206 * submitted async (out-of-line), then completions can come in while
8207 * we're waiting for refs to drop. We need to reap these manually,
8208 * as nobody else will be looking for them.
8209 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008210 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008211 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008212 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008213 io_iopoll_try_reap_events(ctx);
8214 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008215 io_ring_ctx_free(ctx);
8216}
8217
Jens Axboe2b188cc2019-01-07 10:46:33 -07008218static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8219{
8220 mutex_lock(&ctx->uring_lock);
8221 percpu_ref_kill(&ctx->refs);
8222 mutex_unlock(&ctx->uring_lock);
8223
Jens Axboef3606e32020-09-22 08:18:24 -06008224 io_kill_timeouts(ctx, NULL);
8225 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008226
8227 if (ctx->io_wq)
8228 io_wq_cancel_all(ctx->io_wq);
8229
Jens Axboe15dff282019-11-13 09:09:23 -07008230 /* if we failed setting up the ctx, we might not have any rings */
8231 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008232 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008233 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008234 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008235
8236 /*
8237 * Do this upfront, so we won't have a grace period where the ring
8238 * is closed but resources aren't reaped yet. This can cause
8239 * spurious failure in setting up a new ring.
8240 */
Jens Axboe760618f2020-07-24 12:53:31 -06008241 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8242 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008243
Jens Axboe85faa7b2020-04-09 18:14:00 -06008244 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008245 /*
8246 * Use system_unbound_wq to avoid spawning tons of event kworkers
8247 * if we're exiting a ton of rings at the same time. It just adds
8248 * noise and overhead, there's no discernable change in runtime
8249 * over using system_wq.
8250 */
8251 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008252}
8253
8254static int io_uring_release(struct inode *inode, struct file *file)
8255{
8256 struct io_ring_ctx *ctx = file->private_data;
8257
8258 file->private_data = NULL;
8259 io_ring_ctx_wait_and_kill(ctx);
8260 return 0;
8261}
8262
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008263static bool io_wq_files_match(struct io_wq_work *work, void *data)
8264{
8265 struct files_struct *files = data;
8266
Jens Axboe0f212202020-09-13 13:09:39 -06008267 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008268}
8269
Jens Axboef254ac02020-08-12 17:33:30 -06008270/*
8271 * Returns true if 'preq' is the link parent of 'req'
8272 */
8273static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8274{
8275 struct io_kiocb *link;
8276
8277 if (!(preq->flags & REQ_F_LINK_HEAD))
8278 return false;
8279
8280 list_for_each_entry(link, &preq->link_list, link_list) {
8281 if (link == req)
8282 return true;
8283 }
8284
8285 return false;
8286}
8287
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008288static bool io_match_link_files(struct io_kiocb *req,
8289 struct files_struct *files)
8290{
8291 struct io_kiocb *link;
8292
8293 if (io_match_files(req, files))
8294 return true;
8295 if (req->flags & REQ_F_LINK_HEAD) {
8296 list_for_each_entry(link, &req->link_list, link_list) {
8297 if (io_match_files(link, files))
8298 return true;
8299 }
8300 }
8301 return false;
8302}
8303
Jens Axboef254ac02020-08-12 17:33:30 -06008304/*
8305 * We're looking to cancel 'req' because it's holding on to our files, but
8306 * 'req' could be a link to another request. See if it is, and cancel that
8307 * parent request if so.
8308 */
8309static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8310{
8311 struct hlist_node *tmp;
8312 struct io_kiocb *preq;
8313 bool found = false;
8314 int i;
8315
8316 spin_lock_irq(&ctx->completion_lock);
8317 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8318 struct hlist_head *list;
8319
8320 list = &ctx->cancel_hash[i];
8321 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8322 found = io_match_link(preq, req);
8323 if (found) {
8324 io_poll_remove_one(preq);
8325 break;
8326 }
8327 }
8328 }
8329 spin_unlock_irq(&ctx->completion_lock);
8330 return found;
8331}
8332
8333static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8334 struct io_kiocb *req)
8335{
8336 struct io_kiocb *preq;
8337 bool found = false;
8338
8339 spin_lock_irq(&ctx->completion_lock);
8340 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8341 found = io_match_link(preq, req);
8342 if (found) {
8343 __io_timeout_cancel(preq);
8344 break;
8345 }
8346 }
8347 spin_unlock_irq(&ctx->completion_lock);
8348 return found;
8349}
8350
Jens Axboeb711d4e2020-08-16 08:23:05 -07008351static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8352{
8353 return io_match_link(container_of(work, struct io_kiocb, work), data);
8354}
8355
8356static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8357{
8358 enum io_wq_cancel cret;
8359
8360 /* cancel this particular work, if it's running */
8361 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8362 if (cret != IO_WQ_CANCEL_NOTFOUND)
8363 return;
8364
8365 /* find links that hold this pending, cancel those */
8366 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8367 if (cret != IO_WQ_CANCEL_NOTFOUND)
8368 return;
8369
8370 /* if we have a poll link holding this pending, cancel that */
8371 if (io_poll_remove_link(ctx, req))
8372 return;
8373
8374 /* final option, timeout link is holding this req pending */
8375 io_timeout_remove_link(ctx, req);
8376}
8377
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008378static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8379 struct files_struct *files)
8380{
8381 struct io_defer_entry *de = NULL;
8382 LIST_HEAD(list);
8383
8384 spin_lock_irq(&ctx->completion_lock);
8385 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008386 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008387 list_cut_position(&list, &ctx->defer_list, &de->list);
8388 break;
8389 }
8390 }
8391 spin_unlock_irq(&ctx->completion_lock);
8392
8393 while (!list_empty(&list)) {
8394 de = list_first_entry(&list, struct io_defer_entry, list);
8395 list_del_init(&de->list);
8396 req_set_fail_links(de->req);
8397 io_put_req(de->req);
8398 io_req_complete(de->req, -ECANCELED);
8399 kfree(de);
8400 }
8401}
8402
Jens Axboe76e1b642020-09-26 15:05:03 -06008403/*
8404 * Returns true if we found and killed one or more files pinning requests
8405 */
8406static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008407 struct files_struct *files)
8408{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008409 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008410 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008411
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008412 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008413 /* cancel all at once, should be faster than doing it one by one*/
8414 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8415
Jens Axboefcb323c2019-10-24 12:39:47 -06008416 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008417 struct io_kiocb *cancel_req = NULL, *req;
8418 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008419
8420 spin_lock_irq(&ctx->inflight_lock);
8421 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008422 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008423 continue;
8424 /* req is being completed, ignore */
8425 if (!refcount_inc_not_zero(&req->refs))
8426 continue;
8427 cancel_req = req;
8428 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008429 }
Jens Axboe768134d2019-11-10 20:30:53 -07008430 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008431 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008432 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008433 spin_unlock_irq(&ctx->inflight_lock);
8434
Jens Axboe768134d2019-11-10 20:30:53 -07008435 /* We need to keep going until we don't find a matching req */
8436 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008437 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008438 /* cancel this request, or head link requests */
8439 io_attempt_cancel(ctx, cancel_req);
8440 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008441 /* cancellations _may_ trigger task work */
8442 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008443 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008444 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008445 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008446
8447 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008448}
8449
Pavel Begunkov801dd572020-06-15 10:33:14 +03008450static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008451{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008452 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8453 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008454
Jens Axboef3606e32020-09-22 08:18:24 -06008455 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008456}
8457
Jens Axboe0f212202020-09-13 13:09:39 -06008458static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8459 struct task_struct *task,
8460 struct files_struct *files)
8461{
8462 bool ret;
8463
8464 ret = io_uring_cancel_files(ctx, files);
8465 if (!files) {
8466 enum io_wq_cancel cret;
8467
8468 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8469 if (cret != IO_WQ_CANCEL_NOTFOUND)
8470 ret = true;
8471
8472 /* SQPOLL thread does its own polling */
8473 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8474 while (!list_empty_careful(&ctx->iopoll_list)) {
8475 io_iopoll_try_reap_events(ctx);
8476 ret = true;
8477 }
8478 }
8479
8480 ret |= io_poll_remove_all(ctx, task);
8481 ret |= io_kill_timeouts(ctx, task);
8482 }
8483
8484 return ret;
8485}
8486
8487/*
8488 * We need to iteratively cancel requests, in case a request has dependent
8489 * hard links. These persist even for failure of cancelations, hence keep
8490 * looping until none are found.
8491 */
8492static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8493 struct files_struct *files)
8494{
8495 struct task_struct *task = current;
8496
Jens Axboe534ca6d2020-09-02 13:52:19 -06008497 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8498 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008499
8500 io_cqring_overflow_flush(ctx, true, task, files);
8501
8502 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8503 io_run_task_work();
8504 cond_resched();
8505 }
8506}
8507
8508/*
8509 * Note that this task has used io_uring. We use it for cancelation purposes.
8510 */
8511static int io_uring_add_task_file(struct file *file)
8512{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008513 struct io_uring_task *tctx = current->io_uring;
8514
8515 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008516 int ret;
8517
8518 ret = io_uring_alloc_task_context(current);
8519 if (unlikely(ret))
8520 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008521 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008522 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008523 if (tctx->last != file) {
8524 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008525
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008526 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008527 get_file(file);
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008528 xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008529 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008530 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008531 }
8532
8533 return 0;
8534}
8535
8536/*
8537 * Remove this io_uring_file -> task mapping.
8538 */
8539static void io_uring_del_task_file(struct file *file)
8540{
8541 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008542
8543 if (tctx->last == file)
8544 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008545 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008546 if (file)
8547 fput(file);
8548}
8549
8550static void __io_uring_attempt_task_drop(struct file *file)
8551{
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008552 struct file *old = xa_load(&current->io_uring->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008553
8554 if (old == file)
8555 io_uring_del_task_file(file);
8556}
8557
8558/*
8559 * Drop task note for this file if we're the only ones that hold it after
8560 * pending fput()
8561 */
8562static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8563{
8564 if (!current->io_uring)
8565 return;
8566 /*
8567 * fput() is pending, will be 2 if the only other ref is our potential
8568 * task file note. If the task is exiting, drop regardless of count.
8569 */
8570 if (!exiting && atomic_long_read(&file->f_count) != 2)
8571 return;
8572
8573 __io_uring_attempt_task_drop(file);
8574}
8575
8576void __io_uring_files_cancel(struct files_struct *files)
8577{
8578 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008579 struct file *file;
8580 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008581
8582 /* make sure overflow events are dropped */
8583 tctx->in_idle = true;
8584
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008585 xa_for_each(&tctx->xa, index, file) {
8586 struct io_ring_ctx *ctx = file->private_data;
Jens Axboe0f212202020-09-13 13:09:39 -06008587
8588 io_uring_cancel_task_requests(ctx, files);
8589 if (files)
8590 io_uring_del_task_file(file);
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008591 }
Jens Axboe0f212202020-09-13 13:09:39 -06008592}
8593
8594static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8595{
8596 return atomic_long_read(&tctx->req_issue) ==
8597 atomic_long_read(&tctx->req_complete);
8598}
8599
8600/*
8601 * Find any io_uring fd that this task has registered or done IO on, and cancel
8602 * requests.
8603 */
8604void __io_uring_task_cancel(void)
8605{
8606 struct io_uring_task *tctx = current->io_uring;
8607 DEFINE_WAIT(wait);
8608 long completions;
8609
8610 /* make sure overflow events are dropped */
8611 tctx->in_idle = true;
8612
8613 while (!io_uring_task_idle(tctx)) {
8614 /* read completions before cancelations */
8615 completions = atomic_long_read(&tctx->req_complete);
8616 __io_uring_files_cancel(NULL);
8617
8618 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8619
8620 /*
8621 * If we've seen completions, retry. This avoids a race where
8622 * a completion comes in before we did prepare_to_wait().
8623 */
8624 if (completions != atomic_long_read(&tctx->req_complete))
8625 continue;
8626 if (io_uring_task_idle(tctx))
8627 break;
8628 schedule();
8629 }
8630
8631 finish_wait(&tctx->wait, &wait);
8632 tctx->in_idle = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06008633}
8634
8635static int io_uring_flush(struct file *file, void *data)
8636{
8637 struct io_ring_ctx *ctx = file->private_data;
8638
Jens Axboe6ab23142020-02-08 20:23:59 -07008639 /*
8640 * If the task is going away, cancel work it may have pending
8641 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008642 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008643 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008644
Jens Axboe0f212202020-09-13 13:09:39 -06008645 io_uring_cancel_task_requests(ctx, data);
8646 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008647 return 0;
8648}
8649
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008650static void *io_uring_validate_mmap_request(struct file *file,
8651 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008652{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008653 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008654 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008655 struct page *page;
8656 void *ptr;
8657
8658 switch (offset) {
8659 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008660 case IORING_OFF_CQ_RING:
8661 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008662 break;
8663 case IORING_OFF_SQES:
8664 ptr = ctx->sq_sqes;
8665 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008666 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008667 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008668 }
8669
8670 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008671 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008672 return ERR_PTR(-EINVAL);
8673
8674 return ptr;
8675}
8676
8677#ifdef CONFIG_MMU
8678
8679static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8680{
8681 size_t sz = vma->vm_end - vma->vm_start;
8682 unsigned long pfn;
8683 void *ptr;
8684
8685 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8686 if (IS_ERR(ptr))
8687 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008688
8689 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8690 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8691}
8692
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008693#else /* !CONFIG_MMU */
8694
8695static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8696{
8697 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8698}
8699
8700static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8701{
8702 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8703}
8704
8705static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8706 unsigned long addr, unsigned long len,
8707 unsigned long pgoff, unsigned long flags)
8708{
8709 void *ptr;
8710
8711 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8712 if (IS_ERR(ptr))
8713 return PTR_ERR(ptr);
8714
8715 return (unsigned long) ptr;
8716}
8717
8718#endif /* !CONFIG_MMU */
8719
Jens Axboe90554202020-09-03 12:12:41 -06008720static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8721{
8722 DEFINE_WAIT(wait);
8723
8724 do {
8725 if (!io_sqring_full(ctx))
8726 break;
8727
8728 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8729
8730 if (!io_sqring_full(ctx))
8731 break;
8732
8733 schedule();
8734 } while (!signal_pending(current));
8735
8736 finish_wait(&ctx->sqo_sq_wait, &wait);
8737}
8738
Jens Axboe2b188cc2019-01-07 10:46:33 -07008739SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8740 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8741 size_t, sigsz)
8742{
8743 struct io_ring_ctx *ctx;
8744 long ret = -EBADF;
8745 int submitted = 0;
8746 struct fd f;
8747
Jens Axboe4c6e2772020-07-01 11:29:10 -06008748 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008749
Jens Axboe90554202020-09-03 12:12:41 -06008750 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8751 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008752 return -EINVAL;
8753
8754 f = fdget(fd);
8755 if (!f.file)
8756 return -EBADF;
8757
8758 ret = -EOPNOTSUPP;
8759 if (f.file->f_op != &io_uring_fops)
8760 goto out_fput;
8761
8762 ret = -ENXIO;
8763 ctx = f.file->private_data;
8764 if (!percpu_ref_tryget(&ctx->refs))
8765 goto out_fput;
8766
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008767 ret = -EBADFD;
8768 if (ctx->flags & IORING_SETUP_R_DISABLED)
8769 goto out;
8770
Jens Axboe6c271ce2019-01-10 11:22:30 -07008771 /*
8772 * For SQ polling, the thread will do all submissions and completions.
8773 * Just return the requested submit count, and wake the thread if
8774 * we were asked to.
8775 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008776 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008777 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008778 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008779 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008780 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008781 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008782 if (flags & IORING_ENTER_SQ_WAIT)
8783 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008784 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008785 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008786 ret = io_uring_add_task_file(f.file);
8787 if (unlikely(ret))
8788 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008789 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008790 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008791 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008792
8793 if (submitted != to_submit)
8794 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008795 }
8796 if (flags & IORING_ENTER_GETEVENTS) {
8797 min_complete = min(min_complete, ctx->cq_entries);
8798
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008799 /*
8800 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8801 * space applications don't need to do io completion events
8802 * polling again, they can rely on io_sq_thread to do polling
8803 * work, which can reduce cpu usage and uring_lock contention.
8804 */
8805 if (ctx->flags & IORING_SETUP_IOPOLL &&
8806 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008807 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008808 } else {
8809 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8810 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008811 }
8812
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008813out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008814 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008815out_fput:
8816 fdput(f);
8817 return submitted ? submitted : ret;
8818}
8819
Tobias Klauserbebdb652020-02-26 18:38:32 +01008820#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008821static int io_uring_show_cred(int id, void *p, void *data)
8822{
8823 const struct cred *cred = p;
8824 struct seq_file *m = data;
8825 struct user_namespace *uns = seq_user_ns(m);
8826 struct group_info *gi;
8827 kernel_cap_t cap;
8828 unsigned __capi;
8829 int g;
8830
8831 seq_printf(m, "%5d\n", id);
8832 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8833 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8834 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8835 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8836 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8837 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8838 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8839 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8840 seq_puts(m, "\n\tGroups:\t");
8841 gi = cred->group_info;
8842 for (g = 0; g < gi->ngroups; g++) {
8843 seq_put_decimal_ull(m, g ? " " : "",
8844 from_kgid_munged(uns, gi->gid[g]));
8845 }
8846 seq_puts(m, "\n\tCapEff:\t");
8847 cap = cred->cap_effective;
8848 CAP_FOR_EACH_U32(__capi)
8849 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8850 seq_putc(m, '\n');
8851 return 0;
8852}
8853
8854static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8855{
Joseph Qidbbe9c62020-09-29 09:01:22 -06008856 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06008857 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008858 int i;
8859
Jens Axboefad8e0d2020-09-28 08:57:48 -06008860 /*
8861 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8862 * since fdinfo case grabs it in the opposite direction of normal use
8863 * cases. If we fail to get the lock, we just don't iterate any
8864 * structures that could be going away outside the io_uring mutex.
8865 */
8866 has_lock = mutex_trylock(&ctx->uring_lock);
8867
Joseph Qidbbe9c62020-09-29 09:01:22 -06008868 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
8869 sq = ctx->sq_data;
8870
8871 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
8872 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07008873 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008874 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008875 struct fixed_file_table *table;
8876 struct file *f;
8877
8878 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8879 f = table->files[i & IORING_FILE_TABLE_MASK];
8880 if (f)
8881 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8882 else
8883 seq_printf(m, "%5u: <none>\n", i);
8884 }
8885 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008886 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008887 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8888
8889 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8890 (unsigned int) buf->len);
8891 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008892 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008893 seq_printf(m, "Personalities:\n");
8894 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8895 }
Jens Axboed7718a92020-02-14 22:23:12 -07008896 seq_printf(m, "PollList:\n");
8897 spin_lock_irq(&ctx->completion_lock);
8898 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8899 struct hlist_head *list = &ctx->cancel_hash[i];
8900 struct io_kiocb *req;
8901
8902 hlist_for_each_entry(req, list, hash_node)
8903 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8904 req->task->task_works != NULL);
8905 }
8906 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008907 if (has_lock)
8908 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008909}
8910
8911static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8912{
8913 struct io_ring_ctx *ctx = f->private_data;
8914
8915 if (percpu_ref_tryget(&ctx->refs)) {
8916 __io_uring_show_fdinfo(ctx, m);
8917 percpu_ref_put(&ctx->refs);
8918 }
8919}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008920#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008921
Jens Axboe2b188cc2019-01-07 10:46:33 -07008922static const struct file_operations io_uring_fops = {
8923 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008924 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008925 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008926#ifndef CONFIG_MMU
8927 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8928 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8929#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008930 .poll = io_uring_poll,
8931 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008932#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008933 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008934#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008935};
8936
8937static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8938 struct io_uring_params *p)
8939{
Hristo Venev75b28af2019-08-26 17:23:46 +00008940 struct io_rings *rings;
8941 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008942
Jens Axboebd740482020-08-05 12:58:23 -06008943 /* make sure these are sane, as we already accounted them */
8944 ctx->sq_entries = p->sq_entries;
8945 ctx->cq_entries = p->cq_entries;
8946
Hristo Venev75b28af2019-08-26 17:23:46 +00008947 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8948 if (size == SIZE_MAX)
8949 return -EOVERFLOW;
8950
8951 rings = io_mem_alloc(size);
8952 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008953 return -ENOMEM;
8954
Hristo Venev75b28af2019-08-26 17:23:46 +00008955 ctx->rings = rings;
8956 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8957 rings->sq_ring_mask = p->sq_entries - 1;
8958 rings->cq_ring_mask = p->cq_entries - 1;
8959 rings->sq_ring_entries = p->sq_entries;
8960 rings->cq_ring_entries = p->cq_entries;
8961 ctx->sq_mask = rings->sq_ring_mask;
8962 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008963
8964 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008965 if (size == SIZE_MAX) {
8966 io_mem_free(ctx->rings);
8967 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008968 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008969 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008970
8971 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008972 if (!ctx->sq_sqes) {
8973 io_mem_free(ctx->rings);
8974 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008975 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008976 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008977
Jens Axboe2b188cc2019-01-07 10:46:33 -07008978 return 0;
8979}
8980
8981/*
8982 * Allocate an anonymous fd, this is what constitutes the application
8983 * visible backing of an io_uring instance. The application mmaps this
8984 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8985 * we have to tie this fd to a socket for file garbage collection purposes.
8986 */
8987static int io_uring_get_fd(struct io_ring_ctx *ctx)
8988{
8989 struct file *file;
8990 int ret;
8991
8992#if defined(CONFIG_UNIX)
8993 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8994 &ctx->ring_sock);
8995 if (ret)
8996 return ret;
8997#endif
8998
8999 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9000 if (ret < 0)
9001 goto err;
9002
9003 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9004 O_RDWR | O_CLOEXEC);
9005 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009006err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009007 put_unused_fd(ret);
9008 ret = PTR_ERR(file);
9009 goto err;
9010 }
9011
9012#if defined(CONFIG_UNIX)
9013 ctx->ring_sock->file = file;
9014#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009015 if (unlikely(io_uring_add_task_file(file))) {
9016 file = ERR_PTR(-ENOMEM);
9017 goto err_fd;
9018 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009019 fd_install(ret, file);
9020 return ret;
9021err:
9022#if defined(CONFIG_UNIX)
9023 sock_release(ctx->ring_sock);
9024 ctx->ring_sock = NULL;
9025#endif
9026 return ret;
9027}
9028
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009029static int io_uring_create(unsigned entries, struct io_uring_params *p,
9030 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009031{
9032 struct user_struct *user = NULL;
9033 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009034 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009035 int ret;
9036
Jens Axboe8110c1a2019-12-28 15:39:54 -07009037 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009038 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009039 if (entries > IORING_MAX_ENTRIES) {
9040 if (!(p->flags & IORING_SETUP_CLAMP))
9041 return -EINVAL;
9042 entries = IORING_MAX_ENTRIES;
9043 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009044
9045 /*
9046 * Use twice as many entries for the CQ ring. It's possible for the
9047 * application to drive a higher depth than the size of the SQ ring,
9048 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009049 * some flexibility in overcommitting a bit. If the application has
9050 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9051 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009052 */
9053 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009054 if (p->flags & IORING_SETUP_CQSIZE) {
9055 /*
9056 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9057 * to a power-of-two, if it isn't already. We do NOT impose
9058 * any cq vs sq ring sizing.
9059 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009060 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009061 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009062 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9063 if (!(p->flags & IORING_SETUP_CLAMP))
9064 return -EINVAL;
9065 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9066 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009067 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9068 } else {
9069 p->cq_entries = 2 * p->sq_entries;
9070 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009071
9072 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009073 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009074
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009075 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009076 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009077 ring_pages(p->sq_entries, p->cq_entries));
9078 if (ret) {
9079 free_uid(user);
9080 return ret;
9081 }
9082 }
9083
9084 ctx = io_ring_ctx_alloc(p);
9085 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009086 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009087 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009088 p->cq_entries));
9089 free_uid(user);
9090 return -ENOMEM;
9091 }
9092 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009093 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009094 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009095
Jens Axboe2aede0e2020-09-14 10:45:53 -06009096 ctx->sqo_task = get_task_struct(current);
9097
9098 /*
9099 * This is just grabbed for accounting purposes. When a process exits,
9100 * the mm is exited and dropped before the files, hence we need to hang
9101 * on to this mm purely for the purposes of being able to unaccount
9102 * memory (locked/pinned vm). It's not used for anything else.
9103 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009104 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009105 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009106
Dennis Zhou91d8f512020-09-16 13:41:05 -07009107#ifdef CONFIG_BLK_CGROUP
9108 /*
9109 * The sq thread will belong to the original cgroup it was inited in.
9110 * If the cgroup goes offline (e.g. disabling the io controller), then
9111 * issued bios will be associated with the closest cgroup later in the
9112 * block layer.
9113 */
9114 rcu_read_lock();
9115 ctx->sqo_blkcg_css = blkcg_css();
9116 ret = css_tryget_online(ctx->sqo_blkcg_css);
9117 rcu_read_unlock();
9118 if (!ret) {
9119 /* don't init against a dying cgroup, have the user try again */
9120 ctx->sqo_blkcg_css = NULL;
9121 ret = -ENODEV;
9122 goto err;
9123 }
9124#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009125
Jens Axboe2b188cc2019-01-07 10:46:33 -07009126 /*
9127 * Account memory _before_ installing the file descriptor. Once
9128 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009129 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009130 * will un-account as well.
9131 */
9132 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9133 ACCT_LOCKED);
9134 ctx->limit_mem = limit_mem;
9135
9136 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009137 if (ret)
9138 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009139
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009140 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009141 if (ret)
9142 goto err;
9143
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009144 if (!(p->flags & IORING_SETUP_R_DISABLED))
9145 io_sq_offload_start(ctx);
9146
Jens Axboe2b188cc2019-01-07 10:46:33 -07009147 memset(&p->sq_off, 0, sizeof(p->sq_off));
9148 p->sq_off.head = offsetof(struct io_rings, sq.head);
9149 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9150 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9151 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9152 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9153 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9154 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9155
9156 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009157 p->cq_off.head = offsetof(struct io_rings, cq.head);
9158 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9159 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9160 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9161 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9162 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009163 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009164
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009165 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9166 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009167 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9168 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009169
9170 if (copy_to_user(params, p, sizeof(*p))) {
9171 ret = -EFAULT;
9172 goto err;
9173 }
Jens Axboed1719f72020-07-30 13:43:53 -06009174
9175 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009176 * Install ring fd as the very last thing, so we don't risk someone
9177 * having closed it before we finish setup
9178 */
9179 ret = io_uring_get_fd(ctx);
9180 if (ret < 0)
9181 goto err;
9182
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009183 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009184 return ret;
9185err:
9186 io_ring_ctx_wait_and_kill(ctx);
9187 return ret;
9188}
9189
9190/*
9191 * Sets up an aio uring context, and returns the fd. Applications asks for a
9192 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9193 * params structure passed in.
9194 */
9195static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9196{
9197 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009198 int i;
9199
9200 if (copy_from_user(&p, params, sizeof(p)))
9201 return -EFAULT;
9202 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9203 if (p.resv[i])
9204 return -EINVAL;
9205 }
9206
Jens Axboe6c271ce2019-01-10 11:22:30 -07009207 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009208 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009209 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9210 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009211 return -EINVAL;
9212
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009213 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009214}
9215
9216SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9217 struct io_uring_params __user *, params)
9218{
9219 return io_uring_setup(entries, params);
9220}
9221
Jens Axboe66f4af92020-01-16 15:36:52 -07009222static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9223{
9224 struct io_uring_probe *p;
9225 size_t size;
9226 int i, ret;
9227
9228 size = struct_size(p, ops, nr_args);
9229 if (size == SIZE_MAX)
9230 return -EOVERFLOW;
9231 p = kzalloc(size, GFP_KERNEL);
9232 if (!p)
9233 return -ENOMEM;
9234
9235 ret = -EFAULT;
9236 if (copy_from_user(p, arg, size))
9237 goto out;
9238 ret = -EINVAL;
9239 if (memchr_inv(p, 0, size))
9240 goto out;
9241
9242 p->last_op = IORING_OP_LAST - 1;
9243 if (nr_args > IORING_OP_LAST)
9244 nr_args = IORING_OP_LAST;
9245
9246 for (i = 0; i < nr_args; i++) {
9247 p->ops[i].op = i;
9248 if (!io_op_defs[i].not_supported)
9249 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9250 }
9251 p->ops_len = i;
9252
9253 ret = 0;
9254 if (copy_to_user(arg, p, size))
9255 ret = -EFAULT;
9256out:
9257 kfree(p);
9258 return ret;
9259}
9260
Jens Axboe071698e2020-01-28 10:04:42 -07009261static int io_register_personality(struct io_ring_ctx *ctx)
9262{
9263 const struct cred *creds = get_current_cred();
9264 int id;
9265
9266 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9267 USHRT_MAX, GFP_KERNEL);
9268 if (id < 0)
9269 put_cred(creds);
9270 return id;
9271}
9272
9273static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9274{
9275 const struct cred *old_creds;
9276
9277 old_creds = idr_remove(&ctx->personality_idr, id);
9278 if (old_creds) {
9279 put_cred(old_creds);
9280 return 0;
9281 }
9282
9283 return -EINVAL;
9284}
9285
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009286static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9287 unsigned int nr_args)
9288{
9289 struct io_uring_restriction *res;
9290 size_t size;
9291 int i, ret;
9292
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009293 /* Restrictions allowed only if rings started disabled */
9294 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9295 return -EBADFD;
9296
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009297 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009298 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009299 return -EBUSY;
9300
9301 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9302 return -EINVAL;
9303
9304 size = array_size(nr_args, sizeof(*res));
9305 if (size == SIZE_MAX)
9306 return -EOVERFLOW;
9307
9308 res = memdup_user(arg, size);
9309 if (IS_ERR(res))
9310 return PTR_ERR(res);
9311
9312 ret = 0;
9313
9314 for (i = 0; i < nr_args; i++) {
9315 switch (res[i].opcode) {
9316 case IORING_RESTRICTION_REGISTER_OP:
9317 if (res[i].register_op >= IORING_REGISTER_LAST) {
9318 ret = -EINVAL;
9319 goto out;
9320 }
9321
9322 __set_bit(res[i].register_op,
9323 ctx->restrictions.register_op);
9324 break;
9325 case IORING_RESTRICTION_SQE_OP:
9326 if (res[i].sqe_op >= IORING_OP_LAST) {
9327 ret = -EINVAL;
9328 goto out;
9329 }
9330
9331 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9332 break;
9333 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9334 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9335 break;
9336 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9337 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9338 break;
9339 default:
9340 ret = -EINVAL;
9341 goto out;
9342 }
9343 }
9344
9345out:
9346 /* Reset all restrictions if an error happened */
9347 if (ret != 0)
9348 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9349 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009350 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009351
9352 kfree(res);
9353 return ret;
9354}
9355
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009356static int io_register_enable_rings(struct io_ring_ctx *ctx)
9357{
9358 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9359 return -EBADFD;
9360
9361 if (ctx->restrictions.registered)
9362 ctx->restricted = 1;
9363
9364 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9365
9366 io_sq_offload_start(ctx);
9367
9368 return 0;
9369}
9370
Jens Axboe071698e2020-01-28 10:04:42 -07009371static bool io_register_op_must_quiesce(int op)
9372{
9373 switch (op) {
9374 case IORING_UNREGISTER_FILES:
9375 case IORING_REGISTER_FILES_UPDATE:
9376 case IORING_REGISTER_PROBE:
9377 case IORING_REGISTER_PERSONALITY:
9378 case IORING_UNREGISTER_PERSONALITY:
9379 return false;
9380 default:
9381 return true;
9382 }
9383}
9384
Jens Axboeedafcce2019-01-09 09:16:05 -07009385static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9386 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009387 __releases(ctx->uring_lock)
9388 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009389{
9390 int ret;
9391
Jens Axboe35fa71a2019-04-22 10:23:23 -06009392 /*
9393 * We're inside the ring mutex, if the ref is already dying, then
9394 * someone else killed the ctx or is already going through
9395 * io_uring_register().
9396 */
9397 if (percpu_ref_is_dying(&ctx->refs))
9398 return -ENXIO;
9399
Jens Axboe071698e2020-01-28 10:04:42 -07009400 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009401 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009402
Jens Axboe05f3fb32019-12-09 11:22:50 -07009403 /*
9404 * Drop uring mutex before waiting for references to exit. If
9405 * another thread is currently inside io_uring_enter() it might
9406 * need to grab the uring_lock to make progress. If we hold it
9407 * here across the drain wait, then we can deadlock. It's safe
9408 * to drop the mutex here, since no new references will come in
9409 * after we've killed the percpu ref.
9410 */
9411 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009412 do {
9413 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9414 if (!ret)
9415 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009416 ret = io_run_task_work_sig();
9417 if (ret < 0)
9418 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009419 } while (1);
9420
Jens Axboe05f3fb32019-12-09 11:22:50 -07009421 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009422
Jens Axboec1503682020-01-08 08:26:07 -07009423 if (ret) {
9424 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009425 goto out_quiesce;
9426 }
9427 }
9428
9429 if (ctx->restricted) {
9430 if (opcode >= IORING_REGISTER_LAST) {
9431 ret = -EINVAL;
9432 goto out;
9433 }
9434
9435 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9436 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009437 goto out;
9438 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009439 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009440
9441 switch (opcode) {
9442 case IORING_REGISTER_BUFFERS:
9443 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9444 break;
9445 case IORING_UNREGISTER_BUFFERS:
9446 ret = -EINVAL;
9447 if (arg || nr_args)
9448 break;
9449 ret = io_sqe_buffer_unregister(ctx);
9450 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009451 case IORING_REGISTER_FILES:
9452 ret = io_sqe_files_register(ctx, arg, nr_args);
9453 break;
9454 case IORING_UNREGISTER_FILES:
9455 ret = -EINVAL;
9456 if (arg || nr_args)
9457 break;
9458 ret = io_sqe_files_unregister(ctx);
9459 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009460 case IORING_REGISTER_FILES_UPDATE:
9461 ret = io_sqe_files_update(ctx, arg, nr_args);
9462 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009463 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009464 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009465 ret = -EINVAL;
9466 if (nr_args != 1)
9467 break;
9468 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009469 if (ret)
9470 break;
9471 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9472 ctx->eventfd_async = 1;
9473 else
9474 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009475 break;
9476 case IORING_UNREGISTER_EVENTFD:
9477 ret = -EINVAL;
9478 if (arg || nr_args)
9479 break;
9480 ret = io_eventfd_unregister(ctx);
9481 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009482 case IORING_REGISTER_PROBE:
9483 ret = -EINVAL;
9484 if (!arg || nr_args > 256)
9485 break;
9486 ret = io_probe(ctx, arg, nr_args);
9487 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009488 case IORING_REGISTER_PERSONALITY:
9489 ret = -EINVAL;
9490 if (arg || nr_args)
9491 break;
9492 ret = io_register_personality(ctx);
9493 break;
9494 case IORING_UNREGISTER_PERSONALITY:
9495 ret = -EINVAL;
9496 if (arg)
9497 break;
9498 ret = io_unregister_personality(ctx, nr_args);
9499 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009500 case IORING_REGISTER_ENABLE_RINGS:
9501 ret = -EINVAL;
9502 if (arg || nr_args)
9503 break;
9504 ret = io_register_enable_rings(ctx);
9505 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009506 case IORING_REGISTER_RESTRICTIONS:
9507 ret = io_register_restrictions(ctx, arg, nr_args);
9508 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009509 default:
9510 ret = -EINVAL;
9511 break;
9512 }
9513
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009514out:
Jens Axboe071698e2020-01-28 10:04:42 -07009515 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009516 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009517 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009518out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009519 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009520 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009521 return ret;
9522}
9523
9524SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9525 void __user *, arg, unsigned int, nr_args)
9526{
9527 struct io_ring_ctx *ctx;
9528 long ret = -EBADF;
9529 struct fd f;
9530
9531 f = fdget(fd);
9532 if (!f.file)
9533 return -EBADF;
9534
9535 ret = -EOPNOTSUPP;
9536 if (f.file->f_op != &io_uring_fops)
9537 goto out_fput;
9538
9539 ctx = f.file->private_data;
9540
9541 mutex_lock(&ctx->uring_lock);
9542 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9543 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009544 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9545 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009546out_fput:
9547 fdput(f);
9548 return ret;
9549}
9550
Jens Axboe2b188cc2019-01-07 10:46:33 -07009551static int __init io_uring_init(void)
9552{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009553#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9554 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9555 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9556} while (0)
9557
9558#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9559 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9560 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9561 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9562 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9563 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9564 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9565 BUILD_BUG_SQE_ELEM(8, __u64, off);
9566 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9567 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009568 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009569 BUILD_BUG_SQE_ELEM(24, __u32, len);
9570 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9571 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9572 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9573 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009574 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9575 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009576 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9577 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9578 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9579 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9580 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9581 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9582 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9583 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009584 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009585 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9586 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9587 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009588 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009589
Jens Axboed3656342019-12-18 09:50:26 -07009590 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009591 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009592 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9593 return 0;
9594};
9595__initcall(io_uring_init);