blob: ffdaea55e820f9719dcba342afcfcd84e7f092a3 [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
Xiaoguang Wang05589552020-03-31 14:05:18 +0800213 struct percpu_ref *cur_refs;
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;
437 u64 addr;
438 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300439 u32 off;
440 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300441 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700442};
443
Jens Axboe9adbd452019-12-20 08:45:55 -0700444struct io_rw {
445 /* NOTE: kiocb has the file as the first member, so don't do it here */
446 struct kiocb kiocb;
447 u64 addr;
448 u64 len;
449};
450
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700451struct io_connect {
452 struct file *file;
453 struct sockaddr __user *addr;
454 int addr_len;
455};
456
Jens Axboee47293f2019-12-20 08:58:21 -0700457struct io_sr_msg {
458 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700459 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300460 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700461 void __user *buf;
462 };
Jens Axboee47293f2019-12-20 08:58:21 -0700463 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700464 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700465 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700466 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700467};
468
Jens Axboe15b71ab2019-12-11 11:20:36 -0700469struct io_open {
470 struct file *file;
471 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700472 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700473 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600474 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700475};
476
Jens Axboe05f3fb32019-12-09 11:22:50 -0700477struct io_files_update {
478 struct file *file;
479 u64 arg;
480 u32 nr_args;
481 u32 offset;
482};
483
Jens Axboe4840e412019-12-25 22:03:45 -0700484struct io_fadvise {
485 struct file *file;
486 u64 offset;
487 u32 len;
488 u32 advice;
489};
490
Jens Axboec1ca7572019-12-25 22:18:28 -0700491struct io_madvise {
492 struct file *file;
493 u64 addr;
494 u32 len;
495 u32 advice;
496};
497
Jens Axboe3e4827b2020-01-08 15:18:09 -0700498struct io_epoll {
499 struct file *file;
500 int epfd;
501 int op;
502 int fd;
503 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700504};
505
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300506struct io_splice {
507 struct file *file_out;
508 struct file *file_in;
509 loff_t off_out;
510 loff_t off_in;
511 u64 len;
512 unsigned int flags;
513};
514
Jens Axboeddf0322d2020-02-23 16:41:33 -0700515struct io_provide_buf {
516 struct file *file;
517 __u64 addr;
518 __s32 len;
519 __u32 bgid;
520 __u16 nbufs;
521 __u16 bid;
522};
523
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700524struct io_statx {
525 struct file *file;
526 int dfd;
527 unsigned int mask;
528 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700529 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700530 struct statx __user *buffer;
531};
532
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300533struct io_completion {
534 struct file *file;
535 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300536 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300537};
538
Jens Axboef499a022019-12-02 16:28:46 -0700539struct io_async_connect {
540 struct sockaddr_storage address;
541};
542
Jens Axboe03b12302019-12-02 18:50:25 -0700543struct io_async_msghdr {
544 struct iovec fast_iov[UIO_FASTIOV];
545 struct iovec *iov;
546 struct sockaddr __user *uaddr;
547 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700548 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700549};
550
Jens Axboef67676d2019-12-02 11:03:47 -0700551struct io_async_rw {
552 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600553 const struct iovec *free_iovec;
554 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600555 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600556 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700557};
558
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300559enum {
560 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
561 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
562 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
563 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
564 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700565 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300566
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300567 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568 REQ_F_FAIL_LINK_BIT,
569 REQ_F_INFLIGHT_BIT,
570 REQ_F_CUR_POS_BIT,
571 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300572 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300573 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300574 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300575 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700576 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700577 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600578 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800579 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700580
581 /* not a real bit, just to check we're not overflowing the space */
582 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300583};
584
585enum {
586 /* ctx owns file */
587 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
588 /* drain existing IO first */
589 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
590 /* linked sqes */
591 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
592 /* doesn't sever on completion < 0 */
593 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
594 /* IOSQE_ASYNC */
595 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700596 /* IOSQE_BUFFER_SELECT */
597 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300598
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300599 /* head of a link */
600 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300601 /* fail rest of links */
602 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
603 /* on inflight list */
604 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
605 /* read/write uses file position */
606 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
607 /* must not punt to workers */
608 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609 /* has linked timeout */
610 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300611 /* regular file */
612 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300613 /* completion under lock */
614 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300615 /* needs cleanup */
616 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700617 /* already went through poll handler */
618 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700619 /* buffer already selected */
620 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600621 /* doesn't need file table for this request */
622 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800623 /* io_wq_work is initialized */
624 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700625};
626
627struct async_poll {
628 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600629 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300630};
631
Jens Axboe09bb8392019-03-13 12:39:28 -0600632/*
633 * NOTE! Each of the iocb union members has the file pointer
634 * as the first entry in their struct definition. So you can
635 * access the file pointer through any of the sub-structs,
636 * or directly as just 'ki_filp' in this struct.
637 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700639 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600640 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700641 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700642 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700643 struct io_accept accept;
644 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700645 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700646 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700647 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700648 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700649 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700650 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700651 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700652 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700653 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700654 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300655 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700656 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700657 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300658 /* use only after cleaning per-op data, see io_clean_op() */
659 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700660 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700661
Jens Axboee8c2bc12020-08-15 18:44:09 -0700662 /* opcode allocated if it needs to store data for async defer */
663 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700664 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800665 /* polled IO has completed */
666 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700667
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700668 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300669 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700670
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300671 struct io_ring_ctx *ctx;
672 unsigned int flags;
673 refcount_t refs;
674 struct task_struct *task;
675 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700676
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300677 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700678
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300679 /*
680 * 1. used with ctx->iopoll_list with reads/writes
681 * 2. to track reqs with ->files (see io_op_def::file_table)
682 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300683 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600684
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300685 struct percpu_ref *fixed_file_refs;
686 struct callback_head task_work;
687 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
688 struct hlist_node hash_node;
689 struct async_poll *apoll;
690 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700691};
692
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300693struct io_defer_entry {
694 struct list_head list;
695 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300696 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300697};
698
Jens Axboedef596e2019-01-09 08:59:42 -0700699#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700700
Jens Axboe013538b2020-06-22 09:29:15 -0600701struct io_comp_state {
702 unsigned int nr;
703 struct list_head list;
704 struct io_ring_ctx *ctx;
705};
706
Jens Axboe9a56a232019-01-09 09:06:50 -0700707struct io_submit_state {
708 struct blk_plug plug;
709
710 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700711 * io_kiocb alloc cache
712 */
713 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300714 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700715
716 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600717 * Batch completion logic
718 */
719 struct io_comp_state comp;
720
721 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700722 * File reference cache
723 */
724 struct file *file;
725 unsigned int fd;
726 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700727 unsigned int ios_left;
728};
729
Jens Axboed3656342019-12-18 09:50:26 -0700730struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700731 /* needs current->mm setup, does mm access */
732 unsigned needs_mm : 1;
733 /* needs req->file assigned */
734 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600735 /* don't fail if file grab fails */
736 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700737 /* hash wq insertion if file is a regular file */
738 unsigned hash_reg_file : 1;
739 /* unbound wq insertion if file is a non-regular file */
740 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700741 /* opcode is not supported by this kernel */
742 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700743 /* needs file table */
744 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700745 /* needs ->fs */
746 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700747 /* set if opcode supports polled "wait" */
748 unsigned pollin : 1;
749 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700750 /* op supports buffer selection */
751 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700752 /* needs rlimit(RLIMIT_FSIZE) assigned */
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300753 unsigned needs_fsize : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700754 /* must always have async data allocated */
755 unsigned needs_async_data : 1;
Dennis Zhou91d8f512020-09-16 13:41:05 -0700756 /* needs blkcg context, issues async io potentially */
757 unsigned needs_blkcg : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700758 /* size of async data needed, if any */
759 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700760};
761
Jens Axboe738277a2020-09-03 05:54:56 -0600762static const struct io_op_def io_op_defs[] __read_mostly = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_NOP] = {},
764 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700765 .needs_mm = 1,
766 .needs_file = 1,
767 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700768 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700769 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700770 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700771 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700772 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_mm = 1,
776 .needs_file = 1,
777 .hash_reg_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700779 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300780 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700781 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700782 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700783 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700787 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700790 .needs_file = 1,
791 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700792 .pollin = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700793 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700794 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_file = 1,
798 .hash_reg_file = 1,
799 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700800 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300801 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700802 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700803 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700804 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300805 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700806 .needs_file = 1,
807 .unbound_nonreg_file = 1,
808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_POLL_REMOVE] = {},
810 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700811 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700812 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700815 .needs_mm = 1,
816 .needs_file = 1,
817 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700818 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700819 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700820 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700821 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700822 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700823 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300824 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700825 .needs_mm = 1,
826 .needs_file = 1,
827 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700828 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700829 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700830 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700831 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700832 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700833 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700837 .needs_async_data = 1,
838 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_TIMEOUT_REMOVE] = {},
841 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700842 .needs_mm = 1,
843 .needs_file = 1,
844 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700845 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700846 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700847 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_ASYNC_CANCEL] = {},
849 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700850 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700851 .needs_async_data = 1,
852 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700855 .needs_mm = 1,
856 .needs_file = 1,
857 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700858 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700859 .needs_async_data = 1,
860 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700863 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300864 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700865 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700868 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700869 .needs_fs = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700870 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700871 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300872 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600873 .needs_file = 1,
874 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700875 .file_table = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700876 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700879 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700880 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700883 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700884 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600885 .file_table = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700886 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700889 .needs_mm = 1,
890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700892 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700893 .buffer_select = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700894 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700895 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700898 .needs_mm = 1,
899 .needs_file = 1,
900 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700901 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300902 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700903 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700904 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700907 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700908 .needs_blkcg = 1,
Jens Axboe4840e412019-12-25 22:03:45 -0700909 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300910 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700911 .needs_mm = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700912 .needs_blkcg = 1,
Jens Axboec1ca7572019-12-25 22:18:28 -0700913 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300914 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700915 .needs_mm = 1,
916 .needs_file = 1,
917 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700918 .pollout = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700919 .needs_blkcg = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700922 .needs_mm = 1,
923 .needs_file = 1,
924 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700925 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700926 .buffer_select = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700927 .needs_blkcg = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700928 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300929 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700930 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700931 .needs_fs = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700932 .needs_blkcg = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700933 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700934 [IORING_OP_EPOLL_CTL] = {
935 .unbound_nonreg_file = 1,
936 .file_table = 1,
937 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300938 [IORING_OP_SPLICE] = {
939 .needs_file = 1,
940 .hash_reg_file = 1,
941 .unbound_nonreg_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700942 .needs_blkcg = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700943 },
944 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700945 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300946 [IORING_OP_TEE] = {
947 .needs_file = 1,
948 .hash_reg_file = 1,
949 .unbound_nonreg_file = 1,
950 },
Jens Axboed3656342019-12-18 09:50:26 -0700951};
952
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700953enum io_mem_account {
954 ACCT_LOCKED,
955 ACCT_PINNED,
956};
957
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300958static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
959 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700960static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800961static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600962static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700963static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700964static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600965static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700966static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700967static int __io_sqe_files_update(struct io_ring_ctx *ctx,
968 struct io_uring_files_update *ip,
969 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300970static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +0100971static struct file *io_file_get(struct io_submit_state *state,
972 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +0300973static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600974static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600975
Jens Axboeb63534c2020-06-04 11:28:00 -0600976static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
977 struct iovec **iovec, struct iov_iter *iter,
978 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600979static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
980 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600981 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700982
983static struct kmem_cache *req_cachep;
984
Jens Axboe738277a2020-09-03 05:54:56 -0600985static const struct file_operations io_uring_fops __read_mostly;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700986
987struct sock *io_uring_get_socket(struct file *file)
988{
989#if defined(CONFIG_UNIX)
990 if (file->f_op == &io_uring_fops) {
991 struct io_ring_ctx *ctx = file->private_data;
992
993 return ctx->ring_sock->sk;
994 }
995#endif
996 return NULL;
997}
998EXPORT_SYMBOL(io_uring_get_socket);
999
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001000static inline void io_clean_op(struct io_kiocb *req)
1001{
Pavel Begunkovbb175342020-08-20 11:33:35 +03001002 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
1003 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001004 __io_clean_op(req);
1005}
1006
Jens Axboe4349f302020-07-09 15:07:01 -06001007static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001008{
1009 struct mm_struct *mm = current->mm;
1010
1011 if (mm) {
1012 kthread_unuse_mm(mm);
1013 mmput(mm);
1014 }
1015}
1016
1017static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1018{
1019 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +03001020 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -06001021 !ctx->sqo_task->mm ||
1022 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -06001023 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -06001024 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -06001025 }
1026
1027 return 0;
1028}
1029
1030static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1031 struct io_kiocb *req)
1032{
1033 if (!io_op_defs[req->opcode].needs_mm)
1034 return 0;
1035 return __io_sq_thread_acquire_mm(ctx);
1036}
1037
Dennis Zhou91d8f512020-09-16 13:41:05 -07001038static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1039 struct cgroup_subsys_state **cur_css)
1040
1041{
1042#ifdef CONFIG_BLK_CGROUP
1043 /* puts the old one when swapping */
1044 if (*cur_css != ctx->sqo_blkcg_css) {
1045 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1046 *cur_css = ctx->sqo_blkcg_css;
1047 }
1048#endif
1049}
1050
1051static void io_sq_thread_unassociate_blkcg(void)
1052{
1053#ifdef CONFIG_BLK_CGROUP
1054 kthread_associate_blkcg(NULL);
1055#endif
1056}
1057
Jens Axboec40f6372020-06-25 15:39:59 -06001058static inline void req_set_fail_links(struct io_kiocb *req)
1059{
1060 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1061 req->flags |= REQ_F_FAIL_LINK;
1062}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001063
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001064/*
1065 * Note: must call io_req_init_async() for the first time you
1066 * touch any members of io_wq_work.
1067 */
1068static inline void io_req_init_async(struct io_kiocb *req)
1069{
1070 if (req->flags & REQ_F_WORK_INITIALIZED)
1071 return;
1072
1073 memset(&req->work, 0, sizeof(req->work));
1074 req->flags |= REQ_F_WORK_INITIALIZED;
1075}
1076
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001077static inline bool io_async_submit(struct io_ring_ctx *ctx)
1078{
1079 return ctx->flags & IORING_SETUP_SQPOLL;
1080}
1081
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1083{
1084 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1085
Jens Axboe0f158b42020-05-14 17:18:39 -06001086 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087}
1088
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001089static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1090{
1091 return !req->timeout.off;
1092}
1093
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1095{
1096 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001097 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098
1099 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1100 if (!ctx)
1101 return NULL;
1102
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001103 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1104 if (!ctx->fallback_req)
1105 goto err;
1106
Jens Axboe78076bb2019-12-04 19:56:40 -07001107 /*
1108 * Use 5 bits less than the max cq entries, that should give us around
1109 * 32 entries per hash list if totally full and uniformly spread.
1110 */
1111 hash_bits = ilog2(p->cq_entries);
1112 hash_bits -= 5;
1113 if (hash_bits <= 0)
1114 hash_bits = 1;
1115 ctx->cancel_hash_bits = hash_bits;
1116 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1117 GFP_KERNEL);
1118 if (!ctx->cancel_hash)
1119 goto err;
1120 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1121
Roman Gushchin21482892019-05-07 10:01:48 -07001122 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001123 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1124 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125
1126 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001127 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001128 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001130 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001131 init_completion(&ctx->ref_comp);
1132 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001133 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001134 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001135 mutex_init(&ctx->uring_lock);
1136 init_waitqueue_head(&ctx->wait);
1137 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001138 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001139 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001140 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001141 init_waitqueue_head(&ctx->inflight_wait);
1142 spin_lock_init(&ctx->inflight_lock);
1143 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001144 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1145 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001147err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001148 if (ctx->fallback_req)
1149 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001150 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001151 kfree(ctx);
1152 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001153}
1154
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001155static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001156{
Jens Axboe2bc99302020-07-09 09:43:27 -06001157 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1158 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001159
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001160 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001161 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001162 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001163
Bob Liu9d858b22019-11-13 18:06:25 +08001164 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001165}
1166
Jens Axboede0617e2019-04-06 21:51:27 -06001167static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168{
Hristo Venev75b28af2019-08-26 17:23:46 +00001169 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170
Pavel Begunkov07910152020-01-17 03:52:46 +03001171 /* order cqe stores with ring update */
1172 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173
Pavel Begunkov07910152020-01-17 03:52:46 +03001174 if (wq_has_sleeper(&ctx->cq_wait)) {
1175 wake_up_interruptible(&ctx->cq_wait);
1176 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177 }
1178}
1179
Jens Axboe51a4cc12020-08-10 10:55:56 -06001180/*
1181 * Returns true if we need to defer file table putting. This can only happen
1182 * from the error path with REQ_F_COMP_LOCKED set.
1183 */
1184static bool 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))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001187 return false;
1188
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
Jens Axboe51a4cc12020-08-10 10:55:56 -06001206 if (req->flags & REQ_F_COMP_LOCKED)
1207 return true;
1208
Jens Axboeff002b32020-02-07 16:05:21 -07001209 spin_lock(&req->work.fs->lock);
1210 if (--fs->users)
1211 fs = NULL;
1212 spin_unlock(&req->work.fs->lock);
1213 if (fs)
1214 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001215 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001216 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001217
1218 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001219}
1220
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001221static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001222{
Jens Axboed3656342019-12-18 09:50:26 -07001223 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001224 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001225
Pavel Begunkov16d59802020-07-12 16:16:47 +03001226 io_req_init_async(req);
1227
Jens Axboed3656342019-12-18 09:50:26 -07001228 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001229 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001230 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001231 } else {
1232 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001233 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001234 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001235 if (!req->work.files && io_op_defs[req->opcode].file_table &&
1236 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1237 req->work.files = get_files_struct(current);
1238 get_nsproxy(current->nsproxy);
1239 req->work.nsproxy = current->nsproxy;
1240 req->flags |= REQ_F_INFLIGHT;
1241
1242 spin_lock_irq(&ctx->inflight_lock);
1243 list_add(&req->inflight_entry, &ctx->inflight_list);
1244 spin_unlock_irq(&ctx->inflight_lock);
1245 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001246 if (!req->work.mm && def->needs_mm) {
1247 mmgrab(current->mm);
1248 req->work.mm = current->mm;
1249 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001250#ifdef CONFIG_BLK_CGROUP
1251 if (!req->work.blkcg_css && def->needs_blkcg) {
1252 rcu_read_lock();
1253 req->work.blkcg_css = blkcg_css();
1254 /*
1255 * This should be rare, either the cgroup is dying or the task
1256 * is moving cgroups. Just punt to root for the handful of ios.
1257 */
1258 if (!css_tryget_online(req->work.blkcg_css))
1259 req->work.blkcg_css = NULL;
1260 rcu_read_unlock();
1261 }
1262#endif
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001263 if (!req->work.creds)
1264 req->work.creds = get_current_cred();
1265 if (!req->work.fs && def->needs_fs) {
1266 spin_lock(&current->fs->lock);
1267 if (!current->fs->in_exec) {
1268 req->work.fs = current->fs;
1269 req->work.fs->users++;
1270 } else {
1271 req->work.flags |= IO_WQ_WORK_CANCEL;
1272 }
1273 spin_unlock(&current->fs->lock);
1274 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001275 if (def->needs_fsize)
1276 req->work.fsize = rlimit(RLIMIT_FSIZE);
1277 else
1278 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001279}
1280
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001281static void io_prep_async_link(struct io_kiocb *req)
1282{
1283 struct io_kiocb *cur;
1284
1285 io_prep_async_work(req);
1286 if (req->flags & REQ_F_LINK_HEAD)
1287 list_for_each_entry(cur, &req->link_list, link_list)
1288 io_prep_async_work(cur);
1289}
1290
Jens Axboe7271ef32020-08-10 09:55:22 -06001291static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001292{
Jackie Liua197f662019-11-08 08:09:12 -07001293 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001294 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001295
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001296 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1297 &req->work, req->flags);
1298 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001299 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001300}
1301
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001302static void io_queue_async_work(struct io_kiocb *req)
1303{
Jens Axboe7271ef32020-08-10 09:55:22 -06001304 struct io_kiocb *link;
1305
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001306 /* init ->work of the whole link before punting */
1307 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001308 link = __io_queue_async_work(req);
1309
1310 if (link)
1311 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001312}
1313
Jens Axboe5262f562019-09-17 12:26:57 -06001314static void io_kill_timeout(struct io_kiocb *req)
1315{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001316 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001317 int ret;
1318
Jens Axboee8c2bc12020-08-15 18:44:09 -07001319 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001320 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001321 atomic_set(&req->ctx->cq_timeouts,
1322 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001323 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001324 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001325 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001326 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001327 }
1328}
1329
Jens Axboef3606e32020-09-22 08:18:24 -06001330static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1331{
1332 struct io_ring_ctx *ctx = req->ctx;
1333
1334 if (!tsk || req->task == tsk)
1335 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001336 if (ctx->flags & IORING_SETUP_SQPOLL) {
1337 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1338 return true;
1339 }
Jens Axboef3606e32020-09-22 08:18:24 -06001340 return false;
1341}
1342
Jens Axboe76e1b642020-09-26 15:05:03 -06001343/*
1344 * Returns true if we found and killed one or more timeouts
1345 */
1346static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001347{
1348 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001349 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001350
1351 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001352 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001353 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001354 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001355 canceled++;
1356 }
Jens Axboef3606e32020-09-22 08:18:24 -06001357 }
Jens Axboe5262f562019-09-17 12:26:57 -06001358 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001359 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001360}
1361
Pavel Begunkov04518942020-05-26 20:34:05 +03001362static void __io_queue_deferred(struct io_ring_ctx *ctx)
1363{
1364 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001365 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1366 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001367 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001368
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001369 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001370 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001371 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001372 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001373 link = __io_queue_async_work(de->req);
1374 if (link) {
1375 __io_queue_linked_timeout(link);
1376 /* drop submission reference */
1377 link->flags |= REQ_F_COMP_LOCKED;
1378 io_put_req(link);
1379 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001380 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001381 } while (!list_empty(&ctx->defer_list));
1382}
1383
Pavel Begunkov360428f2020-05-30 14:54:17 +03001384static void io_flush_timeouts(struct io_ring_ctx *ctx)
1385{
1386 while (!list_empty(&ctx->timeout_list)) {
1387 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001388 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001389
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001390 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001391 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001392 if (req->timeout.target_seq != ctx->cached_cq_tail
1393 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001394 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001395
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001396 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001397 io_kill_timeout(req);
1398 }
1399}
1400
Jens Axboede0617e2019-04-06 21:51:27 -06001401static void io_commit_cqring(struct io_ring_ctx *ctx)
1402{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001403 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001404 __io_commit_cqring(ctx);
1405
Pavel Begunkov04518942020-05-26 20:34:05 +03001406 if (unlikely(!list_empty(&ctx->defer_list)))
1407 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001408}
1409
Jens Axboe90554202020-09-03 12:12:41 -06001410static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1411{
1412 struct io_rings *r = ctx->rings;
1413
1414 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1415}
1416
Jens Axboe2b188cc2019-01-07 10:46:33 -07001417static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1418{
Hristo Venev75b28af2019-08-26 17:23:46 +00001419 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 unsigned tail;
1421
1422 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001423 /*
1424 * writes to the cq entry need to come after reading head; the
1425 * control dependency is enough as we're using WRITE_ONCE to
1426 * fill the cq entry
1427 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001428 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429 return NULL;
1430
1431 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001432 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001433}
1434
Jens Axboef2842ab2020-01-08 11:04:00 -07001435static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1436{
Jens Axboef0b493e2020-02-01 21:30:11 -07001437 if (!ctx->cq_ev_fd)
1438 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001439 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1440 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001441 if (!ctx->eventfd_async)
1442 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001443 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001444}
1445
Jens Axboeb41e9852020-02-17 09:52:41 -07001446static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001447{
1448 if (waitqueue_active(&ctx->wait))
1449 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001450 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1451 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001452 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001453 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001454}
1455
Pavel Begunkov46930142020-07-30 18:43:49 +03001456static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1457{
1458 if (list_empty(&ctx->cq_overflow_list)) {
1459 clear_bit(0, &ctx->sq_check_overflow);
1460 clear_bit(0, &ctx->cq_check_overflow);
1461 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1462 }
1463}
1464
Jens Axboee6c8aa92020-09-28 13:10:13 -06001465static inline bool io_match_files(struct io_kiocb *req,
1466 struct files_struct *files)
1467{
1468 if (!files)
1469 return true;
1470 if (req->flags & REQ_F_WORK_INITIALIZED)
1471 return req->work.files == files;
1472 return false;
1473}
1474
Jens Axboec4a2ed72019-11-21 21:01:26 -07001475/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001476static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1477 struct task_struct *tsk,
1478 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001480 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001481 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001482 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001483 unsigned long flags;
1484 LIST_HEAD(list);
1485
1486 if (!force) {
1487 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001488 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001489 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1490 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001491 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001492 }
1493
1494 spin_lock_irqsave(&ctx->completion_lock, flags);
1495
1496 /* if force is set, the ring is going away. always drop after that */
1497 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001498 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001499
Jens Axboec4a2ed72019-11-21 21:01:26 -07001500 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001501 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1502 if (tsk && req->task != tsk)
1503 continue;
1504 if (!io_match_files(req, files))
1505 continue;
1506
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001507 cqe = io_get_cqring(ctx);
1508 if (!cqe && !force)
1509 break;
1510
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001511 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001512 if (cqe) {
1513 WRITE_ONCE(cqe->user_data, req->user_data);
1514 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001515 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001516 } else {
1517 WRITE_ONCE(ctx->rings->cq_overflow,
1518 atomic_inc_return(&ctx->cached_cq_overflow));
1519 }
1520 }
1521
1522 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001523 io_cqring_mark_overflow(ctx);
1524
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001525 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1526 io_cqring_ev_posted(ctx);
1527
1528 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001529 req = list_first_entry(&list, struct io_kiocb, compl.list);
1530 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001531 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001532 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001533
1534 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001535}
1536
Jens Axboebcda7ba2020-02-23 16:42:51 -07001537static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001539 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001540 struct io_uring_cqe *cqe;
1541
Jens Axboe78e19bb2019-11-06 15:21:34 -07001542 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001543
Jens Axboe2b188cc2019-01-07 10:46:33 -07001544 /*
1545 * If we can't get a cq entry, userspace overflowed the
1546 * submission (by quite a lot). Increment the overflow count in
1547 * the ring.
1548 */
1549 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001550 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001551 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001552 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001553 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001554 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1555 /*
1556 * If we're in ring overflow flush mode, or in task cancel mode,
1557 * then we cannot store the request for later flushing, we need
1558 * to drop it on the floor.
1559 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001560 WRITE_ONCE(ctx->rings->cq_overflow,
1561 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001562 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001563 if (list_empty(&ctx->cq_overflow_list)) {
1564 set_bit(0, &ctx->sq_check_overflow);
1565 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001566 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001567 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001568 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001569 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001570 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001571 refcount_inc(&req->refs);
1572 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001573 }
1574}
1575
Jens Axboebcda7ba2020-02-23 16:42:51 -07001576static void io_cqring_fill_event(struct io_kiocb *req, long res)
1577{
1578 __io_cqring_fill_event(req, res, 0);
1579}
1580
Jens Axboee1e16092020-06-22 09:17:17 -06001581static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001582{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001583 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001584 unsigned long flags;
1585
1586 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001587 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001588 io_commit_cqring(ctx);
1589 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1590
Jens Axboe8c838782019-03-12 15:48:16 -06001591 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001592}
1593
Jens Axboe229a7b62020-06-22 10:13:11 -06001594static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001595{
Jens Axboe229a7b62020-06-22 10:13:11 -06001596 struct io_ring_ctx *ctx = cs->ctx;
1597
1598 spin_lock_irq(&ctx->completion_lock);
1599 while (!list_empty(&cs->list)) {
1600 struct io_kiocb *req;
1601
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001602 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1603 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001604 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001605 if (!(req->flags & REQ_F_LINK_HEAD)) {
1606 req->flags |= REQ_F_COMP_LOCKED;
1607 io_put_req(req);
1608 } else {
1609 spin_unlock_irq(&ctx->completion_lock);
1610 io_put_req(req);
1611 spin_lock_irq(&ctx->completion_lock);
1612 }
1613 }
1614 io_commit_cqring(ctx);
1615 spin_unlock_irq(&ctx->completion_lock);
1616
1617 io_cqring_ev_posted(ctx);
1618 cs->nr = 0;
1619}
1620
1621static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1622 struct io_comp_state *cs)
1623{
1624 if (!cs) {
1625 io_cqring_add_event(req, res, cflags);
1626 io_put_req(req);
1627 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001628 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001629 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001630 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001631 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001632 if (++cs->nr >= 32)
1633 io_submit_flush_completions(cs);
1634 }
Jens Axboee1e16092020-06-22 09:17:17 -06001635}
1636
1637static void io_req_complete(struct io_kiocb *req, long res)
1638{
Jens Axboe229a7b62020-06-22 10:13:11 -06001639 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001640}
1641
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001642static inline bool io_is_fallback_req(struct io_kiocb *req)
1643{
1644 return req == (struct io_kiocb *)
1645 ((unsigned long) req->ctx->fallback_req & ~1UL);
1646}
1647
1648static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1649{
1650 struct io_kiocb *req;
1651
1652 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001653 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001654 return req;
1655
1656 return NULL;
1657}
1658
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001659static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1660 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001661{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001662 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001663 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001664 size_t sz;
1665 int ret;
1666
1667 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001668 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1669
1670 /*
1671 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1672 * retry single alloc to be on the safe side.
1673 */
1674 if (unlikely(ret <= 0)) {
1675 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1676 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001677 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001678 ret = 1;
1679 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001680 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681 }
1682
Pavel Begunkov291b2822020-09-30 22:57:01 +03001683 state->free_reqs--;
1684 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001685fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001686 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001687}
1688
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001689static inline void io_put_file(struct io_kiocb *req, struct file *file,
1690 bool fixed)
1691{
1692 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001693 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001694 else
1695 fput(file);
1696}
1697
Jens Axboe51a4cc12020-08-10 10:55:56 -06001698static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001699{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001700 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001701
Jens Axboee8c2bc12020-08-15 18:44:09 -07001702 if (req->async_data)
1703 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001704 if (req->file)
1705 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001706
Jens Axboe51a4cc12020-08-10 10:55:56 -06001707 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001708}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001709
Jens Axboe51a4cc12020-08-10 10:55:56 -06001710static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001711{
Jens Axboe0f212202020-09-13 13:09:39 -06001712 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001713 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001714
Jens Axboe0f212202020-09-13 13:09:39 -06001715 atomic_long_inc(&tctx->req_complete);
1716 if (tctx->in_idle)
1717 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001718 put_task_struct(req->task);
1719
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001720 if (likely(!io_is_fallback_req(req)))
1721 kmem_cache_free(req_cachep, req);
1722 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001723 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1724 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001725}
1726
Jens Axboe51a4cc12020-08-10 10:55:56 -06001727static void io_req_task_file_table_put(struct callback_head *cb)
1728{
1729 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1730 struct fs_struct *fs = req->work.fs;
1731
1732 spin_lock(&req->work.fs->lock);
1733 if (--fs->users)
1734 fs = NULL;
1735 spin_unlock(&req->work.fs->lock);
1736 if (fs)
1737 free_fs_struct(fs);
1738 req->work.fs = NULL;
1739 __io_free_req_finish(req);
1740}
1741
1742static void __io_free_req(struct io_kiocb *req)
1743{
1744 if (!io_dismantle_req(req)) {
1745 __io_free_req_finish(req);
1746 } else {
1747 int ret;
1748
1749 init_task_work(&req->task_work, io_req_task_file_table_put);
1750 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1751 if (unlikely(ret)) {
1752 struct task_struct *tsk;
1753
1754 tsk = io_wq_get_task(req->ctx->io_wq);
1755 task_work_add(tsk, &req->task_work, 0);
1756 }
1757 }
1758}
1759
Jackie Liua197f662019-11-08 08:09:12 -07001760static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001761{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001762 struct io_timeout_data *io = req->async_data;
Jackie Liua197f662019-11-08 08:09:12 -07001763 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001764 int ret;
1765
Jens Axboee8c2bc12020-08-15 18:44:09 -07001766 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001767 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001768 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001769 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001770 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001771 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001772 return true;
1773 }
1774
1775 return false;
1776}
1777
Jens Axboeab0b6452020-06-30 08:43:15 -06001778static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001779{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001780 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001781 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001782
1783 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001784 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001785 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1786 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001787 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001788
1789 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001790 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001791 wake_ev = io_link_cancel_timeout(link);
1792 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001793 return wake_ev;
1794}
1795
1796static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001797{
Jens Axboe2665abf2019-11-05 12:40:47 -07001798 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001799 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001800
Jens Axboeab0b6452020-06-30 08:43:15 -06001801 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1802 unsigned long flags;
1803
1804 spin_lock_irqsave(&ctx->completion_lock, flags);
1805 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001806 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001807 } else {
1808 wake_ev = __io_kill_linked_timeout(req);
1809 }
1810
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001811 if (wake_ev)
1812 io_cqring_ev_posted(ctx);
1813}
1814
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001815static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001816{
1817 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001818
Jens Axboe9e645e112019-05-10 16:07:28 -06001819 /*
1820 * The list should never be empty when we are called here. But could
1821 * potentially happen if the chain is messed up, check to be on the
1822 * safe side.
1823 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001824 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001825 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001826
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001827 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1828 list_del_init(&req->link_list);
1829 if (!list_empty(&nxt->link_list))
1830 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001831 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001832}
1833
1834/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001835 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001836 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001837static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001838{
Jens Axboe2665abf2019-11-05 12:40:47 -07001839 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001840
1841 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001842 struct io_kiocb *link = list_first_entry(&req->link_list,
1843 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001844
Pavel Begunkov44932332019-12-05 16:16:35 +03001845 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001846 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001847
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001848 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001849 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001850 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001851 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001852 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001853
1854 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001855 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001856}
1857
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001858static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001859{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001860 struct io_ring_ctx *ctx = req->ctx;
1861
1862 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1863 unsigned long flags;
1864
1865 spin_lock_irqsave(&ctx->completion_lock, flags);
1866 __io_fail_links(req);
1867 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1868 } else {
1869 __io_fail_links(req);
1870 }
1871
Jens Axboe9e645e112019-05-10 16:07:28 -06001872 io_cqring_ev_posted(ctx);
1873}
1874
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001875static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001876{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001877 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001878 if (req->flags & REQ_F_LINK_TIMEOUT)
1879 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001880
Jens Axboe9e645e112019-05-10 16:07:28 -06001881 /*
1882 * If LINK is set, we have dependent requests in this chain. If we
1883 * didn't fail this request, queue the first one up, moving any other
1884 * dependencies to the next request. In case of failure, fail the rest
1885 * of the chain.
1886 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001887 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1888 return io_req_link_next(req);
1889 io_fail_links(req);
1890 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001891}
Jens Axboe2665abf2019-11-05 12:40:47 -07001892
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001893static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1894{
1895 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1896 return NULL;
1897 return __io_req_find_next(req);
1898}
1899
Jens Axboe87c43112020-09-30 21:00:14 -06001900static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001901{
1902 struct task_struct *tsk = req->task;
1903 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001904 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001905
Jens Axboe6200b0a2020-09-13 14:38:30 -06001906 if (tsk->flags & PF_EXITING)
1907 return -ESRCH;
1908
Jens Axboec2c4c832020-07-01 15:37:11 -06001909 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001910 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1911 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1912 * processing task_work. There's no reliable way to tell if TWA_RESUME
1913 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001914 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001915 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001916 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001917 notify = TWA_SIGNAL;
1918
Jens Axboe87c43112020-09-30 21:00:14 -06001919 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001920 if (!ret)
1921 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001922
Jens Axboec2c4c832020-07-01 15:37:11 -06001923 return ret;
1924}
1925
Jens Axboec40f6372020-06-25 15:39:59 -06001926static void __io_req_task_cancel(struct io_kiocb *req, int error)
1927{
1928 struct io_ring_ctx *ctx = req->ctx;
1929
1930 spin_lock_irq(&ctx->completion_lock);
1931 io_cqring_fill_event(req, error);
1932 io_commit_cqring(ctx);
1933 spin_unlock_irq(&ctx->completion_lock);
1934
1935 io_cqring_ev_posted(ctx);
1936 req_set_fail_links(req);
1937 io_double_put_req(req);
1938}
1939
1940static void io_req_task_cancel(struct callback_head *cb)
1941{
1942 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001943 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001944
1945 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001946 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001947}
1948
1949static void __io_req_task_submit(struct io_kiocb *req)
1950{
1951 struct io_ring_ctx *ctx = req->ctx;
1952
Jens Axboec40f6372020-06-25 15:39:59 -06001953 if (!__io_sq_thread_acquire_mm(ctx)) {
1954 mutex_lock(&ctx->uring_lock);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001955 __io_queue_sqe(req, NULL);
Jens Axboec40f6372020-06-25 15:39:59 -06001956 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001957 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001958 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001959 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001960}
1961
Jens Axboec40f6372020-06-25 15:39:59 -06001962static void io_req_task_submit(struct callback_head *cb)
1963{
1964 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001965 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001966
1967 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001968 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001969}
1970
1971static void io_req_task_queue(struct io_kiocb *req)
1972{
Jens Axboec40f6372020-06-25 15:39:59 -06001973 int ret;
1974
1975 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001976 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001977
Jens Axboe87c43112020-09-30 21:00:14 -06001978 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001979 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001980 struct task_struct *tsk;
1981
Jens Axboec40f6372020-06-25 15:39:59 -06001982 init_task_work(&req->task_work, io_req_task_cancel);
1983 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001984 task_work_add(tsk, &req->task_work, 0);
1985 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001986 }
Jens Axboec40f6372020-06-25 15:39:59 -06001987}
1988
Pavel Begunkovc3524382020-06-28 12:52:32 +03001989static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001990{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001991 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001992
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001993 if (nxt)
1994 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001995}
1996
Jens Axboe9e645e112019-05-10 16:07:28 -06001997static void io_free_req(struct io_kiocb *req)
1998{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001999 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002000 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002001}
2002
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002003struct req_batch {
2004 void *reqs[IO_IOPOLL_BATCH];
2005 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002006
2007 struct task_struct *task;
2008 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002009};
2010
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002011static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002012{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002013 rb->to_free = 0;
2014 rb->task_refs = 0;
2015 rb->task = NULL;
2016}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002017
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002018static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2019 struct req_batch *rb)
2020{
2021 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2022 percpu_ref_put_many(&ctx->refs, rb->to_free);
2023 rb->to_free = 0;
2024}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002025
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002026static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2027 struct req_batch *rb)
2028{
2029 if (rb->to_free)
2030 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002031 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002032 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002033 put_task_struct_many(rb->task, rb->task_refs);
2034 rb->task = NULL;
2035 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002036}
2037
2038static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2039{
2040 if (unlikely(io_is_fallback_req(req))) {
2041 io_free_req(req);
2042 return;
2043 }
2044 if (req->flags & REQ_F_LINK_HEAD)
2045 io_queue_next(req);
2046
Jens Axboee3bc8e92020-09-24 08:45:57 -06002047 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002048 if (rb->task) {
2049 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002050 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06002051 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002052 rb->task = req->task;
2053 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002054 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002055 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002056
Jens Axboe51a4cc12020-08-10 10:55:56 -06002057 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002058 rb->reqs[rb->to_free++] = req;
2059 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2060 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002061}
2062
Jens Axboeba816ad2019-09-28 11:36:45 -06002063/*
2064 * Drop reference to request, return next in chain (if there is one) if this
2065 * was the last reference to this request.
2066 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002067static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002068{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002069 struct io_kiocb *nxt = NULL;
2070
Jens Axboe2a44f462020-02-25 13:25:41 -07002071 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002072 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002073 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002074 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002075 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002076}
2077
Jens Axboe2b188cc2019-01-07 10:46:33 -07002078static void io_put_req(struct io_kiocb *req)
2079{
Jens Axboedef596e2019-01-09 08:59:42 -07002080 if (refcount_dec_and_test(&req->refs))
2081 io_free_req(req);
2082}
2083
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002084static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002085{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002086 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002087
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002088 /*
2089 * A ref is owned by io-wq in which context we're. So, if that's the
2090 * last one, it's safe to steal next work. False negatives are Ok,
2091 * it just will be re-punted async in io_put_work()
2092 */
2093 if (refcount_read(&req->refs) != 1)
2094 return NULL;
2095
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002096 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002097 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002098}
2099
Jens Axboe978db572019-11-14 22:39:04 -07002100/*
2101 * Must only be used if we don't need to care about links, usually from
2102 * within the completion handling itself.
2103 */
2104static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002105{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002106 /* drop both submit and complete references */
2107 if (refcount_sub_and_test(2, &req->refs))
2108 __io_free_req(req);
2109}
2110
Jens Axboe978db572019-11-14 22:39:04 -07002111static void io_double_put_req(struct io_kiocb *req)
2112{
2113 /* drop both submit and complete references */
2114 if (refcount_sub_and_test(2, &req->refs))
2115 io_free_req(req);
2116}
2117
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002118static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002119{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002120 struct io_rings *rings = ctx->rings;
2121
Jens Axboead3eb2c2019-12-18 17:12:20 -07002122 if (test_bit(0, &ctx->cq_check_overflow)) {
2123 /*
2124 * noflush == true is from the waitqueue handler, just ensure
2125 * we wake up the task, and the next invocation will flush the
2126 * entries. We cannot safely to it from here.
2127 */
2128 if (noflush && !list_empty(&ctx->cq_overflow_list))
2129 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002130
Jens Axboee6c8aa92020-09-28 13:10:13 -06002131 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002132 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002133
Jens Axboea3a0e432019-08-20 11:03:11 -06002134 /* See comment at the top of this file */
2135 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002136 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002137}
2138
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002139static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2140{
2141 struct io_rings *rings = ctx->rings;
2142
2143 /* make sure SQ entry isn't read before tail */
2144 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2145}
2146
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002147static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002148{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002149 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002150
Jens Axboebcda7ba2020-02-23 16:42:51 -07002151 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2152 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002153 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002154 kfree(kbuf);
2155 return cflags;
2156}
2157
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002158static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2159{
2160 struct io_buffer *kbuf;
2161
2162 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2163 return io_put_kbuf(req, kbuf);
2164}
2165
Jens Axboe4c6e2772020-07-01 11:29:10 -06002166static inline bool io_run_task_work(void)
2167{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002168 /*
2169 * Not safe to run on exiting task, and the task_work handling will
2170 * not add work to such a task.
2171 */
2172 if (unlikely(current->flags & PF_EXITING))
2173 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002174 if (current->task_works) {
2175 __set_current_state(TASK_RUNNING);
2176 task_work_run();
2177 return true;
2178 }
2179
2180 return false;
2181}
2182
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002183static void io_iopoll_queue(struct list_head *again)
2184{
2185 struct io_kiocb *req;
2186
2187 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002188 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2189 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002190 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002191 } while (!list_empty(again));
2192}
2193
Jens Axboedef596e2019-01-09 08:59:42 -07002194/*
2195 * Find and free completed poll iocbs
2196 */
2197static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2198 struct list_head *done)
2199{
Jens Axboe8237e042019-12-28 10:48:22 -07002200 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002201 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002202 LIST_HEAD(again);
2203
2204 /* order with ->result store in io_complete_rw_iopoll() */
2205 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002206
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002207 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002208 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002209 int cflags = 0;
2210
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002211 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002212 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002213 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002214 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002215 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002216 continue;
2217 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002218 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002219
Jens Axboebcda7ba2020-02-23 16:42:51 -07002220 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002221 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002222
2223 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002224 (*nr_events)++;
2225
Pavel Begunkovc3524382020-06-28 12:52:32 +03002226 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002227 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002228 }
Jens Axboedef596e2019-01-09 08:59:42 -07002229
Jens Axboe09bb8392019-03-13 12:39:28 -06002230 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002231 if (ctx->flags & IORING_SETUP_SQPOLL)
2232 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002233 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002234
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002235 if (!list_empty(&again))
2236 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002237}
2238
Jens Axboedef596e2019-01-09 08:59:42 -07002239static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2240 long min)
2241{
2242 struct io_kiocb *req, *tmp;
2243 LIST_HEAD(done);
2244 bool spin;
2245 int ret;
2246
2247 /*
2248 * Only spin for completions if we don't have multiple devices hanging
2249 * off our complete list, and we're under the requested amount.
2250 */
2251 spin = !ctx->poll_multi_file && *nr_events < min;
2252
2253 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002254 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002255 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002256
2257 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002258 * Move completed and retryable entries to our local lists.
2259 * If we find a request that requires polling, break out
2260 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002261 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002262 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002263 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002264 continue;
2265 }
2266 if (!list_empty(&done))
2267 break;
2268
2269 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2270 if (ret < 0)
2271 break;
2272
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002273 /* iopoll may have completed current req */
2274 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002275 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002276
Jens Axboedef596e2019-01-09 08:59:42 -07002277 if (ret && spin)
2278 spin = false;
2279 ret = 0;
2280 }
2281
2282 if (!list_empty(&done))
2283 io_iopoll_complete(ctx, nr_events, &done);
2284
2285 return ret;
2286}
2287
2288/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002289 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002290 * non-spinning poll check - we'll still enter the driver poll loop, but only
2291 * as a non-spinning completion check.
2292 */
2293static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2294 long min)
2295{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002296 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002297 int ret;
2298
2299 ret = io_do_iopoll(ctx, nr_events, min);
2300 if (ret < 0)
2301 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002302 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002303 return 0;
2304 }
2305
2306 return 1;
2307}
2308
2309/*
2310 * We can't just wait for polled events to come to us, we have to actively
2311 * find and complete them.
2312 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002313static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002314{
2315 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2316 return;
2317
2318 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002319 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002320 unsigned int nr_events = 0;
2321
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002322 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002323
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002324 /* let it sleep and repeat later if can't complete a request */
2325 if (nr_events == 0)
2326 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002327 /*
2328 * Ensure we allow local-to-the-cpu processing to take place,
2329 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002330 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002331 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002332 if (need_resched()) {
2333 mutex_unlock(&ctx->uring_lock);
2334 cond_resched();
2335 mutex_lock(&ctx->uring_lock);
2336 }
Jens Axboedef596e2019-01-09 08:59:42 -07002337 }
2338 mutex_unlock(&ctx->uring_lock);
2339}
2340
Pavel Begunkov7668b922020-07-07 16:36:21 +03002341static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002342{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002343 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002344 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002345
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002346 /*
2347 * We disallow the app entering submit/complete with polling, but we
2348 * still need to lock the ring to prevent racing with polled issue
2349 * that got punted to a workqueue.
2350 */
2351 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002352 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002353 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002354 * Don't enter poll loop if we already have events pending.
2355 * If we do, we can potentially be spinning for commands that
2356 * already triggered a CQE (eg in error).
2357 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002358 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002359 break;
2360
2361 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002362 * If a submit got punted to a workqueue, we can have the
2363 * application entering polling for a command before it gets
2364 * issued. That app will hold the uring_lock for the duration
2365 * of the poll right here, so we need to take a breather every
2366 * now and then to ensure that the issue has a chance to add
2367 * the poll to the issued list. Otherwise we can spin here
2368 * forever, while the workqueue is stuck trying to acquire the
2369 * very same mutex.
2370 */
2371 if (!(++iters & 7)) {
2372 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002373 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002374 mutex_lock(&ctx->uring_lock);
2375 }
2376
Pavel Begunkov7668b922020-07-07 16:36:21 +03002377 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002378 if (ret <= 0)
2379 break;
2380 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002381 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002382
Jens Axboe500f9fb2019-08-19 12:15:59 -06002383 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002384 return ret;
2385}
2386
Jens Axboe491381ce2019-10-17 09:20:46 -06002387static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002388{
Jens Axboe491381ce2019-10-17 09:20:46 -06002389 /*
2390 * Tell lockdep we inherited freeze protection from submission
2391 * thread.
2392 */
2393 if (req->flags & REQ_F_ISREG) {
2394 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002395
Jens Axboe491381ce2019-10-17 09:20:46 -06002396 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002397 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002398 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002399}
2400
Jens Axboea1d7c392020-06-22 11:09:46 -06002401static void io_complete_rw_common(struct kiocb *kiocb, long res,
2402 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002403{
Jens Axboe9adbd452019-12-20 08:45:55 -07002404 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002405 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002406
Jens Axboe491381ce2019-10-17 09:20:46 -06002407 if (kiocb->ki_flags & IOCB_WRITE)
2408 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002409
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002410 if (res != req->result)
2411 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002412 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002413 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002414 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002415}
2416
Jens Axboeb63534c2020-06-04 11:28:00 -06002417#ifdef CONFIG_BLOCK
2418static bool io_resubmit_prep(struct io_kiocb *req, int error)
2419{
2420 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2421 ssize_t ret = -ECANCELED;
2422 struct iov_iter iter;
2423 int rw;
2424
2425 if (error) {
2426 ret = error;
2427 goto end_req;
2428 }
2429
2430 switch (req->opcode) {
2431 case IORING_OP_READV:
2432 case IORING_OP_READ_FIXED:
2433 case IORING_OP_READ:
2434 rw = READ;
2435 break;
2436 case IORING_OP_WRITEV:
2437 case IORING_OP_WRITE_FIXED:
2438 case IORING_OP_WRITE:
2439 rw = WRITE;
2440 break;
2441 default:
2442 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2443 req->opcode);
2444 goto end_req;
2445 }
2446
Jens Axboee8c2bc12020-08-15 18:44:09 -07002447 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002448 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2449 if (ret < 0)
2450 goto end_req;
2451 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2452 if (!ret)
2453 return true;
2454 kfree(iovec);
2455 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002456 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002457 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002458end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002459 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002460 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002461 return false;
2462}
Jens Axboeb63534c2020-06-04 11:28:00 -06002463#endif
2464
2465static bool io_rw_reissue(struct io_kiocb *req, long res)
2466{
2467#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002468 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002469 int ret;
2470
Jens Axboe355afae2020-09-02 09:30:31 -06002471 if (!S_ISBLK(mode) && !S_ISREG(mode))
2472 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002473 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2474 return false;
2475
Jens Axboefdee9462020-08-27 16:46:24 -06002476 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002477
Jens Axboefdee9462020-08-27 16:46:24 -06002478 if (io_resubmit_prep(req, ret)) {
2479 refcount_inc(&req->refs);
2480 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002481 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002482 }
2483
Jens Axboeb63534c2020-06-04 11:28:00 -06002484#endif
2485 return false;
2486}
2487
Jens Axboea1d7c392020-06-22 11:09:46 -06002488static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2489 struct io_comp_state *cs)
2490{
2491 if (!io_rw_reissue(req, res))
2492 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002493}
2494
2495static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2496{
Jens Axboe9adbd452019-12-20 08:45:55 -07002497 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002498
Jens Axboea1d7c392020-06-22 11:09:46 -06002499 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002500}
2501
Jens Axboedef596e2019-01-09 08:59:42 -07002502static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2503{
Jens Axboe9adbd452019-12-20 08:45:55 -07002504 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002505
Jens Axboe491381ce2019-10-17 09:20:46 -06002506 if (kiocb->ki_flags & IOCB_WRITE)
2507 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002508
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002509 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002510 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002511
2512 WRITE_ONCE(req->result, res);
2513 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002514 smp_wmb();
2515 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002516}
2517
2518/*
2519 * After the iocb has been issued, it's safe to be found on the poll list.
2520 * Adding the kiocb to the list AFTER submission ensures that we don't
2521 * find it from a io_iopoll_getevents() thread before the issuer is done
2522 * accessing the kiocb cookie.
2523 */
2524static void io_iopoll_req_issued(struct io_kiocb *req)
2525{
2526 struct io_ring_ctx *ctx = req->ctx;
2527
2528 /*
2529 * Track whether we have multiple files in our lists. This will impact
2530 * how we do polling eventually, not spinning if we're on potentially
2531 * different devices.
2532 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002533 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002534 ctx->poll_multi_file = false;
2535 } else if (!ctx->poll_multi_file) {
2536 struct io_kiocb *list_req;
2537
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002538 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002539 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002540 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002541 ctx->poll_multi_file = true;
2542 }
2543
2544 /*
2545 * For fast devices, IO may have already completed. If it has, add
2546 * it to the front so we find it first.
2547 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002548 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002549 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002550 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002551 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002552
Jens Axboe534ca6d2020-09-02 13:52:19 -06002553 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2554 wq_has_sleeper(&ctx->sq_data->wait))
2555 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002556}
2557
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002558static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002559{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002560 if (state->has_refs)
2561 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002562 state->file = NULL;
2563}
2564
2565static inline void io_state_file_put(struct io_submit_state *state)
2566{
2567 if (state->file)
2568 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002569}
2570
2571/*
2572 * Get as many references to a file as we have IOs left in this submission,
2573 * assuming most submissions are for one file, or at least that each file
2574 * has more than one submission.
2575 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002576static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002577{
2578 if (!state)
2579 return fget(fd);
2580
2581 if (state->file) {
2582 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002583 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002584 state->ios_left--;
2585 return state->file;
2586 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002587 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002588 }
2589 state->file = fget_many(fd, state->ios_left);
2590 if (!state->file)
2591 return NULL;
2592
2593 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002594 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002595 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002596 return state->file;
2597}
2598
Jens Axboe4503b762020-06-01 10:00:27 -06002599static bool io_bdev_nowait(struct block_device *bdev)
2600{
2601#ifdef CONFIG_BLOCK
2602 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2603#else
2604 return true;
2605#endif
2606}
2607
Jens Axboe2b188cc2019-01-07 10:46:33 -07002608/*
2609 * If we tracked the file through the SCM inflight mechanism, we could support
2610 * any file. For now, just ensure that anything potentially problematic is done
2611 * inline.
2612 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002613static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002614{
2615 umode_t mode = file_inode(file)->i_mode;
2616
Jens Axboe4503b762020-06-01 10:00:27 -06002617 if (S_ISBLK(mode)) {
2618 if (io_bdev_nowait(file->f_inode->i_bdev))
2619 return true;
2620 return false;
2621 }
2622 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002623 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002624 if (S_ISREG(mode)) {
2625 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2626 file->f_op != &io_uring_fops)
2627 return true;
2628 return false;
2629 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002630
Jens Axboec5b85622020-06-09 19:23:05 -06002631 /* any ->read/write should understand O_NONBLOCK */
2632 if (file->f_flags & O_NONBLOCK)
2633 return true;
2634
Jens Axboeaf197f52020-04-28 13:15:06 -06002635 if (!(file->f_mode & FMODE_NOWAIT))
2636 return false;
2637
2638 if (rw == READ)
2639 return file->f_op->read_iter != NULL;
2640
2641 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002642}
2643
Pavel Begunkova88fc402020-09-30 22:57:53 +03002644static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002645{
Jens Axboedef596e2019-01-09 08:59:42 -07002646 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002647 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002648 unsigned ioprio;
2649 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650
Jens Axboe491381ce2019-10-17 09:20:46 -06002651 if (S_ISREG(file_inode(req->file)->i_mode))
2652 req->flags |= REQ_F_ISREG;
2653
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002655 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2656 req->flags |= REQ_F_CUR_POS;
2657 kiocb->ki_pos = req->file->f_pos;
2658 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002659 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002660 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2661 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2662 if (unlikely(ret))
2663 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002664
2665 ioprio = READ_ONCE(sqe->ioprio);
2666 if (ioprio) {
2667 ret = ioprio_check_cap(ioprio);
2668 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002669 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002670
2671 kiocb->ki_ioprio = ioprio;
2672 } else
2673 kiocb->ki_ioprio = get_current_ioprio();
2674
Stefan Bühler8449eed2019-04-27 20:34:19 +02002675 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002676 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002677 req->flags |= REQ_F_NOWAIT;
2678
Jens Axboedef596e2019-01-09 08:59:42 -07002679 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002680 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2681 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002682 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683
Jens Axboedef596e2019-01-09 08:59:42 -07002684 kiocb->ki_flags |= IOCB_HIPRI;
2685 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002686 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002687 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002688 if (kiocb->ki_flags & IOCB_HIPRI)
2689 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002690 kiocb->ki_complete = io_complete_rw;
2691 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002692
Jens Axboe3529d8c2019-12-19 18:24:38 -07002693 req->rw.addr = READ_ONCE(sqe->addr);
2694 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002695 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002696 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002697}
2698
2699static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2700{
2701 switch (ret) {
2702 case -EIOCBQUEUED:
2703 break;
2704 case -ERESTARTSYS:
2705 case -ERESTARTNOINTR:
2706 case -ERESTARTNOHAND:
2707 case -ERESTART_RESTARTBLOCK:
2708 /*
2709 * We can't just restart the syscall, since previously
2710 * submitted sqes may already be in progress. Just fail this
2711 * IO with EINTR.
2712 */
2713 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002714 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002715 default:
2716 kiocb->ki_complete(kiocb, ret, 0);
2717 }
2718}
2719
Jens Axboea1d7c392020-06-22 11:09:46 -06002720static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2721 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002722{
Jens Axboeba042912019-12-25 16:33:42 -07002723 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002724 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002725
Jens Axboe227c0c92020-08-13 11:51:40 -06002726 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002727 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002728 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002729 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002730 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002731 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002732 }
2733
Jens Axboeba042912019-12-25 16:33:42 -07002734 if (req->flags & REQ_F_CUR_POS)
2735 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002736 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002737 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002738 else
2739 io_rw_done(kiocb, ret);
2740}
2741
Jens Axboe9adbd452019-12-20 08:45:55 -07002742static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002743 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002744{
Jens Axboe9adbd452019-12-20 08:45:55 -07002745 struct io_ring_ctx *ctx = req->ctx;
2746 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002747 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002748 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002749 size_t offset;
2750 u64 buf_addr;
2751
Jens Axboeedafcce2019-01-09 09:16:05 -07002752 if (unlikely(buf_index >= ctx->nr_user_bufs))
2753 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002754 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2755 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002756 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002757
2758 /* overflow */
2759 if (buf_addr + len < buf_addr)
2760 return -EFAULT;
2761 /* not inside the mapped region */
2762 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2763 return -EFAULT;
2764
2765 /*
2766 * May not be a start of buffer, set size appropriately
2767 * and advance us to the beginning.
2768 */
2769 offset = buf_addr - imu->ubuf;
2770 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002771
2772 if (offset) {
2773 /*
2774 * Don't use iov_iter_advance() here, as it's really slow for
2775 * using the latter parts of a big fixed buffer - it iterates
2776 * over each segment manually. We can cheat a bit here, because
2777 * we know that:
2778 *
2779 * 1) it's a BVEC iter, we set it up
2780 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2781 * first and last bvec
2782 *
2783 * So just find our index, and adjust the iterator afterwards.
2784 * If the offset is within the first bvec (or the whole first
2785 * bvec, just use iov_iter_advance(). This makes it easier
2786 * since we can just skip the first segment, which may not
2787 * be PAGE_SIZE aligned.
2788 */
2789 const struct bio_vec *bvec = imu->bvec;
2790
2791 if (offset <= bvec->bv_len) {
2792 iov_iter_advance(iter, offset);
2793 } else {
2794 unsigned long seg_skip;
2795
2796 /* skip first vec */
2797 offset -= bvec->bv_len;
2798 seg_skip = 1 + (offset >> PAGE_SHIFT);
2799
2800 iter->bvec = bvec + seg_skip;
2801 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002802 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002803 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002804 }
2805 }
2806
Jens Axboe5e559562019-11-13 16:12:46 -07002807 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002808}
2809
Jens Axboebcda7ba2020-02-23 16:42:51 -07002810static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2811{
2812 if (needs_lock)
2813 mutex_unlock(&ctx->uring_lock);
2814}
2815
2816static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2817{
2818 /*
2819 * "Normal" inline submissions always hold the uring_lock, since we
2820 * grab it from the system call. Same is true for the SQPOLL offload.
2821 * The only exception is when we've detached the request and issue it
2822 * from an async worker thread, grab the lock for that case.
2823 */
2824 if (needs_lock)
2825 mutex_lock(&ctx->uring_lock);
2826}
2827
2828static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2829 int bgid, struct io_buffer *kbuf,
2830 bool needs_lock)
2831{
2832 struct io_buffer *head;
2833
2834 if (req->flags & REQ_F_BUFFER_SELECTED)
2835 return kbuf;
2836
2837 io_ring_submit_lock(req->ctx, needs_lock);
2838
2839 lockdep_assert_held(&req->ctx->uring_lock);
2840
2841 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2842 if (head) {
2843 if (!list_empty(&head->list)) {
2844 kbuf = list_last_entry(&head->list, struct io_buffer,
2845 list);
2846 list_del(&kbuf->list);
2847 } else {
2848 kbuf = head;
2849 idr_remove(&req->ctx->io_buffer_idr, bgid);
2850 }
2851 if (*len > kbuf->len)
2852 *len = kbuf->len;
2853 } else {
2854 kbuf = ERR_PTR(-ENOBUFS);
2855 }
2856
2857 io_ring_submit_unlock(req->ctx, needs_lock);
2858
2859 return kbuf;
2860}
2861
Jens Axboe4d954c22020-02-27 07:31:19 -07002862static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2863 bool needs_lock)
2864{
2865 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002866 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002867
2868 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002869 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002870 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2871 if (IS_ERR(kbuf))
2872 return kbuf;
2873 req->rw.addr = (u64) (unsigned long) kbuf;
2874 req->flags |= REQ_F_BUFFER_SELECTED;
2875 return u64_to_user_ptr(kbuf->addr);
2876}
2877
2878#ifdef CONFIG_COMPAT
2879static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2880 bool needs_lock)
2881{
2882 struct compat_iovec __user *uiov;
2883 compat_ssize_t clen;
2884 void __user *buf;
2885 ssize_t len;
2886
2887 uiov = u64_to_user_ptr(req->rw.addr);
2888 if (!access_ok(uiov, sizeof(*uiov)))
2889 return -EFAULT;
2890 if (__get_user(clen, &uiov->iov_len))
2891 return -EFAULT;
2892 if (clen < 0)
2893 return -EINVAL;
2894
2895 len = clen;
2896 buf = io_rw_buffer_select(req, &len, needs_lock);
2897 if (IS_ERR(buf))
2898 return PTR_ERR(buf);
2899 iov[0].iov_base = buf;
2900 iov[0].iov_len = (compat_size_t) len;
2901 return 0;
2902}
2903#endif
2904
2905static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2906 bool needs_lock)
2907{
2908 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2909 void __user *buf;
2910 ssize_t len;
2911
2912 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2913 return -EFAULT;
2914
2915 len = iov[0].iov_len;
2916 if (len < 0)
2917 return -EINVAL;
2918 buf = io_rw_buffer_select(req, &len, needs_lock);
2919 if (IS_ERR(buf))
2920 return PTR_ERR(buf);
2921 iov[0].iov_base = buf;
2922 iov[0].iov_len = len;
2923 return 0;
2924}
2925
2926static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2927 bool needs_lock)
2928{
Jens Axboedddb3e22020-06-04 11:27:01 -06002929 if (req->flags & REQ_F_BUFFER_SELECTED) {
2930 struct io_buffer *kbuf;
2931
2932 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2933 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2934 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002935 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002936 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002937 if (!req->rw.len)
2938 return 0;
2939 else if (req->rw.len > 1)
2940 return -EINVAL;
2941
2942#ifdef CONFIG_COMPAT
2943 if (req->ctx->compat)
2944 return io_compat_import(req, iov, needs_lock);
2945#endif
2946
2947 return __io_iov_buffer_select(req, iov, needs_lock);
2948}
2949
Jens Axboe8452fd02020-08-18 13:58:33 -07002950static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2951 struct iovec **iovec, struct iov_iter *iter,
2952 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002953{
Jens Axboe9adbd452019-12-20 08:45:55 -07002954 void __user *buf = u64_to_user_ptr(req->rw.addr);
2955 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002956 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002957 u8 opcode;
2958
Jens Axboed625c6e2019-12-17 19:53:05 -07002959 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002960 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002961 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002962 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002963 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002964
Jens Axboebcda7ba2020-02-23 16:42:51 -07002965 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002966 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002967 return -EINVAL;
2968
Jens Axboe3a6820f2019-12-22 15:19:35 -07002969 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002970 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002971 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002972 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002973 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002974 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002975 }
2976
Jens Axboe3a6820f2019-12-22 15:19:35 -07002977 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2978 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002979 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002980 }
2981
Jens Axboe4d954c22020-02-27 07:31:19 -07002982 if (req->flags & REQ_F_BUFFER_SELECT) {
2983 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002984 if (!ret) {
2985 ret = (*iovec)->iov_len;
2986 iov_iter_init(iter, rw, *iovec, 1, ret);
2987 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002988 *iovec = NULL;
2989 return ret;
2990 }
2991
Jens Axboe2b188cc2019-01-07 10:46:33 -07002992#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002993 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002994 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2995 iovec, iter);
2996#endif
2997
2998 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2999}
3000
Jens Axboe8452fd02020-08-18 13:58:33 -07003001static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
3002 struct iovec **iovec, struct iov_iter *iter,
3003 bool needs_lock)
3004{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003005 struct io_async_rw *iorw = req->async_data;
3006
3007 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07003008 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3009 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003010 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07003011}
3012
Jens Axboe0fef9482020-08-26 10:36:20 -06003013static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3014{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003015 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003016}
3017
Jens Axboe32960612019-09-23 11:05:34 -06003018/*
3019 * For files that don't have ->read_iter() and ->write_iter(), handle them
3020 * by looping over ->read() or ->write() manually.
3021 */
3022static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
3023 struct iov_iter *iter)
3024{
3025 ssize_t ret = 0;
3026
3027 /*
3028 * Don't support polled IO through this interface, and we can't
3029 * support non-blocking either. For the latter, this just causes
3030 * the kiocb to be handled from an async context.
3031 */
3032 if (kiocb->ki_flags & IOCB_HIPRI)
3033 return -EOPNOTSUPP;
3034 if (kiocb->ki_flags & IOCB_NOWAIT)
3035 return -EAGAIN;
3036
3037 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003038 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003039 ssize_t nr;
3040
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003041 if (!iov_iter_is_bvec(iter)) {
3042 iovec = iov_iter_iovec(iter);
3043 } else {
3044 /* fixed buffers import bvec */
3045 iovec.iov_base = kmap(iter->bvec->bv_page)
3046 + iter->iov_offset;
3047 iovec.iov_len = min(iter->count,
3048 iter->bvec->bv_len - iter->iov_offset);
3049 }
3050
Jens Axboe32960612019-09-23 11:05:34 -06003051 if (rw == READ) {
3052 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003053 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003054 } else {
3055 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003056 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003057 }
3058
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003059 if (iov_iter_is_bvec(iter))
3060 kunmap(iter->bvec->bv_page);
3061
Jens Axboe32960612019-09-23 11:05:34 -06003062 if (nr < 0) {
3063 if (!ret)
3064 ret = nr;
3065 break;
3066 }
3067 ret += nr;
3068 if (nr != iovec.iov_len)
3069 break;
3070 iov_iter_advance(iter, nr);
3071 }
3072
3073 return ret;
3074}
3075
Jens Axboeff6165b2020-08-13 09:47:43 -06003076static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3077 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003078{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003079 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003080
Jens Axboeff6165b2020-08-13 09:47:43 -06003081 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003082 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003083 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003084 /* can only be fixed buffers, no need to do anything */
3085 if (iter->type == ITER_BVEC)
3086 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003087 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003088 unsigned iov_off = 0;
3089
3090 rw->iter.iov = rw->fast_iov;
3091 if (iter->iov != fast_iov) {
3092 iov_off = iter->iov - fast_iov;
3093 rw->iter.iov += iov_off;
3094 }
3095 if (rw->fast_iov != fast_iov)
3096 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003097 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003098 } else {
3099 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003100 }
3101}
3102
Jens Axboee8c2bc12020-08-15 18:44:09 -07003103static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003104{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003105 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3106 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3107 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003108}
3109
Jens Axboee8c2bc12020-08-15 18:44:09 -07003110static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003111{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003112 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003113 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003114
Jens Axboee8c2bc12020-08-15 18:44:09 -07003115 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003116}
3117
Jens Axboeff6165b2020-08-13 09:47:43 -06003118static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3119 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003120 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003121{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003122 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003123 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003124 if (!req->async_data) {
3125 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003126 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003127
Jens Axboeff6165b2020-08-13 09:47:43 -06003128 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003129 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003130 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003131}
3132
Pavel Begunkov73debe62020-09-30 22:57:54 +03003133static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003134{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003135 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003136 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003137 ssize_t ret;
3138
Pavel Begunkov73debe62020-09-30 22:57:54 +03003139 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003140 if (unlikely(ret < 0))
3141 return ret;
3142
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003143 iorw->bytes_done = 0;
3144 iorw->free_iovec = iov;
3145 if (iov)
3146 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003147 return 0;
3148}
3149
Pavel Begunkov73debe62020-09-30 22:57:54 +03003150static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003151{
3152 ssize_t ret;
3153
Pavel Begunkova88fc402020-09-30 22:57:53 +03003154 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003155 if (ret)
3156 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003157
Jens Axboe3529d8c2019-12-19 18:24:38 -07003158 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3159 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003160
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003161 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003162 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003163 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003164 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003165}
3166
Jens Axboec1dd91d2020-08-03 16:43:59 -06003167/*
3168 * This is our waitqueue callback handler, registered through lock_page_async()
3169 * when we initially tried to do the IO with the iocb armed our waitqueue.
3170 * This gets called when the page is unlocked, and we generally expect that to
3171 * happen when the page IO is completed and the page is now uptodate. This will
3172 * queue a task_work based retry of the operation, attempting to copy the data
3173 * again. If the latter fails because the page was NOT uptodate, then we will
3174 * do a thread based blocking retry of the operation. That's the unexpected
3175 * slow path.
3176 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003177static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3178 int sync, void *arg)
3179{
3180 struct wait_page_queue *wpq;
3181 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003182 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003183 int ret;
3184
3185 wpq = container_of(wait, struct wait_page_queue, wait);
3186
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003187 if (!wake_page_match(wpq, key))
3188 return 0;
3189
Hao Xuc8d317a2020-09-29 20:00:45 +08003190 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003191 list_del_init(&wait->entry);
3192
Pavel Begunkove7375122020-07-12 20:42:04 +03003193 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003194 percpu_ref_get(&req->ctx->refs);
3195
Jens Axboebcf5a062020-05-22 09:24:42 -06003196 /* submit ref gets dropped, acquire a new one */
3197 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003198 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003199 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003200 struct task_struct *tsk;
3201
Jens Axboebcf5a062020-05-22 09:24:42 -06003202 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003203 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003204 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003205 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003206 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003207 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003208 return 1;
3209}
3210
Jens Axboec1dd91d2020-08-03 16:43:59 -06003211/*
3212 * This controls whether a given IO request should be armed for async page
3213 * based retry. If we return false here, the request is handed to the async
3214 * worker threads for retry. If we're doing buffered reads on a regular file,
3215 * we prepare a private wait_page_queue entry and retry the operation. This
3216 * will either succeed because the page is now uptodate and unlocked, or it
3217 * will register a callback when the page is unlocked at IO completion. Through
3218 * that callback, io_uring uses task_work to setup a retry of the operation.
3219 * That retry will attempt the buffered read again. The retry will generally
3220 * succeed, or in rare cases where it fails, we then fall back to using the
3221 * async worker threads for a blocking retry.
3222 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003223static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003224{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003225 struct io_async_rw *rw = req->async_data;
3226 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003227 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003228
3229 /* never retry for NOWAIT, we just complete with -EAGAIN */
3230 if (req->flags & REQ_F_NOWAIT)
3231 return false;
3232
Jens Axboe227c0c92020-08-13 11:51:40 -06003233 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003234 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003235 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003236
Jens Axboebcf5a062020-05-22 09:24:42 -06003237 /*
3238 * just use poll if we can, and don't attempt if the fs doesn't
3239 * support callback based unlocks
3240 */
3241 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3242 return false;
3243
Jens Axboe3b2a4432020-08-16 10:58:43 -07003244 wait->wait.func = io_async_buf_func;
3245 wait->wait.private = req;
3246 wait->wait.flags = 0;
3247 INIT_LIST_HEAD(&wait->wait.entry);
3248 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003249 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003250 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003251 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003252}
3253
3254static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3255{
3256 if (req->file->f_op->read_iter)
3257 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003258 else if (req->file->f_op->read)
3259 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3260 else
3261 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003262}
3263
Jens Axboea1d7c392020-06-22 11:09:46 -06003264static int io_read(struct io_kiocb *req, bool force_nonblock,
3265 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266{
3267 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003268 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003269 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003270 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003271 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003272 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003273 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003274
Jens Axboee8c2bc12020-08-15 18:44:09 -07003275 if (rw)
3276 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003277
3278 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003279 if (ret < 0)
3280 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003281 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003282 io_size = ret;
3283 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003284 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003285
Jens Axboefd6c2e42019-12-18 12:19:41 -07003286 /* Ensure we clear previously set non-block flag */
3287 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003288 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003289 else
3290 kiocb->ki_flags |= IOCB_NOWAIT;
3291
Jens Axboefd6c2e42019-12-18 12:19:41 -07003292
Pavel Begunkov24c74672020-06-21 13:09:51 +03003293 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003294 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3295 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003296 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003297
Jens Axboe0fef9482020-08-26 10:36:20 -06003298 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003299 if (unlikely(ret))
3300 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003301
Jens Axboe227c0c92020-08-13 11:51:40 -06003302 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003303
Jens Axboe227c0c92020-08-13 11:51:40 -06003304 if (!ret) {
3305 goto done;
3306 } else if (ret == -EIOCBQUEUED) {
3307 ret = 0;
3308 goto out_free;
3309 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003310 /* IOPOLL retry should happen for io-wq threads */
3311 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003312 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003313 /* no retry on NONBLOCK marked file */
3314 if (req->file->f_flags & O_NONBLOCK)
3315 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003316 /* some cases will consume bytes even on error returns */
3317 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003318 ret = 0;
3319 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003320 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003321 /* make sure -ERESTARTSYS -> -EINTR is done */
3322 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003323 }
3324
3325 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003326 if (!iov_iter_count(iter) || !force_nonblock ||
3327 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003328 goto done;
3329
3330 io_size -= ret;
3331copy_iov:
3332 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3333 if (ret2) {
3334 ret = ret2;
3335 goto out_free;
3336 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003337 if (no_async)
3338 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003339 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003340 /* it's copied and will be cleaned with ->io */
3341 iovec = NULL;
3342 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003343 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003344retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003345 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003346 /* if we can retry, do so with the callbacks armed */
3347 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003348 kiocb->ki_flags &= ~IOCB_WAITQ;
3349 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003350 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003351
3352 /*
3353 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3354 * get -EIOCBQUEUED, then we'll get a notification when the desired
3355 * page gets unlocked. We can also get a partial read here, and if we
3356 * do, then just retry at the new offset.
3357 */
3358 ret = io_iter_do_read(req, iter);
3359 if (ret == -EIOCBQUEUED) {
3360 ret = 0;
3361 goto out_free;
3362 } else if (ret > 0 && ret < io_size) {
3363 /* we got some bytes, but not all. retry. */
3364 goto retry;
3365 }
3366done:
3367 kiocb_done(kiocb, ret, cs);
3368 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003369out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003370 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003371 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003372 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003373 return ret;
3374}
3375
Pavel Begunkov73debe62020-09-30 22:57:54 +03003376static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003377{
3378 ssize_t ret;
3379
Pavel Begunkova88fc402020-09-30 22:57:53 +03003380 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003381 if (ret)
3382 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003383
Jens Axboe3529d8c2019-12-19 18:24:38 -07003384 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3385 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003386
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003387 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003388 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003389 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003390 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003391}
3392
Jens Axboea1d7c392020-06-22 11:09:46 -06003393static int io_write(struct io_kiocb *req, bool force_nonblock,
3394 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003395{
3396 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003397 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003398 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003399 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003400 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003401 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003402
Jens Axboee8c2bc12020-08-15 18:44:09 -07003403 if (rw)
3404 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003405
3406 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003407 if (ret < 0)
3408 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003409 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003410 io_size = ret;
3411 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003412
Jens Axboefd6c2e42019-12-18 12:19:41 -07003413 /* Ensure we clear previously set non-block flag */
3414 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003415 kiocb->ki_flags &= ~IOCB_NOWAIT;
3416 else
3417 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003418
Pavel Begunkov24c74672020-06-21 13:09:51 +03003419 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003420 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003421 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003422
Jens Axboe10d59342019-12-09 20:16:22 -07003423 /* file path doesn't support NOWAIT for non-direct_IO */
3424 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3425 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003426 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003427
Jens Axboe0fef9482020-08-26 10:36:20 -06003428 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003429 if (unlikely(ret))
3430 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003431
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003432 /*
3433 * Open-code file_start_write here to grab freeze protection,
3434 * which will be released by another thread in
3435 * io_complete_rw(). Fool lockdep by telling it the lock got
3436 * released so that it doesn't complain about the held lock when
3437 * we return to userspace.
3438 */
3439 if (req->flags & REQ_F_ISREG) {
3440 __sb_start_write(file_inode(req->file)->i_sb,
3441 SB_FREEZE_WRITE, true);
3442 __sb_writers_release(file_inode(req->file)->i_sb,
3443 SB_FREEZE_WRITE);
3444 }
3445 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003446
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003447 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003448 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003449 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003450 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003451 else
3452 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003453
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003454 /*
3455 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3456 * retry them without IOCB_NOWAIT.
3457 */
3458 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3459 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003460 /* no retry on NONBLOCK marked file */
3461 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3462 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003463 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003464 /* IOPOLL retry should happen for io-wq threads */
3465 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3466 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003467done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003468 kiocb_done(kiocb, ret2, cs);
3469 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003470copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003471 /* some cases will consume bytes even on error returns */
3472 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003473 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003474 if (!ret)
3475 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003476 }
Jens Axboe31b51512019-01-18 22:56:34 -07003477out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003478 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003479 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003480 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003481 return ret;
3482}
3483
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003484static int __io_splice_prep(struct io_kiocb *req,
3485 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003486{
3487 struct io_splice* sp = &req->splice;
3488 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003489
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003490 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3491 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003492
3493 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003494 sp->len = READ_ONCE(sqe->len);
3495 sp->flags = READ_ONCE(sqe->splice_flags);
3496
3497 if (unlikely(sp->flags & ~valid_flags))
3498 return -EINVAL;
3499
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003500 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3501 (sp->flags & SPLICE_F_FD_IN_FIXED));
3502 if (!sp->file_in)
3503 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003504 req->flags |= REQ_F_NEED_CLEANUP;
3505
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003506 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3507 /*
3508 * Splice operation will be punted aync, and here need to
3509 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3510 */
3511 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003512 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003513 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003514
3515 return 0;
3516}
3517
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003518static int io_tee_prep(struct io_kiocb *req,
3519 const struct io_uring_sqe *sqe)
3520{
3521 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3522 return -EINVAL;
3523 return __io_splice_prep(req, sqe);
3524}
3525
3526static int io_tee(struct io_kiocb *req, bool force_nonblock)
3527{
3528 struct io_splice *sp = &req->splice;
3529 struct file *in = sp->file_in;
3530 struct file *out = sp->file_out;
3531 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3532 long ret = 0;
3533
3534 if (force_nonblock)
3535 return -EAGAIN;
3536 if (sp->len)
3537 ret = do_tee(in, out, sp->len, flags);
3538
3539 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3540 req->flags &= ~REQ_F_NEED_CLEANUP;
3541
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003542 if (ret != sp->len)
3543 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003544 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003545 return 0;
3546}
3547
3548static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3549{
3550 struct io_splice* sp = &req->splice;
3551
3552 sp->off_in = READ_ONCE(sqe->splice_off_in);
3553 sp->off_out = READ_ONCE(sqe->off);
3554 return __io_splice_prep(req, sqe);
3555}
3556
Pavel Begunkov014db002020-03-03 21:33:12 +03003557static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003558{
3559 struct io_splice *sp = &req->splice;
3560 struct file *in = sp->file_in;
3561 struct file *out = sp->file_out;
3562 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3563 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003564 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003565
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003566 if (force_nonblock)
3567 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003568
3569 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3570 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003571
Jens Axboe948a7742020-05-17 14:21:38 -06003572 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003573 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003574
3575 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3576 req->flags &= ~REQ_F_NEED_CLEANUP;
3577
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003578 if (ret != sp->len)
3579 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003580 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003581 return 0;
3582}
3583
Jens Axboe2b188cc2019-01-07 10:46:33 -07003584/*
3585 * IORING_OP_NOP just posts a completion event, nothing else.
3586 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003587static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003588{
3589 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003590
Jens Axboedef596e2019-01-09 08:59:42 -07003591 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3592 return -EINVAL;
3593
Jens Axboe229a7b62020-06-22 10:13:11 -06003594 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003595 return 0;
3596}
3597
Jens Axboe3529d8c2019-12-19 18:24:38 -07003598static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003599{
Jens Axboe6b063142019-01-10 22:13:58 -07003600 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003601
Jens Axboe09bb8392019-03-13 12:39:28 -06003602 if (!req->file)
3603 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003604
Jens Axboe6b063142019-01-10 22:13:58 -07003605 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003606 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003607 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003608 return -EINVAL;
3609
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003610 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3611 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3612 return -EINVAL;
3613
3614 req->sync.off = READ_ONCE(sqe->off);
3615 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003616 return 0;
3617}
3618
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003619static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003620{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003621 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003622 int ret;
3623
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003624 /* fsync always requires a blocking context */
3625 if (force_nonblock)
3626 return -EAGAIN;
3627
Jens Axboe9adbd452019-12-20 08:45:55 -07003628 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003629 end > 0 ? end : LLONG_MAX,
3630 req->sync.flags & IORING_FSYNC_DATASYNC);
3631 if (ret < 0)
3632 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003633 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003634 return 0;
3635}
3636
Jens Axboed63d1b52019-12-10 10:38:56 -07003637static int io_fallocate_prep(struct io_kiocb *req,
3638 const struct io_uring_sqe *sqe)
3639{
3640 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3641 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003642 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3643 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003644
3645 req->sync.off = READ_ONCE(sqe->off);
3646 req->sync.len = READ_ONCE(sqe->addr);
3647 req->sync.mode = READ_ONCE(sqe->len);
3648 return 0;
3649}
3650
Pavel Begunkov014db002020-03-03 21:33:12 +03003651static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003652{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003653 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003654
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003655 /* fallocate always requiring blocking context */
3656 if (force_nonblock)
3657 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003658 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3659 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003660 if (ret < 0)
3661 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003662 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003663 return 0;
3664}
3665
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003666static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003667{
Jens Axboef8748882020-01-08 17:47:02 -07003668 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003669 int ret;
3670
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003671 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003672 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003673 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003674 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003675
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003676 /* open.how should be already initialised */
3677 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003678 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003679
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003680 req->open.dfd = READ_ONCE(sqe->fd);
3681 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003682 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003683 if (IS_ERR(req->open.filename)) {
3684 ret = PTR_ERR(req->open.filename);
3685 req->open.filename = NULL;
3686 return ret;
3687 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003688 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003689 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003690 return 0;
3691}
3692
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003693static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3694{
3695 u64 flags, mode;
3696
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003697 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3698 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003699 mode = READ_ONCE(sqe->len);
3700 flags = READ_ONCE(sqe->open_flags);
3701 req->open.how = build_open_how(flags, mode);
3702 return __io_openat_prep(req, sqe);
3703}
3704
Jens Axboecebdb982020-01-08 17:59:24 -07003705static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3706{
3707 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003708 size_t len;
3709 int ret;
3710
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003711 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3712 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003713 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3714 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003715 if (len < OPEN_HOW_SIZE_VER0)
3716 return -EINVAL;
3717
3718 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3719 len);
3720 if (ret)
3721 return ret;
3722
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003723 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003724}
3725
Pavel Begunkov014db002020-03-03 21:33:12 +03003726static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003727{
3728 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003729 struct file *file;
3730 int ret;
3731
Jens Axboef86cd202020-01-29 13:46:44 -07003732 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003733 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003734
Jens Axboecebdb982020-01-08 17:59:24 -07003735 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003736 if (ret)
3737 goto err;
3738
Jens Axboe4022e7a2020-03-19 19:23:18 -06003739 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003740 if (ret < 0)
3741 goto err;
3742
3743 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3744 if (IS_ERR(file)) {
3745 put_unused_fd(ret);
3746 ret = PTR_ERR(file);
3747 } else {
3748 fsnotify_open(file);
3749 fd_install(ret, file);
3750 }
3751err:
3752 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003753 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003754 if (ret < 0)
3755 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003756 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003757 return 0;
3758}
3759
Pavel Begunkov014db002020-03-03 21:33:12 +03003760static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003761{
Pavel Begunkov014db002020-03-03 21:33:12 +03003762 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003763}
3764
Jens Axboe067524e2020-03-02 16:32:28 -07003765static int io_remove_buffers_prep(struct io_kiocb *req,
3766 const struct io_uring_sqe *sqe)
3767{
3768 struct io_provide_buf *p = &req->pbuf;
3769 u64 tmp;
3770
3771 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3772 return -EINVAL;
3773
3774 tmp = READ_ONCE(sqe->fd);
3775 if (!tmp || tmp > USHRT_MAX)
3776 return -EINVAL;
3777
3778 memset(p, 0, sizeof(*p));
3779 p->nbufs = tmp;
3780 p->bgid = READ_ONCE(sqe->buf_group);
3781 return 0;
3782}
3783
3784static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3785 int bgid, unsigned nbufs)
3786{
3787 unsigned i = 0;
3788
3789 /* shouldn't happen */
3790 if (!nbufs)
3791 return 0;
3792
3793 /* the head kbuf is the list itself */
3794 while (!list_empty(&buf->list)) {
3795 struct io_buffer *nxt;
3796
3797 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3798 list_del(&nxt->list);
3799 kfree(nxt);
3800 if (++i == nbufs)
3801 return i;
3802 }
3803 i++;
3804 kfree(buf);
3805 idr_remove(&ctx->io_buffer_idr, bgid);
3806
3807 return i;
3808}
3809
Jens Axboe229a7b62020-06-22 10:13:11 -06003810static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3811 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003812{
3813 struct io_provide_buf *p = &req->pbuf;
3814 struct io_ring_ctx *ctx = req->ctx;
3815 struct io_buffer *head;
3816 int ret = 0;
3817
3818 io_ring_submit_lock(ctx, !force_nonblock);
3819
3820 lockdep_assert_held(&ctx->uring_lock);
3821
3822 ret = -ENOENT;
3823 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3824 if (head)
3825 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3826
3827 io_ring_submit_lock(ctx, !force_nonblock);
3828 if (ret < 0)
3829 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003830 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003831 return 0;
3832}
3833
Jens Axboeddf0322d2020-02-23 16:41:33 -07003834static int io_provide_buffers_prep(struct io_kiocb *req,
3835 const struct io_uring_sqe *sqe)
3836{
3837 struct io_provide_buf *p = &req->pbuf;
3838 u64 tmp;
3839
3840 if (sqe->ioprio || sqe->rw_flags)
3841 return -EINVAL;
3842
3843 tmp = READ_ONCE(sqe->fd);
3844 if (!tmp || tmp > USHRT_MAX)
3845 return -E2BIG;
3846 p->nbufs = tmp;
3847 p->addr = READ_ONCE(sqe->addr);
3848 p->len = READ_ONCE(sqe->len);
3849
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003850 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003851 return -EFAULT;
3852
3853 p->bgid = READ_ONCE(sqe->buf_group);
3854 tmp = READ_ONCE(sqe->off);
3855 if (tmp > USHRT_MAX)
3856 return -E2BIG;
3857 p->bid = tmp;
3858 return 0;
3859}
3860
3861static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3862{
3863 struct io_buffer *buf;
3864 u64 addr = pbuf->addr;
3865 int i, bid = pbuf->bid;
3866
3867 for (i = 0; i < pbuf->nbufs; i++) {
3868 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3869 if (!buf)
3870 break;
3871
3872 buf->addr = addr;
3873 buf->len = pbuf->len;
3874 buf->bid = bid;
3875 addr += pbuf->len;
3876 bid++;
3877 if (!*head) {
3878 INIT_LIST_HEAD(&buf->list);
3879 *head = buf;
3880 } else {
3881 list_add_tail(&buf->list, &(*head)->list);
3882 }
3883 }
3884
3885 return i ? i : -ENOMEM;
3886}
3887
Jens Axboe229a7b62020-06-22 10:13:11 -06003888static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3889 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003890{
3891 struct io_provide_buf *p = &req->pbuf;
3892 struct io_ring_ctx *ctx = req->ctx;
3893 struct io_buffer *head, *list;
3894 int ret = 0;
3895
3896 io_ring_submit_lock(ctx, !force_nonblock);
3897
3898 lockdep_assert_held(&ctx->uring_lock);
3899
3900 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3901
3902 ret = io_add_buffers(p, &head);
3903 if (ret < 0)
3904 goto out;
3905
3906 if (!list) {
3907 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3908 GFP_KERNEL);
3909 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003910 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003911 goto out;
3912 }
3913 }
3914out:
3915 io_ring_submit_unlock(ctx, !force_nonblock);
3916 if (ret < 0)
3917 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003918 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003919 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003920}
3921
Jens Axboe3e4827b2020-01-08 15:18:09 -07003922static int io_epoll_ctl_prep(struct io_kiocb *req,
3923 const struct io_uring_sqe *sqe)
3924{
3925#if defined(CONFIG_EPOLL)
3926 if (sqe->ioprio || sqe->buf_index)
3927 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003928 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003929 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003930
3931 req->epoll.epfd = READ_ONCE(sqe->fd);
3932 req->epoll.op = READ_ONCE(sqe->len);
3933 req->epoll.fd = READ_ONCE(sqe->off);
3934
3935 if (ep_op_has_event(req->epoll.op)) {
3936 struct epoll_event __user *ev;
3937
3938 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3939 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3940 return -EFAULT;
3941 }
3942
3943 return 0;
3944#else
3945 return -EOPNOTSUPP;
3946#endif
3947}
3948
Jens Axboe229a7b62020-06-22 10:13:11 -06003949static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3950 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003951{
3952#if defined(CONFIG_EPOLL)
3953 struct io_epoll *ie = &req->epoll;
3954 int ret;
3955
3956 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3957 if (force_nonblock && ret == -EAGAIN)
3958 return -EAGAIN;
3959
3960 if (ret < 0)
3961 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003962 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003963 return 0;
3964#else
3965 return -EOPNOTSUPP;
3966#endif
3967}
3968
Jens Axboec1ca7572019-12-25 22:18:28 -07003969static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3970{
3971#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3972 if (sqe->ioprio || sqe->buf_index || sqe->off)
3973 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003974 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3975 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003976
3977 req->madvise.addr = READ_ONCE(sqe->addr);
3978 req->madvise.len = READ_ONCE(sqe->len);
3979 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3980 return 0;
3981#else
3982 return -EOPNOTSUPP;
3983#endif
3984}
3985
Pavel Begunkov014db002020-03-03 21:33:12 +03003986static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003987{
3988#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3989 struct io_madvise *ma = &req->madvise;
3990 int ret;
3991
3992 if (force_nonblock)
3993 return -EAGAIN;
3994
3995 ret = do_madvise(ma->addr, ma->len, ma->advice);
3996 if (ret < 0)
3997 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003998 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003999 return 0;
4000#else
4001 return -EOPNOTSUPP;
4002#endif
4003}
4004
Jens Axboe4840e412019-12-25 22:03:45 -07004005static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4006{
4007 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4008 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004009 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4010 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004011
4012 req->fadvise.offset = READ_ONCE(sqe->off);
4013 req->fadvise.len = READ_ONCE(sqe->len);
4014 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4015 return 0;
4016}
4017
Pavel Begunkov014db002020-03-03 21:33:12 +03004018static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004019{
4020 struct io_fadvise *fa = &req->fadvise;
4021 int ret;
4022
Jens Axboe3e694262020-02-01 09:22:49 -07004023 if (force_nonblock) {
4024 switch (fa->advice) {
4025 case POSIX_FADV_NORMAL:
4026 case POSIX_FADV_RANDOM:
4027 case POSIX_FADV_SEQUENTIAL:
4028 break;
4029 default:
4030 return -EAGAIN;
4031 }
4032 }
Jens Axboe4840e412019-12-25 22:03:45 -07004033
4034 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4035 if (ret < 0)
4036 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004037 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004038 return 0;
4039}
4040
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004041static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4042{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004043 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004044 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004045 if (sqe->ioprio || sqe->buf_index)
4046 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004047 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004048 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004049
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004050 req->statx.dfd = READ_ONCE(sqe->fd);
4051 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004052 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004053 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4054 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004055
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004056 return 0;
4057}
4058
Pavel Begunkov014db002020-03-03 21:33:12 +03004059static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004060{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004061 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004062 int ret;
4063
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004064 if (force_nonblock) {
4065 /* only need file table for an actual valid fd */
4066 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4067 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004068 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004069 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004070
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004071 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4072 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004073
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004074 if (ret < 0)
4075 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004076 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004077 return 0;
4078}
4079
Jens Axboeb5dba592019-12-11 14:02:38 -07004080static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4081{
4082 /*
4083 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004084 * leave the 'file' in an undeterminate state, and here need to modify
4085 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004086 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004087 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004088 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4089
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004090 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4091 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004092 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4093 sqe->rw_flags || sqe->buf_index)
4094 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004095 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004096 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004097
4098 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004099 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004100 return -EBADF;
4101
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004102 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004103 return 0;
4104}
4105
Jens Axboe229a7b62020-06-22 10:13:11 -06004106static int io_close(struct io_kiocb *req, bool force_nonblock,
4107 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004108{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004109 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004110 int ret;
4111
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004112 /* might be already done during nonblock submission */
4113 if (!close->put_file) {
4114 ret = __close_fd_get_file(close->fd, &close->put_file);
4115 if (ret < 0)
4116 return (ret == -ENOENT) ? -EBADF : ret;
4117 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004118
4119 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004120 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004121 /* was never set, but play safe */
4122 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004123 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004124 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004125 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004126 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004127
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004128 /* No ->flush() or already async, safely close from here */
4129 ret = filp_close(close->put_file, req->work.files);
4130 if (ret < 0)
4131 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004132 fput(close->put_file);
4133 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004134 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004135 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004136}
4137
Jens Axboe3529d8c2019-12-19 18:24:38 -07004138static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004139{
4140 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004141
4142 if (!req->file)
4143 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004144
4145 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4146 return -EINVAL;
4147 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4148 return -EINVAL;
4149
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004150 req->sync.off = READ_ONCE(sqe->off);
4151 req->sync.len = READ_ONCE(sqe->len);
4152 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004153 return 0;
4154}
4155
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004156static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004157{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004158 int ret;
4159
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004160 /* sync_file_range always requires a blocking context */
4161 if (force_nonblock)
4162 return -EAGAIN;
4163
Jens Axboe9adbd452019-12-20 08:45:55 -07004164 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004165 req->sync.flags);
4166 if (ret < 0)
4167 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004168 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004169 return 0;
4170}
4171
YueHaibing469956e2020-03-04 15:53:52 +08004172#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004173static int io_setup_async_msg(struct io_kiocb *req,
4174 struct io_async_msghdr *kmsg)
4175{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004176 struct io_async_msghdr *async_msg = req->async_data;
4177
4178 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004179 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004180 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004181 if (kmsg->iov != kmsg->fast_iov)
4182 kfree(kmsg->iov);
4183 return -ENOMEM;
4184 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004185 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004186 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004187 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004188 return -EAGAIN;
4189}
4190
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004191static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4192 struct io_async_msghdr *iomsg)
4193{
4194 iomsg->iov = iomsg->fast_iov;
4195 iomsg->msg.msg_name = &iomsg->addr;
4196 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4197 req->sr_msg.msg_flags, &iomsg->iov);
4198}
4199
Jens Axboe3529d8c2019-12-19 18:24:38 -07004200static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004201{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004202 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004203 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004204 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004205
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004206 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4207 return -EINVAL;
4208
Jens Axboee47293f2019-12-20 08:58:21 -07004209 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004210 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004211 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004212
Jens Axboed8768362020-02-27 14:17:49 -07004213#ifdef CONFIG_COMPAT
4214 if (req->ctx->compat)
4215 sr->msg_flags |= MSG_CMSG_COMPAT;
4216#endif
4217
Jens Axboee8c2bc12020-08-15 18:44:09 -07004218 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004219 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004220 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004221 if (!ret)
4222 req->flags |= REQ_F_NEED_CLEANUP;
4223 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004224}
4225
Jens Axboe229a7b62020-06-22 10:13:11 -06004226static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4227 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004228{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004229 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004230 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004231 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004232 int ret;
4233
Jens Axboe03b12302019-12-02 18:50:25 -07004234 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004235 if (unlikely(!sock))
4236 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004237
Jens Axboee8c2bc12020-08-15 18:44:09 -07004238 if (req->async_data) {
4239 kmsg = req->async_data;
4240 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004241 /* if iov is set, it's allocated already */
4242 if (!kmsg->iov)
4243 kmsg->iov = kmsg->fast_iov;
4244 kmsg->msg.msg_iter.iov = kmsg->iov;
4245 } else {
4246 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004247 if (ret)
4248 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004249 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004250 }
4251
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004252 flags = req->sr_msg.msg_flags;
4253 if (flags & MSG_DONTWAIT)
4254 req->flags |= REQ_F_NOWAIT;
4255 else if (force_nonblock)
4256 flags |= MSG_DONTWAIT;
4257
4258 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4259 if (force_nonblock && ret == -EAGAIN)
4260 return io_setup_async_msg(req, kmsg);
4261 if (ret == -ERESTARTSYS)
4262 ret = -EINTR;
4263
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004264 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004265 kfree(kmsg->iov);
4266 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004267 if (ret < 0)
4268 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004269 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004270 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004271}
4272
Jens Axboe229a7b62020-06-22 10:13:11 -06004273static int io_send(struct io_kiocb *req, bool force_nonblock,
4274 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004275{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004276 struct io_sr_msg *sr = &req->sr_msg;
4277 struct msghdr msg;
4278 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004279 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004280 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004281 int ret;
4282
4283 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004284 if (unlikely(!sock))
4285 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004286
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004287 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4288 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004289 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004290
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004291 msg.msg_name = NULL;
4292 msg.msg_control = NULL;
4293 msg.msg_controllen = 0;
4294 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004295
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004296 flags = req->sr_msg.msg_flags;
4297 if (flags & MSG_DONTWAIT)
4298 req->flags |= REQ_F_NOWAIT;
4299 else if (force_nonblock)
4300 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004301
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004302 msg.msg_flags = flags;
4303 ret = sock_sendmsg(sock, &msg);
4304 if (force_nonblock && ret == -EAGAIN)
4305 return -EAGAIN;
4306 if (ret == -ERESTARTSYS)
4307 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004308
Jens Axboe03b12302019-12-02 18:50:25 -07004309 if (ret < 0)
4310 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004311 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004312 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004313}
4314
Pavel Begunkov1400e692020-07-12 20:41:05 +03004315static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4316 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004317{
4318 struct io_sr_msg *sr = &req->sr_msg;
4319 struct iovec __user *uiov;
4320 size_t iov_len;
4321 int ret;
4322
Pavel Begunkov1400e692020-07-12 20:41:05 +03004323 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4324 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004325 if (ret)
4326 return ret;
4327
4328 if (req->flags & REQ_F_BUFFER_SELECT) {
4329 if (iov_len > 1)
4330 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004331 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004332 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004333 sr->len = iomsg->iov[0].iov_len;
4334 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004335 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004336 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004337 } else {
4338 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004339 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004340 if (ret > 0)
4341 ret = 0;
4342 }
4343
4344 return ret;
4345}
4346
4347#ifdef CONFIG_COMPAT
4348static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004349 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004350{
4351 struct compat_msghdr __user *msg_compat;
4352 struct io_sr_msg *sr = &req->sr_msg;
4353 struct compat_iovec __user *uiov;
4354 compat_uptr_t ptr;
4355 compat_size_t len;
4356 int ret;
4357
Pavel Begunkov270a5942020-07-12 20:41:04 +03004358 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004359 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004360 &ptr, &len);
4361 if (ret)
4362 return ret;
4363
4364 uiov = compat_ptr(ptr);
4365 if (req->flags & REQ_F_BUFFER_SELECT) {
4366 compat_ssize_t clen;
4367
4368 if (len > 1)
4369 return -EINVAL;
4370 if (!access_ok(uiov, sizeof(*uiov)))
4371 return -EFAULT;
4372 if (__get_user(clen, &uiov->iov_len))
4373 return -EFAULT;
4374 if (clen < 0)
4375 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004376 sr->len = iomsg->iov[0].iov_len;
4377 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004378 } else {
4379 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004380 &iomsg->iov,
4381 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004382 if (ret < 0)
4383 return ret;
4384 }
4385
4386 return 0;
4387}
Jens Axboe03b12302019-12-02 18:50:25 -07004388#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004389
Pavel Begunkov1400e692020-07-12 20:41:05 +03004390static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4391 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004392{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004393 iomsg->msg.msg_name = &iomsg->addr;
4394 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004395
4396#ifdef CONFIG_COMPAT
4397 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004398 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004399#endif
4400
Pavel Begunkov1400e692020-07-12 20:41:05 +03004401 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004402}
4403
Jens Axboebcda7ba2020-02-23 16:42:51 -07004404static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004405 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004406{
4407 struct io_sr_msg *sr = &req->sr_msg;
4408 struct io_buffer *kbuf;
4409
Jens Axboebcda7ba2020-02-23 16:42:51 -07004410 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4411 if (IS_ERR(kbuf))
4412 return kbuf;
4413
4414 sr->kbuf = kbuf;
4415 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004416 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004417}
4418
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004419static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4420{
4421 return io_put_kbuf(req, req->sr_msg.kbuf);
4422}
4423
Jens Axboe3529d8c2019-12-19 18:24:38 -07004424static int io_recvmsg_prep(struct io_kiocb *req,
4425 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004426{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004427 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004428 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004429 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004430
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004431 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4432 return -EINVAL;
4433
Jens Axboe3529d8c2019-12-19 18:24:38 -07004434 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004435 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004436 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004437 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004438
Jens Axboed8768362020-02-27 14:17:49 -07004439#ifdef CONFIG_COMPAT
4440 if (req->ctx->compat)
4441 sr->msg_flags |= MSG_CMSG_COMPAT;
4442#endif
4443
Jens Axboee8c2bc12020-08-15 18:44:09 -07004444 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004445 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004446 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004447 if (!ret)
4448 req->flags |= REQ_F_NEED_CLEANUP;
4449 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004450}
4451
Jens Axboe229a7b62020-06-22 10:13:11 -06004452static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4453 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004454{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004455 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004456 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004457 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004458 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004459 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004460
Jens Axboe0fa03c62019-04-19 13:34:07 -06004461 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004462 if (unlikely(!sock))
4463 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004464
Jens Axboee8c2bc12020-08-15 18:44:09 -07004465 if (req->async_data) {
4466 kmsg = req->async_data;
4467 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004468 /* if iov is set, it's allocated already */
4469 if (!kmsg->iov)
4470 kmsg->iov = kmsg->fast_iov;
4471 kmsg->msg.msg_iter.iov = kmsg->iov;
4472 } else {
4473 ret = io_recvmsg_copy_hdr(req, &iomsg);
4474 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004475 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004476 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004477 }
4478
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004479 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004480 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004481 if (IS_ERR(kbuf))
4482 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004483 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4484 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4485 1, req->sr_msg.len);
4486 }
4487
4488 flags = req->sr_msg.msg_flags;
4489 if (flags & MSG_DONTWAIT)
4490 req->flags |= REQ_F_NOWAIT;
4491 else if (force_nonblock)
4492 flags |= MSG_DONTWAIT;
4493
4494 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4495 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004496 if (force_nonblock && ret == -EAGAIN)
4497 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004498 if (ret == -ERESTARTSYS)
4499 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004500
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004501 if (req->flags & REQ_F_BUFFER_SELECTED)
4502 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004503 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004504 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004505 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004506 if (ret < 0)
4507 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004508 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004509 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004510}
4511
Jens Axboe229a7b62020-06-22 10:13:11 -06004512static int io_recv(struct io_kiocb *req, bool force_nonblock,
4513 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004514{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004515 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004516 struct io_sr_msg *sr = &req->sr_msg;
4517 struct msghdr msg;
4518 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004519 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004520 struct iovec iov;
4521 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004522 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004523
Jens Axboefddafac2020-01-04 20:19:44 -07004524 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004525 if (unlikely(!sock))
4526 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004527
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004528 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004529 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004530 if (IS_ERR(kbuf))
4531 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004532 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004533 }
4534
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004535 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004536 if (unlikely(ret))
4537 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004538
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004539 msg.msg_name = NULL;
4540 msg.msg_control = NULL;
4541 msg.msg_controllen = 0;
4542 msg.msg_namelen = 0;
4543 msg.msg_iocb = NULL;
4544 msg.msg_flags = 0;
4545
4546 flags = req->sr_msg.msg_flags;
4547 if (flags & MSG_DONTWAIT)
4548 req->flags |= REQ_F_NOWAIT;
4549 else if (force_nonblock)
4550 flags |= MSG_DONTWAIT;
4551
4552 ret = sock_recvmsg(sock, &msg, flags);
4553 if (force_nonblock && ret == -EAGAIN)
4554 return -EAGAIN;
4555 if (ret == -ERESTARTSYS)
4556 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004557out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004558 if (req->flags & REQ_F_BUFFER_SELECTED)
4559 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004560 if (ret < 0)
4561 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004562 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004563 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004564}
4565
Jens Axboe3529d8c2019-12-19 18:24:38 -07004566static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004567{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004568 struct io_accept *accept = &req->accept;
4569
Jens Axboe17f2fe32019-10-17 14:42:58 -06004570 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4571 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004572 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004573 return -EINVAL;
4574
Jens Axboed55e5f52019-12-11 16:12:15 -07004575 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4576 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004577 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004578 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004579 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004580}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004581
Jens Axboe229a7b62020-06-22 10:13:11 -06004582static int io_accept(struct io_kiocb *req, bool force_nonblock,
4583 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004584{
4585 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004586 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004587 int ret;
4588
Jiufei Xuee697dee2020-06-10 13:41:59 +08004589 if (req->file->f_flags & O_NONBLOCK)
4590 req->flags |= REQ_F_NOWAIT;
4591
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004592 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004593 accept->addr_len, accept->flags,
4594 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004595 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004596 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004597 if (ret < 0) {
4598 if (ret == -ERESTARTSYS)
4599 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004600 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004601 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004602 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004603 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004604}
4605
Jens Axboe3529d8c2019-12-19 18:24:38 -07004606static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004607{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004608 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004609 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004610
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004611 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4612 return -EINVAL;
4613 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4614 return -EINVAL;
4615
Jens Axboe3529d8c2019-12-19 18:24:38 -07004616 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4617 conn->addr_len = READ_ONCE(sqe->addr2);
4618
4619 if (!io)
4620 return 0;
4621
4622 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004623 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004624}
4625
Jens Axboe229a7b62020-06-22 10:13:11 -06004626static int io_connect(struct io_kiocb *req, bool force_nonblock,
4627 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004628{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004629 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004630 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004631 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004632
Jens Axboee8c2bc12020-08-15 18:44:09 -07004633 if (req->async_data) {
4634 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004635 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004636 ret = move_addr_to_kernel(req->connect.addr,
4637 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004638 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004639 if (ret)
4640 goto out;
4641 io = &__io;
4642 }
4643
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004644 file_flags = force_nonblock ? O_NONBLOCK : 0;
4645
Jens Axboee8c2bc12020-08-15 18:44:09 -07004646 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004647 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004648 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004649 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004650 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004651 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004652 ret = -ENOMEM;
4653 goto out;
4654 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004655 io = req->async_data;
4656 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004657 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004658 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004659 if (ret == -ERESTARTSYS)
4660 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004661out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004662 if (ret < 0)
4663 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004664 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004665 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004666}
YueHaibing469956e2020-03-04 15:53:52 +08004667#else /* !CONFIG_NET */
4668static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4669{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004670 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004671}
4672
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004673static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4674 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004675{
YueHaibing469956e2020-03-04 15:53:52 +08004676 return -EOPNOTSUPP;
4677}
4678
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004679static int io_send(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_recvmsg_prep(struct io_kiocb *req,
4686 const struct io_uring_sqe *sqe)
4687{
4688 return -EOPNOTSUPP;
4689}
4690
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004691static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4692 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004693{
4694 return -EOPNOTSUPP;
4695}
4696
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004697static int io_recv(struct io_kiocb *req, bool force_nonblock,
4698 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004699{
4700 return -EOPNOTSUPP;
4701}
4702
4703static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4704{
4705 return -EOPNOTSUPP;
4706}
4707
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004708static int io_accept(struct io_kiocb *req, bool force_nonblock,
4709 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004710{
4711 return -EOPNOTSUPP;
4712}
4713
4714static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4715{
4716 return -EOPNOTSUPP;
4717}
4718
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004719static int io_connect(struct io_kiocb *req, bool force_nonblock,
4720 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004721{
4722 return -EOPNOTSUPP;
4723}
4724#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004725
Jens Axboed7718a92020-02-14 22:23:12 -07004726struct io_poll_table {
4727 struct poll_table_struct pt;
4728 struct io_kiocb *req;
4729 int error;
4730};
4731
Jens Axboed7718a92020-02-14 22:23:12 -07004732static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4733 __poll_t mask, task_work_func_t func)
4734{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004735 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004736 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004737
4738 /* for instances that support it check for an event match first: */
4739 if (mask && !(mask & poll->events))
4740 return 0;
4741
4742 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4743
4744 list_del_init(&poll->wait.entry);
4745
Jens Axboed7718a92020-02-14 22:23:12 -07004746 req->result = mask;
4747 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004748 percpu_ref_get(&req->ctx->refs);
4749
Jens Axboed7718a92020-02-14 22:23:12 -07004750 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004751 * If we using the signalfd wait_queue_head for this wakeup, then
4752 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4753 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4754 * either, as the normal wakeup will suffice.
4755 */
4756 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4757
4758 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004759 * If this fails, then the task is exiting. When a task exits, the
4760 * work gets canceled, so just cancel this request as well instead
4761 * of executing it. We can't safely execute it anyway, as we may not
4762 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004763 */
Jens Axboe87c43112020-09-30 21:00:14 -06004764 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004765 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004766 struct task_struct *tsk;
4767
Jens Axboee3aabf92020-05-18 11:04:17 -06004768 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004769 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004770 task_work_add(tsk, &req->task_work, 0);
4771 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004772 }
Jens Axboed7718a92020-02-14 22:23:12 -07004773 return 1;
4774}
4775
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004776static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4777 __acquires(&req->ctx->completion_lock)
4778{
4779 struct io_ring_ctx *ctx = req->ctx;
4780
4781 if (!req->result && !READ_ONCE(poll->canceled)) {
4782 struct poll_table_struct pt = { ._key = poll->events };
4783
4784 req->result = vfs_poll(req->file, &pt) & poll->events;
4785 }
4786
4787 spin_lock_irq(&ctx->completion_lock);
4788 if (!req->result && !READ_ONCE(poll->canceled)) {
4789 add_wait_queue(poll->head, &poll->wait);
4790 return true;
4791 }
4792
4793 return false;
4794}
4795
Jens Axboed4e7cd32020-08-15 11:44:50 -07004796static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004797{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004798 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004799 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004800 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004801 return req->apoll->double_poll;
4802}
4803
4804static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4805{
4806 if (req->opcode == IORING_OP_POLL_ADD)
4807 return &req->poll;
4808 return &req->apoll->poll;
4809}
4810
4811static void io_poll_remove_double(struct io_kiocb *req)
4812{
4813 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004814
4815 lockdep_assert_held(&req->ctx->completion_lock);
4816
4817 if (poll && poll->head) {
4818 struct wait_queue_head *head = poll->head;
4819
4820 spin_lock(&head->lock);
4821 list_del_init(&poll->wait.entry);
4822 if (poll->wait.private)
4823 refcount_dec(&req->refs);
4824 poll->head = NULL;
4825 spin_unlock(&head->lock);
4826 }
4827}
4828
4829static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4830{
4831 struct io_ring_ctx *ctx = req->ctx;
4832
Jens Axboed4e7cd32020-08-15 11:44:50 -07004833 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004834 req->poll.done = true;
4835 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4836 io_commit_cqring(ctx);
4837}
4838
4839static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4840{
4841 struct io_ring_ctx *ctx = req->ctx;
4842
4843 if (io_poll_rewait(req, &req->poll)) {
4844 spin_unlock_irq(&ctx->completion_lock);
4845 return;
4846 }
4847
4848 hash_del(&req->hash_node);
4849 io_poll_complete(req, req->result, 0);
4850 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03004851 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004852 spin_unlock_irq(&ctx->completion_lock);
4853
4854 io_cqring_ev_posted(ctx);
4855}
4856
4857static void io_poll_task_func(struct callback_head *cb)
4858{
4859 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004860 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004861 struct io_kiocb *nxt = NULL;
4862
4863 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004864 if (nxt)
4865 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004866 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004867}
4868
4869static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4870 int sync, void *key)
4871{
4872 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004873 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004874 __poll_t mask = key_to_poll(key);
4875
4876 /* for instances that support it check for an event match first: */
4877 if (mask && !(mask & poll->events))
4878 return 0;
4879
Jens Axboe8706e042020-09-28 08:38:54 -06004880 list_del_init(&wait->entry);
4881
Jens Axboe807abcb2020-07-17 17:09:27 -06004882 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004883 bool done;
4884
Jens Axboe807abcb2020-07-17 17:09:27 -06004885 spin_lock(&poll->head->lock);
4886 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004887 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004888 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004889 /* make sure double remove sees this as being gone */
4890 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004891 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004892 if (!done)
4893 __io_async_wake(req, poll, mask, io_poll_task_func);
4894 }
4895 refcount_dec(&req->refs);
4896 return 1;
4897}
4898
4899static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4900 wait_queue_func_t wake_func)
4901{
4902 poll->head = NULL;
4903 poll->done = false;
4904 poll->canceled = false;
4905 poll->events = events;
4906 INIT_LIST_HEAD(&poll->wait.entry);
4907 init_waitqueue_func_entry(&poll->wait, wake_func);
4908}
4909
4910static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004911 struct wait_queue_head *head,
4912 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004913{
4914 struct io_kiocb *req = pt->req;
4915
4916 /*
4917 * If poll->head is already set, it's because the file being polled
4918 * uses multiple waitqueues for poll handling (eg one for read, one
4919 * for write). Setup a separate io_poll_iocb if this happens.
4920 */
4921 if (unlikely(poll->head)) {
4922 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004923 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004924 pt->error = -EINVAL;
4925 return;
4926 }
4927 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4928 if (!poll) {
4929 pt->error = -ENOMEM;
4930 return;
4931 }
4932 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4933 refcount_inc(&req->refs);
4934 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004935 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004936 }
4937
4938 pt->error = 0;
4939 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004940
4941 if (poll->events & EPOLLEXCLUSIVE)
4942 add_wait_queue_exclusive(head, &poll->wait);
4943 else
4944 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004945}
4946
4947static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4948 struct poll_table_struct *p)
4949{
4950 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004951 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004952
Jens Axboe807abcb2020-07-17 17:09:27 -06004953 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004954}
4955
Jens Axboed7718a92020-02-14 22:23:12 -07004956static void io_async_task_func(struct callback_head *cb)
4957{
4958 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4959 struct async_poll *apoll = req->apoll;
4960 struct io_ring_ctx *ctx = req->ctx;
4961
4962 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4963
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004964 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004965 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004966 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004967 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004968 }
4969
Jens Axboe31067252020-05-17 17:43:31 -06004970 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004971 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004972 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004973
Jens Axboed4e7cd32020-08-15 11:44:50 -07004974 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004975 spin_unlock_irq(&ctx->completion_lock);
4976
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004977 if (!READ_ONCE(apoll->poll.canceled))
4978 __io_req_task_submit(req);
4979 else
4980 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004981
Jens Axboe6d816e02020-08-11 08:04:14 -06004982 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004983 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004984 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004985}
4986
4987static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4988 void *key)
4989{
4990 struct io_kiocb *req = wait->private;
4991 struct io_poll_iocb *poll = &req->apoll->poll;
4992
4993 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4994 key_to_poll(key));
4995
4996 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4997}
4998
4999static void io_poll_req_insert(struct io_kiocb *req)
5000{
5001 struct io_ring_ctx *ctx = req->ctx;
5002 struct hlist_head *list;
5003
5004 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5005 hlist_add_head(&req->hash_node, list);
5006}
5007
5008static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5009 struct io_poll_iocb *poll,
5010 struct io_poll_table *ipt, __poll_t mask,
5011 wait_queue_func_t wake_func)
5012 __acquires(&ctx->completion_lock)
5013{
5014 struct io_ring_ctx *ctx = req->ctx;
5015 bool cancel = false;
5016
Jens Axboe18bceab2020-05-15 11:56:54 -06005017 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005018 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005019 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005020
5021 ipt->pt._key = mask;
5022 ipt->req = req;
5023 ipt->error = -EINVAL;
5024
Jens Axboed7718a92020-02-14 22:23:12 -07005025 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5026
5027 spin_lock_irq(&ctx->completion_lock);
5028 if (likely(poll->head)) {
5029 spin_lock(&poll->head->lock);
5030 if (unlikely(list_empty(&poll->wait.entry))) {
5031 if (ipt->error)
5032 cancel = true;
5033 ipt->error = 0;
5034 mask = 0;
5035 }
5036 if (mask || ipt->error)
5037 list_del_init(&poll->wait.entry);
5038 else if (cancel)
5039 WRITE_ONCE(poll->canceled, true);
5040 else if (!poll->done) /* actually waiting for an event */
5041 io_poll_req_insert(req);
5042 spin_unlock(&poll->head->lock);
5043 }
5044
5045 return mask;
5046}
5047
5048static bool io_arm_poll_handler(struct io_kiocb *req)
5049{
5050 const struct io_op_def *def = &io_op_defs[req->opcode];
5051 struct io_ring_ctx *ctx = req->ctx;
5052 struct async_poll *apoll;
5053 struct io_poll_table ipt;
5054 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005055 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005056
5057 if (!req->file || !file_can_poll(req->file))
5058 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005059 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005060 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005061 if (def->pollin)
5062 rw = READ;
5063 else if (def->pollout)
5064 rw = WRITE;
5065 else
5066 return false;
5067 /* if we can't nonblock try, then no point in arming a poll handler */
5068 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005069 return false;
5070
5071 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5072 if (unlikely(!apoll))
5073 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005074 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005075
5076 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005077 req->apoll = apoll;
5078 INIT_HLIST_NODE(&req->hash_node);
5079
Nathan Chancellor8755d972020-03-02 16:01:19 -07005080 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005081 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005082 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005083 if (def->pollout)
5084 mask |= POLLOUT | POLLWRNORM;
5085 mask |= POLLERR | POLLPRI;
5086
5087 ipt.pt._qproc = io_async_queue_proc;
5088
5089 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5090 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005091 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005092 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005093 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005094 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005095 kfree(apoll);
5096 return false;
5097 }
5098 spin_unlock_irq(&ctx->completion_lock);
5099 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5100 apoll->poll.events);
5101 return true;
5102}
5103
5104static bool __io_poll_remove_one(struct io_kiocb *req,
5105 struct io_poll_iocb *poll)
5106{
Jens Axboeb41e9852020-02-17 09:52:41 -07005107 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005108
5109 spin_lock(&poll->head->lock);
5110 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005111 if (!list_empty(&poll->wait.entry)) {
5112 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005113 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005114 }
5115 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005116 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005117 return do_complete;
5118}
5119
5120static bool io_poll_remove_one(struct io_kiocb *req)
5121{
5122 bool do_complete;
5123
Jens Axboed4e7cd32020-08-15 11:44:50 -07005124 io_poll_remove_double(req);
5125
Jens Axboed7718a92020-02-14 22:23:12 -07005126 if (req->opcode == IORING_OP_POLL_ADD) {
5127 do_complete = __io_poll_remove_one(req, &req->poll);
5128 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005129 struct async_poll *apoll = req->apoll;
5130
Jens Axboed7718a92020-02-14 22:23:12 -07005131 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005132 do_complete = __io_poll_remove_one(req, &apoll->poll);
5133 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005134 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005135 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005136 kfree(apoll);
5137 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005138 }
5139
Jens Axboeb41e9852020-02-17 09:52:41 -07005140 if (do_complete) {
5141 io_cqring_fill_event(req, -ECANCELED);
5142 io_commit_cqring(req->ctx);
5143 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005144 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005145 io_put_req(req);
5146 }
5147
5148 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005149}
5150
Jens Axboe76e1b642020-09-26 15:05:03 -06005151/*
5152 * Returns true if we found and killed one or more poll requests
5153 */
5154static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005155{
Jens Axboe78076bb2019-12-04 19:56:40 -07005156 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005157 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005158 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005159
5160 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005161 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5162 struct hlist_head *list;
5163
5164 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005165 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5166 if (io_task_match(req, tsk))
5167 posted += io_poll_remove_one(req);
5168 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005169 }
5170 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005171
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005172 if (posted)
5173 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005174
5175 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005176}
5177
Jens Axboe47f46762019-11-09 17:43:02 -07005178static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5179{
Jens Axboe78076bb2019-12-04 19:56:40 -07005180 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005181 struct io_kiocb *req;
5182
Jens Axboe78076bb2019-12-04 19:56:40 -07005183 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5184 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005185 if (sqe_addr != req->user_data)
5186 continue;
5187 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005188 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005189 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005190 }
5191
5192 return -ENOENT;
5193}
5194
Jens Axboe3529d8c2019-12-19 18:24:38 -07005195static int io_poll_remove_prep(struct io_kiocb *req,
5196 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005197{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005198 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5199 return -EINVAL;
5200 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5201 sqe->poll_events)
5202 return -EINVAL;
5203
Jens Axboe0969e782019-12-17 18:40:57 -07005204 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005205 return 0;
5206}
5207
5208/*
5209 * Find a running poll command that matches one specified in sqe->addr,
5210 * and remove it if found.
5211 */
5212static int io_poll_remove(struct io_kiocb *req)
5213{
5214 struct io_ring_ctx *ctx = req->ctx;
5215 u64 addr;
5216 int ret;
5217
Jens Axboe0969e782019-12-17 18:40:57 -07005218 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005219 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005220 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005221 spin_unlock_irq(&ctx->completion_lock);
5222
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005223 if (ret < 0)
5224 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005225 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005226 return 0;
5227}
5228
Jens Axboe221c5eb2019-01-17 09:41:58 -07005229static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5230 void *key)
5231{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005232 struct io_kiocb *req = wait->private;
5233 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005234
Jens Axboed7718a92020-02-14 22:23:12 -07005235 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005236}
5237
Jens Axboe221c5eb2019-01-17 09:41:58 -07005238static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5239 struct poll_table_struct *p)
5240{
5241 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5242
Jens Axboee8c2bc12020-08-15 18:44:09 -07005243 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005244}
5245
Jens Axboe3529d8c2019-12-19 18:24:38 -07005246static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005247{
5248 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005249 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005250
5251 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5252 return -EINVAL;
5253 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5254 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005255 if (!poll->file)
5256 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005257
Jiufei Xue5769a352020-06-17 17:53:55 +08005258 events = READ_ONCE(sqe->poll32_events);
5259#ifdef __BIG_ENDIAN
5260 events = swahw32(events);
5261#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005262 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5263 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005264 return 0;
5265}
5266
Pavel Begunkov014db002020-03-03 21:33:12 +03005267static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005268{
5269 struct io_poll_iocb *poll = &req->poll;
5270 struct io_ring_ctx *ctx = req->ctx;
5271 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005272 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005273
Jens Axboe78076bb2019-12-04 19:56:40 -07005274 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005275 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005276
Jens Axboed7718a92020-02-14 22:23:12 -07005277 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5278 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005279
Jens Axboe8c838782019-03-12 15:48:16 -06005280 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005281 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005282 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005283 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005284 spin_unlock_irq(&ctx->completion_lock);
5285
Jens Axboe8c838782019-03-12 15:48:16 -06005286 if (mask) {
5287 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005288 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005289 }
Jens Axboe8c838782019-03-12 15:48:16 -06005290 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005291}
5292
Jens Axboe5262f562019-09-17 12:26:57 -06005293static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5294{
Jens Axboead8a48a2019-11-15 08:49:11 -07005295 struct io_timeout_data *data = container_of(timer,
5296 struct io_timeout_data, timer);
5297 struct io_kiocb *req = data->req;
5298 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005299 unsigned long flags;
5300
Jens Axboe5262f562019-09-17 12:26:57 -06005301 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005302 atomic_set(&req->ctx->cq_timeouts,
5303 atomic_read(&req->ctx->cq_timeouts) + 1);
5304
zhangyi (F)ef036812019-10-23 15:10:08 +08005305 /*
Jens Axboe11365042019-10-16 09:08:32 -06005306 * We could be racing with timeout deletion. If the list is empty,
5307 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005308 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005309 if (!list_empty(&req->timeout.list))
5310 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005311
Jens Axboe78e19bb2019-11-06 15:21:34 -07005312 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005313 io_commit_cqring(ctx);
5314 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5315
5316 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005317 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005318 io_put_req(req);
5319 return HRTIMER_NORESTART;
5320}
5321
Jens Axboef254ac02020-08-12 17:33:30 -06005322static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005323{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005324 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005325 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005326
Jens Axboef254ac02020-08-12 17:33:30 -06005327 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005328
Jens Axboee8c2bc12020-08-15 18:44:09 -07005329 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005330 if (ret == -1)
5331 return -EALREADY;
5332
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005333 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005334 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005335 io_cqring_fill_event(req, -ECANCELED);
5336 io_put_req(req);
5337 return 0;
5338}
5339
Jens Axboef254ac02020-08-12 17:33:30 -06005340static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5341{
5342 struct io_kiocb *req;
5343 int ret = -ENOENT;
5344
5345 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5346 if (user_data == req->user_data) {
5347 ret = 0;
5348 break;
5349 }
5350 }
5351
5352 if (ret == -ENOENT)
5353 return ret;
5354
5355 return __io_timeout_cancel(req);
5356}
5357
Jens Axboe3529d8c2019-12-19 18:24:38 -07005358static int io_timeout_remove_prep(struct io_kiocb *req,
5359 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005360{
Jens Axboeb29472e2019-12-17 18:50:29 -07005361 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5362 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005363 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5364 return -EINVAL;
5365 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005366 return -EINVAL;
5367
5368 req->timeout.addr = READ_ONCE(sqe->addr);
5369 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5370 if (req->timeout.flags)
5371 return -EINVAL;
5372
Jens Axboeb29472e2019-12-17 18:50:29 -07005373 return 0;
5374}
5375
Jens Axboe11365042019-10-16 09:08:32 -06005376/*
5377 * Remove or update an existing timeout command
5378 */
Jens Axboefc4df992019-12-10 14:38:45 -07005379static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005380{
5381 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005382 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005383
Jens Axboe11365042019-10-16 09:08:32 -06005384 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005385 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005386
Jens Axboe47f46762019-11-09 17:43:02 -07005387 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005388 io_commit_cqring(ctx);
5389 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005390 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005391 if (ret < 0)
5392 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005393 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005394 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005395}
5396
Jens Axboe3529d8c2019-12-19 18:24:38 -07005397static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005398 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005399{
Jens Axboead8a48a2019-11-15 08:49:11 -07005400 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005401 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005402 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005403
Jens Axboead8a48a2019-11-15 08:49:11 -07005404 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005405 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005406 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005407 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005408 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005409 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005410 flags = READ_ONCE(sqe->timeout_flags);
5411 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005412 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005413
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005414 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005415
Jens Axboee8c2bc12020-08-15 18:44:09 -07005416 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005417 return -ENOMEM;
5418
Jens Axboee8c2bc12020-08-15 18:44:09 -07005419 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005420 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005421
5422 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005423 return -EFAULT;
5424
Jens Axboe11365042019-10-16 09:08:32 -06005425 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005426 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005427 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005428 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005429
Jens Axboead8a48a2019-11-15 08:49:11 -07005430 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5431 return 0;
5432}
5433
Jens Axboefc4df992019-12-10 14:38:45 -07005434static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005435{
Jens Axboead8a48a2019-11-15 08:49:11 -07005436 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005437 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005438 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005439 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005440
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005441 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005442
Jens Axboe5262f562019-09-17 12:26:57 -06005443 /*
5444 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005445 * timeout event to be satisfied. If it isn't set, then this is
5446 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005447 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005448 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005449 entry = ctx->timeout_list.prev;
5450 goto add;
5451 }
Jens Axboe5262f562019-09-17 12:26:57 -06005452
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005453 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5454 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005455
5456 /*
5457 * Insertion sort, ensuring the first entry in the list is always
5458 * the one we need first.
5459 */
Jens Axboe5262f562019-09-17 12:26:57 -06005460 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005461 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5462 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005463
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005464 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005465 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005466 /* nxt.seq is behind @tail, otherwise would've been completed */
5467 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005468 break;
5469 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005470add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005471 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005472 data->timer.function = io_timeout_fn;
5473 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005474 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005475 return 0;
5476}
5477
Jens Axboe62755e32019-10-28 21:49:21 -06005478static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005479{
Jens Axboe62755e32019-10-28 21:49:21 -06005480 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005481
Jens Axboe62755e32019-10-28 21:49:21 -06005482 return req->user_data == (unsigned long) data;
5483}
5484
Jens Axboee977d6d2019-11-05 12:39:45 -07005485static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005486{
Jens Axboe62755e32019-10-28 21:49:21 -06005487 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005488 int ret = 0;
5489
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005490 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005491 switch (cancel_ret) {
5492 case IO_WQ_CANCEL_OK:
5493 ret = 0;
5494 break;
5495 case IO_WQ_CANCEL_RUNNING:
5496 ret = -EALREADY;
5497 break;
5498 case IO_WQ_CANCEL_NOTFOUND:
5499 ret = -ENOENT;
5500 break;
5501 }
5502
Jens Axboee977d6d2019-11-05 12:39:45 -07005503 return ret;
5504}
5505
Jens Axboe47f46762019-11-09 17:43:02 -07005506static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5507 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005508 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005509{
5510 unsigned long flags;
5511 int ret;
5512
5513 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5514 if (ret != -ENOENT) {
5515 spin_lock_irqsave(&ctx->completion_lock, flags);
5516 goto done;
5517 }
5518
5519 spin_lock_irqsave(&ctx->completion_lock, flags);
5520 ret = io_timeout_cancel(ctx, sqe_addr);
5521 if (ret != -ENOENT)
5522 goto done;
5523 ret = io_poll_cancel(ctx, sqe_addr);
5524done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005525 if (!ret)
5526 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005527 io_cqring_fill_event(req, ret);
5528 io_commit_cqring(ctx);
5529 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5530 io_cqring_ev_posted(ctx);
5531
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005532 if (ret < 0)
5533 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005534 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005535}
5536
Jens Axboe3529d8c2019-12-19 18:24:38 -07005537static int io_async_cancel_prep(struct io_kiocb *req,
5538 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005539{
Jens Axboefbf23842019-12-17 18:45:56 -07005540 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005541 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005542 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5543 return -EINVAL;
5544 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005545 return -EINVAL;
5546
Jens Axboefbf23842019-12-17 18:45:56 -07005547 req->cancel.addr = READ_ONCE(sqe->addr);
5548 return 0;
5549}
5550
Pavel Begunkov014db002020-03-03 21:33:12 +03005551static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005552{
5553 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005554
Pavel Begunkov014db002020-03-03 21:33:12 +03005555 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005556 return 0;
5557}
5558
Jens Axboe05f3fb32019-12-09 11:22:50 -07005559static int io_files_update_prep(struct io_kiocb *req,
5560 const struct io_uring_sqe *sqe)
5561{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005562 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5563 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005564 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5565 return -EINVAL;
5566 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005567 return -EINVAL;
5568
5569 req->files_update.offset = READ_ONCE(sqe->off);
5570 req->files_update.nr_args = READ_ONCE(sqe->len);
5571 if (!req->files_update.nr_args)
5572 return -EINVAL;
5573 req->files_update.arg = READ_ONCE(sqe->addr);
5574 return 0;
5575}
5576
Jens Axboe229a7b62020-06-22 10:13:11 -06005577static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5578 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005579{
5580 struct io_ring_ctx *ctx = req->ctx;
5581 struct io_uring_files_update up;
5582 int ret;
5583
Jens Axboef86cd202020-01-29 13:46:44 -07005584 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005585 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005586
5587 up.offset = req->files_update.offset;
5588 up.fds = req->files_update.arg;
5589
5590 mutex_lock(&ctx->uring_lock);
5591 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5592 mutex_unlock(&ctx->uring_lock);
5593
5594 if (ret < 0)
5595 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005596 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005597 return 0;
5598}
5599
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005600static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5601{
5602 switch (req->opcode) {
5603 case IORING_OP_NOP:
5604 return 0;
5605 case IORING_OP_READV:
5606 case IORING_OP_READ_FIXED:
5607 case IORING_OP_READ:
5608 return io_read_prep(req, sqe);
5609 case IORING_OP_WRITEV:
5610 case IORING_OP_WRITE_FIXED:
5611 case IORING_OP_WRITE:
5612 return io_write_prep(req, sqe);
5613 case IORING_OP_POLL_ADD:
5614 return io_poll_add_prep(req, sqe);
5615 case IORING_OP_POLL_REMOVE:
5616 return io_poll_remove_prep(req, sqe);
5617 case IORING_OP_FSYNC:
5618 return io_prep_fsync(req, sqe);
5619 case IORING_OP_SYNC_FILE_RANGE:
5620 return io_prep_sfr(req, sqe);
5621 case IORING_OP_SENDMSG:
5622 case IORING_OP_SEND:
5623 return io_sendmsg_prep(req, sqe);
5624 case IORING_OP_RECVMSG:
5625 case IORING_OP_RECV:
5626 return io_recvmsg_prep(req, sqe);
5627 case IORING_OP_CONNECT:
5628 return io_connect_prep(req, sqe);
5629 case IORING_OP_TIMEOUT:
5630 return io_timeout_prep(req, sqe, false);
5631 case IORING_OP_TIMEOUT_REMOVE:
5632 return io_timeout_remove_prep(req, sqe);
5633 case IORING_OP_ASYNC_CANCEL:
5634 return io_async_cancel_prep(req, sqe);
5635 case IORING_OP_LINK_TIMEOUT:
5636 return io_timeout_prep(req, sqe, true);
5637 case IORING_OP_ACCEPT:
5638 return io_accept_prep(req, sqe);
5639 case IORING_OP_FALLOCATE:
5640 return io_fallocate_prep(req, sqe);
5641 case IORING_OP_OPENAT:
5642 return io_openat_prep(req, sqe);
5643 case IORING_OP_CLOSE:
5644 return io_close_prep(req, sqe);
5645 case IORING_OP_FILES_UPDATE:
5646 return io_files_update_prep(req, sqe);
5647 case IORING_OP_STATX:
5648 return io_statx_prep(req, sqe);
5649 case IORING_OP_FADVISE:
5650 return io_fadvise_prep(req, sqe);
5651 case IORING_OP_MADVISE:
5652 return io_madvise_prep(req, sqe);
5653 case IORING_OP_OPENAT2:
5654 return io_openat2_prep(req, sqe);
5655 case IORING_OP_EPOLL_CTL:
5656 return io_epoll_ctl_prep(req, sqe);
5657 case IORING_OP_SPLICE:
5658 return io_splice_prep(req, sqe);
5659 case IORING_OP_PROVIDE_BUFFERS:
5660 return io_provide_buffers_prep(req, sqe);
5661 case IORING_OP_REMOVE_BUFFERS:
5662 return io_remove_buffers_prep(req, sqe);
5663 case IORING_OP_TEE:
5664 return io_tee_prep(req, sqe);
5665 }
5666
5667 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5668 req->opcode);
5669 return-EINVAL;
5670}
5671
Jens Axboe3529d8c2019-12-19 18:24:38 -07005672static int io_req_defer_prep(struct io_kiocb *req,
5673 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005674{
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005675 if (!sqe)
5676 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005677 if (io_alloc_async_data(req))
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005678 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005679 return io_req_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005680}
5681
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005682static u32 io_get_sequence(struct io_kiocb *req)
5683{
5684 struct io_kiocb *pos;
5685 struct io_ring_ctx *ctx = req->ctx;
5686 u32 total_submitted, nr_reqs = 1;
5687
5688 if (req->flags & REQ_F_LINK_HEAD)
5689 list_for_each_entry(pos, &req->link_list, link_list)
5690 nr_reqs++;
5691
5692 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5693 return total_submitted - nr_reqs;
5694}
5695
Jens Axboe3529d8c2019-12-19 18:24:38 -07005696static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005697{
Jackie Liua197f662019-11-08 08:09:12 -07005698 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005699 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005700 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005701 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005702
Bob Liu9d858b22019-11-13 18:06:25 +08005703 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005704 if (likely(list_empty_careful(&ctx->defer_list) &&
5705 !(req->flags & REQ_F_IO_DRAIN)))
5706 return 0;
5707
5708 seq = io_get_sequence(req);
5709 /* Still a chance to pass the sequence check */
5710 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005711 return 0;
5712
Jens Axboee8c2bc12020-08-15 18:44:09 -07005713 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005714 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005715 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005716 return ret;
5717 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005718 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005719 de = kmalloc(sizeof(*de), GFP_KERNEL);
5720 if (!de)
5721 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005722
Jens Axboede0617e2019-04-06 21:51:27 -06005723 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005724 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005725 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005726 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005727 io_queue_async_work(req);
5728 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005729 }
5730
Jens Axboe915967f2019-11-21 09:01:20 -07005731 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005732 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005733 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005734 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005735 spin_unlock_irq(&ctx->completion_lock);
5736 return -EIOCBQUEUED;
5737}
5738
Jens Axboef573d382020-09-22 10:19:24 -06005739static void io_req_drop_files(struct io_kiocb *req)
5740{
5741 struct io_ring_ctx *ctx = req->ctx;
5742 unsigned long flags;
5743
5744 spin_lock_irqsave(&ctx->inflight_lock, flags);
5745 list_del(&req->inflight_entry);
5746 if (waitqueue_active(&ctx->inflight_wait))
5747 wake_up(&ctx->inflight_wait);
5748 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5749 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005750 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005751 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005752 req->work.files = NULL;
5753}
5754
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005755static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005756{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005757 if (req->flags & REQ_F_BUFFER_SELECTED) {
5758 switch (req->opcode) {
5759 case IORING_OP_READV:
5760 case IORING_OP_READ_FIXED:
5761 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005762 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005763 break;
5764 case IORING_OP_RECVMSG:
5765 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005766 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005767 break;
5768 }
5769 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005770 }
5771
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005772 if (req->flags & REQ_F_NEED_CLEANUP) {
5773 switch (req->opcode) {
5774 case IORING_OP_READV:
5775 case IORING_OP_READ_FIXED:
5776 case IORING_OP_READ:
5777 case IORING_OP_WRITEV:
5778 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005779 case IORING_OP_WRITE: {
5780 struct io_async_rw *io = req->async_data;
5781 if (io->free_iovec)
5782 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005783 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005784 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005785 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005786 case IORING_OP_SENDMSG: {
5787 struct io_async_msghdr *io = req->async_data;
5788 if (io->iov != io->fast_iov)
5789 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005790 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005791 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005792 case IORING_OP_SPLICE:
5793 case IORING_OP_TEE:
5794 io_put_file(req, req->splice.file_in,
5795 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5796 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005797 case IORING_OP_OPENAT:
5798 case IORING_OP_OPENAT2:
5799 if (req->open.filename)
5800 putname(req->open.filename);
5801 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005802 }
5803 req->flags &= ~REQ_F_NEED_CLEANUP;
5804 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005805
Jens Axboef573d382020-09-22 10:19:24 -06005806 if (req->flags & REQ_F_INFLIGHT)
5807 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005808}
5809
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005810static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
5811 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005812{
Jackie Liua197f662019-11-08 08:09:12 -07005813 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005814 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005815
Jens Axboed625c6e2019-12-17 19:53:05 -07005816 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005817 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005818 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005819 break;
5820 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005821 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005822 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06005823 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005824 break;
5825 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005826 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005827 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06005828 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005829 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005830 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03005831 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005832 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005833 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03005834 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005835 break;
5836 case IORING_OP_POLL_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07005837 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005838 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005839 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005840 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005841 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005842 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005843 case IORING_OP_SEND:
Jens Axboefddafac2020-01-04 20:19:44 -07005844 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005845 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005846 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005847 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005848 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005849 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005850 case IORING_OP_RECV:
Jens Axboefddafac2020-01-04 20:19:44 -07005851 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005852 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005853 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005854 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005855 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005856 case IORING_OP_TIMEOUT:
Jens Axboefc4df992019-12-10 14:38:45 -07005857 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005858 break;
Jens Axboe11365042019-10-16 09:08:32 -06005859 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07005860 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005861 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005862 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005863 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005864 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005865 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005866 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005867 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005868 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03005869 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005870 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005871 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005872 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005873 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005874 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03005875 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005876 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005877 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06005878 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005879 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005880 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06005881 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005882 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005883 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03005884 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005885 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005886 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005887 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005888 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005889 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005890 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005891 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005892 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03005893 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005894 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005895 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06005896 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005897 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005898 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005899 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005900 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005901 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06005902 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005903 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005904 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06005905 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005906 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005907 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005908 ret = io_tee(req, force_nonblock);
5909 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005910 default:
5911 ret = -EINVAL;
5912 break;
5913 }
5914
5915 if (ret)
5916 return ret;
5917
Jens Axboeb5325762020-05-19 21:20:27 -06005918 /* If the op doesn't have a file, we're not polling for it */
5919 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005920 const bool in_async = io_wq_current_is_worker();
5921
Jens Axboe11ba8202020-01-15 21:51:17 -07005922 /* workqueue context doesn't hold uring_lock, grab it now */
5923 if (in_async)
5924 mutex_lock(&ctx->uring_lock);
5925
Jens Axboe2b188cc2019-01-07 10:46:33 -07005926 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005927
5928 if (in_async)
5929 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005930 }
5931
5932 return 0;
5933}
5934
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005935static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005936{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005937 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005938 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005939 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005940
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005941 timeout = io_prep_linked_timeout(req);
5942 if (timeout)
5943 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005944
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005945 /* if NO_CANCEL is set, we must still run the work */
5946 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5947 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005948 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005949 }
Jens Axboe31b51512019-01-18 22:56:34 -07005950
Jens Axboe561fb042019-10-24 07:25:42 -06005951 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005952 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005953 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005954 /*
5955 * We can get EAGAIN for polled IO even though we're
5956 * forcing a sync submission from here, since we can't
5957 * wait for request slots on the block side.
5958 */
5959 if (ret != -EAGAIN)
5960 break;
5961 cond_resched();
5962 } while (1);
5963 }
Jens Axboe31b51512019-01-18 22:56:34 -07005964
Jens Axboe561fb042019-10-24 07:25:42 -06005965 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005966 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005967 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005968 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005969
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005970 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005971}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005972
Jens Axboe65e19f52019-10-26 07:20:21 -06005973static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5974 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005975{
Jens Axboe65e19f52019-10-26 07:20:21 -06005976 struct fixed_file_table *table;
5977
Jens Axboe05f3fb32019-12-09 11:22:50 -07005978 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005979 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005980}
5981
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005982static struct file *io_file_get(struct io_submit_state *state,
5983 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005984{
5985 struct io_ring_ctx *ctx = req->ctx;
5986 struct file *file;
5987
5988 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01005989 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005990 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005991 fd = array_index_nospec(fd, ctx->nr_user_files);
5992 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005993 if (file) {
5994 req->fixed_file_refs = ctx->file_data->cur_refs;
5995 percpu_ref_get(req->fixed_file_refs);
5996 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005997 } else {
5998 trace_io_uring_file_get(ctx, fd);
5999 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006000 }
6001
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006002 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006003}
6004
Jens Axboe3529d8c2019-12-19 18:24:38 -07006005static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006006 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006007{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006008 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006009
Jens Axboe63ff8222020-05-07 14:56:15 -06006010 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006011 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006012 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006013
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006014 req->file = io_file_get(state, req, fd, fixed);
6015 if (req->file || io_op_defs[req->opcode].needs_file_no_error)
6016 return 0;
6017 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006018}
6019
Jens Axboe2665abf2019-11-05 12:40:47 -07006020static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6021{
Jens Axboead8a48a2019-11-15 08:49:11 -07006022 struct io_timeout_data *data = container_of(timer,
6023 struct io_timeout_data, timer);
6024 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006025 struct io_ring_ctx *ctx = req->ctx;
6026 struct io_kiocb *prev = NULL;
6027 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006028
6029 spin_lock_irqsave(&ctx->completion_lock, flags);
6030
6031 /*
6032 * We don't expect the list to be empty, that will only happen if we
6033 * race with the completion of the linked work.
6034 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006035 if (!list_empty(&req->link_list)) {
6036 prev = list_entry(req->link_list.prev, struct io_kiocb,
6037 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006038 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006039 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006040 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6041 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006042 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006043 }
6044
6045 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6046
6047 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006048 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006049 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006050 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006051 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006052 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006053 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006054 return HRTIMER_NORESTART;
6055}
6056
Jens Axboe7271ef32020-08-10 09:55:22 -06006057static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006058{
Jens Axboe76a46e02019-11-10 23:34:16 -07006059 /*
6060 * If the list is now empty, then our linked request finished before
6061 * we got a chance to setup the timer
6062 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006063 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006064 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006065
Jens Axboead8a48a2019-11-15 08:49:11 -07006066 data->timer.function = io_link_timeout_fn;
6067 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6068 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006069 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006070}
6071
6072static void io_queue_linked_timeout(struct io_kiocb *req)
6073{
6074 struct io_ring_ctx *ctx = req->ctx;
6075
6076 spin_lock_irq(&ctx->completion_lock);
6077 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006078 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006079
Jens Axboe2665abf2019-11-05 12:40:47 -07006080 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006081 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006082}
6083
Jens Axboead8a48a2019-11-15 08:49:11 -07006084static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006085{
6086 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006087
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006088 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006089 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006090 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006091 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006092
Pavel Begunkov44932332019-12-05 16:16:35 +03006093 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6094 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006095 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006096 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006097
Jens Axboe76a46e02019-11-10 23:34:16 -07006098 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006099 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006100}
6101
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006102static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006103{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006104 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006105 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006106 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006107 int ret;
6108
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006109again:
6110 linked_timeout = io_prep_linked_timeout(req);
6111
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006112 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6113 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006114 if (old_creds)
6115 revert_creds(old_creds);
6116 if (old_creds == req->work.creds)
6117 old_creds = NULL; /* restored original creds */
6118 else
6119 old_creds = override_creds(req->work.creds);
6120 }
6121
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006122 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006123
6124 /*
6125 * We async punt it if the file wasn't marked NOWAIT, or if the file
6126 * doesn't support non-blocking read/write attempts
6127 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006128 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006129 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006130punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006131 /*
6132 * Queued up for async execution, worker will release
6133 * submit reference when the iocb is actually submitted.
6134 */
6135 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006136 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006137
Pavel Begunkovf063c542020-07-25 14:41:59 +03006138 if (linked_timeout)
6139 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006140 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006141 }
Jens Axboee65ef562019-03-12 10:16:44 -06006142
Pavel Begunkov652532a2020-07-03 22:15:07 +03006143 if (unlikely(ret)) {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006144 /* un-prep timeout, so it'll be killed as any other linked */
6145 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006146 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006147 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006148 io_req_complete(req, ret);
6149 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006150 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006151
Jens Axboe6c271ce2019-01-10 11:22:30 -07006152 /* drop submission reference */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03006153 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006154 if (linked_timeout)
6155 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006156
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006157 if (nxt) {
6158 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006159
6160 if (req->flags & REQ_F_FORCE_ASYNC)
6161 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006162 goto again;
6163 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006164exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006165 if (old_creds)
6166 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006167}
6168
Jens Axboef13fad72020-06-22 09:34:30 -06006169static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6170 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006171{
6172 int ret;
6173
Jens Axboe3529d8c2019-12-19 18:24:38 -07006174 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006175 if (ret) {
6176 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006177fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006178 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006179 io_put_req(req);
6180 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006181 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006182 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006183 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006184 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006185 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006186 goto fail_req;
6187 }
6188
Jens Axboece35a472019-12-17 08:04:44 -07006189 /*
6190 * Never try inline submit of IOSQE_ASYNC is set, go straight
6191 * to async execution.
6192 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006193 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006194 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6195 io_queue_async_work(req);
6196 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006197 if (sqe) {
6198 ret = io_req_prep(req, sqe);
6199 if (unlikely(ret))
6200 goto fail_req;
6201 }
6202 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006203 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006204}
6205
Jens Axboef13fad72020-06-22 09:34:30 -06006206static inline void io_queue_link_head(struct io_kiocb *req,
6207 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006208{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006209 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006210 io_put_req(req);
6211 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006212 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006213 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006214}
6215
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006216static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006217 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006218{
Jackie Liua197f662019-11-08 08:09:12 -07006219 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006220 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006221
Jens Axboe9e645e112019-05-10 16:07:28 -06006222 /*
6223 * If we already have a head request, queue this one for async
6224 * submittal once the head completes. If we don't have a head but
6225 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6226 * submitted sync once the chain is complete. If none of those
6227 * conditions are true (normal request), then just queue it.
6228 */
6229 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006230 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006231
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006232 /*
6233 * Taking sequential execution of a link, draining both sides
6234 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6235 * requests in the link. So, it drains the head and the
6236 * next after the link request. The last one is done via
6237 * drain_next flag to persist the effect across calls.
6238 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006239 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006240 head->flags |= REQ_F_IO_DRAIN;
6241 ctx->drain_next = 1;
6242 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006243 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006244 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006245 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006246 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006247 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006248 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006249 trace_io_uring_link(ctx, req, head);
6250 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006251
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006252 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006253 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006254 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006255 *link = NULL;
6256 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006257 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006258 if (unlikely(ctx->drain_next)) {
6259 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006260 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006261 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006262 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006263 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006264 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006265
Pavel Begunkov711be032020-01-17 03:57:59 +03006266 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006267 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006268 req->flags |= REQ_F_FAIL_LINK;
6269 *link = req;
6270 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006271 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006272 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006273 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006274
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006275 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006276}
6277
Jens Axboe9a56a232019-01-09 09:06:50 -07006278/*
6279 * Batched submission is done, ensure local IO is flushed out.
6280 */
6281static void io_submit_state_end(struct io_submit_state *state)
6282{
Jens Axboef13fad72020-06-22 09:34:30 -06006283 if (!list_empty(&state->comp.list))
6284 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006285 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006286 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006287 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006288 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006289}
6290
6291/*
6292 * Start submission side cache.
6293 */
6294static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006295 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006296{
6297 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006298 state->comp.nr = 0;
6299 INIT_LIST_HEAD(&state->comp.list);
6300 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006301 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006302 state->file = NULL;
6303 state->ios_left = max_ios;
6304}
6305
Jens Axboe2b188cc2019-01-07 10:46:33 -07006306static void io_commit_sqring(struct io_ring_ctx *ctx)
6307{
Hristo Venev75b28af2019-08-26 17:23:46 +00006308 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006309
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006310 /*
6311 * Ensure any loads from the SQEs are done at this point,
6312 * since once we write the new head, the application could
6313 * write new data to them.
6314 */
6315 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316}
6317
6318/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006319 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320 * that is mapped by userspace. This means that care needs to be taken to
6321 * ensure that reads are stable, as we cannot rely on userspace always
6322 * being a good citizen. If members of the sqe are validated and then later
6323 * used, it's important that those reads are done through READ_ONCE() to
6324 * prevent a re-load down the line.
6325 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006326static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006327{
Hristo Venev75b28af2019-08-26 17:23:46 +00006328 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006329 unsigned head;
6330
6331 /*
6332 * The cached sq head (or cq tail) serves two purposes:
6333 *
6334 * 1) allows us to batch the cost of updating the user visible
6335 * head updates.
6336 * 2) allows the kernel side to track the head on its own, even
6337 * though the application is the one updating it.
6338 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006339 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006340 if (likely(head < ctx->sq_entries))
6341 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006342
6343 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006344 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006345 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006346 return NULL;
6347}
6348
6349static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6350{
6351 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006352}
6353
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006354/*
6355 * Check SQE restrictions (opcode and flags).
6356 *
6357 * Returns 'true' if SQE is allowed, 'false' otherwise.
6358 */
6359static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6360 struct io_kiocb *req,
6361 unsigned int sqe_flags)
6362{
6363 if (!ctx->restricted)
6364 return true;
6365
6366 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6367 return false;
6368
6369 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6370 ctx->restrictions.sqe_flags_required)
6371 return false;
6372
6373 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6374 ctx->restrictions.sqe_flags_required))
6375 return false;
6376
6377 return true;
6378}
6379
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006380#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6381 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6382 IOSQE_BUFFER_SELECT)
6383
6384static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6385 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006386 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006387{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006388 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006389 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006390
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006391 req->opcode = READ_ONCE(sqe->opcode);
6392 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006393 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006394 req->file = NULL;
6395 req->ctx = ctx;
6396 req->flags = 0;
6397 /* one is dropped after submission, the other at completion */
6398 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006399 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006400 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006401
6402 if (unlikely(req->opcode >= IORING_OP_LAST))
6403 return -EINVAL;
6404
Jens Axboe9d8426a2020-06-16 18:42:49 -06006405 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6406 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006407
6408 sqe_flags = READ_ONCE(sqe->flags);
6409 /* enforce forwards compatibility on users */
6410 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6411 return -EINVAL;
6412
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006413 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6414 return -EACCES;
6415
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006416 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6417 !io_op_defs[req->opcode].buffer_select)
6418 return -EOPNOTSUPP;
6419
6420 id = READ_ONCE(sqe->personality);
6421 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006422 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006423 req->work.creds = idr_find(&ctx->personality_idr, id);
6424 if (unlikely(!req->work.creds))
6425 return -EINVAL;
6426 get_cred(req->work.creds);
6427 }
6428
6429 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006430 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006431
Jens Axboe63ff8222020-05-07 14:56:15 -06006432 if (!io_op_defs[req->opcode].needs_file)
6433 return 0;
6434
6435 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006436}
6437
Jens Axboe0f212202020-09-13 13:09:39 -06006438static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006439{
Jens Axboeac8691c2020-06-01 08:30:41 -06006440 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006441 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006442 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006443
Jens Axboec4a2ed72019-11-21 21:01:26 -07006444 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006445 if (test_bit(0, &ctx->sq_check_overflow)) {
6446 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006447 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006448 return -EBUSY;
6449 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006450
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006451 /* make sure SQ entry isn't read before tail */
6452 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006453
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006454 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6455 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006456
Jens Axboefaf7b512020-10-07 12:48:53 -06006457 atomic_long_add(nr, &current->io_uring->req_issue);
6458 refcount_add(nr, &current->usage);
6459
Jens Axboe013538b2020-06-22 09:29:15 -06006460 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006461
6462 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006463 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006464 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006465 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006466
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006467 sqe = io_get_sqe(ctx);
6468 if (unlikely(!sqe)) {
6469 io_consume_sqe(ctx);
6470 break;
6471 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006472 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006473 if (unlikely(!req)) {
6474 if (!submitted)
6475 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006476 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006477 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006478
Jens Axboeac8691c2020-06-01 08:30:41 -06006479 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006480 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006481 /* will complete beyond this point, count as submitted */
6482 submitted++;
6483
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006484 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006485fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006486 io_put_req(req);
6487 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006488 break;
6489 }
6490
Jens Axboe354420f2020-01-08 18:55:15 -07006491 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006492 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006493 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006494 if (err)
6495 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006496 }
6497
Pavel Begunkov9466f432020-01-25 22:34:01 +03006498 if (unlikely(submitted != nr)) {
6499 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6500
6501 percpu_ref_put_many(&ctx->refs, nr - ref_used);
Jens Axboefaf7b512020-10-07 12:48:53 -06006502 atomic_long_sub(nr - ref_used, &current->io_uring->req_issue);
6503 put_task_struct_many(current, nr - ref_used);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006504 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006505 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006506 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006507 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006508
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006509 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6510 io_commit_sqring(ctx);
6511
Jens Axboe6c271ce2019-01-10 11:22:30 -07006512 return submitted;
6513}
6514
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006515static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6516{
6517 /* Tell userspace we may need a wakeup call */
6518 spin_lock_irq(&ctx->completion_lock);
6519 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6520 spin_unlock_irq(&ctx->completion_lock);
6521}
6522
6523static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6524{
6525 spin_lock_irq(&ctx->completion_lock);
6526 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6527 spin_unlock_irq(&ctx->completion_lock);
6528}
6529
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006530static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6531 int sync, void *key)
6532{
6533 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6534 int ret;
6535
6536 ret = autoremove_wake_function(wqe, mode, sync, key);
6537 if (ret) {
6538 unsigned long flags;
6539
6540 spin_lock_irqsave(&ctx->completion_lock, flags);
6541 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6542 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6543 }
6544 return ret;
6545}
6546
Jens Axboec8d1ba52020-09-14 11:07:26 -06006547enum sq_ret {
6548 SQT_IDLE = 1,
6549 SQT_SPIN = 2,
6550 SQT_DID_WORK = 4,
6551};
6552
6553static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006554 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006555{
6556 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006557 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006558 unsigned int to_submit;
6559 int ret = 0;
6560
6561again:
6562 if (!list_empty(&ctx->iopoll_list)) {
6563 unsigned nr_events = 0;
6564
6565 mutex_lock(&ctx->uring_lock);
6566 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6567 io_do_iopoll(ctx, &nr_events, 0);
6568 mutex_unlock(&ctx->uring_lock);
6569 }
6570
6571 to_submit = io_sqring_entries(ctx);
6572
6573 /*
6574 * If submit got -EBUSY, flag us as needing the application
6575 * to enter the kernel to reap and flush events.
6576 */
6577 if (!to_submit || ret == -EBUSY || need_resched()) {
6578 /*
6579 * Drop cur_mm before scheduling, we can't hold it for
6580 * long periods (or over schedule()). Do this before
6581 * adding ourselves to the waitqueue, as the unuse/drop
6582 * may sleep.
6583 */
6584 io_sq_thread_drop_mm();
6585
6586 /*
6587 * We're polling. If we're within the defined idle
6588 * period, then let us spin without work before going
6589 * to sleep. The exception is if we got EBUSY doing
6590 * more IO, we should wait for the application to
6591 * reap events and wake us up.
6592 */
6593 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6594 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6595 !percpu_ref_is_dying(&ctx->refs)))
6596 return SQT_SPIN;
6597
Jens Axboe534ca6d2020-09-02 13:52:19 -06006598 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006599 TASK_INTERRUPTIBLE);
6600
6601 /*
6602 * While doing polled IO, before going to sleep, we need
6603 * to check if there are new reqs added to iopoll_list,
6604 * it is because reqs may have been punted to io worker
6605 * and will be added to iopoll_list later, hence check
6606 * the iopoll_list again.
6607 */
6608 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6609 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006610 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006611 goto again;
6612 }
6613
Jens Axboec8d1ba52020-09-14 11:07:26 -06006614 to_submit = io_sqring_entries(ctx);
6615 if (!to_submit || ret == -EBUSY)
6616 return SQT_IDLE;
6617 }
6618
Jens Axboe534ca6d2020-09-02 13:52:19 -06006619 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006620 io_ring_clear_wakeup_flag(ctx);
6621
Jens Axboee95eee22020-09-08 09:11:32 -06006622 /* if we're handling multiple rings, cap submit size for fairness */
6623 if (cap_entries && to_submit > 8)
6624 to_submit = 8;
6625
Jens Axboec8d1ba52020-09-14 11:07:26 -06006626 mutex_lock(&ctx->uring_lock);
6627 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6628 ret = io_submit_sqes(ctx, to_submit);
6629 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006630
6631 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6632 wake_up(&ctx->sqo_sq_wait);
6633
Jens Axboec8d1ba52020-09-14 11:07:26 -06006634 return SQT_DID_WORK;
6635}
6636
Jens Axboe69fb2132020-09-14 11:16:23 -06006637static void io_sqd_init_new(struct io_sq_data *sqd)
6638{
6639 struct io_ring_ctx *ctx;
6640
6641 while (!list_empty(&sqd->ctx_new_list)) {
6642 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6643 init_wait(&ctx->sqo_wait_entry);
6644 ctx->sqo_wait_entry.func = io_sq_wake_function;
6645 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6646 complete(&ctx->sq_thread_comp);
6647 }
6648}
6649
Jens Axboe6c271ce2019-01-10 11:22:30 -07006650static int io_sq_thread(void *data)
6651{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006652 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006653 const struct cred *old_cred = NULL;
6654 struct io_sq_data *sqd = data;
6655 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006656 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006657
Jens Axboec8d1ba52020-09-14 11:07:26 -06006658 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006659 while (!kthread_should_stop()) {
6660 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006661 bool cap_entries;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006662
Jens Axboe69fb2132020-09-14 11:16:23 -06006663 /*
6664 * Any changes to the sqd lists are synchronized through the
6665 * kthread parking. This synchronizes the thread vs users,
6666 * the users are synchronized on the sqd->ctx_lock.
6667 */
6668 if (kthread_should_park())
6669 kthread_parkme();
6670
6671 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6672 io_sqd_init_new(sqd);
6673
Jens Axboee95eee22020-09-08 09:11:32 -06006674 cap_entries = !list_is_singular(&sqd->ctx_list);
6675
Jens Axboe69fb2132020-09-14 11:16:23 -06006676 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6677 if (current->cred != ctx->creds) {
6678 if (old_cred)
6679 revert_creds(old_cred);
6680 old_cred = override_creds(ctx->creds);
6681 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006682 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe69fb2132020-09-14 11:16:23 -06006683
Jens Axboee95eee22020-09-08 09:11:32 -06006684 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006685
6686 io_sq_thread_drop_mm();
6687 }
6688
6689 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006690 io_run_task_work();
6691 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006692 } else if (ret == SQT_IDLE) {
6693 if (kthread_should_park())
6694 continue;
6695 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6696 io_ring_set_wakeup_flag(ctx);
6697 schedule();
6698 start_jiffies = jiffies;
6699 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6700 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006701 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006702 }
6703
Jens Axboe4c6e2772020-07-01 11:29:10 -06006704 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006705
Dennis Zhou91d8f512020-09-16 13:41:05 -07006706 if (cur_css)
6707 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006708 if (old_cred)
6709 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006710
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006711 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006712
Jens Axboe6c271ce2019-01-10 11:22:30 -07006713 return 0;
6714}
6715
Jens Axboebda52162019-09-24 13:47:15 -06006716struct io_wait_queue {
6717 struct wait_queue_entry wq;
6718 struct io_ring_ctx *ctx;
6719 unsigned to_wait;
6720 unsigned nr_timeouts;
6721};
6722
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006723static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006724{
6725 struct io_ring_ctx *ctx = iowq->ctx;
6726
6727 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006728 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006729 * started waiting. For timeouts, we always want to return to userspace,
6730 * regardless of event count.
6731 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006732 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006733 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6734}
6735
6736static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6737 int wake_flags, void *key)
6738{
6739 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6740 wq);
6741
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006742 /* use noflush == true, as we can't safely rely on locking context */
6743 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006744 return -1;
6745
6746 return autoremove_wake_function(curr, mode, wake_flags, key);
6747}
6748
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006749static int io_run_task_work_sig(void)
6750{
6751 if (io_run_task_work())
6752 return 1;
6753 if (!signal_pending(current))
6754 return 0;
6755 if (current->jobctl & JOBCTL_TASK_WORK) {
6756 spin_lock_irq(&current->sighand->siglock);
6757 current->jobctl &= ~JOBCTL_TASK_WORK;
6758 recalc_sigpending();
6759 spin_unlock_irq(&current->sighand->siglock);
6760 return 1;
6761 }
6762 return -EINTR;
6763}
6764
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765/*
6766 * Wait until events become available, if we don't already have some. The
6767 * application must reap them itself, as they reside on the shared cq ring.
6768 */
6769static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6770 const sigset_t __user *sig, size_t sigsz)
6771{
Jens Axboebda52162019-09-24 13:47:15 -06006772 struct io_wait_queue iowq = {
6773 .wq = {
6774 .private = current,
6775 .func = io_wake_function,
6776 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6777 },
6778 .ctx = ctx,
6779 .to_wait = min_events,
6780 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006781 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006782 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006783
Jens Axboeb41e9852020-02-17 09:52:41 -07006784 do {
6785 if (io_cqring_events(ctx, false) >= min_events)
6786 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006787 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006788 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006789 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006790
6791 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006792#ifdef CONFIG_COMPAT
6793 if (in_compat_syscall())
6794 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006795 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006796 else
6797#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006798 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006799
Jens Axboe2b188cc2019-01-07 10:46:33 -07006800 if (ret)
6801 return ret;
6802 }
6803
Jens Axboebda52162019-09-24 13:47:15 -06006804 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006805 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006806 do {
6807 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6808 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006809 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006810 ret = io_run_task_work_sig();
6811 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06006812 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006813 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06006814 break;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006815 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006816 break;
6817 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006818 } while (1);
6819 finish_wait(&ctx->wait, &iowq.wq);
6820
Jens Axboeb7db41c2020-07-04 08:55:50 -06006821 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006822
Hristo Venev75b28af2019-08-26 17:23:46 +00006823 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006824}
6825
Jens Axboe6b063142019-01-10 22:13:58 -07006826static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6827{
6828#if defined(CONFIG_UNIX)
6829 if (ctx->ring_sock) {
6830 struct sock *sock = ctx->ring_sock->sk;
6831 struct sk_buff *skb;
6832
6833 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6834 kfree_skb(skb);
6835 }
6836#else
6837 int i;
6838
Jens Axboe65e19f52019-10-26 07:20:21 -06006839 for (i = 0; i < ctx->nr_user_files; i++) {
6840 struct file *file;
6841
6842 file = io_file_from_index(ctx, i);
6843 if (file)
6844 fput(file);
6845 }
Jens Axboe6b063142019-01-10 22:13:58 -07006846#endif
6847}
6848
Jens Axboe05f3fb32019-12-09 11:22:50 -07006849static void io_file_ref_kill(struct percpu_ref *ref)
6850{
6851 struct fixed_file_data *data;
6852
6853 data = container_of(ref, struct fixed_file_data, refs);
6854 complete(&data->done);
6855}
6856
Jens Axboe6b063142019-01-10 22:13:58 -07006857static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6858{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006859 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006860 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006861 unsigned nr_tables, i;
6862
Jens Axboe05f3fb32019-12-09 11:22:50 -07006863 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006864 return -ENXIO;
6865
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006866 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006867 if (!list_empty(&data->ref_list))
6868 ref_node = list_first_entry(&data->ref_list,
6869 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006870 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006871 if (ref_node)
6872 percpu_ref_kill(&ref_node->refs);
6873
6874 percpu_ref_kill(&data->refs);
6875
6876 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006877 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006878 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006879
Jens Axboe6b063142019-01-10 22:13:58 -07006880 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006881 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6882 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006883 kfree(data->table[i].files);
6884 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006885 percpu_ref_exit(&data->refs);
6886 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006887 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006888 ctx->nr_user_files = 0;
6889 return 0;
6890}
6891
Jens Axboe534ca6d2020-09-02 13:52:19 -06006892static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006893{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006894 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006895 /*
6896 * The park is a bit of a work-around, without it we get
6897 * warning spews on shutdown with SQPOLL set and affinity
6898 * set to a single CPU.
6899 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06006900 if (sqd->thread) {
6901 kthread_park(sqd->thread);
6902 kthread_stop(sqd->thread);
6903 }
6904
6905 kfree(sqd);
6906 }
6907}
6908
Jens Axboeaa061652020-09-02 14:50:27 -06006909static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
6910{
6911 struct io_ring_ctx *ctx_attach;
6912 struct io_sq_data *sqd;
6913 struct fd f;
6914
6915 f = fdget(p->wq_fd);
6916 if (!f.file)
6917 return ERR_PTR(-ENXIO);
6918 if (f.file->f_op != &io_uring_fops) {
6919 fdput(f);
6920 return ERR_PTR(-EINVAL);
6921 }
6922
6923 ctx_attach = f.file->private_data;
6924 sqd = ctx_attach->sq_data;
6925 if (!sqd) {
6926 fdput(f);
6927 return ERR_PTR(-EINVAL);
6928 }
6929
6930 refcount_inc(&sqd->refs);
6931 fdput(f);
6932 return sqd;
6933}
6934
Jens Axboe534ca6d2020-09-02 13:52:19 -06006935static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
6936{
6937 struct io_sq_data *sqd;
6938
Jens Axboeaa061652020-09-02 14:50:27 -06006939 if (p->flags & IORING_SETUP_ATTACH_WQ)
6940 return io_attach_sq_data(p);
6941
Jens Axboe534ca6d2020-09-02 13:52:19 -06006942 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
6943 if (!sqd)
6944 return ERR_PTR(-ENOMEM);
6945
6946 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06006947 INIT_LIST_HEAD(&sqd->ctx_list);
6948 INIT_LIST_HEAD(&sqd->ctx_new_list);
6949 mutex_init(&sqd->ctx_lock);
6950 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06006951 init_waitqueue_head(&sqd->wait);
6952 return sqd;
6953}
6954
Jens Axboe69fb2132020-09-14 11:16:23 -06006955static void io_sq_thread_unpark(struct io_sq_data *sqd)
6956 __releases(&sqd->lock)
6957{
6958 if (!sqd->thread)
6959 return;
6960 kthread_unpark(sqd->thread);
6961 mutex_unlock(&sqd->lock);
6962}
6963
6964static void io_sq_thread_park(struct io_sq_data *sqd)
6965 __acquires(&sqd->lock)
6966{
6967 if (!sqd->thread)
6968 return;
6969 mutex_lock(&sqd->lock);
6970 kthread_park(sqd->thread);
6971}
6972
Jens Axboe534ca6d2020-09-02 13:52:19 -06006973static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6974{
6975 struct io_sq_data *sqd = ctx->sq_data;
6976
6977 if (sqd) {
6978 if (sqd->thread) {
6979 /*
6980 * We may arrive here from the error branch in
6981 * io_sq_offload_create() where the kthread is created
6982 * without being waked up, thus wake it up now to make
6983 * sure the wait will complete.
6984 */
6985 wake_up_process(sqd->thread);
6986 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06006987
6988 io_sq_thread_park(sqd);
6989 }
6990
6991 mutex_lock(&sqd->ctx_lock);
6992 list_del(&ctx->sqd_list);
6993 mutex_unlock(&sqd->ctx_lock);
6994
6995 if (sqd->thread) {
6996 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
6997 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06006998 }
6999
7000 io_put_sq_data(sqd);
7001 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007002 }
7003}
7004
Jens Axboe6b063142019-01-10 22:13:58 -07007005static void io_finish_async(struct io_ring_ctx *ctx)
7006{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007007 io_sq_thread_stop(ctx);
7008
Jens Axboe561fb042019-10-24 07:25:42 -06007009 if (ctx->io_wq) {
7010 io_wq_destroy(ctx->io_wq);
7011 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007012 }
7013}
7014
7015#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007016/*
7017 * Ensure the UNIX gc is aware of our file set, so we are certain that
7018 * the io_uring can be safely unregistered on process exit, even if we have
7019 * loops in the file referencing.
7020 */
7021static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7022{
7023 struct sock *sk = ctx->ring_sock->sk;
7024 struct scm_fp_list *fpl;
7025 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007026 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007027
Jens Axboe6b063142019-01-10 22:13:58 -07007028 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7029 if (!fpl)
7030 return -ENOMEM;
7031
7032 skb = alloc_skb(0, GFP_KERNEL);
7033 if (!skb) {
7034 kfree(fpl);
7035 return -ENOMEM;
7036 }
7037
7038 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007039
Jens Axboe08a45172019-10-03 08:11:03 -06007040 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007041 fpl->user = get_uid(ctx->user);
7042 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007043 struct file *file = io_file_from_index(ctx, i + offset);
7044
7045 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007046 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007047 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007048 unix_inflight(fpl->user, fpl->fp[nr_files]);
7049 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007050 }
7051
Jens Axboe08a45172019-10-03 08:11:03 -06007052 if (nr_files) {
7053 fpl->max = SCM_MAX_FD;
7054 fpl->count = nr_files;
7055 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007056 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007057 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7058 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007059
Jens Axboe08a45172019-10-03 08:11:03 -06007060 for (i = 0; i < nr_files; i++)
7061 fput(fpl->fp[i]);
7062 } else {
7063 kfree_skb(skb);
7064 kfree(fpl);
7065 }
Jens Axboe6b063142019-01-10 22:13:58 -07007066
7067 return 0;
7068}
7069
7070/*
7071 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7072 * causes regular reference counting to break down. We rely on the UNIX
7073 * garbage collection to take care of this problem for us.
7074 */
7075static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7076{
7077 unsigned left, total;
7078 int ret = 0;
7079
7080 total = 0;
7081 left = ctx->nr_user_files;
7082 while (left) {
7083 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007084
7085 ret = __io_sqe_files_scm(ctx, this_files, total);
7086 if (ret)
7087 break;
7088 left -= this_files;
7089 total += this_files;
7090 }
7091
7092 if (!ret)
7093 return 0;
7094
7095 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007096 struct file *file = io_file_from_index(ctx, total);
7097
7098 if (file)
7099 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007100 total++;
7101 }
7102
7103 return ret;
7104}
7105#else
7106static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7107{
7108 return 0;
7109}
7110#endif
7111
Jens Axboe65e19f52019-10-26 07:20:21 -06007112static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7113 unsigned nr_files)
7114{
7115 int i;
7116
7117 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007118 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007119 unsigned this_files;
7120
7121 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7122 table->files = kcalloc(this_files, sizeof(struct file *),
7123 GFP_KERNEL);
7124 if (!table->files)
7125 break;
7126 nr_files -= this_files;
7127 }
7128
7129 if (i == nr_tables)
7130 return 0;
7131
7132 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007133 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007134 kfree(table->files);
7135 }
7136 return 1;
7137}
7138
Jens Axboe05f3fb32019-12-09 11:22:50 -07007139static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007140{
7141#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007142 struct sock *sock = ctx->ring_sock->sk;
7143 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7144 struct sk_buff *skb;
7145 int i;
7146
7147 __skb_queue_head_init(&list);
7148
7149 /*
7150 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7151 * remove this entry and rearrange the file array.
7152 */
7153 skb = skb_dequeue(head);
7154 while (skb) {
7155 struct scm_fp_list *fp;
7156
7157 fp = UNIXCB(skb).fp;
7158 for (i = 0; i < fp->count; i++) {
7159 int left;
7160
7161 if (fp->fp[i] != file)
7162 continue;
7163
7164 unix_notinflight(fp->user, fp->fp[i]);
7165 left = fp->count - 1 - i;
7166 if (left) {
7167 memmove(&fp->fp[i], &fp->fp[i + 1],
7168 left * sizeof(struct file *));
7169 }
7170 fp->count--;
7171 if (!fp->count) {
7172 kfree_skb(skb);
7173 skb = NULL;
7174 } else {
7175 __skb_queue_tail(&list, skb);
7176 }
7177 fput(file);
7178 file = NULL;
7179 break;
7180 }
7181
7182 if (!file)
7183 break;
7184
7185 __skb_queue_tail(&list, skb);
7186
7187 skb = skb_dequeue(head);
7188 }
7189
7190 if (skb_peek(&list)) {
7191 spin_lock_irq(&head->lock);
7192 while ((skb = __skb_dequeue(&list)) != NULL)
7193 __skb_queue_tail(head, skb);
7194 spin_unlock_irq(&head->lock);
7195 }
7196#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007197 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007198#endif
7199}
7200
Jens Axboe05f3fb32019-12-09 11:22:50 -07007201struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007202 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007203 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007204};
7205
Jens Axboe4a38aed22020-05-14 17:21:15 -06007206static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007207{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007208 struct fixed_file_data *file_data = ref_node->file_data;
7209 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007210 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007211
7212 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007213 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007214 io_ring_file_put(ctx, pfile->file);
7215 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007216 }
7217
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007218 spin_lock(&file_data->lock);
7219 list_del(&ref_node->node);
7220 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007221
Xiaoguang Wang05589552020-03-31 14:05:18 +08007222 percpu_ref_exit(&ref_node->refs);
7223 kfree(ref_node);
7224 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007225}
7226
Jens Axboe4a38aed22020-05-14 17:21:15 -06007227static void io_file_put_work(struct work_struct *work)
7228{
7229 struct io_ring_ctx *ctx;
7230 struct llist_node *node;
7231
7232 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7233 node = llist_del_all(&ctx->file_put_llist);
7234
7235 while (node) {
7236 struct fixed_file_ref_node *ref_node;
7237 struct llist_node *next = node->next;
7238
7239 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7240 __io_file_put_work(ref_node);
7241 node = next;
7242 }
7243}
7244
Jens Axboe05f3fb32019-12-09 11:22:50 -07007245static void io_file_data_ref_zero(struct percpu_ref *ref)
7246{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007247 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007248 struct io_ring_ctx *ctx;
7249 bool first_add;
7250 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007251
Xiaoguang Wang05589552020-03-31 14:05:18 +08007252 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007253 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007254
Jens Axboe4a38aed22020-05-14 17:21:15 -06007255 if (percpu_ref_is_dying(&ctx->file_data->refs))
7256 delay = 0;
7257
7258 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7259 if (!delay)
7260 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7261 else if (first_add)
7262 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007263}
7264
7265static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7266 struct io_ring_ctx *ctx)
7267{
7268 struct fixed_file_ref_node *ref_node;
7269
7270 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7271 if (!ref_node)
7272 return ERR_PTR(-ENOMEM);
7273
7274 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7275 0, GFP_KERNEL)) {
7276 kfree(ref_node);
7277 return ERR_PTR(-ENOMEM);
7278 }
7279 INIT_LIST_HEAD(&ref_node->node);
7280 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007281 ref_node->file_data = ctx->file_data;
7282 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007283}
7284
7285static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7286{
7287 percpu_ref_exit(&ref_node->refs);
7288 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007289}
7290
7291static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7292 unsigned nr_args)
7293{
7294 __s32 __user *fds = (__s32 __user *) arg;
7295 unsigned nr_tables;
7296 struct file *file;
7297 int fd, ret = 0;
7298 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007299 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007300
7301 if (ctx->file_data)
7302 return -EBUSY;
7303 if (!nr_args)
7304 return -EINVAL;
7305 if (nr_args > IORING_MAX_FIXED_FILES)
7306 return -EMFILE;
7307
7308 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7309 if (!ctx->file_data)
7310 return -ENOMEM;
7311 ctx->file_data->ctx = ctx;
7312 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007313 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007314 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007315
7316 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7317 ctx->file_data->table = kcalloc(nr_tables,
7318 sizeof(struct fixed_file_table),
7319 GFP_KERNEL);
7320 if (!ctx->file_data->table) {
7321 kfree(ctx->file_data);
7322 ctx->file_data = NULL;
7323 return -ENOMEM;
7324 }
7325
Xiaoguang Wang05589552020-03-31 14:05:18 +08007326 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007327 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7328 kfree(ctx->file_data->table);
7329 kfree(ctx->file_data);
7330 ctx->file_data = NULL;
7331 return -ENOMEM;
7332 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007333
7334 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7335 percpu_ref_exit(&ctx->file_data->refs);
7336 kfree(ctx->file_data->table);
7337 kfree(ctx->file_data);
7338 ctx->file_data = NULL;
7339 return -ENOMEM;
7340 }
7341
7342 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7343 struct fixed_file_table *table;
7344 unsigned index;
7345
7346 ret = -EFAULT;
7347 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7348 break;
7349 /* allow sparse sets */
7350 if (fd == -1) {
7351 ret = 0;
7352 continue;
7353 }
7354
7355 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7356 index = i & IORING_FILE_TABLE_MASK;
7357 file = fget(fd);
7358
7359 ret = -EBADF;
7360 if (!file)
7361 break;
7362
7363 /*
7364 * Don't allow io_uring instances to be registered. If UNIX
7365 * isn't enabled, then this causes a reference cycle and this
7366 * instance can never get freed. If UNIX is enabled we'll
7367 * handle it just fine, but there's still no point in allowing
7368 * a ring fd as it doesn't support regular read/write anyway.
7369 */
7370 if (file->f_op == &io_uring_fops) {
7371 fput(file);
7372 break;
7373 }
7374 ret = 0;
7375 table->files[index] = file;
7376 }
7377
7378 if (ret) {
7379 for (i = 0; i < ctx->nr_user_files; i++) {
7380 file = io_file_from_index(ctx, i);
7381 if (file)
7382 fput(file);
7383 }
7384 for (i = 0; i < nr_tables; i++)
7385 kfree(ctx->file_data->table[i].files);
7386
Yang Yingliang667e57d2020-07-10 14:14:20 +00007387 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007388 kfree(ctx->file_data->table);
7389 kfree(ctx->file_data);
7390 ctx->file_data = NULL;
7391 ctx->nr_user_files = 0;
7392 return ret;
7393 }
7394
7395 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007396 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007397 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007398 return ret;
7399 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007400
Xiaoguang Wang05589552020-03-31 14:05:18 +08007401 ref_node = alloc_fixed_file_ref_node(ctx);
7402 if (IS_ERR(ref_node)) {
7403 io_sqe_files_unregister(ctx);
7404 return PTR_ERR(ref_node);
7405 }
7406
7407 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007408 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007409 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007410 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007411 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007412 return ret;
7413}
7414
Jens Axboec3a31e62019-10-03 13:59:56 -06007415static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7416 int index)
7417{
7418#if defined(CONFIG_UNIX)
7419 struct sock *sock = ctx->ring_sock->sk;
7420 struct sk_buff_head *head = &sock->sk_receive_queue;
7421 struct sk_buff *skb;
7422
7423 /*
7424 * See if we can merge this file into an existing skb SCM_RIGHTS
7425 * file set. If there's no room, fall back to allocating a new skb
7426 * and filling it in.
7427 */
7428 spin_lock_irq(&head->lock);
7429 skb = skb_peek(head);
7430 if (skb) {
7431 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7432
7433 if (fpl->count < SCM_MAX_FD) {
7434 __skb_unlink(skb, head);
7435 spin_unlock_irq(&head->lock);
7436 fpl->fp[fpl->count] = get_file(file);
7437 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7438 fpl->count++;
7439 spin_lock_irq(&head->lock);
7440 __skb_queue_head(head, skb);
7441 } else {
7442 skb = NULL;
7443 }
7444 }
7445 spin_unlock_irq(&head->lock);
7446
7447 if (skb) {
7448 fput(file);
7449 return 0;
7450 }
7451
7452 return __io_sqe_files_scm(ctx, 1, index);
7453#else
7454 return 0;
7455#endif
7456}
7457
Hillf Dantona5318d32020-03-23 17:47:15 +08007458static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007459 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007460{
Hillf Dantona5318d32020-03-23 17:47:15 +08007461 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007462 struct percpu_ref *refs = data->cur_refs;
7463 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007464
Jens Axboe05f3fb32019-12-09 11:22:50 -07007465 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007466 if (!pfile)
7467 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007468
Xiaoguang Wang05589552020-03-31 14:05:18 +08007469 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007470 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007471 list_add(&pfile->list, &ref_node->file_list);
7472
Hillf Dantona5318d32020-03-23 17:47:15 +08007473 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007474}
7475
7476static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7477 struct io_uring_files_update *up,
7478 unsigned nr_args)
7479{
7480 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007481 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007482 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007483 __s32 __user *fds;
7484 int fd, i, err;
7485 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007486 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007487
Jens Axboe05f3fb32019-12-09 11:22:50 -07007488 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007489 return -EOVERFLOW;
7490 if (done > ctx->nr_user_files)
7491 return -EINVAL;
7492
Xiaoguang Wang05589552020-03-31 14:05:18 +08007493 ref_node = alloc_fixed_file_ref_node(ctx);
7494 if (IS_ERR(ref_node))
7495 return PTR_ERR(ref_node);
7496
Jens Axboec3a31e62019-10-03 13:59:56 -06007497 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007498 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007499 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007500 struct fixed_file_table *table;
7501 unsigned index;
7502
Jens Axboec3a31e62019-10-03 13:59:56 -06007503 err = 0;
7504 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7505 err = -EFAULT;
7506 break;
7507 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007508 i = array_index_nospec(up->offset, ctx->nr_user_files);
7509 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007510 index = i & IORING_FILE_TABLE_MASK;
7511 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007512 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007513 err = io_queue_file_removal(data, file);
7514 if (err)
7515 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007516 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007517 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007518 }
7519 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007520 file = fget(fd);
7521 if (!file) {
7522 err = -EBADF;
7523 break;
7524 }
7525 /*
7526 * Don't allow io_uring instances to be registered. If
7527 * UNIX isn't enabled, then this causes a reference
7528 * cycle and this instance can never get freed. If UNIX
7529 * is enabled we'll handle it just fine, but there's
7530 * still no point in allowing a ring fd as it doesn't
7531 * support regular read/write anyway.
7532 */
7533 if (file->f_op == &io_uring_fops) {
7534 fput(file);
7535 err = -EBADF;
7536 break;
7537 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007538 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007539 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007540 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007541 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007542 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007543 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007544 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007545 }
7546 nr_args--;
7547 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007548 up->offset++;
7549 }
7550
Xiaoguang Wang05589552020-03-31 14:05:18 +08007551 if (needs_switch) {
7552 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007553 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007554 list_add(&ref_node->node, &data->ref_list);
7555 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007556 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007557 percpu_ref_get(&ctx->file_data->refs);
7558 } else
7559 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007560
7561 return done ? done : err;
7562}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007563
Jens Axboe05f3fb32019-12-09 11:22:50 -07007564static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7565 unsigned nr_args)
7566{
7567 struct io_uring_files_update up;
7568
7569 if (!ctx->file_data)
7570 return -ENXIO;
7571 if (!nr_args)
7572 return -EINVAL;
7573 if (copy_from_user(&up, arg, sizeof(up)))
7574 return -EFAULT;
7575 if (up.resv)
7576 return -EINVAL;
7577
7578 return __io_sqe_files_update(ctx, &up, nr_args);
7579}
Jens Axboec3a31e62019-10-03 13:59:56 -06007580
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007581static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007582{
7583 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7584
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007585 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007586 io_put_req(req);
7587}
7588
Pavel Begunkov24369c22020-01-28 03:15:48 +03007589static int io_init_wq_offload(struct io_ring_ctx *ctx,
7590 struct io_uring_params *p)
7591{
7592 struct io_wq_data data;
7593 struct fd f;
7594 struct io_ring_ctx *ctx_attach;
7595 unsigned int concurrency;
7596 int ret = 0;
7597
7598 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007599 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007600 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007601
7602 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7603 /* Do QD, or 4 * CPUS, whatever is smallest */
7604 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7605
7606 ctx->io_wq = io_wq_create(concurrency, &data);
7607 if (IS_ERR(ctx->io_wq)) {
7608 ret = PTR_ERR(ctx->io_wq);
7609 ctx->io_wq = NULL;
7610 }
7611 return ret;
7612 }
7613
7614 f = fdget(p->wq_fd);
7615 if (!f.file)
7616 return -EBADF;
7617
7618 if (f.file->f_op != &io_uring_fops) {
7619 ret = -EINVAL;
7620 goto out_fput;
7621 }
7622
7623 ctx_attach = f.file->private_data;
7624 /* @io_wq is protected by holding the fd */
7625 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7626 ret = -EINVAL;
7627 goto out_fput;
7628 }
7629
7630 ctx->io_wq = ctx_attach->io_wq;
7631out_fput:
7632 fdput(f);
7633 return ret;
7634}
7635
Jens Axboe0f212202020-09-13 13:09:39 -06007636static int io_uring_alloc_task_context(struct task_struct *task)
7637{
7638 struct io_uring_task *tctx;
7639
7640 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7641 if (unlikely(!tctx))
7642 return -ENOMEM;
7643
7644 xa_init(&tctx->xa);
7645 init_waitqueue_head(&tctx->wait);
7646 tctx->last = NULL;
7647 tctx->in_idle = 0;
7648 atomic_long_set(&tctx->req_issue, 0);
7649 atomic_long_set(&tctx->req_complete, 0);
7650 task->io_uring = tctx;
7651 return 0;
7652}
7653
7654void __io_uring_free(struct task_struct *tsk)
7655{
7656 struct io_uring_task *tctx = tsk->io_uring;
7657
7658 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe0f212202020-09-13 13:09:39 -06007659 kfree(tctx);
7660 tsk->io_uring = NULL;
7661}
7662
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007663static int io_sq_offload_create(struct io_ring_ctx *ctx,
7664 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007665{
7666 int ret;
7667
Jens Axboe6c271ce2019-01-10 11:22:30 -07007668 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007669 struct io_sq_data *sqd;
7670
Jens Axboe3ec482d2019-04-08 10:51:01 -06007671 ret = -EPERM;
7672 if (!capable(CAP_SYS_ADMIN))
7673 goto err;
7674
Jens Axboe534ca6d2020-09-02 13:52:19 -06007675 sqd = io_get_sq_data(p);
7676 if (IS_ERR(sqd)) {
7677 ret = PTR_ERR(sqd);
7678 goto err;
7679 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007680
Jens Axboe534ca6d2020-09-02 13:52:19 -06007681 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007682 io_sq_thread_park(sqd);
7683 mutex_lock(&sqd->ctx_lock);
7684 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7685 mutex_unlock(&sqd->ctx_lock);
7686 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007687
Jens Axboe917257d2019-04-13 09:28:55 -06007688 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7689 if (!ctx->sq_thread_idle)
7690 ctx->sq_thread_idle = HZ;
7691
Jens Axboeaa061652020-09-02 14:50:27 -06007692 if (sqd->thread)
7693 goto done;
7694
Jens Axboe6c271ce2019-01-10 11:22:30 -07007695 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007696 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007697
Jens Axboe917257d2019-04-13 09:28:55 -06007698 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007699 if (cpu >= nr_cpu_ids)
7700 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007701 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007702 goto err;
7703
Jens Axboe69fb2132020-09-14 11:16:23 -06007704 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007705 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007706 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007707 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007708 "io_uring-sq");
7709 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007710 if (IS_ERR(sqd->thread)) {
7711 ret = PTR_ERR(sqd->thread);
7712 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007713 goto err;
7714 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007715 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007716 if (ret)
7717 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007718 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7719 /* Can't have SQ_AFF without SQPOLL */
7720 ret = -EINVAL;
7721 goto err;
7722 }
7723
Jens Axboeaa061652020-09-02 14:50:27 -06007724done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007725 ret = io_init_wq_offload(ctx, p);
7726 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007727 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007728
7729 return 0;
7730err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007731 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007732 return ret;
7733}
7734
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007735static void io_sq_offload_start(struct io_ring_ctx *ctx)
7736{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007737 struct io_sq_data *sqd = ctx->sq_data;
7738
7739 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7740 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007741}
7742
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007743static inline void __io_unaccount_mem(struct user_struct *user,
7744 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007745{
7746 atomic_long_sub(nr_pages, &user->locked_vm);
7747}
7748
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007749static inline int __io_account_mem(struct user_struct *user,
7750 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007751{
7752 unsigned long page_limit, cur_pages, new_pages;
7753
7754 /* Don't allow more pages than we can safely lock */
7755 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7756
7757 do {
7758 cur_pages = atomic_long_read(&user->locked_vm);
7759 new_pages = cur_pages + nr_pages;
7760 if (new_pages > page_limit)
7761 return -ENOMEM;
7762 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7763 new_pages) != cur_pages);
7764
7765 return 0;
7766}
7767
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007768static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7769 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007770{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007771 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007772 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007773
Jens Axboe2aede0e2020-09-14 10:45:53 -06007774 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007775 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007776 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007777 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007778 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007779 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007780}
7781
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007782static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7783 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007784{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007785 int ret;
7786
7787 if (ctx->limit_mem) {
7788 ret = __io_account_mem(ctx->user, nr_pages);
7789 if (ret)
7790 return ret;
7791 }
7792
Jens Axboe2aede0e2020-09-14 10:45:53 -06007793 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007794 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007795 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007796 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007797 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007798 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007799
7800 return 0;
7801}
7802
Jens Axboe2b188cc2019-01-07 10:46:33 -07007803static void io_mem_free(void *ptr)
7804{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007805 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007806
Mark Rutland52e04ef2019-04-30 17:30:21 +01007807 if (!ptr)
7808 return;
7809
7810 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007811 if (put_page_testzero(page))
7812 free_compound_page(page);
7813}
7814
7815static void *io_mem_alloc(size_t size)
7816{
7817 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7818 __GFP_NORETRY;
7819
7820 return (void *) __get_free_pages(gfp_flags, get_order(size));
7821}
7822
Hristo Venev75b28af2019-08-26 17:23:46 +00007823static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7824 size_t *sq_offset)
7825{
7826 struct io_rings *rings;
7827 size_t off, sq_array_size;
7828
7829 off = struct_size(rings, cqes, cq_entries);
7830 if (off == SIZE_MAX)
7831 return SIZE_MAX;
7832
7833#ifdef CONFIG_SMP
7834 off = ALIGN(off, SMP_CACHE_BYTES);
7835 if (off == 0)
7836 return SIZE_MAX;
7837#endif
7838
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007839 if (sq_offset)
7840 *sq_offset = off;
7841
Hristo Venev75b28af2019-08-26 17:23:46 +00007842 sq_array_size = array_size(sizeof(u32), sq_entries);
7843 if (sq_array_size == SIZE_MAX)
7844 return SIZE_MAX;
7845
7846 if (check_add_overflow(off, sq_array_size, &off))
7847 return SIZE_MAX;
7848
Hristo Venev75b28af2019-08-26 17:23:46 +00007849 return off;
7850}
7851
Jens Axboe2b188cc2019-01-07 10:46:33 -07007852static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7853{
Hristo Venev75b28af2019-08-26 17:23:46 +00007854 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007855
Hristo Venev75b28af2019-08-26 17:23:46 +00007856 pages = (size_t)1 << get_order(
7857 rings_size(sq_entries, cq_entries, NULL));
7858 pages += (size_t)1 << get_order(
7859 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007860
Hristo Venev75b28af2019-08-26 17:23:46 +00007861 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007862}
7863
Jens Axboeedafcce2019-01-09 09:16:05 -07007864static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7865{
7866 int i, j;
7867
7868 if (!ctx->user_bufs)
7869 return -ENXIO;
7870
7871 for (i = 0; i < ctx->nr_user_bufs; i++) {
7872 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7873
7874 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007875 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007876
Jens Axboede293932020-09-17 16:19:16 -06007877 if (imu->acct_pages)
7878 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007879 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007880 imu->nr_bvecs = 0;
7881 }
7882
7883 kfree(ctx->user_bufs);
7884 ctx->user_bufs = NULL;
7885 ctx->nr_user_bufs = 0;
7886 return 0;
7887}
7888
7889static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7890 void __user *arg, unsigned index)
7891{
7892 struct iovec __user *src;
7893
7894#ifdef CONFIG_COMPAT
7895 if (ctx->compat) {
7896 struct compat_iovec __user *ciovs;
7897 struct compat_iovec ciov;
7898
7899 ciovs = (struct compat_iovec __user *) arg;
7900 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7901 return -EFAULT;
7902
Jens Axboed55e5f52019-12-11 16:12:15 -07007903 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007904 dst->iov_len = ciov.iov_len;
7905 return 0;
7906 }
7907#endif
7908 src = (struct iovec __user *) arg;
7909 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7910 return -EFAULT;
7911 return 0;
7912}
7913
Jens Axboede293932020-09-17 16:19:16 -06007914/*
7915 * Not super efficient, but this is just a registration time. And we do cache
7916 * the last compound head, so generally we'll only do a full search if we don't
7917 * match that one.
7918 *
7919 * We check if the given compound head page has already been accounted, to
7920 * avoid double accounting it. This allows us to account the full size of the
7921 * page, not just the constituent pages of a huge page.
7922 */
7923static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
7924 int nr_pages, struct page *hpage)
7925{
7926 int i, j;
7927
7928 /* check current page array */
7929 for (i = 0; i < nr_pages; i++) {
7930 if (!PageCompound(pages[i]))
7931 continue;
7932 if (compound_head(pages[i]) == hpage)
7933 return true;
7934 }
7935
7936 /* check previously registered pages */
7937 for (i = 0; i < ctx->nr_user_bufs; i++) {
7938 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7939
7940 for (j = 0; j < imu->nr_bvecs; j++) {
7941 if (!PageCompound(imu->bvec[j].bv_page))
7942 continue;
7943 if (compound_head(imu->bvec[j].bv_page) == hpage)
7944 return true;
7945 }
7946 }
7947
7948 return false;
7949}
7950
7951static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
7952 int nr_pages, struct io_mapped_ubuf *imu,
7953 struct page **last_hpage)
7954{
7955 int i, ret;
7956
7957 for (i = 0; i < nr_pages; i++) {
7958 if (!PageCompound(pages[i])) {
7959 imu->acct_pages++;
7960 } else {
7961 struct page *hpage;
7962
7963 hpage = compound_head(pages[i]);
7964 if (hpage == *last_hpage)
7965 continue;
7966 *last_hpage = hpage;
7967 if (headpage_already_acct(ctx, pages, i, hpage))
7968 continue;
7969 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
7970 }
7971 }
7972
7973 if (!imu->acct_pages)
7974 return 0;
7975
7976 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
7977 if (ret)
7978 imu->acct_pages = 0;
7979 return ret;
7980}
7981
Jens Axboeedafcce2019-01-09 09:16:05 -07007982static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7983 unsigned nr_args)
7984{
7985 struct vm_area_struct **vmas = NULL;
7986 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06007987 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07007988 int i, j, got_pages = 0;
7989 int ret = -EINVAL;
7990
7991 if (ctx->user_bufs)
7992 return -EBUSY;
7993 if (!nr_args || nr_args > UIO_MAXIOV)
7994 return -EINVAL;
7995
7996 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7997 GFP_KERNEL);
7998 if (!ctx->user_bufs)
7999 return -ENOMEM;
8000
8001 for (i = 0; i < nr_args; i++) {
8002 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8003 unsigned long off, start, end, ubuf;
8004 int pret, nr_pages;
8005 struct iovec iov;
8006 size_t size;
8007
8008 ret = io_copy_iov(ctx, &iov, arg, i);
8009 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008010 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008011
8012 /*
8013 * Don't impose further limits on the size and buffer
8014 * constraints here, we'll -EINVAL later when IO is
8015 * submitted if they are wrong.
8016 */
8017 ret = -EFAULT;
8018 if (!iov.iov_base || !iov.iov_len)
8019 goto err;
8020
8021 /* arbitrary limit, but we need something */
8022 if (iov.iov_len > SZ_1G)
8023 goto err;
8024
8025 ubuf = (unsigned long) iov.iov_base;
8026 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8027 start = ubuf >> PAGE_SHIFT;
8028 nr_pages = end - start;
8029
Jens Axboeedafcce2019-01-09 09:16:05 -07008030 ret = 0;
8031 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008032 kvfree(vmas);
8033 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008034 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008035 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008036 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008037 sizeof(struct vm_area_struct *),
8038 GFP_KERNEL);
8039 if (!pages || !vmas) {
8040 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008041 goto err;
8042 }
8043 got_pages = nr_pages;
8044 }
8045
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008046 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008047 GFP_KERNEL);
8048 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008049 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008050 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008051
8052 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008053 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008054 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008055 FOLL_WRITE | FOLL_LONGTERM,
8056 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008057 if (pret == nr_pages) {
8058 /* don't support file backed memory */
8059 for (j = 0; j < nr_pages; j++) {
8060 struct vm_area_struct *vma = vmas[j];
8061
8062 if (vma->vm_file &&
8063 !is_file_hugepages(vma->vm_file)) {
8064 ret = -EOPNOTSUPP;
8065 break;
8066 }
8067 }
8068 } else {
8069 ret = pret < 0 ? pret : -EFAULT;
8070 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008071 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008072 if (ret) {
8073 /*
8074 * if we did partial map, or found file backed vmas,
8075 * release any pages we did get
8076 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008077 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008078 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008079 kvfree(imu->bvec);
8080 goto err;
8081 }
8082
8083 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8084 if (ret) {
8085 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008086 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008087 goto err;
8088 }
8089
8090 off = ubuf & ~PAGE_MASK;
8091 size = iov.iov_len;
8092 for (j = 0; j < nr_pages; j++) {
8093 size_t vec_len;
8094
8095 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8096 imu->bvec[j].bv_page = pages[j];
8097 imu->bvec[j].bv_len = vec_len;
8098 imu->bvec[j].bv_offset = off;
8099 off = 0;
8100 size -= vec_len;
8101 }
8102 /* store original address for later verification */
8103 imu->ubuf = ubuf;
8104 imu->len = iov.iov_len;
8105 imu->nr_bvecs = nr_pages;
8106
8107 ctx->nr_user_bufs++;
8108 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008109 kvfree(pages);
8110 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008111 return 0;
8112err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008113 kvfree(pages);
8114 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008115 io_sqe_buffer_unregister(ctx);
8116 return ret;
8117}
8118
Jens Axboe9b402842019-04-11 11:45:41 -06008119static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8120{
8121 __s32 __user *fds = arg;
8122 int fd;
8123
8124 if (ctx->cq_ev_fd)
8125 return -EBUSY;
8126
8127 if (copy_from_user(&fd, fds, sizeof(*fds)))
8128 return -EFAULT;
8129
8130 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8131 if (IS_ERR(ctx->cq_ev_fd)) {
8132 int ret = PTR_ERR(ctx->cq_ev_fd);
8133 ctx->cq_ev_fd = NULL;
8134 return ret;
8135 }
8136
8137 return 0;
8138}
8139
8140static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8141{
8142 if (ctx->cq_ev_fd) {
8143 eventfd_ctx_put(ctx->cq_ev_fd);
8144 ctx->cq_ev_fd = NULL;
8145 return 0;
8146 }
8147
8148 return -ENXIO;
8149}
8150
Jens Axboe5a2e7452020-02-23 16:23:11 -07008151static int __io_destroy_buffers(int id, void *p, void *data)
8152{
8153 struct io_ring_ctx *ctx = data;
8154 struct io_buffer *buf = p;
8155
Jens Axboe067524e2020-03-02 16:32:28 -07008156 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008157 return 0;
8158}
8159
8160static void io_destroy_buffers(struct io_ring_ctx *ctx)
8161{
8162 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8163 idr_destroy(&ctx->io_buffer_idr);
8164}
8165
Jens Axboe2b188cc2019-01-07 10:46:33 -07008166static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8167{
Jens Axboe6b063142019-01-10 22:13:58 -07008168 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008169 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008170
8171 if (ctx->sqo_task) {
8172 put_task_struct(ctx->sqo_task);
8173 ctx->sqo_task = NULL;
8174 mmdrop(ctx->mm_account);
8175 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008176 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008177
Dennis Zhou91d8f512020-09-16 13:41:05 -07008178#ifdef CONFIG_BLK_CGROUP
8179 if (ctx->sqo_blkcg_css)
8180 css_put(ctx->sqo_blkcg_css);
8181#endif
8182
Jens Axboe6b063142019-01-10 22:13:58 -07008183 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008184 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008185 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008186 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008187
Jens Axboe2b188cc2019-01-07 10:46:33 -07008188#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008189 if (ctx->ring_sock) {
8190 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008191 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008192 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008193#endif
8194
Hristo Venev75b28af2019-08-26 17:23:46 +00008195 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008196 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008197
8198 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008199 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008200 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008201 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008202 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008203 kfree(ctx);
8204}
8205
8206static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8207{
8208 struct io_ring_ctx *ctx = file->private_data;
8209 __poll_t mask = 0;
8210
8211 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008212 /*
8213 * synchronizes with barrier from wq_has_sleeper call in
8214 * io_commit_cqring
8215 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008216 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008217 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008218 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008219 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008220 mask |= EPOLLIN | EPOLLRDNORM;
8221
8222 return mask;
8223}
8224
8225static int io_uring_fasync(int fd, struct file *file, int on)
8226{
8227 struct io_ring_ctx *ctx = file->private_data;
8228
8229 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8230}
8231
Jens Axboe071698e2020-01-28 10:04:42 -07008232static int io_remove_personalities(int id, void *p, void *data)
8233{
8234 struct io_ring_ctx *ctx = data;
8235 const struct cred *cred;
8236
8237 cred = idr_remove(&ctx->personality_idr, id);
8238 if (cred)
8239 put_cred(cred);
8240 return 0;
8241}
8242
Jens Axboe85faa7b2020-04-09 18:14:00 -06008243static void io_ring_exit_work(struct work_struct *work)
8244{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008245 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8246 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008247
Jens Axboe56952e92020-06-17 15:00:04 -06008248 /*
8249 * If we're doing polled IO and end up having requests being
8250 * submitted async (out-of-line), then completions can come in while
8251 * we're waiting for refs to drop. We need to reap these manually,
8252 * as nobody else will be looking for them.
8253 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008254 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008255 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008256 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008257 io_iopoll_try_reap_events(ctx);
8258 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008259 io_ring_ctx_free(ctx);
8260}
8261
Jens Axboe2b188cc2019-01-07 10:46:33 -07008262static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8263{
8264 mutex_lock(&ctx->uring_lock);
8265 percpu_ref_kill(&ctx->refs);
8266 mutex_unlock(&ctx->uring_lock);
8267
Jens Axboef3606e32020-09-22 08:18:24 -06008268 io_kill_timeouts(ctx, NULL);
8269 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008270
8271 if (ctx->io_wq)
8272 io_wq_cancel_all(ctx->io_wq);
8273
Jens Axboe15dff282019-11-13 09:09:23 -07008274 /* if we failed setting up the ctx, we might not have any rings */
8275 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008276 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008277 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008278 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008279
8280 /*
8281 * Do this upfront, so we won't have a grace period where the ring
8282 * is closed but resources aren't reaped yet. This can cause
8283 * spurious failure in setting up a new ring.
8284 */
Jens Axboe760618f2020-07-24 12:53:31 -06008285 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8286 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008287
Jens Axboe85faa7b2020-04-09 18:14:00 -06008288 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008289 /*
8290 * Use system_unbound_wq to avoid spawning tons of event kworkers
8291 * if we're exiting a ton of rings at the same time. It just adds
8292 * noise and overhead, there's no discernable change in runtime
8293 * over using system_wq.
8294 */
8295 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008296}
8297
8298static int io_uring_release(struct inode *inode, struct file *file)
8299{
8300 struct io_ring_ctx *ctx = file->private_data;
8301
8302 file->private_data = NULL;
8303 io_ring_ctx_wait_and_kill(ctx);
8304 return 0;
8305}
8306
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008307static bool io_wq_files_match(struct io_wq_work *work, void *data)
8308{
8309 struct files_struct *files = data;
8310
Jens Axboe0f212202020-09-13 13:09:39 -06008311 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008312}
8313
Jens Axboef254ac02020-08-12 17:33:30 -06008314/*
8315 * Returns true if 'preq' is the link parent of 'req'
8316 */
8317static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8318{
8319 struct io_kiocb *link;
8320
8321 if (!(preq->flags & REQ_F_LINK_HEAD))
8322 return false;
8323
8324 list_for_each_entry(link, &preq->link_list, link_list) {
8325 if (link == req)
8326 return true;
8327 }
8328
8329 return false;
8330}
8331
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008332static bool io_match_link_files(struct io_kiocb *req,
8333 struct files_struct *files)
8334{
8335 struct io_kiocb *link;
8336
8337 if (io_match_files(req, files))
8338 return true;
8339 if (req->flags & REQ_F_LINK_HEAD) {
8340 list_for_each_entry(link, &req->link_list, link_list) {
8341 if (io_match_files(link, files))
8342 return true;
8343 }
8344 }
8345 return false;
8346}
8347
Jens Axboef254ac02020-08-12 17:33:30 -06008348/*
8349 * We're looking to cancel 'req' because it's holding on to our files, but
8350 * 'req' could be a link to another request. See if it is, and cancel that
8351 * parent request if so.
8352 */
8353static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8354{
8355 struct hlist_node *tmp;
8356 struct io_kiocb *preq;
8357 bool found = false;
8358 int i;
8359
8360 spin_lock_irq(&ctx->completion_lock);
8361 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8362 struct hlist_head *list;
8363
8364 list = &ctx->cancel_hash[i];
8365 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8366 found = io_match_link(preq, req);
8367 if (found) {
8368 io_poll_remove_one(preq);
8369 break;
8370 }
8371 }
8372 }
8373 spin_unlock_irq(&ctx->completion_lock);
8374 return found;
8375}
8376
8377static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8378 struct io_kiocb *req)
8379{
8380 struct io_kiocb *preq;
8381 bool found = false;
8382
8383 spin_lock_irq(&ctx->completion_lock);
8384 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8385 found = io_match_link(preq, req);
8386 if (found) {
8387 __io_timeout_cancel(preq);
8388 break;
8389 }
8390 }
8391 spin_unlock_irq(&ctx->completion_lock);
8392 return found;
8393}
8394
Jens Axboeb711d4e2020-08-16 08:23:05 -07008395static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8396{
8397 return io_match_link(container_of(work, struct io_kiocb, work), data);
8398}
8399
8400static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8401{
8402 enum io_wq_cancel cret;
8403
8404 /* cancel this particular work, if it's running */
8405 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8406 if (cret != IO_WQ_CANCEL_NOTFOUND)
8407 return;
8408
8409 /* find links that hold this pending, cancel those */
8410 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8411 if (cret != IO_WQ_CANCEL_NOTFOUND)
8412 return;
8413
8414 /* if we have a poll link holding this pending, cancel that */
8415 if (io_poll_remove_link(ctx, req))
8416 return;
8417
8418 /* final option, timeout link is holding this req pending */
8419 io_timeout_remove_link(ctx, req);
8420}
8421
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008422static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8423 struct files_struct *files)
8424{
8425 struct io_defer_entry *de = NULL;
8426 LIST_HEAD(list);
8427
8428 spin_lock_irq(&ctx->completion_lock);
8429 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008430 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008431 list_cut_position(&list, &ctx->defer_list, &de->list);
8432 break;
8433 }
8434 }
8435 spin_unlock_irq(&ctx->completion_lock);
8436
8437 while (!list_empty(&list)) {
8438 de = list_first_entry(&list, struct io_defer_entry, list);
8439 list_del_init(&de->list);
8440 req_set_fail_links(de->req);
8441 io_put_req(de->req);
8442 io_req_complete(de->req, -ECANCELED);
8443 kfree(de);
8444 }
8445}
8446
Jens Axboe76e1b642020-09-26 15:05:03 -06008447/*
8448 * Returns true if we found and killed one or more files pinning requests
8449 */
8450static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008451 struct files_struct *files)
8452{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008453 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008454 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008455
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008456 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008457 /* cancel all at once, should be faster than doing it one by one*/
8458 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8459
Jens Axboefcb323c2019-10-24 12:39:47 -06008460 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008461 struct io_kiocb *cancel_req = NULL, *req;
8462 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008463
8464 spin_lock_irq(&ctx->inflight_lock);
8465 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008466 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008467 continue;
8468 /* req is being completed, ignore */
8469 if (!refcount_inc_not_zero(&req->refs))
8470 continue;
8471 cancel_req = req;
8472 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008473 }
Jens Axboe768134d2019-11-10 20:30:53 -07008474 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008475 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008476 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008477 spin_unlock_irq(&ctx->inflight_lock);
8478
Jens Axboe768134d2019-11-10 20:30:53 -07008479 /* We need to keep going until we don't find a matching req */
8480 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008481 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008482 /* cancel this request, or head link requests */
8483 io_attempt_cancel(ctx, cancel_req);
8484 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008485 /* cancellations _may_ trigger task work */
8486 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008487 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008488 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008489 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008490
8491 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008492}
8493
Pavel Begunkov801dd572020-06-15 10:33:14 +03008494static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008495{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008496 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8497 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008498
Jens Axboef3606e32020-09-22 08:18:24 -06008499 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008500}
8501
Jens Axboe0f212202020-09-13 13:09:39 -06008502static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8503 struct task_struct *task,
8504 struct files_struct *files)
8505{
8506 bool ret;
8507
8508 ret = io_uring_cancel_files(ctx, files);
8509 if (!files) {
8510 enum io_wq_cancel cret;
8511
8512 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8513 if (cret != IO_WQ_CANCEL_NOTFOUND)
8514 ret = true;
8515
8516 /* SQPOLL thread does its own polling */
8517 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8518 while (!list_empty_careful(&ctx->iopoll_list)) {
8519 io_iopoll_try_reap_events(ctx);
8520 ret = true;
8521 }
8522 }
8523
8524 ret |= io_poll_remove_all(ctx, task);
8525 ret |= io_kill_timeouts(ctx, task);
8526 }
8527
8528 return ret;
8529}
8530
8531/*
8532 * We need to iteratively cancel requests, in case a request has dependent
8533 * hard links. These persist even for failure of cancelations, hence keep
8534 * looping until none are found.
8535 */
8536static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8537 struct files_struct *files)
8538{
8539 struct task_struct *task = current;
8540
Jens Axboe534ca6d2020-09-02 13:52:19 -06008541 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8542 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008543
8544 io_cqring_overflow_flush(ctx, true, task, files);
8545
8546 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8547 io_run_task_work();
8548 cond_resched();
8549 }
8550}
8551
8552/*
8553 * Note that this task has used io_uring. We use it for cancelation purposes.
8554 */
8555static int io_uring_add_task_file(struct file *file)
8556{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008557 struct io_uring_task *tctx = current->io_uring;
8558
8559 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008560 int ret;
8561
8562 ret = io_uring_alloc_task_context(current);
8563 if (unlikely(ret))
8564 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008565 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008566 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008567 if (tctx->last != file) {
8568 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008569
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008570 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008571 get_file(file);
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008572 xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008573 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008574 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008575 }
8576
8577 return 0;
8578}
8579
8580/*
8581 * Remove this io_uring_file -> task mapping.
8582 */
8583static void io_uring_del_task_file(struct file *file)
8584{
8585 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008586
8587 if (tctx->last == file)
8588 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008589 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008590 if (file)
8591 fput(file);
8592}
8593
8594static void __io_uring_attempt_task_drop(struct file *file)
8595{
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008596 struct file *old = xa_load(&current->io_uring->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008597
8598 if (old == file)
8599 io_uring_del_task_file(file);
8600}
8601
8602/*
8603 * Drop task note for this file if we're the only ones that hold it after
8604 * pending fput()
8605 */
8606static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8607{
8608 if (!current->io_uring)
8609 return;
8610 /*
8611 * fput() is pending, will be 2 if the only other ref is our potential
8612 * task file note. If the task is exiting, drop regardless of count.
8613 */
8614 if (!exiting && atomic_long_read(&file->f_count) != 2)
8615 return;
8616
8617 __io_uring_attempt_task_drop(file);
8618}
8619
8620void __io_uring_files_cancel(struct files_struct *files)
8621{
8622 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008623 struct file *file;
8624 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008625
8626 /* make sure overflow events are dropped */
8627 tctx->in_idle = true;
8628
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008629 xa_for_each(&tctx->xa, index, file) {
8630 struct io_ring_ctx *ctx = file->private_data;
Jens Axboe0f212202020-09-13 13:09:39 -06008631
8632 io_uring_cancel_task_requests(ctx, files);
8633 if (files)
8634 io_uring_del_task_file(file);
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008635 }
Jens Axboe0f212202020-09-13 13:09:39 -06008636}
8637
8638static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8639{
8640 return atomic_long_read(&tctx->req_issue) ==
8641 atomic_long_read(&tctx->req_complete);
8642}
8643
8644/*
8645 * Find any io_uring fd that this task has registered or done IO on, and cancel
8646 * requests.
8647 */
8648void __io_uring_task_cancel(void)
8649{
8650 struct io_uring_task *tctx = current->io_uring;
8651 DEFINE_WAIT(wait);
8652 long completions;
8653
8654 /* make sure overflow events are dropped */
8655 tctx->in_idle = true;
8656
8657 while (!io_uring_task_idle(tctx)) {
8658 /* read completions before cancelations */
8659 completions = atomic_long_read(&tctx->req_complete);
8660 __io_uring_files_cancel(NULL);
8661
8662 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8663
8664 /*
8665 * If we've seen completions, retry. This avoids a race where
8666 * a completion comes in before we did prepare_to_wait().
8667 */
8668 if (completions != atomic_long_read(&tctx->req_complete))
8669 continue;
8670 if (io_uring_task_idle(tctx))
8671 break;
8672 schedule();
8673 }
8674
8675 finish_wait(&tctx->wait, &wait);
8676 tctx->in_idle = false;
8677}
8678
Jens Axboefcb323c2019-10-24 12:39:47 -06008679static int io_uring_flush(struct file *file, void *data)
8680{
8681 struct io_ring_ctx *ctx = file->private_data;
8682
Jens Axboe6ab23142020-02-08 20:23:59 -07008683 /*
8684 * If the task is going away, cancel work it may have pending
8685 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008686 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008687 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008688
Jens Axboe0f212202020-09-13 13:09:39 -06008689 io_uring_cancel_task_requests(ctx, data);
8690 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008691 return 0;
8692}
8693
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008694static void *io_uring_validate_mmap_request(struct file *file,
8695 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008696{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008697 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008698 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008699 struct page *page;
8700 void *ptr;
8701
8702 switch (offset) {
8703 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008704 case IORING_OFF_CQ_RING:
8705 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008706 break;
8707 case IORING_OFF_SQES:
8708 ptr = ctx->sq_sqes;
8709 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008710 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008711 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008712 }
8713
8714 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008715 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008716 return ERR_PTR(-EINVAL);
8717
8718 return ptr;
8719}
8720
8721#ifdef CONFIG_MMU
8722
8723static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8724{
8725 size_t sz = vma->vm_end - vma->vm_start;
8726 unsigned long pfn;
8727 void *ptr;
8728
8729 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8730 if (IS_ERR(ptr))
8731 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008732
8733 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8734 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8735}
8736
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008737#else /* !CONFIG_MMU */
8738
8739static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8740{
8741 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8742}
8743
8744static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8745{
8746 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8747}
8748
8749static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8750 unsigned long addr, unsigned long len,
8751 unsigned long pgoff, unsigned long flags)
8752{
8753 void *ptr;
8754
8755 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8756 if (IS_ERR(ptr))
8757 return PTR_ERR(ptr);
8758
8759 return (unsigned long) ptr;
8760}
8761
8762#endif /* !CONFIG_MMU */
8763
Jens Axboe90554202020-09-03 12:12:41 -06008764static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8765{
8766 DEFINE_WAIT(wait);
8767
8768 do {
8769 if (!io_sqring_full(ctx))
8770 break;
8771
8772 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8773
8774 if (!io_sqring_full(ctx))
8775 break;
8776
8777 schedule();
8778 } while (!signal_pending(current));
8779
8780 finish_wait(&ctx->sqo_sq_wait, &wait);
8781}
8782
Jens Axboe2b188cc2019-01-07 10:46:33 -07008783SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8784 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8785 size_t, sigsz)
8786{
8787 struct io_ring_ctx *ctx;
8788 long ret = -EBADF;
8789 int submitted = 0;
8790 struct fd f;
8791
Jens Axboe4c6e2772020-07-01 11:29:10 -06008792 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008793
Jens Axboe90554202020-09-03 12:12:41 -06008794 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8795 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008796 return -EINVAL;
8797
8798 f = fdget(fd);
8799 if (!f.file)
8800 return -EBADF;
8801
8802 ret = -EOPNOTSUPP;
8803 if (f.file->f_op != &io_uring_fops)
8804 goto out_fput;
8805
8806 ret = -ENXIO;
8807 ctx = f.file->private_data;
8808 if (!percpu_ref_tryget(&ctx->refs))
8809 goto out_fput;
8810
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008811 ret = -EBADFD;
8812 if (ctx->flags & IORING_SETUP_R_DISABLED)
8813 goto out;
8814
Jens Axboe6c271ce2019-01-10 11:22:30 -07008815 /*
8816 * For SQ polling, the thread will do all submissions and completions.
8817 * Just return the requested submit count, and wake the thread if
8818 * we were asked to.
8819 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008820 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008821 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008822 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008823 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008824 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008825 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008826 if (flags & IORING_ENTER_SQ_WAIT)
8827 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008828 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008829 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008830 ret = io_uring_add_task_file(f.file);
8831 if (unlikely(ret))
8832 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008833 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008834 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008835 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008836
8837 if (submitted != to_submit)
8838 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008839 }
8840 if (flags & IORING_ENTER_GETEVENTS) {
8841 min_complete = min(min_complete, ctx->cq_entries);
8842
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008843 /*
8844 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8845 * space applications don't need to do io completion events
8846 * polling again, they can rely on io_sq_thread to do polling
8847 * work, which can reduce cpu usage and uring_lock contention.
8848 */
8849 if (ctx->flags & IORING_SETUP_IOPOLL &&
8850 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008851 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008852 } else {
8853 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8854 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008855 }
8856
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008857out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008858 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008859out_fput:
8860 fdput(f);
8861 return submitted ? submitted : ret;
8862}
8863
Tobias Klauserbebdb652020-02-26 18:38:32 +01008864#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008865static int io_uring_show_cred(int id, void *p, void *data)
8866{
8867 const struct cred *cred = p;
8868 struct seq_file *m = data;
8869 struct user_namespace *uns = seq_user_ns(m);
8870 struct group_info *gi;
8871 kernel_cap_t cap;
8872 unsigned __capi;
8873 int g;
8874
8875 seq_printf(m, "%5d\n", id);
8876 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8877 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8878 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8879 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8880 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8881 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8882 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8883 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8884 seq_puts(m, "\n\tGroups:\t");
8885 gi = cred->group_info;
8886 for (g = 0; g < gi->ngroups; g++) {
8887 seq_put_decimal_ull(m, g ? " " : "",
8888 from_kgid_munged(uns, gi->gid[g]));
8889 }
8890 seq_puts(m, "\n\tCapEff:\t");
8891 cap = cred->cap_effective;
8892 CAP_FOR_EACH_U32(__capi)
8893 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8894 seq_putc(m, '\n');
8895 return 0;
8896}
8897
8898static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8899{
Joseph Qidbbe9c62020-09-29 09:01:22 -06008900 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06008901 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008902 int i;
8903
Jens Axboefad8e0d2020-09-28 08:57:48 -06008904 /*
8905 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8906 * since fdinfo case grabs it in the opposite direction of normal use
8907 * cases. If we fail to get the lock, we just don't iterate any
8908 * structures that could be going away outside the io_uring mutex.
8909 */
8910 has_lock = mutex_trylock(&ctx->uring_lock);
8911
Joseph Qidbbe9c62020-09-29 09:01:22 -06008912 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
8913 sq = ctx->sq_data;
8914
8915 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
8916 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07008917 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008918 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008919 struct fixed_file_table *table;
8920 struct file *f;
8921
8922 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8923 f = table->files[i & IORING_FILE_TABLE_MASK];
8924 if (f)
8925 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8926 else
8927 seq_printf(m, "%5u: <none>\n", i);
8928 }
8929 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008930 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008931 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8932
8933 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8934 (unsigned int) buf->len);
8935 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008936 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008937 seq_printf(m, "Personalities:\n");
8938 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8939 }
Jens Axboed7718a92020-02-14 22:23:12 -07008940 seq_printf(m, "PollList:\n");
8941 spin_lock_irq(&ctx->completion_lock);
8942 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8943 struct hlist_head *list = &ctx->cancel_hash[i];
8944 struct io_kiocb *req;
8945
8946 hlist_for_each_entry(req, list, hash_node)
8947 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8948 req->task->task_works != NULL);
8949 }
8950 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008951 if (has_lock)
8952 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008953}
8954
8955static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8956{
8957 struct io_ring_ctx *ctx = f->private_data;
8958
8959 if (percpu_ref_tryget(&ctx->refs)) {
8960 __io_uring_show_fdinfo(ctx, m);
8961 percpu_ref_put(&ctx->refs);
8962 }
8963}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008964#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008965
Jens Axboe2b188cc2019-01-07 10:46:33 -07008966static const struct file_operations io_uring_fops = {
8967 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008968 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008969 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008970#ifndef CONFIG_MMU
8971 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8972 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8973#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008974 .poll = io_uring_poll,
8975 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008976#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008977 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008978#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008979};
8980
8981static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8982 struct io_uring_params *p)
8983{
Hristo Venev75b28af2019-08-26 17:23:46 +00008984 struct io_rings *rings;
8985 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008986
Jens Axboebd740482020-08-05 12:58:23 -06008987 /* make sure these are sane, as we already accounted them */
8988 ctx->sq_entries = p->sq_entries;
8989 ctx->cq_entries = p->cq_entries;
8990
Hristo Venev75b28af2019-08-26 17:23:46 +00008991 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8992 if (size == SIZE_MAX)
8993 return -EOVERFLOW;
8994
8995 rings = io_mem_alloc(size);
8996 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008997 return -ENOMEM;
8998
Hristo Venev75b28af2019-08-26 17:23:46 +00008999 ctx->rings = rings;
9000 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9001 rings->sq_ring_mask = p->sq_entries - 1;
9002 rings->cq_ring_mask = p->cq_entries - 1;
9003 rings->sq_ring_entries = p->sq_entries;
9004 rings->cq_ring_entries = p->cq_entries;
9005 ctx->sq_mask = rings->sq_ring_mask;
9006 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009007
9008 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009009 if (size == SIZE_MAX) {
9010 io_mem_free(ctx->rings);
9011 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009012 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009013 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009014
9015 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009016 if (!ctx->sq_sqes) {
9017 io_mem_free(ctx->rings);
9018 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009019 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009020 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009021
Jens Axboe2b188cc2019-01-07 10:46:33 -07009022 return 0;
9023}
9024
9025/*
9026 * Allocate an anonymous fd, this is what constitutes the application
9027 * visible backing of an io_uring instance. The application mmaps this
9028 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9029 * we have to tie this fd to a socket for file garbage collection purposes.
9030 */
9031static int io_uring_get_fd(struct io_ring_ctx *ctx)
9032{
9033 struct file *file;
9034 int ret;
9035
9036#if defined(CONFIG_UNIX)
9037 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9038 &ctx->ring_sock);
9039 if (ret)
9040 return ret;
9041#endif
9042
9043 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9044 if (ret < 0)
9045 goto err;
9046
9047 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9048 O_RDWR | O_CLOEXEC);
9049 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009050err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009051 put_unused_fd(ret);
9052 ret = PTR_ERR(file);
9053 goto err;
9054 }
9055
9056#if defined(CONFIG_UNIX)
9057 ctx->ring_sock->file = file;
9058#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009059 if (unlikely(io_uring_add_task_file(file))) {
9060 file = ERR_PTR(-ENOMEM);
9061 goto err_fd;
9062 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009063 fd_install(ret, file);
9064 return ret;
9065err:
9066#if defined(CONFIG_UNIX)
9067 sock_release(ctx->ring_sock);
9068 ctx->ring_sock = NULL;
9069#endif
9070 return ret;
9071}
9072
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009073static int io_uring_create(unsigned entries, struct io_uring_params *p,
9074 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009075{
9076 struct user_struct *user = NULL;
9077 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009078 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009079 int ret;
9080
Jens Axboe8110c1a2019-12-28 15:39:54 -07009081 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009082 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009083 if (entries > IORING_MAX_ENTRIES) {
9084 if (!(p->flags & IORING_SETUP_CLAMP))
9085 return -EINVAL;
9086 entries = IORING_MAX_ENTRIES;
9087 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009088
9089 /*
9090 * Use twice as many entries for the CQ ring. It's possible for the
9091 * application to drive a higher depth than the size of the SQ ring,
9092 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009093 * some flexibility in overcommitting a bit. If the application has
9094 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9095 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009096 */
9097 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009098 if (p->flags & IORING_SETUP_CQSIZE) {
9099 /*
9100 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9101 * to a power-of-two, if it isn't already. We do NOT impose
9102 * any cq vs sq ring sizing.
9103 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009104 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009105 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009106 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9107 if (!(p->flags & IORING_SETUP_CLAMP))
9108 return -EINVAL;
9109 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9110 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009111 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9112 } else {
9113 p->cq_entries = 2 * p->sq_entries;
9114 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009115
9116 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009117 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009118
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009119 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009120 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009121 ring_pages(p->sq_entries, p->cq_entries));
9122 if (ret) {
9123 free_uid(user);
9124 return ret;
9125 }
9126 }
9127
9128 ctx = io_ring_ctx_alloc(p);
9129 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009130 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009131 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009132 p->cq_entries));
9133 free_uid(user);
9134 return -ENOMEM;
9135 }
9136 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009137 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009138 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009139
Jens Axboe2aede0e2020-09-14 10:45:53 -06009140 ctx->sqo_task = get_task_struct(current);
9141
9142 /*
9143 * This is just grabbed for accounting purposes. When a process exits,
9144 * the mm is exited and dropped before the files, hence we need to hang
9145 * on to this mm purely for the purposes of being able to unaccount
9146 * memory (locked/pinned vm). It's not used for anything else.
9147 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009148 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009149 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009150
Dennis Zhou91d8f512020-09-16 13:41:05 -07009151#ifdef CONFIG_BLK_CGROUP
9152 /*
9153 * The sq thread will belong to the original cgroup it was inited in.
9154 * If the cgroup goes offline (e.g. disabling the io controller), then
9155 * issued bios will be associated with the closest cgroup later in the
9156 * block layer.
9157 */
9158 rcu_read_lock();
9159 ctx->sqo_blkcg_css = blkcg_css();
9160 ret = css_tryget_online(ctx->sqo_blkcg_css);
9161 rcu_read_unlock();
9162 if (!ret) {
9163 /* don't init against a dying cgroup, have the user try again */
9164 ctx->sqo_blkcg_css = NULL;
9165 ret = -ENODEV;
9166 goto err;
9167 }
9168#endif
9169
Jens Axboef74441e2020-08-05 13:00:44 -06009170 /*
9171 * Account memory _before_ installing the file descriptor. Once
9172 * the descriptor is installed, it can get closed at any time. Also
9173 * do this before hitting the general error path, as ring freeing
9174 * will un-account as well.
9175 */
9176 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9177 ACCT_LOCKED);
9178 ctx->limit_mem = limit_mem;
9179
Jens Axboe2b188cc2019-01-07 10:46:33 -07009180 ret = io_allocate_scq_urings(ctx, p);
9181 if (ret)
9182 goto err;
9183
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009184 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009185 if (ret)
9186 goto err;
9187
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009188 if (!(p->flags & IORING_SETUP_R_DISABLED))
9189 io_sq_offload_start(ctx);
9190
Jens Axboe2b188cc2019-01-07 10:46:33 -07009191 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009192 p->sq_off.head = offsetof(struct io_rings, sq.head);
9193 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9194 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9195 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9196 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9197 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9198 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009199
9200 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009201 p->cq_off.head = offsetof(struct io_rings, cq.head);
9202 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9203 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9204 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9205 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9206 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009207 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009208
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009209 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9210 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009211 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9212 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009213
9214 if (copy_to_user(params, p, sizeof(*p))) {
9215 ret = -EFAULT;
9216 goto err;
9217 }
Jens Axboed1719f72020-07-30 13:43:53 -06009218
9219 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009220 * Install ring fd as the very last thing, so we don't risk someone
9221 * having closed it before we finish setup
9222 */
9223 ret = io_uring_get_fd(ctx);
9224 if (ret < 0)
9225 goto err;
9226
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009227 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009228 return ret;
9229err:
9230 io_ring_ctx_wait_and_kill(ctx);
9231 return ret;
9232}
9233
9234/*
9235 * Sets up an aio uring context, and returns the fd. Applications asks for a
9236 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9237 * params structure passed in.
9238 */
9239static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9240{
9241 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009242 int i;
9243
9244 if (copy_from_user(&p, params, sizeof(p)))
9245 return -EFAULT;
9246 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9247 if (p.resv[i])
9248 return -EINVAL;
9249 }
9250
Jens Axboe6c271ce2019-01-10 11:22:30 -07009251 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009252 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009253 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9254 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009255 return -EINVAL;
9256
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009257 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009258}
9259
9260SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9261 struct io_uring_params __user *, params)
9262{
9263 return io_uring_setup(entries, params);
9264}
9265
Jens Axboe66f4af92020-01-16 15:36:52 -07009266static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9267{
9268 struct io_uring_probe *p;
9269 size_t size;
9270 int i, ret;
9271
9272 size = struct_size(p, ops, nr_args);
9273 if (size == SIZE_MAX)
9274 return -EOVERFLOW;
9275 p = kzalloc(size, GFP_KERNEL);
9276 if (!p)
9277 return -ENOMEM;
9278
9279 ret = -EFAULT;
9280 if (copy_from_user(p, arg, size))
9281 goto out;
9282 ret = -EINVAL;
9283 if (memchr_inv(p, 0, size))
9284 goto out;
9285
9286 p->last_op = IORING_OP_LAST - 1;
9287 if (nr_args > IORING_OP_LAST)
9288 nr_args = IORING_OP_LAST;
9289
9290 for (i = 0; i < nr_args; i++) {
9291 p->ops[i].op = i;
9292 if (!io_op_defs[i].not_supported)
9293 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9294 }
9295 p->ops_len = i;
9296
9297 ret = 0;
9298 if (copy_to_user(arg, p, size))
9299 ret = -EFAULT;
9300out:
9301 kfree(p);
9302 return ret;
9303}
9304
Jens Axboe071698e2020-01-28 10:04:42 -07009305static int io_register_personality(struct io_ring_ctx *ctx)
9306{
9307 const struct cred *creds = get_current_cred();
9308 int id;
9309
9310 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9311 USHRT_MAX, GFP_KERNEL);
9312 if (id < 0)
9313 put_cred(creds);
9314 return id;
9315}
9316
9317static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9318{
9319 const struct cred *old_creds;
9320
9321 old_creds = idr_remove(&ctx->personality_idr, id);
9322 if (old_creds) {
9323 put_cred(old_creds);
9324 return 0;
9325 }
9326
9327 return -EINVAL;
9328}
9329
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009330static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9331 unsigned int nr_args)
9332{
9333 struct io_uring_restriction *res;
9334 size_t size;
9335 int i, ret;
9336
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009337 /* Restrictions allowed only if rings started disabled */
9338 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9339 return -EBADFD;
9340
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009341 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009342 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009343 return -EBUSY;
9344
9345 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9346 return -EINVAL;
9347
9348 size = array_size(nr_args, sizeof(*res));
9349 if (size == SIZE_MAX)
9350 return -EOVERFLOW;
9351
9352 res = memdup_user(arg, size);
9353 if (IS_ERR(res))
9354 return PTR_ERR(res);
9355
9356 ret = 0;
9357
9358 for (i = 0; i < nr_args; i++) {
9359 switch (res[i].opcode) {
9360 case IORING_RESTRICTION_REGISTER_OP:
9361 if (res[i].register_op >= IORING_REGISTER_LAST) {
9362 ret = -EINVAL;
9363 goto out;
9364 }
9365
9366 __set_bit(res[i].register_op,
9367 ctx->restrictions.register_op);
9368 break;
9369 case IORING_RESTRICTION_SQE_OP:
9370 if (res[i].sqe_op >= IORING_OP_LAST) {
9371 ret = -EINVAL;
9372 goto out;
9373 }
9374
9375 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9376 break;
9377 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9378 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9379 break;
9380 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9381 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9382 break;
9383 default:
9384 ret = -EINVAL;
9385 goto out;
9386 }
9387 }
9388
9389out:
9390 /* Reset all restrictions if an error happened */
9391 if (ret != 0)
9392 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9393 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009394 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009395
9396 kfree(res);
9397 return ret;
9398}
9399
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009400static int io_register_enable_rings(struct io_ring_ctx *ctx)
9401{
9402 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9403 return -EBADFD;
9404
9405 if (ctx->restrictions.registered)
9406 ctx->restricted = 1;
9407
9408 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9409
9410 io_sq_offload_start(ctx);
9411
9412 return 0;
9413}
9414
Jens Axboe071698e2020-01-28 10:04:42 -07009415static bool io_register_op_must_quiesce(int op)
9416{
9417 switch (op) {
9418 case IORING_UNREGISTER_FILES:
9419 case IORING_REGISTER_FILES_UPDATE:
9420 case IORING_REGISTER_PROBE:
9421 case IORING_REGISTER_PERSONALITY:
9422 case IORING_UNREGISTER_PERSONALITY:
9423 return false;
9424 default:
9425 return true;
9426 }
9427}
9428
Jens Axboeedafcce2019-01-09 09:16:05 -07009429static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9430 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009431 __releases(ctx->uring_lock)
9432 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009433{
9434 int ret;
9435
Jens Axboe35fa71a2019-04-22 10:23:23 -06009436 /*
9437 * We're inside the ring mutex, if the ref is already dying, then
9438 * someone else killed the ctx or is already going through
9439 * io_uring_register().
9440 */
9441 if (percpu_ref_is_dying(&ctx->refs))
9442 return -ENXIO;
9443
Jens Axboe071698e2020-01-28 10:04:42 -07009444 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009445 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009446
Jens Axboe05f3fb32019-12-09 11:22:50 -07009447 /*
9448 * Drop uring mutex before waiting for references to exit. If
9449 * another thread is currently inside io_uring_enter() it might
9450 * need to grab the uring_lock to make progress. If we hold it
9451 * here across the drain wait, then we can deadlock. It's safe
9452 * to drop the mutex here, since no new references will come in
9453 * after we've killed the percpu ref.
9454 */
9455 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009456 do {
9457 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9458 if (!ret)
9459 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009460 ret = io_run_task_work_sig();
9461 if (ret < 0)
9462 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009463 } while (1);
9464
Jens Axboe05f3fb32019-12-09 11:22:50 -07009465 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009466
Jens Axboec1503682020-01-08 08:26:07 -07009467 if (ret) {
9468 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009469 goto out_quiesce;
9470 }
9471 }
9472
9473 if (ctx->restricted) {
9474 if (opcode >= IORING_REGISTER_LAST) {
9475 ret = -EINVAL;
9476 goto out;
9477 }
9478
9479 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9480 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009481 goto out;
9482 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009483 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009484
9485 switch (opcode) {
9486 case IORING_REGISTER_BUFFERS:
9487 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9488 break;
9489 case IORING_UNREGISTER_BUFFERS:
9490 ret = -EINVAL;
9491 if (arg || nr_args)
9492 break;
9493 ret = io_sqe_buffer_unregister(ctx);
9494 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009495 case IORING_REGISTER_FILES:
9496 ret = io_sqe_files_register(ctx, arg, nr_args);
9497 break;
9498 case IORING_UNREGISTER_FILES:
9499 ret = -EINVAL;
9500 if (arg || nr_args)
9501 break;
9502 ret = io_sqe_files_unregister(ctx);
9503 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009504 case IORING_REGISTER_FILES_UPDATE:
9505 ret = io_sqe_files_update(ctx, arg, nr_args);
9506 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009507 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009508 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009509 ret = -EINVAL;
9510 if (nr_args != 1)
9511 break;
9512 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009513 if (ret)
9514 break;
9515 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9516 ctx->eventfd_async = 1;
9517 else
9518 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009519 break;
9520 case IORING_UNREGISTER_EVENTFD:
9521 ret = -EINVAL;
9522 if (arg || nr_args)
9523 break;
9524 ret = io_eventfd_unregister(ctx);
9525 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009526 case IORING_REGISTER_PROBE:
9527 ret = -EINVAL;
9528 if (!arg || nr_args > 256)
9529 break;
9530 ret = io_probe(ctx, arg, nr_args);
9531 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009532 case IORING_REGISTER_PERSONALITY:
9533 ret = -EINVAL;
9534 if (arg || nr_args)
9535 break;
9536 ret = io_register_personality(ctx);
9537 break;
9538 case IORING_UNREGISTER_PERSONALITY:
9539 ret = -EINVAL;
9540 if (arg)
9541 break;
9542 ret = io_unregister_personality(ctx, nr_args);
9543 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009544 case IORING_REGISTER_ENABLE_RINGS:
9545 ret = -EINVAL;
9546 if (arg || nr_args)
9547 break;
9548 ret = io_register_enable_rings(ctx);
9549 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009550 case IORING_REGISTER_RESTRICTIONS:
9551 ret = io_register_restrictions(ctx, arg, nr_args);
9552 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009553 default:
9554 ret = -EINVAL;
9555 break;
9556 }
9557
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009558out:
Jens Axboe071698e2020-01-28 10:04:42 -07009559 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009560 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009561 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009562out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009563 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009564 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009565 return ret;
9566}
9567
9568SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9569 void __user *, arg, unsigned int, nr_args)
9570{
9571 struct io_ring_ctx *ctx;
9572 long ret = -EBADF;
9573 struct fd f;
9574
9575 f = fdget(fd);
9576 if (!f.file)
9577 return -EBADF;
9578
9579 ret = -EOPNOTSUPP;
9580 if (f.file->f_op != &io_uring_fops)
9581 goto out_fput;
9582
9583 ctx = f.file->private_data;
9584
9585 mutex_lock(&ctx->uring_lock);
9586 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9587 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009588 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9589 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009590out_fput:
9591 fdput(f);
9592 return ret;
9593}
9594
Jens Axboe2b188cc2019-01-07 10:46:33 -07009595static int __init io_uring_init(void)
9596{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009597#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9598 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9599 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9600} while (0)
9601
9602#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9603 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9604 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9605 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9606 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9607 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9608 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9609 BUILD_BUG_SQE_ELEM(8, __u64, off);
9610 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9611 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009612 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009613 BUILD_BUG_SQE_ELEM(24, __u32, len);
9614 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9615 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9616 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9617 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009618 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9619 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009620 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9621 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9622 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9623 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9624 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9625 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9626 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9627 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009628 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009629 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9630 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9631 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009632 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009633
Jens Axboed3656342019-12-18 09:50:26 -07009634 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009635 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009636 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9637 return 0;
9638};
9639__initcall(io_uring_init);