blob: e92ed22ef924e2074c845c2d1d5bb08702f247c7 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020085#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
Jens Axboe2b188cc2019-01-07 10:46:33 -070088#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060091#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
Daniel Xu5277dea2019-09-14 14:23:45 -070093#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060094#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060095
96/*
97 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
98 */
99#define IORING_FILE_TABLE_SHIFT 9
100#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
101#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
102#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200103#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
104 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700105
106struct io_uring {
107 u32 head ____cacheline_aligned_in_smp;
108 u32 tail ____cacheline_aligned_in_smp;
109};
110
Stefan Bühler1e84b972019-04-24 23:54:16 +0200111/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000112 * This data is shared with the application through the mmap at offsets
113 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 *
115 * The offsets to the member fields are published through struct
116 * io_sqring_offsets when calling io_uring_setup.
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 /*
120 * Head and tail offsets into the ring; the offsets need to be
121 * masked to get valid indices.
122 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 * The kernel controls head of the sq ring and the tail of the cq ring,
124 * and the application controls tail of the sq ring and the head of the
125 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000129 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 * ring_entries - 1)
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 u32 sq_ring_mask, cq_ring_mask;
133 /* Ring sizes (constant, power of 2) */
134 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Number of invalid entries dropped by the kernel due to
137 * invalid index stored in array
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * After a new SQ head value was read by the application this
144 * counter includes all submissions that were dropped reaching
145 * the new SQ head (and possibly more).
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200149 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application.
153 *
154 * The application needs a full memory barrier before checking
155 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200159 * Runtime CQ flags
160 *
161 * Written by the application, shouldn't be modified by the
162 * kernel.
163 */
164 u32 cq_flags;
165 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200166 * Number of completion events lost because the queue was full;
167 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800168 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200169 * the completion queue.
170 *
171 * Written by the kernel, shouldn't be modified by the
172 * application (i.e. get number of "new events" by comparing to
173 * cached value).
174 *
175 * As completion events come in out of order this counter is not
176 * ordered with any other data.
177 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000178 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200179 /*
180 * Ring buffer of completion events.
181 *
182 * The kernel writes completion events fresh every time they are
183 * produced, so the application is allowed to modify pending
184 * entries.
185 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000186 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700187};
188
Jens Axboeedafcce2019-01-09 09:16:05 -0700189struct io_mapped_ubuf {
190 u64 ubuf;
191 size_t len;
192 struct bio_vec *bvec;
193 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600194 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700195};
196
Jens Axboe65e19f52019-10-26 07:20:21 -0600197struct fixed_file_table {
198 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700199};
200
Xiaoguang Wang05589552020-03-31 14:05:18 +0800201struct fixed_file_ref_node {
202 struct percpu_ref refs;
203 struct list_head node;
204 struct list_head file_list;
205 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600206 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800207};
208
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209struct fixed_file_data {
210 struct fixed_file_table *table;
211 struct io_ring_ctx *ctx;
212
Pavel Begunkovb2e96852020-10-10 18:34:16 +0100213 struct fixed_file_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700214 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700215 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800216 struct list_head ref_list;
217 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700218};
219
Jens Axboe5a2e7452020-02-23 16:23:11 -0700220struct io_buffer {
221 struct list_head list;
222 __u64 addr;
223 __s32 len;
224 __u16 bid;
225};
226
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200227struct io_restriction {
228 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
229 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
230 u8 sqe_flags_allowed;
231 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200232 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200233};
234
Jens Axboe534ca6d2020-09-02 13:52:19 -0600235struct io_sq_data {
236 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600237 struct mutex lock;
238
239 /* ctx's that are using this sqd */
240 struct list_head ctx_list;
241 struct list_head ctx_new_list;
242 struct mutex ctx_lock;
243
Jens Axboe534ca6d2020-09-02 13:52:19 -0600244 struct task_struct *thread;
245 struct wait_queue_head wait;
246};
247
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248struct io_ring_ctx {
249 struct {
250 struct percpu_ref refs;
251 } ____cacheline_aligned_in_smp;
252
253 struct {
254 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800255 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700256 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800257 unsigned int cq_overflow_flushed: 1;
258 unsigned int drain_next: 1;
259 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200260 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261
Hristo Venev75b28af2019-08-26 17:23:46 +0000262 /*
263 * Ring buffer of indices into array of io_uring_sqe, which is
264 * mmapped by the application using the IORING_OFF_SQES offset.
265 *
266 * This indirection could e.g. be used to assign fixed
267 * io_uring_sqe entries to operations and only submit them to
268 * the queue when needed.
269 *
270 * The kernel modifies neither the indices array nor the entries
271 * array.
272 */
273 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700274 unsigned cached_sq_head;
275 unsigned sq_entries;
276 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700277 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600278 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700280 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600281
282 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600283 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700284 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285
Jens Axboefcb323c2019-10-24 12:39:47 -0600286 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700287 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700288 } ____cacheline_aligned_in_smp;
289
Hristo Venev75b28af2019-08-26 17:23:46 +0000290 struct io_rings *rings;
291
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600293 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600294
295 /*
296 * For SQPOLL usage - we hold a reference to the parent task, so we
297 * have access to the ->files
298 */
299 struct task_struct *sqo_task;
300
301 /* Only used for accounting purposes */
302 struct mm_struct *mm_account;
303
Dennis Zhou91d8f512020-09-16 13:41:05 -0700304#ifdef CONFIG_BLK_CGROUP
305 struct cgroup_subsys_state *sqo_blkcg_css;
306#endif
307
Jens Axboe534ca6d2020-09-02 13:52:19 -0600308 struct io_sq_data *sq_data; /* if using sq thread polling */
309
Jens Axboe90554202020-09-03 12:12:41 -0600310 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600311 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600312 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
Jens Axboe6b063142019-01-10 22:13:58 -0700314 /*
315 * If used, fixed file set. Writers must ensure that ->refs is dead,
316 * readers must ensure that ->refs is alive as long as the file* is
317 * used. Only updated through io_uring_register(2).
318 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700319 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700320 unsigned nr_user_files;
321
Jens Axboeedafcce2019-01-09 09:16:05 -0700322 /* if used, fixed mapped user buffers */
323 unsigned nr_user_bufs;
324 struct io_mapped_ubuf *user_bufs;
325
Jens Axboe2b188cc2019-01-07 10:46:33 -0700326 struct user_struct *user;
327
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700328 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700329
Jens Axboe0f158b42020-05-14 17:18:39 -0600330 struct completion ref_comp;
331 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700332
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700333 /* if all else fails... */
334 struct io_kiocb *fallback_req;
335
Jens Axboe206aefd2019-11-07 18:27:42 -0700336#if defined(CONFIG_UNIX)
337 struct socket *ring_sock;
338#endif
339
Jens Axboe5a2e7452020-02-23 16:23:11 -0700340 struct idr io_buffer_idr;
341
Jens Axboe071698e2020-01-28 10:04:42 -0700342 struct idr personality_idr;
343
Jens Axboe206aefd2019-11-07 18:27:42 -0700344 struct {
345 unsigned cached_cq_tail;
346 unsigned cq_entries;
347 unsigned cq_mask;
348 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700349 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700350 struct wait_queue_head cq_wait;
351 struct fasync_struct *cq_fasync;
352 struct eventfd_ctx *cq_ev_fd;
353 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700354
355 struct {
356 struct mutex uring_lock;
357 wait_queue_head_t wait;
358 } ____cacheline_aligned_in_smp;
359
360 struct {
361 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700362
Jens Axboedef596e2019-01-09 08:59:42 -0700363 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300364 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700365 * io_uring instances that don't use IORING_SETUP_SQPOLL.
366 * For SQPOLL, only the single threaded io_sq_thread() will
367 * manipulate the list, hence no extra locking is needed there.
368 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300369 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700370 struct hlist_head *cancel_hash;
371 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700372 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600373
374 spinlock_t inflight_lock;
375 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600377
Jens Axboe4a38aed22020-05-14 17:21:15 -0600378 struct delayed_work file_put_work;
379 struct llist_head file_put_llist;
380
Jens Axboe85faa7b2020-04-09 18:14:00 -0600381 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200382 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383};
384
Jens Axboe09bb8392019-03-13 12:39:28 -0600385/*
386 * First field must be the file pointer in all the
387 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
388 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700389struct io_poll_iocb {
390 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700391 union {
392 struct wait_queue_head *head;
393 u64 addr;
394 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700395 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600396 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700397 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700398 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700399};
400
Jens Axboeb5dba592019-12-11 14:02:38 -0700401struct io_close {
402 struct file *file;
403 struct file *put_file;
404 int fd;
405};
406
Jens Axboead8a48a2019-11-15 08:49:11 -0700407struct io_timeout_data {
408 struct io_kiocb *req;
409 struct hrtimer timer;
410 struct timespec64 ts;
411 enum hrtimer_mode mode;
412};
413
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700414struct io_accept {
415 struct file *file;
416 struct sockaddr __user *addr;
417 int __user *addr_len;
418 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600419 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700420};
421
422struct io_sync {
423 struct file *file;
424 loff_t len;
425 loff_t off;
426 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700427 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700428};
429
Jens Axboefbf23842019-12-17 18:45:56 -0700430struct io_cancel {
431 struct file *file;
432 u64 addr;
433};
434
Jens Axboeb29472e2019-12-17 18:50:29 -0700435struct io_timeout {
436 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300437 u32 off;
438 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300439 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700440};
441
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100442struct io_timeout_rem {
443 struct file *file;
444 u64 addr;
445};
446
Jens Axboe9adbd452019-12-20 08:45:55 -0700447struct io_rw {
448 /* NOTE: kiocb has the file as the first member, so don't do it here */
449 struct kiocb kiocb;
450 u64 addr;
451 u64 len;
452};
453
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700454struct io_connect {
455 struct file *file;
456 struct sockaddr __user *addr;
457 int addr_len;
458};
459
Jens Axboee47293f2019-12-20 08:58:21 -0700460struct io_sr_msg {
461 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700462 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300463 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700464 void __user *buf;
465 };
Jens Axboee47293f2019-12-20 08:58:21 -0700466 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700467 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700468 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700469 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700470};
471
Jens Axboe15b71ab2019-12-11 11:20:36 -0700472struct io_open {
473 struct file *file;
474 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700475 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700476 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600477 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700478};
479
Jens Axboe05f3fb32019-12-09 11:22:50 -0700480struct io_files_update {
481 struct file *file;
482 u64 arg;
483 u32 nr_args;
484 u32 offset;
485};
486
Jens Axboe4840e412019-12-25 22:03:45 -0700487struct io_fadvise {
488 struct file *file;
489 u64 offset;
490 u32 len;
491 u32 advice;
492};
493
Jens Axboec1ca7572019-12-25 22:18:28 -0700494struct io_madvise {
495 struct file *file;
496 u64 addr;
497 u32 len;
498 u32 advice;
499};
500
Jens Axboe3e4827b2020-01-08 15:18:09 -0700501struct io_epoll {
502 struct file *file;
503 int epfd;
504 int op;
505 int fd;
506 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700507};
508
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300509struct io_splice {
510 struct file *file_out;
511 struct file *file_in;
512 loff_t off_out;
513 loff_t off_in;
514 u64 len;
515 unsigned int flags;
516};
517
Jens Axboeddf0322d2020-02-23 16:41:33 -0700518struct io_provide_buf {
519 struct file *file;
520 __u64 addr;
521 __s32 len;
522 __u32 bgid;
523 __u16 nbufs;
524 __u16 bid;
525};
526
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700527struct io_statx {
528 struct file *file;
529 int dfd;
530 unsigned int mask;
531 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700532 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700533 struct statx __user *buffer;
534};
535
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300536struct io_completion {
537 struct file *file;
538 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300539 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300540};
541
Jens Axboef499a022019-12-02 16:28:46 -0700542struct io_async_connect {
543 struct sockaddr_storage address;
544};
545
Jens Axboe03b12302019-12-02 18:50:25 -0700546struct io_async_msghdr {
547 struct iovec fast_iov[UIO_FASTIOV];
548 struct iovec *iov;
549 struct sockaddr __user *uaddr;
550 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700551 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700552};
553
Jens Axboef67676d2019-12-02 11:03:47 -0700554struct io_async_rw {
555 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600556 const struct iovec *free_iovec;
557 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600558 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600559 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700560};
561
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562enum {
563 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
564 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
565 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
566 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
567 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700568 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300569
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300570 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300571 REQ_F_FAIL_LINK_BIT,
572 REQ_F_INFLIGHT_BIT,
573 REQ_F_CUR_POS_BIT,
574 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300576 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300577 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700578 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700579 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600580 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800581 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700582
583 /* not a real bit, just to check we're not overflowing the space */
584 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300585};
586
587enum {
588 /* ctx owns file */
589 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
590 /* drain existing IO first */
591 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
592 /* linked sqes */
593 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
594 /* doesn't sever on completion < 0 */
595 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
596 /* IOSQE_ASYNC */
597 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700598 /* IOSQE_BUFFER_SELECT */
599 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300600
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300601 /* head of a link */
602 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300603 /* fail rest of links */
604 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
605 /* on inflight list */
606 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
607 /* read/write uses file position */
608 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
609 /* must not punt to workers */
610 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300611 /* has linked timeout */
612 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300613 /* regular file */
614 REQ_F_ISREG = BIT(REQ_F_ISREG_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;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100647 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700648 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700649 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700650 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700651 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700652 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700653 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700654 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700655 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300656 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700657 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700658 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300659 /* use only after cleaning per-op data, see io_clean_op() */
660 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700661 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700662
Jens Axboee8c2bc12020-08-15 18:44:09 -0700663 /* opcode allocated if it needs to store data for async defer */
664 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700665 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800666 /* polled IO has completed */
667 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700668
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700669 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300670 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700671
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300672 struct io_ring_ctx *ctx;
673 unsigned int flags;
674 refcount_t refs;
675 struct task_struct *task;
676 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700677
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300678 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700679
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300680 /*
681 * 1. used with ctx->iopoll_list with reads/writes
682 * 2. to track reqs with ->files (see io_op_def::file_table)
683 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300684 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600685
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300686 struct percpu_ref *fixed_file_refs;
687 struct callback_head task_work;
688 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
689 struct hlist_node hash_node;
690 struct async_poll *apoll;
691 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700692};
693
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300694struct io_defer_entry {
695 struct list_head list;
696 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300697 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300698};
699
Jens Axboedef596e2019-01-09 08:59:42 -0700700#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700701
Jens Axboe013538b2020-06-22 09:29:15 -0600702struct io_comp_state {
703 unsigned int nr;
704 struct list_head list;
705 struct io_ring_ctx *ctx;
706};
707
Jens Axboe9a56a232019-01-09 09:06:50 -0700708struct io_submit_state {
709 struct blk_plug plug;
710
711 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700712 * io_kiocb alloc cache
713 */
714 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300715 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700716
717 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600718 * Batch completion logic
719 */
720 struct io_comp_state comp;
721
722 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700723 * File reference cache
724 */
725 struct file *file;
726 unsigned int fd;
727 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700728 unsigned int ios_left;
729};
730
Jens Axboed3656342019-12-18 09:50:26 -0700731struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700732 /* needs req->file assigned */
733 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600734 /* don't fail if file grab fails */
735 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700736 /* hash wq insertion if file is a regular file */
737 unsigned hash_reg_file : 1;
738 /* unbound wq insertion if file is a non-regular file */
739 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700740 /* opcode is not supported by this kernel */
741 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700742 /* set if opcode supports polled "wait" */
743 unsigned pollin : 1;
744 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700745 /* op supports buffer selection */
746 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700747 /* needs rlimit(RLIMIT_FSIZE) assigned */
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300748 unsigned needs_fsize : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700749 /* must always have async data allocated */
750 unsigned needs_async_data : 1;
751 /* size of async data needed, if any */
752 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600753 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700754};
755
Jens Axboe09186822020-10-13 15:01:40 -0600756static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300757 [IORING_OP_NOP] = {},
758 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700759 .needs_file = 1,
760 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700761 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700762 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700763 .needs_async_data = 1,
764 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600765 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700768 .needs_file = 1,
769 .hash_reg_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700771 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300772 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700773 .needs_async_data = 1,
774 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600775 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700776 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300777 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600779 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700780 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300781 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700782 .needs_file = 1,
783 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700784 .pollin = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700785 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600786 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700787 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300788 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700789 .needs_file = 1,
790 .hash_reg_file = 1,
791 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700792 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300793 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700794 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600795 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700798 .needs_file = 1,
799 .unbound_nonreg_file = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_POLL_REMOVE] = {},
802 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700803 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600804 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700805 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300806 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700807 .needs_file = 1,
808 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700809 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700810 .needs_async_data = 1,
811 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600812 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
813 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700814 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300815 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700816 .needs_file = 1,
817 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700818 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700819 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700820 .needs_async_data = 1,
821 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600822 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
823 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700824 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300825 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700826 .needs_async_data = 1,
827 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600828 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_TIMEOUT_REMOVE] = {},
831 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700832 .needs_file = 1,
833 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700834 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600835 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700836 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300837 [IORING_OP_ASYNC_CANCEL] = {},
838 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700839 .needs_async_data = 1,
840 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600841 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700842 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300843 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700844 .needs_file = 1,
845 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700846 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700847 .needs_async_data = 1,
848 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600849 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700850 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300851 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700852 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300853 .needs_fsize = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600854 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700855 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300856 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600857 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
858 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600861 .needs_file = 1,
862 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600863 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700864 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300865 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600866 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700867 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300868 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600869 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
870 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700871 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300872 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700873 .needs_file = 1,
874 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700875 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700876 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700877 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600878 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700879 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300880 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700881 .needs_file = 1,
882 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700883 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300884 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700885 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600886 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700889 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600890 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700891 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300892 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600893 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700894 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300895 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700896 .needs_file = 1,
897 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700898 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600899 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700900 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300901 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700902 .needs_file = 1,
903 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700904 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700905 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600906 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700907 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300908 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600909 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
910 IO_WQ_WORK_BLKCG,
Jens Axboecebdb982020-01-08 17:59:24 -0700911 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700912 [IORING_OP_EPOLL_CTL] = {
913 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600914 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700915 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300916 [IORING_OP_SPLICE] = {
917 .needs_file = 1,
918 .hash_reg_file = 1,
919 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600920 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700921 },
922 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700923 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300924 [IORING_OP_TEE] = {
925 .needs_file = 1,
926 .hash_reg_file = 1,
927 .unbound_nonreg_file = 1,
928 },
Jens Axboed3656342019-12-18 09:50:26 -0700929};
930
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700931enum io_mem_account {
932 ACCT_LOCKED,
933 ACCT_PINNED,
934};
935
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300936static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
937 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700938static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800939static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +0100940static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -0600941static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700942static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600943static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700944static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700945static int __io_sqe_files_update(struct io_ring_ctx *ctx,
946 struct io_uring_files_update *ip,
947 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300948static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +0100949static struct file *io_file_get(struct io_submit_state *state,
950 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +0300951static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600952static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600953
Jens Axboeb63534c2020-06-04 11:28:00 -0600954static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
955 struct iovec **iovec, struct iov_iter *iter,
956 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600957static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
958 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600959 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700960
961static struct kmem_cache *req_cachep;
962
Jens Axboe09186822020-10-13 15:01:40 -0600963static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700964
965struct sock *io_uring_get_socket(struct file *file)
966{
967#if defined(CONFIG_UNIX)
968 if (file->f_op == &io_uring_fops) {
969 struct io_ring_ctx *ctx = file->private_data;
970
971 return ctx->ring_sock->sk;
972 }
973#endif
974 return NULL;
975}
976EXPORT_SYMBOL(io_uring_get_socket);
977
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300978static inline void io_clean_op(struct io_kiocb *req)
979{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300980 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
981 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300982 __io_clean_op(req);
983}
984
Jens Axboe4349f302020-07-09 15:07:01 -0600985static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600986{
987 struct mm_struct *mm = current->mm;
988
989 if (mm) {
990 kthread_unuse_mm(mm);
991 mmput(mm);
992 }
993}
994
995static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
996{
997 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300998 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600999 !ctx->sqo_task->mm ||
1000 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -06001001 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -06001002 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -06001003 }
1004
1005 return 0;
1006}
1007
1008static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1009 struct io_kiocb *req)
1010{
Jens Axboe0f203762020-10-14 09:23:55 -06001011 if (!(io_op_defs[req->opcode].work_flags & IO_WQ_WORK_MM))
Jens Axboec40f6372020-06-25 15:39:59 -06001012 return 0;
1013 return __io_sq_thread_acquire_mm(ctx);
1014}
1015
Dennis Zhou91d8f512020-09-16 13:41:05 -07001016static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1017 struct cgroup_subsys_state **cur_css)
1018
1019{
1020#ifdef CONFIG_BLK_CGROUP
1021 /* puts the old one when swapping */
1022 if (*cur_css != ctx->sqo_blkcg_css) {
1023 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1024 *cur_css = ctx->sqo_blkcg_css;
1025 }
1026#endif
1027}
1028
1029static void io_sq_thread_unassociate_blkcg(void)
1030{
1031#ifdef CONFIG_BLK_CGROUP
1032 kthread_associate_blkcg(NULL);
1033#endif
1034}
1035
Jens Axboec40f6372020-06-25 15:39:59 -06001036static inline void req_set_fail_links(struct io_kiocb *req)
1037{
1038 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1039 req->flags |= REQ_F_FAIL_LINK;
1040}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001041
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001042/*
1043 * Note: must call io_req_init_async() for the first time you
1044 * touch any members of io_wq_work.
1045 */
1046static inline void io_req_init_async(struct io_kiocb *req)
1047{
1048 if (req->flags & REQ_F_WORK_INITIALIZED)
1049 return;
1050
1051 memset(&req->work, 0, sizeof(req->work));
1052 req->flags |= REQ_F_WORK_INITIALIZED;
1053}
1054
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001055static inline bool io_async_submit(struct io_ring_ctx *ctx)
1056{
1057 return ctx->flags & IORING_SETUP_SQPOLL;
1058}
1059
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1061{
1062 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1063
Jens Axboe0f158b42020-05-14 17:18:39 -06001064 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001065}
1066
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001067static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1068{
1069 return !req->timeout.off;
1070}
1071
Jens Axboe2b188cc2019-01-07 10:46:33 -07001072static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1073{
1074 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001075 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076
1077 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1078 if (!ctx)
1079 return NULL;
1080
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001081 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1082 if (!ctx->fallback_req)
1083 goto err;
1084
Jens Axboe78076bb2019-12-04 19:56:40 -07001085 /*
1086 * Use 5 bits less than the max cq entries, that should give us around
1087 * 32 entries per hash list if totally full and uniformly spread.
1088 */
1089 hash_bits = ilog2(p->cq_entries);
1090 hash_bits -= 5;
1091 if (hash_bits <= 0)
1092 hash_bits = 1;
1093 ctx->cancel_hash_bits = hash_bits;
1094 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1095 GFP_KERNEL);
1096 if (!ctx->cancel_hash)
1097 goto err;
1098 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1099
Roman Gushchin21482892019-05-07 10:01:48 -07001100 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001101 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1102 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001103
1104 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001105 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001106 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001107 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001108 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001109 init_completion(&ctx->ref_comp);
1110 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001111 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001112 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001113 mutex_init(&ctx->uring_lock);
1114 init_waitqueue_head(&ctx->wait);
1115 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001116 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001117 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001118 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001119 init_waitqueue_head(&ctx->inflight_wait);
1120 spin_lock_init(&ctx->inflight_lock);
1121 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001122 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1123 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001124 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001125err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001126 if (ctx->fallback_req)
1127 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001128 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001129 kfree(ctx);
1130 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001131}
1132
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001133static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001134{
Jens Axboe2bc99302020-07-09 09:43:27 -06001135 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1136 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001137
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001138 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001139 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001140 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001141
Bob Liu9d858b22019-11-13 18:06:25 +08001142 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001143}
1144
Jens Axboede0617e2019-04-06 21:51:27 -06001145static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146{
Hristo Venev75b28af2019-08-26 17:23:46 +00001147 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001148
Pavel Begunkov07910152020-01-17 03:52:46 +03001149 /* order cqe stores with ring update */
1150 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001151
Pavel Begunkov07910152020-01-17 03:52:46 +03001152 if (wq_has_sleeper(&ctx->cq_wait)) {
1153 wake_up_interruptible(&ctx->cq_wait);
1154 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001155 }
1156}
1157
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001158static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001159{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001160 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001161 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001162
1163 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001164
Jens Axboedfead8a2020-10-14 10:12:37 -06001165 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboecccf0ee2020-01-27 16:34:48 -07001166 mmdrop(req->work.mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001167 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001168 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001169#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001170 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Dennis Zhou91d8f512020-09-16 13:41:05 -07001171 css_put(req->work.blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001172 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001173 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001174#endif
1175 if (req->work.flags & IO_WQ_WORK_CREDS) {
1176 put_cred(req->work.creds);
1177 req->work.flags &= ~IO_WQ_WORK_CREDS;
1178 }
1179 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboeff002b32020-02-07 16:05:21 -07001180 struct fs_struct *fs = req->work.fs;
1181
1182 spin_lock(&req->work.fs->lock);
1183 if (--fs->users)
1184 fs = NULL;
1185 spin_unlock(&req->work.fs->lock);
1186 if (fs)
1187 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001188 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001189 }
Jens Axboe561fb042019-10-24 07:25:42 -06001190}
1191
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001192static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001193{
Jens Axboed3656342019-12-18 09:50:26 -07001194 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001195 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001196
Pavel Begunkov16d59802020-07-12 16:16:47 +03001197 io_req_init_async(req);
1198
Jens Axboed3656342019-12-18 09:50:26 -07001199 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001200 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001201 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001202 } else {
1203 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001204 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001205 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001206 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
Jens Axboe0f203762020-10-14 09:23:55 -06001207 (io_op_defs[req->opcode].work_flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov23329512020-10-10 18:34:06 +01001208 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1209 req->work.files = get_files_struct(current);
1210 get_nsproxy(current->nsproxy);
1211 req->work.nsproxy = current->nsproxy;
1212 req->flags |= REQ_F_INFLIGHT;
1213
1214 spin_lock_irq(&ctx->inflight_lock);
1215 list_add(&req->inflight_entry, &ctx->inflight_list);
1216 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboedfead8a2020-10-14 10:12:37 -06001217 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkov23329512020-10-10 18:34:06 +01001218 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001219 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1220 (def->work_flags & IO_WQ_WORK_MM)) {
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001221 mmgrab(current->mm);
1222 req->work.mm = current->mm;
Jens Axboedfead8a2020-10-14 10:12:37 -06001223 req->work.flags |= IO_WQ_WORK_MM;
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001224 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001225#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001226 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1227 (def->work_flags & IO_WQ_WORK_BLKCG)) {
Dennis Zhou91d8f512020-09-16 13:41:05 -07001228 rcu_read_lock();
1229 req->work.blkcg_css = blkcg_css();
1230 /*
1231 * This should be rare, either the cgroup is dying or the task
1232 * is moving cgroups. Just punt to root for the handful of ios.
1233 */
1234 if (!css_tryget_online(req->work.blkcg_css))
1235 req->work.blkcg_css = NULL;
Jens Axboedfead8a2020-10-14 10:12:37 -06001236 else
1237 req->work.flags |= IO_WQ_WORK_BLKCG;
Dennis Zhou91d8f512020-09-16 13:41:05 -07001238 rcu_read_unlock();
1239 }
1240#endif
Jens Axboedfead8a2020-10-14 10:12:37 -06001241 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001242 req->work.creds = get_current_cred();
Jens Axboedfead8a2020-10-14 10:12:37 -06001243 req->work.flags |= IO_WQ_WORK_CREDS;
1244 }
1245 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1246 (def->work_flags & IO_WQ_WORK_FS)) {
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001247 spin_lock(&current->fs->lock);
1248 if (!current->fs->in_exec) {
1249 req->work.fs = current->fs;
1250 req->work.fs->users++;
Jens Axboedfead8a2020-10-14 10:12:37 -06001251 req->work.flags |= IO_WQ_WORK_FS;
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001252 } else {
1253 req->work.flags |= IO_WQ_WORK_CANCEL;
1254 }
1255 spin_unlock(&current->fs->lock);
1256 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001257 if (def->needs_fsize)
1258 req->work.fsize = rlimit(RLIMIT_FSIZE);
1259 else
1260 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001261}
1262
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001263static void io_prep_async_link(struct io_kiocb *req)
1264{
1265 struct io_kiocb *cur;
1266
1267 io_prep_async_work(req);
1268 if (req->flags & REQ_F_LINK_HEAD)
1269 list_for_each_entry(cur, &req->link_list, link_list)
1270 io_prep_async_work(cur);
1271}
1272
Jens Axboe7271ef32020-08-10 09:55:22 -06001273static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001274{
Jackie Liua197f662019-11-08 08:09:12 -07001275 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001276 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001277
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001278 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1279 &req->work, req->flags);
1280 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001281 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001282}
1283
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001284static void io_queue_async_work(struct io_kiocb *req)
1285{
Jens Axboe7271ef32020-08-10 09:55:22 -06001286 struct io_kiocb *link;
1287
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001288 /* init ->work of the whole link before punting */
1289 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001290 link = __io_queue_async_work(req);
1291
1292 if (link)
1293 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001294}
1295
Jens Axboe5262f562019-09-17 12:26:57 -06001296static void io_kill_timeout(struct io_kiocb *req)
1297{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001298 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001299 int ret;
1300
Jens Axboee8c2bc12020-08-15 18:44:09 -07001301 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001302 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001303 atomic_set(&req->ctx->cq_timeouts,
1304 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001305 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001306 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001307 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001308 }
1309}
1310
Jens Axboef3606e32020-09-22 08:18:24 -06001311static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1312{
1313 struct io_ring_ctx *ctx = req->ctx;
1314
1315 if (!tsk || req->task == tsk)
1316 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001317 if (ctx->flags & IORING_SETUP_SQPOLL) {
1318 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1319 return true;
1320 }
Jens Axboef3606e32020-09-22 08:18:24 -06001321 return false;
1322}
1323
Jens Axboe76e1b642020-09-26 15:05:03 -06001324/*
1325 * Returns true if we found and killed one or more timeouts
1326 */
1327static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001328{
1329 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001330 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001331
1332 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001333 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001334 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001335 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001336 canceled++;
1337 }
Jens Axboef3606e32020-09-22 08:18:24 -06001338 }
Jens Axboe5262f562019-09-17 12:26:57 -06001339 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001340 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001341}
1342
Pavel Begunkov04518942020-05-26 20:34:05 +03001343static void __io_queue_deferred(struct io_ring_ctx *ctx)
1344{
1345 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001346 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1347 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001348 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001349
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001350 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001351 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001352 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001353 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001354 link = __io_queue_async_work(de->req);
1355 if (link) {
1356 __io_queue_linked_timeout(link);
1357 /* drop submission reference */
Pavel Begunkov216578e2020-10-13 09:44:00 +01001358 io_put_req_deferred(link, 1);
Jens Axboe7271ef32020-08-10 09:55:22 -06001359 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001360 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001361 } while (!list_empty(&ctx->defer_list));
1362}
1363
Pavel Begunkov360428f2020-05-30 14:54:17 +03001364static void io_flush_timeouts(struct io_ring_ctx *ctx)
1365{
1366 while (!list_empty(&ctx->timeout_list)) {
1367 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001368 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001369
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001370 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001371 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001372 if (req->timeout.target_seq != ctx->cached_cq_tail
1373 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001374 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001375
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001376 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001377 io_kill_timeout(req);
1378 }
1379}
1380
Jens Axboede0617e2019-04-06 21:51:27 -06001381static void io_commit_cqring(struct io_ring_ctx *ctx)
1382{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001383 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001384 __io_commit_cqring(ctx);
1385
Pavel Begunkov04518942020-05-26 20:34:05 +03001386 if (unlikely(!list_empty(&ctx->defer_list)))
1387 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001388}
1389
Jens Axboe90554202020-09-03 12:12:41 -06001390static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1391{
1392 struct io_rings *r = ctx->rings;
1393
1394 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1395}
1396
Jens Axboe2b188cc2019-01-07 10:46:33 -07001397static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1398{
Hristo Venev75b28af2019-08-26 17:23:46 +00001399 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400 unsigned tail;
1401
1402 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001403 /*
1404 * writes to the cq entry need to come after reading head; the
1405 * control dependency is enough as we're using WRITE_ONCE to
1406 * fill the cq entry
1407 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001408 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409 return NULL;
1410
1411 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001412 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001413}
1414
Jens Axboef2842ab2020-01-08 11:04:00 -07001415static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1416{
Jens Axboef0b493e2020-02-01 21:30:11 -07001417 if (!ctx->cq_ev_fd)
1418 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001419 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1420 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001421 if (!ctx->eventfd_async)
1422 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001423 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001424}
1425
Jens Axboeb41e9852020-02-17 09:52:41 -07001426static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001427{
1428 if (waitqueue_active(&ctx->wait))
1429 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001430 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1431 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001432 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001433 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001434}
1435
Pavel Begunkov46930142020-07-30 18:43:49 +03001436static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1437{
1438 if (list_empty(&ctx->cq_overflow_list)) {
1439 clear_bit(0, &ctx->sq_check_overflow);
1440 clear_bit(0, &ctx->cq_check_overflow);
1441 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1442 }
1443}
1444
Jens Axboee6c8aa92020-09-28 13:10:13 -06001445static inline bool io_match_files(struct io_kiocb *req,
1446 struct files_struct *files)
1447{
1448 if (!files)
1449 return true;
Jens Axboedfead8a2020-10-14 10:12:37 -06001450 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
1451 (req->work.flags & IO_WQ_WORK_FILES))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001452 return req->work.files == files;
1453 return false;
1454}
1455
Jens Axboec4a2ed72019-11-21 21:01:26 -07001456/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001457static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1458 struct task_struct *tsk,
1459 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001460{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001461 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001462 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001463 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001464 unsigned long flags;
1465 LIST_HEAD(list);
1466
1467 if (!force) {
1468 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001469 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001470 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1471 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001472 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001473 }
1474
1475 spin_lock_irqsave(&ctx->completion_lock, flags);
1476
1477 /* if force is set, the ring is going away. always drop after that */
1478 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001479 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001480
Jens Axboec4a2ed72019-11-21 21:01:26 -07001481 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001482 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1483 if (tsk && req->task != tsk)
1484 continue;
1485 if (!io_match_files(req, files))
1486 continue;
1487
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001488 cqe = io_get_cqring(ctx);
1489 if (!cqe && !force)
1490 break;
1491
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001492 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001493 if (cqe) {
1494 WRITE_ONCE(cqe->user_data, req->user_data);
1495 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001496 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001497 } else {
1498 WRITE_ONCE(ctx->rings->cq_overflow,
1499 atomic_inc_return(&ctx->cached_cq_overflow));
1500 }
1501 }
1502
1503 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001504 io_cqring_mark_overflow(ctx);
1505
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001506 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1507 io_cqring_ev_posted(ctx);
1508
1509 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001510 req = list_first_entry(&list, struct io_kiocb, compl.list);
1511 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001512 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001513 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001514
1515 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001516}
1517
Jens Axboebcda7ba2020-02-23 16:42:51 -07001518static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001519{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001520 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001521 struct io_uring_cqe *cqe;
1522
Jens Axboe78e19bb2019-11-06 15:21:34 -07001523 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001524
Jens Axboe2b188cc2019-01-07 10:46:33 -07001525 /*
1526 * If we can't get a cq entry, userspace overflowed the
1527 * submission (by quite a lot). Increment the overflow count in
1528 * the ring.
1529 */
1530 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001531 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001532 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001533 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001534 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001535 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1536 /*
1537 * If we're in ring overflow flush mode, or in task cancel mode,
1538 * then we cannot store the request for later flushing, we need
1539 * to drop it on the floor.
1540 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001541 WRITE_ONCE(ctx->rings->cq_overflow,
1542 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001543 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001544 if (list_empty(&ctx->cq_overflow_list)) {
1545 set_bit(0, &ctx->sq_check_overflow);
1546 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001547 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001548 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001549 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001550 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001551 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001552 refcount_inc(&req->refs);
1553 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001554 }
1555}
1556
Jens Axboebcda7ba2020-02-23 16:42:51 -07001557static void io_cqring_fill_event(struct io_kiocb *req, long res)
1558{
1559 __io_cqring_fill_event(req, res, 0);
1560}
1561
Jens Axboee1e16092020-06-22 09:17:17 -06001562static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001563{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001564 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001565 unsigned long flags;
1566
1567 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001568 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001569 io_commit_cqring(ctx);
1570 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1571
Jens Axboe8c838782019-03-12 15:48:16 -06001572 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001573}
1574
Jens Axboe229a7b62020-06-22 10:13:11 -06001575static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001576{
Jens Axboe229a7b62020-06-22 10:13:11 -06001577 struct io_ring_ctx *ctx = cs->ctx;
1578
1579 spin_lock_irq(&ctx->completion_lock);
1580 while (!list_empty(&cs->list)) {
1581 struct io_kiocb *req;
1582
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001583 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1584 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001585 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001586
1587 /*
1588 * io_free_req() doesn't care about completion_lock unless one
1589 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1590 * because of a potential deadlock with req->work.fs->lock
1591 */
1592 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1593 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001594 spin_unlock_irq(&ctx->completion_lock);
1595 io_put_req(req);
1596 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001597 } else {
1598 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001599 }
1600 }
1601 io_commit_cqring(ctx);
1602 spin_unlock_irq(&ctx->completion_lock);
1603
1604 io_cqring_ev_posted(ctx);
1605 cs->nr = 0;
1606}
1607
1608static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1609 struct io_comp_state *cs)
1610{
1611 if (!cs) {
1612 io_cqring_add_event(req, res, cflags);
1613 io_put_req(req);
1614 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001615 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001616 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001617 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001618 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001619 if (++cs->nr >= 32)
1620 io_submit_flush_completions(cs);
1621 }
Jens Axboee1e16092020-06-22 09:17:17 -06001622}
1623
1624static void io_req_complete(struct io_kiocb *req, long res)
1625{
Jens Axboe229a7b62020-06-22 10:13:11 -06001626 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001627}
1628
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001629static inline bool io_is_fallback_req(struct io_kiocb *req)
1630{
1631 return req == (struct io_kiocb *)
1632 ((unsigned long) req->ctx->fallback_req & ~1UL);
1633}
1634
1635static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1636{
1637 struct io_kiocb *req;
1638
1639 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001640 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001641 return req;
1642
1643 return NULL;
1644}
1645
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001646static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1647 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001648{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001649 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001650 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001651 size_t sz;
1652 int ret;
1653
1654 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001655 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1656
1657 /*
1658 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1659 * retry single alloc to be on the safe side.
1660 */
1661 if (unlikely(ret <= 0)) {
1662 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1663 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001664 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001665 ret = 1;
1666 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001667 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668 }
1669
Pavel Begunkov291b2822020-09-30 22:57:01 +03001670 state->free_reqs--;
1671 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001672fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001673 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674}
1675
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001676static inline void io_put_file(struct io_kiocb *req, struct file *file,
1677 bool fixed)
1678{
1679 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001680 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001681 else
1682 fput(file);
1683}
1684
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001685static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001687 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001688
Jens Axboee8c2bc12020-08-15 18:44:09 -07001689 if (req->async_data)
1690 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001691 if (req->file)
1692 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001693
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001694 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001695}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001696
Pavel Begunkov216578e2020-10-13 09:44:00 +01001697static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001698{
Pavel Begunkov216578e2020-10-13 09:44:00 +01001699 struct io_uring_task *tctx;
1700 struct io_ring_ctx *ctx;
1701
1702 io_dismantle_req(req);
1703 tctx = req->task->io_uring;
1704 ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001705
Jens Axboe0f212202020-09-13 13:09:39 -06001706 atomic_long_inc(&tctx->req_complete);
1707 if (tctx->in_idle)
1708 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001709 put_task_struct(req->task);
1710
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001711 if (likely(!io_is_fallback_req(req)))
1712 kmem_cache_free(req_cachep, req);
1713 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001714 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1715 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001716}
1717
Jackie Liua197f662019-11-08 08:09:12 -07001718static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001719{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001720 struct io_timeout_data *io = req->async_data;
Jackie Liua197f662019-11-08 08:09:12 -07001721 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001722 int ret;
1723
Jens Axboee8c2bc12020-08-15 18:44:09 -07001724 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001725 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001726 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001727 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001728 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001729 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07001730 return true;
1731 }
1732
1733 return false;
1734}
1735
Jens Axboeab0b6452020-06-30 08:43:15 -06001736static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001737{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001738 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001739 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001740
1741 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001742 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001743 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1744 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001745 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001746
1747 list_del_init(&link->link_list);
1748 wake_ev = io_link_cancel_timeout(link);
1749 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001750 return wake_ev;
1751}
1752
1753static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001754{
Jens Axboe2665abf2019-11-05 12:40:47 -07001755 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001756 unsigned long flags;
Jens Axboeab0b6452020-06-30 08:43:15 -06001757 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001758
Pavel Begunkov216578e2020-10-13 09:44:00 +01001759 spin_lock_irqsave(&ctx->completion_lock, flags);
1760 wake_ev = __io_kill_linked_timeout(req);
1761 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001762
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001763 if (wake_ev)
1764 io_cqring_ev_posted(ctx);
1765}
1766
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001767static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001768{
1769 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001770
Jens Axboe9e645e112019-05-10 16:07:28 -06001771 /*
1772 * The list should never be empty when we are called here. But could
1773 * potentially happen if the chain is messed up, check to be on the
1774 * safe side.
1775 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001776 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001777 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001778
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001779 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1780 list_del_init(&req->link_list);
1781 if (!list_empty(&nxt->link_list))
1782 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001783 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001784}
1785
1786/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001787 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001788 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001789static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001790{
Jens Axboe2665abf2019-11-05 12:40:47 -07001791 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001792
1793 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001794 struct io_kiocb *link = list_first_entry(&req->link_list,
1795 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001796
Pavel Begunkov44932332019-12-05 16:16:35 +03001797 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001798 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001799
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001800 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001801
1802 /*
1803 * It's ok to free under spinlock as they're not linked anymore,
1804 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
1805 * work.fs->lock.
1806 */
1807 if (link->flags & REQ_F_WORK_INITIALIZED)
1808 io_put_req_deferred(link, 2);
1809 else
1810 io_double_put_req(link);
Jens Axboe9e645e112019-05-10 16:07:28 -06001811 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001812
1813 io_commit_cqring(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001814}
1815
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001816static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001817{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001818 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001819 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001820
Pavel Begunkov216578e2020-10-13 09:44:00 +01001821 spin_lock_irqsave(&ctx->completion_lock, flags);
1822 __io_fail_links(req);
1823 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001824
Jens Axboe9e645e112019-05-10 16:07:28 -06001825 io_cqring_ev_posted(ctx);
1826}
1827
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001828static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001829{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001830 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001831 if (req->flags & REQ_F_LINK_TIMEOUT)
1832 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001833
Jens Axboe9e645e112019-05-10 16:07:28 -06001834 /*
1835 * If LINK is set, we have dependent requests in this chain. If we
1836 * didn't fail this request, queue the first one up, moving any other
1837 * dependencies to the next request. In case of failure, fail the rest
1838 * of the chain.
1839 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001840 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1841 return io_req_link_next(req);
1842 io_fail_links(req);
1843 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001844}
Jens Axboe2665abf2019-11-05 12:40:47 -07001845
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001846static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1847{
1848 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1849 return NULL;
1850 return __io_req_find_next(req);
1851}
1852
Jens Axboe87c43112020-09-30 21:00:14 -06001853static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001854{
1855 struct task_struct *tsk = req->task;
1856 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001857 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001858
Jens Axboe6200b0a2020-09-13 14:38:30 -06001859 if (tsk->flags & PF_EXITING)
1860 return -ESRCH;
1861
Jens Axboec2c4c832020-07-01 15:37:11 -06001862 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001863 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1864 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1865 * processing task_work. There's no reliable way to tell if TWA_RESUME
1866 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001867 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001868 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001869 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001870 notify = TWA_SIGNAL;
1871
Jens Axboe87c43112020-09-30 21:00:14 -06001872 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001873 if (!ret)
1874 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001875
Jens Axboec2c4c832020-07-01 15:37:11 -06001876 return ret;
1877}
1878
Jens Axboec40f6372020-06-25 15:39:59 -06001879static void __io_req_task_cancel(struct io_kiocb *req, int error)
1880{
1881 struct io_ring_ctx *ctx = req->ctx;
1882
1883 spin_lock_irq(&ctx->completion_lock);
1884 io_cqring_fill_event(req, error);
1885 io_commit_cqring(ctx);
1886 spin_unlock_irq(&ctx->completion_lock);
1887
1888 io_cqring_ev_posted(ctx);
1889 req_set_fail_links(req);
1890 io_double_put_req(req);
1891}
1892
1893static void io_req_task_cancel(struct callback_head *cb)
1894{
1895 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001896 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001897
1898 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001899 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001900}
1901
1902static void __io_req_task_submit(struct io_kiocb *req)
1903{
1904 struct io_ring_ctx *ctx = req->ctx;
1905
Jens Axboec40f6372020-06-25 15:39:59 -06001906 if (!__io_sq_thread_acquire_mm(ctx)) {
1907 mutex_lock(&ctx->uring_lock);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001908 __io_queue_sqe(req, NULL);
Jens Axboec40f6372020-06-25 15:39:59 -06001909 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001910 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001911 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001912 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001913}
1914
Jens Axboec40f6372020-06-25 15:39:59 -06001915static void io_req_task_submit(struct callback_head *cb)
1916{
1917 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001918 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001919
1920 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001921 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001922}
1923
1924static void io_req_task_queue(struct io_kiocb *req)
1925{
Jens Axboec40f6372020-06-25 15:39:59 -06001926 int ret;
1927
1928 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001929 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001930
Jens Axboe87c43112020-09-30 21:00:14 -06001931 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001932 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001933 struct task_struct *tsk;
1934
Jens Axboec40f6372020-06-25 15:39:59 -06001935 init_task_work(&req->task_work, io_req_task_cancel);
1936 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001937 task_work_add(tsk, &req->task_work, 0);
1938 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001939 }
Jens Axboec40f6372020-06-25 15:39:59 -06001940}
1941
Pavel Begunkovc3524382020-06-28 12:52:32 +03001942static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001943{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001944 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001945
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001946 if (nxt)
1947 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001948}
1949
Jens Axboe9e645e112019-05-10 16:07:28 -06001950static void io_free_req(struct io_kiocb *req)
1951{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001952 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001953 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001954}
1955
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001956struct req_batch {
1957 void *reqs[IO_IOPOLL_BATCH];
1958 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001959
1960 struct task_struct *task;
1961 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001962};
1963
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001964static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001965{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001966 rb->to_free = 0;
1967 rb->task_refs = 0;
1968 rb->task = NULL;
1969}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001970
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001971static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1972 struct req_batch *rb)
1973{
1974 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1975 percpu_ref_put_many(&ctx->refs, rb->to_free);
1976 rb->to_free = 0;
1977}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001978
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001979static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1980 struct req_batch *rb)
1981{
1982 if (rb->to_free)
1983 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001984 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001985 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001986 put_task_struct_many(rb->task, rb->task_refs);
1987 rb->task = NULL;
1988 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001989}
1990
1991static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1992{
1993 if (unlikely(io_is_fallback_req(req))) {
1994 io_free_req(req);
1995 return;
1996 }
1997 if (req->flags & REQ_F_LINK_HEAD)
1998 io_queue_next(req);
1999
Jens Axboee3bc8e92020-09-24 08:45:57 -06002000 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002001 if (rb->task) {
2002 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002003 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002004 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002005 rb->task = req->task;
2006 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002007 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002008 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002009
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002010 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002011 rb->reqs[rb->to_free++] = req;
2012 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2013 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002014}
2015
Jens Axboeba816ad2019-09-28 11:36:45 -06002016/*
2017 * Drop reference to request, return next in chain (if there is one) if this
2018 * was the last reference to this request.
2019 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002020static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002021{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002022 struct io_kiocb *nxt = NULL;
2023
Jens Axboe2a44f462020-02-25 13:25:41 -07002024 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002025 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002026 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002027 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002028 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002029}
2030
Jens Axboe2b188cc2019-01-07 10:46:33 -07002031static void io_put_req(struct io_kiocb *req)
2032{
Jens Axboedef596e2019-01-09 08:59:42 -07002033 if (refcount_dec_and_test(&req->refs))
2034 io_free_req(req);
2035}
2036
Pavel Begunkov216578e2020-10-13 09:44:00 +01002037static void io_put_req_deferred_cb(struct callback_head *cb)
2038{
2039 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2040
2041 io_free_req(req);
2042}
2043
2044static void io_free_req_deferred(struct io_kiocb *req)
2045{
2046 int ret;
2047
2048 init_task_work(&req->task_work, io_put_req_deferred_cb);
2049 ret = io_req_task_work_add(req, true);
2050 if (unlikely(ret)) {
2051 struct task_struct *tsk;
2052
2053 tsk = io_wq_get_task(req->ctx->io_wq);
2054 task_work_add(tsk, &req->task_work, 0);
2055 wake_up_process(tsk);
2056 }
2057}
2058
2059static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2060{
2061 if (refcount_sub_and_test(refs, &req->refs))
2062 io_free_req_deferred(req);
2063}
2064
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002065static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002066{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002067 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002068
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002069 /*
2070 * A ref is owned by io-wq in which context we're. So, if that's the
2071 * last one, it's safe to steal next work. False negatives are Ok,
2072 * it just will be re-punted async in io_put_work()
2073 */
2074 if (refcount_read(&req->refs) != 1)
2075 return NULL;
2076
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002077 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002078 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002079}
2080
Jens Axboe978db572019-11-14 22:39:04 -07002081static void io_double_put_req(struct io_kiocb *req)
2082{
2083 /* drop both submit and complete references */
2084 if (refcount_sub_and_test(2, &req->refs))
2085 io_free_req(req);
2086}
2087
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002088static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002089{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002090 struct io_rings *rings = ctx->rings;
2091
Jens Axboead3eb2c2019-12-18 17:12:20 -07002092 if (test_bit(0, &ctx->cq_check_overflow)) {
2093 /*
2094 * noflush == true is from the waitqueue handler, just ensure
2095 * we wake up the task, and the next invocation will flush the
2096 * entries. We cannot safely to it from here.
2097 */
2098 if (noflush && !list_empty(&ctx->cq_overflow_list))
2099 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002100
Jens Axboee6c8aa92020-09-28 13:10:13 -06002101 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002102 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002103
Jens Axboea3a0e432019-08-20 11:03:11 -06002104 /* See comment at the top of this file */
2105 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002106 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002107}
2108
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002109static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2110{
2111 struct io_rings *rings = ctx->rings;
2112
2113 /* make sure SQ entry isn't read before tail */
2114 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2115}
2116
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002117static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002118{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002119 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002120
Jens Axboebcda7ba2020-02-23 16:42:51 -07002121 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2122 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002123 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002124 kfree(kbuf);
2125 return cflags;
2126}
2127
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002128static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2129{
2130 struct io_buffer *kbuf;
2131
2132 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2133 return io_put_kbuf(req, kbuf);
2134}
2135
Jens Axboe4c6e2772020-07-01 11:29:10 -06002136static inline bool io_run_task_work(void)
2137{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002138 /*
2139 * Not safe to run on exiting task, and the task_work handling will
2140 * not add work to such a task.
2141 */
2142 if (unlikely(current->flags & PF_EXITING))
2143 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002144 if (current->task_works) {
2145 __set_current_state(TASK_RUNNING);
2146 task_work_run();
2147 return true;
2148 }
2149
2150 return false;
2151}
2152
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002153static void io_iopoll_queue(struct list_head *again)
2154{
2155 struct io_kiocb *req;
2156
2157 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002158 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2159 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002160 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002161 } while (!list_empty(again));
2162}
2163
Jens Axboedef596e2019-01-09 08:59:42 -07002164/*
2165 * Find and free completed poll iocbs
2166 */
2167static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2168 struct list_head *done)
2169{
Jens Axboe8237e042019-12-28 10:48:22 -07002170 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002171 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002172 LIST_HEAD(again);
2173
2174 /* order with ->result store in io_complete_rw_iopoll() */
2175 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002176
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002177 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002178 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002179 int cflags = 0;
2180
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002181 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002182 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002183 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002184 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002185 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002186 continue;
2187 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002188 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002189
Jens Axboebcda7ba2020-02-23 16:42:51 -07002190 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002191 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002192
2193 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002194 (*nr_events)++;
2195
Pavel Begunkovc3524382020-06-28 12:52:32 +03002196 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002197 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002198 }
Jens Axboedef596e2019-01-09 08:59:42 -07002199
Jens Axboe09bb8392019-03-13 12:39:28 -06002200 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002201 if (ctx->flags & IORING_SETUP_SQPOLL)
2202 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002203 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002204
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002205 if (!list_empty(&again))
2206 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002207}
2208
Jens Axboedef596e2019-01-09 08:59:42 -07002209static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2210 long min)
2211{
2212 struct io_kiocb *req, *tmp;
2213 LIST_HEAD(done);
2214 bool spin;
2215 int ret;
2216
2217 /*
2218 * Only spin for completions if we don't have multiple devices hanging
2219 * off our complete list, and we're under the requested amount.
2220 */
2221 spin = !ctx->poll_multi_file && *nr_events < min;
2222
2223 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002224 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002225 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002226
2227 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002228 * Move completed and retryable entries to our local lists.
2229 * If we find a request that requires polling, break out
2230 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002231 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002232 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002233 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002234 continue;
2235 }
2236 if (!list_empty(&done))
2237 break;
2238
2239 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2240 if (ret < 0)
2241 break;
2242
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002243 /* iopoll may have completed current req */
2244 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002245 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002246
Jens Axboedef596e2019-01-09 08:59:42 -07002247 if (ret && spin)
2248 spin = false;
2249 ret = 0;
2250 }
2251
2252 if (!list_empty(&done))
2253 io_iopoll_complete(ctx, nr_events, &done);
2254
2255 return ret;
2256}
2257
2258/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002259 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002260 * non-spinning poll check - we'll still enter the driver poll loop, but only
2261 * as a non-spinning completion check.
2262 */
2263static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2264 long min)
2265{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002266 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002267 int ret;
2268
2269 ret = io_do_iopoll(ctx, nr_events, min);
2270 if (ret < 0)
2271 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002272 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002273 return 0;
2274 }
2275
2276 return 1;
2277}
2278
2279/*
2280 * We can't just wait for polled events to come to us, we have to actively
2281 * find and complete them.
2282 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002283static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002284{
2285 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2286 return;
2287
2288 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002289 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002290 unsigned int nr_events = 0;
2291
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002292 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002293
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002294 /* let it sleep and repeat later if can't complete a request */
2295 if (nr_events == 0)
2296 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002297 /*
2298 * Ensure we allow local-to-the-cpu processing to take place,
2299 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002300 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002301 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002302 if (need_resched()) {
2303 mutex_unlock(&ctx->uring_lock);
2304 cond_resched();
2305 mutex_lock(&ctx->uring_lock);
2306 }
Jens Axboedef596e2019-01-09 08:59:42 -07002307 }
2308 mutex_unlock(&ctx->uring_lock);
2309}
2310
Pavel Begunkov7668b922020-07-07 16:36:21 +03002311static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002312{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002313 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002314 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002315
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002316 /*
2317 * We disallow the app entering submit/complete with polling, but we
2318 * still need to lock the ring to prevent racing with polled issue
2319 * that got punted to a workqueue.
2320 */
2321 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002322 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002323 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002324 * Don't enter poll loop if we already have events pending.
2325 * If we do, we can potentially be spinning for commands that
2326 * already triggered a CQE (eg in error).
2327 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002328 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002329 break;
2330
2331 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002332 * If a submit got punted to a workqueue, we can have the
2333 * application entering polling for a command before it gets
2334 * issued. That app will hold the uring_lock for the duration
2335 * of the poll right here, so we need to take a breather every
2336 * now and then to ensure that the issue has a chance to add
2337 * the poll to the issued list. Otherwise we can spin here
2338 * forever, while the workqueue is stuck trying to acquire the
2339 * very same mutex.
2340 */
2341 if (!(++iters & 7)) {
2342 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002343 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002344 mutex_lock(&ctx->uring_lock);
2345 }
2346
Pavel Begunkov7668b922020-07-07 16:36:21 +03002347 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002348 if (ret <= 0)
2349 break;
2350 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002351 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002352
Jens Axboe500f9fb2019-08-19 12:15:59 -06002353 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002354 return ret;
2355}
2356
Jens Axboe491381ce2019-10-17 09:20:46 -06002357static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002358{
Jens Axboe491381ce2019-10-17 09:20:46 -06002359 /*
2360 * Tell lockdep we inherited freeze protection from submission
2361 * thread.
2362 */
2363 if (req->flags & REQ_F_ISREG) {
2364 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002365
Jens Axboe491381ce2019-10-17 09:20:46 -06002366 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002368 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002369}
2370
Jens Axboea1d7c392020-06-22 11:09:46 -06002371static void io_complete_rw_common(struct kiocb *kiocb, long res,
2372 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002373{
Jens Axboe9adbd452019-12-20 08:45:55 -07002374 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002375 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002376
Jens Axboe491381ce2019-10-17 09:20:46 -06002377 if (kiocb->ki_flags & IOCB_WRITE)
2378 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002379
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002380 if (res != req->result)
2381 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002382 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002383 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002384 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002385}
2386
Jens Axboeb63534c2020-06-04 11:28:00 -06002387#ifdef CONFIG_BLOCK
2388static bool io_resubmit_prep(struct io_kiocb *req, int error)
2389{
2390 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2391 ssize_t ret = -ECANCELED;
2392 struct iov_iter iter;
2393 int rw;
2394
2395 if (error) {
2396 ret = error;
2397 goto end_req;
2398 }
2399
2400 switch (req->opcode) {
2401 case IORING_OP_READV:
2402 case IORING_OP_READ_FIXED:
2403 case IORING_OP_READ:
2404 rw = READ;
2405 break;
2406 case IORING_OP_WRITEV:
2407 case IORING_OP_WRITE_FIXED:
2408 case IORING_OP_WRITE:
2409 rw = WRITE;
2410 break;
2411 default:
2412 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2413 req->opcode);
2414 goto end_req;
2415 }
2416
Jens Axboee8c2bc12020-08-15 18:44:09 -07002417 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002418 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2419 if (ret < 0)
2420 goto end_req;
2421 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2422 if (!ret)
2423 return true;
2424 kfree(iovec);
2425 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002426 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002427 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002428end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002429 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002430 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002431 return false;
2432}
Jens Axboeb63534c2020-06-04 11:28:00 -06002433#endif
2434
2435static bool io_rw_reissue(struct io_kiocb *req, long res)
2436{
2437#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002438 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002439 int ret;
2440
Jens Axboe355afae2020-09-02 09:30:31 -06002441 if (!S_ISBLK(mode) && !S_ISREG(mode))
2442 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002443 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2444 return false;
2445
Jens Axboefdee9462020-08-27 16:46:24 -06002446 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002447
Jens Axboefdee9462020-08-27 16:46:24 -06002448 if (io_resubmit_prep(req, ret)) {
2449 refcount_inc(&req->refs);
2450 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002451 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002452 }
2453
Jens Axboeb63534c2020-06-04 11:28:00 -06002454#endif
2455 return false;
2456}
2457
Jens Axboea1d7c392020-06-22 11:09:46 -06002458static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2459 struct io_comp_state *cs)
2460{
2461 if (!io_rw_reissue(req, res))
2462 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002463}
2464
2465static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2466{
Jens Axboe9adbd452019-12-20 08:45:55 -07002467 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002468
Jens Axboea1d7c392020-06-22 11:09:46 -06002469 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002470}
2471
Jens Axboedef596e2019-01-09 08:59:42 -07002472static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2473{
Jens Axboe9adbd452019-12-20 08:45:55 -07002474 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002475
Jens Axboe491381ce2019-10-17 09:20:46 -06002476 if (kiocb->ki_flags & IOCB_WRITE)
2477 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002478
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002479 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002480 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002481
2482 WRITE_ONCE(req->result, res);
2483 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002484 smp_wmb();
2485 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002486}
2487
2488/*
2489 * After the iocb has been issued, it's safe to be found on the poll list.
2490 * Adding the kiocb to the list AFTER submission ensures that we don't
2491 * find it from a io_iopoll_getevents() thread before the issuer is done
2492 * accessing the kiocb cookie.
2493 */
2494static void io_iopoll_req_issued(struct io_kiocb *req)
2495{
2496 struct io_ring_ctx *ctx = req->ctx;
2497
2498 /*
2499 * Track whether we have multiple files in our lists. This will impact
2500 * how we do polling eventually, not spinning if we're on potentially
2501 * different devices.
2502 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002503 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002504 ctx->poll_multi_file = false;
2505 } else if (!ctx->poll_multi_file) {
2506 struct io_kiocb *list_req;
2507
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002508 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002509 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002510 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002511 ctx->poll_multi_file = true;
2512 }
2513
2514 /*
2515 * For fast devices, IO may have already completed. If it has, add
2516 * it to the front so we find it first.
2517 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002518 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002519 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002520 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002521 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002522
2523 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002524 wq_has_sleeper(&ctx->sq_data->wait))
2525 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002526}
2527
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002528static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002529{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002530 if (state->has_refs)
2531 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002532 state->file = NULL;
2533}
2534
2535static inline void io_state_file_put(struct io_submit_state *state)
2536{
2537 if (state->file)
2538 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002539}
2540
2541/*
2542 * Get as many references to a file as we have IOs left in this submission,
2543 * assuming most submissions are for one file, or at least that each file
2544 * has more than one submission.
2545 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002546static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002547{
2548 if (!state)
2549 return fget(fd);
2550
2551 if (state->file) {
2552 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002553 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002554 return state->file;
2555 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002556 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002557 }
2558 state->file = fget_many(fd, state->ios_left);
2559 if (!state->file)
2560 return NULL;
2561
2562 state->fd = fd;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01002563 state->has_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002564 return state->file;
2565}
2566
Jens Axboe4503b762020-06-01 10:00:27 -06002567static bool io_bdev_nowait(struct block_device *bdev)
2568{
2569#ifdef CONFIG_BLOCK
2570 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2571#else
2572 return true;
2573#endif
2574}
2575
Jens Axboe2b188cc2019-01-07 10:46:33 -07002576/*
2577 * If we tracked the file through the SCM inflight mechanism, we could support
2578 * any file. For now, just ensure that anything potentially problematic is done
2579 * inline.
2580 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002581static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002582{
2583 umode_t mode = file_inode(file)->i_mode;
2584
Jens Axboe4503b762020-06-01 10:00:27 -06002585 if (S_ISBLK(mode)) {
2586 if (io_bdev_nowait(file->f_inode->i_bdev))
2587 return true;
2588 return false;
2589 }
2590 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002591 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002592 if (S_ISREG(mode)) {
2593 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2594 file->f_op != &io_uring_fops)
2595 return true;
2596 return false;
2597 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002598
Jens Axboec5b85622020-06-09 19:23:05 -06002599 /* any ->read/write should understand O_NONBLOCK */
2600 if (file->f_flags & O_NONBLOCK)
2601 return true;
2602
Jens Axboeaf197f52020-04-28 13:15:06 -06002603 if (!(file->f_mode & FMODE_NOWAIT))
2604 return false;
2605
2606 if (rw == READ)
2607 return file->f_op->read_iter != NULL;
2608
2609 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002610}
2611
Pavel Begunkova88fc402020-09-30 22:57:53 +03002612static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002613{
Jens Axboedef596e2019-01-09 08:59:42 -07002614 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002615 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002616 unsigned ioprio;
2617 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002618
Jens Axboe491381ce2019-10-17 09:20:46 -06002619 if (S_ISREG(file_inode(req->file)->i_mode))
2620 req->flags |= REQ_F_ISREG;
2621
Jens Axboe2b188cc2019-01-07 10:46:33 -07002622 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002623 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2624 req->flags |= REQ_F_CUR_POS;
2625 kiocb->ki_pos = req->file->f_pos;
2626 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002627 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002628 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2629 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2630 if (unlikely(ret))
2631 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002632
2633 ioprio = READ_ONCE(sqe->ioprio);
2634 if (ioprio) {
2635 ret = ioprio_check_cap(ioprio);
2636 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002637 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002638
2639 kiocb->ki_ioprio = ioprio;
2640 } else
2641 kiocb->ki_ioprio = get_current_ioprio();
2642
Stefan Bühler8449eed2019-04-27 20:34:19 +02002643 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002644 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002645 req->flags |= REQ_F_NOWAIT;
2646
Jens Axboedef596e2019-01-09 08:59:42 -07002647 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002648 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2649 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002650 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002651
Jens Axboedef596e2019-01-09 08:59:42 -07002652 kiocb->ki_flags |= IOCB_HIPRI;
2653 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002654 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002655 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002656 if (kiocb->ki_flags & IOCB_HIPRI)
2657 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002658 kiocb->ki_complete = io_complete_rw;
2659 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002660
Jens Axboe3529d8c2019-12-19 18:24:38 -07002661 req->rw.addr = READ_ONCE(sqe->addr);
2662 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002663 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002664 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002665}
2666
2667static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2668{
2669 switch (ret) {
2670 case -EIOCBQUEUED:
2671 break;
2672 case -ERESTARTSYS:
2673 case -ERESTARTNOINTR:
2674 case -ERESTARTNOHAND:
2675 case -ERESTART_RESTARTBLOCK:
2676 /*
2677 * We can't just restart the syscall, since previously
2678 * submitted sqes may already be in progress. Just fail this
2679 * IO with EINTR.
2680 */
2681 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002682 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683 default:
2684 kiocb->ki_complete(kiocb, ret, 0);
2685 }
2686}
2687
Jens Axboea1d7c392020-06-22 11:09:46 -06002688static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2689 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002690{
Jens Axboeba042912019-12-25 16:33:42 -07002691 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002692 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002693
Jens Axboe227c0c92020-08-13 11:51:40 -06002694 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002695 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002696 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002697 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002698 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002699 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002700 }
2701
Jens Axboeba042912019-12-25 16:33:42 -07002702 if (req->flags & REQ_F_CUR_POS)
2703 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002704 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002705 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002706 else
2707 io_rw_done(kiocb, ret);
2708}
2709
Jens Axboe9adbd452019-12-20 08:45:55 -07002710static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002711 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002712{
Jens Axboe9adbd452019-12-20 08:45:55 -07002713 struct io_ring_ctx *ctx = req->ctx;
2714 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002715 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002716 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002717 size_t offset;
2718 u64 buf_addr;
2719
Jens Axboeedafcce2019-01-09 09:16:05 -07002720 if (unlikely(buf_index >= ctx->nr_user_bufs))
2721 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002722 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2723 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002724 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002725
2726 /* overflow */
2727 if (buf_addr + len < buf_addr)
2728 return -EFAULT;
2729 /* not inside the mapped region */
2730 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2731 return -EFAULT;
2732
2733 /*
2734 * May not be a start of buffer, set size appropriately
2735 * and advance us to the beginning.
2736 */
2737 offset = buf_addr - imu->ubuf;
2738 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002739
2740 if (offset) {
2741 /*
2742 * Don't use iov_iter_advance() here, as it's really slow for
2743 * using the latter parts of a big fixed buffer - it iterates
2744 * over each segment manually. We can cheat a bit here, because
2745 * we know that:
2746 *
2747 * 1) it's a BVEC iter, we set it up
2748 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2749 * first and last bvec
2750 *
2751 * So just find our index, and adjust the iterator afterwards.
2752 * If the offset is within the first bvec (or the whole first
2753 * bvec, just use iov_iter_advance(). This makes it easier
2754 * since we can just skip the first segment, which may not
2755 * be PAGE_SIZE aligned.
2756 */
2757 const struct bio_vec *bvec = imu->bvec;
2758
2759 if (offset <= bvec->bv_len) {
2760 iov_iter_advance(iter, offset);
2761 } else {
2762 unsigned long seg_skip;
2763
2764 /* skip first vec */
2765 offset -= bvec->bv_len;
2766 seg_skip = 1 + (offset >> PAGE_SHIFT);
2767
2768 iter->bvec = bvec + seg_skip;
2769 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002770 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002771 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002772 }
2773 }
2774
Jens Axboe5e559562019-11-13 16:12:46 -07002775 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002776}
2777
Jens Axboebcda7ba2020-02-23 16:42:51 -07002778static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2779{
2780 if (needs_lock)
2781 mutex_unlock(&ctx->uring_lock);
2782}
2783
2784static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2785{
2786 /*
2787 * "Normal" inline submissions always hold the uring_lock, since we
2788 * grab it from the system call. Same is true for the SQPOLL offload.
2789 * The only exception is when we've detached the request and issue it
2790 * from an async worker thread, grab the lock for that case.
2791 */
2792 if (needs_lock)
2793 mutex_lock(&ctx->uring_lock);
2794}
2795
2796static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2797 int bgid, struct io_buffer *kbuf,
2798 bool needs_lock)
2799{
2800 struct io_buffer *head;
2801
2802 if (req->flags & REQ_F_BUFFER_SELECTED)
2803 return kbuf;
2804
2805 io_ring_submit_lock(req->ctx, needs_lock);
2806
2807 lockdep_assert_held(&req->ctx->uring_lock);
2808
2809 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2810 if (head) {
2811 if (!list_empty(&head->list)) {
2812 kbuf = list_last_entry(&head->list, struct io_buffer,
2813 list);
2814 list_del(&kbuf->list);
2815 } else {
2816 kbuf = head;
2817 idr_remove(&req->ctx->io_buffer_idr, bgid);
2818 }
2819 if (*len > kbuf->len)
2820 *len = kbuf->len;
2821 } else {
2822 kbuf = ERR_PTR(-ENOBUFS);
2823 }
2824
2825 io_ring_submit_unlock(req->ctx, needs_lock);
2826
2827 return kbuf;
2828}
2829
Jens Axboe4d954c22020-02-27 07:31:19 -07002830static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2831 bool needs_lock)
2832{
2833 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002834 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002835
2836 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002837 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002838 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2839 if (IS_ERR(kbuf))
2840 return kbuf;
2841 req->rw.addr = (u64) (unsigned long) kbuf;
2842 req->flags |= REQ_F_BUFFER_SELECTED;
2843 return u64_to_user_ptr(kbuf->addr);
2844}
2845
2846#ifdef CONFIG_COMPAT
2847static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2848 bool needs_lock)
2849{
2850 struct compat_iovec __user *uiov;
2851 compat_ssize_t clen;
2852 void __user *buf;
2853 ssize_t len;
2854
2855 uiov = u64_to_user_ptr(req->rw.addr);
2856 if (!access_ok(uiov, sizeof(*uiov)))
2857 return -EFAULT;
2858 if (__get_user(clen, &uiov->iov_len))
2859 return -EFAULT;
2860 if (clen < 0)
2861 return -EINVAL;
2862
2863 len = clen;
2864 buf = io_rw_buffer_select(req, &len, needs_lock);
2865 if (IS_ERR(buf))
2866 return PTR_ERR(buf);
2867 iov[0].iov_base = buf;
2868 iov[0].iov_len = (compat_size_t) len;
2869 return 0;
2870}
2871#endif
2872
2873static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2874 bool needs_lock)
2875{
2876 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2877 void __user *buf;
2878 ssize_t len;
2879
2880 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2881 return -EFAULT;
2882
2883 len = iov[0].iov_len;
2884 if (len < 0)
2885 return -EINVAL;
2886 buf = io_rw_buffer_select(req, &len, needs_lock);
2887 if (IS_ERR(buf))
2888 return PTR_ERR(buf);
2889 iov[0].iov_base = buf;
2890 iov[0].iov_len = len;
2891 return 0;
2892}
2893
2894static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2895 bool needs_lock)
2896{
Jens Axboedddb3e22020-06-04 11:27:01 -06002897 if (req->flags & REQ_F_BUFFER_SELECTED) {
2898 struct io_buffer *kbuf;
2899
2900 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2901 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2902 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002903 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002904 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002905 if (!req->rw.len)
2906 return 0;
2907 else if (req->rw.len > 1)
2908 return -EINVAL;
2909
2910#ifdef CONFIG_COMPAT
2911 if (req->ctx->compat)
2912 return io_compat_import(req, iov, needs_lock);
2913#endif
2914
2915 return __io_iov_buffer_select(req, iov, needs_lock);
2916}
2917
Jens Axboe8452fd02020-08-18 13:58:33 -07002918static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2919 struct iovec **iovec, struct iov_iter *iter,
2920 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921{
Jens Axboe9adbd452019-12-20 08:45:55 -07002922 void __user *buf = u64_to_user_ptr(req->rw.addr);
2923 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002924 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002925 u8 opcode;
2926
Jens Axboed625c6e2019-12-17 19:53:05 -07002927 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002928 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002929 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002930 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002931 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002932
Jens Axboebcda7ba2020-02-23 16:42:51 -07002933 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002934 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002935 return -EINVAL;
2936
Jens Axboe3a6820f2019-12-22 15:19:35 -07002937 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002938 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002939 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002940 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002941 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002942 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002943 }
2944
Jens Axboe3a6820f2019-12-22 15:19:35 -07002945 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2946 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002947 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002948 }
2949
Jens Axboe4d954c22020-02-27 07:31:19 -07002950 if (req->flags & REQ_F_BUFFER_SELECT) {
2951 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002952 if (!ret) {
2953 ret = (*iovec)->iov_len;
2954 iov_iter_init(iter, rw, *iovec, 1, ret);
2955 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002956 *iovec = NULL;
2957 return ret;
2958 }
2959
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002960 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
2961 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002962}
2963
Jens Axboe8452fd02020-08-18 13:58:33 -07002964static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2965 struct iovec **iovec, struct iov_iter *iter,
2966 bool needs_lock)
2967{
Jens Axboee8c2bc12020-08-15 18:44:09 -07002968 struct io_async_rw *iorw = req->async_data;
2969
2970 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07002971 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2972 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07002973 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07002974}
2975
Jens Axboe0fef9482020-08-26 10:36:20 -06002976static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2977{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03002978 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06002979}
2980
Jens Axboe32960612019-09-23 11:05:34 -06002981/*
2982 * For files that don't have ->read_iter() and ->write_iter(), handle them
2983 * by looping over ->read() or ->write() manually.
2984 */
2985static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2986 struct iov_iter *iter)
2987{
2988 ssize_t ret = 0;
2989
2990 /*
2991 * Don't support polled IO through this interface, and we can't
2992 * support non-blocking either. For the latter, this just causes
2993 * the kiocb to be handled from an async context.
2994 */
2995 if (kiocb->ki_flags & IOCB_HIPRI)
2996 return -EOPNOTSUPP;
2997 if (kiocb->ki_flags & IOCB_NOWAIT)
2998 return -EAGAIN;
2999
3000 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003001 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003002 ssize_t nr;
3003
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003004 if (!iov_iter_is_bvec(iter)) {
3005 iovec = iov_iter_iovec(iter);
3006 } else {
3007 /* fixed buffers import bvec */
3008 iovec.iov_base = kmap(iter->bvec->bv_page)
3009 + iter->iov_offset;
3010 iovec.iov_len = min(iter->count,
3011 iter->bvec->bv_len - iter->iov_offset);
3012 }
3013
Jens Axboe32960612019-09-23 11:05:34 -06003014 if (rw == READ) {
3015 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003016 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003017 } else {
3018 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003019 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003020 }
3021
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003022 if (iov_iter_is_bvec(iter))
3023 kunmap(iter->bvec->bv_page);
3024
Jens Axboe32960612019-09-23 11:05:34 -06003025 if (nr < 0) {
3026 if (!ret)
3027 ret = nr;
3028 break;
3029 }
3030 ret += nr;
3031 if (nr != iovec.iov_len)
3032 break;
3033 iov_iter_advance(iter, nr);
3034 }
3035
3036 return ret;
3037}
3038
Jens Axboeff6165b2020-08-13 09:47:43 -06003039static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3040 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003041{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003042 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003043
Jens Axboeff6165b2020-08-13 09:47:43 -06003044 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003045 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003046 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003047 /* can only be fixed buffers, no need to do anything */
3048 if (iter->type == ITER_BVEC)
3049 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003050 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003051 unsigned iov_off = 0;
3052
3053 rw->iter.iov = rw->fast_iov;
3054 if (iter->iov != fast_iov) {
3055 iov_off = iter->iov - fast_iov;
3056 rw->iter.iov += iov_off;
3057 }
3058 if (rw->fast_iov != fast_iov)
3059 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003060 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003061 } else {
3062 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003063 }
3064}
3065
Jens Axboee8c2bc12020-08-15 18:44:09 -07003066static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003067{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003068 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3069 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3070 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003071}
3072
Jens Axboee8c2bc12020-08-15 18:44:09 -07003073static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003074{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003075 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003076 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003077
Jens Axboee8c2bc12020-08-15 18:44:09 -07003078 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003079}
3080
Jens Axboeff6165b2020-08-13 09:47:43 -06003081static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3082 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003083 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003084{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003085 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003086 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003087 if (!req->async_data) {
3088 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003089 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003090
Jens Axboeff6165b2020-08-13 09:47:43 -06003091 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003092 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003093 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003094}
3095
Pavel Begunkov73debe62020-09-30 22:57:54 +03003096static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003097{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003098 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003099 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003100 ssize_t ret;
3101
Pavel Begunkov73debe62020-09-30 22:57:54 +03003102 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003103 if (unlikely(ret < 0))
3104 return ret;
3105
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003106 iorw->bytes_done = 0;
3107 iorw->free_iovec = iov;
3108 if (iov)
3109 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003110 return 0;
3111}
3112
Pavel Begunkov73debe62020-09-30 22:57:54 +03003113static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003114{
3115 ssize_t ret;
3116
Pavel Begunkova88fc402020-09-30 22:57:53 +03003117 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003118 if (ret)
3119 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003120
Jens Axboe3529d8c2019-12-19 18:24:38 -07003121 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3122 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003123
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003124 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003125 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003126 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003127 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003128}
3129
Jens Axboec1dd91d2020-08-03 16:43:59 -06003130/*
3131 * This is our waitqueue callback handler, registered through lock_page_async()
3132 * when we initially tried to do the IO with the iocb armed our waitqueue.
3133 * This gets called when the page is unlocked, and we generally expect that to
3134 * happen when the page IO is completed and the page is now uptodate. This will
3135 * queue a task_work based retry of the operation, attempting to copy the data
3136 * again. If the latter fails because the page was NOT uptodate, then we will
3137 * do a thread based blocking retry of the operation. That's the unexpected
3138 * slow path.
3139 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003140static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3141 int sync, void *arg)
3142{
3143 struct wait_page_queue *wpq;
3144 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003145 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003146 int ret;
3147
3148 wpq = container_of(wait, struct wait_page_queue, wait);
3149
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003150 if (!wake_page_match(wpq, key))
3151 return 0;
3152
Hao Xuc8d317a2020-09-29 20:00:45 +08003153 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003154 list_del_init(&wait->entry);
3155
Pavel Begunkove7375122020-07-12 20:42:04 +03003156 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003157 percpu_ref_get(&req->ctx->refs);
3158
Jens Axboebcf5a062020-05-22 09:24:42 -06003159 /* submit ref gets dropped, acquire a new one */
3160 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003161 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003162 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003163 struct task_struct *tsk;
3164
Jens Axboebcf5a062020-05-22 09:24:42 -06003165 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003166 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003167 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003168 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003169 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003170 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003171 return 1;
3172}
3173
Jens Axboec1dd91d2020-08-03 16:43:59 -06003174/*
3175 * This controls whether a given IO request should be armed for async page
3176 * based retry. If we return false here, the request is handed to the async
3177 * worker threads for retry. If we're doing buffered reads on a regular file,
3178 * we prepare a private wait_page_queue entry and retry the operation. This
3179 * will either succeed because the page is now uptodate and unlocked, or it
3180 * will register a callback when the page is unlocked at IO completion. Through
3181 * that callback, io_uring uses task_work to setup a retry of the operation.
3182 * That retry will attempt the buffered read again. The retry will generally
3183 * succeed, or in rare cases where it fails, we then fall back to using the
3184 * async worker threads for a blocking retry.
3185 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003186static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003187{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003188 struct io_async_rw *rw = req->async_data;
3189 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003190 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003191
3192 /* never retry for NOWAIT, we just complete with -EAGAIN */
3193 if (req->flags & REQ_F_NOWAIT)
3194 return false;
3195
Jens Axboe227c0c92020-08-13 11:51:40 -06003196 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003197 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003198 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003199
Jens Axboebcf5a062020-05-22 09:24:42 -06003200 /*
3201 * just use poll if we can, and don't attempt if the fs doesn't
3202 * support callback based unlocks
3203 */
3204 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3205 return false;
3206
Jens Axboe3b2a4432020-08-16 10:58:43 -07003207 wait->wait.func = io_async_buf_func;
3208 wait->wait.private = req;
3209 wait->wait.flags = 0;
3210 INIT_LIST_HEAD(&wait->wait.entry);
3211 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003212 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003213 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003214 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003215}
3216
3217static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3218{
3219 if (req->file->f_op->read_iter)
3220 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003221 else if (req->file->f_op->read)
3222 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3223 else
3224 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003225}
3226
Jens Axboea1d7c392020-06-22 11:09:46 -06003227static int io_read(struct io_kiocb *req, bool force_nonblock,
3228 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003229{
3230 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003231 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003232 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003233 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003234 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003235 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003236 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003237
Jens Axboee8c2bc12020-08-15 18:44:09 -07003238 if (rw)
3239 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003240
3241 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003242 if (ret < 0)
3243 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003244 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003245 io_size = ret;
3246 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003247 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003248
Jens Axboefd6c2e42019-12-18 12:19:41 -07003249 /* Ensure we clear previously set non-block flag */
3250 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003251 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003252 else
3253 kiocb->ki_flags |= IOCB_NOWAIT;
3254
Jens Axboefd6c2e42019-12-18 12:19:41 -07003255
Pavel Begunkov24c74672020-06-21 13:09:51 +03003256 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003257 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3258 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003259 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003260
Jens Axboe0fef9482020-08-26 10:36:20 -06003261 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003262 if (unlikely(ret))
3263 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003264
Jens Axboe227c0c92020-08-13 11:51:40 -06003265 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003266
Jens Axboe227c0c92020-08-13 11:51:40 -06003267 if (!ret) {
3268 goto done;
3269 } else if (ret == -EIOCBQUEUED) {
3270 ret = 0;
3271 goto out_free;
3272 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003273 /* IOPOLL retry should happen for io-wq threads */
3274 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003275 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003276 /* no retry on NONBLOCK marked file */
3277 if (req->file->f_flags & O_NONBLOCK)
3278 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003279 /* some cases will consume bytes even on error returns */
3280 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003281 ret = 0;
3282 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003283 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003284 /* make sure -ERESTARTSYS -> -EINTR is done */
3285 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003286 }
3287
3288 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003289 if (!iov_iter_count(iter) || !force_nonblock ||
3290 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003291 goto done;
3292
3293 io_size -= ret;
3294copy_iov:
3295 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3296 if (ret2) {
3297 ret = ret2;
3298 goto out_free;
3299 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003300 if (no_async)
3301 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003302 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003303 /* it's copied and will be cleaned with ->io */
3304 iovec = NULL;
3305 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003306 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003307retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003308 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003309 /* if we can retry, do so with the callbacks armed */
3310 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003311 kiocb->ki_flags &= ~IOCB_WAITQ;
3312 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003313 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003314
3315 /*
3316 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3317 * get -EIOCBQUEUED, then we'll get a notification when the desired
3318 * page gets unlocked. We can also get a partial read here, and if we
3319 * do, then just retry at the new offset.
3320 */
3321 ret = io_iter_do_read(req, iter);
3322 if (ret == -EIOCBQUEUED) {
3323 ret = 0;
3324 goto out_free;
3325 } else if (ret > 0 && ret < io_size) {
3326 /* we got some bytes, but not all. retry. */
3327 goto retry;
3328 }
3329done:
3330 kiocb_done(kiocb, ret, cs);
3331 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003332out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003333 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003334 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003335 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003336 return ret;
3337}
3338
Pavel Begunkov73debe62020-09-30 22:57:54 +03003339static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003340{
3341 ssize_t ret;
3342
Pavel Begunkova88fc402020-09-30 22:57:53 +03003343 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003344 if (ret)
3345 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003346
Jens Axboe3529d8c2019-12-19 18:24:38 -07003347 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3348 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003349
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003350 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003351 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003352 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003353 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003354}
3355
Jens Axboea1d7c392020-06-22 11:09:46 -06003356static int io_write(struct io_kiocb *req, bool force_nonblock,
3357 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003358{
3359 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003360 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003361 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003362 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003363 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003364 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003365
Jens Axboee8c2bc12020-08-15 18:44:09 -07003366 if (rw)
3367 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003368
3369 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003370 if (ret < 0)
3371 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003372 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003373 io_size = ret;
3374 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003375
Jens Axboefd6c2e42019-12-18 12:19:41 -07003376 /* Ensure we clear previously set non-block flag */
3377 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003378 kiocb->ki_flags &= ~IOCB_NOWAIT;
3379 else
3380 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003381
Pavel Begunkov24c74672020-06-21 13:09:51 +03003382 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003383 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003384 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003385
Jens Axboe10d59342019-12-09 20:16:22 -07003386 /* file path doesn't support NOWAIT for non-direct_IO */
3387 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3388 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003389 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003390
Jens Axboe0fef9482020-08-26 10:36:20 -06003391 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003392 if (unlikely(ret))
3393 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003394
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003395 /*
3396 * Open-code file_start_write here to grab freeze protection,
3397 * which will be released by another thread in
3398 * io_complete_rw(). Fool lockdep by telling it the lock got
3399 * released so that it doesn't complain about the held lock when
3400 * we return to userspace.
3401 */
3402 if (req->flags & REQ_F_ISREG) {
3403 __sb_start_write(file_inode(req->file)->i_sb,
3404 SB_FREEZE_WRITE, true);
3405 __sb_writers_release(file_inode(req->file)->i_sb,
3406 SB_FREEZE_WRITE);
3407 }
3408 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003409
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003410 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003411 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003412 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003413 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003414 else
3415 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003416
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003417 /*
3418 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3419 * retry them without IOCB_NOWAIT.
3420 */
3421 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3422 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003423 /* no retry on NONBLOCK marked file */
3424 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3425 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003426 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003427 /* IOPOLL retry should happen for io-wq threads */
3428 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3429 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003430done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003431 kiocb_done(kiocb, ret2, cs);
3432 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003433copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003434 /* some cases will consume bytes even on error returns */
3435 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003436 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003437 if (!ret)
3438 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003439 }
Jens Axboe31b51512019-01-18 22:56:34 -07003440out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003441 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003442 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003443 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003444 return ret;
3445}
3446
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003447static int __io_splice_prep(struct io_kiocb *req,
3448 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003449{
3450 struct io_splice* sp = &req->splice;
3451 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003452
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003453 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3454 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003455
3456 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003457 sp->len = READ_ONCE(sqe->len);
3458 sp->flags = READ_ONCE(sqe->splice_flags);
3459
3460 if (unlikely(sp->flags & ~valid_flags))
3461 return -EINVAL;
3462
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003463 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3464 (sp->flags & SPLICE_F_FD_IN_FIXED));
3465 if (!sp->file_in)
3466 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003467 req->flags |= REQ_F_NEED_CLEANUP;
3468
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003469 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3470 /*
3471 * Splice operation will be punted aync, and here need to
3472 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3473 */
3474 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003475 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003476 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003477
3478 return 0;
3479}
3480
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003481static int io_tee_prep(struct io_kiocb *req,
3482 const struct io_uring_sqe *sqe)
3483{
3484 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3485 return -EINVAL;
3486 return __io_splice_prep(req, sqe);
3487}
3488
3489static int io_tee(struct io_kiocb *req, bool force_nonblock)
3490{
3491 struct io_splice *sp = &req->splice;
3492 struct file *in = sp->file_in;
3493 struct file *out = sp->file_out;
3494 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3495 long ret = 0;
3496
3497 if (force_nonblock)
3498 return -EAGAIN;
3499 if (sp->len)
3500 ret = do_tee(in, out, sp->len, flags);
3501
3502 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3503 req->flags &= ~REQ_F_NEED_CLEANUP;
3504
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003505 if (ret != sp->len)
3506 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003507 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003508 return 0;
3509}
3510
3511static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3512{
3513 struct io_splice* sp = &req->splice;
3514
3515 sp->off_in = READ_ONCE(sqe->splice_off_in);
3516 sp->off_out = READ_ONCE(sqe->off);
3517 return __io_splice_prep(req, sqe);
3518}
3519
Pavel Begunkov014db002020-03-03 21:33:12 +03003520static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003521{
3522 struct io_splice *sp = &req->splice;
3523 struct file *in = sp->file_in;
3524 struct file *out = sp->file_out;
3525 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3526 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003527 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003528
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003529 if (force_nonblock)
3530 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003531
3532 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3533 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003534
Jens Axboe948a7742020-05-17 14:21:38 -06003535 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003536 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003537
3538 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3539 req->flags &= ~REQ_F_NEED_CLEANUP;
3540
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003541 if (ret != sp->len)
3542 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003543 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003544 return 0;
3545}
3546
Jens Axboe2b188cc2019-01-07 10:46:33 -07003547/*
3548 * IORING_OP_NOP just posts a completion event, nothing else.
3549 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003550static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003551{
3552 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003553
Jens Axboedef596e2019-01-09 08:59:42 -07003554 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3555 return -EINVAL;
3556
Jens Axboe229a7b62020-06-22 10:13:11 -06003557 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003558 return 0;
3559}
3560
Jens Axboe3529d8c2019-12-19 18:24:38 -07003561static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003562{
Jens Axboe6b063142019-01-10 22:13:58 -07003563 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003564
Jens Axboe09bb8392019-03-13 12:39:28 -06003565 if (!req->file)
3566 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003567
Jens Axboe6b063142019-01-10 22:13:58 -07003568 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003569 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003570 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003571 return -EINVAL;
3572
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003573 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3574 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3575 return -EINVAL;
3576
3577 req->sync.off = READ_ONCE(sqe->off);
3578 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003579 return 0;
3580}
3581
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003582static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003583{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003584 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003585 int ret;
3586
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003587 /* fsync always requires a blocking context */
3588 if (force_nonblock)
3589 return -EAGAIN;
3590
Jens Axboe9adbd452019-12-20 08:45:55 -07003591 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003592 end > 0 ? end : LLONG_MAX,
3593 req->sync.flags & IORING_FSYNC_DATASYNC);
3594 if (ret < 0)
3595 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003596 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003597 return 0;
3598}
3599
Jens Axboed63d1b52019-12-10 10:38:56 -07003600static int io_fallocate_prep(struct io_kiocb *req,
3601 const struct io_uring_sqe *sqe)
3602{
3603 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3604 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003605 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3606 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003607
3608 req->sync.off = READ_ONCE(sqe->off);
3609 req->sync.len = READ_ONCE(sqe->addr);
3610 req->sync.mode = READ_ONCE(sqe->len);
3611 return 0;
3612}
3613
Pavel Begunkov014db002020-03-03 21:33:12 +03003614static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003615{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003616 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003617
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003618 /* fallocate always requiring blocking context */
3619 if (force_nonblock)
3620 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003621 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3622 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003623 if (ret < 0)
3624 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003625 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003626 return 0;
3627}
3628
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003629static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003630{
Jens Axboef8748882020-01-08 17:47:02 -07003631 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003632 int ret;
3633
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003634 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003635 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003636 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003637 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003638
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003639 /* open.how should be already initialised */
3640 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003641 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003642
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003643 req->open.dfd = READ_ONCE(sqe->fd);
3644 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003645 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003646 if (IS_ERR(req->open.filename)) {
3647 ret = PTR_ERR(req->open.filename);
3648 req->open.filename = NULL;
3649 return ret;
3650 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003651 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003652 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003653 return 0;
3654}
3655
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003656static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3657{
3658 u64 flags, mode;
3659
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003660 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3661 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003662 mode = READ_ONCE(sqe->len);
3663 flags = READ_ONCE(sqe->open_flags);
3664 req->open.how = build_open_how(flags, mode);
3665 return __io_openat_prep(req, sqe);
3666}
3667
Jens Axboecebdb982020-01-08 17:59:24 -07003668static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3669{
3670 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003671 size_t len;
3672 int ret;
3673
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003674 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3675 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003676 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3677 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003678 if (len < OPEN_HOW_SIZE_VER0)
3679 return -EINVAL;
3680
3681 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3682 len);
3683 if (ret)
3684 return ret;
3685
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003686 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003687}
3688
Pavel Begunkov014db002020-03-03 21:33:12 +03003689static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003690{
3691 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003692 struct file *file;
3693 int ret;
3694
Jens Axboef86cd202020-01-29 13:46:44 -07003695 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003696 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003697
Jens Axboecebdb982020-01-08 17:59:24 -07003698 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003699 if (ret)
3700 goto err;
3701
Jens Axboe4022e7a2020-03-19 19:23:18 -06003702 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003703 if (ret < 0)
3704 goto err;
3705
3706 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3707 if (IS_ERR(file)) {
3708 put_unused_fd(ret);
3709 ret = PTR_ERR(file);
3710 } else {
3711 fsnotify_open(file);
3712 fd_install(ret, file);
3713 }
3714err:
3715 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003716 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003717 if (ret < 0)
3718 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003719 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003720 return 0;
3721}
3722
Pavel Begunkov014db002020-03-03 21:33:12 +03003723static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003724{
Pavel Begunkov014db002020-03-03 21:33:12 +03003725 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003726}
3727
Jens Axboe067524e2020-03-02 16:32:28 -07003728static int io_remove_buffers_prep(struct io_kiocb *req,
3729 const struct io_uring_sqe *sqe)
3730{
3731 struct io_provide_buf *p = &req->pbuf;
3732 u64 tmp;
3733
3734 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3735 return -EINVAL;
3736
3737 tmp = READ_ONCE(sqe->fd);
3738 if (!tmp || tmp > USHRT_MAX)
3739 return -EINVAL;
3740
3741 memset(p, 0, sizeof(*p));
3742 p->nbufs = tmp;
3743 p->bgid = READ_ONCE(sqe->buf_group);
3744 return 0;
3745}
3746
3747static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3748 int bgid, unsigned nbufs)
3749{
3750 unsigned i = 0;
3751
3752 /* shouldn't happen */
3753 if (!nbufs)
3754 return 0;
3755
3756 /* the head kbuf is the list itself */
3757 while (!list_empty(&buf->list)) {
3758 struct io_buffer *nxt;
3759
3760 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3761 list_del(&nxt->list);
3762 kfree(nxt);
3763 if (++i == nbufs)
3764 return i;
3765 }
3766 i++;
3767 kfree(buf);
3768 idr_remove(&ctx->io_buffer_idr, bgid);
3769
3770 return i;
3771}
3772
Jens Axboe229a7b62020-06-22 10:13:11 -06003773static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3774 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003775{
3776 struct io_provide_buf *p = &req->pbuf;
3777 struct io_ring_ctx *ctx = req->ctx;
3778 struct io_buffer *head;
3779 int ret = 0;
3780
3781 io_ring_submit_lock(ctx, !force_nonblock);
3782
3783 lockdep_assert_held(&ctx->uring_lock);
3784
3785 ret = -ENOENT;
3786 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3787 if (head)
3788 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3789
3790 io_ring_submit_lock(ctx, !force_nonblock);
3791 if (ret < 0)
3792 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003793 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003794 return 0;
3795}
3796
Jens Axboeddf0322d2020-02-23 16:41:33 -07003797static int io_provide_buffers_prep(struct io_kiocb *req,
3798 const struct io_uring_sqe *sqe)
3799{
3800 struct io_provide_buf *p = &req->pbuf;
3801 u64 tmp;
3802
3803 if (sqe->ioprio || sqe->rw_flags)
3804 return -EINVAL;
3805
3806 tmp = READ_ONCE(sqe->fd);
3807 if (!tmp || tmp > USHRT_MAX)
3808 return -E2BIG;
3809 p->nbufs = tmp;
3810 p->addr = READ_ONCE(sqe->addr);
3811 p->len = READ_ONCE(sqe->len);
3812
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003813 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003814 return -EFAULT;
3815
3816 p->bgid = READ_ONCE(sqe->buf_group);
3817 tmp = READ_ONCE(sqe->off);
3818 if (tmp > USHRT_MAX)
3819 return -E2BIG;
3820 p->bid = tmp;
3821 return 0;
3822}
3823
3824static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3825{
3826 struct io_buffer *buf;
3827 u64 addr = pbuf->addr;
3828 int i, bid = pbuf->bid;
3829
3830 for (i = 0; i < pbuf->nbufs; i++) {
3831 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3832 if (!buf)
3833 break;
3834
3835 buf->addr = addr;
3836 buf->len = pbuf->len;
3837 buf->bid = bid;
3838 addr += pbuf->len;
3839 bid++;
3840 if (!*head) {
3841 INIT_LIST_HEAD(&buf->list);
3842 *head = buf;
3843 } else {
3844 list_add_tail(&buf->list, &(*head)->list);
3845 }
3846 }
3847
3848 return i ? i : -ENOMEM;
3849}
3850
Jens Axboe229a7b62020-06-22 10:13:11 -06003851static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3852 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003853{
3854 struct io_provide_buf *p = &req->pbuf;
3855 struct io_ring_ctx *ctx = req->ctx;
3856 struct io_buffer *head, *list;
3857 int ret = 0;
3858
3859 io_ring_submit_lock(ctx, !force_nonblock);
3860
3861 lockdep_assert_held(&ctx->uring_lock);
3862
3863 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3864
3865 ret = io_add_buffers(p, &head);
3866 if (ret < 0)
3867 goto out;
3868
3869 if (!list) {
3870 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3871 GFP_KERNEL);
3872 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003873 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003874 goto out;
3875 }
3876 }
3877out:
3878 io_ring_submit_unlock(ctx, !force_nonblock);
3879 if (ret < 0)
3880 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003881 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003882 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003883}
3884
Jens Axboe3e4827b2020-01-08 15:18:09 -07003885static int io_epoll_ctl_prep(struct io_kiocb *req,
3886 const struct io_uring_sqe *sqe)
3887{
3888#if defined(CONFIG_EPOLL)
3889 if (sqe->ioprio || sqe->buf_index)
3890 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003891 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003892 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003893
3894 req->epoll.epfd = READ_ONCE(sqe->fd);
3895 req->epoll.op = READ_ONCE(sqe->len);
3896 req->epoll.fd = READ_ONCE(sqe->off);
3897
3898 if (ep_op_has_event(req->epoll.op)) {
3899 struct epoll_event __user *ev;
3900
3901 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3902 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3903 return -EFAULT;
3904 }
3905
3906 return 0;
3907#else
3908 return -EOPNOTSUPP;
3909#endif
3910}
3911
Jens Axboe229a7b62020-06-22 10:13:11 -06003912static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3913 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003914{
3915#if defined(CONFIG_EPOLL)
3916 struct io_epoll *ie = &req->epoll;
3917 int ret;
3918
3919 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3920 if (force_nonblock && ret == -EAGAIN)
3921 return -EAGAIN;
3922
3923 if (ret < 0)
3924 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003925 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003926 return 0;
3927#else
3928 return -EOPNOTSUPP;
3929#endif
3930}
3931
Jens Axboec1ca7572019-12-25 22:18:28 -07003932static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3933{
3934#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3935 if (sqe->ioprio || sqe->buf_index || sqe->off)
3936 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003937 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3938 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003939
3940 req->madvise.addr = READ_ONCE(sqe->addr);
3941 req->madvise.len = READ_ONCE(sqe->len);
3942 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3943 return 0;
3944#else
3945 return -EOPNOTSUPP;
3946#endif
3947}
3948
Pavel Begunkov014db002020-03-03 21:33:12 +03003949static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003950{
3951#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3952 struct io_madvise *ma = &req->madvise;
3953 int ret;
3954
3955 if (force_nonblock)
3956 return -EAGAIN;
3957
3958 ret = do_madvise(ma->addr, ma->len, ma->advice);
3959 if (ret < 0)
3960 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003961 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003962 return 0;
3963#else
3964 return -EOPNOTSUPP;
3965#endif
3966}
3967
Jens Axboe4840e412019-12-25 22:03:45 -07003968static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3969{
3970 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3971 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003972 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3973 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003974
3975 req->fadvise.offset = READ_ONCE(sqe->off);
3976 req->fadvise.len = READ_ONCE(sqe->len);
3977 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3978 return 0;
3979}
3980
Pavel Begunkov014db002020-03-03 21:33:12 +03003981static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003982{
3983 struct io_fadvise *fa = &req->fadvise;
3984 int ret;
3985
Jens Axboe3e694262020-02-01 09:22:49 -07003986 if (force_nonblock) {
3987 switch (fa->advice) {
3988 case POSIX_FADV_NORMAL:
3989 case POSIX_FADV_RANDOM:
3990 case POSIX_FADV_SEQUENTIAL:
3991 break;
3992 default:
3993 return -EAGAIN;
3994 }
3995 }
Jens Axboe4840e412019-12-25 22:03:45 -07003996
3997 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3998 if (ret < 0)
3999 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004000 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004001 return 0;
4002}
4003
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004004static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4005{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004006 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004007 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004008 if (sqe->ioprio || sqe->buf_index)
4009 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004010 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004011 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004012
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004013 req->statx.dfd = READ_ONCE(sqe->fd);
4014 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004015 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004016 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4017 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004018
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004019 return 0;
4020}
4021
Pavel Begunkov014db002020-03-03 21:33:12 +03004022static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004023{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004024 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004025 int ret;
4026
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004027 if (force_nonblock) {
4028 /* only need file table for an actual valid fd */
4029 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4030 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004031 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004032 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004033
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004034 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4035 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004036
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004037 if (ret < 0)
4038 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004039 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004040 return 0;
4041}
4042
Jens Axboeb5dba592019-12-11 14:02:38 -07004043static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4044{
4045 /*
4046 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004047 * leave the 'file' in an undeterminate state, and here need to modify
4048 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004049 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004050 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004051 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4052
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004053 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4054 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004055 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4056 sqe->rw_flags || sqe->buf_index)
4057 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004058 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004059 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004060
4061 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004062 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004063 return -EBADF;
4064
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004065 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004066 return 0;
4067}
4068
Jens Axboe229a7b62020-06-22 10:13:11 -06004069static int io_close(struct io_kiocb *req, bool force_nonblock,
4070 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004071{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004072 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004073 int ret;
4074
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004075 /* might be already done during nonblock submission */
4076 if (!close->put_file) {
4077 ret = __close_fd_get_file(close->fd, &close->put_file);
4078 if (ret < 0)
4079 return (ret == -ENOENT) ? -EBADF : ret;
4080 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004081
4082 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004083 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004084 /* was never set, but play safe */
4085 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004086 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004087 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004088 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004089 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004090
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004091 /* No ->flush() or already async, safely close from here */
4092 ret = filp_close(close->put_file, req->work.files);
4093 if (ret < 0)
4094 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004095 fput(close->put_file);
4096 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004097 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004098 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004099}
4100
Jens Axboe3529d8c2019-12-19 18:24:38 -07004101static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004102{
4103 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004104
4105 if (!req->file)
4106 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004107
4108 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4109 return -EINVAL;
4110 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4111 return -EINVAL;
4112
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004113 req->sync.off = READ_ONCE(sqe->off);
4114 req->sync.len = READ_ONCE(sqe->len);
4115 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004116 return 0;
4117}
4118
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004119static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004120{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004121 int ret;
4122
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004123 /* sync_file_range always requires a blocking context */
4124 if (force_nonblock)
4125 return -EAGAIN;
4126
Jens Axboe9adbd452019-12-20 08:45:55 -07004127 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004128 req->sync.flags);
4129 if (ret < 0)
4130 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004131 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004132 return 0;
4133}
4134
YueHaibing469956e2020-03-04 15:53:52 +08004135#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004136static int io_setup_async_msg(struct io_kiocb *req,
4137 struct io_async_msghdr *kmsg)
4138{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004139 struct io_async_msghdr *async_msg = req->async_data;
4140
4141 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004142 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004143 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004144 if (kmsg->iov != kmsg->fast_iov)
4145 kfree(kmsg->iov);
4146 return -ENOMEM;
4147 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004148 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004149 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004150 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004151 return -EAGAIN;
4152}
4153
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004154static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4155 struct io_async_msghdr *iomsg)
4156{
4157 iomsg->iov = iomsg->fast_iov;
4158 iomsg->msg.msg_name = &iomsg->addr;
4159 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4160 req->sr_msg.msg_flags, &iomsg->iov);
4161}
4162
Jens Axboe3529d8c2019-12-19 18:24:38 -07004163static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004164{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004165 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004166 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004167 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004168
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004169 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4170 return -EINVAL;
4171
Jens Axboee47293f2019-12-20 08:58:21 -07004172 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004173 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004174 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004175
Jens Axboed8768362020-02-27 14:17:49 -07004176#ifdef CONFIG_COMPAT
4177 if (req->ctx->compat)
4178 sr->msg_flags |= MSG_CMSG_COMPAT;
4179#endif
4180
Jens Axboee8c2bc12020-08-15 18:44:09 -07004181 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004183 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004184 if (!ret)
4185 req->flags |= REQ_F_NEED_CLEANUP;
4186 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004187}
4188
Jens Axboe229a7b62020-06-22 10:13:11 -06004189static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4190 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004191{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004192 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004193 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004194 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004195 int ret;
4196
Jens Axboe03b12302019-12-02 18:50:25 -07004197 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004198 if (unlikely(!sock))
4199 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004200
Jens Axboee8c2bc12020-08-15 18:44:09 -07004201 if (req->async_data) {
4202 kmsg = req->async_data;
4203 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004204 /* if iov is set, it's allocated already */
4205 if (!kmsg->iov)
4206 kmsg->iov = kmsg->fast_iov;
4207 kmsg->msg.msg_iter.iov = kmsg->iov;
4208 } else {
4209 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004210 if (ret)
4211 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004212 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004213 }
4214
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004215 flags = req->sr_msg.msg_flags;
4216 if (flags & MSG_DONTWAIT)
4217 req->flags |= REQ_F_NOWAIT;
4218 else if (force_nonblock)
4219 flags |= MSG_DONTWAIT;
4220
4221 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4222 if (force_nonblock && ret == -EAGAIN)
4223 return io_setup_async_msg(req, kmsg);
4224 if (ret == -ERESTARTSYS)
4225 ret = -EINTR;
4226
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004227 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004228 kfree(kmsg->iov);
4229 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004230 if (ret < 0)
4231 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004232 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004233 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004234}
4235
Jens Axboe229a7b62020-06-22 10:13:11 -06004236static int io_send(struct io_kiocb *req, bool force_nonblock,
4237 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004238{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004239 struct io_sr_msg *sr = &req->sr_msg;
4240 struct msghdr msg;
4241 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004242 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004243 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004244 int ret;
4245
4246 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004247 if (unlikely(!sock))
4248 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004249
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004250 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4251 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004252 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004253
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004254 msg.msg_name = NULL;
4255 msg.msg_control = NULL;
4256 msg.msg_controllen = 0;
4257 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004258
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004259 flags = req->sr_msg.msg_flags;
4260 if (flags & MSG_DONTWAIT)
4261 req->flags |= REQ_F_NOWAIT;
4262 else if (force_nonblock)
4263 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004264
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004265 msg.msg_flags = flags;
4266 ret = sock_sendmsg(sock, &msg);
4267 if (force_nonblock && ret == -EAGAIN)
4268 return -EAGAIN;
4269 if (ret == -ERESTARTSYS)
4270 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004271
Jens Axboe03b12302019-12-02 18:50:25 -07004272 if (ret < 0)
4273 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004274 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004275 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004276}
4277
Pavel Begunkov1400e692020-07-12 20:41:05 +03004278static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4279 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004280{
4281 struct io_sr_msg *sr = &req->sr_msg;
4282 struct iovec __user *uiov;
4283 size_t iov_len;
4284 int ret;
4285
Pavel Begunkov1400e692020-07-12 20:41:05 +03004286 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4287 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004288 if (ret)
4289 return ret;
4290
4291 if (req->flags & REQ_F_BUFFER_SELECT) {
4292 if (iov_len > 1)
4293 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004294 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004295 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004296 sr->len = iomsg->iov[0].iov_len;
4297 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004298 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004299 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004300 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004301 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4302 &iomsg->iov, &iomsg->msg.msg_iter,
4303 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004304 if (ret > 0)
4305 ret = 0;
4306 }
4307
4308 return ret;
4309}
4310
4311#ifdef CONFIG_COMPAT
4312static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004313 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004314{
4315 struct compat_msghdr __user *msg_compat;
4316 struct io_sr_msg *sr = &req->sr_msg;
4317 struct compat_iovec __user *uiov;
4318 compat_uptr_t ptr;
4319 compat_size_t len;
4320 int ret;
4321
Pavel Begunkov270a5942020-07-12 20:41:04 +03004322 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004323 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004324 &ptr, &len);
4325 if (ret)
4326 return ret;
4327
4328 uiov = compat_ptr(ptr);
4329 if (req->flags & REQ_F_BUFFER_SELECT) {
4330 compat_ssize_t clen;
4331
4332 if (len > 1)
4333 return -EINVAL;
4334 if (!access_ok(uiov, sizeof(*uiov)))
4335 return -EFAULT;
4336 if (__get_user(clen, &uiov->iov_len))
4337 return -EFAULT;
4338 if (clen < 0)
4339 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004340 sr->len = iomsg->iov[0].iov_len;
4341 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004342 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004343 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4344 UIO_FASTIOV, &iomsg->iov,
4345 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004346 if (ret < 0)
4347 return ret;
4348 }
4349
4350 return 0;
4351}
Jens Axboe03b12302019-12-02 18:50:25 -07004352#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004353
Pavel Begunkov1400e692020-07-12 20:41:05 +03004354static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4355 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004356{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004357 iomsg->msg.msg_name = &iomsg->addr;
4358 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004359
4360#ifdef CONFIG_COMPAT
4361 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004362 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004363#endif
4364
Pavel Begunkov1400e692020-07-12 20:41:05 +03004365 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004366}
4367
Jens Axboebcda7ba2020-02-23 16:42:51 -07004368static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004369 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004370{
4371 struct io_sr_msg *sr = &req->sr_msg;
4372 struct io_buffer *kbuf;
4373
Jens Axboebcda7ba2020-02-23 16:42:51 -07004374 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4375 if (IS_ERR(kbuf))
4376 return kbuf;
4377
4378 sr->kbuf = kbuf;
4379 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004380 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004381}
4382
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004383static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4384{
4385 return io_put_kbuf(req, req->sr_msg.kbuf);
4386}
4387
Jens Axboe3529d8c2019-12-19 18:24:38 -07004388static int io_recvmsg_prep(struct io_kiocb *req,
4389 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004390{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004391 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004392 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004393 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004394
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004395 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4396 return -EINVAL;
4397
Jens Axboe3529d8c2019-12-19 18:24:38 -07004398 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004399 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004400 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004401 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004402
Jens Axboed8768362020-02-27 14:17:49 -07004403#ifdef CONFIG_COMPAT
4404 if (req->ctx->compat)
4405 sr->msg_flags |= MSG_CMSG_COMPAT;
4406#endif
4407
Jens Axboee8c2bc12020-08-15 18:44:09 -07004408 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004409 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004410 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004411 if (!ret)
4412 req->flags |= REQ_F_NEED_CLEANUP;
4413 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004414}
4415
Jens Axboe229a7b62020-06-22 10:13:11 -06004416static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4417 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004418{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004419 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004420 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004421 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004422 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004423 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004424
Jens Axboe0fa03c62019-04-19 13:34:07 -06004425 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004426 if (unlikely(!sock))
4427 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004428
Jens Axboee8c2bc12020-08-15 18:44:09 -07004429 if (req->async_data) {
4430 kmsg = req->async_data;
4431 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004432 /* if iov is set, it's allocated already */
4433 if (!kmsg->iov)
4434 kmsg->iov = kmsg->fast_iov;
4435 kmsg->msg.msg_iter.iov = kmsg->iov;
4436 } else {
4437 ret = io_recvmsg_copy_hdr(req, &iomsg);
4438 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004439 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004440 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004441 }
4442
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004443 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004444 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004445 if (IS_ERR(kbuf))
4446 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004447 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4448 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4449 1, req->sr_msg.len);
4450 }
4451
4452 flags = req->sr_msg.msg_flags;
4453 if (flags & MSG_DONTWAIT)
4454 req->flags |= REQ_F_NOWAIT;
4455 else if (force_nonblock)
4456 flags |= MSG_DONTWAIT;
4457
4458 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4459 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004460 if (force_nonblock && ret == -EAGAIN)
4461 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004462 if (ret == -ERESTARTSYS)
4463 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004464
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004465 if (req->flags & REQ_F_BUFFER_SELECTED)
4466 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004467 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004468 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004469 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004470 if (ret < 0)
4471 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004472 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004473 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004474}
4475
Jens Axboe229a7b62020-06-22 10:13:11 -06004476static int io_recv(struct io_kiocb *req, bool force_nonblock,
4477 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004478{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004479 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004480 struct io_sr_msg *sr = &req->sr_msg;
4481 struct msghdr msg;
4482 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004483 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004484 struct iovec iov;
4485 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004486 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004487
Jens Axboefddafac2020-01-04 20:19:44 -07004488 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004489 if (unlikely(!sock))
4490 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004491
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004492 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004493 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004494 if (IS_ERR(kbuf))
4495 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004496 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004497 }
4498
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004499 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004500 if (unlikely(ret))
4501 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004502
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004503 msg.msg_name = NULL;
4504 msg.msg_control = NULL;
4505 msg.msg_controllen = 0;
4506 msg.msg_namelen = 0;
4507 msg.msg_iocb = NULL;
4508 msg.msg_flags = 0;
4509
4510 flags = req->sr_msg.msg_flags;
4511 if (flags & MSG_DONTWAIT)
4512 req->flags |= REQ_F_NOWAIT;
4513 else if (force_nonblock)
4514 flags |= MSG_DONTWAIT;
4515
4516 ret = sock_recvmsg(sock, &msg, flags);
4517 if (force_nonblock && ret == -EAGAIN)
4518 return -EAGAIN;
4519 if (ret == -ERESTARTSYS)
4520 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004521out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004522 if (req->flags & REQ_F_BUFFER_SELECTED)
4523 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004524 if (ret < 0)
4525 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004526 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004527 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004528}
4529
Jens Axboe3529d8c2019-12-19 18:24:38 -07004530static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004531{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004532 struct io_accept *accept = &req->accept;
4533
Jens Axboe17f2fe32019-10-17 14:42:58 -06004534 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4535 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004536 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004537 return -EINVAL;
4538
Jens Axboed55e5f52019-12-11 16:12:15 -07004539 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4540 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004541 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004542 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004543 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004544}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004545
Jens Axboe229a7b62020-06-22 10:13:11 -06004546static int io_accept(struct io_kiocb *req, bool force_nonblock,
4547 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004548{
4549 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004550 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004551 int ret;
4552
Jiufei Xuee697dee2020-06-10 13:41:59 +08004553 if (req->file->f_flags & O_NONBLOCK)
4554 req->flags |= REQ_F_NOWAIT;
4555
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004556 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004557 accept->addr_len, accept->flags,
4558 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004559 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004560 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004561 if (ret < 0) {
4562 if (ret == -ERESTARTSYS)
4563 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004564 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004565 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004566 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004567 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004568}
4569
Jens Axboe3529d8c2019-12-19 18:24:38 -07004570static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004571{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004572 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004573 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004574
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004575 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4576 return -EINVAL;
4577 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4578 return -EINVAL;
4579
Jens Axboe3529d8c2019-12-19 18:24:38 -07004580 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4581 conn->addr_len = READ_ONCE(sqe->addr2);
4582
4583 if (!io)
4584 return 0;
4585
4586 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004587 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004588}
4589
Jens Axboe229a7b62020-06-22 10:13:11 -06004590static int io_connect(struct io_kiocb *req, bool force_nonblock,
4591 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004592{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004593 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004594 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004595 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004596
Jens Axboee8c2bc12020-08-15 18:44:09 -07004597 if (req->async_data) {
4598 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004599 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004600 ret = move_addr_to_kernel(req->connect.addr,
4601 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004602 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004603 if (ret)
4604 goto out;
4605 io = &__io;
4606 }
4607
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004608 file_flags = force_nonblock ? O_NONBLOCK : 0;
4609
Jens Axboee8c2bc12020-08-15 18:44:09 -07004610 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004611 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004612 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004613 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004614 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004615 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004616 ret = -ENOMEM;
4617 goto out;
4618 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004619 io = req->async_data;
4620 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004621 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004622 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004623 if (ret == -ERESTARTSYS)
4624 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004625out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004626 if (ret < 0)
4627 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004628 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004629 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004630}
YueHaibing469956e2020-03-04 15:53:52 +08004631#else /* !CONFIG_NET */
4632static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4633{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004634 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004635}
4636
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004637static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4638 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004639{
YueHaibing469956e2020-03-04 15:53:52 +08004640 return -EOPNOTSUPP;
4641}
4642
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004643static int io_send(struct io_kiocb *req, bool force_nonblock,
4644 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004645{
4646 return -EOPNOTSUPP;
4647}
4648
4649static int io_recvmsg_prep(struct io_kiocb *req,
4650 const struct io_uring_sqe *sqe)
4651{
4652 return -EOPNOTSUPP;
4653}
4654
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004655static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4656 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004657{
4658 return -EOPNOTSUPP;
4659}
4660
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004661static int io_recv(struct io_kiocb *req, bool force_nonblock,
4662 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004663{
4664 return -EOPNOTSUPP;
4665}
4666
4667static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4668{
4669 return -EOPNOTSUPP;
4670}
4671
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004672static int io_accept(struct io_kiocb *req, bool force_nonblock,
4673 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004674{
4675 return -EOPNOTSUPP;
4676}
4677
4678static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4679{
4680 return -EOPNOTSUPP;
4681}
4682
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004683static int io_connect(struct io_kiocb *req, bool force_nonblock,
4684 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004685{
4686 return -EOPNOTSUPP;
4687}
4688#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004689
Jens Axboed7718a92020-02-14 22:23:12 -07004690struct io_poll_table {
4691 struct poll_table_struct pt;
4692 struct io_kiocb *req;
4693 int error;
4694};
4695
Jens Axboed7718a92020-02-14 22:23:12 -07004696static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4697 __poll_t mask, task_work_func_t func)
4698{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004699 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004700 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004701
4702 /* for instances that support it check for an event match first: */
4703 if (mask && !(mask & poll->events))
4704 return 0;
4705
4706 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4707
4708 list_del_init(&poll->wait.entry);
4709
Jens Axboed7718a92020-02-14 22:23:12 -07004710 req->result = mask;
4711 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004712 percpu_ref_get(&req->ctx->refs);
4713
Jens Axboed7718a92020-02-14 22:23:12 -07004714 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004715 * If we using the signalfd wait_queue_head for this wakeup, then
4716 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4717 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4718 * either, as the normal wakeup will suffice.
4719 */
4720 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4721
4722 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004723 * If this fails, then the task is exiting. When a task exits, the
4724 * work gets canceled, so just cancel this request as well instead
4725 * of executing it. We can't safely execute it anyway, as we may not
4726 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004727 */
Jens Axboe87c43112020-09-30 21:00:14 -06004728 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004729 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004730 struct task_struct *tsk;
4731
Jens Axboee3aabf92020-05-18 11:04:17 -06004732 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004733 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004734 task_work_add(tsk, &req->task_work, 0);
4735 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004736 }
Jens Axboed7718a92020-02-14 22:23:12 -07004737 return 1;
4738}
4739
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004740static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4741 __acquires(&req->ctx->completion_lock)
4742{
4743 struct io_ring_ctx *ctx = req->ctx;
4744
4745 if (!req->result && !READ_ONCE(poll->canceled)) {
4746 struct poll_table_struct pt = { ._key = poll->events };
4747
4748 req->result = vfs_poll(req->file, &pt) & poll->events;
4749 }
4750
4751 spin_lock_irq(&ctx->completion_lock);
4752 if (!req->result && !READ_ONCE(poll->canceled)) {
4753 add_wait_queue(poll->head, &poll->wait);
4754 return true;
4755 }
4756
4757 return false;
4758}
4759
Jens Axboed4e7cd32020-08-15 11:44:50 -07004760static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004761{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004762 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004763 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004764 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004765 return req->apoll->double_poll;
4766}
4767
4768static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4769{
4770 if (req->opcode == IORING_OP_POLL_ADD)
4771 return &req->poll;
4772 return &req->apoll->poll;
4773}
4774
4775static void io_poll_remove_double(struct io_kiocb *req)
4776{
4777 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004778
4779 lockdep_assert_held(&req->ctx->completion_lock);
4780
4781 if (poll && poll->head) {
4782 struct wait_queue_head *head = poll->head;
4783
4784 spin_lock(&head->lock);
4785 list_del_init(&poll->wait.entry);
4786 if (poll->wait.private)
4787 refcount_dec(&req->refs);
4788 poll->head = NULL;
4789 spin_unlock(&head->lock);
4790 }
4791}
4792
4793static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4794{
4795 struct io_ring_ctx *ctx = req->ctx;
4796
Jens Axboed4e7cd32020-08-15 11:44:50 -07004797 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004798 req->poll.done = true;
4799 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4800 io_commit_cqring(ctx);
4801}
4802
4803static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4804{
4805 struct io_ring_ctx *ctx = req->ctx;
4806
4807 if (io_poll_rewait(req, &req->poll)) {
4808 spin_unlock_irq(&ctx->completion_lock);
4809 return;
4810 }
4811
4812 hash_del(&req->hash_node);
4813 io_poll_complete(req, req->result, 0);
Jens Axboe18bceab2020-05-15 11:56:54 -06004814 spin_unlock_irq(&ctx->completion_lock);
4815
Pavel Begunkov6a0af222020-10-13 09:43:58 +01004816 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004817 io_cqring_ev_posted(ctx);
4818}
4819
4820static void io_poll_task_func(struct callback_head *cb)
4821{
4822 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004823 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004824 struct io_kiocb *nxt = NULL;
4825
4826 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004827 if (nxt)
4828 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004829 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004830}
4831
4832static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4833 int sync, void *key)
4834{
4835 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004836 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004837 __poll_t mask = key_to_poll(key);
4838
4839 /* for instances that support it check for an event match first: */
4840 if (mask && !(mask & poll->events))
4841 return 0;
4842
Jens Axboe8706e042020-09-28 08:38:54 -06004843 list_del_init(&wait->entry);
4844
Jens Axboe807abcb2020-07-17 17:09:27 -06004845 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004846 bool done;
4847
Jens Axboe807abcb2020-07-17 17:09:27 -06004848 spin_lock(&poll->head->lock);
4849 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004850 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004851 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004852 /* make sure double remove sees this as being gone */
4853 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004854 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004855 if (!done)
4856 __io_async_wake(req, poll, mask, io_poll_task_func);
4857 }
4858 refcount_dec(&req->refs);
4859 return 1;
4860}
4861
4862static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4863 wait_queue_func_t wake_func)
4864{
4865 poll->head = NULL;
4866 poll->done = false;
4867 poll->canceled = false;
4868 poll->events = events;
4869 INIT_LIST_HEAD(&poll->wait.entry);
4870 init_waitqueue_func_entry(&poll->wait, wake_func);
4871}
4872
4873static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004874 struct wait_queue_head *head,
4875 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004876{
4877 struct io_kiocb *req = pt->req;
4878
4879 /*
4880 * If poll->head is already set, it's because the file being polled
4881 * uses multiple waitqueues for poll handling (eg one for read, one
4882 * for write). Setup a separate io_poll_iocb if this happens.
4883 */
4884 if (unlikely(poll->head)) {
4885 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004886 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004887 pt->error = -EINVAL;
4888 return;
4889 }
4890 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4891 if (!poll) {
4892 pt->error = -ENOMEM;
4893 return;
4894 }
4895 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4896 refcount_inc(&req->refs);
4897 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004898 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004899 }
4900
4901 pt->error = 0;
4902 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004903
4904 if (poll->events & EPOLLEXCLUSIVE)
4905 add_wait_queue_exclusive(head, &poll->wait);
4906 else
4907 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004908}
4909
4910static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4911 struct poll_table_struct *p)
4912{
4913 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004914 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004915
Jens Axboe807abcb2020-07-17 17:09:27 -06004916 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004917}
4918
Jens Axboed7718a92020-02-14 22:23:12 -07004919static void io_async_task_func(struct callback_head *cb)
4920{
4921 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4922 struct async_poll *apoll = req->apoll;
4923 struct io_ring_ctx *ctx = req->ctx;
4924
4925 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4926
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004927 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004928 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004929 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004930 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004931 }
4932
Jens Axboe31067252020-05-17 17:43:31 -06004933 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004934 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004935 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004936
Jens Axboed4e7cd32020-08-15 11:44:50 -07004937 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004938 spin_unlock_irq(&ctx->completion_lock);
4939
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004940 if (!READ_ONCE(apoll->poll.canceled))
4941 __io_req_task_submit(req);
4942 else
4943 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004944
Jens Axboe6d816e02020-08-11 08:04:14 -06004945 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004946 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004947 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004948}
4949
4950static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4951 void *key)
4952{
4953 struct io_kiocb *req = wait->private;
4954 struct io_poll_iocb *poll = &req->apoll->poll;
4955
4956 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4957 key_to_poll(key));
4958
4959 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4960}
4961
4962static void io_poll_req_insert(struct io_kiocb *req)
4963{
4964 struct io_ring_ctx *ctx = req->ctx;
4965 struct hlist_head *list;
4966
4967 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4968 hlist_add_head(&req->hash_node, list);
4969}
4970
4971static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4972 struct io_poll_iocb *poll,
4973 struct io_poll_table *ipt, __poll_t mask,
4974 wait_queue_func_t wake_func)
4975 __acquires(&ctx->completion_lock)
4976{
4977 struct io_ring_ctx *ctx = req->ctx;
4978 bool cancel = false;
4979
Jens Axboe18bceab2020-05-15 11:56:54 -06004980 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004981 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004982 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004983
4984 ipt->pt._key = mask;
4985 ipt->req = req;
4986 ipt->error = -EINVAL;
4987
Jens Axboed7718a92020-02-14 22:23:12 -07004988 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4989
4990 spin_lock_irq(&ctx->completion_lock);
4991 if (likely(poll->head)) {
4992 spin_lock(&poll->head->lock);
4993 if (unlikely(list_empty(&poll->wait.entry))) {
4994 if (ipt->error)
4995 cancel = true;
4996 ipt->error = 0;
4997 mask = 0;
4998 }
4999 if (mask || ipt->error)
5000 list_del_init(&poll->wait.entry);
5001 else if (cancel)
5002 WRITE_ONCE(poll->canceled, true);
5003 else if (!poll->done) /* actually waiting for an event */
5004 io_poll_req_insert(req);
5005 spin_unlock(&poll->head->lock);
5006 }
5007
5008 return mask;
5009}
5010
5011static bool io_arm_poll_handler(struct io_kiocb *req)
5012{
5013 const struct io_op_def *def = &io_op_defs[req->opcode];
5014 struct io_ring_ctx *ctx = req->ctx;
5015 struct async_poll *apoll;
5016 struct io_poll_table ipt;
5017 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005018 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005019
5020 if (!req->file || !file_can_poll(req->file))
5021 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005022 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005023 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005024 if (def->pollin)
5025 rw = READ;
5026 else if (def->pollout)
5027 rw = WRITE;
5028 else
5029 return false;
5030 /* if we can't nonblock try, then no point in arming a poll handler */
5031 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005032 return false;
5033
5034 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5035 if (unlikely(!apoll))
5036 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005037 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005038
5039 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005040 req->apoll = apoll;
5041 INIT_HLIST_NODE(&req->hash_node);
5042
Nathan Chancellor8755d972020-03-02 16:01:19 -07005043 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005044 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005045 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005046 if (def->pollout)
5047 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005048
5049 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5050 if ((req->opcode == IORING_OP_RECVMSG) &&
5051 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5052 mask &= ~POLLIN;
5053
Jens Axboed7718a92020-02-14 22:23:12 -07005054 mask |= POLLERR | POLLPRI;
5055
5056 ipt.pt._qproc = io_async_queue_proc;
5057
5058 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5059 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005060 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005061 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005062 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005063 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005064 kfree(apoll);
5065 return false;
5066 }
5067 spin_unlock_irq(&ctx->completion_lock);
5068 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5069 apoll->poll.events);
5070 return true;
5071}
5072
5073static bool __io_poll_remove_one(struct io_kiocb *req,
5074 struct io_poll_iocb *poll)
5075{
Jens Axboeb41e9852020-02-17 09:52:41 -07005076 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005077
5078 spin_lock(&poll->head->lock);
5079 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005080 if (!list_empty(&poll->wait.entry)) {
5081 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005082 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005083 }
5084 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005085 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005086 return do_complete;
5087}
5088
5089static bool io_poll_remove_one(struct io_kiocb *req)
5090{
5091 bool do_complete;
5092
Jens Axboed4e7cd32020-08-15 11:44:50 -07005093 io_poll_remove_double(req);
5094
Jens Axboed7718a92020-02-14 22:23:12 -07005095 if (req->opcode == IORING_OP_POLL_ADD) {
5096 do_complete = __io_poll_remove_one(req, &req->poll);
5097 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005098 struct async_poll *apoll = req->apoll;
5099
Jens Axboed7718a92020-02-14 22:23:12 -07005100 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005101 do_complete = __io_poll_remove_one(req, &apoll->poll);
5102 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005103 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005104 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005105 kfree(apoll);
5106 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005107 }
5108
Jens Axboeb41e9852020-02-17 09:52:41 -07005109 if (do_complete) {
5110 io_cqring_fill_event(req, -ECANCELED);
5111 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005112 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005113 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005114 }
5115
5116 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005117}
5118
Jens Axboe76e1b642020-09-26 15:05:03 -06005119/*
5120 * Returns true if we found and killed one or more poll requests
5121 */
5122static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005123{
Jens Axboe78076bb2019-12-04 19:56:40 -07005124 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005125 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005126 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005127
5128 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005129 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5130 struct hlist_head *list;
5131
5132 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005133 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5134 if (io_task_match(req, tsk))
5135 posted += io_poll_remove_one(req);
5136 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005137 }
5138 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005139
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005140 if (posted)
5141 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005142
5143 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005144}
5145
Jens Axboe47f46762019-11-09 17:43:02 -07005146static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5147{
Jens Axboe78076bb2019-12-04 19:56:40 -07005148 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005149 struct io_kiocb *req;
5150
Jens Axboe78076bb2019-12-04 19:56:40 -07005151 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5152 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005153 if (sqe_addr != req->user_data)
5154 continue;
5155 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005156 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005157 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005158 }
5159
5160 return -ENOENT;
5161}
5162
Jens Axboe3529d8c2019-12-19 18:24:38 -07005163static int io_poll_remove_prep(struct io_kiocb *req,
5164 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005165{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005166 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5167 return -EINVAL;
5168 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5169 sqe->poll_events)
5170 return -EINVAL;
5171
Jens Axboe0969e782019-12-17 18:40:57 -07005172 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005173 return 0;
5174}
5175
5176/*
5177 * Find a running poll command that matches one specified in sqe->addr,
5178 * and remove it if found.
5179 */
5180static int io_poll_remove(struct io_kiocb *req)
5181{
5182 struct io_ring_ctx *ctx = req->ctx;
5183 u64 addr;
5184 int ret;
5185
Jens Axboe0969e782019-12-17 18:40:57 -07005186 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005187 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005188 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005189 spin_unlock_irq(&ctx->completion_lock);
5190
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005191 if (ret < 0)
5192 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005193 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005194 return 0;
5195}
5196
Jens Axboe221c5eb2019-01-17 09:41:58 -07005197static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5198 void *key)
5199{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005200 struct io_kiocb *req = wait->private;
5201 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005202
Jens Axboed7718a92020-02-14 22:23:12 -07005203 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005204}
5205
Jens Axboe221c5eb2019-01-17 09:41:58 -07005206static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5207 struct poll_table_struct *p)
5208{
5209 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5210
Jens Axboee8c2bc12020-08-15 18:44:09 -07005211 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005212}
5213
Jens Axboe3529d8c2019-12-19 18:24:38 -07005214static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005215{
5216 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005217 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005218
5219 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5220 return -EINVAL;
5221 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5222 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005223 if (!poll->file)
5224 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005225
Jiufei Xue5769a352020-06-17 17:53:55 +08005226 events = READ_ONCE(sqe->poll32_events);
5227#ifdef __BIG_ENDIAN
5228 events = swahw32(events);
5229#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005230 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5231 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005232 return 0;
5233}
5234
Pavel Begunkov014db002020-03-03 21:33:12 +03005235static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005236{
5237 struct io_poll_iocb *poll = &req->poll;
5238 struct io_ring_ctx *ctx = req->ctx;
5239 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005240 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005241
Jens Axboe78076bb2019-12-04 19:56:40 -07005242 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005243 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005244
Jens Axboed7718a92020-02-14 22:23:12 -07005245 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5246 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005247
Jens Axboe8c838782019-03-12 15:48:16 -06005248 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005249 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005250 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005251 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005252 spin_unlock_irq(&ctx->completion_lock);
5253
Jens Axboe8c838782019-03-12 15:48:16 -06005254 if (mask) {
5255 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005256 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005257 }
Jens Axboe8c838782019-03-12 15:48:16 -06005258 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005259}
5260
Jens Axboe5262f562019-09-17 12:26:57 -06005261static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5262{
Jens Axboead8a48a2019-11-15 08:49:11 -07005263 struct io_timeout_data *data = container_of(timer,
5264 struct io_timeout_data, timer);
5265 struct io_kiocb *req = data->req;
5266 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005267 unsigned long flags;
5268
Jens Axboe5262f562019-09-17 12:26:57 -06005269 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005270 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005271 atomic_set(&req->ctx->cq_timeouts,
5272 atomic_read(&req->ctx->cq_timeouts) + 1);
5273
Jens Axboe78e19bb2019-11-06 15:21:34 -07005274 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005275 io_commit_cqring(ctx);
5276 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5277
5278 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005279 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005280 io_put_req(req);
5281 return HRTIMER_NORESTART;
5282}
5283
Jens Axboef254ac02020-08-12 17:33:30 -06005284static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005285{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005286 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005287 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005288
Jens Axboee8c2bc12020-08-15 18:44:09 -07005289 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005290 if (ret == -1)
5291 return -EALREADY;
Pavel Begunkova71976f2020-10-10 18:34:11 +01005292 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005293
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005294 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005295 io_cqring_fill_event(req, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005296 io_put_req_deferred(req, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07005297 return 0;
5298}
5299
Jens Axboef254ac02020-08-12 17:33:30 -06005300static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5301{
5302 struct io_kiocb *req;
5303 int ret = -ENOENT;
5304
5305 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5306 if (user_data == req->user_data) {
5307 ret = 0;
5308 break;
5309 }
5310 }
5311
5312 if (ret == -ENOENT)
5313 return ret;
5314
5315 return __io_timeout_cancel(req);
5316}
5317
Jens Axboe3529d8c2019-12-19 18:24:38 -07005318static int io_timeout_remove_prep(struct io_kiocb *req,
5319 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005320{
Jens Axboeb29472e2019-12-17 18:50:29 -07005321 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5322 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005323 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5324 return -EINVAL;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005325 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags)
Jens Axboeb29472e2019-12-17 18:50:29 -07005326 return -EINVAL;
5327
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005328 req->timeout_rem.addr = READ_ONCE(sqe->addr);
Jens Axboeb29472e2019-12-17 18:50:29 -07005329 return 0;
5330}
5331
Jens Axboe11365042019-10-16 09:08:32 -06005332/*
5333 * Remove or update an existing timeout command
5334 */
Jens Axboefc4df992019-12-10 14:38:45 -07005335static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005336{
5337 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005338 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005339
Jens Axboe11365042019-10-16 09:08:32 -06005340 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005341 ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005342
Jens Axboe47f46762019-11-09 17:43:02 -07005343 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005344 io_commit_cqring(ctx);
5345 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005346 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005347 if (ret < 0)
5348 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005349 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005350 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005351}
5352
Jens Axboe3529d8c2019-12-19 18:24:38 -07005353static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005354 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005355{
Jens Axboead8a48a2019-11-15 08:49:11 -07005356 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005357 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005358 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005359
Jens Axboead8a48a2019-11-15 08:49:11 -07005360 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005361 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005362 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005363 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005364 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005365 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005366 flags = READ_ONCE(sqe->timeout_flags);
5367 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005368 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005369
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005370 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005371
Jens Axboee8c2bc12020-08-15 18:44:09 -07005372 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005373 return -ENOMEM;
5374
Jens Axboee8c2bc12020-08-15 18:44:09 -07005375 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005376 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005377
5378 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005379 return -EFAULT;
5380
Jens Axboe11365042019-10-16 09:08:32 -06005381 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005382 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005383 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005384 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005385
Jens Axboead8a48a2019-11-15 08:49:11 -07005386 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5387 return 0;
5388}
5389
Jens Axboefc4df992019-12-10 14:38:45 -07005390static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005391{
Jens Axboead8a48a2019-11-15 08:49:11 -07005392 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005393 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005394 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005395 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005396
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005397 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005398
Jens Axboe5262f562019-09-17 12:26:57 -06005399 /*
5400 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005401 * timeout event to be satisfied. If it isn't set, then this is
5402 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005403 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005404 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005405 entry = ctx->timeout_list.prev;
5406 goto add;
5407 }
Jens Axboe5262f562019-09-17 12:26:57 -06005408
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005409 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5410 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005411
5412 /*
5413 * Insertion sort, ensuring the first entry in the list is always
5414 * the one we need first.
5415 */
Jens Axboe5262f562019-09-17 12:26:57 -06005416 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005417 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5418 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005419
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005420 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005421 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005422 /* nxt.seq is behind @tail, otherwise would've been completed */
5423 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005424 break;
5425 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005426add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005427 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005428 data->timer.function = io_timeout_fn;
5429 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005430 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005431 return 0;
5432}
5433
Jens Axboe62755e32019-10-28 21:49:21 -06005434static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005435{
Jens Axboe62755e32019-10-28 21:49:21 -06005436 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005437
Jens Axboe62755e32019-10-28 21:49:21 -06005438 return req->user_data == (unsigned long) data;
5439}
5440
Jens Axboee977d6d2019-11-05 12:39:45 -07005441static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005442{
Jens Axboe62755e32019-10-28 21:49:21 -06005443 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005444 int ret = 0;
5445
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005446 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005447 switch (cancel_ret) {
5448 case IO_WQ_CANCEL_OK:
5449 ret = 0;
5450 break;
5451 case IO_WQ_CANCEL_RUNNING:
5452 ret = -EALREADY;
5453 break;
5454 case IO_WQ_CANCEL_NOTFOUND:
5455 ret = -ENOENT;
5456 break;
5457 }
5458
Jens Axboee977d6d2019-11-05 12:39:45 -07005459 return ret;
5460}
5461
Jens Axboe47f46762019-11-09 17:43:02 -07005462static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5463 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005464 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005465{
5466 unsigned long flags;
5467 int ret;
5468
5469 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5470 if (ret != -ENOENT) {
5471 spin_lock_irqsave(&ctx->completion_lock, flags);
5472 goto done;
5473 }
5474
5475 spin_lock_irqsave(&ctx->completion_lock, flags);
5476 ret = io_timeout_cancel(ctx, sqe_addr);
5477 if (ret != -ENOENT)
5478 goto done;
5479 ret = io_poll_cancel(ctx, sqe_addr);
5480done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005481 if (!ret)
5482 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005483 io_cqring_fill_event(req, ret);
5484 io_commit_cqring(ctx);
5485 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5486 io_cqring_ev_posted(ctx);
5487
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005488 if (ret < 0)
5489 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005490 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005491}
5492
Jens Axboe3529d8c2019-12-19 18:24:38 -07005493static int io_async_cancel_prep(struct io_kiocb *req,
5494 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005495{
Jens Axboefbf23842019-12-17 18:45:56 -07005496 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005497 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005498 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5499 return -EINVAL;
5500 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005501 return -EINVAL;
5502
Jens Axboefbf23842019-12-17 18:45:56 -07005503 req->cancel.addr = READ_ONCE(sqe->addr);
5504 return 0;
5505}
5506
Pavel Begunkov014db002020-03-03 21:33:12 +03005507static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005508{
5509 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005510
Pavel Begunkov014db002020-03-03 21:33:12 +03005511 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005512 return 0;
5513}
5514
Jens Axboe05f3fb32019-12-09 11:22:50 -07005515static int io_files_update_prep(struct io_kiocb *req,
5516 const struct io_uring_sqe *sqe)
5517{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005518 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5519 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005520 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5521 return -EINVAL;
5522 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005523 return -EINVAL;
5524
5525 req->files_update.offset = READ_ONCE(sqe->off);
5526 req->files_update.nr_args = READ_ONCE(sqe->len);
5527 if (!req->files_update.nr_args)
5528 return -EINVAL;
5529 req->files_update.arg = READ_ONCE(sqe->addr);
5530 return 0;
5531}
5532
Jens Axboe229a7b62020-06-22 10:13:11 -06005533static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5534 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005535{
5536 struct io_ring_ctx *ctx = req->ctx;
5537 struct io_uring_files_update up;
5538 int ret;
5539
Jens Axboef86cd202020-01-29 13:46:44 -07005540 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005541 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005542
5543 up.offset = req->files_update.offset;
5544 up.fds = req->files_update.arg;
5545
5546 mutex_lock(&ctx->uring_lock);
5547 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5548 mutex_unlock(&ctx->uring_lock);
5549
5550 if (ret < 0)
5551 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005552 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005553 return 0;
5554}
5555
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005556static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005557{
Jens Axboed625c6e2019-12-17 19:53:05 -07005558 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005559 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005560 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005561 case IORING_OP_READV:
5562 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005563 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005564 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005565 case IORING_OP_WRITEV:
5566 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005567 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005568 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005569 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005570 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005571 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005572 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005573 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005574 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005575 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005576 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005577 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005578 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005579 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005580 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005581 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005582 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005583 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005584 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005585 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005586 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005587 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005588 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005589 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005590 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005591 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005592 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005593 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005594 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005595 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005596 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005597 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005598 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005599 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005600 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005601 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005602 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005603 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005604 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005605 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005606 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005607 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005608 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005609 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005610 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005611 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005612 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005613 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005614 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005615 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005616 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005617 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005618 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005619 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005620 return io_tee_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005621 }
5622
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005623 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5624 req->opcode);
5625 return-EINVAL;
5626}
5627
Jens Axboedef596e2019-01-09 08:59:42 -07005628static int io_req_defer_prep(struct io_kiocb *req,
5629 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07005630{
Jens Axboedef596e2019-01-09 08:59:42 -07005631 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005632 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005633 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005634 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005635 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005636}
5637
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005638static u32 io_get_sequence(struct io_kiocb *req)
5639{
5640 struct io_kiocb *pos;
5641 struct io_ring_ctx *ctx = req->ctx;
5642 u32 total_submitted, nr_reqs = 1;
5643
5644 if (req->flags & REQ_F_LINK_HEAD)
5645 list_for_each_entry(pos, &req->link_list, link_list)
5646 nr_reqs++;
5647
5648 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5649 return total_submitted - nr_reqs;
5650}
5651
Jens Axboe3529d8c2019-12-19 18:24:38 -07005652static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005653{
5654 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005655 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005656 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005657 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005658
5659 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005660 if (likely(list_empty_careful(&ctx->defer_list) &&
5661 !(req->flags & REQ_F_IO_DRAIN)))
5662 return 0;
5663
5664 seq = io_get_sequence(req);
5665 /* Still a chance to pass the sequence check */
5666 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005667 return 0;
5668
Jens Axboee8c2bc12020-08-15 18:44:09 -07005669 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005670 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005671 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005672 return ret;
5673 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005674 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005675 de = kmalloc(sizeof(*de), GFP_KERNEL);
5676 if (!de)
5677 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005678
5679 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005680 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005681 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005682 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005683 io_queue_async_work(req);
5684 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005685 }
5686
5687 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005688 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005689 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005690 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005691 spin_unlock_irq(&ctx->completion_lock);
5692 return -EIOCBQUEUED;
5693}
Jens Axboeedafcce2019-01-09 09:16:05 -07005694
Jens Axboef573d382020-09-22 10:19:24 -06005695static void io_req_drop_files(struct io_kiocb *req)
5696{
5697 struct io_ring_ctx *ctx = req->ctx;
5698 unsigned long flags;
5699
5700 spin_lock_irqsave(&ctx->inflight_lock, flags);
5701 list_del(&req->inflight_entry);
5702 if (waitqueue_active(&ctx->inflight_wait))
5703 wake_up(&ctx->inflight_wait);
5704 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5705 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005706 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005707 put_nsproxy(req->work.nsproxy);
Jens Axboedfead8a2020-10-14 10:12:37 -06005708 req->work.flags &= ~IO_WQ_WORK_FILES;
Jens Axboef573d382020-09-22 10:19:24 -06005709}
5710
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005711static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005712{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005713 if (req->flags & REQ_F_BUFFER_SELECTED) {
5714 switch (req->opcode) {
5715 case IORING_OP_READV:
5716 case IORING_OP_READ_FIXED:
5717 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005718 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005719 break;
5720 case IORING_OP_RECVMSG:
5721 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005722 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005723 break;
5724 }
5725 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005726 }
5727
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005728 if (req->flags & REQ_F_NEED_CLEANUP) {
5729 switch (req->opcode) {
5730 case IORING_OP_READV:
5731 case IORING_OP_READ_FIXED:
5732 case IORING_OP_READ:
5733 case IORING_OP_WRITEV:
5734 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005735 case IORING_OP_WRITE: {
5736 struct io_async_rw *io = req->async_data;
5737 if (io->free_iovec)
5738 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005739 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005740 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005741 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005742 case IORING_OP_SENDMSG: {
5743 struct io_async_msghdr *io = req->async_data;
5744 if (io->iov != io->fast_iov)
5745 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005746 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005747 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005748 case IORING_OP_SPLICE:
5749 case IORING_OP_TEE:
5750 io_put_file(req, req->splice.file_in,
5751 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5752 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005753 case IORING_OP_OPENAT:
5754 case IORING_OP_OPENAT2:
5755 if (req->open.filename)
5756 putname(req->open.filename);
5757 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005758 }
5759 req->flags &= ~REQ_F_NEED_CLEANUP;
5760 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005761
Jens Axboef573d382020-09-22 10:19:24 -06005762 if (req->flags & REQ_F_INFLIGHT)
5763 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005764}
5765
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005766static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
5767 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07005768{
Jens Axboeedafcce2019-01-09 09:16:05 -07005769 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005770 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005771
Jens Axboed625c6e2019-12-17 19:53:05 -07005772 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005773 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005774 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005775 break;
5776 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005777 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005778 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06005779 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005780 break;
5781 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005782 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005783 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06005784 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005785 break;
5786 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03005787 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005788 break;
5789 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03005790 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005791 break;
5792 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07005793 ret = io_poll_remove(req);
5794 break;
5795 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005796 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07005797 break;
5798 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005799 ret = io_sendmsg(req, force_nonblock, cs);
5800 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005801 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005802 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07005803 break;
5804 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005805 ret = io_recvmsg(req, force_nonblock, cs);
5806 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005807 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005808 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005809 break;
5810 case IORING_OP_TIMEOUT:
5811 ret = io_timeout(req);
5812 break;
5813 case IORING_OP_TIMEOUT_REMOVE:
5814 ret = io_timeout_remove(req);
5815 break;
5816 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005817 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005818 break;
5819 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005820 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005821 break;
5822 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03005823 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005824 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005825 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005826 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005827 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005828 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03005829 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005830 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005831 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06005832 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005833 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005834 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06005835 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005836 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005837 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03005838 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005839 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005840 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005841 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005842 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005843 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005844 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005845 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005846 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03005847 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005848 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005849 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06005850 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005851 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005852 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005853 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005854 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005855 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06005856 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005857 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005858 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06005859 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005860 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005861 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005862 ret = io_tee(req, force_nonblock);
5863 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005864 default:
5865 ret = -EINVAL;
5866 break;
Jens Axboe31b51512019-01-18 22:56:34 -07005867 }
5868
5869 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07005870 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005871
Jens Axboeb5325762020-05-19 21:20:27 -06005872 /* If the op doesn't have a file, we're not polling for it */
5873 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005874 const bool in_async = io_wq_current_is_worker();
5875
Jens Axboe11ba8202020-01-15 21:51:17 -07005876 /* workqueue context doesn't hold uring_lock, grab it now */
5877 if (in_async)
5878 mutex_lock(&ctx->uring_lock);
5879
Jens Axboe2b188cc2019-01-07 10:46:33 -07005880 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005881
5882 if (in_async)
5883 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005884 }
5885
5886 return 0;
5887}
5888
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005889static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005890{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005891 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005892 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005893 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005894
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005895 timeout = io_prep_linked_timeout(req);
5896 if (timeout)
5897 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005898
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005899 /* if NO_CANCEL is set, we must still run the work */
5900 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5901 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005902 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005903 }
Jens Axboe31b51512019-01-18 22:56:34 -07005904
Jens Axboe561fb042019-10-24 07:25:42 -06005905 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005906 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005907 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005908 /*
5909 * We can get EAGAIN for polled IO even though we're
5910 * forcing a sync submission from here, since we can't
5911 * wait for request slots on the block side.
5912 */
5913 if (ret != -EAGAIN)
5914 break;
5915 cond_resched();
5916 } while (1);
5917 }
Jens Axboe31b51512019-01-18 22:56:34 -07005918
Jens Axboe561fb042019-10-24 07:25:42 -06005919 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005920 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005921 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005922 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005923
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005924 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005925}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005926
Jens Axboe65e19f52019-10-26 07:20:21 -06005927static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5928 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005929{
Jens Axboe65e19f52019-10-26 07:20:21 -06005930 struct fixed_file_table *table;
5931
Jens Axboe05f3fb32019-12-09 11:22:50 -07005932 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005933 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005934}
5935
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005936static struct file *io_file_get(struct io_submit_state *state,
5937 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005938{
5939 struct io_ring_ctx *ctx = req->ctx;
5940 struct file *file;
5941
5942 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01005943 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005944 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005945 fd = array_index_nospec(fd, ctx->nr_user_files);
5946 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005947 if (file) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01005948 req->fixed_file_refs = &ctx->file_data->node->refs;
Jens Axboefd2206e2020-06-02 16:40:47 -06005949 percpu_ref_get(req->fixed_file_refs);
5950 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005951 } else {
5952 trace_io_uring_file_get(ctx, fd);
5953 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005954 }
5955
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005956 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005957}
5958
Jens Axboe3529d8c2019-12-19 18:24:38 -07005959static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005960 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005961{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005962 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005963
Jens Axboe63ff8222020-05-07 14:56:15 -06005964 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005965 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005966 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005967
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005968 req->file = io_file_get(state, req, fd, fixed);
5969 if (req->file || io_op_defs[req->opcode].needs_file_no_error)
Jens Axboef86cd202020-01-29 13:46:44 -07005970 return 0;
Pavel Begunkov8371adf2020-10-10 18:34:08 +01005971 return -EBADF;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005972}
5973
Jens Axboe2665abf2019-11-05 12:40:47 -07005974static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5975{
Jens Axboead8a48a2019-11-15 08:49:11 -07005976 struct io_timeout_data *data = container_of(timer,
5977 struct io_timeout_data, timer);
5978 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005979 struct io_ring_ctx *ctx = req->ctx;
5980 struct io_kiocb *prev = NULL;
5981 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005982
5983 spin_lock_irqsave(&ctx->completion_lock, flags);
5984
5985 /*
5986 * We don't expect the list to be empty, that will only happen if we
5987 * race with the completion of the linked work.
5988 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005989 if (!list_empty(&req->link_list)) {
5990 prev = list_entry(req->link_list.prev, struct io_kiocb,
5991 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005992 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005993 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005994 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5995 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005996 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005997 }
5998
5999 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6000
6001 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006002 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006003 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006004 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006005 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006006 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006007 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006008 return HRTIMER_NORESTART;
6009}
6010
Jens Axboe7271ef32020-08-10 09:55:22 -06006011static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006012{
Jens Axboe76a46e02019-11-10 23:34:16 -07006013 /*
6014 * If the list is now empty, then our linked request finished before
6015 * we got a chance to setup the timer
6016 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006017 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006018 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006019
Jens Axboead8a48a2019-11-15 08:49:11 -07006020 data->timer.function = io_link_timeout_fn;
6021 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6022 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006023 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006024}
6025
6026static void io_queue_linked_timeout(struct io_kiocb *req)
6027{
6028 struct io_ring_ctx *ctx = req->ctx;
6029
6030 spin_lock_irq(&ctx->completion_lock);
6031 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006032 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006033
Jens Axboe2665abf2019-11-05 12:40:47 -07006034 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006035 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006036}
6037
Jens Axboead8a48a2019-11-15 08:49:11 -07006038static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006039{
6040 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006041
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006042 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006043 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006044 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006045 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006046
Pavel Begunkov44932332019-12-05 16:16:35 +03006047 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6048 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006049 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006050 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006051
Jens Axboe76a46e02019-11-10 23:34:16 -07006052 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006053 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006054}
6055
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006056static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006057{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006058 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006059 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006060 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006061 int ret;
6062
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006063again:
6064 linked_timeout = io_prep_linked_timeout(req);
6065
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006066 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6067 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006068 if (old_creds)
6069 revert_creds(old_creds);
6070 if (old_creds == req->work.creds)
6071 old_creds = NULL; /* restored original creds */
6072 else
6073 old_creds = override_creds(req->work.creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06006074 req->work.flags |= IO_WQ_WORK_CREDS;
Jens Axboe193155c2020-02-22 23:22:19 -07006075 }
6076
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006077 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006078
6079 /*
6080 * We async punt it if the file wasn't marked NOWAIT, or if the file
6081 * doesn't support non-blocking read/write attempts
6082 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006083 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006084 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006085punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006086 /*
6087 * Queued up for async execution, worker will release
6088 * submit reference when the iocb is actually submitted.
6089 */
6090 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006091 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006092
Pavel Begunkovf063c542020-07-25 14:41:59 +03006093 if (linked_timeout)
6094 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006095 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006096 }
Jens Axboee65ef562019-03-12 10:16:44 -06006097
Pavel Begunkov652532a2020-07-03 22:15:07 +03006098 if (unlikely(ret)) {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006099 /* un-prep timeout, so it'll be killed as any other linked */
6100 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006101 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006102 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006103 io_req_complete(req, ret);
6104 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006105 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006106
Jens Axboe6c271ce2019-01-10 11:22:30 -07006107 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006108 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006109 if (linked_timeout)
6110 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006111
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006112 if (nxt) {
6113 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006114
6115 if (req->flags & REQ_F_FORCE_ASYNC)
6116 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006117 goto again;
6118 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006119exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006120 if (old_creds)
6121 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006122}
6123
Jens Axboef13fad72020-06-22 09:34:30 -06006124static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6125 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006126{
6127 int ret;
6128
Jens Axboe3529d8c2019-12-19 18:24:38 -07006129 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006130 if (ret) {
6131 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006132fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006133 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006134 io_put_req(req);
6135 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006136 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006137 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006138 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006139 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006140 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006141 goto fail_req;
6142 }
6143
Jens Axboece35a472019-12-17 08:04:44 -07006144 /*
6145 * Never try inline submit of IOSQE_ASYNC is set, go straight
6146 * to async execution.
6147 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006148 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006149 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6150 io_queue_async_work(req);
6151 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006152 if (sqe) {
6153 ret = io_req_prep(req, sqe);
6154 if (unlikely(ret))
6155 goto fail_req;
6156 }
6157 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006158 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006159}
6160
Jens Axboef13fad72020-06-22 09:34:30 -06006161static inline void io_queue_link_head(struct io_kiocb *req,
6162 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006163{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006164 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006165 io_put_req(req);
6166 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006167 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006168 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006169}
6170
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006171static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006172 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006173{
Jackie Liua197f662019-11-08 08:09:12 -07006174 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006175 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006176
Jens Axboe9e645e112019-05-10 16:07:28 -06006177 /*
6178 * If we already have a head request, queue this one for async
6179 * submittal once the head completes. If we don't have a head but
6180 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6181 * submitted sync once the chain is complete. If none of those
6182 * conditions are true (normal request), then just queue it.
6183 */
6184 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006185 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006186
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006187 /*
6188 * Taking sequential execution of a link, draining both sides
6189 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6190 * requests in the link. So, it drains the head and the
6191 * next after the link request. The last one is done via
6192 * drain_next flag to persist the effect across calls.
6193 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006194 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006195 head->flags |= REQ_F_IO_DRAIN;
6196 ctx->drain_next = 1;
6197 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006198 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006199 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006200 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006201 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006202 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006203 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006204 trace_io_uring_link(ctx, req, head);
6205 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006206
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006207 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006208 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006209 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006210 *link = NULL;
6211 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006212 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006213 if (unlikely(ctx->drain_next)) {
6214 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006215 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006216 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006217 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006218 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006219 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006220
Pavel Begunkov711be032020-01-17 03:57:59 +03006221 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006222 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006223 req->flags |= REQ_F_FAIL_LINK;
6224 *link = req;
6225 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006226 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006227 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006228 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006229
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006230 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006231}
6232
Jens Axboe9a56a232019-01-09 09:06:50 -07006233/*
6234 * Batched submission is done, ensure local IO is flushed out.
6235 */
6236static void io_submit_state_end(struct io_submit_state *state)
6237{
Jens Axboef13fad72020-06-22 09:34:30 -06006238 if (!list_empty(&state->comp.list))
6239 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006240 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006241 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006242 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006243 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006244}
6245
6246/*
6247 * Start submission side cache.
6248 */
6249static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006250 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006251{
6252 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006253 state->comp.nr = 0;
6254 INIT_LIST_HEAD(&state->comp.list);
6255 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006256 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006257 state->file = NULL;
6258 state->ios_left = max_ios;
6259}
6260
Jens Axboe2b188cc2019-01-07 10:46:33 -07006261static void io_commit_sqring(struct io_ring_ctx *ctx)
6262{
Hristo Venev75b28af2019-08-26 17:23:46 +00006263 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006264
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006265 /*
6266 * Ensure any loads from the SQEs are done at this point,
6267 * since once we write the new head, the application could
6268 * write new data to them.
6269 */
6270 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006271}
6272
6273/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006274 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006275 * that is mapped by userspace. This means that care needs to be taken to
6276 * ensure that reads are stable, as we cannot rely on userspace always
6277 * being a good citizen. If members of the sqe are validated and then later
6278 * used, it's important that those reads are done through READ_ONCE() to
6279 * prevent a re-load down the line.
6280 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006281static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006282{
Hristo Venev75b28af2019-08-26 17:23:46 +00006283 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006284 unsigned head;
6285
6286 /*
6287 * The cached sq head (or cq tail) serves two purposes:
6288 *
6289 * 1) allows us to batch the cost of updating the user visible
6290 * head updates.
6291 * 2) allows the kernel side to track the head on its own, even
6292 * though the application is the one updating it.
6293 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006294 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006295 if (likely(head < ctx->sq_entries))
6296 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006297
6298 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006299 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006300 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006301 return NULL;
6302}
6303
6304static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6305{
6306 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006307}
6308
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006309/*
6310 * Check SQE restrictions (opcode and flags).
6311 *
6312 * Returns 'true' if SQE is allowed, 'false' otherwise.
6313 */
6314static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6315 struct io_kiocb *req,
6316 unsigned int sqe_flags)
6317{
6318 if (!ctx->restricted)
6319 return true;
6320
6321 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6322 return false;
6323
6324 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6325 ctx->restrictions.sqe_flags_required)
6326 return false;
6327
6328 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6329 ctx->restrictions.sqe_flags_required))
6330 return false;
6331
6332 return true;
6333}
6334
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006335#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6336 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6337 IOSQE_BUFFER_SELECT)
6338
6339static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6340 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006341 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006342{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006343 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006344 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006345
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006346 req->opcode = READ_ONCE(sqe->opcode);
6347 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006348 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006349 req->file = NULL;
6350 req->ctx = ctx;
6351 req->flags = 0;
6352 /* one is dropped after submission, the other at completion */
6353 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006354 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006355 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006356
6357 if (unlikely(req->opcode >= IORING_OP_LAST))
6358 return -EINVAL;
6359
Jens Axboe9d8426a2020-06-16 18:42:49 -06006360 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6361 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006362
6363 sqe_flags = READ_ONCE(sqe->flags);
6364 /* enforce forwards compatibility on users */
6365 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6366 return -EINVAL;
6367
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006368 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6369 return -EACCES;
6370
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006371 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6372 !io_op_defs[req->opcode].buffer_select)
6373 return -EOPNOTSUPP;
6374
6375 id = READ_ONCE(sqe->personality);
6376 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006377 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006378 req->work.creds = idr_find(&ctx->personality_idr, id);
6379 if (unlikely(!req->work.creds))
6380 return -EINVAL;
6381 get_cred(req->work.creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06006382 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006383 }
6384
6385 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006386 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006387
Jens Axboe63ff8222020-05-07 14:56:15 -06006388 if (!io_op_defs[req->opcode].needs_file)
6389 return 0;
6390
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006391 ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
6392 state->ios_left--;
6393 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006394}
6395
Jens Axboe0f212202020-09-13 13:09:39 -06006396static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006397{
Jens Axboeac8691c2020-06-01 08:30:41 -06006398 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006399 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006400 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006401
Jens Axboec4a2ed72019-11-21 21:01:26 -07006402 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006403 if (test_bit(0, &ctx->sq_check_overflow)) {
6404 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006405 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006406 return -EBUSY;
6407 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006408
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006409 /* make sure SQ entry isn't read before tail */
6410 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006411
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006412 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6413 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006414
Jens Axboefaf7b512020-10-07 12:48:53 -06006415 atomic_long_add(nr, &current->io_uring->req_issue);
6416 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006417
Jens Axboe6c271ce2019-01-10 11:22:30 -07006418 io_submit_state_start(&state, ctx, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006419
Jens Axboe6c271ce2019-01-10 11:22:30 -07006420 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006421 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006422 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006423 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006424
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006425 sqe = io_get_sqe(ctx);
6426 if (unlikely(!sqe)) {
6427 io_consume_sqe(ctx);
6428 break;
6429 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006430 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006431 if (unlikely(!req)) {
6432 if (!submitted)
6433 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006434 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006435 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006436 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006437 /* will complete beyond this point, count as submitted */
6438 submitted++;
6439
Pavel Begunkov692d8362020-10-10 18:34:13 +01006440 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006441 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006442fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006443 io_put_req(req);
6444 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006445 break;
6446 }
6447
Jens Axboe354420f2020-01-08 18:55:15 -07006448 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006449 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006450 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006451 if (err)
6452 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006453 }
6454
Pavel Begunkov9466f432020-01-25 22:34:01 +03006455 if (unlikely(submitted != nr)) {
6456 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6457
6458 percpu_ref_put_many(&ctx->refs, nr - ref_used);
Jens Axboefaf7b512020-10-07 12:48:53 -06006459 atomic_long_sub(nr - ref_used, &current->io_uring->req_issue);
6460 put_task_struct_many(current, nr - ref_used);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006461 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006462 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006463 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006464 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006465
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006466 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6467 io_commit_sqring(ctx);
6468
Jens Axboe6c271ce2019-01-10 11:22:30 -07006469 return submitted;
6470}
6471
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006472static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6473{
6474 /* Tell userspace we may need a wakeup call */
6475 spin_lock_irq(&ctx->completion_lock);
6476 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6477 spin_unlock_irq(&ctx->completion_lock);
6478}
6479
6480static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6481{
6482 spin_lock_irq(&ctx->completion_lock);
6483 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6484 spin_unlock_irq(&ctx->completion_lock);
6485}
6486
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006487static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6488 int sync, void *key)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006489{
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006490 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6491 int ret;
6492
6493 ret = autoremove_wake_function(wqe, mode, sync, key);
6494 if (ret) {
6495 unsigned long flags;
6496
6497 spin_lock_irqsave(&ctx->completion_lock, flags);
6498 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6499 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6500 }
6501 return ret;
6502}
6503
Jens Axboec8d1ba52020-09-14 11:07:26 -06006504enum sq_ret {
6505 SQT_IDLE = 1,
6506 SQT_SPIN = 2,
6507 SQT_DID_WORK = 4,
6508};
6509
6510static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006511 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006512{
6513 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006514 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006515 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006516 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006517
Jens Axboec8d1ba52020-09-14 11:07:26 -06006518again:
6519 if (!list_empty(&ctx->iopoll_list)) {
6520 unsigned nr_events = 0;
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006521
Jens Axboec8d1ba52020-09-14 11:07:26 -06006522 mutex_lock(&ctx->uring_lock);
6523 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6524 io_do_iopoll(ctx, &nr_events, 0);
6525 mutex_unlock(&ctx->uring_lock);
6526 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006527
Jens Axboec8d1ba52020-09-14 11:07:26 -06006528 to_submit = io_sqring_entries(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006529
Jens Axboec8d1ba52020-09-14 11:07:26 -06006530 /*
6531 * If submit got -EBUSY, flag us as needing the application
6532 * to enter the kernel to reap and flush events.
6533 */
6534 if (!to_submit || ret == -EBUSY || need_resched()) {
6535 /*
6536 * Drop cur_mm before scheduling, we can't hold it for
6537 * long periods (or over schedule()). Do this before
6538 * adding ourselves to the waitqueue, as the unuse/drop
6539 * may sleep.
6540 */
6541 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006542
Jens Axboec8d1ba52020-09-14 11:07:26 -06006543 /*
6544 * We're polling. If we're within the defined idle
6545 * period, then let us spin without work before going
6546 * to sleep. The exception is if we got EBUSY doing
6547 * more IO, we should wait for the application to
6548 * reap events and wake us up.
6549 */
6550 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6551 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6552 !percpu_ref_is_dying(&ctx->refs)))
6553 return SQT_SPIN;
6554
Jens Axboe534ca6d2020-09-02 13:52:19 -06006555 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006556 TASK_INTERRUPTIBLE);
6557
6558 /*
6559 * While doing polled IO, before going to sleep, we need
6560 * to check if there are new reqs added to iopoll_list,
6561 * it is because reqs may have been punted to io worker
6562 * and will be added to iopoll_list later, hence check
6563 * the iopoll_list again.
6564 */
6565 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6566 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006567 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006568 goto again;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006569 }
6570
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006571 to_submit = io_sqring_entries(ctx);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006572 if (!to_submit || ret == -EBUSY)
6573 return SQT_IDLE;
6574 }
6575
Jens Axboe534ca6d2020-09-02 13:52:19 -06006576 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006577 io_ring_clear_wakeup_flag(ctx);
6578
Jens Axboee95eee22020-09-08 09:11:32 -06006579 /* if we're handling multiple rings, cap submit size for fairness */
6580 if (cap_entries && to_submit > 8)
6581 to_submit = 8;
6582
Jens Axboec8d1ba52020-09-14 11:07:26 -06006583 mutex_lock(&ctx->uring_lock);
6584 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6585 ret = io_submit_sqes(ctx, to_submit);
6586 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006587
6588 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6589 wake_up(&ctx->sqo_sq_wait);
6590
Jens Axboec8d1ba52020-09-14 11:07:26 -06006591 return SQT_DID_WORK;
6592}
6593
Jens Axboe69fb2132020-09-14 11:16:23 -06006594static void io_sqd_init_new(struct io_sq_data *sqd)
6595{
6596 struct io_ring_ctx *ctx;
6597
6598 while (!list_empty(&sqd->ctx_new_list)) {
6599 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6600 init_wait(&ctx->sqo_wait_entry);
6601 ctx->sqo_wait_entry.func = io_sq_wake_function;
6602 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6603 complete(&ctx->sq_thread_comp);
6604 }
6605}
6606
Jens Axboe6c271ce2019-01-10 11:22:30 -07006607static int io_sq_thread(void *data)
6608{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006609 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006610 const struct cred *old_cred = NULL;
6611 struct io_sq_data *sqd = data;
6612 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006613 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006614
Jens Axboec8d1ba52020-09-14 11:07:26 -06006615 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006616 while (!kthread_should_stop()) {
6617 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006618 bool cap_entries;
Jens Axboec1edbf52019-11-10 16:56:04 -07006619
6620 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006621 * Any changes to the sqd lists are synchronized through the
6622 * kthread parking. This synchronizes the thread vs users,
6623 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006624 */
Jens Axboe69fb2132020-09-14 11:16:23 -06006625 if (kthread_should_park())
6626 kthread_parkme();
6627
6628 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6629 io_sqd_init_new(sqd);
6630
Jens Axboee95eee22020-09-08 09:11:32 -06006631 cap_entries = !list_is_singular(&sqd->ctx_list);
6632
Jens Axboe69fb2132020-09-14 11:16:23 -06006633 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6634 if (current->cred != ctx->creds) {
6635 if (old_cred)
6636 revert_creds(old_cred);
6637 old_cred = override_creds(ctx->creds);
6638 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006639 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe69fb2132020-09-14 11:16:23 -06006640
Jens Axboee95eee22020-09-08 09:11:32 -06006641 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006642
Jens Axboe4349f302020-07-09 15:07:01 -06006643 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006644 }
6645
Jens Axboe69fb2132020-09-14 11:16:23 -06006646 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006647 io_run_task_work();
6648 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006649 } else if (ret == SQT_IDLE) {
6650 if (kthread_should_park())
6651 continue;
6652 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6653 io_ring_set_wakeup_flag(ctx);
6654 schedule();
6655 start_jiffies = jiffies;
6656 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6657 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006658 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006659 }
6660
Jens Axboe4c6e2772020-07-01 11:29:10 -06006661 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006662
Dennis Zhou91d8f512020-09-16 13:41:05 -07006663 if (cur_css)
6664 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006665 if (old_cred)
6666 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006667
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006668 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006669
Jens Axboe6c271ce2019-01-10 11:22:30 -07006670 return 0;
6671}
6672
Jens Axboebda52162019-09-24 13:47:15 -06006673struct io_wait_queue {
6674 struct wait_queue_entry wq;
6675 struct io_ring_ctx *ctx;
6676 unsigned to_wait;
6677 unsigned nr_timeouts;
6678};
6679
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006680static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006681{
6682 struct io_ring_ctx *ctx = iowq->ctx;
6683
6684 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006685 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006686 * started waiting. For timeouts, we always want to return to userspace,
6687 * regardless of event count.
6688 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006689 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006690 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6691}
6692
6693static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6694 int wake_flags, void *key)
6695{
6696 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6697 wq);
6698
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006699 /* use noflush == true, as we can't safely rely on locking context */
6700 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006701 return -1;
6702
6703 return autoremove_wake_function(curr, mode, wake_flags, key);
6704}
6705
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006706static int io_run_task_work_sig(void)
6707{
6708 if (io_run_task_work())
6709 return 1;
6710 if (!signal_pending(current))
6711 return 0;
6712 if (current->jobctl & JOBCTL_TASK_WORK) {
6713 spin_lock_irq(&current->sighand->siglock);
6714 current->jobctl &= ~JOBCTL_TASK_WORK;
6715 recalc_sigpending();
6716 spin_unlock_irq(&current->sighand->siglock);
6717 return 1;
6718 }
6719 return -EINTR;
6720}
6721
Jens Axboe2b188cc2019-01-07 10:46:33 -07006722/*
6723 * Wait until events become available, if we don't already have some. The
6724 * application must reap them itself, as they reside on the shared cq ring.
6725 */
6726static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6727 const sigset_t __user *sig, size_t sigsz)
6728{
Jens Axboebda52162019-09-24 13:47:15 -06006729 struct io_wait_queue iowq = {
6730 .wq = {
6731 .private = current,
6732 .func = io_wake_function,
6733 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6734 },
6735 .ctx = ctx,
6736 .to_wait = min_events,
6737 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006738 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006739 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740
Jens Axboeb41e9852020-02-17 09:52:41 -07006741 do {
6742 if (io_cqring_events(ctx, false) >= min_events)
6743 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006744 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006745 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006746 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006747
6748 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006749#ifdef CONFIG_COMPAT
6750 if (in_compat_syscall())
6751 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006752 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006753 else
6754#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006755 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006756
Jens Axboe2b188cc2019-01-07 10:46:33 -07006757 if (ret)
6758 return ret;
6759 }
6760
Jens Axboebda52162019-09-24 13:47:15 -06006761 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006762 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006763 do {
6764 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6765 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006766 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006767 ret = io_run_task_work_sig();
6768 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06006769 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006770 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06006771 break;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006772 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006773 break;
6774 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006775 } while (1);
6776 finish_wait(&ctx->wait, &iowq.wq);
6777
Jens Axboeb7db41c2020-07-04 08:55:50 -06006778 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006779
Hristo Venev75b28af2019-08-26 17:23:46 +00006780 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006781}
6782
Jens Axboe6b063142019-01-10 22:13:58 -07006783static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6784{
6785#if defined(CONFIG_UNIX)
6786 if (ctx->ring_sock) {
6787 struct sock *sock = ctx->ring_sock->sk;
6788 struct sk_buff *skb;
6789
6790 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6791 kfree_skb(skb);
6792 }
6793#else
6794 int i;
6795
Jens Axboe65e19f52019-10-26 07:20:21 -06006796 for (i = 0; i < ctx->nr_user_files; i++) {
6797 struct file *file;
6798
6799 file = io_file_from_index(ctx, i);
6800 if (file)
6801 fput(file);
6802 }
Jens Axboe6b063142019-01-10 22:13:58 -07006803#endif
6804}
6805
Jens Axboe05f3fb32019-12-09 11:22:50 -07006806static void io_file_ref_kill(struct percpu_ref *ref)
6807{
6808 struct fixed_file_data *data;
6809
6810 data = container_of(ref, struct fixed_file_data, refs);
6811 complete(&data->done);
6812}
6813
Jens Axboe6b063142019-01-10 22:13:58 -07006814static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6815{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006816 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006817 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006818 unsigned nr_tables, i;
6819
Jens Axboe05f3fb32019-12-09 11:22:50 -07006820 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006821 return -ENXIO;
6822
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006823 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006824 if (!list_empty(&data->ref_list))
6825 ref_node = list_first_entry(&data->ref_list,
6826 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006827 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006828 if (ref_node)
6829 percpu_ref_kill(&ref_node->refs);
6830
6831 percpu_ref_kill(&data->refs);
6832
6833 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006834 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006835 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006836
Jens Axboe6b063142019-01-10 22:13:58 -07006837 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006838 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6839 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006840 kfree(data->table[i].files);
6841 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006842 percpu_ref_exit(&data->refs);
6843 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006844 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006845 ctx->nr_user_files = 0;
6846 return 0;
6847}
6848
Jens Axboe534ca6d2020-09-02 13:52:19 -06006849static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006850{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006851 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006852 /*
6853 * The park is a bit of a work-around, without it we get
6854 * warning spews on shutdown with SQPOLL set and affinity
6855 * set to a single CPU.
6856 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06006857 if (sqd->thread) {
6858 kthread_park(sqd->thread);
6859 kthread_stop(sqd->thread);
6860 }
6861
6862 kfree(sqd);
6863 }
6864}
6865
Jens Axboeaa061652020-09-02 14:50:27 -06006866static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
6867{
6868 struct io_ring_ctx *ctx_attach;
6869 struct io_sq_data *sqd;
6870 struct fd f;
6871
6872 f = fdget(p->wq_fd);
6873 if (!f.file)
6874 return ERR_PTR(-ENXIO);
6875 if (f.file->f_op != &io_uring_fops) {
6876 fdput(f);
6877 return ERR_PTR(-EINVAL);
6878 }
6879
6880 ctx_attach = f.file->private_data;
6881 sqd = ctx_attach->sq_data;
6882 if (!sqd) {
6883 fdput(f);
6884 return ERR_PTR(-EINVAL);
6885 }
6886
6887 refcount_inc(&sqd->refs);
6888 fdput(f);
6889 return sqd;
6890}
6891
Jens Axboe534ca6d2020-09-02 13:52:19 -06006892static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
6893{
6894 struct io_sq_data *sqd;
6895
Jens Axboeaa061652020-09-02 14:50:27 -06006896 if (p->flags & IORING_SETUP_ATTACH_WQ)
6897 return io_attach_sq_data(p);
6898
Jens Axboe534ca6d2020-09-02 13:52:19 -06006899 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
6900 if (!sqd)
6901 return ERR_PTR(-ENOMEM);
6902
6903 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06006904 INIT_LIST_HEAD(&sqd->ctx_list);
6905 INIT_LIST_HEAD(&sqd->ctx_new_list);
6906 mutex_init(&sqd->ctx_lock);
6907 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06006908 init_waitqueue_head(&sqd->wait);
6909 return sqd;
6910}
6911
Jens Axboe69fb2132020-09-14 11:16:23 -06006912static void io_sq_thread_unpark(struct io_sq_data *sqd)
6913 __releases(&sqd->lock)
6914{
6915 if (!sqd->thread)
6916 return;
6917 kthread_unpark(sqd->thread);
6918 mutex_unlock(&sqd->lock);
6919}
6920
6921static void io_sq_thread_park(struct io_sq_data *sqd)
6922 __acquires(&sqd->lock)
6923{
6924 if (!sqd->thread)
6925 return;
6926 mutex_lock(&sqd->lock);
6927 kthread_park(sqd->thread);
6928}
6929
Jens Axboe534ca6d2020-09-02 13:52:19 -06006930static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6931{
6932 struct io_sq_data *sqd = ctx->sq_data;
6933
6934 if (sqd) {
6935 if (sqd->thread) {
6936 /*
6937 * We may arrive here from the error branch in
6938 * io_sq_offload_create() where the kthread is created
6939 * without being waked up, thus wake it up now to make
6940 * sure the wait will complete.
6941 */
6942 wake_up_process(sqd->thread);
6943 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06006944
6945 io_sq_thread_park(sqd);
6946 }
6947
6948 mutex_lock(&sqd->ctx_lock);
6949 list_del(&ctx->sqd_list);
6950 mutex_unlock(&sqd->ctx_lock);
6951
6952 if (sqd->thread) {
6953 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
6954 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06006955 }
6956
6957 io_put_sq_data(sqd);
6958 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006959 }
6960}
6961
Jens Axboe6b063142019-01-10 22:13:58 -07006962static void io_finish_async(struct io_ring_ctx *ctx)
6963{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006964 io_sq_thread_stop(ctx);
6965
Jens Axboe561fb042019-10-24 07:25:42 -06006966 if (ctx->io_wq) {
6967 io_wq_destroy(ctx->io_wq);
6968 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006969 }
6970}
6971
6972#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006973/*
6974 * Ensure the UNIX gc is aware of our file set, so we are certain that
6975 * the io_uring can be safely unregistered on process exit, even if we have
6976 * loops in the file referencing.
6977 */
6978static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6979{
6980 struct sock *sk = ctx->ring_sock->sk;
6981 struct scm_fp_list *fpl;
6982 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006983 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006984
Jens Axboe6b063142019-01-10 22:13:58 -07006985 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6986 if (!fpl)
6987 return -ENOMEM;
6988
6989 skb = alloc_skb(0, GFP_KERNEL);
6990 if (!skb) {
6991 kfree(fpl);
6992 return -ENOMEM;
6993 }
6994
6995 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006996
Jens Axboe08a45172019-10-03 08:11:03 -06006997 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006998 fpl->user = get_uid(ctx->user);
6999 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007000 struct file *file = io_file_from_index(ctx, i + offset);
7001
7002 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007003 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007004 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007005 unix_inflight(fpl->user, fpl->fp[nr_files]);
7006 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007007 }
7008
Jens Axboe08a45172019-10-03 08:11:03 -06007009 if (nr_files) {
7010 fpl->max = SCM_MAX_FD;
7011 fpl->count = nr_files;
7012 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007013 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007014 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7015 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007016
Jens Axboe08a45172019-10-03 08:11:03 -06007017 for (i = 0; i < nr_files; i++)
7018 fput(fpl->fp[i]);
7019 } else {
7020 kfree_skb(skb);
7021 kfree(fpl);
7022 }
Jens Axboe6b063142019-01-10 22:13:58 -07007023
7024 return 0;
7025}
7026
7027/*
7028 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7029 * causes regular reference counting to break down. We rely on the UNIX
7030 * garbage collection to take care of this problem for us.
7031 */
7032static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7033{
7034 unsigned left, total;
7035 int ret = 0;
7036
7037 total = 0;
7038 left = ctx->nr_user_files;
7039 while (left) {
7040 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007041
7042 ret = __io_sqe_files_scm(ctx, this_files, total);
7043 if (ret)
7044 break;
7045 left -= this_files;
7046 total += this_files;
7047 }
7048
7049 if (!ret)
7050 return 0;
7051
7052 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007053 struct file *file = io_file_from_index(ctx, total);
7054
7055 if (file)
7056 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007057 total++;
7058 }
7059
7060 return ret;
7061}
7062#else
7063static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7064{
7065 return 0;
7066}
7067#endif
7068
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007069static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7070 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007071{
7072 int i;
7073
7074 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007075 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007076 unsigned this_files;
7077
7078 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7079 table->files = kcalloc(this_files, sizeof(struct file *),
7080 GFP_KERNEL);
7081 if (!table->files)
7082 break;
7083 nr_files -= this_files;
7084 }
7085
7086 if (i == nr_tables)
7087 return 0;
7088
7089 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007090 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007091 kfree(table->files);
7092 }
7093 return 1;
7094}
7095
Jens Axboe05f3fb32019-12-09 11:22:50 -07007096static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007097{
7098#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007099 struct sock *sock = ctx->ring_sock->sk;
7100 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7101 struct sk_buff *skb;
7102 int i;
7103
7104 __skb_queue_head_init(&list);
7105
7106 /*
7107 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7108 * remove this entry and rearrange the file array.
7109 */
7110 skb = skb_dequeue(head);
7111 while (skb) {
7112 struct scm_fp_list *fp;
7113
7114 fp = UNIXCB(skb).fp;
7115 for (i = 0; i < fp->count; i++) {
7116 int left;
7117
7118 if (fp->fp[i] != file)
7119 continue;
7120
7121 unix_notinflight(fp->user, fp->fp[i]);
7122 left = fp->count - 1 - i;
7123 if (left) {
7124 memmove(&fp->fp[i], &fp->fp[i + 1],
7125 left * sizeof(struct file *));
7126 }
7127 fp->count--;
7128 if (!fp->count) {
7129 kfree_skb(skb);
7130 skb = NULL;
7131 } else {
7132 __skb_queue_tail(&list, skb);
7133 }
7134 fput(file);
7135 file = NULL;
7136 break;
7137 }
7138
7139 if (!file)
7140 break;
7141
7142 __skb_queue_tail(&list, skb);
7143
7144 skb = skb_dequeue(head);
7145 }
7146
7147 if (skb_peek(&list)) {
7148 spin_lock_irq(&head->lock);
7149 while ((skb = __skb_dequeue(&list)) != NULL)
7150 __skb_queue_tail(head, skb);
7151 spin_unlock_irq(&head->lock);
7152 }
7153#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007154 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007155#endif
7156}
7157
Jens Axboe05f3fb32019-12-09 11:22:50 -07007158struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007159 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007160 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007161};
7162
Jens Axboe4a38aed22020-05-14 17:21:15 -06007163static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007164{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007165 struct fixed_file_data *file_data = ref_node->file_data;
7166 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007167 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007168
7169 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007170 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007171 io_ring_file_put(ctx, pfile->file);
7172 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007173 }
7174
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007175 spin_lock(&file_data->lock);
7176 list_del(&ref_node->node);
7177 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007178
Xiaoguang Wang05589552020-03-31 14:05:18 +08007179 percpu_ref_exit(&ref_node->refs);
7180 kfree(ref_node);
7181 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007182}
7183
Jens Axboe4a38aed22020-05-14 17:21:15 -06007184static void io_file_put_work(struct work_struct *work)
7185{
7186 struct io_ring_ctx *ctx;
7187 struct llist_node *node;
7188
7189 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7190 node = llist_del_all(&ctx->file_put_llist);
7191
7192 while (node) {
7193 struct fixed_file_ref_node *ref_node;
7194 struct llist_node *next = node->next;
7195
7196 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7197 __io_file_put_work(ref_node);
7198 node = next;
7199 }
7200}
7201
Jens Axboe05f3fb32019-12-09 11:22:50 -07007202static void io_file_data_ref_zero(struct percpu_ref *ref)
7203{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007204 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007205 struct io_ring_ctx *ctx;
7206 bool first_add;
7207 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007208
Xiaoguang Wang05589552020-03-31 14:05:18 +08007209 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007210 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007211
Jens Axboe4a38aed22020-05-14 17:21:15 -06007212 if (percpu_ref_is_dying(&ctx->file_data->refs))
7213 delay = 0;
7214
7215 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7216 if (!delay)
7217 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7218 else if (first_add)
7219 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007220}
7221
7222static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7223 struct io_ring_ctx *ctx)
7224{
7225 struct fixed_file_ref_node *ref_node;
7226
7227 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7228 if (!ref_node)
7229 return ERR_PTR(-ENOMEM);
7230
7231 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7232 0, GFP_KERNEL)) {
7233 kfree(ref_node);
7234 return ERR_PTR(-ENOMEM);
7235 }
7236 INIT_LIST_HEAD(&ref_node->node);
7237 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007238 ref_node->file_data = ctx->file_data;
7239 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007240}
7241
7242static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7243{
7244 percpu_ref_exit(&ref_node->refs);
7245 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007246}
7247
7248static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7249 unsigned nr_args)
7250{
7251 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007252 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007253 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007254 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007255 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007256 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007257
7258 if (ctx->file_data)
7259 return -EBUSY;
7260 if (!nr_args)
7261 return -EINVAL;
7262 if (nr_args > IORING_MAX_FIXED_FILES)
7263 return -EMFILE;
7264
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007265 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7266 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007267 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007268 file_data->ctx = ctx;
7269 init_completion(&file_data->done);
7270 INIT_LIST_HEAD(&file_data->ref_list);
7271 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007272
7273 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007274 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007275 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007276 if (!file_data->table)
7277 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007278
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007279 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007280 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7281 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007282
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007283 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7284 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007285 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007286
7287 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7288 struct fixed_file_table *table;
7289 unsigned index;
7290
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007291 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7292 ret = -EFAULT;
7293 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007294 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007295 /* allow sparse sets */
7296 if (fd == -1)
7297 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007298
Jens Axboe05f3fb32019-12-09 11:22:50 -07007299 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007300 ret = -EBADF;
7301 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007302 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007303
7304 /*
7305 * Don't allow io_uring instances to be registered. If UNIX
7306 * isn't enabled, then this causes a reference cycle and this
7307 * instance can never get freed. If UNIX is enabled we'll
7308 * handle it just fine, but there's still no point in allowing
7309 * a ring fd as it doesn't support regular read/write anyway.
7310 */
7311 if (file->f_op == &io_uring_fops) {
7312 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007313 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007314 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007315 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7316 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007317 table->files[index] = file;
7318 }
7319
Jens Axboe05f3fb32019-12-09 11:22:50 -07007320 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007321 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007322 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007323 return ret;
7324 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007325
Xiaoguang Wang05589552020-03-31 14:05:18 +08007326 ref_node = alloc_fixed_file_ref_node(ctx);
7327 if (IS_ERR(ref_node)) {
7328 io_sqe_files_unregister(ctx);
7329 return PTR_ERR(ref_node);
7330 }
7331
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007332 file_data->node = ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007333 spin_lock(&file_data->lock);
7334 list_add(&ref_node->node, &file_data->ref_list);
7335 spin_unlock(&file_data->lock);
7336 percpu_ref_get(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007337 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007338out_fput:
7339 for (i = 0; i < ctx->nr_user_files; i++) {
7340 file = io_file_from_index(ctx, i);
7341 if (file)
7342 fput(file);
7343 }
7344 for (i = 0; i < nr_tables; i++)
7345 kfree(file_data->table[i].files);
7346 ctx->nr_user_files = 0;
7347out_ref:
7348 percpu_ref_exit(&file_data->refs);
7349out_free:
7350 kfree(file_data->table);
7351 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007352 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007353 return ret;
7354}
7355
Jens Axboec3a31e62019-10-03 13:59:56 -06007356static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7357 int index)
7358{
7359#if defined(CONFIG_UNIX)
7360 struct sock *sock = ctx->ring_sock->sk;
7361 struct sk_buff_head *head = &sock->sk_receive_queue;
7362 struct sk_buff *skb;
7363
7364 /*
7365 * See if we can merge this file into an existing skb SCM_RIGHTS
7366 * file set. If there's no room, fall back to allocating a new skb
7367 * and filling it in.
7368 */
7369 spin_lock_irq(&head->lock);
7370 skb = skb_peek(head);
7371 if (skb) {
7372 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7373
7374 if (fpl->count < SCM_MAX_FD) {
7375 __skb_unlink(skb, head);
7376 spin_unlock_irq(&head->lock);
7377 fpl->fp[fpl->count] = get_file(file);
7378 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7379 fpl->count++;
7380 spin_lock_irq(&head->lock);
7381 __skb_queue_head(head, skb);
7382 } else {
7383 skb = NULL;
7384 }
7385 }
7386 spin_unlock_irq(&head->lock);
7387
7388 if (skb) {
7389 fput(file);
7390 return 0;
7391 }
7392
7393 return __io_sqe_files_scm(ctx, 1, index);
7394#else
7395 return 0;
7396#endif
7397}
7398
Hillf Dantona5318d32020-03-23 17:47:15 +08007399static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007400 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007401{
Hillf Dantona5318d32020-03-23 17:47:15 +08007402 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007403 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007404
Jens Axboe05f3fb32019-12-09 11:22:50 -07007405 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007406 if (!pfile)
7407 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007408
7409 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007410 list_add(&pfile->list, &ref_node->file_list);
7411
Hillf Dantona5318d32020-03-23 17:47:15 +08007412 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007413}
7414
7415static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7416 struct io_uring_files_update *up,
7417 unsigned nr_args)
7418{
7419 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007420 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007421 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007422 __s32 __user *fds;
7423 int fd, i, err;
7424 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007425 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007426
Jens Axboe05f3fb32019-12-09 11:22:50 -07007427 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007428 return -EOVERFLOW;
7429 if (done > ctx->nr_user_files)
7430 return -EINVAL;
7431
Xiaoguang Wang05589552020-03-31 14:05:18 +08007432 ref_node = alloc_fixed_file_ref_node(ctx);
7433 if (IS_ERR(ref_node))
7434 return PTR_ERR(ref_node);
7435
Jens Axboec3a31e62019-10-03 13:59:56 -06007436 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007437 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007438 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007439 struct fixed_file_table *table;
7440 unsigned index;
7441
Jens Axboec3a31e62019-10-03 13:59:56 -06007442 err = 0;
7443 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7444 err = -EFAULT;
7445 break;
7446 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007447 i = array_index_nospec(up->offset, ctx->nr_user_files);
7448 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007449 index = i & IORING_FILE_TABLE_MASK;
7450 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007451 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007452 err = io_queue_file_removal(data, file);
7453 if (err)
7454 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007455 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007456 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007457 }
7458 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007459 file = fget(fd);
7460 if (!file) {
7461 err = -EBADF;
7462 break;
7463 }
7464 /*
7465 * Don't allow io_uring instances to be registered. If
7466 * UNIX isn't enabled, then this causes a reference
7467 * cycle and this instance can never get freed. If UNIX
7468 * is enabled we'll handle it just fine, but there's
7469 * still no point in allowing a ring fd as it doesn't
7470 * support regular read/write anyway.
7471 */
7472 if (file->f_op == &io_uring_fops) {
7473 fput(file);
7474 err = -EBADF;
7475 break;
7476 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007477 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007478 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007479 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007480 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007481 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007482 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007483 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007484 }
7485 nr_args--;
7486 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007487 up->offset++;
7488 }
7489
Xiaoguang Wang05589552020-03-31 14:05:18 +08007490 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007491 percpu_ref_kill(&data->node->refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007492 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007493 list_add(&ref_node->node, &data->ref_list);
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007494 data->node = ref_node;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007495 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007496 percpu_ref_get(&ctx->file_data->refs);
7497 } else
7498 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007499
7500 return done ? done : err;
7501}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007502
Jens Axboe05f3fb32019-12-09 11:22:50 -07007503static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7504 unsigned nr_args)
7505{
7506 struct io_uring_files_update up;
7507
7508 if (!ctx->file_data)
7509 return -ENXIO;
7510 if (!nr_args)
7511 return -EINVAL;
7512 if (copy_from_user(&up, arg, sizeof(up)))
7513 return -EFAULT;
7514 if (up.resv)
7515 return -EINVAL;
7516
7517 return __io_sqe_files_update(ctx, &up, nr_args);
7518}
Jens Axboec3a31e62019-10-03 13:59:56 -06007519
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007520static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007521{
7522 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7523
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007524 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007525 io_put_req(req);
7526}
7527
Pavel Begunkov24369c22020-01-28 03:15:48 +03007528static int io_init_wq_offload(struct io_ring_ctx *ctx,
7529 struct io_uring_params *p)
7530{
7531 struct io_wq_data data;
7532 struct fd f;
7533 struct io_ring_ctx *ctx_attach;
7534 unsigned int concurrency;
7535 int ret = 0;
7536
7537 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007538 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007539 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007540
7541 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7542 /* Do QD, or 4 * CPUS, whatever is smallest */
7543 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7544
7545 ctx->io_wq = io_wq_create(concurrency, &data);
7546 if (IS_ERR(ctx->io_wq)) {
7547 ret = PTR_ERR(ctx->io_wq);
7548 ctx->io_wq = NULL;
7549 }
7550 return ret;
7551 }
7552
7553 f = fdget(p->wq_fd);
7554 if (!f.file)
7555 return -EBADF;
7556
7557 if (f.file->f_op != &io_uring_fops) {
7558 ret = -EINVAL;
7559 goto out_fput;
7560 }
7561
7562 ctx_attach = f.file->private_data;
7563 /* @io_wq is protected by holding the fd */
7564 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7565 ret = -EINVAL;
7566 goto out_fput;
7567 }
7568
7569 ctx->io_wq = ctx_attach->io_wq;
7570out_fput:
7571 fdput(f);
7572 return ret;
7573}
7574
Jens Axboe0f212202020-09-13 13:09:39 -06007575static int io_uring_alloc_task_context(struct task_struct *task)
7576{
7577 struct io_uring_task *tctx;
7578
7579 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7580 if (unlikely(!tctx))
7581 return -ENOMEM;
7582
7583 xa_init(&tctx->xa);
7584 init_waitqueue_head(&tctx->wait);
7585 tctx->last = NULL;
7586 tctx->in_idle = 0;
7587 atomic_long_set(&tctx->req_issue, 0);
7588 atomic_long_set(&tctx->req_complete, 0);
7589 task->io_uring = tctx;
7590 return 0;
7591}
7592
7593void __io_uring_free(struct task_struct *tsk)
7594{
7595 struct io_uring_task *tctx = tsk->io_uring;
7596
7597 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe0f212202020-09-13 13:09:39 -06007598 kfree(tctx);
7599 tsk->io_uring = NULL;
7600}
7601
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007602static int io_sq_offload_create(struct io_ring_ctx *ctx,
7603 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007604{
7605 int ret;
7606
Jens Axboe6c271ce2019-01-10 11:22:30 -07007607 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007608 struct io_sq_data *sqd;
7609
Jens Axboe3ec482d2019-04-08 10:51:01 -06007610 ret = -EPERM;
7611 if (!capable(CAP_SYS_ADMIN))
7612 goto err;
7613
Jens Axboe534ca6d2020-09-02 13:52:19 -06007614 sqd = io_get_sq_data(p);
7615 if (IS_ERR(sqd)) {
7616 ret = PTR_ERR(sqd);
7617 goto err;
7618 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007619
Jens Axboe534ca6d2020-09-02 13:52:19 -06007620 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007621 io_sq_thread_park(sqd);
7622 mutex_lock(&sqd->ctx_lock);
7623 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7624 mutex_unlock(&sqd->ctx_lock);
7625 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007626
Jens Axboe917257d2019-04-13 09:28:55 -06007627 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7628 if (!ctx->sq_thread_idle)
7629 ctx->sq_thread_idle = HZ;
7630
Jens Axboeaa061652020-09-02 14:50:27 -06007631 if (sqd->thread)
7632 goto done;
7633
Jens Axboe6c271ce2019-01-10 11:22:30 -07007634 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007635 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007636
Jens Axboe917257d2019-04-13 09:28:55 -06007637 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007638 if (cpu >= nr_cpu_ids)
7639 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007640 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007641 goto err;
7642
Jens Axboe69fb2132020-09-14 11:16:23 -06007643 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007644 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007645 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007646 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007647 "io_uring-sq");
7648 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007649 if (IS_ERR(sqd->thread)) {
7650 ret = PTR_ERR(sqd->thread);
7651 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007652 goto err;
7653 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007654 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007655 if (ret)
7656 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007657 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7658 /* Can't have SQ_AFF without SQPOLL */
7659 ret = -EINVAL;
7660 goto err;
7661 }
7662
Jens Axboeaa061652020-09-02 14:50:27 -06007663done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007664 ret = io_init_wq_offload(ctx, p);
7665 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007666 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007667
7668 return 0;
7669err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007670 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007671 return ret;
7672}
7673
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007674static void io_sq_offload_start(struct io_ring_ctx *ctx)
7675{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007676 struct io_sq_data *sqd = ctx->sq_data;
7677
7678 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7679 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007680}
7681
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007682static inline void __io_unaccount_mem(struct user_struct *user,
7683 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007684{
7685 atomic_long_sub(nr_pages, &user->locked_vm);
7686}
7687
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007688static inline int __io_account_mem(struct user_struct *user,
7689 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007690{
7691 unsigned long page_limit, cur_pages, new_pages;
7692
7693 /* Don't allow more pages than we can safely lock */
7694 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7695
7696 do {
7697 cur_pages = atomic_long_read(&user->locked_vm);
7698 new_pages = cur_pages + nr_pages;
7699 if (new_pages > page_limit)
7700 return -ENOMEM;
7701 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7702 new_pages) != cur_pages);
7703
7704 return 0;
7705}
7706
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007707static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7708 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007709{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007710 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007711 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007712
Jens Axboe2aede0e2020-09-14 10:45:53 -06007713 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007714 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007715 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007716 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007717 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007718 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007719}
7720
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007721static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7722 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007723{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007724 int ret;
7725
7726 if (ctx->limit_mem) {
7727 ret = __io_account_mem(ctx->user, nr_pages);
7728 if (ret)
7729 return ret;
7730 }
7731
Jens Axboe2aede0e2020-09-14 10:45:53 -06007732 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007733 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007734 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007735 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007736 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007737 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007738
7739 return 0;
7740}
7741
Jens Axboe2b188cc2019-01-07 10:46:33 -07007742static void io_mem_free(void *ptr)
7743{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007744 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007745
Mark Rutland52e04ef2019-04-30 17:30:21 +01007746 if (!ptr)
7747 return;
7748
7749 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007750 if (put_page_testzero(page))
7751 free_compound_page(page);
7752}
7753
7754static void *io_mem_alloc(size_t size)
7755{
7756 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7757 __GFP_NORETRY;
7758
7759 return (void *) __get_free_pages(gfp_flags, get_order(size));
7760}
7761
Hristo Venev75b28af2019-08-26 17:23:46 +00007762static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7763 size_t *sq_offset)
7764{
7765 struct io_rings *rings;
7766 size_t off, sq_array_size;
7767
7768 off = struct_size(rings, cqes, cq_entries);
7769 if (off == SIZE_MAX)
7770 return SIZE_MAX;
7771
7772#ifdef CONFIG_SMP
7773 off = ALIGN(off, SMP_CACHE_BYTES);
7774 if (off == 0)
7775 return SIZE_MAX;
7776#endif
7777
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007778 if (sq_offset)
7779 *sq_offset = off;
7780
Hristo Venev75b28af2019-08-26 17:23:46 +00007781 sq_array_size = array_size(sizeof(u32), sq_entries);
7782 if (sq_array_size == SIZE_MAX)
7783 return SIZE_MAX;
7784
7785 if (check_add_overflow(off, sq_array_size, &off))
7786 return SIZE_MAX;
7787
Hristo Venev75b28af2019-08-26 17:23:46 +00007788 return off;
7789}
7790
Jens Axboe2b188cc2019-01-07 10:46:33 -07007791static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7792{
Hristo Venev75b28af2019-08-26 17:23:46 +00007793 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007794
Hristo Venev75b28af2019-08-26 17:23:46 +00007795 pages = (size_t)1 << get_order(
7796 rings_size(sq_entries, cq_entries, NULL));
7797 pages += (size_t)1 << get_order(
7798 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007799
Hristo Venev75b28af2019-08-26 17:23:46 +00007800 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007801}
7802
Jens Axboeedafcce2019-01-09 09:16:05 -07007803static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7804{
7805 int i, j;
7806
7807 if (!ctx->user_bufs)
7808 return -ENXIO;
7809
7810 for (i = 0; i < ctx->nr_user_bufs; i++) {
7811 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7812
7813 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007814 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007815
Jens Axboede293932020-09-17 16:19:16 -06007816 if (imu->acct_pages)
7817 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007818 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007819 imu->nr_bvecs = 0;
7820 }
7821
7822 kfree(ctx->user_bufs);
7823 ctx->user_bufs = NULL;
7824 ctx->nr_user_bufs = 0;
7825 return 0;
7826}
7827
7828static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7829 void __user *arg, unsigned index)
7830{
7831 struct iovec __user *src;
7832
7833#ifdef CONFIG_COMPAT
7834 if (ctx->compat) {
7835 struct compat_iovec __user *ciovs;
7836 struct compat_iovec ciov;
7837
7838 ciovs = (struct compat_iovec __user *) arg;
7839 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7840 return -EFAULT;
7841
Jens Axboed55e5f52019-12-11 16:12:15 -07007842 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007843 dst->iov_len = ciov.iov_len;
7844 return 0;
7845 }
7846#endif
7847 src = (struct iovec __user *) arg;
7848 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7849 return -EFAULT;
7850 return 0;
7851}
7852
Jens Axboede293932020-09-17 16:19:16 -06007853/*
7854 * Not super efficient, but this is just a registration time. And we do cache
7855 * the last compound head, so generally we'll only do a full search if we don't
7856 * match that one.
7857 *
7858 * We check if the given compound head page has already been accounted, to
7859 * avoid double accounting it. This allows us to account the full size of the
7860 * page, not just the constituent pages of a huge page.
7861 */
7862static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
7863 int nr_pages, struct page *hpage)
7864{
7865 int i, j;
7866
7867 /* check current page array */
7868 for (i = 0; i < nr_pages; i++) {
7869 if (!PageCompound(pages[i]))
7870 continue;
7871 if (compound_head(pages[i]) == hpage)
7872 return true;
7873 }
7874
7875 /* check previously registered pages */
7876 for (i = 0; i < ctx->nr_user_bufs; i++) {
7877 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7878
7879 for (j = 0; j < imu->nr_bvecs; j++) {
7880 if (!PageCompound(imu->bvec[j].bv_page))
7881 continue;
7882 if (compound_head(imu->bvec[j].bv_page) == hpage)
7883 return true;
7884 }
7885 }
7886
7887 return false;
7888}
7889
7890static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
7891 int nr_pages, struct io_mapped_ubuf *imu,
7892 struct page **last_hpage)
7893{
7894 int i, ret;
7895
7896 for (i = 0; i < nr_pages; i++) {
7897 if (!PageCompound(pages[i])) {
7898 imu->acct_pages++;
7899 } else {
7900 struct page *hpage;
7901
7902 hpage = compound_head(pages[i]);
7903 if (hpage == *last_hpage)
7904 continue;
7905 *last_hpage = hpage;
7906 if (headpage_already_acct(ctx, pages, i, hpage))
7907 continue;
7908 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
7909 }
7910 }
7911
7912 if (!imu->acct_pages)
7913 return 0;
7914
7915 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
7916 if (ret)
7917 imu->acct_pages = 0;
7918 return ret;
7919}
7920
Jens Axboeedafcce2019-01-09 09:16:05 -07007921static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7922 unsigned nr_args)
7923{
7924 struct vm_area_struct **vmas = NULL;
7925 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06007926 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07007927 int i, j, got_pages = 0;
7928 int ret = -EINVAL;
7929
7930 if (ctx->user_bufs)
7931 return -EBUSY;
7932 if (!nr_args || nr_args > UIO_MAXIOV)
7933 return -EINVAL;
7934
7935 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7936 GFP_KERNEL);
7937 if (!ctx->user_bufs)
7938 return -ENOMEM;
7939
7940 for (i = 0; i < nr_args; i++) {
7941 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7942 unsigned long off, start, end, ubuf;
7943 int pret, nr_pages;
7944 struct iovec iov;
7945 size_t size;
7946
7947 ret = io_copy_iov(ctx, &iov, arg, i);
7948 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007949 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007950
7951 /*
7952 * Don't impose further limits on the size and buffer
7953 * constraints here, we'll -EINVAL later when IO is
7954 * submitted if they are wrong.
7955 */
7956 ret = -EFAULT;
7957 if (!iov.iov_base || !iov.iov_len)
7958 goto err;
7959
7960 /* arbitrary limit, but we need something */
7961 if (iov.iov_len > SZ_1G)
7962 goto err;
7963
7964 ubuf = (unsigned long) iov.iov_base;
7965 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7966 start = ubuf >> PAGE_SHIFT;
7967 nr_pages = end - start;
7968
Jens Axboeedafcce2019-01-09 09:16:05 -07007969 ret = 0;
7970 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007971 kvfree(vmas);
7972 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007973 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007974 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007975 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007976 sizeof(struct vm_area_struct *),
7977 GFP_KERNEL);
7978 if (!pages || !vmas) {
7979 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07007980 goto err;
7981 }
7982 got_pages = nr_pages;
7983 }
7984
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007985 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007986 GFP_KERNEL);
7987 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06007988 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07007989 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007990
7991 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007992 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007993 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007994 FOLL_WRITE | FOLL_LONGTERM,
7995 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007996 if (pret == nr_pages) {
7997 /* don't support file backed memory */
7998 for (j = 0; j < nr_pages; j++) {
7999 struct vm_area_struct *vma = vmas[j];
8000
8001 if (vma->vm_file &&
8002 !is_file_hugepages(vma->vm_file)) {
8003 ret = -EOPNOTSUPP;
8004 break;
8005 }
8006 }
8007 } else {
8008 ret = pret < 0 ? pret : -EFAULT;
8009 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008010 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008011 if (ret) {
8012 /*
8013 * if we did partial map, or found file backed vmas,
8014 * release any pages we did get
8015 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008016 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008017 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008018 kvfree(imu->bvec);
8019 goto err;
8020 }
8021
8022 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8023 if (ret) {
8024 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008025 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008026 goto err;
8027 }
8028
8029 off = ubuf & ~PAGE_MASK;
8030 size = iov.iov_len;
8031 for (j = 0; j < nr_pages; j++) {
8032 size_t vec_len;
8033
8034 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8035 imu->bvec[j].bv_page = pages[j];
8036 imu->bvec[j].bv_len = vec_len;
8037 imu->bvec[j].bv_offset = off;
8038 off = 0;
8039 size -= vec_len;
8040 }
8041 /* store original address for later verification */
8042 imu->ubuf = ubuf;
8043 imu->len = iov.iov_len;
8044 imu->nr_bvecs = nr_pages;
8045
8046 ctx->nr_user_bufs++;
8047 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008048 kvfree(pages);
8049 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008050 return 0;
8051err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008052 kvfree(pages);
8053 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008054 io_sqe_buffer_unregister(ctx);
8055 return ret;
8056}
8057
Jens Axboe9b402842019-04-11 11:45:41 -06008058static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8059{
8060 __s32 __user *fds = arg;
8061 int fd;
8062
8063 if (ctx->cq_ev_fd)
8064 return -EBUSY;
8065
8066 if (copy_from_user(&fd, fds, sizeof(*fds)))
8067 return -EFAULT;
8068
8069 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8070 if (IS_ERR(ctx->cq_ev_fd)) {
8071 int ret = PTR_ERR(ctx->cq_ev_fd);
8072 ctx->cq_ev_fd = NULL;
8073 return ret;
8074 }
8075
8076 return 0;
8077}
8078
8079static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8080{
8081 if (ctx->cq_ev_fd) {
8082 eventfd_ctx_put(ctx->cq_ev_fd);
8083 ctx->cq_ev_fd = NULL;
8084 return 0;
8085 }
8086
8087 return -ENXIO;
8088}
8089
Jens Axboe5a2e7452020-02-23 16:23:11 -07008090static int __io_destroy_buffers(int id, void *p, void *data)
8091{
8092 struct io_ring_ctx *ctx = data;
8093 struct io_buffer *buf = p;
8094
Jens Axboe067524e2020-03-02 16:32:28 -07008095 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008096 return 0;
8097}
8098
8099static void io_destroy_buffers(struct io_ring_ctx *ctx)
8100{
8101 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8102 idr_destroy(&ctx->io_buffer_idr);
8103}
8104
Jens Axboe2b188cc2019-01-07 10:46:33 -07008105static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8106{
Jens Axboe6b063142019-01-10 22:13:58 -07008107 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008108 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008109
8110 if (ctx->sqo_task) {
8111 put_task_struct(ctx->sqo_task);
8112 ctx->sqo_task = NULL;
8113 mmdrop(ctx->mm_account);
8114 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008115 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008116
Dennis Zhou91d8f512020-09-16 13:41:05 -07008117#ifdef CONFIG_BLK_CGROUP
8118 if (ctx->sqo_blkcg_css)
8119 css_put(ctx->sqo_blkcg_css);
8120#endif
8121
Jens Axboe6b063142019-01-10 22:13:58 -07008122 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008123 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008124 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008125 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008126
Jens Axboe2b188cc2019-01-07 10:46:33 -07008127#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008128 if (ctx->ring_sock) {
8129 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008130 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008131 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008132#endif
8133
Hristo Venev75b28af2019-08-26 17:23:46 +00008134 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008135 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008136
8137 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008138 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008139 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008140 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008141 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008142 kfree(ctx);
8143}
8144
8145static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8146{
8147 struct io_ring_ctx *ctx = file->private_data;
8148 __poll_t mask = 0;
8149
8150 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008151 /*
8152 * synchronizes with barrier from wq_has_sleeper call in
8153 * io_commit_cqring
8154 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008155 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008156 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008157 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008158 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008159 mask |= EPOLLIN | EPOLLRDNORM;
8160
8161 return mask;
8162}
8163
8164static int io_uring_fasync(int fd, struct file *file, int on)
8165{
8166 struct io_ring_ctx *ctx = file->private_data;
8167
8168 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8169}
8170
Jens Axboe071698e2020-01-28 10:04:42 -07008171static int io_remove_personalities(int id, void *p, void *data)
8172{
8173 struct io_ring_ctx *ctx = data;
8174 const struct cred *cred;
8175
8176 cred = idr_remove(&ctx->personality_idr, id);
8177 if (cred)
8178 put_cred(cred);
8179 return 0;
8180}
8181
Jens Axboe85faa7b2020-04-09 18:14:00 -06008182static void io_ring_exit_work(struct work_struct *work)
8183{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008184 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8185 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008186
Jens Axboe56952e92020-06-17 15:00:04 -06008187 /*
8188 * If we're doing polled IO and end up having requests being
8189 * submitted async (out-of-line), then completions can come in while
8190 * we're waiting for refs to drop. We need to reap these manually,
8191 * as nobody else will be looking for them.
8192 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008193 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008194 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008195 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008196 io_iopoll_try_reap_events(ctx);
8197 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008198 io_ring_ctx_free(ctx);
8199}
8200
Jens Axboe2b188cc2019-01-07 10:46:33 -07008201static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8202{
8203 mutex_lock(&ctx->uring_lock);
8204 percpu_ref_kill(&ctx->refs);
8205 mutex_unlock(&ctx->uring_lock);
8206
Jens Axboef3606e32020-09-22 08:18:24 -06008207 io_kill_timeouts(ctx, NULL);
8208 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008209
8210 if (ctx->io_wq)
8211 io_wq_cancel_all(ctx->io_wq);
8212
Jens Axboe15dff282019-11-13 09:09:23 -07008213 /* if we failed setting up the ctx, we might not have any rings */
8214 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008215 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008216 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008217 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008218
8219 /*
8220 * Do this upfront, so we won't have a grace period where the ring
8221 * is closed but resources aren't reaped yet. This can cause
8222 * spurious failure in setting up a new ring.
8223 */
Jens Axboe760618f2020-07-24 12:53:31 -06008224 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8225 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008226
Jens Axboe85faa7b2020-04-09 18:14:00 -06008227 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008228 /*
8229 * Use system_unbound_wq to avoid spawning tons of event kworkers
8230 * if we're exiting a ton of rings at the same time. It just adds
8231 * noise and overhead, there's no discernable change in runtime
8232 * over using system_wq.
8233 */
8234 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008235}
8236
8237static int io_uring_release(struct inode *inode, struct file *file)
8238{
8239 struct io_ring_ctx *ctx = file->private_data;
8240
8241 file->private_data = NULL;
8242 io_ring_ctx_wait_and_kill(ctx);
8243 return 0;
8244}
8245
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008246static bool io_wq_files_match(struct io_wq_work *work, void *data)
8247{
8248 struct files_struct *files = data;
8249
Jens Axboedfead8a2020-10-14 10:12:37 -06008250 return !files || ((work->flags & IO_WQ_WORK_FILES) &&
8251 work->files == files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008252}
8253
Jens Axboef254ac02020-08-12 17:33:30 -06008254/*
8255 * Returns true if 'preq' is the link parent of 'req'
8256 */
8257static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8258{
8259 struct io_kiocb *link;
8260
8261 if (!(preq->flags & REQ_F_LINK_HEAD))
8262 return false;
8263
8264 list_for_each_entry(link, &preq->link_list, link_list) {
8265 if (link == req)
8266 return true;
8267 }
8268
8269 return false;
8270}
8271
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008272static bool io_match_link_files(struct io_kiocb *req,
8273 struct files_struct *files)
8274{
8275 struct io_kiocb *link;
8276
8277 if (io_match_files(req, files))
8278 return true;
8279 if (req->flags & REQ_F_LINK_HEAD) {
8280 list_for_each_entry(link, &req->link_list, link_list) {
8281 if (io_match_files(link, files))
8282 return true;
8283 }
8284 }
8285 return false;
8286}
8287
Jens Axboef254ac02020-08-12 17:33:30 -06008288/*
8289 * We're looking to cancel 'req' because it's holding on to our files, but
8290 * 'req' could be a link to another request. See if it is, and cancel that
8291 * parent request if so.
8292 */
8293static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8294{
8295 struct hlist_node *tmp;
8296 struct io_kiocb *preq;
8297 bool found = false;
8298 int i;
8299
8300 spin_lock_irq(&ctx->completion_lock);
8301 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8302 struct hlist_head *list;
8303
8304 list = &ctx->cancel_hash[i];
8305 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8306 found = io_match_link(preq, req);
8307 if (found) {
8308 io_poll_remove_one(preq);
8309 break;
8310 }
8311 }
8312 }
8313 spin_unlock_irq(&ctx->completion_lock);
8314 return found;
8315}
8316
8317static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8318 struct io_kiocb *req)
8319{
8320 struct io_kiocb *preq;
8321 bool found = false;
8322
8323 spin_lock_irq(&ctx->completion_lock);
8324 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8325 found = io_match_link(preq, req);
8326 if (found) {
8327 __io_timeout_cancel(preq);
8328 break;
8329 }
8330 }
8331 spin_unlock_irq(&ctx->completion_lock);
8332 return found;
8333}
8334
Jens Axboeb711d4e2020-08-16 08:23:05 -07008335static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8336{
8337 return io_match_link(container_of(work, struct io_kiocb, work), data);
8338}
8339
8340static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8341{
8342 enum io_wq_cancel cret;
8343
8344 /* cancel this particular work, if it's running */
8345 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8346 if (cret != IO_WQ_CANCEL_NOTFOUND)
8347 return;
8348
8349 /* find links that hold this pending, cancel those */
8350 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8351 if (cret != IO_WQ_CANCEL_NOTFOUND)
8352 return;
8353
8354 /* if we have a poll link holding this pending, cancel that */
8355 if (io_poll_remove_link(ctx, req))
8356 return;
8357
8358 /* final option, timeout link is holding this req pending */
8359 io_timeout_remove_link(ctx, req);
8360}
8361
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008362static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8363 struct files_struct *files)
8364{
8365 struct io_defer_entry *de = NULL;
8366 LIST_HEAD(list);
8367
8368 spin_lock_irq(&ctx->completion_lock);
8369 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008370 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008371 list_cut_position(&list, &ctx->defer_list, &de->list);
8372 break;
8373 }
8374 }
8375 spin_unlock_irq(&ctx->completion_lock);
8376
8377 while (!list_empty(&list)) {
8378 de = list_first_entry(&list, struct io_defer_entry, list);
8379 list_del_init(&de->list);
8380 req_set_fail_links(de->req);
8381 io_put_req(de->req);
8382 io_req_complete(de->req, -ECANCELED);
8383 kfree(de);
8384 }
8385}
8386
Jens Axboe76e1b642020-09-26 15:05:03 -06008387/*
8388 * Returns true if we found and killed one or more files pinning requests
8389 */
8390static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008391 struct files_struct *files)
8392{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008393 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008394 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008395
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008396 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008397 /* cancel all at once, should be faster than doing it one by one*/
8398 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8399
Jens Axboefcb323c2019-10-24 12:39:47 -06008400 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008401 struct io_kiocb *cancel_req = NULL, *req;
8402 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008403
8404 spin_lock_irq(&ctx->inflight_lock);
8405 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboedfead8a2020-10-14 10:12:37 -06008406 if (files && (req->work.flags & IO_WQ_WORK_FILES) &&
8407 req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008408 continue;
8409 /* req is being completed, ignore */
8410 if (!refcount_inc_not_zero(&req->refs))
8411 continue;
8412 cancel_req = req;
8413 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008414 }
Jens Axboe768134d2019-11-10 20:30:53 -07008415 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008416 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008417 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008418 spin_unlock_irq(&ctx->inflight_lock);
8419
Jens Axboe768134d2019-11-10 20:30:53 -07008420 /* We need to keep going until we don't find a matching req */
8421 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008422 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008423 /* cancel this request, or head link requests */
8424 io_attempt_cancel(ctx, cancel_req);
8425 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008426 /* cancellations _may_ trigger task work */
8427 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008428 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008429 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008430 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008431
8432 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008433}
8434
Pavel Begunkov801dd572020-06-15 10:33:14 +03008435static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008436{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008437 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8438 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008439
Jens Axboef3606e32020-09-22 08:18:24 -06008440 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008441}
8442
Jens Axboe0f212202020-09-13 13:09:39 -06008443static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8444 struct task_struct *task,
8445 struct files_struct *files)
8446{
8447 bool ret;
8448
8449 ret = io_uring_cancel_files(ctx, files);
8450 if (!files) {
8451 enum io_wq_cancel cret;
8452
8453 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8454 if (cret != IO_WQ_CANCEL_NOTFOUND)
8455 ret = true;
8456
8457 /* SQPOLL thread does its own polling */
8458 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8459 while (!list_empty_careful(&ctx->iopoll_list)) {
8460 io_iopoll_try_reap_events(ctx);
8461 ret = true;
8462 }
8463 }
8464
8465 ret |= io_poll_remove_all(ctx, task);
8466 ret |= io_kill_timeouts(ctx, task);
8467 }
8468
8469 return ret;
8470}
8471
8472/*
8473 * We need to iteratively cancel requests, in case a request has dependent
8474 * hard links. These persist even for failure of cancelations, hence keep
8475 * looping until none are found.
8476 */
8477static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8478 struct files_struct *files)
8479{
8480 struct task_struct *task = current;
8481
Jens Axboe534ca6d2020-09-02 13:52:19 -06008482 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8483 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008484
8485 io_cqring_overflow_flush(ctx, true, task, files);
8486
8487 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8488 io_run_task_work();
8489 cond_resched();
8490 }
8491}
8492
8493/*
8494 * Note that this task has used io_uring. We use it for cancelation purposes.
8495 */
8496static int io_uring_add_task_file(struct file *file)
8497{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008498 struct io_uring_task *tctx = current->io_uring;
8499
8500 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008501 int ret;
8502
8503 ret = io_uring_alloc_task_context(current);
8504 if (unlikely(ret))
8505 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008506 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008507 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008508 if (tctx->last != file) {
8509 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008510
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008511 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008512 get_file(file);
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008513 xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008514 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008515 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008516 }
8517
8518 return 0;
8519}
8520
8521/*
8522 * Remove this io_uring_file -> task mapping.
8523 */
8524static void io_uring_del_task_file(struct file *file)
8525{
8526 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008527
8528 if (tctx->last == file)
8529 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008530 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008531 if (file)
8532 fput(file);
8533}
8534
8535static void __io_uring_attempt_task_drop(struct file *file)
8536{
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008537 struct file *old = xa_load(&current->io_uring->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008538
8539 if (old == file)
8540 io_uring_del_task_file(file);
8541}
8542
8543/*
8544 * Drop task note for this file if we're the only ones that hold it after
8545 * pending fput()
8546 */
8547static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8548{
8549 if (!current->io_uring)
8550 return;
8551 /*
8552 * fput() is pending, will be 2 if the only other ref is our potential
8553 * task file note. If the task is exiting, drop regardless of count.
8554 */
8555 if (!exiting && atomic_long_read(&file->f_count) != 2)
8556 return;
8557
8558 __io_uring_attempt_task_drop(file);
8559}
8560
8561void __io_uring_files_cancel(struct files_struct *files)
8562{
8563 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008564 struct file *file;
8565 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008566
8567 /* make sure overflow events are dropped */
8568 tctx->in_idle = true;
8569
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008570 xa_for_each(&tctx->xa, index, file) {
8571 struct io_ring_ctx *ctx = file->private_data;
Jens Axboe0f212202020-09-13 13:09:39 -06008572
8573 io_uring_cancel_task_requests(ctx, files);
8574 if (files)
8575 io_uring_del_task_file(file);
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008576 }
Jens Axboe0f212202020-09-13 13:09:39 -06008577}
8578
8579static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8580{
8581 return atomic_long_read(&tctx->req_issue) ==
8582 atomic_long_read(&tctx->req_complete);
8583}
8584
8585/*
8586 * Find any io_uring fd that this task has registered or done IO on, and cancel
8587 * requests.
8588 */
8589void __io_uring_task_cancel(void)
8590{
8591 struct io_uring_task *tctx = current->io_uring;
8592 DEFINE_WAIT(wait);
8593 long completions;
8594
8595 /* make sure overflow events are dropped */
8596 tctx->in_idle = true;
8597
8598 while (!io_uring_task_idle(tctx)) {
8599 /* read completions before cancelations */
8600 completions = atomic_long_read(&tctx->req_complete);
8601 __io_uring_files_cancel(NULL);
8602
8603 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8604
8605 /*
8606 * If we've seen completions, retry. This avoids a race where
8607 * a completion comes in before we did prepare_to_wait().
8608 */
8609 if (completions != atomic_long_read(&tctx->req_complete))
8610 continue;
8611 if (io_uring_task_idle(tctx))
8612 break;
8613 schedule();
8614 }
8615
8616 finish_wait(&tctx->wait, &wait);
8617 tctx->in_idle = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06008618}
8619
8620static int io_uring_flush(struct file *file, void *data)
8621{
8622 struct io_ring_ctx *ctx = file->private_data;
8623
Jens Axboe6ab23142020-02-08 20:23:59 -07008624 /*
8625 * If the task is going away, cancel work it may have pending
8626 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008627 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008628 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008629
Jens Axboe0f212202020-09-13 13:09:39 -06008630 io_uring_cancel_task_requests(ctx, data);
8631 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008632 return 0;
8633}
8634
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008635static void *io_uring_validate_mmap_request(struct file *file,
8636 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008637{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008638 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008639 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008640 struct page *page;
8641 void *ptr;
8642
8643 switch (offset) {
8644 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008645 case IORING_OFF_CQ_RING:
8646 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008647 break;
8648 case IORING_OFF_SQES:
8649 ptr = ctx->sq_sqes;
8650 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008651 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008652 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008653 }
8654
8655 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008656 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008657 return ERR_PTR(-EINVAL);
8658
8659 return ptr;
8660}
8661
8662#ifdef CONFIG_MMU
8663
8664static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8665{
8666 size_t sz = vma->vm_end - vma->vm_start;
8667 unsigned long pfn;
8668 void *ptr;
8669
8670 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8671 if (IS_ERR(ptr))
8672 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008673
8674 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8675 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8676}
8677
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008678#else /* !CONFIG_MMU */
8679
8680static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8681{
8682 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8683}
8684
8685static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8686{
8687 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8688}
8689
8690static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8691 unsigned long addr, unsigned long len,
8692 unsigned long pgoff, unsigned long flags)
8693{
8694 void *ptr;
8695
8696 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8697 if (IS_ERR(ptr))
8698 return PTR_ERR(ptr);
8699
8700 return (unsigned long) ptr;
8701}
8702
8703#endif /* !CONFIG_MMU */
8704
Jens Axboe90554202020-09-03 12:12:41 -06008705static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8706{
8707 DEFINE_WAIT(wait);
8708
8709 do {
8710 if (!io_sqring_full(ctx))
8711 break;
8712
8713 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8714
8715 if (!io_sqring_full(ctx))
8716 break;
8717
8718 schedule();
8719 } while (!signal_pending(current));
8720
8721 finish_wait(&ctx->sqo_sq_wait, &wait);
8722}
8723
Jens Axboe2b188cc2019-01-07 10:46:33 -07008724SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8725 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8726 size_t, sigsz)
8727{
8728 struct io_ring_ctx *ctx;
8729 long ret = -EBADF;
8730 int submitted = 0;
8731 struct fd f;
8732
Jens Axboe4c6e2772020-07-01 11:29:10 -06008733 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008734
Jens Axboe90554202020-09-03 12:12:41 -06008735 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8736 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008737 return -EINVAL;
8738
8739 f = fdget(fd);
8740 if (!f.file)
8741 return -EBADF;
8742
8743 ret = -EOPNOTSUPP;
8744 if (f.file->f_op != &io_uring_fops)
8745 goto out_fput;
8746
8747 ret = -ENXIO;
8748 ctx = f.file->private_data;
8749 if (!percpu_ref_tryget(&ctx->refs))
8750 goto out_fput;
8751
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008752 ret = -EBADFD;
8753 if (ctx->flags & IORING_SETUP_R_DISABLED)
8754 goto out;
8755
Jens Axboe6c271ce2019-01-10 11:22:30 -07008756 /*
8757 * For SQ polling, the thread will do all submissions and completions.
8758 * Just return the requested submit count, and wake the thread if
8759 * we were asked to.
8760 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008761 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008762 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008763 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008764 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008765 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008766 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008767 if (flags & IORING_ENTER_SQ_WAIT)
8768 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008769 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008770 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008771 ret = io_uring_add_task_file(f.file);
8772 if (unlikely(ret))
8773 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008774 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008775 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008776 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008777
8778 if (submitted != to_submit)
8779 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008780 }
8781 if (flags & IORING_ENTER_GETEVENTS) {
8782 min_complete = min(min_complete, ctx->cq_entries);
8783
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008784 /*
8785 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8786 * space applications don't need to do io completion events
8787 * polling again, they can rely on io_sq_thread to do polling
8788 * work, which can reduce cpu usage and uring_lock contention.
8789 */
8790 if (ctx->flags & IORING_SETUP_IOPOLL &&
8791 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008792 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008793 } else {
8794 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8795 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008796 }
8797
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008798out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008799 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008800out_fput:
8801 fdput(f);
8802 return submitted ? submitted : ret;
8803}
8804
Tobias Klauserbebdb652020-02-26 18:38:32 +01008805#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008806static int io_uring_show_cred(int id, void *p, void *data)
8807{
8808 const struct cred *cred = p;
8809 struct seq_file *m = data;
8810 struct user_namespace *uns = seq_user_ns(m);
8811 struct group_info *gi;
8812 kernel_cap_t cap;
8813 unsigned __capi;
8814 int g;
8815
8816 seq_printf(m, "%5d\n", id);
8817 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8818 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8819 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8820 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8821 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8822 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8823 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8824 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8825 seq_puts(m, "\n\tGroups:\t");
8826 gi = cred->group_info;
8827 for (g = 0; g < gi->ngroups; g++) {
8828 seq_put_decimal_ull(m, g ? " " : "",
8829 from_kgid_munged(uns, gi->gid[g]));
8830 }
8831 seq_puts(m, "\n\tCapEff:\t");
8832 cap = cred->cap_effective;
8833 CAP_FOR_EACH_U32(__capi)
8834 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8835 seq_putc(m, '\n');
8836 return 0;
8837}
8838
8839static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8840{
Joseph Qidbbe9c62020-09-29 09:01:22 -06008841 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06008842 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008843 int i;
8844
Jens Axboefad8e0d2020-09-28 08:57:48 -06008845 /*
8846 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8847 * since fdinfo case grabs it in the opposite direction of normal use
8848 * cases. If we fail to get the lock, we just don't iterate any
8849 * structures that could be going away outside the io_uring mutex.
8850 */
8851 has_lock = mutex_trylock(&ctx->uring_lock);
8852
Joseph Qidbbe9c62020-09-29 09:01:22 -06008853 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
8854 sq = ctx->sq_data;
8855
8856 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
8857 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07008858 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008859 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008860 struct fixed_file_table *table;
8861 struct file *f;
8862
8863 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8864 f = table->files[i & IORING_FILE_TABLE_MASK];
8865 if (f)
8866 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8867 else
8868 seq_printf(m, "%5u: <none>\n", i);
8869 }
8870 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008871 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008872 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8873
8874 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8875 (unsigned int) buf->len);
8876 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008877 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008878 seq_printf(m, "Personalities:\n");
8879 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8880 }
Jens Axboed7718a92020-02-14 22:23:12 -07008881 seq_printf(m, "PollList:\n");
8882 spin_lock_irq(&ctx->completion_lock);
8883 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8884 struct hlist_head *list = &ctx->cancel_hash[i];
8885 struct io_kiocb *req;
8886
8887 hlist_for_each_entry(req, list, hash_node)
8888 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8889 req->task->task_works != NULL);
8890 }
8891 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008892 if (has_lock)
8893 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008894}
8895
8896static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8897{
8898 struct io_ring_ctx *ctx = f->private_data;
8899
8900 if (percpu_ref_tryget(&ctx->refs)) {
8901 __io_uring_show_fdinfo(ctx, m);
8902 percpu_ref_put(&ctx->refs);
8903 }
8904}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008905#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008906
Jens Axboe2b188cc2019-01-07 10:46:33 -07008907static const struct file_operations io_uring_fops = {
8908 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008909 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008910 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008911#ifndef CONFIG_MMU
8912 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8913 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8914#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008915 .poll = io_uring_poll,
8916 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008917#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008918 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008919#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008920};
8921
8922static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8923 struct io_uring_params *p)
8924{
Hristo Venev75b28af2019-08-26 17:23:46 +00008925 struct io_rings *rings;
8926 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008927
Jens Axboebd740482020-08-05 12:58:23 -06008928 /* make sure these are sane, as we already accounted them */
8929 ctx->sq_entries = p->sq_entries;
8930 ctx->cq_entries = p->cq_entries;
8931
Hristo Venev75b28af2019-08-26 17:23:46 +00008932 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8933 if (size == SIZE_MAX)
8934 return -EOVERFLOW;
8935
8936 rings = io_mem_alloc(size);
8937 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008938 return -ENOMEM;
8939
Hristo Venev75b28af2019-08-26 17:23:46 +00008940 ctx->rings = rings;
8941 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8942 rings->sq_ring_mask = p->sq_entries - 1;
8943 rings->cq_ring_mask = p->cq_entries - 1;
8944 rings->sq_ring_entries = p->sq_entries;
8945 rings->cq_ring_entries = p->cq_entries;
8946 ctx->sq_mask = rings->sq_ring_mask;
8947 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008948
8949 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008950 if (size == SIZE_MAX) {
8951 io_mem_free(ctx->rings);
8952 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008953 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008954 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008955
8956 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008957 if (!ctx->sq_sqes) {
8958 io_mem_free(ctx->rings);
8959 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008960 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008961 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008962
Jens Axboe2b188cc2019-01-07 10:46:33 -07008963 return 0;
8964}
8965
8966/*
8967 * Allocate an anonymous fd, this is what constitutes the application
8968 * visible backing of an io_uring instance. The application mmaps this
8969 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8970 * we have to tie this fd to a socket for file garbage collection purposes.
8971 */
8972static int io_uring_get_fd(struct io_ring_ctx *ctx)
8973{
8974 struct file *file;
8975 int ret;
8976
8977#if defined(CONFIG_UNIX)
8978 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8979 &ctx->ring_sock);
8980 if (ret)
8981 return ret;
8982#endif
8983
8984 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8985 if (ret < 0)
8986 goto err;
8987
8988 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8989 O_RDWR | O_CLOEXEC);
8990 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008991err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07008992 put_unused_fd(ret);
8993 ret = PTR_ERR(file);
8994 goto err;
8995 }
8996
8997#if defined(CONFIG_UNIX)
8998 ctx->ring_sock->file = file;
8999#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009000 if (unlikely(io_uring_add_task_file(file))) {
9001 file = ERR_PTR(-ENOMEM);
9002 goto err_fd;
9003 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009004 fd_install(ret, file);
9005 return ret;
9006err:
9007#if defined(CONFIG_UNIX)
9008 sock_release(ctx->ring_sock);
9009 ctx->ring_sock = NULL;
9010#endif
9011 return ret;
9012}
9013
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009014static int io_uring_create(unsigned entries, struct io_uring_params *p,
9015 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009016{
9017 struct user_struct *user = NULL;
9018 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009019 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009020 int ret;
9021
Jens Axboe8110c1a2019-12-28 15:39:54 -07009022 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009023 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009024 if (entries > IORING_MAX_ENTRIES) {
9025 if (!(p->flags & IORING_SETUP_CLAMP))
9026 return -EINVAL;
9027 entries = IORING_MAX_ENTRIES;
9028 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009029
9030 /*
9031 * Use twice as many entries for the CQ ring. It's possible for the
9032 * application to drive a higher depth than the size of the SQ ring,
9033 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009034 * some flexibility in overcommitting a bit. If the application has
9035 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9036 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009037 */
9038 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009039 if (p->flags & IORING_SETUP_CQSIZE) {
9040 /*
9041 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9042 * to a power-of-two, if it isn't already. We do NOT impose
9043 * any cq vs sq ring sizing.
9044 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009045 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009046 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009047 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9048 if (!(p->flags & IORING_SETUP_CLAMP))
9049 return -EINVAL;
9050 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9051 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009052 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9053 } else {
9054 p->cq_entries = 2 * p->sq_entries;
9055 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009056
9057 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009058 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009059
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009060 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009061 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009062 ring_pages(p->sq_entries, p->cq_entries));
9063 if (ret) {
9064 free_uid(user);
9065 return ret;
9066 }
9067 }
9068
9069 ctx = io_ring_ctx_alloc(p);
9070 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009071 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009072 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009073 p->cq_entries));
9074 free_uid(user);
9075 return -ENOMEM;
9076 }
9077 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009078 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009079 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009080
Jens Axboe2aede0e2020-09-14 10:45:53 -06009081 ctx->sqo_task = get_task_struct(current);
9082
9083 /*
9084 * This is just grabbed for accounting purposes. When a process exits,
9085 * the mm is exited and dropped before the files, hence we need to hang
9086 * on to this mm purely for the purposes of being able to unaccount
9087 * memory (locked/pinned vm). It's not used for anything else.
9088 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009089 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009090 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009091
Dennis Zhou91d8f512020-09-16 13:41:05 -07009092#ifdef CONFIG_BLK_CGROUP
9093 /*
9094 * The sq thread will belong to the original cgroup it was inited in.
9095 * If the cgroup goes offline (e.g. disabling the io controller), then
9096 * issued bios will be associated with the closest cgroup later in the
9097 * block layer.
9098 */
9099 rcu_read_lock();
9100 ctx->sqo_blkcg_css = blkcg_css();
9101 ret = css_tryget_online(ctx->sqo_blkcg_css);
9102 rcu_read_unlock();
9103 if (!ret) {
9104 /* don't init against a dying cgroup, have the user try again */
9105 ctx->sqo_blkcg_css = NULL;
9106 ret = -ENODEV;
9107 goto err;
9108 }
9109#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009110
Jens Axboe2b188cc2019-01-07 10:46:33 -07009111 /*
9112 * Account memory _before_ installing the file descriptor. Once
9113 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009114 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009115 * will un-account as well.
9116 */
9117 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9118 ACCT_LOCKED);
9119 ctx->limit_mem = limit_mem;
9120
9121 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009122 if (ret)
9123 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009124
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009125 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009126 if (ret)
9127 goto err;
9128
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009129 if (!(p->flags & IORING_SETUP_R_DISABLED))
9130 io_sq_offload_start(ctx);
9131
Jens Axboe2b188cc2019-01-07 10:46:33 -07009132 memset(&p->sq_off, 0, sizeof(p->sq_off));
9133 p->sq_off.head = offsetof(struct io_rings, sq.head);
9134 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9135 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9136 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9137 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9138 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9139 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9140
9141 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009142 p->cq_off.head = offsetof(struct io_rings, cq.head);
9143 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9144 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9145 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9146 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9147 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009148 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009149
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009150 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9151 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009152 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9153 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009154
9155 if (copy_to_user(params, p, sizeof(*p))) {
9156 ret = -EFAULT;
9157 goto err;
9158 }
Jens Axboed1719f72020-07-30 13:43:53 -06009159
9160 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009161 * Install ring fd as the very last thing, so we don't risk someone
9162 * having closed it before we finish setup
9163 */
9164 ret = io_uring_get_fd(ctx);
9165 if (ret < 0)
9166 goto err;
9167
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009168 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009169 return ret;
9170err:
9171 io_ring_ctx_wait_and_kill(ctx);
9172 return ret;
9173}
9174
9175/*
9176 * Sets up an aio uring context, and returns the fd. Applications asks for a
9177 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9178 * params structure passed in.
9179 */
9180static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9181{
9182 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009183 int i;
9184
9185 if (copy_from_user(&p, params, sizeof(p)))
9186 return -EFAULT;
9187 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9188 if (p.resv[i])
9189 return -EINVAL;
9190 }
9191
Jens Axboe6c271ce2019-01-10 11:22:30 -07009192 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009193 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009194 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9195 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009196 return -EINVAL;
9197
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009198 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009199}
9200
9201SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9202 struct io_uring_params __user *, params)
9203{
9204 return io_uring_setup(entries, params);
9205}
9206
Jens Axboe66f4af92020-01-16 15:36:52 -07009207static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9208{
9209 struct io_uring_probe *p;
9210 size_t size;
9211 int i, ret;
9212
9213 size = struct_size(p, ops, nr_args);
9214 if (size == SIZE_MAX)
9215 return -EOVERFLOW;
9216 p = kzalloc(size, GFP_KERNEL);
9217 if (!p)
9218 return -ENOMEM;
9219
9220 ret = -EFAULT;
9221 if (copy_from_user(p, arg, size))
9222 goto out;
9223 ret = -EINVAL;
9224 if (memchr_inv(p, 0, size))
9225 goto out;
9226
9227 p->last_op = IORING_OP_LAST - 1;
9228 if (nr_args > IORING_OP_LAST)
9229 nr_args = IORING_OP_LAST;
9230
9231 for (i = 0; i < nr_args; i++) {
9232 p->ops[i].op = i;
9233 if (!io_op_defs[i].not_supported)
9234 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9235 }
9236 p->ops_len = i;
9237
9238 ret = 0;
9239 if (copy_to_user(arg, p, size))
9240 ret = -EFAULT;
9241out:
9242 kfree(p);
9243 return ret;
9244}
9245
Jens Axboe071698e2020-01-28 10:04:42 -07009246static int io_register_personality(struct io_ring_ctx *ctx)
9247{
9248 const struct cred *creds = get_current_cred();
9249 int id;
9250
9251 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9252 USHRT_MAX, GFP_KERNEL);
9253 if (id < 0)
9254 put_cred(creds);
9255 return id;
9256}
9257
9258static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9259{
9260 const struct cred *old_creds;
9261
9262 old_creds = idr_remove(&ctx->personality_idr, id);
9263 if (old_creds) {
9264 put_cred(old_creds);
9265 return 0;
9266 }
9267
9268 return -EINVAL;
9269}
9270
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009271static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9272 unsigned int nr_args)
9273{
9274 struct io_uring_restriction *res;
9275 size_t size;
9276 int i, ret;
9277
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009278 /* Restrictions allowed only if rings started disabled */
9279 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9280 return -EBADFD;
9281
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009282 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009283 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009284 return -EBUSY;
9285
9286 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9287 return -EINVAL;
9288
9289 size = array_size(nr_args, sizeof(*res));
9290 if (size == SIZE_MAX)
9291 return -EOVERFLOW;
9292
9293 res = memdup_user(arg, size);
9294 if (IS_ERR(res))
9295 return PTR_ERR(res);
9296
9297 ret = 0;
9298
9299 for (i = 0; i < nr_args; i++) {
9300 switch (res[i].opcode) {
9301 case IORING_RESTRICTION_REGISTER_OP:
9302 if (res[i].register_op >= IORING_REGISTER_LAST) {
9303 ret = -EINVAL;
9304 goto out;
9305 }
9306
9307 __set_bit(res[i].register_op,
9308 ctx->restrictions.register_op);
9309 break;
9310 case IORING_RESTRICTION_SQE_OP:
9311 if (res[i].sqe_op >= IORING_OP_LAST) {
9312 ret = -EINVAL;
9313 goto out;
9314 }
9315
9316 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9317 break;
9318 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9319 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9320 break;
9321 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9322 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9323 break;
9324 default:
9325 ret = -EINVAL;
9326 goto out;
9327 }
9328 }
9329
9330out:
9331 /* Reset all restrictions if an error happened */
9332 if (ret != 0)
9333 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9334 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009335 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009336
9337 kfree(res);
9338 return ret;
9339}
9340
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009341static int io_register_enable_rings(struct io_ring_ctx *ctx)
9342{
9343 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9344 return -EBADFD;
9345
9346 if (ctx->restrictions.registered)
9347 ctx->restricted = 1;
9348
9349 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9350
9351 io_sq_offload_start(ctx);
9352
9353 return 0;
9354}
9355
Jens Axboe071698e2020-01-28 10:04:42 -07009356static bool io_register_op_must_quiesce(int op)
9357{
9358 switch (op) {
9359 case IORING_UNREGISTER_FILES:
9360 case IORING_REGISTER_FILES_UPDATE:
9361 case IORING_REGISTER_PROBE:
9362 case IORING_REGISTER_PERSONALITY:
9363 case IORING_UNREGISTER_PERSONALITY:
9364 return false;
9365 default:
9366 return true;
9367 }
9368}
9369
Jens Axboeedafcce2019-01-09 09:16:05 -07009370static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9371 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009372 __releases(ctx->uring_lock)
9373 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009374{
9375 int ret;
9376
Jens Axboe35fa71a2019-04-22 10:23:23 -06009377 /*
9378 * We're inside the ring mutex, if the ref is already dying, then
9379 * someone else killed the ctx or is already going through
9380 * io_uring_register().
9381 */
9382 if (percpu_ref_is_dying(&ctx->refs))
9383 return -ENXIO;
9384
Jens Axboe071698e2020-01-28 10:04:42 -07009385 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009386 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009387
Jens Axboe05f3fb32019-12-09 11:22:50 -07009388 /*
9389 * Drop uring mutex before waiting for references to exit. If
9390 * another thread is currently inside io_uring_enter() it might
9391 * need to grab the uring_lock to make progress. If we hold it
9392 * here across the drain wait, then we can deadlock. It's safe
9393 * to drop the mutex here, since no new references will come in
9394 * after we've killed the percpu ref.
9395 */
9396 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009397 do {
9398 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9399 if (!ret)
9400 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009401 ret = io_run_task_work_sig();
9402 if (ret < 0)
9403 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009404 } while (1);
9405
Jens Axboe05f3fb32019-12-09 11:22:50 -07009406 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009407
Jens Axboec1503682020-01-08 08:26:07 -07009408 if (ret) {
9409 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009410 goto out_quiesce;
9411 }
9412 }
9413
9414 if (ctx->restricted) {
9415 if (opcode >= IORING_REGISTER_LAST) {
9416 ret = -EINVAL;
9417 goto out;
9418 }
9419
9420 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9421 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009422 goto out;
9423 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009424 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009425
9426 switch (opcode) {
9427 case IORING_REGISTER_BUFFERS:
9428 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9429 break;
9430 case IORING_UNREGISTER_BUFFERS:
9431 ret = -EINVAL;
9432 if (arg || nr_args)
9433 break;
9434 ret = io_sqe_buffer_unregister(ctx);
9435 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009436 case IORING_REGISTER_FILES:
9437 ret = io_sqe_files_register(ctx, arg, nr_args);
9438 break;
9439 case IORING_UNREGISTER_FILES:
9440 ret = -EINVAL;
9441 if (arg || nr_args)
9442 break;
9443 ret = io_sqe_files_unregister(ctx);
9444 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009445 case IORING_REGISTER_FILES_UPDATE:
9446 ret = io_sqe_files_update(ctx, arg, nr_args);
9447 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009448 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009449 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009450 ret = -EINVAL;
9451 if (nr_args != 1)
9452 break;
9453 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009454 if (ret)
9455 break;
9456 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9457 ctx->eventfd_async = 1;
9458 else
9459 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009460 break;
9461 case IORING_UNREGISTER_EVENTFD:
9462 ret = -EINVAL;
9463 if (arg || nr_args)
9464 break;
9465 ret = io_eventfd_unregister(ctx);
9466 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009467 case IORING_REGISTER_PROBE:
9468 ret = -EINVAL;
9469 if (!arg || nr_args > 256)
9470 break;
9471 ret = io_probe(ctx, arg, nr_args);
9472 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009473 case IORING_REGISTER_PERSONALITY:
9474 ret = -EINVAL;
9475 if (arg || nr_args)
9476 break;
9477 ret = io_register_personality(ctx);
9478 break;
9479 case IORING_UNREGISTER_PERSONALITY:
9480 ret = -EINVAL;
9481 if (arg)
9482 break;
9483 ret = io_unregister_personality(ctx, nr_args);
9484 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009485 case IORING_REGISTER_ENABLE_RINGS:
9486 ret = -EINVAL;
9487 if (arg || nr_args)
9488 break;
9489 ret = io_register_enable_rings(ctx);
9490 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009491 case IORING_REGISTER_RESTRICTIONS:
9492 ret = io_register_restrictions(ctx, arg, nr_args);
9493 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009494 default:
9495 ret = -EINVAL;
9496 break;
9497 }
9498
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009499out:
Jens Axboe071698e2020-01-28 10:04:42 -07009500 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009501 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009502 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009503out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009504 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009505 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009506 return ret;
9507}
9508
9509SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9510 void __user *, arg, unsigned int, nr_args)
9511{
9512 struct io_ring_ctx *ctx;
9513 long ret = -EBADF;
9514 struct fd f;
9515
9516 f = fdget(fd);
9517 if (!f.file)
9518 return -EBADF;
9519
9520 ret = -EOPNOTSUPP;
9521 if (f.file->f_op != &io_uring_fops)
9522 goto out_fput;
9523
9524 ctx = f.file->private_data;
9525
9526 mutex_lock(&ctx->uring_lock);
9527 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9528 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009529 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9530 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009531out_fput:
9532 fdput(f);
9533 return ret;
9534}
9535
Jens Axboe2b188cc2019-01-07 10:46:33 -07009536static int __init io_uring_init(void)
9537{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009538#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9539 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9540 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9541} while (0)
9542
9543#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9544 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9545 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9546 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9547 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9548 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9549 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9550 BUILD_BUG_SQE_ELEM(8, __u64, off);
9551 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9552 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009553 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009554 BUILD_BUG_SQE_ELEM(24, __u32, len);
9555 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9556 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9557 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9558 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009559 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9560 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009561 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9562 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9563 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9564 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9565 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9566 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9567 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9568 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009569 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009570 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9571 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9572 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009573 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009574
Jens Axboed3656342019-12-18 09:50:26 -07009575 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009576 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009577 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9578 return 0;
9579};
9580__initcall(io_uring_init);