blob: fcb4e95cb88d28e6422bd4413afb3aa1fb16fdde [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020085#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
Jens Axboe2b188cc2019-01-07 10:46:33 -070088#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060091#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
Daniel Xu5277dea2019-09-14 14:23:45 -070093#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060094#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060095
96/*
97 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
98 */
99#define IORING_FILE_TABLE_SHIFT 9
100#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
101#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
102#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200103#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
104 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700105
106struct io_uring {
107 u32 head ____cacheline_aligned_in_smp;
108 u32 tail ____cacheline_aligned_in_smp;
109};
110
Stefan Bühler1e84b972019-04-24 23:54:16 +0200111/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000112 * This data is shared with the application through the mmap at offsets
113 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 *
115 * The offsets to the member fields are published through struct
116 * io_sqring_offsets when calling io_uring_setup.
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 /*
120 * Head and tail offsets into the ring; the offsets need to be
121 * masked to get valid indices.
122 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 * The kernel controls head of the sq ring and the tail of the cq ring,
124 * and the application controls tail of the sq ring and the head of the
125 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000129 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 * ring_entries - 1)
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 u32 sq_ring_mask, cq_ring_mask;
133 /* Ring sizes (constant, power of 2) */
134 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Number of invalid entries dropped by the kernel due to
137 * invalid index stored in array
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * After a new SQ head value was read by the application this
144 * counter includes all submissions that were dropped reaching
145 * the new SQ head (and possibly more).
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200149 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application.
153 *
154 * The application needs a full memory barrier before checking
155 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200159 * Runtime CQ flags
160 *
161 * Written by the application, shouldn't be modified by the
162 * kernel.
163 */
164 u32 cq_flags;
165 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200166 * Number of completion events lost because the queue was full;
167 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800168 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200169 * the completion queue.
170 *
171 * Written by the kernel, shouldn't be modified by the
172 * application (i.e. get number of "new events" by comparing to
173 * cached value).
174 *
175 * As completion events come in out of order this counter is not
176 * ordered with any other data.
177 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000178 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200179 /*
180 * Ring buffer of completion events.
181 *
182 * The kernel writes completion events fresh every time they are
183 * produced, so the application is allowed to modify pending
184 * entries.
185 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000186 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700187};
188
Jens Axboeedafcce2019-01-09 09:16:05 -0700189struct io_mapped_ubuf {
190 u64 ubuf;
191 size_t len;
192 struct bio_vec *bvec;
193 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600194 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700195};
196
Jens Axboe65e19f52019-10-26 07:20:21 -0600197struct fixed_file_table {
198 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700199};
200
Xiaoguang Wang05589552020-03-31 14:05:18 +0800201struct fixed_file_ref_node {
202 struct percpu_ref refs;
203 struct list_head node;
204 struct list_head file_list;
205 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600206 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800207};
208
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209struct fixed_file_data {
210 struct fixed_file_table *table;
211 struct io_ring_ctx *ctx;
212
Xiaoguang Wang05589552020-03-31 14:05:18 +0800213 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700214 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700215 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800216 struct list_head ref_list;
217 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700218};
219
Jens Axboe5a2e7452020-02-23 16:23:11 -0700220struct io_buffer {
221 struct list_head list;
222 __u64 addr;
223 __s32 len;
224 __u16 bid;
225};
226
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200227struct io_restriction {
228 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
229 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
230 u8 sqe_flags_allowed;
231 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200232 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200233};
234
Jens Axboe534ca6d2020-09-02 13:52:19 -0600235struct io_sq_data {
236 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600237 struct mutex lock;
238
239 /* ctx's that are using this sqd */
240 struct list_head ctx_list;
241 struct list_head ctx_new_list;
242 struct mutex ctx_lock;
243
Jens Axboe534ca6d2020-09-02 13:52:19 -0600244 struct task_struct *thread;
245 struct wait_queue_head wait;
246};
247
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248struct io_ring_ctx {
249 struct {
250 struct percpu_ref refs;
251 } ____cacheline_aligned_in_smp;
252
253 struct {
254 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800255 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700256 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800257 unsigned int cq_overflow_flushed: 1;
258 unsigned int drain_next: 1;
259 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200260 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261
Hristo Venev75b28af2019-08-26 17:23:46 +0000262 /*
263 * Ring buffer of indices into array of io_uring_sqe, which is
264 * mmapped by the application using the IORING_OFF_SQES offset.
265 *
266 * This indirection could e.g. be used to assign fixed
267 * io_uring_sqe entries to operations and only submit them to
268 * the queue when needed.
269 *
270 * The kernel modifies neither the indices array nor the entries
271 * array.
272 */
273 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700274 unsigned cached_sq_head;
275 unsigned sq_entries;
276 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700277 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600278 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700280 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600281
282 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600283 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700284 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285
Jens Axboefcb323c2019-10-24 12:39:47 -0600286 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700287 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700288 } ____cacheline_aligned_in_smp;
289
Hristo Venev75b28af2019-08-26 17:23:46 +0000290 struct io_rings *rings;
291
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600293 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600294
295 /*
296 * For SQPOLL usage - we hold a reference to the parent task, so we
297 * have access to the ->files
298 */
299 struct task_struct *sqo_task;
300
301 /* Only used for accounting purposes */
302 struct mm_struct *mm_account;
303
Dennis Zhou91d8f512020-09-16 13:41:05 -0700304#ifdef CONFIG_BLK_CGROUP
305 struct cgroup_subsys_state *sqo_blkcg_css;
306#endif
307
Jens Axboe534ca6d2020-09-02 13:52:19 -0600308 struct io_sq_data *sq_data; /* if using sq thread polling */
309
Jens Axboe90554202020-09-03 12:12:41 -0600310 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600311 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600312 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
Jens Axboe6b063142019-01-10 22:13:58 -0700314 /*
315 * If used, fixed file set. Writers must ensure that ->refs is dead,
316 * readers must ensure that ->refs is alive as long as the file* is
317 * used. Only updated through io_uring_register(2).
318 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700319 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700320 unsigned nr_user_files;
321
Jens Axboeedafcce2019-01-09 09:16:05 -0700322 /* if used, fixed mapped user buffers */
323 unsigned nr_user_bufs;
324 struct io_mapped_ubuf *user_bufs;
325
Jens Axboe2b188cc2019-01-07 10:46:33 -0700326 struct user_struct *user;
327
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700328 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700329
Jens Axboe0f158b42020-05-14 17:18:39 -0600330 struct completion ref_comp;
331 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700332
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700333 /* if all else fails... */
334 struct io_kiocb *fallback_req;
335
Jens Axboe206aefd2019-11-07 18:27:42 -0700336#if defined(CONFIG_UNIX)
337 struct socket *ring_sock;
338#endif
339
Jens Axboe5a2e7452020-02-23 16:23:11 -0700340 struct idr io_buffer_idr;
341
Jens Axboe071698e2020-01-28 10:04:42 -0700342 struct idr personality_idr;
343
Jens Axboe206aefd2019-11-07 18:27:42 -0700344 struct {
345 unsigned cached_cq_tail;
346 unsigned cq_entries;
347 unsigned cq_mask;
348 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700349 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700350 struct wait_queue_head cq_wait;
351 struct fasync_struct *cq_fasync;
352 struct eventfd_ctx *cq_ev_fd;
353 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700354
355 struct {
356 struct mutex uring_lock;
357 wait_queue_head_t wait;
358 } ____cacheline_aligned_in_smp;
359
360 struct {
361 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700362
Jens Axboedef596e2019-01-09 08:59:42 -0700363 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300364 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700365 * io_uring instances that don't use IORING_SETUP_SQPOLL.
366 * For SQPOLL, only the single threaded io_sq_thread() will
367 * manipulate the list, hence no extra locking is needed there.
368 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300369 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700370 struct hlist_head *cancel_hash;
371 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700372 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600373
374 spinlock_t inflight_lock;
375 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600377
Jens Axboe4a38aed22020-05-14 17:21:15 -0600378 struct delayed_work file_put_work;
379 struct llist_head file_put_llist;
380
Jens Axboe85faa7b2020-04-09 18:14:00 -0600381 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200382 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383};
384
Jens Axboe09bb8392019-03-13 12:39:28 -0600385/*
386 * First field must be the file pointer in all the
387 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
388 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700389struct io_poll_iocb {
390 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700391 union {
392 struct wait_queue_head *head;
393 u64 addr;
394 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700395 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600396 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700397 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700398 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700399};
400
Jens Axboeb5dba592019-12-11 14:02:38 -0700401struct io_close {
402 struct file *file;
403 struct file *put_file;
404 int fd;
405};
406
Jens Axboead8a48a2019-11-15 08:49:11 -0700407struct io_timeout_data {
408 struct io_kiocb *req;
409 struct hrtimer timer;
410 struct timespec64 ts;
411 enum hrtimer_mode mode;
412};
413
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700414struct io_accept {
415 struct file *file;
416 struct sockaddr __user *addr;
417 int __user *addr_len;
418 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600419 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700420};
421
422struct io_sync {
423 struct file *file;
424 loff_t len;
425 loff_t off;
426 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700427 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700428};
429
Jens Axboefbf23842019-12-17 18:45:56 -0700430struct io_cancel {
431 struct file *file;
432 u64 addr;
433};
434
Jens Axboeb29472e2019-12-17 18:50:29 -0700435struct io_timeout {
436 struct file *file;
437 u64 addr;
438 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300439 u32 off;
440 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300441 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700442};
443
Jens Axboe9adbd452019-12-20 08:45:55 -0700444struct io_rw {
445 /* NOTE: kiocb has the file as the first member, so don't do it here */
446 struct kiocb kiocb;
447 u64 addr;
448 u64 len;
449};
450
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700451struct io_connect {
452 struct file *file;
453 struct sockaddr __user *addr;
454 int addr_len;
455};
456
Jens Axboee47293f2019-12-20 08:58:21 -0700457struct io_sr_msg {
458 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700459 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300460 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700461 void __user *buf;
462 };
Jens Axboee47293f2019-12-20 08:58:21 -0700463 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700464 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700465 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700466 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700467};
468
Jens Axboe15b71ab2019-12-11 11:20:36 -0700469struct io_open {
470 struct file *file;
471 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700472 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700473 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600474 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700475};
476
Jens Axboe05f3fb32019-12-09 11:22:50 -0700477struct io_files_update {
478 struct file *file;
479 u64 arg;
480 u32 nr_args;
481 u32 offset;
482};
483
Jens Axboe4840e412019-12-25 22:03:45 -0700484struct io_fadvise {
485 struct file *file;
486 u64 offset;
487 u32 len;
488 u32 advice;
489};
490
Jens Axboec1ca7572019-12-25 22:18:28 -0700491struct io_madvise {
492 struct file *file;
493 u64 addr;
494 u32 len;
495 u32 advice;
496};
497
Jens Axboe3e4827b2020-01-08 15:18:09 -0700498struct io_epoll {
499 struct file *file;
500 int epfd;
501 int op;
502 int fd;
503 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700504};
505
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300506struct io_splice {
507 struct file *file_out;
508 struct file *file_in;
509 loff_t off_out;
510 loff_t off_in;
511 u64 len;
512 unsigned int flags;
513};
514
Jens Axboeddf0322d2020-02-23 16:41:33 -0700515struct io_provide_buf {
516 struct file *file;
517 __u64 addr;
518 __s32 len;
519 __u32 bgid;
520 __u16 nbufs;
521 __u16 bid;
522};
523
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700524struct io_statx {
525 struct file *file;
526 int dfd;
527 unsigned int mask;
528 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700529 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700530 struct statx __user *buffer;
531};
532
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300533struct io_completion {
534 struct file *file;
535 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300536 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300537};
538
Jens Axboef499a022019-12-02 16:28:46 -0700539struct io_async_connect {
540 struct sockaddr_storage address;
541};
542
Jens Axboe03b12302019-12-02 18:50:25 -0700543struct io_async_msghdr {
544 struct iovec fast_iov[UIO_FASTIOV];
545 struct iovec *iov;
546 struct sockaddr __user *uaddr;
547 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700548 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700549};
550
Jens Axboef67676d2019-12-02 11:03:47 -0700551struct io_async_rw {
552 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600553 const struct iovec *free_iovec;
554 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600555 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600556 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700557};
558
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300559enum {
560 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
561 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
562 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
563 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
564 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700565 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300566
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300567 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568 REQ_F_FAIL_LINK_BIT,
569 REQ_F_INFLIGHT_BIT,
570 REQ_F_CUR_POS_BIT,
571 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300572 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300573 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300574 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300575 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700576 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700577 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600578 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800579 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700580
581 /* not a real bit, just to check we're not overflowing the space */
582 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300583};
584
585enum {
586 /* ctx owns file */
587 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
588 /* drain existing IO first */
589 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
590 /* linked sqes */
591 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
592 /* doesn't sever on completion < 0 */
593 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
594 /* IOSQE_ASYNC */
595 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700596 /* IOSQE_BUFFER_SELECT */
597 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300598
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300599 /* head of a link */
600 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300601 /* fail rest of links */
602 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
603 /* on inflight list */
604 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
605 /* read/write uses file position */
606 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
607 /* must not punt to workers */
608 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609 /* has linked timeout */
610 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300611 /* regular file */
612 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300613 /* completion under lock */
614 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300615 /* needs cleanup */
616 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700617 /* already went through poll handler */
618 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700619 /* buffer already selected */
620 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600621 /* doesn't need file table for this request */
622 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800623 /* io_wq_work is initialized */
624 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700625};
626
627struct async_poll {
628 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600629 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300630};
631
Jens Axboe09bb8392019-03-13 12:39:28 -0600632/*
633 * NOTE! Each of the iocb union members has the file pointer
634 * as the first entry in their struct definition. So you can
635 * access the file pointer through any of the sub-structs,
636 * or directly as just 'ki_filp' in this struct.
637 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700639 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600640 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700641 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700642 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700643 struct io_accept accept;
644 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700645 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700646 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700647 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700648 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700649 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700650 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700651 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700652 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700653 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700654 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300655 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700656 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700657 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300658 /* use only after cleaning per-op data, see io_clean_op() */
659 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700660 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700661
Jens Axboee8c2bc12020-08-15 18:44:09 -0700662 /* opcode allocated if it needs to store data for async defer */
663 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700664 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800665 /* polled IO has completed */
666 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700667
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700668 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300669 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700670
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300671 struct io_ring_ctx *ctx;
672 unsigned int flags;
673 refcount_t refs;
674 struct task_struct *task;
675 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700676
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300677 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700678
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300679 /*
680 * 1. used with ctx->iopoll_list with reads/writes
681 * 2. to track reqs with ->files (see io_op_def::file_table)
682 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300683 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600684
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300685 struct percpu_ref *fixed_file_refs;
686 struct callback_head task_work;
687 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
688 struct hlist_node hash_node;
689 struct async_poll *apoll;
690 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700691};
692
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300693struct io_defer_entry {
694 struct list_head list;
695 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300696 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300697};
698
Jens Axboedef596e2019-01-09 08:59:42 -0700699#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700700
Jens Axboe013538b2020-06-22 09:29:15 -0600701struct io_comp_state {
702 unsigned int nr;
703 struct list_head list;
704 struct io_ring_ctx *ctx;
705};
706
Jens Axboe9a56a232019-01-09 09:06:50 -0700707struct io_submit_state {
708 struct blk_plug plug;
709
710 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700711 * io_kiocb alloc cache
712 */
713 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300714 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700715
716 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600717 * Batch completion logic
718 */
719 struct io_comp_state comp;
720
721 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700722 * File reference cache
723 */
724 struct file *file;
725 unsigned int fd;
726 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700727 unsigned int ios_left;
728};
729
Jens Axboed3656342019-12-18 09:50:26 -0700730struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700731 /* needs current->mm setup, does mm access */
732 unsigned needs_mm : 1;
733 /* needs req->file assigned */
734 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600735 /* don't fail if file grab fails */
736 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700737 /* hash wq insertion if file is a regular file */
738 unsigned hash_reg_file : 1;
739 /* unbound wq insertion if file is a non-regular file */
740 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700741 /* opcode is not supported by this kernel */
742 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700743 /* needs file table */
744 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700745 /* needs ->fs */
746 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700747 /* set if opcode supports polled "wait" */
748 unsigned pollin : 1;
749 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700750 /* op supports buffer selection */
751 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700752 /* needs rlimit(RLIMIT_FSIZE) assigned */
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300753 unsigned needs_fsize : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700754 /* must always have async data allocated */
755 unsigned needs_async_data : 1;
Dennis Zhou91d8f512020-09-16 13:41:05 -0700756 /* needs blkcg context, issues async io potentially */
757 unsigned needs_blkcg : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700758 /* size of async data needed, if any */
759 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700760};
761
Jens Axboe738277a2020-09-03 05:54:56 -0600762static const struct io_op_def io_op_defs[] __read_mostly = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_NOP] = {},
764 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700765 .needs_mm = 1,
766 .needs_file = 1,
767 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700768 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700769 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700770 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700771 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700772 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_mm = 1,
776 .needs_file = 1,
777 .hash_reg_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700779 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300780 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700781 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700782 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700783 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700787 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700790 .needs_file = 1,
791 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700792 .pollin = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700793 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700794 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_file = 1,
798 .hash_reg_file = 1,
799 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700800 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300801 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700802 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700803 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700804 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300805 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700806 .needs_file = 1,
807 .unbound_nonreg_file = 1,
808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_POLL_REMOVE] = {},
810 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700811 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700812 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700815 .needs_mm = 1,
816 .needs_file = 1,
817 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700818 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700819 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700820 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700821 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700822 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700823 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300824 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700825 .needs_mm = 1,
826 .needs_file = 1,
827 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700828 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700829 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700830 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700831 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700832 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700833 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700837 .needs_async_data = 1,
838 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_TIMEOUT_REMOVE] = {},
841 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700842 .needs_mm = 1,
843 .needs_file = 1,
844 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700845 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700846 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700847 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_ASYNC_CANCEL] = {},
849 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700850 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700851 .needs_async_data = 1,
852 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700855 .needs_mm = 1,
856 .needs_file = 1,
857 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700858 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700859 .needs_async_data = 1,
860 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700863 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300864 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700865 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700868 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700869 .needs_fs = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700870 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700871 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300872 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600873 .needs_file = 1,
874 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700875 .file_table = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700876 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700879 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700880 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700883 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700884 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600885 .file_table = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700886 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700889 .needs_mm = 1,
890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700892 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700893 .buffer_select = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700894 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700895 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700898 .needs_mm = 1,
899 .needs_file = 1,
900 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700901 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300902 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700903 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700904 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700907 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700908 .needs_blkcg = 1,
Jens Axboe4840e412019-12-25 22:03:45 -0700909 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300910 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700911 .needs_mm = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700912 .needs_blkcg = 1,
Jens Axboec1ca7572019-12-25 22:18:28 -0700913 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300914 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700915 .needs_mm = 1,
916 .needs_file = 1,
917 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700918 .pollout = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700919 .needs_blkcg = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700922 .needs_mm = 1,
923 .needs_file = 1,
924 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700925 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700926 .buffer_select = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700927 .needs_blkcg = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700928 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300929 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700930 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700931 .needs_fs = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700932 .needs_blkcg = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700933 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700934 [IORING_OP_EPOLL_CTL] = {
935 .unbound_nonreg_file = 1,
936 .file_table = 1,
937 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300938 [IORING_OP_SPLICE] = {
939 .needs_file = 1,
940 .hash_reg_file = 1,
941 .unbound_nonreg_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700942 .needs_blkcg = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700943 },
944 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700945 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300946 [IORING_OP_TEE] = {
947 .needs_file = 1,
948 .hash_reg_file = 1,
949 .unbound_nonreg_file = 1,
950 },
Jens Axboed3656342019-12-18 09:50:26 -0700951};
952
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700953enum io_mem_account {
954 ACCT_LOCKED,
955 ACCT_PINNED,
956};
957
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300958static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
959 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700960static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800961static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600962static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700963static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700964static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600965static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700966static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700967static int __io_sqe_files_update(struct io_ring_ctx *ctx,
968 struct io_uring_files_update *ip,
969 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300970static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300971static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700972static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
973 int fd, struct file **out_file, bool fixed);
974static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600975 const struct io_uring_sqe *sqe,
976 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600977static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600978
Jens Axboeb63534c2020-06-04 11:28:00 -0600979static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
980 struct iovec **iovec, struct iov_iter *iter,
981 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600982static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
983 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600984 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700985
986static struct kmem_cache *req_cachep;
987
Jens Axboe738277a2020-09-03 05:54:56 -0600988static const struct file_operations io_uring_fops __read_mostly;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989
990struct sock *io_uring_get_socket(struct file *file)
991{
992#if defined(CONFIG_UNIX)
993 if (file->f_op == &io_uring_fops) {
994 struct io_ring_ctx *ctx = file->private_data;
995
996 return ctx->ring_sock->sk;
997 }
998#endif
999 return NULL;
1000}
1001EXPORT_SYMBOL(io_uring_get_socket);
1002
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001003static inline void io_clean_op(struct io_kiocb *req)
1004{
Pavel Begunkovbb175342020-08-20 11:33:35 +03001005 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
1006 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001007 __io_clean_op(req);
1008}
1009
Jens Axboe4349f302020-07-09 15:07:01 -06001010static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001011{
1012 struct mm_struct *mm = current->mm;
1013
1014 if (mm) {
1015 kthread_unuse_mm(mm);
1016 mmput(mm);
1017 }
1018}
1019
1020static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1021{
1022 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +03001023 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -06001024 !ctx->sqo_task->mm ||
1025 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -06001026 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -06001027 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -06001028 }
1029
1030 return 0;
1031}
1032
1033static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1034 struct io_kiocb *req)
1035{
1036 if (!io_op_defs[req->opcode].needs_mm)
1037 return 0;
1038 return __io_sq_thread_acquire_mm(ctx);
1039}
1040
Dennis Zhou91d8f512020-09-16 13:41:05 -07001041static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1042 struct cgroup_subsys_state **cur_css)
1043
1044{
1045#ifdef CONFIG_BLK_CGROUP
1046 /* puts the old one when swapping */
1047 if (*cur_css != ctx->sqo_blkcg_css) {
1048 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1049 *cur_css = ctx->sqo_blkcg_css;
1050 }
1051#endif
1052}
1053
1054static void io_sq_thread_unassociate_blkcg(void)
1055{
1056#ifdef CONFIG_BLK_CGROUP
1057 kthread_associate_blkcg(NULL);
1058#endif
1059}
1060
Jens Axboec40f6372020-06-25 15:39:59 -06001061static inline void req_set_fail_links(struct io_kiocb *req)
1062{
1063 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1064 req->flags |= REQ_F_FAIL_LINK;
1065}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001066
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001067/*
1068 * Note: must call io_req_init_async() for the first time you
1069 * touch any members of io_wq_work.
1070 */
1071static inline void io_req_init_async(struct io_kiocb *req)
1072{
1073 if (req->flags & REQ_F_WORK_INITIALIZED)
1074 return;
1075
1076 memset(&req->work, 0, sizeof(req->work));
1077 req->flags |= REQ_F_WORK_INITIALIZED;
1078}
1079
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001080static inline bool io_async_submit(struct io_ring_ctx *ctx)
1081{
1082 return ctx->flags & IORING_SETUP_SQPOLL;
1083}
1084
Jens Axboe2b188cc2019-01-07 10:46:33 -07001085static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1086{
1087 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1088
Jens Axboe0f158b42020-05-14 17:18:39 -06001089 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001090}
1091
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001092static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1093{
1094 return !req->timeout.off;
1095}
1096
Jens Axboe2b188cc2019-01-07 10:46:33 -07001097static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1098{
1099 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001100 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101
1102 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1103 if (!ctx)
1104 return NULL;
1105
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001106 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1107 if (!ctx->fallback_req)
1108 goto err;
1109
Jens Axboe78076bb2019-12-04 19:56:40 -07001110 /*
1111 * Use 5 bits less than the max cq entries, that should give us around
1112 * 32 entries per hash list if totally full and uniformly spread.
1113 */
1114 hash_bits = ilog2(p->cq_entries);
1115 hash_bits -= 5;
1116 if (hash_bits <= 0)
1117 hash_bits = 1;
1118 ctx->cancel_hash_bits = hash_bits;
1119 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1120 GFP_KERNEL);
1121 if (!ctx->cancel_hash)
1122 goto err;
1123 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1124
Roman Gushchin21482892019-05-07 10:01:48 -07001125 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001126 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1127 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001128
1129 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001130 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001131 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001133 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001134 init_completion(&ctx->ref_comp);
1135 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001136 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001137 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001138 mutex_init(&ctx->uring_lock);
1139 init_waitqueue_head(&ctx->wait);
1140 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001141 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001142 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001143 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001144 init_waitqueue_head(&ctx->inflight_wait);
1145 spin_lock_init(&ctx->inflight_lock);
1146 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001147 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1148 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001150err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001151 if (ctx->fallback_req)
1152 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001153 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001154 kfree(ctx);
1155 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001156}
1157
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001158static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001159{
Jens Axboe2bc99302020-07-09 09:43:27 -06001160 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1161 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001162
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001163 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001164 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001165 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001166
Bob Liu9d858b22019-11-13 18:06:25 +08001167 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001168}
1169
Jens Axboede0617e2019-04-06 21:51:27 -06001170static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171{
Hristo Venev75b28af2019-08-26 17:23:46 +00001172 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173
Pavel Begunkov07910152020-01-17 03:52:46 +03001174 /* order cqe stores with ring update */
1175 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001176
Pavel Begunkov07910152020-01-17 03:52:46 +03001177 if (wq_has_sleeper(&ctx->cq_wait)) {
1178 wake_up_interruptible(&ctx->cq_wait);
1179 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180 }
1181}
1182
Jens Axboe51a4cc12020-08-10 10:55:56 -06001183/*
1184 * Returns true if we need to defer file table putting. This can only happen
1185 * from the error path with REQ_F_COMP_LOCKED set.
1186 */
1187static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001188{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001189 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001190 return false;
1191
1192 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001193
Jens Axboecccf0ee2020-01-27 16:34:48 -07001194 if (req->work.mm) {
1195 mmdrop(req->work.mm);
1196 req->work.mm = NULL;
1197 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001198#ifdef CONFIG_BLK_CGROUP
1199 if (req->work.blkcg_css)
1200 css_put(req->work.blkcg_css);
1201#endif
Jens Axboecccf0ee2020-01-27 16:34:48 -07001202 if (req->work.creds) {
1203 put_cred(req->work.creds);
1204 req->work.creds = NULL;
1205 }
Jens Axboeff002b32020-02-07 16:05:21 -07001206 if (req->work.fs) {
1207 struct fs_struct *fs = req->work.fs;
1208
Jens Axboe51a4cc12020-08-10 10:55:56 -06001209 if (req->flags & REQ_F_COMP_LOCKED)
1210 return true;
1211
Jens Axboeff002b32020-02-07 16:05:21 -07001212 spin_lock(&req->work.fs->lock);
1213 if (--fs->users)
1214 fs = NULL;
1215 spin_unlock(&req->work.fs->lock);
1216 if (fs)
1217 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001218 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001219 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001220
1221 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001222}
1223
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001224static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001225{
Jens Axboed3656342019-12-18 09:50:26 -07001226 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001227
Pavel Begunkov16d59802020-07-12 16:16:47 +03001228 io_req_init_async(req);
1229
Jens Axboed3656342019-12-18 09:50:26 -07001230 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001231 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001232 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001233 } else {
1234 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001235 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001236 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001237 if (!req->work.mm && def->needs_mm) {
1238 mmgrab(current->mm);
1239 req->work.mm = current->mm;
1240 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001241#ifdef CONFIG_BLK_CGROUP
1242 if (!req->work.blkcg_css && def->needs_blkcg) {
1243 rcu_read_lock();
1244 req->work.blkcg_css = blkcg_css();
1245 /*
1246 * This should be rare, either the cgroup is dying or the task
1247 * is moving cgroups. Just punt to root for the handful of ios.
1248 */
1249 if (!css_tryget_online(req->work.blkcg_css))
1250 req->work.blkcg_css = NULL;
1251 rcu_read_unlock();
1252 }
1253#endif
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001254 if (!req->work.creds)
1255 req->work.creds = get_current_cred();
1256 if (!req->work.fs && def->needs_fs) {
1257 spin_lock(&current->fs->lock);
1258 if (!current->fs->in_exec) {
1259 req->work.fs = current->fs;
1260 req->work.fs->users++;
1261 } else {
1262 req->work.flags |= IO_WQ_WORK_CANCEL;
1263 }
1264 spin_unlock(&current->fs->lock);
1265 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001266 if (def->needs_fsize)
1267 req->work.fsize = rlimit(RLIMIT_FSIZE);
1268 else
1269 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001270}
1271
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001272static void io_prep_async_link(struct io_kiocb *req)
1273{
1274 struct io_kiocb *cur;
1275
1276 io_prep_async_work(req);
1277 if (req->flags & REQ_F_LINK_HEAD)
1278 list_for_each_entry(cur, &req->link_list, link_list)
1279 io_prep_async_work(cur);
1280}
1281
Jens Axboe7271ef32020-08-10 09:55:22 -06001282static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001283{
Jackie Liua197f662019-11-08 08:09:12 -07001284 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001285 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001286
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001287 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1288 &req->work, req->flags);
1289 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001290 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001291}
1292
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001293static void io_queue_async_work(struct io_kiocb *req)
1294{
Jens Axboe7271ef32020-08-10 09:55:22 -06001295 struct io_kiocb *link;
1296
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001297 /* init ->work of the whole link before punting */
1298 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001299 link = __io_queue_async_work(req);
1300
1301 if (link)
1302 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001303}
1304
Jens Axboe5262f562019-09-17 12:26:57 -06001305static void io_kill_timeout(struct io_kiocb *req)
1306{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001307 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001308 int ret;
1309
Jens Axboee8c2bc12020-08-15 18:44:09 -07001310 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001311 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001312 atomic_set(&req->ctx->cq_timeouts,
1313 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001314 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001315 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001316 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001317 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001318 }
1319}
1320
Jens Axboef3606e32020-09-22 08:18:24 -06001321static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1322{
1323 struct io_ring_ctx *ctx = req->ctx;
1324
1325 if (!tsk || req->task == tsk)
1326 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001327 if (ctx->flags & IORING_SETUP_SQPOLL) {
1328 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1329 return true;
1330 }
Jens Axboef3606e32020-09-22 08:18:24 -06001331 return false;
1332}
1333
Jens Axboe76e1b642020-09-26 15:05:03 -06001334/*
1335 * Returns true if we found and killed one or more timeouts
1336 */
1337static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001338{
1339 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001340 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001341
1342 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001343 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001344 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001345 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001346 canceled++;
1347 }
Jens Axboef3606e32020-09-22 08:18:24 -06001348 }
Jens Axboe5262f562019-09-17 12:26:57 -06001349 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001350 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001351}
1352
Pavel Begunkov04518942020-05-26 20:34:05 +03001353static void __io_queue_deferred(struct io_ring_ctx *ctx)
1354{
1355 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001356 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1357 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001358 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001359
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001360 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001361 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001362 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001363 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001364 link = __io_queue_async_work(de->req);
1365 if (link) {
1366 __io_queue_linked_timeout(link);
1367 /* drop submission reference */
1368 link->flags |= REQ_F_COMP_LOCKED;
1369 io_put_req(link);
1370 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001371 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001372 } while (!list_empty(&ctx->defer_list));
1373}
1374
Pavel Begunkov360428f2020-05-30 14:54:17 +03001375static void io_flush_timeouts(struct io_ring_ctx *ctx)
1376{
1377 while (!list_empty(&ctx->timeout_list)) {
1378 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001379 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001380
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001381 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001382 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001383 if (req->timeout.target_seq != ctx->cached_cq_tail
1384 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001385 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001386
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001387 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001388 io_kill_timeout(req);
1389 }
1390}
1391
Jens Axboede0617e2019-04-06 21:51:27 -06001392static void io_commit_cqring(struct io_ring_ctx *ctx)
1393{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001394 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001395 __io_commit_cqring(ctx);
1396
Pavel Begunkov04518942020-05-26 20:34:05 +03001397 if (unlikely(!list_empty(&ctx->defer_list)))
1398 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001399}
1400
Jens Axboe90554202020-09-03 12:12:41 -06001401static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1402{
1403 struct io_rings *r = ctx->rings;
1404
1405 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1406}
1407
Jens Axboe2b188cc2019-01-07 10:46:33 -07001408static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1409{
Hristo Venev75b28af2019-08-26 17:23:46 +00001410 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411 unsigned tail;
1412
1413 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001414 /*
1415 * writes to the cq entry need to come after reading head; the
1416 * control dependency is enough as we're using WRITE_ONCE to
1417 * fill the cq entry
1418 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001419 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 return NULL;
1421
1422 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001423 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424}
1425
Jens Axboef2842ab2020-01-08 11:04:00 -07001426static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1427{
Jens Axboef0b493e2020-02-01 21:30:11 -07001428 if (!ctx->cq_ev_fd)
1429 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001430 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1431 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001432 if (!ctx->eventfd_async)
1433 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001434 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001435}
1436
Jens Axboeb41e9852020-02-17 09:52:41 -07001437static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001438{
1439 if (waitqueue_active(&ctx->wait))
1440 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001441 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1442 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001443 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001444 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001445}
1446
Pavel Begunkov46930142020-07-30 18:43:49 +03001447static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1448{
1449 if (list_empty(&ctx->cq_overflow_list)) {
1450 clear_bit(0, &ctx->sq_check_overflow);
1451 clear_bit(0, &ctx->cq_check_overflow);
1452 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1453 }
1454}
1455
Jens Axboee6c8aa92020-09-28 13:10:13 -06001456static inline bool io_match_files(struct io_kiocb *req,
1457 struct files_struct *files)
1458{
1459 if (!files)
1460 return true;
1461 if (req->flags & REQ_F_WORK_INITIALIZED)
1462 return req->work.files == files;
1463 return false;
1464}
1465
Jens Axboec4a2ed72019-11-21 21:01:26 -07001466/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001467static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1468 struct task_struct *tsk,
1469 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001470{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001471 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001472 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001473 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001474 unsigned long flags;
1475 LIST_HEAD(list);
1476
1477 if (!force) {
1478 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001479 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001480 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1481 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001482 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001483 }
1484
1485 spin_lock_irqsave(&ctx->completion_lock, flags);
1486
1487 /* if force is set, the ring is going away. always drop after that */
1488 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001489 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001490
Jens Axboec4a2ed72019-11-21 21:01:26 -07001491 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001492 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1493 if (tsk && req->task != tsk)
1494 continue;
1495 if (!io_match_files(req, files))
1496 continue;
1497
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001498 cqe = io_get_cqring(ctx);
1499 if (!cqe && !force)
1500 break;
1501
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001502 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001503 if (cqe) {
1504 WRITE_ONCE(cqe->user_data, req->user_data);
1505 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001506 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001507 } else {
1508 WRITE_ONCE(ctx->rings->cq_overflow,
1509 atomic_inc_return(&ctx->cached_cq_overflow));
1510 }
1511 }
1512
1513 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001514 io_cqring_mark_overflow(ctx);
1515
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001516 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1517 io_cqring_ev_posted(ctx);
1518
1519 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001520 req = list_first_entry(&list, struct io_kiocb, compl.list);
1521 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001522 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001523 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001524
1525 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001526}
1527
Jens Axboebcda7ba2020-02-23 16:42:51 -07001528static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001529{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001530 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001531 struct io_uring_cqe *cqe;
1532
Jens Axboe78e19bb2019-11-06 15:21:34 -07001533 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001534
Jens Axboe2b188cc2019-01-07 10:46:33 -07001535 /*
1536 * If we can't get a cq entry, userspace overflowed the
1537 * submission (by quite a lot). Increment the overflow count in
1538 * the ring.
1539 */
1540 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001541 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001542 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001543 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001544 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001545 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1546 /*
1547 * If we're in ring overflow flush mode, or in task cancel mode,
1548 * then we cannot store the request for later flushing, we need
1549 * to drop it on the floor.
1550 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001551 WRITE_ONCE(ctx->rings->cq_overflow,
1552 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001553 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001554 if (list_empty(&ctx->cq_overflow_list)) {
1555 set_bit(0, &ctx->sq_check_overflow);
1556 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001557 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001558 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001559 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001560 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001561 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001562 refcount_inc(&req->refs);
1563 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001564 }
1565}
1566
Jens Axboebcda7ba2020-02-23 16:42:51 -07001567static void io_cqring_fill_event(struct io_kiocb *req, long res)
1568{
1569 __io_cqring_fill_event(req, res, 0);
1570}
1571
Jens Axboee1e16092020-06-22 09:17:17 -06001572static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001573{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001574 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001575 unsigned long flags;
1576
1577 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001578 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579 io_commit_cqring(ctx);
1580 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1581
Jens Axboe8c838782019-03-12 15:48:16 -06001582 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001583}
1584
Jens Axboe229a7b62020-06-22 10:13:11 -06001585static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001586{
Jens Axboe229a7b62020-06-22 10:13:11 -06001587 struct io_ring_ctx *ctx = cs->ctx;
1588
1589 spin_lock_irq(&ctx->completion_lock);
1590 while (!list_empty(&cs->list)) {
1591 struct io_kiocb *req;
1592
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001593 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1594 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001595 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001596 if (!(req->flags & REQ_F_LINK_HEAD)) {
1597 req->flags |= REQ_F_COMP_LOCKED;
1598 io_put_req(req);
1599 } else {
1600 spin_unlock_irq(&ctx->completion_lock);
1601 io_put_req(req);
1602 spin_lock_irq(&ctx->completion_lock);
1603 }
1604 }
1605 io_commit_cqring(ctx);
1606 spin_unlock_irq(&ctx->completion_lock);
1607
1608 io_cqring_ev_posted(ctx);
1609 cs->nr = 0;
1610}
1611
1612static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1613 struct io_comp_state *cs)
1614{
1615 if (!cs) {
1616 io_cqring_add_event(req, res, cflags);
1617 io_put_req(req);
1618 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001619 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001620 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001621 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001622 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001623 if (++cs->nr >= 32)
1624 io_submit_flush_completions(cs);
1625 }
Jens Axboee1e16092020-06-22 09:17:17 -06001626}
1627
1628static void io_req_complete(struct io_kiocb *req, long res)
1629{
Jens Axboe229a7b62020-06-22 10:13:11 -06001630 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001631}
1632
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001633static inline bool io_is_fallback_req(struct io_kiocb *req)
1634{
1635 return req == (struct io_kiocb *)
1636 ((unsigned long) req->ctx->fallback_req & ~1UL);
1637}
1638
1639static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1640{
1641 struct io_kiocb *req;
1642
1643 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001644 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001645 return req;
1646
1647 return NULL;
1648}
1649
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001650static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1651 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652{
Jens Axboefd6fab22019-03-14 16:30:06 -06001653 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001654 struct io_kiocb *req;
1655
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001656 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001657 size_t sz;
1658 int ret;
1659
1660 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001661 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1662
1663 /*
1664 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1665 * retry single alloc to be on the safe side.
1666 */
1667 if (unlikely(ret <= 0)) {
1668 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1669 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001670 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001671 ret = 1;
1672 }
Jens Axboe2579f912019-01-09 09:10:43 -07001673 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001674 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001675 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001676 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001677 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678 }
1679
Jens Axboe2579f912019-01-09 09:10:43 -07001680 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001681fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001682 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001683}
1684
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001685static inline void io_put_file(struct io_kiocb *req, struct file *file,
1686 bool fixed)
1687{
1688 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001689 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001690 else
1691 fput(file);
1692}
1693
Jens Axboe51a4cc12020-08-10 10:55:56 -06001694static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001695{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001696 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001697
Jens Axboee8c2bc12020-08-15 18:44:09 -07001698 if (req->async_data)
1699 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001700 if (req->file)
1701 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001702
Jens Axboe51a4cc12020-08-10 10:55:56 -06001703 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001704}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001705
Jens Axboe51a4cc12020-08-10 10:55:56 -06001706static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001707{
Jens Axboe0f212202020-09-13 13:09:39 -06001708 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001709 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001710
Jens Axboe0f212202020-09-13 13:09:39 -06001711 atomic_long_inc(&tctx->req_complete);
1712 if (tctx->in_idle)
1713 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001714 put_task_struct(req->task);
1715
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001716 if (likely(!io_is_fallback_req(req)))
1717 kmem_cache_free(req_cachep, req);
1718 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001719 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1720 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001721}
1722
Jens Axboe51a4cc12020-08-10 10:55:56 -06001723static void io_req_task_file_table_put(struct callback_head *cb)
1724{
1725 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1726 struct fs_struct *fs = req->work.fs;
1727
1728 spin_lock(&req->work.fs->lock);
1729 if (--fs->users)
1730 fs = NULL;
1731 spin_unlock(&req->work.fs->lock);
1732 if (fs)
1733 free_fs_struct(fs);
1734 req->work.fs = NULL;
1735 __io_free_req_finish(req);
1736}
1737
1738static void __io_free_req(struct io_kiocb *req)
1739{
1740 if (!io_dismantle_req(req)) {
1741 __io_free_req_finish(req);
1742 } else {
1743 int ret;
1744
1745 init_task_work(&req->task_work, io_req_task_file_table_put);
1746 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1747 if (unlikely(ret)) {
1748 struct task_struct *tsk;
1749
1750 tsk = io_wq_get_task(req->ctx->io_wq);
1751 task_work_add(tsk, &req->task_work, 0);
1752 }
1753 }
1754}
1755
Jackie Liua197f662019-11-08 08:09:12 -07001756static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001757{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001758 struct io_timeout_data *io = req->async_data;
Jackie Liua197f662019-11-08 08:09:12 -07001759 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001760 int ret;
1761
Jens Axboee8c2bc12020-08-15 18:44:09 -07001762 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001763 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001764 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001765 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001766 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001767 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001768 return true;
1769 }
1770
1771 return false;
1772}
1773
Jens Axboeab0b6452020-06-30 08:43:15 -06001774static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001775{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001776 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001777 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001778
1779 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001780 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001781 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1782 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001783 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001784
1785 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001786 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001787 wake_ev = io_link_cancel_timeout(link);
1788 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001789 return wake_ev;
1790}
1791
1792static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001793{
Jens Axboe2665abf2019-11-05 12:40:47 -07001794 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001795 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001796
Jens Axboeab0b6452020-06-30 08:43:15 -06001797 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1798 unsigned long flags;
1799
1800 spin_lock_irqsave(&ctx->completion_lock, flags);
1801 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001802 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001803 } else {
1804 wake_ev = __io_kill_linked_timeout(req);
1805 }
1806
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001807 if (wake_ev)
1808 io_cqring_ev_posted(ctx);
1809}
1810
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001811static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001812{
1813 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001814
Jens Axboe9e645e112019-05-10 16:07:28 -06001815 /*
1816 * The list should never be empty when we are called here. But could
1817 * potentially happen if the chain is messed up, check to be on the
1818 * safe side.
1819 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001820 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001821 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001822
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001823 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1824 list_del_init(&req->link_list);
1825 if (!list_empty(&nxt->link_list))
1826 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001827 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001828}
1829
1830/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001831 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001832 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001833static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001834{
Jens Axboe2665abf2019-11-05 12:40:47 -07001835 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001836
1837 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001838 struct io_kiocb *link = list_first_entry(&req->link_list,
1839 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001840
Pavel Begunkov44932332019-12-05 16:16:35 +03001841 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001842 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001843
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001844 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001845 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001846 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001847 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001848 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001849
1850 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001851 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001852}
1853
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001854static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001855{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001856 struct io_ring_ctx *ctx = req->ctx;
1857
1858 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1859 unsigned long flags;
1860
1861 spin_lock_irqsave(&ctx->completion_lock, flags);
1862 __io_fail_links(req);
1863 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1864 } else {
1865 __io_fail_links(req);
1866 }
1867
Jens Axboe9e645e112019-05-10 16:07:28 -06001868 io_cqring_ev_posted(ctx);
1869}
1870
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001871static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001872{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001873 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001874 if (req->flags & REQ_F_LINK_TIMEOUT)
1875 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001876
Jens Axboe9e645e112019-05-10 16:07:28 -06001877 /*
1878 * If LINK is set, we have dependent requests in this chain. If we
1879 * didn't fail this request, queue the first one up, moving any other
1880 * dependencies to the next request. In case of failure, fail the rest
1881 * of the chain.
1882 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001883 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1884 return io_req_link_next(req);
1885 io_fail_links(req);
1886 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001887}
Jens Axboe2665abf2019-11-05 12:40:47 -07001888
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001889static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1890{
1891 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1892 return NULL;
1893 return __io_req_find_next(req);
1894}
1895
Jens Axboefd7d6de2020-08-23 11:00:37 -06001896static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1897 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001898{
1899 struct task_struct *tsk = req->task;
1900 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001901 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001902
Jens Axboe6200b0a2020-09-13 14:38:30 -06001903 if (tsk->flags & PF_EXITING)
1904 return -ESRCH;
1905
Jens Axboec2c4c832020-07-01 15:37:11 -06001906 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001907 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1908 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1909 * processing task_work. There's no reliable way to tell if TWA_RESUME
1910 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001911 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001912 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001913 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001914 notify = TWA_SIGNAL;
1915
1916 ret = task_work_add(tsk, cb, notify);
1917 if (!ret)
1918 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001919
Jens Axboec2c4c832020-07-01 15:37:11 -06001920 return ret;
1921}
1922
Jens Axboec40f6372020-06-25 15:39:59 -06001923static void __io_req_task_cancel(struct io_kiocb *req, int error)
1924{
1925 struct io_ring_ctx *ctx = req->ctx;
1926
1927 spin_lock_irq(&ctx->completion_lock);
1928 io_cqring_fill_event(req, error);
1929 io_commit_cqring(ctx);
1930 spin_unlock_irq(&ctx->completion_lock);
1931
1932 io_cqring_ev_posted(ctx);
1933 req_set_fail_links(req);
1934 io_double_put_req(req);
1935}
1936
1937static void io_req_task_cancel(struct callback_head *cb)
1938{
1939 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001940 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001941
1942 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001943 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001944}
1945
1946static void __io_req_task_submit(struct io_kiocb *req)
1947{
1948 struct io_ring_ctx *ctx = req->ctx;
1949
Jens Axboec40f6372020-06-25 15:39:59 -06001950 if (!__io_sq_thread_acquire_mm(ctx)) {
1951 mutex_lock(&ctx->uring_lock);
1952 __io_queue_sqe(req, NULL, NULL);
1953 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001954 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001955 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001956 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001957}
1958
Jens Axboec40f6372020-06-25 15:39:59 -06001959static void io_req_task_submit(struct callback_head *cb)
1960{
1961 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001962 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001963
1964 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001965 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001966}
1967
1968static void io_req_task_queue(struct io_kiocb *req)
1969{
Jens Axboec40f6372020-06-25 15:39:59 -06001970 int ret;
1971
1972 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001973 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001974
Jens Axboefd7d6de2020-08-23 11:00:37 -06001975 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001976 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001977 struct task_struct *tsk;
1978
Jens Axboec40f6372020-06-25 15:39:59 -06001979 init_task_work(&req->task_work, io_req_task_cancel);
1980 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001981 task_work_add(tsk, &req->task_work, 0);
1982 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001983 }
Jens Axboec40f6372020-06-25 15:39:59 -06001984}
1985
Pavel Begunkovc3524382020-06-28 12:52:32 +03001986static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001987{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001988 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001989
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001990 if (nxt)
1991 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001992}
1993
Jens Axboe9e645e112019-05-10 16:07:28 -06001994static void io_free_req(struct io_kiocb *req)
1995{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001996 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001997 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001998}
1999
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002000struct req_batch {
2001 void *reqs[IO_IOPOLL_BATCH];
2002 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002003
2004 struct task_struct *task;
2005 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002006};
2007
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002008static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002009{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002010 rb->to_free = 0;
2011 rb->task_refs = 0;
2012 rb->task = NULL;
2013}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002014
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002015static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2016 struct req_batch *rb)
2017{
2018 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2019 percpu_ref_put_many(&ctx->refs, rb->to_free);
2020 rb->to_free = 0;
2021}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002022
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002023static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2024 struct req_batch *rb)
2025{
2026 if (rb->to_free)
2027 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002028 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002029 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002030 put_task_struct_many(rb->task, rb->task_refs);
2031 rb->task = NULL;
2032 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002033}
2034
2035static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2036{
2037 if (unlikely(io_is_fallback_req(req))) {
2038 io_free_req(req);
2039 return;
2040 }
2041 if (req->flags & REQ_F_LINK_HEAD)
2042 io_queue_next(req);
2043
Jens Axboee3bc8e92020-09-24 08:45:57 -06002044 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002045 if (rb->task) {
2046 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002047 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06002048 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002049 rb->task = req->task;
2050 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002051 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002052 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002053
Jens Axboe51a4cc12020-08-10 10:55:56 -06002054 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002055 rb->reqs[rb->to_free++] = req;
2056 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2057 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002058}
2059
Jens Axboeba816ad2019-09-28 11:36:45 -06002060/*
2061 * Drop reference to request, return next in chain (if there is one) if this
2062 * was the last reference to this request.
2063 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002064static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002065{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002066 struct io_kiocb *nxt = NULL;
2067
Jens Axboe2a44f462020-02-25 13:25:41 -07002068 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002069 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002070 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002071 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002072 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073}
2074
Jens Axboe2b188cc2019-01-07 10:46:33 -07002075static void io_put_req(struct io_kiocb *req)
2076{
Jens Axboedef596e2019-01-09 08:59:42 -07002077 if (refcount_dec_and_test(&req->refs))
2078 io_free_req(req);
2079}
2080
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002081static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002082{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002083 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002084
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002085 /*
2086 * A ref is owned by io-wq in which context we're. So, if that's the
2087 * last one, it's safe to steal next work. False negatives are Ok,
2088 * it just will be re-punted async in io_put_work()
2089 */
2090 if (refcount_read(&req->refs) != 1)
2091 return NULL;
2092
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002093 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002094 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002095}
2096
Jens Axboe978db572019-11-14 22:39:04 -07002097/*
2098 * Must only be used if we don't need to care about links, usually from
2099 * within the completion handling itself.
2100 */
2101static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002102{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002103 /* drop both submit and complete references */
2104 if (refcount_sub_and_test(2, &req->refs))
2105 __io_free_req(req);
2106}
2107
Jens Axboe978db572019-11-14 22:39:04 -07002108static void io_double_put_req(struct io_kiocb *req)
2109{
2110 /* drop both submit and complete references */
2111 if (refcount_sub_and_test(2, &req->refs))
2112 io_free_req(req);
2113}
2114
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002115static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002116{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002117 struct io_rings *rings = ctx->rings;
2118
Jens Axboead3eb2c2019-12-18 17:12:20 -07002119 if (test_bit(0, &ctx->cq_check_overflow)) {
2120 /*
2121 * noflush == true is from the waitqueue handler, just ensure
2122 * we wake up the task, and the next invocation will flush the
2123 * entries. We cannot safely to it from here.
2124 */
2125 if (noflush && !list_empty(&ctx->cq_overflow_list))
2126 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002127
Jens Axboee6c8aa92020-09-28 13:10:13 -06002128 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002129 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002130
Jens Axboea3a0e432019-08-20 11:03:11 -06002131 /* See comment at the top of this file */
2132 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002133 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002134}
2135
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002136static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2137{
2138 struct io_rings *rings = ctx->rings;
2139
2140 /* make sure SQ entry isn't read before tail */
2141 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2142}
2143
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002144static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002145{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002146 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002147
Jens Axboebcda7ba2020-02-23 16:42:51 -07002148 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2149 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002150 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002151 kfree(kbuf);
2152 return cflags;
2153}
2154
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002155static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2156{
2157 struct io_buffer *kbuf;
2158
2159 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2160 return io_put_kbuf(req, kbuf);
2161}
2162
Jens Axboe4c6e2772020-07-01 11:29:10 -06002163static inline bool io_run_task_work(void)
2164{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002165 /*
2166 * Not safe to run on exiting task, and the task_work handling will
2167 * not add work to such a task.
2168 */
2169 if (unlikely(current->flags & PF_EXITING))
2170 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002171 if (current->task_works) {
2172 __set_current_state(TASK_RUNNING);
2173 task_work_run();
2174 return true;
2175 }
2176
2177 return false;
2178}
2179
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002180static void io_iopoll_queue(struct list_head *again)
2181{
2182 struct io_kiocb *req;
2183
2184 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002185 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2186 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002187 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002188 } while (!list_empty(again));
2189}
2190
Jens Axboedef596e2019-01-09 08:59:42 -07002191/*
2192 * Find and free completed poll iocbs
2193 */
2194static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2195 struct list_head *done)
2196{
Jens Axboe8237e042019-12-28 10:48:22 -07002197 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002198 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002199 LIST_HEAD(again);
2200
2201 /* order with ->result store in io_complete_rw_iopoll() */
2202 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002203
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002204 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002205 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002206 int cflags = 0;
2207
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002208 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002209 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002210 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002211 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002212 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002213 continue;
2214 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002215 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002216
Jens Axboebcda7ba2020-02-23 16:42:51 -07002217 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002218 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002219
2220 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002221 (*nr_events)++;
2222
Pavel Begunkovc3524382020-06-28 12:52:32 +03002223 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002224 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002225 }
Jens Axboedef596e2019-01-09 08:59:42 -07002226
Jens Axboe09bb8392019-03-13 12:39:28 -06002227 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002228 if (ctx->flags & IORING_SETUP_SQPOLL)
2229 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002230 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002231
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002232 if (!list_empty(&again))
2233 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002234}
2235
Jens Axboedef596e2019-01-09 08:59:42 -07002236static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2237 long min)
2238{
2239 struct io_kiocb *req, *tmp;
2240 LIST_HEAD(done);
2241 bool spin;
2242 int ret;
2243
2244 /*
2245 * Only spin for completions if we don't have multiple devices hanging
2246 * off our complete list, and we're under the requested amount.
2247 */
2248 spin = !ctx->poll_multi_file && *nr_events < min;
2249
2250 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002251 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002252 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002253
2254 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002255 * Move completed and retryable entries to our local lists.
2256 * If we find a request that requires polling, break out
2257 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002258 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002259 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002260 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002261 continue;
2262 }
2263 if (!list_empty(&done))
2264 break;
2265
2266 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2267 if (ret < 0)
2268 break;
2269
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002270 /* iopoll may have completed current req */
2271 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002272 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002273
Jens Axboedef596e2019-01-09 08:59:42 -07002274 if (ret && spin)
2275 spin = false;
2276 ret = 0;
2277 }
2278
2279 if (!list_empty(&done))
2280 io_iopoll_complete(ctx, nr_events, &done);
2281
2282 return ret;
2283}
2284
2285/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002286 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002287 * non-spinning poll check - we'll still enter the driver poll loop, but only
2288 * as a non-spinning completion check.
2289 */
2290static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2291 long min)
2292{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002293 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002294 int ret;
2295
2296 ret = io_do_iopoll(ctx, nr_events, min);
2297 if (ret < 0)
2298 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002299 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002300 return 0;
2301 }
2302
2303 return 1;
2304}
2305
2306/*
2307 * We can't just wait for polled events to come to us, we have to actively
2308 * find and complete them.
2309 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002310static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002311{
2312 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2313 return;
2314
2315 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002316 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002317 unsigned int nr_events = 0;
2318
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002319 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002320
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002321 /* let it sleep and repeat later if can't complete a request */
2322 if (nr_events == 0)
2323 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002324 /*
2325 * Ensure we allow local-to-the-cpu processing to take place,
2326 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002327 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002328 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002329 if (need_resched()) {
2330 mutex_unlock(&ctx->uring_lock);
2331 cond_resched();
2332 mutex_lock(&ctx->uring_lock);
2333 }
Jens Axboedef596e2019-01-09 08:59:42 -07002334 }
2335 mutex_unlock(&ctx->uring_lock);
2336}
2337
Pavel Begunkov7668b922020-07-07 16:36:21 +03002338static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002339{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002340 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002341 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002342
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002343 /*
2344 * We disallow the app entering submit/complete with polling, but we
2345 * still need to lock the ring to prevent racing with polled issue
2346 * that got punted to a workqueue.
2347 */
2348 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002349 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002350 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002351 * Don't enter poll loop if we already have events pending.
2352 * If we do, we can potentially be spinning for commands that
2353 * already triggered a CQE (eg in error).
2354 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002355 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002356 break;
2357
2358 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002359 * If a submit got punted to a workqueue, we can have the
2360 * application entering polling for a command before it gets
2361 * issued. That app will hold the uring_lock for the duration
2362 * of the poll right here, so we need to take a breather every
2363 * now and then to ensure that the issue has a chance to add
2364 * the poll to the issued list. Otherwise we can spin here
2365 * forever, while the workqueue is stuck trying to acquire the
2366 * very same mutex.
2367 */
2368 if (!(++iters & 7)) {
2369 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002370 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002371 mutex_lock(&ctx->uring_lock);
2372 }
2373
Pavel Begunkov7668b922020-07-07 16:36:21 +03002374 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002375 if (ret <= 0)
2376 break;
2377 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002378 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002379
Jens Axboe500f9fb2019-08-19 12:15:59 -06002380 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002381 return ret;
2382}
2383
Jens Axboe491381ce2019-10-17 09:20:46 -06002384static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002385{
Jens Axboe491381ce2019-10-17 09:20:46 -06002386 /*
2387 * Tell lockdep we inherited freeze protection from submission
2388 * thread.
2389 */
2390 if (req->flags & REQ_F_ISREG) {
2391 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392
Jens Axboe491381ce2019-10-17 09:20:46 -06002393 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002394 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002395 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002396}
2397
Jens Axboea1d7c392020-06-22 11:09:46 -06002398static void io_complete_rw_common(struct kiocb *kiocb, long res,
2399 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400{
Jens Axboe9adbd452019-12-20 08:45:55 -07002401 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002402 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002403
Jens Axboe491381ce2019-10-17 09:20:46 -06002404 if (kiocb->ki_flags & IOCB_WRITE)
2405 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002406
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002407 if (res != req->result)
2408 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002409 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002410 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002411 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002412}
2413
Jens Axboeb63534c2020-06-04 11:28:00 -06002414#ifdef CONFIG_BLOCK
2415static bool io_resubmit_prep(struct io_kiocb *req, int error)
2416{
2417 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2418 ssize_t ret = -ECANCELED;
2419 struct iov_iter iter;
2420 int rw;
2421
2422 if (error) {
2423 ret = error;
2424 goto end_req;
2425 }
2426
2427 switch (req->opcode) {
2428 case IORING_OP_READV:
2429 case IORING_OP_READ_FIXED:
2430 case IORING_OP_READ:
2431 rw = READ;
2432 break;
2433 case IORING_OP_WRITEV:
2434 case IORING_OP_WRITE_FIXED:
2435 case IORING_OP_WRITE:
2436 rw = WRITE;
2437 break;
2438 default:
2439 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2440 req->opcode);
2441 goto end_req;
2442 }
2443
Jens Axboee8c2bc12020-08-15 18:44:09 -07002444 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002445 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2446 if (ret < 0)
2447 goto end_req;
2448 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2449 if (!ret)
2450 return true;
2451 kfree(iovec);
2452 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002453 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002454 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002455end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002456 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002457 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002458 return false;
2459}
Jens Axboeb63534c2020-06-04 11:28:00 -06002460#endif
2461
2462static bool io_rw_reissue(struct io_kiocb *req, long res)
2463{
2464#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002465 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002466 int ret;
2467
Jens Axboe355afae2020-09-02 09:30:31 -06002468 if (!S_ISBLK(mode) && !S_ISREG(mode))
2469 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002470 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2471 return false;
2472
Jens Axboefdee9462020-08-27 16:46:24 -06002473 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002474
Jens Axboefdee9462020-08-27 16:46:24 -06002475 if (io_resubmit_prep(req, ret)) {
2476 refcount_inc(&req->refs);
2477 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002478 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002479 }
2480
Jens Axboeb63534c2020-06-04 11:28:00 -06002481#endif
2482 return false;
2483}
2484
Jens Axboea1d7c392020-06-22 11:09:46 -06002485static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2486 struct io_comp_state *cs)
2487{
2488 if (!io_rw_reissue(req, res))
2489 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002490}
2491
2492static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2493{
Jens Axboe9adbd452019-12-20 08:45:55 -07002494 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002495
Jens Axboea1d7c392020-06-22 11:09:46 -06002496 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002497}
2498
Jens Axboedef596e2019-01-09 08:59:42 -07002499static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2500{
Jens Axboe9adbd452019-12-20 08:45:55 -07002501 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002502
Jens Axboe491381ce2019-10-17 09:20:46 -06002503 if (kiocb->ki_flags & IOCB_WRITE)
2504 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002505
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002506 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002507 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002508
2509 WRITE_ONCE(req->result, res);
2510 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002511 smp_wmb();
2512 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002513}
2514
2515/*
2516 * After the iocb has been issued, it's safe to be found on the poll list.
2517 * Adding the kiocb to the list AFTER submission ensures that we don't
2518 * find it from a io_iopoll_getevents() thread before the issuer is done
2519 * accessing the kiocb cookie.
2520 */
2521static void io_iopoll_req_issued(struct io_kiocb *req)
2522{
2523 struct io_ring_ctx *ctx = req->ctx;
2524
2525 /*
2526 * Track whether we have multiple files in our lists. This will impact
2527 * how we do polling eventually, not spinning if we're on potentially
2528 * different devices.
2529 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002530 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002531 ctx->poll_multi_file = false;
2532 } else if (!ctx->poll_multi_file) {
2533 struct io_kiocb *list_req;
2534
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002535 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002536 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002537 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002538 ctx->poll_multi_file = true;
2539 }
2540
2541 /*
2542 * For fast devices, IO may have already completed. If it has, add
2543 * it to the front so we find it first.
2544 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002545 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002546 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002547 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002548 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002549
Jens Axboe534ca6d2020-09-02 13:52:19 -06002550 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2551 wq_has_sleeper(&ctx->sq_data->wait))
2552 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002553}
2554
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002555static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002556{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002557 if (state->has_refs)
2558 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002559 state->file = NULL;
2560}
2561
2562static inline void io_state_file_put(struct io_submit_state *state)
2563{
2564 if (state->file)
2565 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002566}
2567
2568/*
2569 * Get as many references to a file as we have IOs left in this submission,
2570 * assuming most submissions are for one file, or at least that each file
2571 * has more than one submission.
2572 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002573static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002574{
2575 if (!state)
2576 return fget(fd);
2577
2578 if (state->file) {
2579 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002580 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002581 state->ios_left--;
2582 return state->file;
2583 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002584 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002585 }
2586 state->file = fget_many(fd, state->ios_left);
2587 if (!state->file)
2588 return NULL;
2589
2590 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002591 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002592 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002593 return state->file;
2594}
2595
Jens Axboe4503b762020-06-01 10:00:27 -06002596static bool io_bdev_nowait(struct block_device *bdev)
2597{
2598#ifdef CONFIG_BLOCK
2599 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2600#else
2601 return true;
2602#endif
2603}
2604
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605/*
2606 * If we tracked the file through the SCM inflight mechanism, we could support
2607 * any file. For now, just ensure that anything potentially problematic is done
2608 * inline.
2609 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002610static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002611{
2612 umode_t mode = file_inode(file)->i_mode;
2613
Jens Axboe4503b762020-06-01 10:00:27 -06002614 if (S_ISBLK(mode)) {
2615 if (io_bdev_nowait(file->f_inode->i_bdev))
2616 return true;
2617 return false;
2618 }
2619 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002620 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002621 if (S_ISREG(mode)) {
2622 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2623 file->f_op != &io_uring_fops)
2624 return true;
2625 return false;
2626 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002627
Jens Axboec5b85622020-06-09 19:23:05 -06002628 /* any ->read/write should understand O_NONBLOCK */
2629 if (file->f_flags & O_NONBLOCK)
2630 return true;
2631
Jens Axboeaf197f52020-04-28 13:15:06 -06002632 if (!(file->f_mode & FMODE_NOWAIT))
2633 return false;
2634
2635 if (rw == READ)
2636 return file->f_op->read_iter != NULL;
2637
2638 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639}
2640
Jens Axboe3529d8c2019-12-19 18:24:38 -07002641static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2642 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643{
Jens Axboedef596e2019-01-09 08:59:42 -07002644 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002645 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002646 unsigned ioprio;
2647 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002648
Jens Axboe491381ce2019-10-17 09:20:46 -06002649 if (S_ISREG(file_inode(req->file)->i_mode))
2650 req->flags |= REQ_F_ISREG;
2651
Jens Axboe2b188cc2019-01-07 10:46:33 -07002652 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002653 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2654 req->flags |= REQ_F_CUR_POS;
2655 kiocb->ki_pos = req->file->f_pos;
2656 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002657 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002658 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2659 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2660 if (unlikely(ret))
2661 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002662
2663 ioprio = READ_ONCE(sqe->ioprio);
2664 if (ioprio) {
2665 ret = ioprio_check_cap(ioprio);
2666 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002667 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002668
2669 kiocb->ki_ioprio = ioprio;
2670 } else
2671 kiocb->ki_ioprio = get_current_ioprio();
2672
Stefan Bühler8449eed2019-04-27 20:34:19 +02002673 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002674 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002675 req->flags |= REQ_F_NOWAIT;
2676
2677 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002678 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002679
Jens Axboedef596e2019-01-09 08:59:42 -07002680 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002681 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2682 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002683 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002684
Jens Axboedef596e2019-01-09 08:59:42 -07002685 kiocb->ki_flags |= IOCB_HIPRI;
2686 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002687 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002688 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002689 if (kiocb->ki_flags & IOCB_HIPRI)
2690 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002691 kiocb->ki_complete = io_complete_rw;
2692 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002693
Jens Axboe3529d8c2019-12-19 18:24:38 -07002694 req->rw.addr = READ_ONCE(sqe->addr);
2695 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002696 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002697 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002698}
2699
2700static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2701{
2702 switch (ret) {
2703 case -EIOCBQUEUED:
2704 break;
2705 case -ERESTARTSYS:
2706 case -ERESTARTNOINTR:
2707 case -ERESTARTNOHAND:
2708 case -ERESTART_RESTARTBLOCK:
2709 /*
2710 * We can't just restart the syscall, since previously
2711 * submitted sqes may already be in progress. Just fail this
2712 * IO with EINTR.
2713 */
2714 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002715 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002716 default:
2717 kiocb->ki_complete(kiocb, ret, 0);
2718 }
2719}
2720
Jens Axboea1d7c392020-06-22 11:09:46 -06002721static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2722 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002723{
Jens Axboeba042912019-12-25 16:33:42 -07002724 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002725 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002726
Jens Axboe227c0c92020-08-13 11:51:40 -06002727 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002728 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002729 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002730 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002731 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002732 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002733 }
2734
Jens Axboeba042912019-12-25 16:33:42 -07002735 if (req->flags & REQ_F_CUR_POS)
2736 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002737 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002738 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002739 else
2740 io_rw_done(kiocb, ret);
2741}
2742
Jens Axboe9adbd452019-12-20 08:45:55 -07002743static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002744 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002745{
Jens Axboe9adbd452019-12-20 08:45:55 -07002746 struct io_ring_ctx *ctx = req->ctx;
2747 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002748 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002749 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002750 size_t offset;
2751 u64 buf_addr;
2752
Jens Axboeedafcce2019-01-09 09:16:05 -07002753 if (unlikely(buf_index >= ctx->nr_user_bufs))
2754 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002755 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2756 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002757 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002758
2759 /* overflow */
2760 if (buf_addr + len < buf_addr)
2761 return -EFAULT;
2762 /* not inside the mapped region */
2763 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2764 return -EFAULT;
2765
2766 /*
2767 * May not be a start of buffer, set size appropriately
2768 * and advance us to the beginning.
2769 */
2770 offset = buf_addr - imu->ubuf;
2771 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002772
2773 if (offset) {
2774 /*
2775 * Don't use iov_iter_advance() here, as it's really slow for
2776 * using the latter parts of a big fixed buffer - it iterates
2777 * over each segment manually. We can cheat a bit here, because
2778 * we know that:
2779 *
2780 * 1) it's a BVEC iter, we set it up
2781 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2782 * first and last bvec
2783 *
2784 * So just find our index, and adjust the iterator afterwards.
2785 * If the offset is within the first bvec (or the whole first
2786 * bvec, just use iov_iter_advance(). This makes it easier
2787 * since we can just skip the first segment, which may not
2788 * be PAGE_SIZE aligned.
2789 */
2790 const struct bio_vec *bvec = imu->bvec;
2791
2792 if (offset <= bvec->bv_len) {
2793 iov_iter_advance(iter, offset);
2794 } else {
2795 unsigned long seg_skip;
2796
2797 /* skip first vec */
2798 offset -= bvec->bv_len;
2799 seg_skip = 1 + (offset >> PAGE_SHIFT);
2800
2801 iter->bvec = bvec + seg_skip;
2802 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002803 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002804 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002805 }
2806 }
2807
Jens Axboe5e559562019-11-13 16:12:46 -07002808 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002809}
2810
Jens Axboebcda7ba2020-02-23 16:42:51 -07002811static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2812{
2813 if (needs_lock)
2814 mutex_unlock(&ctx->uring_lock);
2815}
2816
2817static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2818{
2819 /*
2820 * "Normal" inline submissions always hold the uring_lock, since we
2821 * grab it from the system call. Same is true for the SQPOLL offload.
2822 * The only exception is when we've detached the request and issue it
2823 * from an async worker thread, grab the lock for that case.
2824 */
2825 if (needs_lock)
2826 mutex_lock(&ctx->uring_lock);
2827}
2828
2829static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2830 int bgid, struct io_buffer *kbuf,
2831 bool needs_lock)
2832{
2833 struct io_buffer *head;
2834
2835 if (req->flags & REQ_F_BUFFER_SELECTED)
2836 return kbuf;
2837
2838 io_ring_submit_lock(req->ctx, needs_lock);
2839
2840 lockdep_assert_held(&req->ctx->uring_lock);
2841
2842 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2843 if (head) {
2844 if (!list_empty(&head->list)) {
2845 kbuf = list_last_entry(&head->list, struct io_buffer,
2846 list);
2847 list_del(&kbuf->list);
2848 } else {
2849 kbuf = head;
2850 idr_remove(&req->ctx->io_buffer_idr, bgid);
2851 }
2852 if (*len > kbuf->len)
2853 *len = kbuf->len;
2854 } else {
2855 kbuf = ERR_PTR(-ENOBUFS);
2856 }
2857
2858 io_ring_submit_unlock(req->ctx, needs_lock);
2859
2860 return kbuf;
2861}
2862
Jens Axboe4d954c22020-02-27 07:31:19 -07002863static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2864 bool needs_lock)
2865{
2866 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002867 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002868
2869 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002870 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002871 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2872 if (IS_ERR(kbuf))
2873 return kbuf;
2874 req->rw.addr = (u64) (unsigned long) kbuf;
2875 req->flags |= REQ_F_BUFFER_SELECTED;
2876 return u64_to_user_ptr(kbuf->addr);
2877}
2878
2879#ifdef CONFIG_COMPAT
2880static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2881 bool needs_lock)
2882{
2883 struct compat_iovec __user *uiov;
2884 compat_ssize_t clen;
2885 void __user *buf;
2886 ssize_t len;
2887
2888 uiov = u64_to_user_ptr(req->rw.addr);
2889 if (!access_ok(uiov, sizeof(*uiov)))
2890 return -EFAULT;
2891 if (__get_user(clen, &uiov->iov_len))
2892 return -EFAULT;
2893 if (clen < 0)
2894 return -EINVAL;
2895
2896 len = clen;
2897 buf = io_rw_buffer_select(req, &len, needs_lock);
2898 if (IS_ERR(buf))
2899 return PTR_ERR(buf);
2900 iov[0].iov_base = buf;
2901 iov[0].iov_len = (compat_size_t) len;
2902 return 0;
2903}
2904#endif
2905
2906static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2907 bool needs_lock)
2908{
2909 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2910 void __user *buf;
2911 ssize_t len;
2912
2913 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2914 return -EFAULT;
2915
2916 len = iov[0].iov_len;
2917 if (len < 0)
2918 return -EINVAL;
2919 buf = io_rw_buffer_select(req, &len, needs_lock);
2920 if (IS_ERR(buf))
2921 return PTR_ERR(buf);
2922 iov[0].iov_base = buf;
2923 iov[0].iov_len = len;
2924 return 0;
2925}
2926
2927static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2928 bool needs_lock)
2929{
Jens Axboedddb3e22020-06-04 11:27:01 -06002930 if (req->flags & REQ_F_BUFFER_SELECTED) {
2931 struct io_buffer *kbuf;
2932
2933 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2934 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2935 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002936 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002937 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002938 if (!req->rw.len)
2939 return 0;
2940 else if (req->rw.len > 1)
2941 return -EINVAL;
2942
2943#ifdef CONFIG_COMPAT
2944 if (req->ctx->compat)
2945 return io_compat_import(req, iov, needs_lock);
2946#endif
2947
2948 return __io_iov_buffer_select(req, iov, needs_lock);
2949}
2950
Jens Axboe8452fd02020-08-18 13:58:33 -07002951static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2952 struct iovec **iovec, struct iov_iter *iter,
2953 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002954{
Jens Axboe9adbd452019-12-20 08:45:55 -07002955 void __user *buf = u64_to_user_ptr(req->rw.addr);
2956 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002957 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002958 u8 opcode;
2959
Jens Axboed625c6e2019-12-17 19:53:05 -07002960 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002961 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002962 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002963 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002964 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002965
Jens Axboebcda7ba2020-02-23 16:42:51 -07002966 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002967 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002968 return -EINVAL;
2969
Jens Axboe3a6820f2019-12-22 15:19:35 -07002970 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002971 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002972 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002973 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002974 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002975 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002976 }
2977
Jens Axboe3a6820f2019-12-22 15:19:35 -07002978 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2979 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002980 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002981 }
2982
Jens Axboe4d954c22020-02-27 07:31:19 -07002983 if (req->flags & REQ_F_BUFFER_SELECT) {
2984 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002985 if (!ret) {
2986 ret = (*iovec)->iov_len;
2987 iov_iter_init(iter, rw, *iovec, 1, ret);
2988 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002989 *iovec = NULL;
2990 return ret;
2991 }
2992
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002994 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002995 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2996 iovec, iter);
2997#endif
2998
2999 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
3000}
3001
Jens Axboe8452fd02020-08-18 13:58:33 -07003002static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
3003 struct iovec **iovec, struct iov_iter *iter,
3004 bool needs_lock)
3005{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003006 struct io_async_rw *iorw = req->async_data;
3007
3008 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07003009 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3010 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003011 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07003012}
3013
Jens Axboe0fef9482020-08-26 10:36:20 -06003014static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3015{
3016 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
3017}
3018
Jens Axboe32960612019-09-23 11:05:34 -06003019/*
3020 * For files that don't have ->read_iter() and ->write_iter(), handle them
3021 * by looping over ->read() or ->write() manually.
3022 */
3023static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
3024 struct iov_iter *iter)
3025{
3026 ssize_t ret = 0;
3027
3028 /*
3029 * Don't support polled IO through this interface, and we can't
3030 * support non-blocking either. For the latter, this just causes
3031 * the kiocb to be handled from an async context.
3032 */
3033 if (kiocb->ki_flags & IOCB_HIPRI)
3034 return -EOPNOTSUPP;
3035 if (kiocb->ki_flags & IOCB_NOWAIT)
3036 return -EAGAIN;
3037
3038 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003039 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003040 ssize_t nr;
3041
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003042 if (!iov_iter_is_bvec(iter)) {
3043 iovec = iov_iter_iovec(iter);
3044 } else {
3045 /* fixed buffers import bvec */
3046 iovec.iov_base = kmap(iter->bvec->bv_page)
3047 + iter->iov_offset;
3048 iovec.iov_len = min(iter->count,
3049 iter->bvec->bv_len - iter->iov_offset);
3050 }
3051
Jens Axboe32960612019-09-23 11:05:34 -06003052 if (rw == READ) {
3053 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003054 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003055 } else {
3056 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003057 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003058 }
3059
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003060 if (iov_iter_is_bvec(iter))
3061 kunmap(iter->bvec->bv_page);
3062
Jens Axboe32960612019-09-23 11:05:34 -06003063 if (nr < 0) {
3064 if (!ret)
3065 ret = nr;
3066 break;
3067 }
3068 ret += nr;
3069 if (nr != iovec.iov_len)
3070 break;
3071 iov_iter_advance(iter, nr);
3072 }
3073
3074 return ret;
3075}
3076
Jens Axboeff6165b2020-08-13 09:47:43 -06003077static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3078 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003079{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003080 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003081
Jens Axboeff6165b2020-08-13 09:47:43 -06003082 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003083 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003084 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003085 /* can only be fixed buffers, no need to do anything */
3086 if (iter->type == ITER_BVEC)
3087 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003088 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003089 unsigned iov_off = 0;
3090
3091 rw->iter.iov = rw->fast_iov;
3092 if (iter->iov != fast_iov) {
3093 iov_off = iter->iov - fast_iov;
3094 rw->iter.iov += iov_off;
3095 }
3096 if (rw->fast_iov != fast_iov)
3097 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003098 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003099 } else {
3100 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003101 }
3102}
3103
Jens Axboee8c2bc12020-08-15 18:44:09 -07003104static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003105{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003106 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3107 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3108 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003109}
3110
Jens Axboee8c2bc12020-08-15 18:44:09 -07003111static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003112{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003113 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003114 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003115
Jens Axboee8c2bc12020-08-15 18:44:09 -07003116 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003117}
3118
Jens Axboeff6165b2020-08-13 09:47:43 -06003119static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3120 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003121 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003122{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003123 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003124 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003125 if (!req->async_data) {
3126 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003127 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003128
Jens Axboeff6165b2020-08-13 09:47:43 -06003129 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003130 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003131 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003132}
3133
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003134static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3135 bool force_nonblock)
3136{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003137 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003138 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003139 ssize_t ret;
3140
Jens Axboec183edf2020-09-04 22:36:52 -06003141 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003142 if (unlikely(ret < 0))
3143 return ret;
3144
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003145 iorw->bytes_done = 0;
3146 iorw->free_iovec = iov;
3147 if (iov)
3148 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003149 return 0;
3150}
3151
Jens Axboe3529d8c2019-12-19 18:24:38 -07003152static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3153 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003154{
3155 ssize_t ret;
3156
Jens Axboe3529d8c2019-12-19 18:24:38 -07003157 ret = io_prep_rw(req, sqe, force_nonblock);
3158 if (ret)
3159 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003160
Jens Axboe3529d8c2019-12-19 18:24:38 -07003161 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3162 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003163
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003164 /* either don't need iovec imported or already have it */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003165 if (!req->async_data || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003166 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003167 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003168}
3169
Jens Axboec1dd91d2020-08-03 16:43:59 -06003170/*
3171 * This is our waitqueue callback handler, registered through lock_page_async()
3172 * when we initially tried to do the IO with the iocb armed our waitqueue.
3173 * This gets called when the page is unlocked, and we generally expect that to
3174 * happen when the page IO is completed and the page is now uptodate. This will
3175 * queue a task_work based retry of the operation, attempting to copy the data
3176 * again. If the latter fails because the page was NOT uptodate, then we will
3177 * do a thread based blocking retry of the operation. That's the unexpected
3178 * slow path.
3179 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003180static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3181 int sync, void *arg)
3182{
3183 struct wait_page_queue *wpq;
3184 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003185 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003186 int ret;
3187
3188 wpq = container_of(wait, struct wait_page_queue, wait);
3189
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003190 if (!wake_page_match(wpq, key))
3191 return 0;
3192
Hao Xuc8d317a2020-09-29 20:00:45 +08003193 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003194 list_del_init(&wait->entry);
3195
Pavel Begunkove7375122020-07-12 20:42:04 +03003196 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003197 percpu_ref_get(&req->ctx->refs);
3198
Jens Axboebcf5a062020-05-22 09:24:42 -06003199 /* submit ref gets dropped, acquire a new one */
3200 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003201 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003202 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003203 struct task_struct *tsk;
3204
Jens Axboebcf5a062020-05-22 09:24:42 -06003205 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003206 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003207 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003208 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003209 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003210 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003211 return 1;
3212}
3213
Jens Axboec1dd91d2020-08-03 16:43:59 -06003214/*
3215 * This controls whether a given IO request should be armed for async page
3216 * based retry. If we return false here, the request is handed to the async
3217 * worker threads for retry. If we're doing buffered reads on a regular file,
3218 * we prepare a private wait_page_queue entry and retry the operation. This
3219 * will either succeed because the page is now uptodate and unlocked, or it
3220 * will register a callback when the page is unlocked at IO completion. Through
3221 * that callback, io_uring uses task_work to setup a retry of the operation.
3222 * That retry will attempt the buffered read again. The retry will generally
3223 * succeed, or in rare cases where it fails, we then fall back to using the
3224 * async worker threads for a blocking retry.
3225 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003226static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003227{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003228 struct io_async_rw *rw = req->async_data;
3229 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003230 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003231
3232 /* never retry for NOWAIT, we just complete with -EAGAIN */
3233 if (req->flags & REQ_F_NOWAIT)
3234 return false;
3235
Jens Axboe227c0c92020-08-13 11:51:40 -06003236 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003237 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003238 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003239
Jens Axboebcf5a062020-05-22 09:24:42 -06003240 /*
3241 * just use poll if we can, and don't attempt if the fs doesn't
3242 * support callback based unlocks
3243 */
3244 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3245 return false;
3246
Jens Axboe3b2a4432020-08-16 10:58:43 -07003247 wait->wait.func = io_async_buf_func;
3248 wait->wait.private = req;
3249 wait->wait.flags = 0;
3250 INIT_LIST_HEAD(&wait->wait.entry);
3251 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003252 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003253 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003254 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003255}
3256
3257static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3258{
3259 if (req->file->f_op->read_iter)
3260 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003261 else if (req->file->f_op->read)
3262 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3263 else
3264 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003265}
3266
Jens Axboea1d7c392020-06-22 11:09:46 -06003267static int io_read(struct io_kiocb *req, bool force_nonblock,
3268 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003269{
3270 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003271 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003272 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003273 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003274 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003275 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003276 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003277
Jens Axboee8c2bc12020-08-15 18:44:09 -07003278 if (rw)
3279 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003280
3281 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003282 if (ret < 0)
3283 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003284 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003285 io_size = ret;
3286 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003287 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003288
Jens Axboefd6c2e42019-12-18 12:19:41 -07003289 /* Ensure we clear previously set non-block flag */
3290 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003291 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003292
Pavel Begunkov24c74672020-06-21 13:09:51 +03003293 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003294 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3295 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003296 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003297
Jens Axboe0fef9482020-08-26 10:36:20 -06003298 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003299 if (unlikely(ret))
3300 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003301
Jens Axboe227c0c92020-08-13 11:51:40 -06003302 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003303
Jens Axboe227c0c92020-08-13 11:51:40 -06003304 if (!ret) {
3305 goto done;
3306 } else if (ret == -EIOCBQUEUED) {
3307 ret = 0;
3308 goto out_free;
3309 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003310 /* IOPOLL retry should happen for io-wq threads */
3311 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003312 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003313 /* no retry on NONBLOCK marked file */
3314 if (req->file->f_flags & O_NONBLOCK)
3315 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003316 /* some cases will consume bytes even on error returns */
3317 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003318 ret = 0;
3319 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003320 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003321 /* make sure -ERESTARTSYS -> -EINTR is done */
3322 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003323 }
3324
3325 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003326 if (!iov_iter_count(iter) || !force_nonblock ||
3327 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003328 goto done;
3329
3330 io_size -= ret;
3331copy_iov:
3332 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3333 if (ret2) {
3334 ret = ret2;
3335 goto out_free;
3336 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003337 if (no_async)
3338 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003339 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003340 /* it's copied and will be cleaned with ->io */
3341 iovec = NULL;
3342 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003343 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003344retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003345 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003346 /* if we can retry, do so with the callbacks armed */
3347 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003348 kiocb->ki_flags &= ~IOCB_WAITQ;
3349 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003350 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003351
3352 /*
3353 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3354 * get -EIOCBQUEUED, then we'll get a notification when the desired
3355 * page gets unlocked. We can also get a partial read here, and if we
3356 * do, then just retry at the new offset.
3357 */
3358 ret = io_iter_do_read(req, iter);
3359 if (ret == -EIOCBQUEUED) {
3360 ret = 0;
3361 goto out_free;
3362 } else if (ret > 0 && ret < io_size) {
3363 /* we got some bytes, but not all. retry. */
3364 goto retry;
3365 }
3366done:
3367 kiocb_done(kiocb, ret, cs);
3368 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003369out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003370 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003371 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003372 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003373 return ret;
3374}
3375
Jens Axboe3529d8c2019-12-19 18:24:38 -07003376static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3377 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003378{
3379 ssize_t ret;
3380
Jens Axboe3529d8c2019-12-19 18:24:38 -07003381 ret = io_prep_rw(req, sqe, force_nonblock);
3382 if (ret)
3383 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003384
Jens Axboe3529d8c2019-12-19 18:24:38 -07003385 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3386 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003387
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003388 /* either don't need iovec imported or already have it */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003389 if (!req->async_data || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003390 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003391 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003392}
3393
Jens Axboea1d7c392020-06-22 11:09:46 -06003394static int io_write(struct io_kiocb *req, bool force_nonblock,
3395 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003396{
3397 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003398 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003399 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003400 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003401 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003402 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003403
Jens Axboee8c2bc12020-08-15 18:44:09 -07003404 if (rw)
3405 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003406
3407 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003408 if (ret < 0)
3409 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003410 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003411 io_size = ret;
3412 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003413
Jens Axboefd6c2e42019-12-18 12:19:41 -07003414 /* Ensure we clear previously set non-block flag */
3415 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003416 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003417
Pavel Begunkov24c74672020-06-21 13:09:51 +03003418 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003419 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003420 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003421
Jens Axboe10d59342019-12-09 20:16:22 -07003422 /* file path doesn't support NOWAIT for non-direct_IO */
3423 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3424 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003425 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003426
Jens Axboe0fef9482020-08-26 10:36:20 -06003427 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003428 if (unlikely(ret))
3429 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003430
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003431 /*
3432 * Open-code file_start_write here to grab freeze protection,
3433 * which will be released by another thread in
3434 * io_complete_rw(). Fool lockdep by telling it the lock got
3435 * released so that it doesn't complain about the held lock when
3436 * we return to userspace.
3437 */
3438 if (req->flags & REQ_F_ISREG) {
3439 __sb_start_write(file_inode(req->file)->i_sb,
3440 SB_FREEZE_WRITE, true);
3441 __sb_writers_release(file_inode(req->file)->i_sb,
3442 SB_FREEZE_WRITE);
3443 }
3444 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003445
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003446 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003447 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003448 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003449 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003450 else
3451 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003452
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003453 /*
3454 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3455 * retry them without IOCB_NOWAIT.
3456 */
3457 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3458 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003459 /* no retry on NONBLOCK marked file */
3460 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3461 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003462 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003463 /* IOPOLL retry should happen for io-wq threads */
3464 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3465 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003466done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003467 kiocb_done(kiocb, ret2, cs);
3468 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003469copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003470 /* some cases will consume bytes even on error returns */
3471 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003472 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003473 if (!ret)
3474 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003475 }
Jens Axboe31b51512019-01-18 22:56:34 -07003476out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003477 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003478 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003479 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003480 return ret;
3481}
3482
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003483static int __io_splice_prep(struct io_kiocb *req,
3484 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003485{
3486 struct io_splice* sp = &req->splice;
3487 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3488 int ret;
3489
3490 if (req->flags & REQ_F_NEED_CLEANUP)
3491 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003492 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3493 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003494
3495 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003496 sp->len = READ_ONCE(sqe->len);
3497 sp->flags = READ_ONCE(sqe->splice_flags);
3498
3499 if (unlikely(sp->flags & ~valid_flags))
3500 return -EINVAL;
3501
3502 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3503 (sp->flags & SPLICE_F_FD_IN_FIXED));
3504 if (ret)
3505 return ret;
3506 req->flags |= REQ_F_NEED_CLEANUP;
3507
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003508 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3509 /*
3510 * Splice operation will be punted aync, and here need to
3511 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3512 */
3513 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003514 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003515 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003516
3517 return 0;
3518}
3519
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003520static int io_tee_prep(struct io_kiocb *req,
3521 const struct io_uring_sqe *sqe)
3522{
3523 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3524 return -EINVAL;
3525 return __io_splice_prep(req, sqe);
3526}
3527
3528static int io_tee(struct io_kiocb *req, bool force_nonblock)
3529{
3530 struct io_splice *sp = &req->splice;
3531 struct file *in = sp->file_in;
3532 struct file *out = sp->file_out;
3533 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3534 long ret = 0;
3535
3536 if (force_nonblock)
3537 return -EAGAIN;
3538 if (sp->len)
3539 ret = do_tee(in, out, sp->len, flags);
3540
3541 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3542 req->flags &= ~REQ_F_NEED_CLEANUP;
3543
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003544 if (ret != sp->len)
3545 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003546 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003547 return 0;
3548}
3549
3550static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3551{
3552 struct io_splice* sp = &req->splice;
3553
3554 sp->off_in = READ_ONCE(sqe->splice_off_in);
3555 sp->off_out = READ_ONCE(sqe->off);
3556 return __io_splice_prep(req, sqe);
3557}
3558
Pavel Begunkov014db002020-03-03 21:33:12 +03003559static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003560{
3561 struct io_splice *sp = &req->splice;
3562 struct file *in = sp->file_in;
3563 struct file *out = sp->file_out;
3564 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3565 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003566 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003567
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003568 if (force_nonblock)
3569 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003570
3571 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3572 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003573
Jens Axboe948a7742020-05-17 14:21:38 -06003574 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003575 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003576
3577 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3578 req->flags &= ~REQ_F_NEED_CLEANUP;
3579
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003580 if (ret != sp->len)
3581 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003582 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003583 return 0;
3584}
3585
Jens Axboe2b188cc2019-01-07 10:46:33 -07003586/*
3587 * IORING_OP_NOP just posts a completion event, nothing else.
3588 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003589static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003590{
3591 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003592
Jens Axboedef596e2019-01-09 08:59:42 -07003593 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3594 return -EINVAL;
3595
Jens Axboe229a7b62020-06-22 10:13:11 -06003596 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003597 return 0;
3598}
3599
Jens Axboe3529d8c2019-12-19 18:24:38 -07003600static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003601{
Jens Axboe6b063142019-01-10 22:13:58 -07003602 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003603
Jens Axboe09bb8392019-03-13 12:39:28 -06003604 if (!req->file)
3605 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003606
Jens Axboe6b063142019-01-10 22:13:58 -07003607 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003608 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003609 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003610 return -EINVAL;
3611
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003612 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3613 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3614 return -EINVAL;
3615
3616 req->sync.off = READ_ONCE(sqe->off);
3617 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003618 return 0;
3619}
3620
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003621static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003622{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003623 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003624 int ret;
3625
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003626 /* fsync always requires a blocking context */
3627 if (force_nonblock)
3628 return -EAGAIN;
3629
Jens Axboe9adbd452019-12-20 08:45:55 -07003630 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003631 end > 0 ? end : LLONG_MAX,
3632 req->sync.flags & IORING_FSYNC_DATASYNC);
3633 if (ret < 0)
3634 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003635 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003636 return 0;
3637}
3638
Jens Axboed63d1b52019-12-10 10:38:56 -07003639static int io_fallocate_prep(struct io_kiocb *req,
3640 const struct io_uring_sqe *sqe)
3641{
3642 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3643 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003644 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3645 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003646
3647 req->sync.off = READ_ONCE(sqe->off);
3648 req->sync.len = READ_ONCE(sqe->addr);
3649 req->sync.mode = READ_ONCE(sqe->len);
3650 return 0;
3651}
3652
Pavel Begunkov014db002020-03-03 21:33:12 +03003653static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003654{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003655 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003656
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003657 /* fallocate always requiring blocking context */
3658 if (force_nonblock)
3659 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003660 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3661 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003662 if (ret < 0)
3663 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003664 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003665 return 0;
3666}
3667
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003668static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003669{
Jens Axboef8748882020-01-08 17:47:02 -07003670 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003671 int ret;
3672
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003673 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003674 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003675 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003676 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003677
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003678 /* open.how should be already initialised */
3679 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003680 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003681
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003682 req->open.dfd = READ_ONCE(sqe->fd);
3683 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003684 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003685 if (IS_ERR(req->open.filename)) {
3686 ret = PTR_ERR(req->open.filename);
3687 req->open.filename = NULL;
3688 return ret;
3689 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003690 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003691 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003692 return 0;
3693}
3694
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003695static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3696{
3697 u64 flags, mode;
3698
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003699 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3700 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003701 if (req->flags & REQ_F_NEED_CLEANUP)
3702 return 0;
3703 mode = READ_ONCE(sqe->len);
3704 flags = READ_ONCE(sqe->open_flags);
3705 req->open.how = build_open_how(flags, mode);
3706 return __io_openat_prep(req, sqe);
3707}
3708
Jens Axboecebdb982020-01-08 17:59:24 -07003709static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3710{
3711 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003712 size_t len;
3713 int ret;
3714
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003715 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3716 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003717 if (req->flags & REQ_F_NEED_CLEANUP)
3718 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003719 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3720 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003721 if (len < OPEN_HOW_SIZE_VER0)
3722 return -EINVAL;
3723
3724 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3725 len);
3726 if (ret)
3727 return ret;
3728
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003729 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003730}
3731
Pavel Begunkov014db002020-03-03 21:33:12 +03003732static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003733{
3734 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003735 struct file *file;
3736 int ret;
3737
Jens Axboef86cd202020-01-29 13:46:44 -07003738 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003739 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003740
Jens Axboecebdb982020-01-08 17:59:24 -07003741 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003742 if (ret)
3743 goto err;
3744
Jens Axboe4022e7a2020-03-19 19:23:18 -06003745 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003746 if (ret < 0)
3747 goto err;
3748
3749 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3750 if (IS_ERR(file)) {
3751 put_unused_fd(ret);
3752 ret = PTR_ERR(file);
3753 } else {
3754 fsnotify_open(file);
3755 fd_install(ret, file);
3756 }
3757err:
3758 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003759 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003760 if (ret < 0)
3761 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003762 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003763 return 0;
3764}
3765
Pavel Begunkov014db002020-03-03 21:33:12 +03003766static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003767{
Pavel Begunkov014db002020-03-03 21:33:12 +03003768 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003769}
3770
Jens Axboe067524e2020-03-02 16:32:28 -07003771static int io_remove_buffers_prep(struct io_kiocb *req,
3772 const struct io_uring_sqe *sqe)
3773{
3774 struct io_provide_buf *p = &req->pbuf;
3775 u64 tmp;
3776
3777 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3778 return -EINVAL;
3779
3780 tmp = READ_ONCE(sqe->fd);
3781 if (!tmp || tmp > USHRT_MAX)
3782 return -EINVAL;
3783
3784 memset(p, 0, sizeof(*p));
3785 p->nbufs = tmp;
3786 p->bgid = READ_ONCE(sqe->buf_group);
3787 return 0;
3788}
3789
3790static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3791 int bgid, unsigned nbufs)
3792{
3793 unsigned i = 0;
3794
3795 /* shouldn't happen */
3796 if (!nbufs)
3797 return 0;
3798
3799 /* the head kbuf is the list itself */
3800 while (!list_empty(&buf->list)) {
3801 struct io_buffer *nxt;
3802
3803 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3804 list_del(&nxt->list);
3805 kfree(nxt);
3806 if (++i == nbufs)
3807 return i;
3808 }
3809 i++;
3810 kfree(buf);
3811 idr_remove(&ctx->io_buffer_idr, bgid);
3812
3813 return i;
3814}
3815
Jens Axboe229a7b62020-06-22 10:13:11 -06003816static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3817 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003818{
3819 struct io_provide_buf *p = &req->pbuf;
3820 struct io_ring_ctx *ctx = req->ctx;
3821 struct io_buffer *head;
3822 int ret = 0;
3823
3824 io_ring_submit_lock(ctx, !force_nonblock);
3825
3826 lockdep_assert_held(&ctx->uring_lock);
3827
3828 ret = -ENOENT;
3829 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3830 if (head)
3831 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3832
3833 io_ring_submit_lock(ctx, !force_nonblock);
3834 if (ret < 0)
3835 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003836 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003837 return 0;
3838}
3839
Jens Axboeddf0322d2020-02-23 16:41:33 -07003840static int io_provide_buffers_prep(struct io_kiocb *req,
3841 const struct io_uring_sqe *sqe)
3842{
3843 struct io_provide_buf *p = &req->pbuf;
3844 u64 tmp;
3845
3846 if (sqe->ioprio || sqe->rw_flags)
3847 return -EINVAL;
3848
3849 tmp = READ_ONCE(sqe->fd);
3850 if (!tmp || tmp > USHRT_MAX)
3851 return -E2BIG;
3852 p->nbufs = tmp;
3853 p->addr = READ_ONCE(sqe->addr);
3854 p->len = READ_ONCE(sqe->len);
3855
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003856 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003857 return -EFAULT;
3858
3859 p->bgid = READ_ONCE(sqe->buf_group);
3860 tmp = READ_ONCE(sqe->off);
3861 if (tmp > USHRT_MAX)
3862 return -E2BIG;
3863 p->bid = tmp;
3864 return 0;
3865}
3866
3867static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3868{
3869 struct io_buffer *buf;
3870 u64 addr = pbuf->addr;
3871 int i, bid = pbuf->bid;
3872
3873 for (i = 0; i < pbuf->nbufs; i++) {
3874 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3875 if (!buf)
3876 break;
3877
3878 buf->addr = addr;
3879 buf->len = pbuf->len;
3880 buf->bid = bid;
3881 addr += pbuf->len;
3882 bid++;
3883 if (!*head) {
3884 INIT_LIST_HEAD(&buf->list);
3885 *head = buf;
3886 } else {
3887 list_add_tail(&buf->list, &(*head)->list);
3888 }
3889 }
3890
3891 return i ? i : -ENOMEM;
3892}
3893
Jens Axboe229a7b62020-06-22 10:13:11 -06003894static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3895 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003896{
3897 struct io_provide_buf *p = &req->pbuf;
3898 struct io_ring_ctx *ctx = req->ctx;
3899 struct io_buffer *head, *list;
3900 int ret = 0;
3901
3902 io_ring_submit_lock(ctx, !force_nonblock);
3903
3904 lockdep_assert_held(&ctx->uring_lock);
3905
3906 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3907
3908 ret = io_add_buffers(p, &head);
3909 if (ret < 0)
3910 goto out;
3911
3912 if (!list) {
3913 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3914 GFP_KERNEL);
3915 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003916 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003917 goto out;
3918 }
3919 }
3920out:
3921 io_ring_submit_unlock(ctx, !force_nonblock);
3922 if (ret < 0)
3923 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003924 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003925 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003926}
3927
Jens Axboe3e4827b2020-01-08 15:18:09 -07003928static int io_epoll_ctl_prep(struct io_kiocb *req,
3929 const struct io_uring_sqe *sqe)
3930{
3931#if defined(CONFIG_EPOLL)
3932 if (sqe->ioprio || sqe->buf_index)
3933 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003934 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003935 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003936
3937 req->epoll.epfd = READ_ONCE(sqe->fd);
3938 req->epoll.op = READ_ONCE(sqe->len);
3939 req->epoll.fd = READ_ONCE(sqe->off);
3940
3941 if (ep_op_has_event(req->epoll.op)) {
3942 struct epoll_event __user *ev;
3943
3944 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3945 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3946 return -EFAULT;
3947 }
3948
3949 return 0;
3950#else
3951 return -EOPNOTSUPP;
3952#endif
3953}
3954
Jens Axboe229a7b62020-06-22 10:13:11 -06003955static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3956 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003957{
3958#if defined(CONFIG_EPOLL)
3959 struct io_epoll *ie = &req->epoll;
3960 int ret;
3961
3962 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3963 if (force_nonblock && ret == -EAGAIN)
3964 return -EAGAIN;
3965
3966 if (ret < 0)
3967 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003968 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003969 return 0;
3970#else
3971 return -EOPNOTSUPP;
3972#endif
3973}
3974
Jens Axboec1ca7572019-12-25 22:18:28 -07003975static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3976{
3977#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3978 if (sqe->ioprio || sqe->buf_index || sqe->off)
3979 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003980 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3981 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003982
3983 req->madvise.addr = READ_ONCE(sqe->addr);
3984 req->madvise.len = READ_ONCE(sqe->len);
3985 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3986 return 0;
3987#else
3988 return -EOPNOTSUPP;
3989#endif
3990}
3991
Pavel Begunkov014db002020-03-03 21:33:12 +03003992static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003993{
3994#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3995 struct io_madvise *ma = &req->madvise;
3996 int ret;
3997
3998 if (force_nonblock)
3999 return -EAGAIN;
4000
4001 ret = do_madvise(ma->addr, ma->len, ma->advice);
4002 if (ret < 0)
4003 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004004 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004005 return 0;
4006#else
4007 return -EOPNOTSUPP;
4008#endif
4009}
4010
Jens Axboe4840e412019-12-25 22:03:45 -07004011static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4012{
4013 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4014 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004015 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4016 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004017
4018 req->fadvise.offset = READ_ONCE(sqe->off);
4019 req->fadvise.len = READ_ONCE(sqe->len);
4020 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4021 return 0;
4022}
4023
Pavel Begunkov014db002020-03-03 21:33:12 +03004024static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004025{
4026 struct io_fadvise *fa = &req->fadvise;
4027 int ret;
4028
Jens Axboe3e694262020-02-01 09:22:49 -07004029 if (force_nonblock) {
4030 switch (fa->advice) {
4031 case POSIX_FADV_NORMAL:
4032 case POSIX_FADV_RANDOM:
4033 case POSIX_FADV_SEQUENTIAL:
4034 break;
4035 default:
4036 return -EAGAIN;
4037 }
4038 }
Jens Axboe4840e412019-12-25 22:03:45 -07004039
4040 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4041 if (ret < 0)
4042 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004043 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004044 return 0;
4045}
4046
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004047static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4048{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004049 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004050 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004051 if (sqe->ioprio || sqe->buf_index)
4052 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004053 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004054 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004055
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004056 req->statx.dfd = READ_ONCE(sqe->fd);
4057 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004058 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004059 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4060 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004061
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004062 return 0;
4063}
4064
Pavel Begunkov014db002020-03-03 21:33:12 +03004065static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004066{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004067 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004068 int ret;
4069
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004070 if (force_nonblock) {
4071 /* only need file table for an actual valid fd */
4072 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4073 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004074 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004075 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004076
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004077 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4078 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004079
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004080 if (ret < 0)
4081 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004082 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004083 return 0;
4084}
4085
Jens Axboeb5dba592019-12-11 14:02:38 -07004086static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4087{
4088 /*
4089 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004090 * leave the 'file' in an undeterminate state, and here need to modify
4091 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004092 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004093 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004094 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4095
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004096 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4097 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004098 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4099 sqe->rw_flags || sqe->buf_index)
4100 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004101 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004102 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004103
4104 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004105 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004106 return -EBADF;
4107
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004108 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004109 return 0;
4110}
4111
Jens Axboe229a7b62020-06-22 10:13:11 -06004112static int io_close(struct io_kiocb *req, bool force_nonblock,
4113 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004114{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004115 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004116 int ret;
4117
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004118 /* might be already done during nonblock submission */
4119 if (!close->put_file) {
4120 ret = __close_fd_get_file(close->fd, &close->put_file);
4121 if (ret < 0)
4122 return (ret == -ENOENT) ? -EBADF : ret;
4123 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004124
4125 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004126 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004127 /* was never set, but play safe */
4128 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004129 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004130 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004131 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004132 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004133
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004134 /* No ->flush() or already async, safely close from here */
4135 ret = filp_close(close->put_file, req->work.files);
4136 if (ret < 0)
4137 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004138 fput(close->put_file);
4139 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004140 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004141 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004142}
4143
Jens Axboe3529d8c2019-12-19 18:24:38 -07004144static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004145{
4146 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004147
4148 if (!req->file)
4149 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004150
4151 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4152 return -EINVAL;
4153 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4154 return -EINVAL;
4155
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004156 req->sync.off = READ_ONCE(sqe->off);
4157 req->sync.len = READ_ONCE(sqe->len);
4158 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004159 return 0;
4160}
4161
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004162static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004163{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004164 int ret;
4165
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004166 /* sync_file_range always requires a blocking context */
4167 if (force_nonblock)
4168 return -EAGAIN;
4169
Jens Axboe9adbd452019-12-20 08:45:55 -07004170 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004171 req->sync.flags);
4172 if (ret < 0)
4173 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004174 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004175 return 0;
4176}
4177
YueHaibing469956e2020-03-04 15:53:52 +08004178#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004179static int io_setup_async_msg(struct io_kiocb *req,
4180 struct io_async_msghdr *kmsg)
4181{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004182 struct io_async_msghdr *async_msg = req->async_data;
4183
4184 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004185 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004186 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004187 if (kmsg->iov != kmsg->fast_iov)
4188 kfree(kmsg->iov);
4189 return -ENOMEM;
4190 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004191 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004192 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004193 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004194 return -EAGAIN;
4195}
4196
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004197static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4198 struct io_async_msghdr *iomsg)
4199{
4200 iomsg->iov = iomsg->fast_iov;
4201 iomsg->msg.msg_name = &iomsg->addr;
4202 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4203 req->sr_msg.msg_flags, &iomsg->iov);
4204}
4205
Jens Axboe3529d8c2019-12-19 18:24:38 -07004206static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004207{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004208 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004209 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004210 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004211
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004212 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4213 return -EINVAL;
4214
Jens Axboee47293f2019-12-20 08:58:21 -07004215 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004216 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004217 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004218
Jens Axboed8768362020-02-27 14:17:49 -07004219#ifdef CONFIG_COMPAT
4220 if (req->ctx->compat)
4221 sr->msg_flags |= MSG_CMSG_COMPAT;
4222#endif
4223
Jens Axboee8c2bc12020-08-15 18:44:09 -07004224 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004225 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004226 /* iovec is already imported */
4227 if (req->flags & REQ_F_NEED_CLEANUP)
4228 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004229
Jens Axboee8c2bc12020-08-15 18:44:09 -07004230 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004231 if (!ret)
4232 req->flags |= REQ_F_NEED_CLEANUP;
4233 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004234}
4235
Jens Axboe229a7b62020-06-22 10:13:11 -06004236static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4237 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004238{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004239 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004240 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004241 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004242 int ret;
4243
Jens Axboe03b12302019-12-02 18:50:25 -07004244 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004245 if (unlikely(!sock))
4246 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004247
Jens Axboee8c2bc12020-08-15 18:44:09 -07004248 if (req->async_data) {
4249 kmsg = req->async_data;
4250 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004251 /* if iov is set, it's allocated already */
4252 if (!kmsg->iov)
4253 kmsg->iov = kmsg->fast_iov;
4254 kmsg->msg.msg_iter.iov = kmsg->iov;
4255 } else {
4256 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004257 if (ret)
4258 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004259 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004260 }
4261
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004262 flags = req->sr_msg.msg_flags;
4263 if (flags & MSG_DONTWAIT)
4264 req->flags |= REQ_F_NOWAIT;
4265 else if (force_nonblock)
4266 flags |= MSG_DONTWAIT;
4267
4268 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4269 if (force_nonblock && ret == -EAGAIN)
4270 return io_setup_async_msg(req, kmsg);
4271 if (ret == -ERESTARTSYS)
4272 ret = -EINTR;
4273
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004274 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004275 kfree(kmsg->iov);
4276 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004277 if (ret < 0)
4278 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004279 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004280 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004281}
4282
Jens Axboe229a7b62020-06-22 10:13:11 -06004283static int io_send(struct io_kiocb *req, bool force_nonblock,
4284 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004285{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004286 struct io_sr_msg *sr = &req->sr_msg;
4287 struct msghdr msg;
4288 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004289 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004290 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004291 int ret;
4292
4293 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004294 if (unlikely(!sock))
4295 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004296
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004297 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4298 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004299 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004300
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004301 msg.msg_name = NULL;
4302 msg.msg_control = NULL;
4303 msg.msg_controllen = 0;
4304 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004305
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004306 flags = req->sr_msg.msg_flags;
4307 if (flags & MSG_DONTWAIT)
4308 req->flags |= REQ_F_NOWAIT;
4309 else if (force_nonblock)
4310 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004311
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004312 msg.msg_flags = flags;
4313 ret = sock_sendmsg(sock, &msg);
4314 if (force_nonblock && ret == -EAGAIN)
4315 return -EAGAIN;
4316 if (ret == -ERESTARTSYS)
4317 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004318
Jens Axboe03b12302019-12-02 18:50:25 -07004319 if (ret < 0)
4320 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004321 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004322 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004323}
4324
Pavel Begunkov1400e692020-07-12 20:41:05 +03004325static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4326 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004327{
4328 struct io_sr_msg *sr = &req->sr_msg;
4329 struct iovec __user *uiov;
4330 size_t iov_len;
4331 int ret;
4332
Pavel Begunkov1400e692020-07-12 20:41:05 +03004333 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4334 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004335 if (ret)
4336 return ret;
4337
4338 if (req->flags & REQ_F_BUFFER_SELECT) {
4339 if (iov_len > 1)
4340 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004341 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004342 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004343 sr->len = iomsg->iov[0].iov_len;
4344 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004345 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004346 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004347 } else {
4348 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004349 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004350 if (ret > 0)
4351 ret = 0;
4352 }
4353
4354 return ret;
4355}
4356
4357#ifdef CONFIG_COMPAT
4358static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004359 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004360{
4361 struct compat_msghdr __user *msg_compat;
4362 struct io_sr_msg *sr = &req->sr_msg;
4363 struct compat_iovec __user *uiov;
4364 compat_uptr_t ptr;
4365 compat_size_t len;
4366 int ret;
4367
Pavel Begunkov270a5942020-07-12 20:41:04 +03004368 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004369 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004370 &ptr, &len);
4371 if (ret)
4372 return ret;
4373
4374 uiov = compat_ptr(ptr);
4375 if (req->flags & REQ_F_BUFFER_SELECT) {
4376 compat_ssize_t clen;
4377
4378 if (len > 1)
4379 return -EINVAL;
4380 if (!access_ok(uiov, sizeof(*uiov)))
4381 return -EFAULT;
4382 if (__get_user(clen, &uiov->iov_len))
4383 return -EFAULT;
4384 if (clen < 0)
4385 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004386 sr->len = iomsg->iov[0].iov_len;
4387 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004388 } else {
4389 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004390 &iomsg->iov,
4391 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004392 if (ret < 0)
4393 return ret;
4394 }
4395
4396 return 0;
4397}
Jens Axboe03b12302019-12-02 18:50:25 -07004398#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004399
Pavel Begunkov1400e692020-07-12 20:41:05 +03004400static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4401 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004402{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004403 iomsg->msg.msg_name = &iomsg->addr;
4404 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004405
4406#ifdef CONFIG_COMPAT
4407 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004408 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004409#endif
4410
Pavel Begunkov1400e692020-07-12 20:41:05 +03004411 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004412}
4413
Jens Axboebcda7ba2020-02-23 16:42:51 -07004414static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004415 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004416{
4417 struct io_sr_msg *sr = &req->sr_msg;
4418 struct io_buffer *kbuf;
4419
Jens Axboebcda7ba2020-02-23 16:42:51 -07004420 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4421 if (IS_ERR(kbuf))
4422 return kbuf;
4423
4424 sr->kbuf = kbuf;
4425 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004426 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004427}
4428
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004429static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4430{
4431 return io_put_kbuf(req, req->sr_msg.kbuf);
4432}
4433
Jens Axboe3529d8c2019-12-19 18:24:38 -07004434static int io_recvmsg_prep(struct io_kiocb *req,
4435 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004436{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004437 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004438 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004439 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004440
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004441 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4442 return -EINVAL;
4443
Jens Axboe3529d8c2019-12-19 18:24:38 -07004444 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004445 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004446 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004447 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004448
Jens Axboed8768362020-02-27 14:17:49 -07004449#ifdef CONFIG_COMPAT
4450 if (req->ctx->compat)
4451 sr->msg_flags |= MSG_CMSG_COMPAT;
4452#endif
4453
Jens Axboee8c2bc12020-08-15 18:44:09 -07004454 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004455 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004456 /* iovec is already imported */
4457 if (req->flags & REQ_F_NEED_CLEANUP)
4458 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004459
Jens Axboee8c2bc12020-08-15 18:44:09 -07004460 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004461 if (!ret)
4462 req->flags |= REQ_F_NEED_CLEANUP;
4463 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004464}
4465
Jens Axboe229a7b62020-06-22 10:13:11 -06004466static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4467 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004468{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004469 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004470 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004471 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004472 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004473 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004474
Jens Axboe0fa03c62019-04-19 13:34:07 -06004475 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004476 if (unlikely(!sock))
4477 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004478
Jens Axboee8c2bc12020-08-15 18:44:09 -07004479 if (req->async_data) {
4480 kmsg = req->async_data;
4481 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004482 /* if iov is set, it's allocated already */
4483 if (!kmsg->iov)
4484 kmsg->iov = kmsg->fast_iov;
4485 kmsg->msg.msg_iter.iov = kmsg->iov;
4486 } else {
4487 ret = io_recvmsg_copy_hdr(req, &iomsg);
4488 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004489 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004490 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004491 }
4492
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004493 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004494 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004495 if (IS_ERR(kbuf))
4496 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004497 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4498 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4499 1, req->sr_msg.len);
4500 }
4501
4502 flags = req->sr_msg.msg_flags;
4503 if (flags & MSG_DONTWAIT)
4504 req->flags |= REQ_F_NOWAIT;
4505 else if (force_nonblock)
4506 flags |= MSG_DONTWAIT;
4507
4508 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4509 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004510 if (force_nonblock && ret == -EAGAIN)
4511 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004512 if (ret == -ERESTARTSYS)
4513 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004514
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004515 if (req->flags & REQ_F_BUFFER_SELECTED)
4516 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004517 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004518 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004519 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004520 if (ret < 0)
4521 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004522 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004523 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004524}
4525
Jens Axboe229a7b62020-06-22 10:13:11 -06004526static int io_recv(struct io_kiocb *req, bool force_nonblock,
4527 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004528{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004529 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004530 struct io_sr_msg *sr = &req->sr_msg;
4531 struct msghdr msg;
4532 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004533 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004534 struct iovec iov;
4535 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004536 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004537
Jens Axboefddafac2020-01-04 20:19:44 -07004538 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004539 if (unlikely(!sock))
4540 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004541
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004542 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004543 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004544 if (IS_ERR(kbuf))
4545 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004546 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004547 }
4548
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004549 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004550 if (unlikely(ret))
4551 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004552
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004553 msg.msg_name = NULL;
4554 msg.msg_control = NULL;
4555 msg.msg_controllen = 0;
4556 msg.msg_namelen = 0;
4557 msg.msg_iocb = NULL;
4558 msg.msg_flags = 0;
4559
4560 flags = req->sr_msg.msg_flags;
4561 if (flags & MSG_DONTWAIT)
4562 req->flags |= REQ_F_NOWAIT;
4563 else if (force_nonblock)
4564 flags |= MSG_DONTWAIT;
4565
4566 ret = sock_recvmsg(sock, &msg, flags);
4567 if (force_nonblock && ret == -EAGAIN)
4568 return -EAGAIN;
4569 if (ret == -ERESTARTSYS)
4570 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004571out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004572 if (req->flags & REQ_F_BUFFER_SELECTED)
4573 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004574 if (ret < 0)
4575 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004576 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004577 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004578}
4579
Jens Axboe3529d8c2019-12-19 18:24:38 -07004580static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004581{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004582 struct io_accept *accept = &req->accept;
4583
Jens Axboe17f2fe32019-10-17 14:42:58 -06004584 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4585 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004586 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004587 return -EINVAL;
4588
Jens Axboed55e5f52019-12-11 16:12:15 -07004589 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4590 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004591 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004592 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004593 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004594}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004595
Jens Axboe229a7b62020-06-22 10:13:11 -06004596static int io_accept(struct io_kiocb *req, bool force_nonblock,
4597 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004598{
4599 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004600 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004601 int ret;
4602
Jiufei Xuee697dee2020-06-10 13:41:59 +08004603 if (req->file->f_flags & O_NONBLOCK)
4604 req->flags |= REQ_F_NOWAIT;
4605
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004606 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004607 accept->addr_len, accept->flags,
4608 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004609 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004610 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004611 if (ret < 0) {
4612 if (ret == -ERESTARTSYS)
4613 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004614 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004615 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004616 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004617 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004618}
4619
Jens Axboe3529d8c2019-12-19 18:24:38 -07004620static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004621{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004622 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004623 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004624
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004625 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4626 return -EINVAL;
4627 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4628 return -EINVAL;
4629
Jens Axboe3529d8c2019-12-19 18:24:38 -07004630 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4631 conn->addr_len = READ_ONCE(sqe->addr2);
4632
4633 if (!io)
4634 return 0;
4635
4636 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004637 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004638}
4639
Jens Axboe229a7b62020-06-22 10:13:11 -06004640static int io_connect(struct io_kiocb *req, bool force_nonblock,
4641 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004642{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004643 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004644 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004645 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004646
Jens Axboee8c2bc12020-08-15 18:44:09 -07004647 if (req->async_data) {
4648 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004649 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004650 ret = move_addr_to_kernel(req->connect.addr,
4651 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004652 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004653 if (ret)
4654 goto out;
4655 io = &__io;
4656 }
4657
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004658 file_flags = force_nonblock ? O_NONBLOCK : 0;
4659
Jens Axboee8c2bc12020-08-15 18:44:09 -07004660 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004661 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004662 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004663 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004664 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004665 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004666 ret = -ENOMEM;
4667 goto out;
4668 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004669 io = req->async_data;
4670 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004671 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004672 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004673 if (ret == -ERESTARTSYS)
4674 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004675out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004676 if (ret < 0)
4677 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004678 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004679 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004680}
YueHaibing469956e2020-03-04 15:53:52 +08004681#else /* !CONFIG_NET */
4682static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4683{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004684 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004685}
4686
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004687static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4688 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004689{
YueHaibing469956e2020-03-04 15:53:52 +08004690 return -EOPNOTSUPP;
4691}
4692
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004693static int io_send(struct io_kiocb *req, bool force_nonblock,
4694 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004695{
4696 return -EOPNOTSUPP;
4697}
4698
4699static int io_recvmsg_prep(struct io_kiocb *req,
4700 const struct io_uring_sqe *sqe)
4701{
4702 return -EOPNOTSUPP;
4703}
4704
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004705static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4706 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004707{
4708 return -EOPNOTSUPP;
4709}
4710
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004711static int io_recv(struct io_kiocb *req, bool force_nonblock,
4712 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004713{
4714 return -EOPNOTSUPP;
4715}
4716
4717static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4718{
4719 return -EOPNOTSUPP;
4720}
4721
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004722static int io_accept(struct io_kiocb *req, bool force_nonblock,
4723 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004724{
4725 return -EOPNOTSUPP;
4726}
4727
4728static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4729{
4730 return -EOPNOTSUPP;
4731}
4732
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004733static int io_connect(struct io_kiocb *req, bool force_nonblock,
4734 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004735{
4736 return -EOPNOTSUPP;
4737}
4738#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004739
Jens Axboed7718a92020-02-14 22:23:12 -07004740struct io_poll_table {
4741 struct poll_table_struct pt;
4742 struct io_kiocb *req;
4743 int error;
4744};
4745
Jens Axboed7718a92020-02-14 22:23:12 -07004746static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4747 __poll_t mask, task_work_func_t func)
4748{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004749 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004750 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004751
4752 /* for instances that support it check for an event match first: */
4753 if (mask && !(mask & poll->events))
4754 return 0;
4755
4756 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4757
4758 list_del_init(&poll->wait.entry);
4759
Jens Axboed7718a92020-02-14 22:23:12 -07004760 req->result = mask;
4761 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004762 percpu_ref_get(&req->ctx->refs);
4763
Jens Axboed7718a92020-02-14 22:23:12 -07004764 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004765 * If we using the signalfd wait_queue_head for this wakeup, then
4766 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4767 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4768 * either, as the normal wakeup will suffice.
4769 */
4770 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4771
4772 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004773 * If this fails, then the task is exiting. When a task exits, the
4774 * work gets canceled, so just cancel this request as well instead
4775 * of executing it. We can't safely execute it anyway, as we may not
4776 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004777 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004778 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004779 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004780 struct task_struct *tsk;
4781
Jens Axboee3aabf92020-05-18 11:04:17 -06004782 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004783 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004784 task_work_add(tsk, &req->task_work, 0);
4785 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004786 }
Jens Axboed7718a92020-02-14 22:23:12 -07004787 return 1;
4788}
4789
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004790static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4791 __acquires(&req->ctx->completion_lock)
4792{
4793 struct io_ring_ctx *ctx = req->ctx;
4794
4795 if (!req->result && !READ_ONCE(poll->canceled)) {
4796 struct poll_table_struct pt = { ._key = poll->events };
4797
4798 req->result = vfs_poll(req->file, &pt) & poll->events;
4799 }
4800
4801 spin_lock_irq(&ctx->completion_lock);
4802 if (!req->result && !READ_ONCE(poll->canceled)) {
4803 add_wait_queue(poll->head, &poll->wait);
4804 return true;
4805 }
4806
4807 return false;
4808}
4809
Jens Axboed4e7cd32020-08-15 11:44:50 -07004810static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004811{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004812 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004813 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004814 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004815 return req->apoll->double_poll;
4816}
4817
4818static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4819{
4820 if (req->opcode == IORING_OP_POLL_ADD)
4821 return &req->poll;
4822 return &req->apoll->poll;
4823}
4824
4825static void io_poll_remove_double(struct io_kiocb *req)
4826{
4827 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004828
4829 lockdep_assert_held(&req->ctx->completion_lock);
4830
4831 if (poll && poll->head) {
4832 struct wait_queue_head *head = poll->head;
4833
4834 spin_lock(&head->lock);
4835 list_del_init(&poll->wait.entry);
4836 if (poll->wait.private)
4837 refcount_dec(&req->refs);
4838 poll->head = NULL;
4839 spin_unlock(&head->lock);
4840 }
4841}
4842
4843static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4844{
4845 struct io_ring_ctx *ctx = req->ctx;
4846
Jens Axboed4e7cd32020-08-15 11:44:50 -07004847 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004848 req->poll.done = true;
4849 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4850 io_commit_cqring(ctx);
4851}
4852
4853static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4854{
4855 struct io_ring_ctx *ctx = req->ctx;
4856
4857 if (io_poll_rewait(req, &req->poll)) {
4858 spin_unlock_irq(&ctx->completion_lock);
4859 return;
4860 }
4861
4862 hash_del(&req->hash_node);
4863 io_poll_complete(req, req->result, 0);
4864 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004865 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004866 spin_unlock_irq(&ctx->completion_lock);
4867
4868 io_cqring_ev_posted(ctx);
4869}
4870
4871static void io_poll_task_func(struct callback_head *cb)
4872{
4873 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004874 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004875 struct io_kiocb *nxt = NULL;
4876
4877 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004878 if (nxt)
4879 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004880 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004881}
4882
4883static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4884 int sync, void *key)
4885{
4886 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004887 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004888 __poll_t mask = key_to_poll(key);
4889
4890 /* for instances that support it check for an event match first: */
4891 if (mask && !(mask & poll->events))
4892 return 0;
4893
Jens Axboe8706e042020-09-28 08:38:54 -06004894 list_del_init(&wait->entry);
4895
Jens Axboe807abcb2020-07-17 17:09:27 -06004896 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004897 bool done;
4898
Jens Axboe807abcb2020-07-17 17:09:27 -06004899 spin_lock(&poll->head->lock);
4900 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004901 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004902 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004903 /* make sure double remove sees this as being gone */
4904 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004905 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004906 if (!done)
4907 __io_async_wake(req, poll, mask, io_poll_task_func);
4908 }
4909 refcount_dec(&req->refs);
4910 return 1;
4911}
4912
4913static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4914 wait_queue_func_t wake_func)
4915{
4916 poll->head = NULL;
4917 poll->done = false;
4918 poll->canceled = false;
4919 poll->events = events;
4920 INIT_LIST_HEAD(&poll->wait.entry);
4921 init_waitqueue_func_entry(&poll->wait, wake_func);
4922}
4923
4924static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004925 struct wait_queue_head *head,
4926 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004927{
4928 struct io_kiocb *req = pt->req;
4929
4930 /*
4931 * If poll->head is already set, it's because the file being polled
4932 * uses multiple waitqueues for poll handling (eg one for read, one
4933 * for write). Setup a separate io_poll_iocb if this happens.
4934 */
4935 if (unlikely(poll->head)) {
4936 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004937 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004938 pt->error = -EINVAL;
4939 return;
4940 }
4941 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4942 if (!poll) {
4943 pt->error = -ENOMEM;
4944 return;
4945 }
4946 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4947 refcount_inc(&req->refs);
4948 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004949 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004950 }
4951
4952 pt->error = 0;
4953 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004954
4955 if (poll->events & EPOLLEXCLUSIVE)
4956 add_wait_queue_exclusive(head, &poll->wait);
4957 else
4958 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004959}
4960
4961static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4962 struct poll_table_struct *p)
4963{
4964 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004965 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004966
Jens Axboe807abcb2020-07-17 17:09:27 -06004967 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004968}
4969
Jens Axboed7718a92020-02-14 22:23:12 -07004970static void io_async_task_func(struct callback_head *cb)
4971{
4972 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4973 struct async_poll *apoll = req->apoll;
4974 struct io_ring_ctx *ctx = req->ctx;
4975
4976 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4977
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004978 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004979 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004980 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004981 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004982 }
4983
Jens Axboe31067252020-05-17 17:43:31 -06004984 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004985 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004986 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004987
Jens Axboed4e7cd32020-08-15 11:44:50 -07004988 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004989 spin_unlock_irq(&ctx->completion_lock);
4990
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004991 if (!READ_ONCE(apoll->poll.canceled))
4992 __io_req_task_submit(req);
4993 else
4994 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004995
Jens Axboe6d816e02020-08-11 08:04:14 -06004996 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004997 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004998 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004999}
5000
5001static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5002 void *key)
5003{
5004 struct io_kiocb *req = wait->private;
5005 struct io_poll_iocb *poll = &req->apoll->poll;
5006
5007 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5008 key_to_poll(key));
5009
5010 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5011}
5012
5013static void io_poll_req_insert(struct io_kiocb *req)
5014{
5015 struct io_ring_ctx *ctx = req->ctx;
5016 struct hlist_head *list;
5017
5018 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5019 hlist_add_head(&req->hash_node, list);
5020}
5021
5022static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5023 struct io_poll_iocb *poll,
5024 struct io_poll_table *ipt, __poll_t mask,
5025 wait_queue_func_t wake_func)
5026 __acquires(&ctx->completion_lock)
5027{
5028 struct io_ring_ctx *ctx = req->ctx;
5029 bool cancel = false;
5030
Jens Axboe18bceab2020-05-15 11:56:54 -06005031 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005032 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005033 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005034
5035 ipt->pt._key = mask;
5036 ipt->req = req;
5037 ipt->error = -EINVAL;
5038
Jens Axboed7718a92020-02-14 22:23:12 -07005039 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5040
5041 spin_lock_irq(&ctx->completion_lock);
5042 if (likely(poll->head)) {
5043 spin_lock(&poll->head->lock);
5044 if (unlikely(list_empty(&poll->wait.entry))) {
5045 if (ipt->error)
5046 cancel = true;
5047 ipt->error = 0;
5048 mask = 0;
5049 }
5050 if (mask || ipt->error)
5051 list_del_init(&poll->wait.entry);
5052 else if (cancel)
5053 WRITE_ONCE(poll->canceled, true);
5054 else if (!poll->done) /* actually waiting for an event */
5055 io_poll_req_insert(req);
5056 spin_unlock(&poll->head->lock);
5057 }
5058
5059 return mask;
5060}
5061
5062static bool io_arm_poll_handler(struct io_kiocb *req)
5063{
5064 const struct io_op_def *def = &io_op_defs[req->opcode];
5065 struct io_ring_ctx *ctx = req->ctx;
5066 struct async_poll *apoll;
5067 struct io_poll_table ipt;
5068 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005069 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005070
5071 if (!req->file || !file_can_poll(req->file))
5072 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005073 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005074 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005075 if (def->pollin)
5076 rw = READ;
5077 else if (def->pollout)
5078 rw = WRITE;
5079 else
5080 return false;
5081 /* if we can't nonblock try, then no point in arming a poll handler */
5082 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005083 return false;
5084
5085 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5086 if (unlikely(!apoll))
5087 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005088 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005089
5090 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005091 req->apoll = apoll;
5092 INIT_HLIST_NODE(&req->hash_node);
5093
Nathan Chancellor8755d972020-03-02 16:01:19 -07005094 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005095 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005096 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005097 if (def->pollout)
5098 mask |= POLLOUT | POLLWRNORM;
5099 mask |= POLLERR | POLLPRI;
5100
5101 ipt.pt._qproc = io_async_queue_proc;
5102
5103 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5104 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005105 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005106 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005107 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005108 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005109 kfree(apoll);
5110 return false;
5111 }
5112 spin_unlock_irq(&ctx->completion_lock);
5113 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5114 apoll->poll.events);
5115 return true;
5116}
5117
5118static bool __io_poll_remove_one(struct io_kiocb *req,
5119 struct io_poll_iocb *poll)
5120{
Jens Axboeb41e9852020-02-17 09:52:41 -07005121 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005122
5123 spin_lock(&poll->head->lock);
5124 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005125 if (!list_empty(&poll->wait.entry)) {
5126 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005127 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005128 }
5129 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005130 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005131 return do_complete;
5132}
5133
5134static bool io_poll_remove_one(struct io_kiocb *req)
5135{
5136 bool do_complete;
5137
Jens Axboed4e7cd32020-08-15 11:44:50 -07005138 io_poll_remove_double(req);
5139
Jens Axboed7718a92020-02-14 22:23:12 -07005140 if (req->opcode == IORING_OP_POLL_ADD) {
5141 do_complete = __io_poll_remove_one(req, &req->poll);
5142 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005143 struct async_poll *apoll = req->apoll;
5144
Jens Axboed7718a92020-02-14 22:23:12 -07005145 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005146 do_complete = __io_poll_remove_one(req, &apoll->poll);
5147 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005148 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005149 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005150 kfree(apoll);
5151 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005152 }
5153
Jens Axboeb41e9852020-02-17 09:52:41 -07005154 if (do_complete) {
5155 io_cqring_fill_event(req, -ECANCELED);
5156 io_commit_cqring(req->ctx);
5157 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005158 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005159 io_put_req(req);
5160 }
5161
5162 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005163}
5164
Jens Axboe76e1b642020-09-26 15:05:03 -06005165/*
5166 * Returns true if we found and killed one or more poll requests
5167 */
5168static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005169{
Jens Axboe78076bb2019-12-04 19:56:40 -07005170 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005171 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005172 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005173
5174 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005175 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5176 struct hlist_head *list;
5177
5178 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005179 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5180 if (io_task_match(req, tsk))
5181 posted += io_poll_remove_one(req);
5182 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005183 }
5184 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005185
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005186 if (posted)
5187 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005188
5189 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005190}
5191
Jens Axboe47f46762019-11-09 17:43:02 -07005192static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5193{
Jens Axboe78076bb2019-12-04 19:56:40 -07005194 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005195 struct io_kiocb *req;
5196
Jens Axboe78076bb2019-12-04 19:56:40 -07005197 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5198 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005199 if (sqe_addr != req->user_data)
5200 continue;
5201 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005202 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005203 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005204 }
5205
5206 return -ENOENT;
5207}
5208
Jens Axboe3529d8c2019-12-19 18:24:38 -07005209static int io_poll_remove_prep(struct io_kiocb *req,
5210 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005211{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005212 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5213 return -EINVAL;
5214 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5215 sqe->poll_events)
5216 return -EINVAL;
5217
Jens Axboe0969e782019-12-17 18:40:57 -07005218 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005219 return 0;
5220}
5221
5222/*
5223 * Find a running poll command that matches one specified in sqe->addr,
5224 * and remove it if found.
5225 */
5226static int io_poll_remove(struct io_kiocb *req)
5227{
5228 struct io_ring_ctx *ctx = req->ctx;
5229 u64 addr;
5230 int ret;
5231
Jens Axboe0969e782019-12-17 18:40:57 -07005232 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005233 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005234 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005235 spin_unlock_irq(&ctx->completion_lock);
5236
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005237 if (ret < 0)
5238 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005239 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005240 return 0;
5241}
5242
Jens Axboe221c5eb2019-01-17 09:41:58 -07005243static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5244 void *key)
5245{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005246 struct io_kiocb *req = wait->private;
5247 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005248
Jens Axboed7718a92020-02-14 22:23:12 -07005249 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005250}
5251
Jens Axboe221c5eb2019-01-17 09:41:58 -07005252static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5253 struct poll_table_struct *p)
5254{
5255 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5256
Jens Axboee8c2bc12020-08-15 18:44:09 -07005257 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005258}
5259
Jens Axboe3529d8c2019-12-19 18:24:38 -07005260static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005261{
5262 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005263 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005264
5265 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5266 return -EINVAL;
5267 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5268 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005269 if (!poll->file)
5270 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005271
Jiufei Xue5769a352020-06-17 17:53:55 +08005272 events = READ_ONCE(sqe->poll32_events);
5273#ifdef __BIG_ENDIAN
5274 events = swahw32(events);
5275#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005276 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5277 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005278 return 0;
5279}
5280
Pavel Begunkov014db002020-03-03 21:33:12 +03005281static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005282{
5283 struct io_poll_iocb *poll = &req->poll;
5284 struct io_ring_ctx *ctx = req->ctx;
5285 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005286 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005287
Jens Axboe78076bb2019-12-04 19:56:40 -07005288 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005289 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005290
Jens Axboed7718a92020-02-14 22:23:12 -07005291 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5292 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005293
Jens Axboe8c838782019-03-12 15:48:16 -06005294 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005295 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005296 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005297 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005298 spin_unlock_irq(&ctx->completion_lock);
5299
Jens Axboe8c838782019-03-12 15:48:16 -06005300 if (mask) {
5301 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005302 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005303 }
Jens Axboe8c838782019-03-12 15:48:16 -06005304 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005305}
5306
Jens Axboe5262f562019-09-17 12:26:57 -06005307static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5308{
Jens Axboead8a48a2019-11-15 08:49:11 -07005309 struct io_timeout_data *data = container_of(timer,
5310 struct io_timeout_data, timer);
5311 struct io_kiocb *req = data->req;
5312 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005313 unsigned long flags;
5314
Jens Axboe5262f562019-09-17 12:26:57 -06005315 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005316 atomic_set(&req->ctx->cq_timeouts,
5317 atomic_read(&req->ctx->cq_timeouts) + 1);
5318
zhangyi (F)ef036812019-10-23 15:10:08 +08005319 /*
Jens Axboe11365042019-10-16 09:08:32 -06005320 * We could be racing with timeout deletion. If the list is empty,
5321 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005322 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005323 if (!list_empty(&req->timeout.list))
5324 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005325
Jens Axboe78e19bb2019-11-06 15:21:34 -07005326 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005327 io_commit_cqring(ctx);
5328 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5329
5330 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005331 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005332 io_put_req(req);
5333 return HRTIMER_NORESTART;
5334}
5335
Jens Axboef254ac02020-08-12 17:33:30 -06005336static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005337{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005338 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005339 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005340
Jens Axboef254ac02020-08-12 17:33:30 -06005341 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005342
Jens Axboee8c2bc12020-08-15 18:44:09 -07005343 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005344 if (ret == -1)
5345 return -EALREADY;
5346
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005347 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005348 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005349 io_cqring_fill_event(req, -ECANCELED);
5350 io_put_req(req);
5351 return 0;
5352}
5353
Jens Axboef254ac02020-08-12 17:33:30 -06005354static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5355{
5356 struct io_kiocb *req;
5357 int ret = -ENOENT;
5358
5359 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5360 if (user_data == req->user_data) {
5361 ret = 0;
5362 break;
5363 }
5364 }
5365
5366 if (ret == -ENOENT)
5367 return ret;
5368
5369 return __io_timeout_cancel(req);
5370}
5371
Jens Axboe3529d8c2019-12-19 18:24:38 -07005372static int io_timeout_remove_prep(struct io_kiocb *req,
5373 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005374{
Jens Axboeb29472e2019-12-17 18:50:29 -07005375 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5376 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005377 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5378 return -EINVAL;
5379 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005380 return -EINVAL;
5381
5382 req->timeout.addr = READ_ONCE(sqe->addr);
5383 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5384 if (req->timeout.flags)
5385 return -EINVAL;
5386
Jens Axboeb29472e2019-12-17 18:50:29 -07005387 return 0;
5388}
5389
Jens Axboe11365042019-10-16 09:08:32 -06005390/*
5391 * Remove or update an existing timeout command
5392 */
Jens Axboefc4df992019-12-10 14:38:45 -07005393static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005394{
5395 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005396 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005397
Jens Axboe11365042019-10-16 09:08:32 -06005398 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005399 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005400
Jens Axboe47f46762019-11-09 17:43:02 -07005401 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005402 io_commit_cqring(ctx);
5403 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005404 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005405 if (ret < 0)
5406 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005407 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005408 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005409}
5410
Jens Axboe3529d8c2019-12-19 18:24:38 -07005411static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005412 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005413{
Jens Axboead8a48a2019-11-15 08:49:11 -07005414 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005415 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005416 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005417
Jens Axboead8a48a2019-11-15 08:49:11 -07005418 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005419 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005420 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005421 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005422 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005423 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005424 flags = READ_ONCE(sqe->timeout_flags);
5425 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005426 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005427
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005428 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005429
Jens Axboee8c2bc12020-08-15 18:44:09 -07005430 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005431 return -ENOMEM;
5432
Jens Axboee8c2bc12020-08-15 18:44:09 -07005433 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005434 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005435
5436 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005437 return -EFAULT;
5438
Jens Axboe11365042019-10-16 09:08:32 -06005439 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005440 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005441 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005442 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005443
Jens Axboead8a48a2019-11-15 08:49:11 -07005444 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5445 return 0;
5446}
5447
Jens Axboefc4df992019-12-10 14:38:45 -07005448static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005449{
Jens Axboead8a48a2019-11-15 08:49:11 -07005450 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005451 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005452 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005453 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005454
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005455 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005456
Jens Axboe5262f562019-09-17 12:26:57 -06005457 /*
5458 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005459 * timeout event to be satisfied. If it isn't set, then this is
5460 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005461 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005462 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005463 entry = ctx->timeout_list.prev;
5464 goto add;
5465 }
Jens Axboe5262f562019-09-17 12:26:57 -06005466
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005467 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5468 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005469
5470 /*
5471 * Insertion sort, ensuring the first entry in the list is always
5472 * the one we need first.
5473 */
Jens Axboe5262f562019-09-17 12:26:57 -06005474 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005475 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5476 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005477
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005478 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005479 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005480 /* nxt.seq is behind @tail, otherwise would've been completed */
5481 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005482 break;
5483 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005484add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005485 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005486 data->timer.function = io_timeout_fn;
5487 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005488 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005489 return 0;
5490}
5491
Jens Axboe62755e32019-10-28 21:49:21 -06005492static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005493{
Jens Axboe62755e32019-10-28 21:49:21 -06005494 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005495
Jens Axboe62755e32019-10-28 21:49:21 -06005496 return req->user_data == (unsigned long) data;
5497}
5498
Jens Axboee977d6d2019-11-05 12:39:45 -07005499static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005500{
Jens Axboe62755e32019-10-28 21:49:21 -06005501 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005502 int ret = 0;
5503
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005504 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005505 switch (cancel_ret) {
5506 case IO_WQ_CANCEL_OK:
5507 ret = 0;
5508 break;
5509 case IO_WQ_CANCEL_RUNNING:
5510 ret = -EALREADY;
5511 break;
5512 case IO_WQ_CANCEL_NOTFOUND:
5513 ret = -ENOENT;
5514 break;
5515 }
5516
Jens Axboee977d6d2019-11-05 12:39:45 -07005517 return ret;
5518}
5519
Jens Axboe47f46762019-11-09 17:43:02 -07005520static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5521 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005522 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005523{
5524 unsigned long flags;
5525 int ret;
5526
5527 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5528 if (ret != -ENOENT) {
5529 spin_lock_irqsave(&ctx->completion_lock, flags);
5530 goto done;
5531 }
5532
5533 spin_lock_irqsave(&ctx->completion_lock, flags);
5534 ret = io_timeout_cancel(ctx, sqe_addr);
5535 if (ret != -ENOENT)
5536 goto done;
5537 ret = io_poll_cancel(ctx, sqe_addr);
5538done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005539 if (!ret)
5540 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005541 io_cqring_fill_event(req, ret);
5542 io_commit_cqring(ctx);
5543 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5544 io_cqring_ev_posted(ctx);
5545
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005546 if (ret < 0)
5547 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005548 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005549}
5550
Jens Axboe3529d8c2019-12-19 18:24:38 -07005551static int io_async_cancel_prep(struct io_kiocb *req,
5552 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005553{
Jens Axboefbf23842019-12-17 18:45:56 -07005554 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005555 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005556 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5557 return -EINVAL;
5558 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005559 return -EINVAL;
5560
Jens Axboefbf23842019-12-17 18:45:56 -07005561 req->cancel.addr = READ_ONCE(sqe->addr);
5562 return 0;
5563}
5564
Pavel Begunkov014db002020-03-03 21:33:12 +03005565static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005566{
5567 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005568
Pavel Begunkov014db002020-03-03 21:33:12 +03005569 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005570 return 0;
5571}
5572
Jens Axboe05f3fb32019-12-09 11:22:50 -07005573static int io_files_update_prep(struct io_kiocb *req,
5574 const struct io_uring_sqe *sqe)
5575{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005576 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5577 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005578 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5579 return -EINVAL;
5580 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005581 return -EINVAL;
5582
5583 req->files_update.offset = READ_ONCE(sqe->off);
5584 req->files_update.nr_args = READ_ONCE(sqe->len);
5585 if (!req->files_update.nr_args)
5586 return -EINVAL;
5587 req->files_update.arg = READ_ONCE(sqe->addr);
5588 return 0;
5589}
5590
Jens Axboe229a7b62020-06-22 10:13:11 -06005591static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5592 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005593{
5594 struct io_ring_ctx *ctx = req->ctx;
5595 struct io_uring_files_update up;
5596 int ret;
5597
Jens Axboef86cd202020-01-29 13:46:44 -07005598 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005599 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005600
5601 up.offset = req->files_update.offset;
5602 up.fds = req->files_update.arg;
5603
5604 mutex_lock(&ctx->uring_lock);
5605 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5606 mutex_unlock(&ctx->uring_lock);
5607
5608 if (ret < 0)
5609 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005610 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005611 return 0;
5612}
5613
Jens Axboe3529d8c2019-12-19 18:24:38 -07005614static int io_req_defer_prep(struct io_kiocb *req,
5615 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005616{
Jens Axboee7815732019-12-17 19:45:06 -07005617 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005618
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005619 if (!sqe)
5620 return 0;
5621
Jens Axboee8c2bc12020-08-15 18:44:09 -07005622 if (io_alloc_async_data(req))
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005623 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005624 ret = io_prep_work_files(req);
5625 if (unlikely(ret))
5626 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005627
Jens Axboe202700e12020-09-12 13:18:10 -06005628 io_prep_async_work(req);
5629
Jens Axboed625c6e2019-12-17 19:53:05 -07005630 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005631 case IORING_OP_NOP:
5632 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005633 case IORING_OP_READV:
5634 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005635 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005636 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005637 break;
5638 case IORING_OP_WRITEV:
5639 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005640 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005641 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005642 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005643 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005644 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005645 break;
5646 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005647 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005648 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005649 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005650 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005651 break;
5652 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005653 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005654 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005655 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005656 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005657 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005658 break;
5659 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005660 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005661 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005662 break;
Jens Axboef499a022019-12-02 16:28:46 -07005663 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005664 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005665 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005666 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005667 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005668 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005669 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005670 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005671 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005672 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005673 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005674 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005675 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005676 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005677 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005678 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005679 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005680 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005681 case IORING_OP_FALLOCATE:
5682 ret = io_fallocate_prep(req, sqe);
5683 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005684 case IORING_OP_OPENAT:
5685 ret = io_openat_prep(req, sqe);
5686 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005687 case IORING_OP_CLOSE:
5688 ret = io_close_prep(req, sqe);
5689 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005690 case IORING_OP_FILES_UPDATE:
5691 ret = io_files_update_prep(req, sqe);
5692 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005693 case IORING_OP_STATX:
5694 ret = io_statx_prep(req, sqe);
5695 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005696 case IORING_OP_FADVISE:
5697 ret = io_fadvise_prep(req, sqe);
5698 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005699 case IORING_OP_MADVISE:
5700 ret = io_madvise_prep(req, sqe);
5701 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005702 case IORING_OP_OPENAT2:
5703 ret = io_openat2_prep(req, sqe);
5704 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005705 case IORING_OP_EPOLL_CTL:
5706 ret = io_epoll_ctl_prep(req, sqe);
5707 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005708 case IORING_OP_SPLICE:
5709 ret = io_splice_prep(req, sqe);
5710 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005711 case IORING_OP_PROVIDE_BUFFERS:
5712 ret = io_provide_buffers_prep(req, sqe);
5713 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005714 case IORING_OP_REMOVE_BUFFERS:
5715 ret = io_remove_buffers_prep(req, sqe);
5716 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005717 case IORING_OP_TEE:
5718 ret = io_tee_prep(req, sqe);
5719 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005720 default:
Jens Axboee7815732019-12-17 19:45:06 -07005721 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5722 req->opcode);
5723 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005724 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005725 }
5726
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005727 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005728}
5729
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005730static u32 io_get_sequence(struct io_kiocb *req)
5731{
5732 struct io_kiocb *pos;
5733 struct io_ring_ctx *ctx = req->ctx;
5734 u32 total_submitted, nr_reqs = 1;
5735
5736 if (req->flags & REQ_F_LINK_HEAD)
5737 list_for_each_entry(pos, &req->link_list, link_list)
5738 nr_reqs++;
5739
5740 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5741 return total_submitted - nr_reqs;
5742}
5743
Jens Axboe3529d8c2019-12-19 18:24:38 -07005744static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005745{
Jackie Liua197f662019-11-08 08:09:12 -07005746 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005747 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005748 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005749 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005750
Bob Liu9d858b22019-11-13 18:06:25 +08005751 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005752 if (likely(list_empty_careful(&ctx->defer_list) &&
5753 !(req->flags & REQ_F_IO_DRAIN)))
5754 return 0;
5755
5756 seq = io_get_sequence(req);
5757 /* Still a chance to pass the sequence check */
5758 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005759 return 0;
5760
Jens Axboee8c2bc12020-08-15 18:44:09 -07005761 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005762 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005763 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005764 return ret;
5765 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005766 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005767 de = kmalloc(sizeof(*de), GFP_KERNEL);
5768 if (!de)
5769 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005770
Jens Axboede0617e2019-04-06 21:51:27 -06005771 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005772 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005773 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005774 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005775 io_queue_async_work(req);
5776 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005777 }
5778
Jens Axboe915967f2019-11-21 09:01:20 -07005779 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005780 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005781 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005782 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005783 spin_unlock_irq(&ctx->completion_lock);
5784 return -EIOCBQUEUED;
5785}
5786
Jens Axboef573d382020-09-22 10:19:24 -06005787static void io_req_drop_files(struct io_kiocb *req)
5788{
5789 struct io_ring_ctx *ctx = req->ctx;
5790 unsigned long flags;
5791
5792 spin_lock_irqsave(&ctx->inflight_lock, flags);
5793 list_del(&req->inflight_entry);
5794 if (waitqueue_active(&ctx->inflight_wait))
5795 wake_up(&ctx->inflight_wait);
5796 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5797 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005798 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005799 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005800 req->work.files = NULL;
5801}
5802
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005803static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005804{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005805 if (req->flags & REQ_F_BUFFER_SELECTED) {
5806 switch (req->opcode) {
5807 case IORING_OP_READV:
5808 case IORING_OP_READ_FIXED:
5809 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005810 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005811 break;
5812 case IORING_OP_RECVMSG:
5813 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005814 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005815 break;
5816 }
5817 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005818 }
5819
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005820 if (req->flags & REQ_F_NEED_CLEANUP) {
5821 switch (req->opcode) {
5822 case IORING_OP_READV:
5823 case IORING_OP_READ_FIXED:
5824 case IORING_OP_READ:
5825 case IORING_OP_WRITEV:
5826 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005827 case IORING_OP_WRITE: {
5828 struct io_async_rw *io = req->async_data;
5829 if (io->free_iovec)
5830 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005831 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005832 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005833 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005834 case IORING_OP_SENDMSG: {
5835 struct io_async_msghdr *io = req->async_data;
5836 if (io->iov != io->fast_iov)
5837 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005838 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005839 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005840 case IORING_OP_SPLICE:
5841 case IORING_OP_TEE:
5842 io_put_file(req, req->splice.file_in,
5843 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5844 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005845 case IORING_OP_OPENAT:
5846 case IORING_OP_OPENAT2:
5847 if (req->open.filename)
5848 putname(req->open.filename);
5849 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005850 }
5851 req->flags &= ~REQ_F_NEED_CLEANUP;
5852 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005853
Jens Axboef573d382020-09-22 10:19:24 -06005854 if (req->flags & REQ_F_INFLIGHT)
5855 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005856}
5857
Jens Axboe3529d8c2019-12-19 18:24:38 -07005858static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005859 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005860{
Jackie Liua197f662019-11-08 08:09:12 -07005861 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005862 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005863
Jens Axboed625c6e2019-12-17 19:53:05 -07005864 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005865 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005866 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005867 break;
5868 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005869 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005870 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005871 if (sqe) {
5872 ret = io_read_prep(req, sqe, force_nonblock);
5873 if (ret < 0)
5874 break;
5875 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005876 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005877 break;
5878 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005879 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005880 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005881 if (sqe) {
5882 ret = io_write_prep(req, sqe, force_nonblock);
5883 if (ret < 0)
5884 break;
5885 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005886 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005887 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005888 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005889 if (sqe) {
5890 ret = io_prep_fsync(req, sqe);
5891 if (ret < 0)
5892 break;
5893 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005894 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005895 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005896 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005897 if (sqe) {
5898 ret = io_poll_add_prep(req, sqe);
5899 if (ret)
5900 break;
5901 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005902 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005903 break;
5904 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005905 if (sqe) {
5906 ret = io_poll_remove_prep(req, sqe);
5907 if (ret < 0)
5908 break;
5909 }
Jens Axboefc4df992019-12-10 14:38:45 -07005910 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005911 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005912 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005913 if (sqe) {
5914 ret = io_prep_sfr(req, sqe);
5915 if (ret < 0)
5916 break;
5917 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005918 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005919 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005920 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005921 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005922 if (sqe) {
5923 ret = io_sendmsg_prep(req, sqe);
5924 if (ret < 0)
5925 break;
5926 }
Jens Axboefddafac2020-01-04 20:19:44 -07005927 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005928 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005929 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005930 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005931 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005932 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005933 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005934 if (sqe) {
5935 ret = io_recvmsg_prep(req, sqe);
5936 if (ret)
5937 break;
5938 }
Jens Axboefddafac2020-01-04 20:19:44 -07005939 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005940 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005941 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005942 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005943 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005944 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005945 if (sqe) {
5946 ret = io_timeout_prep(req, sqe, false);
5947 if (ret)
5948 break;
5949 }
Jens Axboefc4df992019-12-10 14:38:45 -07005950 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005951 break;
Jens Axboe11365042019-10-16 09:08:32 -06005952 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005953 if (sqe) {
5954 ret = io_timeout_remove_prep(req, sqe);
5955 if (ret)
5956 break;
5957 }
Jens Axboefc4df992019-12-10 14:38:45 -07005958 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005959 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005960 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005961 if (sqe) {
5962 ret = io_accept_prep(req, sqe);
5963 if (ret)
5964 break;
5965 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005966 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005967 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005968 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005969 if (sqe) {
5970 ret = io_connect_prep(req, sqe);
5971 if (ret)
5972 break;
5973 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005974 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005975 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005976 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005977 if (sqe) {
5978 ret = io_async_cancel_prep(req, sqe);
5979 if (ret)
5980 break;
5981 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005982 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005983 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005984 case IORING_OP_FALLOCATE:
5985 if (sqe) {
5986 ret = io_fallocate_prep(req, sqe);
5987 if (ret)
5988 break;
5989 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005990 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005991 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005992 case IORING_OP_OPENAT:
5993 if (sqe) {
5994 ret = io_openat_prep(req, sqe);
5995 if (ret)
5996 break;
5997 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005998 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005999 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006000 case IORING_OP_CLOSE:
6001 if (sqe) {
6002 ret = io_close_prep(req, sqe);
6003 if (ret)
6004 break;
6005 }
Jens Axboe229a7b62020-06-22 10:13:11 -06006006 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006007 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006008 case IORING_OP_FILES_UPDATE:
6009 if (sqe) {
6010 ret = io_files_update_prep(req, sqe);
6011 if (ret)
6012 break;
6013 }
Jens Axboe229a7b62020-06-22 10:13:11 -06006014 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006015 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006016 case IORING_OP_STATX:
6017 if (sqe) {
6018 ret = io_statx_prep(req, sqe);
6019 if (ret)
6020 break;
6021 }
Pavel Begunkov014db002020-03-03 21:33:12 +03006022 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006023 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006024 case IORING_OP_FADVISE:
6025 if (sqe) {
6026 ret = io_fadvise_prep(req, sqe);
6027 if (ret)
6028 break;
6029 }
Pavel Begunkov014db002020-03-03 21:33:12 +03006030 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006031 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006032 case IORING_OP_MADVISE:
6033 if (sqe) {
6034 ret = io_madvise_prep(req, sqe);
6035 if (ret)
6036 break;
6037 }
Pavel Begunkov014db002020-03-03 21:33:12 +03006038 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006039 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006040 case IORING_OP_OPENAT2:
6041 if (sqe) {
6042 ret = io_openat2_prep(req, sqe);
6043 if (ret)
6044 break;
6045 }
Pavel Begunkov014db002020-03-03 21:33:12 +03006046 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006047 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006048 case IORING_OP_EPOLL_CTL:
6049 if (sqe) {
6050 ret = io_epoll_ctl_prep(req, sqe);
6051 if (ret)
6052 break;
6053 }
Jens Axboe229a7b62020-06-22 10:13:11 -06006054 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006055 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006056 case IORING_OP_SPLICE:
6057 if (sqe) {
6058 ret = io_splice_prep(req, sqe);
6059 if (ret < 0)
6060 break;
6061 }
Pavel Begunkov014db002020-03-03 21:33:12 +03006062 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006063 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006064 case IORING_OP_PROVIDE_BUFFERS:
6065 if (sqe) {
6066 ret = io_provide_buffers_prep(req, sqe);
6067 if (ret)
6068 break;
6069 }
Jens Axboe229a7b62020-06-22 10:13:11 -06006070 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006071 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006072 case IORING_OP_REMOVE_BUFFERS:
6073 if (sqe) {
6074 ret = io_remove_buffers_prep(req, sqe);
6075 if (ret)
6076 break;
6077 }
Jens Axboe229a7b62020-06-22 10:13:11 -06006078 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006079 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006080 case IORING_OP_TEE:
6081 if (sqe) {
6082 ret = io_tee_prep(req, sqe);
6083 if (ret < 0)
6084 break;
6085 }
6086 ret = io_tee(req, force_nonblock);
6087 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006088 default:
6089 ret = -EINVAL;
6090 break;
6091 }
6092
6093 if (ret)
6094 return ret;
6095
Jens Axboeb5325762020-05-19 21:20:27 -06006096 /* If the op doesn't have a file, we're not polling for it */
6097 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006098 const bool in_async = io_wq_current_is_worker();
6099
Jens Axboe11ba8202020-01-15 21:51:17 -07006100 /* workqueue context doesn't hold uring_lock, grab it now */
6101 if (in_async)
6102 mutex_lock(&ctx->uring_lock);
6103
Jens Axboe2b188cc2019-01-07 10:46:33 -07006104 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006105
6106 if (in_async)
6107 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006108 }
6109
6110 return 0;
6111}
6112
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006113static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006114{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006115 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006116 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006117 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006118
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006119 timeout = io_prep_linked_timeout(req);
6120 if (timeout)
6121 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006122
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006123 /* if NO_CANCEL is set, we must still run the work */
6124 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6125 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006126 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006127 }
Jens Axboe31b51512019-01-18 22:56:34 -07006128
Jens Axboe561fb042019-10-24 07:25:42 -06006129 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006130 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006131 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006132 /*
6133 * We can get EAGAIN for polled IO even though we're
6134 * forcing a sync submission from here, since we can't
6135 * wait for request slots on the block side.
6136 */
6137 if (ret != -EAGAIN)
6138 break;
6139 cond_resched();
6140 } while (1);
6141 }
Jens Axboe31b51512019-01-18 22:56:34 -07006142
Jens Axboe561fb042019-10-24 07:25:42 -06006143 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006144 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006145 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006146 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006148 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006149}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006150
Jens Axboe65e19f52019-10-26 07:20:21 -06006151static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6152 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006153{
Jens Axboe65e19f52019-10-26 07:20:21 -06006154 struct fixed_file_table *table;
6155
Jens Axboe05f3fb32019-12-09 11:22:50 -07006156 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006157 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006158}
6159
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006160static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6161 int fd, struct file **out_file, bool fixed)
6162{
6163 struct io_ring_ctx *ctx = req->ctx;
6164 struct file *file;
6165
6166 if (fixed) {
6167 if (unlikely(!ctx->file_data ||
6168 (unsigned) fd >= ctx->nr_user_files))
6169 return -EBADF;
6170 fd = array_index_nospec(fd, ctx->nr_user_files);
6171 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006172 if (file) {
6173 req->fixed_file_refs = ctx->file_data->cur_refs;
6174 percpu_ref_get(req->fixed_file_refs);
6175 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006176 } else {
6177 trace_io_uring_file_get(ctx, fd);
6178 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006179 }
6180
Jens Axboefd2206e2020-06-02 16:40:47 -06006181 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6182 *out_file = file;
6183 return 0;
6184 }
6185 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006186}
6187
Jens Axboe3529d8c2019-12-19 18:24:38 -07006188static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006189 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006190{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006191 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006192
Jens Axboe63ff8222020-05-07 14:56:15 -06006193 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006194 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006195 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006196
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006197 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006198}
6199
Jackie Liua197f662019-11-08 08:09:12 -07006200static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006201{
Jackie Liua197f662019-11-08 08:09:12 -07006202 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006203
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006204 io_req_init_async(req);
6205
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006206 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006207 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006208
Jens Axboe0f212202020-09-13 13:09:39 -06006209 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006210 get_nsproxy(current->nsproxy);
6211 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006212 req->flags |= REQ_F_INFLIGHT;
6213
Jens Axboefcb323c2019-10-24 12:39:47 -06006214 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006215 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006216 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006217 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006218}
6219
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006220static inline int io_prep_work_files(struct io_kiocb *req)
6221{
6222 if (!io_op_defs[req->opcode].file_table)
6223 return 0;
6224 return io_grab_files(req);
6225}
6226
Jens Axboe2665abf2019-11-05 12:40:47 -07006227static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6228{
Jens Axboead8a48a2019-11-15 08:49:11 -07006229 struct io_timeout_data *data = container_of(timer,
6230 struct io_timeout_data, timer);
6231 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006232 struct io_ring_ctx *ctx = req->ctx;
6233 struct io_kiocb *prev = NULL;
6234 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006235
6236 spin_lock_irqsave(&ctx->completion_lock, flags);
6237
6238 /*
6239 * We don't expect the list to be empty, that will only happen if we
6240 * race with the completion of the linked work.
6241 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006242 if (!list_empty(&req->link_list)) {
6243 prev = list_entry(req->link_list.prev, struct io_kiocb,
6244 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006245 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006246 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006247 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6248 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006249 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006250 }
6251
6252 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6253
6254 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006255 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006256 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006257 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006258 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006259 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006260 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006261 return HRTIMER_NORESTART;
6262}
6263
Jens Axboe7271ef32020-08-10 09:55:22 -06006264static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006265{
Jens Axboe76a46e02019-11-10 23:34:16 -07006266 /*
6267 * If the list is now empty, then our linked request finished before
6268 * we got a chance to setup the timer
6269 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006270 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006271 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006272
Jens Axboead8a48a2019-11-15 08:49:11 -07006273 data->timer.function = io_link_timeout_fn;
6274 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6275 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006276 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006277}
6278
6279static void io_queue_linked_timeout(struct io_kiocb *req)
6280{
6281 struct io_ring_ctx *ctx = req->ctx;
6282
6283 spin_lock_irq(&ctx->completion_lock);
6284 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006285 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006286
Jens Axboe2665abf2019-11-05 12:40:47 -07006287 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006288 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006289}
6290
Jens Axboead8a48a2019-11-15 08:49:11 -07006291static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006292{
6293 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006294
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006295 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006296 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006297 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006298 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006299
Pavel Begunkov44932332019-12-05 16:16:35 +03006300 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6301 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006302 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006303 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006304
Jens Axboe76a46e02019-11-10 23:34:16 -07006305 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006306 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006307}
6308
Jens Axboef13fad72020-06-22 09:34:30 -06006309static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6310 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006312 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006313 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006314 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006315 int ret;
6316
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006317again:
6318 linked_timeout = io_prep_linked_timeout(req);
6319
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006320 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6321 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006322 if (old_creds)
6323 revert_creds(old_creds);
6324 if (old_creds == req->work.creds)
6325 old_creds = NULL; /* restored original creds */
6326 else
6327 old_creds = override_creds(req->work.creds);
6328 }
6329
Jens Axboef13fad72020-06-22 09:34:30 -06006330 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006331
6332 /*
6333 * We async punt it if the file wasn't marked NOWAIT, or if the file
6334 * doesn't support non-blocking read/write attempts
6335 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006336 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006337 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006338punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006339 ret = io_prep_work_files(req);
6340 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006341 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006342 /*
6343 * Queued up for async execution, worker will release
6344 * submit reference when the iocb is actually submitted.
6345 */
6346 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006347 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006348
Pavel Begunkovf063c542020-07-25 14:41:59 +03006349 if (linked_timeout)
6350 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006351 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006352 }
Jens Axboee65ef562019-03-12 10:16:44 -06006353
Pavel Begunkov652532a2020-07-03 22:15:07 +03006354 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006355err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006356 /* un-prep timeout, so it'll be killed as any other linked */
6357 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006358 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006359 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006360 io_req_complete(req, ret);
6361 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006362 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006363
Jens Axboe6c271ce2019-01-10 11:22:30 -07006364 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006365 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006366 if (linked_timeout)
6367 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006368
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006369 if (nxt) {
6370 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006371
6372 if (req->flags & REQ_F_FORCE_ASYNC)
6373 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006374 goto again;
6375 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006376exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006377 if (old_creds)
6378 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006379}
6380
Jens Axboef13fad72020-06-22 09:34:30 -06006381static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6382 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006383{
6384 int ret;
6385
Jens Axboe3529d8c2019-12-19 18:24:38 -07006386 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006387 if (ret) {
6388 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006389fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006390 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006391 io_put_req(req);
6392 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006393 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006394 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006395 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006396 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006397 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006398 goto fail_req;
6399 }
6400
Jens Axboece35a472019-12-17 08:04:44 -07006401 /*
6402 * Never try inline submit of IOSQE_ASYNC is set, go straight
6403 * to async execution.
6404 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006405 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006406 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6407 io_queue_async_work(req);
6408 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006409 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006410 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006411}
6412
Jens Axboef13fad72020-06-22 09:34:30 -06006413static inline void io_queue_link_head(struct io_kiocb *req,
6414 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006415{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006416 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006417 io_put_req(req);
6418 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006419 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006420 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006421}
6422
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006423static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006424 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006425{
Jackie Liua197f662019-11-08 08:09:12 -07006426 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006427 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006428
Jens Axboe9e645e112019-05-10 16:07:28 -06006429 /*
6430 * If we already have a head request, queue this one for async
6431 * submittal once the head completes. If we don't have a head but
6432 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6433 * submitted sync once the chain is complete. If none of those
6434 * conditions are true (normal request), then just queue it.
6435 */
6436 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006437 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006438
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006439 /*
6440 * Taking sequential execution of a link, draining both sides
6441 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6442 * requests in the link. So, it drains the head and the
6443 * next after the link request. The last one is done via
6444 * drain_next flag to persist the effect across calls.
6445 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006446 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006447 head->flags |= REQ_F_IO_DRAIN;
6448 ctx->drain_next = 1;
6449 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006450 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006451 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006452 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006453 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006454 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006455 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006456 trace_io_uring_link(ctx, req, head);
6457 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006458
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006459 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006460 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006461 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006462 *link = NULL;
6463 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006464 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006465 if (unlikely(ctx->drain_next)) {
6466 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006467 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006468 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006469 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006470 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006471 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006472
Pavel Begunkov711be032020-01-17 03:57:59 +03006473 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006474 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006475 req->flags |= REQ_F_FAIL_LINK;
6476 *link = req;
6477 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006478 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006479 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006480 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006481
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006482 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006483}
6484
Jens Axboe9a56a232019-01-09 09:06:50 -07006485/*
6486 * Batched submission is done, ensure local IO is flushed out.
6487 */
6488static void io_submit_state_end(struct io_submit_state *state)
6489{
Jens Axboef13fad72020-06-22 09:34:30 -06006490 if (!list_empty(&state->comp.list))
6491 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006492 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006493 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006494 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006495 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006496}
6497
6498/*
6499 * Start submission side cache.
6500 */
6501static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006502 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006503{
6504 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006505 state->comp.nr = 0;
6506 INIT_LIST_HEAD(&state->comp.list);
6507 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006508 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006509 state->file = NULL;
6510 state->ios_left = max_ios;
6511}
6512
Jens Axboe2b188cc2019-01-07 10:46:33 -07006513static void io_commit_sqring(struct io_ring_ctx *ctx)
6514{
Hristo Venev75b28af2019-08-26 17:23:46 +00006515 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006516
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006517 /*
6518 * Ensure any loads from the SQEs are done at this point,
6519 * since once we write the new head, the application could
6520 * write new data to them.
6521 */
6522 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006523}
6524
6525/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006526 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006527 * that is mapped by userspace. This means that care needs to be taken to
6528 * ensure that reads are stable, as we cannot rely on userspace always
6529 * being a good citizen. If members of the sqe are validated and then later
6530 * used, it's important that those reads are done through READ_ONCE() to
6531 * prevent a re-load down the line.
6532 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006533static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006534{
Hristo Venev75b28af2019-08-26 17:23:46 +00006535 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006536 unsigned head;
6537
6538 /*
6539 * The cached sq head (or cq tail) serves two purposes:
6540 *
6541 * 1) allows us to batch the cost of updating the user visible
6542 * head updates.
6543 * 2) allows the kernel side to track the head on its own, even
6544 * though the application is the one updating it.
6545 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006546 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006547 if (likely(head < ctx->sq_entries))
6548 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006549
6550 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006551 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006552 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006553 return NULL;
6554}
6555
6556static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6557{
6558 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006559}
6560
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006561/*
6562 * Check SQE restrictions (opcode and flags).
6563 *
6564 * Returns 'true' if SQE is allowed, 'false' otherwise.
6565 */
6566static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6567 struct io_kiocb *req,
6568 unsigned int sqe_flags)
6569{
6570 if (!ctx->restricted)
6571 return true;
6572
6573 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6574 return false;
6575
6576 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6577 ctx->restrictions.sqe_flags_required)
6578 return false;
6579
6580 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6581 ctx->restrictions.sqe_flags_required))
6582 return false;
6583
6584 return true;
6585}
6586
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006587#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6588 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6589 IOSQE_BUFFER_SELECT)
6590
6591static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6592 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006593 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006594{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006595 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006596 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006597
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006598 req->opcode = READ_ONCE(sqe->opcode);
6599 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006600 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006601 req->file = NULL;
6602 req->ctx = ctx;
6603 req->flags = 0;
6604 /* one is dropped after submission, the other at completion */
6605 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006606 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006607 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006608 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006609 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006610
6611 if (unlikely(req->opcode >= IORING_OP_LAST))
6612 return -EINVAL;
6613
Jens Axboe9d8426a2020-06-16 18:42:49 -06006614 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6615 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006616
6617 sqe_flags = READ_ONCE(sqe->flags);
6618 /* enforce forwards compatibility on users */
6619 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6620 return -EINVAL;
6621
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006622 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6623 return -EACCES;
6624
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006625 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6626 !io_op_defs[req->opcode].buffer_select)
6627 return -EOPNOTSUPP;
6628
6629 id = READ_ONCE(sqe->personality);
6630 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006631 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006632 req->work.creds = idr_find(&ctx->personality_idr, id);
6633 if (unlikely(!req->work.creds))
6634 return -EINVAL;
6635 get_cred(req->work.creds);
6636 }
6637
6638 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006639 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006640
Jens Axboe63ff8222020-05-07 14:56:15 -06006641 if (!io_op_defs[req->opcode].needs_file)
6642 return 0;
6643
6644 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006645}
6646
Jens Axboe0f212202020-09-13 13:09:39 -06006647static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006648{
Jens Axboeac8691c2020-06-01 08:30:41 -06006649 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006650 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006651 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006652
Jens Axboec4a2ed72019-11-21 21:01:26 -07006653 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006654 if (test_bit(0, &ctx->sq_check_overflow)) {
6655 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006656 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006657 return -EBUSY;
6658 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006659
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006660 /* make sure SQ entry isn't read before tail */
6661 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006662
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006663 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6664 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006665
Jens Axboe013538b2020-06-22 09:29:15 -06006666 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006667
6668 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006669 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006670 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006671 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006672
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006673 sqe = io_get_sqe(ctx);
6674 if (unlikely(!sqe)) {
6675 io_consume_sqe(ctx);
6676 break;
6677 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006678 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006679 if (unlikely(!req)) {
6680 if (!submitted)
6681 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006682 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006683 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006684
Jens Axboeac8691c2020-06-01 08:30:41 -06006685 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006686 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006687 /* will complete beyond this point, count as submitted */
6688 submitted++;
6689
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006690 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006691fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006692 io_put_req(req);
6693 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006694 break;
6695 }
6696
Jens Axboe354420f2020-01-08 18:55:15 -07006697 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006698 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006699 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006700 if (err)
6701 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006702 }
6703
Pavel Begunkov9466f432020-01-25 22:34:01 +03006704 if (unlikely(submitted != nr)) {
6705 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6706
6707 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6708 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006709 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006710 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006711 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006712
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006713 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6714 io_commit_sqring(ctx);
6715
Jens Axboe6c271ce2019-01-10 11:22:30 -07006716 return submitted;
6717}
6718
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006719static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6720{
6721 /* Tell userspace we may need a wakeup call */
6722 spin_lock_irq(&ctx->completion_lock);
6723 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6724 spin_unlock_irq(&ctx->completion_lock);
6725}
6726
6727static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6728{
6729 spin_lock_irq(&ctx->completion_lock);
6730 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6731 spin_unlock_irq(&ctx->completion_lock);
6732}
6733
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006734static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6735 int sync, void *key)
6736{
6737 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6738 int ret;
6739
6740 ret = autoremove_wake_function(wqe, mode, sync, key);
6741 if (ret) {
6742 unsigned long flags;
6743
6744 spin_lock_irqsave(&ctx->completion_lock, flags);
6745 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6746 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6747 }
6748 return ret;
6749}
6750
Jens Axboec8d1ba52020-09-14 11:07:26 -06006751enum sq_ret {
6752 SQT_IDLE = 1,
6753 SQT_SPIN = 2,
6754 SQT_DID_WORK = 4,
6755};
6756
6757static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006758 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006759{
6760 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006761 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006762 unsigned int to_submit;
6763 int ret = 0;
6764
6765again:
6766 if (!list_empty(&ctx->iopoll_list)) {
6767 unsigned nr_events = 0;
6768
6769 mutex_lock(&ctx->uring_lock);
6770 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6771 io_do_iopoll(ctx, &nr_events, 0);
6772 mutex_unlock(&ctx->uring_lock);
6773 }
6774
6775 to_submit = io_sqring_entries(ctx);
6776
6777 /*
6778 * If submit got -EBUSY, flag us as needing the application
6779 * to enter the kernel to reap and flush events.
6780 */
6781 if (!to_submit || ret == -EBUSY || need_resched()) {
6782 /*
6783 * Drop cur_mm before scheduling, we can't hold it for
6784 * long periods (or over schedule()). Do this before
6785 * adding ourselves to the waitqueue, as the unuse/drop
6786 * may sleep.
6787 */
6788 io_sq_thread_drop_mm();
6789
6790 /*
6791 * We're polling. If we're within the defined idle
6792 * period, then let us spin without work before going
6793 * to sleep. The exception is if we got EBUSY doing
6794 * more IO, we should wait for the application to
6795 * reap events and wake us up.
6796 */
6797 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6798 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6799 !percpu_ref_is_dying(&ctx->refs)))
6800 return SQT_SPIN;
6801
Jens Axboe534ca6d2020-09-02 13:52:19 -06006802 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006803 TASK_INTERRUPTIBLE);
6804
6805 /*
6806 * While doing polled IO, before going to sleep, we need
6807 * to check if there are new reqs added to iopoll_list,
6808 * it is because reqs may have been punted to io worker
6809 * and will be added to iopoll_list later, hence check
6810 * the iopoll_list again.
6811 */
6812 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6813 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006814 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006815 goto again;
6816 }
6817
Jens Axboec8d1ba52020-09-14 11:07:26 -06006818 to_submit = io_sqring_entries(ctx);
6819 if (!to_submit || ret == -EBUSY)
6820 return SQT_IDLE;
6821 }
6822
Jens Axboe534ca6d2020-09-02 13:52:19 -06006823 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006824 io_ring_clear_wakeup_flag(ctx);
6825
Jens Axboee95eee22020-09-08 09:11:32 -06006826 /* if we're handling multiple rings, cap submit size for fairness */
6827 if (cap_entries && to_submit > 8)
6828 to_submit = 8;
6829
Jens Axboec8d1ba52020-09-14 11:07:26 -06006830 mutex_lock(&ctx->uring_lock);
6831 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6832 ret = io_submit_sqes(ctx, to_submit);
6833 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006834
6835 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6836 wake_up(&ctx->sqo_sq_wait);
6837
Jens Axboec8d1ba52020-09-14 11:07:26 -06006838 return SQT_DID_WORK;
6839}
6840
Jens Axboe69fb2132020-09-14 11:16:23 -06006841static void io_sqd_init_new(struct io_sq_data *sqd)
6842{
6843 struct io_ring_ctx *ctx;
6844
6845 while (!list_empty(&sqd->ctx_new_list)) {
6846 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6847 init_wait(&ctx->sqo_wait_entry);
6848 ctx->sqo_wait_entry.func = io_sq_wake_function;
6849 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6850 complete(&ctx->sq_thread_comp);
6851 }
6852}
6853
Jens Axboe6c271ce2019-01-10 11:22:30 -07006854static int io_sq_thread(void *data)
6855{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006856 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006857 const struct cred *old_cred = NULL;
6858 struct io_sq_data *sqd = data;
6859 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006860 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006861
Jens Axboec8d1ba52020-09-14 11:07:26 -06006862 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006863 while (!kthread_should_stop()) {
6864 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006865 bool cap_entries;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006866
Jens Axboe69fb2132020-09-14 11:16:23 -06006867 /*
6868 * Any changes to the sqd lists are synchronized through the
6869 * kthread parking. This synchronizes the thread vs users,
6870 * the users are synchronized on the sqd->ctx_lock.
6871 */
6872 if (kthread_should_park())
6873 kthread_parkme();
6874
6875 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6876 io_sqd_init_new(sqd);
6877
Jens Axboee95eee22020-09-08 09:11:32 -06006878 cap_entries = !list_is_singular(&sqd->ctx_list);
6879
Jens Axboe69fb2132020-09-14 11:16:23 -06006880 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6881 if (current->cred != ctx->creds) {
6882 if (old_cred)
6883 revert_creds(old_cred);
6884 old_cred = override_creds(ctx->creds);
6885 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006886 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe69fb2132020-09-14 11:16:23 -06006887
Jens Axboee95eee22020-09-08 09:11:32 -06006888 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006889
6890 io_sq_thread_drop_mm();
6891 }
6892
6893 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006894 io_run_task_work();
6895 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006896 } else if (ret == SQT_IDLE) {
6897 if (kthread_should_park())
6898 continue;
6899 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6900 io_ring_set_wakeup_flag(ctx);
6901 schedule();
6902 start_jiffies = jiffies;
6903 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6904 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006905 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006906 }
6907
Jens Axboe4c6e2772020-07-01 11:29:10 -06006908 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006909
Dennis Zhou91d8f512020-09-16 13:41:05 -07006910 if (cur_css)
6911 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006912 if (old_cred)
6913 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006914
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006915 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006916
Jens Axboe6c271ce2019-01-10 11:22:30 -07006917 return 0;
6918}
6919
Jens Axboebda52162019-09-24 13:47:15 -06006920struct io_wait_queue {
6921 struct wait_queue_entry wq;
6922 struct io_ring_ctx *ctx;
6923 unsigned to_wait;
6924 unsigned nr_timeouts;
6925};
6926
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006927static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006928{
6929 struct io_ring_ctx *ctx = iowq->ctx;
6930
6931 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006932 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006933 * started waiting. For timeouts, we always want to return to userspace,
6934 * regardless of event count.
6935 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006936 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006937 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6938}
6939
6940static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6941 int wake_flags, void *key)
6942{
6943 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6944 wq);
6945
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006946 /* use noflush == true, as we can't safely rely on locking context */
6947 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006948 return -1;
6949
6950 return autoremove_wake_function(curr, mode, wake_flags, key);
6951}
6952
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006953static int io_run_task_work_sig(void)
6954{
6955 if (io_run_task_work())
6956 return 1;
6957 if (!signal_pending(current))
6958 return 0;
6959 if (current->jobctl & JOBCTL_TASK_WORK) {
6960 spin_lock_irq(&current->sighand->siglock);
6961 current->jobctl &= ~JOBCTL_TASK_WORK;
6962 recalc_sigpending();
6963 spin_unlock_irq(&current->sighand->siglock);
6964 return 1;
6965 }
6966 return -EINTR;
6967}
6968
Jens Axboe2b188cc2019-01-07 10:46:33 -07006969/*
6970 * Wait until events become available, if we don't already have some. The
6971 * application must reap them itself, as they reside on the shared cq ring.
6972 */
6973static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6974 const sigset_t __user *sig, size_t sigsz)
6975{
Jens Axboebda52162019-09-24 13:47:15 -06006976 struct io_wait_queue iowq = {
6977 .wq = {
6978 .private = current,
6979 .func = io_wake_function,
6980 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6981 },
6982 .ctx = ctx,
6983 .to_wait = min_events,
6984 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006985 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006986 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006987
Jens Axboeb41e9852020-02-17 09:52:41 -07006988 do {
6989 if (io_cqring_events(ctx, false) >= min_events)
6990 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006991 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006992 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006993 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006994
6995 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006996#ifdef CONFIG_COMPAT
6997 if (in_compat_syscall())
6998 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006999 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007000 else
7001#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007002 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007003
Jens Axboe2b188cc2019-01-07 10:46:33 -07007004 if (ret)
7005 return ret;
7006 }
7007
Jens Axboebda52162019-09-24 13:47:15 -06007008 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007009 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007010 do {
7011 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7012 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007013 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007014 ret = io_run_task_work_sig();
7015 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06007016 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007017 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007018 break;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07007019 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06007020 break;
7021 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06007022 } while (1);
7023 finish_wait(&ctx->wait, &iowq.wq);
7024
Jens Axboeb7db41c2020-07-04 08:55:50 -06007025 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007026
Hristo Venev75b28af2019-08-26 17:23:46 +00007027 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007028}
7029
Jens Axboe6b063142019-01-10 22:13:58 -07007030static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7031{
7032#if defined(CONFIG_UNIX)
7033 if (ctx->ring_sock) {
7034 struct sock *sock = ctx->ring_sock->sk;
7035 struct sk_buff *skb;
7036
7037 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7038 kfree_skb(skb);
7039 }
7040#else
7041 int i;
7042
Jens Axboe65e19f52019-10-26 07:20:21 -06007043 for (i = 0; i < ctx->nr_user_files; i++) {
7044 struct file *file;
7045
7046 file = io_file_from_index(ctx, i);
7047 if (file)
7048 fput(file);
7049 }
Jens Axboe6b063142019-01-10 22:13:58 -07007050#endif
7051}
7052
Jens Axboe05f3fb32019-12-09 11:22:50 -07007053static void io_file_ref_kill(struct percpu_ref *ref)
7054{
7055 struct fixed_file_data *data;
7056
7057 data = container_of(ref, struct fixed_file_data, refs);
7058 complete(&data->done);
7059}
7060
Jens Axboe6b063142019-01-10 22:13:58 -07007061static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7062{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007063 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007064 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007065 unsigned nr_tables, i;
7066
Jens Axboe05f3fb32019-12-09 11:22:50 -07007067 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007068 return -ENXIO;
7069
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007070 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007071 if (!list_empty(&data->ref_list))
7072 ref_node = list_first_entry(&data->ref_list,
7073 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007074 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007075 if (ref_node)
7076 percpu_ref_kill(&ref_node->refs);
7077
7078 percpu_ref_kill(&data->refs);
7079
7080 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007081 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07007082 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007083
Jens Axboe6b063142019-01-10 22:13:58 -07007084 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007085 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7086 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007087 kfree(data->table[i].files);
7088 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007089 percpu_ref_exit(&data->refs);
7090 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007091 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007092 ctx->nr_user_files = 0;
7093 return 0;
7094}
7095
Jens Axboe534ca6d2020-09-02 13:52:19 -06007096static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007097{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007098 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007099 /*
7100 * The park is a bit of a work-around, without it we get
7101 * warning spews on shutdown with SQPOLL set and affinity
7102 * set to a single CPU.
7103 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007104 if (sqd->thread) {
7105 kthread_park(sqd->thread);
7106 kthread_stop(sqd->thread);
7107 }
7108
7109 kfree(sqd);
7110 }
7111}
7112
Jens Axboeaa061652020-09-02 14:50:27 -06007113static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7114{
7115 struct io_ring_ctx *ctx_attach;
7116 struct io_sq_data *sqd;
7117 struct fd f;
7118
7119 f = fdget(p->wq_fd);
7120 if (!f.file)
7121 return ERR_PTR(-ENXIO);
7122 if (f.file->f_op != &io_uring_fops) {
7123 fdput(f);
7124 return ERR_PTR(-EINVAL);
7125 }
7126
7127 ctx_attach = f.file->private_data;
7128 sqd = ctx_attach->sq_data;
7129 if (!sqd) {
7130 fdput(f);
7131 return ERR_PTR(-EINVAL);
7132 }
7133
7134 refcount_inc(&sqd->refs);
7135 fdput(f);
7136 return sqd;
7137}
7138
Jens Axboe534ca6d2020-09-02 13:52:19 -06007139static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7140{
7141 struct io_sq_data *sqd;
7142
Jens Axboeaa061652020-09-02 14:50:27 -06007143 if (p->flags & IORING_SETUP_ATTACH_WQ)
7144 return io_attach_sq_data(p);
7145
Jens Axboe534ca6d2020-09-02 13:52:19 -06007146 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7147 if (!sqd)
7148 return ERR_PTR(-ENOMEM);
7149
7150 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007151 INIT_LIST_HEAD(&sqd->ctx_list);
7152 INIT_LIST_HEAD(&sqd->ctx_new_list);
7153 mutex_init(&sqd->ctx_lock);
7154 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007155 init_waitqueue_head(&sqd->wait);
7156 return sqd;
7157}
7158
Jens Axboe69fb2132020-09-14 11:16:23 -06007159static void io_sq_thread_unpark(struct io_sq_data *sqd)
7160 __releases(&sqd->lock)
7161{
7162 if (!sqd->thread)
7163 return;
7164 kthread_unpark(sqd->thread);
7165 mutex_unlock(&sqd->lock);
7166}
7167
7168static void io_sq_thread_park(struct io_sq_data *sqd)
7169 __acquires(&sqd->lock)
7170{
7171 if (!sqd->thread)
7172 return;
7173 mutex_lock(&sqd->lock);
7174 kthread_park(sqd->thread);
7175}
7176
Jens Axboe534ca6d2020-09-02 13:52:19 -06007177static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7178{
7179 struct io_sq_data *sqd = ctx->sq_data;
7180
7181 if (sqd) {
7182 if (sqd->thread) {
7183 /*
7184 * We may arrive here from the error branch in
7185 * io_sq_offload_create() where the kthread is created
7186 * without being waked up, thus wake it up now to make
7187 * sure the wait will complete.
7188 */
7189 wake_up_process(sqd->thread);
7190 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007191
7192 io_sq_thread_park(sqd);
7193 }
7194
7195 mutex_lock(&sqd->ctx_lock);
7196 list_del(&ctx->sqd_list);
7197 mutex_unlock(&sqd->ctx_lock);
7198
7199 if (sqd->thread) {
7200 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7201 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007202 }
7203
7204 io_put_sq_data(sqd);
7205 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007206 }
7207}
7208
Jens Axboe6b063142019-01-10 22:13:58 -07007209static void io_finish_async(struct io_ring_ctx *ctx)
7210{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007211 io_sq_thread_stop(ctx);
7212
Jens Axboe561fb042019-10-24 07:25:42 -06007213 if (ctx->io_wq) {
7214 io_wq_destroy(ctx->io_wq);
7215 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007216 }
7217}
7218
7219#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007220/*
7221 * Ensure the UNIX gc is aware of our file set, so we are certain that
7222 * the io_uring can be safely unregistered on process exit, even if we have
7223 * loops in the file referencing.
7224 */
7225static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7226{
7227 struct sock *sk = ctx->ring_sock->sk;
7228 struct scm_fp_list *fpl;
7229 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007230 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007231
Jens Axboe6b063142019-01-10 22:13:58 -07007232 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7233 if (!fpl)
7234 return -ENOMEM;
7235
7236 skb = alloc_skb(0, GFP_KERNEL);
7237 if (!skb) {
7238 kfree(fpl);
7239 return -ENOMEM;
7240 }
7241
7242 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007243
Jens Axboe08a45172019-10-03 08:11:03 -06007244 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007245 fpl->user = get_uid(ctx->user);
7246 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007247 struct file *file = io_file_from_index(ctx, i + offset);
7248
7249 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007250 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007251 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007252 unix_inflight(fpl->user, fpl->fp[nr_files]);
7253 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007254 }
7255
Jens Axboe08a45172019-10-03 08:11:03 -06007256 if (nr_files) {
7257 fpl->max = SCM_MAX_FD;
7258 fpl->count = nr_files;
7259 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007260 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007261 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7262 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007263
Jens Axboe08a45172019-10-03 08:11:03 -06007264 for (i = 0; i < nr_files; i++)
7265 fput(fpl->fp[i]);
7266 } else {
7267 kfree_skb(skb);
7268 kfree(fpl);
7269 }
Jens Axboe6b063142019-01-10 22:13:58 -07007270
7271 return 0;
7272}
7273
7274/*
7275 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7276 * causes regular reference counting to break down. We rely on the UNIX
7277 * garbage collection to take care of this problem for us.
7278 */
7279static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7280{
7281 unsigned left, total;
7282 int ret = 0;
7283
7284 total = 0;
7285 left = ctx->nr_user_files;
7286 while (left) {
7287 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007288
7289 ret = __io_sqe_files_scm(ctx, this_files, total);
7290 if (ret)
7291 break;
7292 left -= this_files;
7293 total += this_files;
7294 }
7295
7296 if (!ret)
7297 return 0;
7298
7299 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007300 struct file *file = io_file_from_index(ctx, total);
7301
7302 if (file)
7303 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007304 total++;
7305 }
7306
7307 return ret;
7308}
7309#else
7310static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7311{
7312 return 0;
7313}
7314#endif
7315
Jens Axboe65e19f52019-10-26 07:20:21 -06007316static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7317 unsigned nr_files)
7318{
7319 int i;
7320
7321 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007322 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007323 unsigned this_files;
7324
7325 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7326 table->files = kcalloc(this_files, sizeof(struct file *),
7327 GFP_KERNEL);
7328 if (!table->files)
7329 break;
7330 nr_files -= this_files;
7331 }
7332
7333 if (i == nr_tables)
7334 return 0;
7335
7336 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007337 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007338 kfree(table->files);
7339 }
7340 return 1;
7341}
7342
Jens Axboe05f3fb32019-12-09 11:22:50 -07007343static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007344{
7345#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007346 struct sock *sock = ctx->ring_sock->sk;
7347 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7348 struct sk_buff *skb;
7349 int i;
7350
7351 __skb_queue_head_init(&list);
7352
7353 /*
7354 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7355 * remove this entry and rearrange the file array.
7356 */
7357 skb = skb_dequeue(head);
7358 while (skb) {
7359 struct scm_fp_list *fp;
7360
7361 fp = UNIXCB(skb).fp;
7362 for (i = 0; i < fp->count; i++) {
7363 int left;
7364
7365 if (fp->fp[i] != file)
7366 continue;
7367
7368 unix_notinflight(fp->user, fp->fp[i]);
7369 left = fp->count - 1 - i;
7370 if (left) {
7371 memmove(&fp->fp[i], &fp->fp[i + 1],
7372 left * sizeof(struct file *));
7373 }
7374 fp->count--;
7375 if (!fp->count) {
7376 kfree_skb(skb);
7377 skb = NULL;
7378 } else {
7379 __skb_queue_tail(&list, skb);
7380 }
7381 fput(file);
7382 file = NULL;
7383 break;
7384 }
7385
7386 if (!file)
7387 break;
7388
7389 __skb_queue_tail(&list, skb);
7390
7391 skb = skb_dequeue(head);
7392 }
7393
7394 if (skb_peek(&list)) {
7395 spin_lock_irq(&head->lock);
7396 while ((skb = __skb_dequeue(&list)) != NULL)
7397 __skb_queue_tail(head, skb);
7398 spin_unlock_irq(&head->lock);
7399 }
7400#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007401 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007402#endif
7403}
7404
Jens Axboe05f3fb32019-12-09 11:22:50 -07007405struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007406 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007407 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007408};
7409
Jens Axboe4a38aed22020-05-14 17:21:15 -06007410static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007411{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007412 struct fixed_file_data *file_data = ref_node->file_data;
7413 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007414 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007415
7416 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007417 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007418 io_ring_file_put(ctx, pfile->file);
7419 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007420 }
7421
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007422 spin_lock(&file_data->lock);
7423 list_del(&ref_node->node);
7424 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007425
Xiaoguang Wang05589552020-03-31 14:05:18 +08007426 percpu_ref_exit(&ref_node->refs);
7427 kfree(ref_node);
7428 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007429}
7430
Jens Axboe4a38aed22020-05-14 17:21:15 -06007431static void io_file_put_work(struct work_struct *work)
7432{
7433 struct io_ring_ctx *ctx;
7434 struct llist_node *node;
7435
7436 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7437 node = llist_del_all(&ctx->file_put_llist);
7438
7439 while (node) {
7440 struct fixed_file_ref_node *ref_node;
7441 struct llist_node *next = node->next;
7442
7443 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7444 __io_file_put_work(ref_node);
7445 node = next;
7446 }
7447}
7448
Jens Axboe05f3fb32019-12-09 11:22:50 -07007449static void io_file_data_ref_zero(struct percpu_ref *ref)
7450{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007451 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007452 struct io_ring_ctx *ctx;
7453 bool first_add;
7454 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007455
Xiaoguang Wang05589552020-03-31 14:05:18 +08007456 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007457 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007458
Jens Axboe4a38aed22020-05-14 17:21:15 -06007459 if (percpu_ref_is_dying(&ctx->file_data->refs))
7460 delay = 0;
7461
7462 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7463 if (!delay)
7464 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7465 else if (first_add)
7466 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007467}
7468
7469static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7470 struct io_ring_ctx *ctx)
7471{
7472 struct fixed_file_ref_node *ref_node;
7473
7474 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7475 if (!ref_node)
7476 return ERR_PTR(-ENOMEM);
7477
7478 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7479 0, GFP_KERNEL)) {
7480 kfree(ref_node);
7481 return ERR_PTR(-ENOMEM);
7482 }
7483 INIT_LIST_HEAD(&ref_node->node);
7484 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007485 ref_node->file_data = ctx->file_data;
7486 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007487}
7488
7489static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7490{
7491 percpu_ref_exit(&ref_node->refs);
7492 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007493}
7494
7495static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7496 unsigned nr_args)
7497{
7498 __s32 __user *fds = (__s32 __user *) arg;
7499 unsigned nr_tables;
7500 struct file *file;
7501 int fd, ret = 0;
7502 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007503 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007504
7505 if (ctx->file_data)
7506 return -EBUSY;
7507 if (!nr_args)
7508 return -EINVAL;
7509 if (nr_args > IORING_MAX_FIXED_FILES)
7510 return -EMFILE;
7511
7512 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7513 if (!ctx->file_data)
7514 return -ENOMEM;
7515 ctx->file_data->ctx = ctx;
7516 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007517 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007518 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007519
7520 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7521 ctx->file_data->table = kcalloc(nr_tables,
7522 sizeof(struct fixed_file_table),
7523 GFP_KERNEL);
7524 if (!ctx->file_data->table) {
7525 kfree(ctx->file_data);
7526 ctx->file_data = NULL;
7527 return -ENOMEM;
7528 }
7529
Xiaoguang Wang05589552020-03-31 14:05:18 +08007530 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007531 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7532 kfree(ctx->file_data->table);
7533 kfree(ctx->file_data);
7534 ctx->file_data = NULL;
7535 return -ENOMEM;
7536 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007537
7538 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7539 percpu_ref_exit(&ctx->file_data->refs);
7540 kfree(ctx->file_data->table);
7541 kfree(ctx->file_data);
7542 ctx->file_data = NULL;
7543 return -ENOMEM;
7544 }
7545
7546 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7547 struct fixed_file_table *table;
7548 unsigned index;
7549
7550 ret = -EFAULT;
7551 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7552 break;
7553 /* allow sparse sets */
7554 if (fd == -1) {
7555 ret = 0;
7556 continue;
7557 }
7558
7559 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7560 index = i & IORING_FILE_TABLE_MASK;
7561 file = fget(fd);
7562
7563 ret = -EBADF;
7564 if (!file)
7565 break;
7566
7567 /*
7568 * Don't allow io_uring instances to be registered. If UNIX
7569 * isn't enabled, then this causes a reference cycle and this
7570 * instance can never get freed. If UNIX is enabled we'll
7571 * handle it just fine, but there's still no point in allowing
7572 * a ring fd as it doesn't support regular read/write anyway.
7573 */
7574 if (file->f_op == &io_uring_fops) {
7575 fput(file);
7576 break;
7577 }
7578 ret = 0;
7579 table->files[index] = file;
7580 }
7581
7582 if (ret) {
7583 for (i = 0; i < ctx->nr_user_files; i++) {
7584 file = io_file_from_index(ctx, i);
7585 if (file)
7586 fput(file);
7587 }
7588 for (i = 0; i < nr_tables; i++)
7589 kfree(ctx->file_data->table[i].files);
7590
Yang Yingliang667e57d2020-07-10 14:14:20 +00007591 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007592 kfree(ctx->file_data->table);
7593 kfree(ctx->file_data);
7594 ctx->file_data = NULL;
7595 ctx->nr_user_files = 0;
7596 return ret;
7597 }
7598
7599 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007600 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007601 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007602 return ret;
7603 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007604
Xiaoguang Wang05589552020-03-31 14:05:18 +08007605 ref_node = alloc_fixed_file_ref_node(ctx);
7606 if (IS_ERR(ref_node)) {
7607 io_sqe_files_unregister(ctx);
7608 return PTR_ERR(ref_node);
7609 }
7610
7611 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007612 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007613 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007614 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007615 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007616 return ret;
7617}
7618
Jens Axboec3a31e62019-10-03 13:59:56 -06007619static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7620 int index)
7621{
7622#if defined(CONFIG_UNIX)
7623 struct sock *sock = ctx->ring_sock->sk;
7624 struct sk_buff_head *head = &sock->sk_receive_queue;
7625 struct sk_buff *skb;
7626
7627 /*
7628 * See if we can merge this file into an existing skb SCM_RIGHTS
7629 * file set. If there's no room, fall back to allocating a new skb
7630 * and filling it in.
7631 */
7632 spin_lock_irq(&head->lock);
7633 skb = skb_peek(head);
7634 if (skb) {
7635 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7636
7637 if (fpl->count < SCM_MAX_FD) {
7638 __skb_unlink(skb, head);
7639 spin_unlock_irq(&head->lock);
7640 fpl->fp[fpl->count] = get_file(file);
7641 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7642 fpl->count++;
7643 spin_lock_irq(&head->lock);
7644 __skb_queue_head(head, skb);
7645 } else {
7646 skb = NULL;
7647 }
7648 }
7649 spin_unlock_irq(&head->lock);
7650
7651 if (skb) {
7652 fput(file);
7653 return 0;
7654 }
7655
7656 return __io_sqe_files_scm(ctx, 1, index);
7657#else
7658 return 0;
7659#endif
7660}
7661
Hillf Dantona5318d32020-03-23 17:47:15 +08007662static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007663 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007664{
Hillf Dantona5318d32020-03-23 17:47:15 +08007665 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007666 struct percpu_ref *refs = data->cur_refs;
7667 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007668
Jens Axboe05f3fb32019-12-09 11:22:50 -07007669 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007670 if (!pfile)
7671 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007672
Xiaoguang Wang05589552020-03-31 14:05:18 +08007673 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007674 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007675 list_add(&pfile->list, &ref_node->file_list);
7676
Hillf Dantona5318d32020-03-23 17:47:15 +08007677 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007678}
7679
7680static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7681 struct io_uring_files_update *up,
7682 unsigned nr_args)
7683{
7684 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007685 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007686 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007687 __s32 __user *fds;
7688 int fd, i, err;
7689 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007690 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007691
Jens Axboe05f3fb32019-12-09 11:22:50 -07007692 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007693 return -EOVERFLOW;
7694 if (done > ctx->nr_user_files)
7695 return -EINVAL;
7696
Xiaoguang Wang05589552020-03-31 14:05:18 +08007697 ref_node = alloc_fixed_file_ref_node(ctx);
7698 if (IS_ERR(ref_node))
7699 return PTR_ERR(ref_node);
7700
Jens Axboec3a31e62019-10-03 13:59:56 -06007701 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007702 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007703 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007704 struct fixed_file_table *table;
7705 unsigned index;
7706
Jens Axboec3a31e62019-10-03 13:59:56 -06007707 err = 0;
7708 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7709 err = -EFAULT;
7710 break;
7711 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007712 i = array_index_nospec(up->offset, ctx->nr_user_files);
7713 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007714 index = i & IORING_FILE_TABLE_MASK;
7715 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007716 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007717 err = io_queue_file_removal(data, file);
7718 if (err)
7719 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007720 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007721 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007722 }
7723 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007724 file = fget(fd);
7725 if (!file) {
7726 err = -EBADF;
7727 break;
7728 }
7729 /*
7730 * Don't allow io_uring instances to be registered. If
7731 * UNIX isn't enabled, then this causes a reference
7732 * cycle and this instance can never get freed. If UNIX
7733 * is enabled we'll handle it just fine, but there's
7734 * still no point in allowing a ring fd as it doesn't
7735 * support regular read/write anyway.
7736 */
7737 if (file->f_op == &io_uring_fops) {
7738 fput(file);
7739 err = -EBADF;
7740 break;
7741 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007742 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007743 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007744 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007745 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007746 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007747 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007748 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007749 }
7750 nr_args--;
7751 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007752 up->offset++;
7753 }
7754
Xiaoguang Wang05589552020-03-31 14:05:18 +08007755 if (needs_switch) {
7756 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007757 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007758 list_add(&ref_node->node, &data->ref_list);
7759 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007760 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007761 percpu_ref_get(&ctx->file_data->refs);
7762 } else
7763 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007764
7765 return done ? done : err;
7766}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007767
Jens Axboe05f3fb32019-12-09 11:22:50 -07007768static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7769 unsigned nr_args)
7770{
7771 struct io_uring_files_update up;
7772
7773 if (!ctx->file_data)
7774 return -ENXIO;
7775 if (!nr_args)
7776 return -EINVAL;
7777 if (copy_from_user(&up, arg, sizeof(up)))
7778 return -EFAULT;
7779 if (up.resv)
7780 return -EINVAL;
7781
7782 return __io_sqe_files_update(ctx, &up, nr_args);
7783}
Jens Axboec3a31e62019-10-03 13:59:56 -06007784
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007785static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007786{
7787 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7788
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007789 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007790 io_put_req(req);
7791}
7792
Pavel Begunkov24369c22020-01-28 03:15:48 +03007793static int io_init_wq_offload(struct io_ring_ctx *ctx,
7794 struct io_uring_params *p)
7795{
7796 struct io_wq_data data;
7797 struct fd f;
7798 struct io_ring_ctx *ctx_attach;
7799 unsigned int concurrency;
7800 int ret = 0;
7801
7802 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007803 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007804 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007805
7806 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7807 /* Do QD, or 4 * CPUS, whatever is smallest */
7808 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7809
7810 ctx->io_wq = io_wq_create(concurrency, &data);
7811 if (IS_ERR(ctx->io_wq)) {
7812 ret = PTR_ERR(ctx->io_wq);
7813 ctx->io_wq = NULL;
7814 }
7815 return ret;
7816 }
7817
7818 f = fdget(p->wq_fd);
7819 if (!f.file)
7820 return -EBADF;
7821
7822 if (f.file->f_op != &io_uring_fops) {
7823 ret = -EINVAL;
7824 goto out_fput;
7825 }
7826
7827 ctx_attach = f.file->private_data;
7828 /* @io_wq is protected by holding the fd */
7829 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7830 ret = -EINVAL;
7831 goto out_fput;
7832 }
7833
7834 ctx->io_wq = ctx_attach->io_wq;
7835out_fput:
7836 fdput(f);
7837 return ret;
7838}
7839
Jens Axboe0f212202020-09-13 13:09:39 -06007840static int io_uring_alloc_task_context(struct task_struct *task)
7841{
7842 struct io_uring_task *tctx;
7843
7844 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7845 if (unlikely(!tctx))
7846 return -ENOMEM;
7847
7848 xa_init(&tctx->xa);
7849 init_waitqueue_head(&tctx->wait);
7850 tctx->last = NULL;
7851 tctx->in_idle = 0;
7852 atomic_long_set(&tctx->req_issue, 0);
7853 atomic_long_set(&tctx->req_complete, 0);
7854 task->io_uring = tctx;
7855 return 0;
7856}
7857
7858void __io_uring_free(struct task_struct *tsk)
7859{
7860 struct io_uring_task *tctx = tsk->io_uring;
7861
7862 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7863 xa_destroy(&tctx->xa);
7864 kfree(tctx);
7865 tsk->io_uring = NULL;
7866}
7867
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007868static int io_sq_offload_create(struct io_ring_ctx *ctx,
7869 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007870{
7871 int ret;
7872
Jens Axboe6c271ce2019-01-10 11:22:30 -07007873 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007874 struct io_sq_data *sqd;
7875
Jens Axboe3ec482d2019-04-08 10:51:01 -06007876 ret = -EPERM;
7877 if (!capable(CAP_SYS_ADMIN))
7878 goto err;
7879
Jens Axboe534ca6d2020-09-02 13:52:19 -06007880 sqd = io_get_sq_data(p);
7881 if (IS_ERR(sqd)) {
7882 ret = PTR_ERR(sqd);
7883 goto err;
7884 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007885
Jens Axboe534ca6d2020-09-02 13:52:19 -06007886 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007887 io_sq_thread_park(sqd);
7888 mutex_lock(&sqd->ctx_lock);
7889 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7890 mutex_unlock(&sqd->ctx_lock);
7891 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007892
Jens Axboe917257d2019-04-13 09:28:55 -06007893 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7894 if (!ctx->sq_thread_idle)
7895 ctx->sq_thread_idle = HZ;
7896
Jens Axboeaa061652020-09-02 14:50:27 -06007897 if (sqd->thread)
7898 goto done;
7899
Jens Axboe6c271ce2019-01-10 11:22:30 -07007900 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007901 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007902
Jens Axboe917257d2019-04-13 09:28:55 -06007903 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007904 if (cpu >= nr_cpu_ids)
7905 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007906 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007907 goto err;
7908
Jens Axboe69fb2132020-09-14 11:16:23 -06007909 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007910 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007911 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007912 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007913 "io_uring-sq");
7914 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007915 if (IS_ERR(sqd->thread)) {
7916 ret = PTR_ERR(sqd->thread);
7917 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007918 goto err;
7919 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007920 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007921 if (ret)
7922 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007923 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7924 /* Can't have SQ_AFF without SQPOLL */
7925 ret = -EINVAL;
7926 goto err;
7927 }
7928
Jens Axboeaa061652020-09-02 14:50:27 -06007929done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007930 ret = io_init_wq_offload(ctx, p);
7931 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007932 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007933
7934 return 0;
7935err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007936 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007937 return ret;
7938}
7939
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007940static void io_sq_offload_start(struct io_ring_ctx *ctx)
7941{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007942 struct io_sq_data *sqd = ctx->sq_data;
7943
7944 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7945 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007946}
7947
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007948static inline void __io_unaccount_mem(struct user_struct *user,
7949 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007950{
7951 atomic_long_sub(nr_pages, &user->locked_vm);
7952}
7953
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007954static inline int __io_account_mem(struct user_struct *user,
7955 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007956{
7957 unsigned long page_limit, cur_pages, new_pages;
7958
7959 /* Don't allow more pages than we can safely lock */
7960 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7961
7962 do {
7963 cur_pages = atomic_long_read(&user->locked_vm);
7964 new_pages = cur_pages + nr_pages;
7965 if (new_pages > page_limit)
7966 return -ENOMEM;
7967 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7968 new_pages) != cur_pages);
7969
7970 return 0;
7971}
7972
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007973static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7974 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007975{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007976 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007977 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007978
Jens Axboe2aede0e2020-09-14 10:45:53 -06007979 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007980 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007981 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007982 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007983 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007984 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007985}
7986
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007987static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7988 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007989{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007990 int ret;
7991
7992 if (ctx->limit_mem) {
7993 ret = __io_account_mem(ctx->user, nr_pages);
7994 if (ret)
7995 return ret;
7996 }
7997
Jens Axboe2aede0e2020-09-14 10:45:53 -06007998 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007999 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008000 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008001 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008002 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008003 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008004
8005 return 0;
8006}
8007
Jens Axboe2b188cc2019-01-07 10:46:33 -07008008static void io_mem_free(void *ptr)
8009{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008010 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008011
Mark Rutland52e04ef2019-04-30 17:30:21 +01008012 if (!ptr)
8013 return;
8014
8015 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008016 if (put_page_testzero(page))
8017 free_compound_page(page);
8018}
8019
8020static void *io_mem_alloc(size_t size)
8021{
8022 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8023 __GFP_NORETRY;
8024
8025 return (void *) __get_free_pages(gfp_flags, get_order(size));
8026}
8027
Hristo Venev75b28af2019-08-26 17:23:46 +00008028static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8029 size_t *sq_offset)
8030{
8031 struct io_rings *rings;
8032 size_t off, sq_array_size;
8033
8034 off = struct_size(rings, cqes, cq_entries);
8035 if (off == SIZE_MAX)
8036 return SIZE_MAX;
8037
8038#ifdef CONFIG_SMP
8039 off = ALIGN(off, SMP_CACHE_BYTES);
8040 if (off == 0)
8041 return SIZE_MAX;
8042#endif
8043
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008044 if (sq_offset)
8045 *sq_offset = off;
8046
Hristo Venev75b28af2019-08-26 17:23:46 +00008047 sq_array_size = array_size(sizeof(u32), sq_entries);
8048 if (sq_array_size == SIZE_MAX)
8049 return SIZE_MAX;
8050
8051 if (check_add_overflow(off, sq_array_size, &off))
8052 return SIZE_MAX;
8053
Hristo Venev75b28af2019-08-26 17:23:46 +00008054 return off;
8055}
8056
Jens Axboe2b188cc2019-01-07 10:46:33 -07008057static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8058{
Hristo Venev75b28af2019-08-26 17:23:46 +00008059 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008060
Hristo Venev75b28af2019-08-26 17:23:46 +00008061 pages = (size_t)1 << get_order(
8062 rings_size(sq_entries, cq_entries, NULL));
8063 pages += (size_t)1 << get_order(
8064 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008065
Hristo Venev75b28af2019-08-26 17:23:46 +00008066 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008067}
8068
Jens Axboeedafcce2019-01-09 09:16:05 -07008069static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8070{
8071 int i, j;
8072
8073 if (!ctx->user_bufs)
8074 return -ENXIO;
8075
8076 for (i = 0; i < ctx->nr_user_bufs; i++) {
8077 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8078
8079 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008080 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008081
Jens Axboede293932020-09-17 16:19:16 -06008082 if (imu->acct_pages)
8083 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008084 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008085 imu->nr_bvecs = 0;
8086 }
8087
8088 kfree(ctx->user_bufs);
8089 ctx->user_bufs = NULL;
8090 ctx->nr_user_bufs = 0;
8091 return 0;
8092}
8093
8094static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8095 void __user *arg, unsigned index)
8096{
8097 struct iovec __user *src;
8098
8099#ifdef CONFIG_COMPAT
8100 if (ctx->compat) {
8101 struct compat_iovec __user *ciovs;
8102 struct compat_iovec ciov;
8103
8104 ciovs = (struct compat_iovec __user *) arg;
8105 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8106 return -EFAULT;
8107
Jens Axboed55e5f52019-12-11 16:12:15 -07008108 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008109 dst->iov_len = ciov.iov_len;
8110 return 0;
8111 }
8112#endif
8113 src = (struct iovec __user *) arg;
8114 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8115 return -EFAULT;
8116 return 0;
8117}
8118
Jens Axboede293932020-09-17 16:19:16 -06008119/*
8120 * Not super efficient, but this is just a registration time. And we do cache
8121 * the last compound head, so generally we'll only do a full search if we don't
8122 * match that one.
8123 *
8124 * We check if the given compound head page has already been accounted, to
8125 * avoid double accounting it. This allows us to account the full size of the
8126 * page, not just the constituent pages of a huge page.
8127 */
8128static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8129 int nr_pages, struct page *hpage)
8130{
8131 int i, j;
8132
8133 /* check current page array */
8134 for (i = 0; i < nr_pages; i++) {
8135 if (!PageCompound(pages[i]))
8136 continue;
8137 if (compound_head(pages[i]) == hpage)
8138 return true;
8139 }
8140
8141 /* check previously registered pages */
8142 for (i = 0; i < ctx->nr_user_bufs; i++) {
8143 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8144
8145 for (j = 0; j < imu->nr_bvecs; j++) {
8146 if (!PageCompound(imu->bvec[j].bv_page))
8147 continue;
8148 if (compound_head(imu->bvec[j].bv_page) == hpage)
8149 return true;
8150 }
8151 }
8152
8153 return false;
8154}
8155
8156static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8157 int nr_pages, struct io_mapped_ubuf *imu,
8158 struct page **last_hpage)
8159{
8160 int i, ret;
8161
8162 for (i = 0; i < nr_pages; i++) {
8163 if (!PageCompound(pages[i])) {
8164 imu->acct_pages++;
8165 } else {
8166 struct page *hpage;
8167
8168 hpage = compound_head(pages[i]);
8169 if (hpage == *last_hpage)
8170 continue;
8171 *last_hpage = hpage;
8172 if (headpage_already_acct(ctx, pages, i, hpage))
8173 continue;
8174 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8175 }
8176 }
8177
8178 if (!imu->acct_pages)
8179 return 0;
8180
8181 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8182 if (ret)
8183 imu->acct_pages = 0;
8184 return ret;
8185}
8186
Jens Axboeedafcce2019-01-09 09:16:05 -07008187static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8188 unsigned nr_args)
8189{
8190 struct vm_area_struct **vmas = NULL;
8191 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008192 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008193 int i, j, got_pages = 0;
8194 int ret = -EINVAL;
8195
8196 if (ctx->user_bufs)
8197 return -EBUSY;
8198 if (!nr_args || nr_args > UIO_MAXIOV)
8199 return -EINVAL;
8200
8201 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8202 GFP_KERNEL);
8203 if (!ctx->user_bufs)
8204 return -ENOMEM;
8205
8206 for (i = 0; i < nr_args; i++) {
8207 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8208 unsigned long off, start, end, ubuf;
8209 int pret, nr_pages;
8210 struct iovec iov;
8211 size_t size;
8212
8213 ret = io_copy_iov(ctx, &iov, arg, i);
8214 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008215 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008216
8217 /*
8218 * Don't impose further limits on the size and buffer
8219 * constraints here, we'll -EINVAL later when IO is
8220 * submitted if they are wrong.
8221 */
8222 ret = -EFAULT;
8223 if (!iov.iov_base || !iov.iov_len)
8224 goto err;
8225
8226 /* arbitrary limit, but we need something */
8227 if (iov.iov_len > SZ_1G)
8228 goto err;
8229
8230 ubuf = (unsigned long) iov.iov_base;
8231 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8232 start = ubuf >> PAGE_SHIFT;
8233 nr_pages = end - start;
8234
Jens Axboeedafcce2019-01-09 09:16:05 -07008235 ret = 0;
8236 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008237 kvfree(vmas);
8238 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008239 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008240 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008241 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008242 sizeof(struct vm_area_struct *),
8243 GFP_KERNEL);
8244 if (!pages || !vmas) {
8245 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008246 goto err;
8247 }
8248 got_pages = nr_pages;
8249 }
8250
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008251 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008252 GFP_KERNEL);
8253 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008254 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008255 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008256
8257 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008258 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008259 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008260 FOLL_WRITE | FOLL_LONGTERM,
8261 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008262 if (pret == nr_pages) {
8263 /* don't support file backed memory */
8264 for (j = 0; j < nr_pages; j++) {
8265 struct vm_area_struct *vma = vmas[j];
8266
8267 if (vma->vm_file &&
8268 !is_file_hugepages(vma->vm_file)) {
8269 ret = -EOPNOTSUPP;
8270 break;
8271 }
8272 }
8273 } else {
8274 ret = pret < 0 ? pret : -EFAULT;
8275 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008276 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008277 if (ret) {
8278 /*
8279 * if we did partial map, or found file backed vmas,
8280 * release any pages we did get
8281 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008282 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008283 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008284 kvfree(imu->bvec);
8285 goto err;
8286 }
8287
8288 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8289 if (ret) {
8290 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008291 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008292 goto err;
8293 }
8294
8295 off = ubuf & ~PAGE_MASK;
8296 size = iov.iov_len;
8297 for (j = 0; j < nr_pages; j++) {
8298 size_t vec_len;
8299
8300 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8301 imu->bvec[j].bv_page = pages[j];
8302 imu->bvec[j].bv_len = vec_len;
8303 imu->bvec[j].bv_offset = off;
8304 off = 0;
8305 size -= vec_len;
8306 }
8307 /* store original address for later verification */
8308 imu->ubuf = ubuf;
8309 imu->len = iov.iov_len;
8310 imu->nr_bvecs = nr_pages;
8311
8312 ctx->nr_user_bufs++;
8313 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008314 kvfree(pages);
8315 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008316 return 0;
8317err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008318 kvfree(pages);
8319 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008320 io_sqe_buffer_unregister(ctx);
8321 return ret;
8322}
8323
Jens Axboe9b402842019-04-11 11:45:41 -06008324static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8325{
8326 __s32 __user *fds = arg;
8327 int fd;
8328
8329 if (ctx->cq_ev_fd)
8330 return -EBUSY;
8331
8332 if (copy_from_user(&fd, fds, sizeof(*fds)))
8333 return -EFAULT;
8334
8335 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8336 if (IS_ERR(ctx->cq_ev_fd)) {
8337 int ret = PTR_ERR(ctx->cq_ev_fd);
8338 ctx->cq_ev_fd = NULL;
8339 return ret;
8340 }
8341
8342 return 0;
8343}
8344
8345static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8346{
8347 if (ctx->cq_ev_fd) {
8348 eventfd_ctx_put(ctx->cq_ev_fd);
8349 ctx->cq_ev_fd = NULL;
8350 return 0;
8351 }
8352
8353 return -ENXIO;
8354}
8355
Jens Axboe5a2e7452020-02-23 16:23:11 -07008356static int __io_destroy_buffers(int id, void *p, void *data)
8357{
8358 struct io_ring_ctx *ctx = data;
8359 struct io_buffer *buf = p;
8360
Jens Axboe067524e2020-03-02 16:32:28 -07008361 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008362 return 0;
8363}
8364
8365static void io_destroy_buffers(struct io_ring_ctx *ctx)
8366{
8367 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8368 idr_destroy(&ctx->io_buffer_idr);
8369}
8370
Jens Axboe2b188cc2019-01-07 10:46:33 -07008371static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8372{
Jens Axboe6b063142019-01-10 22:13:58 -07008373 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008374 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008375
8376 if (ctx->sqo_task) {
8377 put_task_struct(ctx->sqo_task);
8378 ctx->sqo_task = NULL;
8379 mmdrop(ctx->mm_account);
8380 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008381 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008382
Dennis Zhou91d8f512020-09-16 13:41:05 -07008383#ifdef CONFIG_BLK_CGROUP
8384 if (ctx->sqo_blkcg_css)
8385 css_put(ctx->sqo_blkcg_css);
8386#endif
8387
Jens Axboe6b063142019-01-10 22:13:58 -07008388 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008389 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008390 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008391 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008392
Jens Axboe2b188cc2019-01-07 10:46:33 -07008393#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008394 if (ctx->ring_sock) {
8395 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008396 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008397 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008398#endif
8399
Hristo Venev75b28af2019-08-26 17:23:46 +00008400 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008401 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008402
8403 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008404 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008405 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008406 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008407 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008408 kfree(ctx);
8409}
8410
8411static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8412{
8413 struct io_ring_ctx *ctx = file->private_data;
8414 __poll_t mask = 0;
8415
8416 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008417 /*
8418 * synchronizes with barrier from wq_has_sleeper call in
8419 * io_commit_cqring
8420 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008421 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008422 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008423 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008424 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008425 mask |= EPOLLIN | EPOLLRDNORM;
8426
8427 return mask;
8428}
8429
8430static int io_uring_fasync(int fd, struct file *file, int on)
8431{
8432 struct io_ring_ctx *ctx = file->private_data;
8433
8434 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8435}
8436
Jens Axboe071698e2020-01-28 10:04:42 -07008437static int io_remove_personalities(int id, void *p, void *data)
8438{
8439 struct io_ring_ctx *ctx = data;
8440 const struct cred *cred;
8441
8442 cred = idr_remove(&ctx->personality_idr, id);
8443 if (cred)
8444 put_cred(cred);
8445 return 0;
8446}
8447
Jens Axboe85faa7b2020-04-09 18:14:00 -06008448static void io_ring_exit_work(struct work_struct *work)
8449{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008450 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8451 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008452
Jens Axboe56952e92020-06-17 15:00:04 -06008453 /*
8454 * If we're doing polled IO and end up having requests being
8455 * submitted async (out-of-line), then completions can come in while
8456 * we're waiting for refs to drop. We need to reap these manually,
8457 * as nobody else will be looking for them.
8458 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008459 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008460 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008461 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008462 io_iopoll_try_reap_events(ctx);
8463 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008464 io_ring_ctx_free(ctx);
8465}
8466
Jens Axboe2b188cc2019-01-07 10:46:33 -07008467static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8468{
8469 mutex_lock(&ctx->uring_lock);
8470 percpu_ref_kill(&ctx->refs);
8471 mutex_unlock(&ctx->uring_lock);
8472
Jens Axboef3606e32020-09-22 08:18:24 -06008473 io_kill_timeouts(ctx, NULL);
8474 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008475
8476 if (ctx->io_wq)
8477 io_wq_cancel_all(ctx->io_wq);
8478
Jens Axboe15dff282019-11-13 09:09:23 -07008479 /* if we failed setting up the ctx, we might not have any rings */
8480 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008481 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008482 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008483 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008484
8485 /*
8486 * Do this upfront, so we won't have a grace period where the ring
8487 * is closed but resources aren't reaped yet. This can cause
8488 * spurious failure in setting up a new ring.
8489 */
Jens Axboe760618f2020-07-24 12:53:31 -06008490 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8491 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008492
Jens Axboe85faa7b2020-04-09 18:14:00 -06008493 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008494 /*
8495 * Use system_unbound_wq to avoid spawning tons of event kworkers
8496 * if we're exiting a ton of rings at the same time. It just adds
8497 * noise and overhead, there's no discernable change in runtime
8498 * over using system_wq.
8499 */
8500 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008501}
8502
8503static int io_uring_release(struct inode *inode, struct file *file)
8504{
8505 struct io_ring_ctx *ctx = file->private_data;
8506
8507 file->private_data = NULL;
8508 io_ring_ctx_wait_and_kill(ctx);
8509 return 0;
8510}
8511
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008512static bool io_wq_files_match(struct io_wq_work *work, void *data)
8513{
8514 struct files_struct *files = data;
8515
Jens Axboe0f212202020-09-13 13:09:39 -06008516 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008517}
8518
Jens Axboef254ac02020-08-12 17:33:30 -06008519/*
8520 * Returns true if 'preq' is the link parent of 'req'
8521 */
8522static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8523{
8524 struct io_kiocb *link;
8525
8526 if (!(preq->flags & REQ_F_LINK_HEAD))
8527 return false;
8528
8529 list_for_each_entry(link, &preq->link_list, link_list) {
8530 if (link == req)
8531 return true;
8532 }
8533
8534 return false;
8535}
8536
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008537static bool io_match_link_files(struct io_kiocb *req,
8538 struct files_struct *files)
8539{
8540 struct io_kiocb *link;
8541
8542 if (io_match_files(req, files))
8543 return true;
8544 if (req->flags & REQ_F_LINK_HEAD) {
8545 list_for_each_entry(link, &req->link_list, link_list) {
8546 if (io_match_files(link, files))
8547 return true;
8548 }
8549 }
8550 return false;
8551}
8552
Jens Axboef254ac02020-08-12 17:33:30 -06008553/*
8554 * We're looking to cancel 'req' because it's holding on to our files, but
8555 * 'req' could be a link to another request. See if it is, and cancel that
8556 * parent request if so.
8557 */
8558static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8559{
8560 struct hlist_node *tmp;
8561 struct io_kiocb *preq;
8562 bool found = false;
8563 int i;
8564
8565 spin_lock_irq(&ctx->completion_lock);
8566 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8567 struct hlist_head *list;
8568
8569 list = &ctx->cancel_hash[i];
8570 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8571 found = io_match_link(preq, req);
8572 if (found) {
8573 io_poll_remove_one(preq);
8574 break;
8575 }
8576 }
8577 }
8578 spin_unlock_irq(&ctx->completion_lock);
8579 return found;
8580}
8581
8582static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8583 struct io_kiocb *req)
8584{
8585 struct io_kiocb *preq;
8586 bool found = false;
8587
8588 spin_lock_irq(&ctx->completion_lock);
8589 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8590 found = io_match_link(preq, req);
8591 if (found) {
8592 __io_timeout_cancel(preq);
8593 break;
8594 }
8595 }
8596 spin_unlock_irq(&ctx->completion_lock);
8597 return found;
8598}
8599
Jens Axboeb711d4e2020-08-16 08:23:05 -07008600static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8601{
8602 return io_match_link(container_of(work, struct io_kiocb, work), data);
8603}
8604
8605static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8606{
8607 enum io_wq_cancel cret;
8608
8609 /* cancel this particular work, if it's running */
8610 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8611 if (cret != IO_WQ_CANCEL_NOTFOUND)
8612 return;
8613
8614 /* find links that hold this pending, cancel those */
8615 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8616 if (cret != IO_WQ_CANCEL_NOTFOUND)
8617 return;
8618
8619 /* if we have a poll link holding this pending, cancel that */
8620 if (io_poll_remove_link(ctx, req))
8621 return;
8622
8623 /* final option, timeout link is holding this req pending */
8624 io_timeout_remove_link(ctx, req);
8625}
8626
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008627static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8628 struct files_struct *files)
8629{
8630 struct io_defer_entry *de = NULL;
8631 LIST_HEAD(list);
8632
8633 spin_lock_irq(&ctx->completion_lock);
8634 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008635 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008636 list_cut_position(&list, &ctx->defer_list, &de->list);
8637 break;
8638 }
8639 }
8640 spin_unlock_irq(&ctx->completion_lock);
8641
8642 while (!list_empty(&list)) {
8643 de = list_first_entry(&list, struct io_defer_entry, list);
8644 list_del_init(&de->list);
8645 req_set_fail_links(de->req);
8646 io_put_req(de->req);
8647 io_req_complete(de->req, -ECANCELED);
8648 kfree(de);
8649 }
8650}
8651
Jens Axboe76e1b642020-09-26 15:05:03 -06008652/*
8653 * Returns true if we found and killed one or more files pinning requests
8654 */
8655static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008656 struct files_struct *files)
8657{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008658 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008659 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008660
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008661 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008662 /* cancel all at once, should be faster than doing it one by one*/
8663 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8664
Jens Axboefcb323c2019-10-24 12:39:47 -06008665 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008666 struct io_kiocb *cancel_req = NULL, *req;
8667 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008668
8669 spin_lock_irq(&ctx->inflight_lock);
8670 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008671 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008672 continue;
8673 /* req is being completed, ignore */
8674 if (!refcount_inc_not_zero(&req->refs))
8675 continue;
8676 cancel_req = req;
8677 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008678 }
Jens Axboe768134d2019-11-10 20:30:53 -07008679 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008680 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008681 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008682 spin_unlock_irq(&ctx->inflight_lock);
8683
Jens Axboe768134d2019-11-10 20:30:53 -07008684 /* We need to keep going until we don't find a matching req */
8685 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008686 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008687 /* cancel this request, or head link requests */
8688 io_attempt_cancel(ctx, cancel_req);
8689 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008690 /* cancellations _may_ trigger task work */
8691 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008692 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008693 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008694 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008695
8696 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008697}
8698
Pavel Begunkov801dd572020-06-15 10:33:14 +03008699static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008700{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008701 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8702 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008703
Jens Axboef3606e32020-09-22 08:18:24 -06008704 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008705}
8706
Jens Axboe0f212202020-09-13 13:09:39 -06008707static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8708 struct task_struct *task,
8709 struct files_struct *files)
8710{
8711 bool ret;
8712
8713 ret = io_uring_cancel_files(ctx, files);
8714 if (!files) {
8715 enum io_wq_cancel cret;
8716
8717 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8718 if (cret != IO_WQ_CANCEL_NOTFOUND)
8719 ret = true;
8720
8721 /* SQPOLL thread does its own polling */
8722 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8723 while (!list_empty_careful(&ctx->iopoll_list)) {
8724 io_iopoll_try_reap_events(ctx);
8725 ret = true;
8726 }
8727 }
8728
8729 ret |= io_poll_remove_all(ctx, task);
8730 ret |= io_kill_timeouts(ctx, task);
8731 }
8732
8733 return ret;
8734}
8735
8736/*
8737 * We need to iteratively cancel requests, in case a request has dependent
8738 * hard links. These persist even for failure of cancelations, hence keep
8739 * looping until none are found.
8740 */
8741static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8742 struct files_struct *files)
8743{
8744 struct task_struct *task = current;
8745
Jens Axboe534ca6d2020-09-02 13:52:19 -06008746 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8747 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008748
8749 io_cqring_overflow_flush(ctx, true, task, files);
8750
8751 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8752 io_run_task_work();
8753 cond_resched();
8754 }
8755}
8756
8757/*
8758 * Note that this task has used io_uring. We use it for cancelation purposes.
8759 */
8760static int io_uring_add_task_file(struct file *file)
8761{
8762 if (unlikely(!current->io_uring)) {
8763 int ret;
8764
8765 ret = io_uring_alloc_task_context(current);
8766 if (unlikely(ret))
8767 return ret;
8768 }
8769 if (current->io_uring->last != file) {
8770 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8771 void *old;
8772
8773 rcu_read_lock();
8774 old = xas_load(&xas);
8775 if (old != file) {
8776 get_file(file);
8777 xas_lock(&xas);
8778 xas_store(&xas, file);
8779 xas_unlock(&xas);
8780 }
8781 rcu_read_unlock();
8782 current->io_uring->last = file;
8783 }
8784
8785 return 0;
8786}
8787
8788/*
8789 * Remove this io_uring_file -> task mapping.
8790 */
8791static void io_uring_del_task_file(struct file *file)
8792{
8793 struct io_uring_task *tctx = current->io_uring;
8794 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8795
8796 if (tctx->last == file)
8797 tctx->last = NULL;
8798
8799 xas_lock(&xas);
8800 file = xas_store(&xas, NULL);
8801 xas_unlock(&xas);
8802
8803 if (file)
8804 fput(file);
8805}
8806
8807static void __io_uring_attempt_task_drop(struct file *file)
8808{
8809 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8810 struct file *old;
8811
8812 rcu_read_lock();
8813 old = xas_load(&xas);
8814 rcu_read_unlock();
8815
8816 if (old == file)
8817 io_uring_del_task_file(file);
8818}
8819
8820/*
8821 * Drop task note for this file if we're the only ones that hold it after
8822 * pending fput()
8823 */
8824static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8825{
8826 if (!current->io_uring)
8827 return;
8828 /*
8829 * fput() is pending, will be 2 if the only other ref is our potential
8830 * task file note. If the task is exiting, drop regardless of count.
8831 */
8832 if (!exiting && atomic_long_read(&file->f_count) != 2)
8833 return;
8834
8835 __io_uring_attempt_task_drop(file);
8836}
8837
8838void __io_uring_files_cancel(struct files_struct *files)
8839{
8840 struct io_uring_task *tctx = current->io_uring;
8841 XA_STATE(xas, &tctx->xa, 0);
8842
8843 /* make sure overflow events are dropped */
8844 tctx->in_idle = true;
8845
8846 do {
8847 struct io_ring_ctx *ctx;
8848 struct file *file;
8849
8850 xas_lock(&xas);
8851 file = xas_next_entry(&xas, ULONG_MAX);
8852 xas_unlock(&xas);
8853
8854 if (!file)
8855 break;
8856
8857 ctx = file->private_data;
8858
8859 io_uring_cancel_task_requests(ctx, files);
8860 if (files)
8861 io_uring_del_task_file(file);
8862 } while (1);
8863}
8864
8865static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8866{
8867 return atomic_long_read(&tctx->req_issue) ==
8868 atomic_long_read(&tctx->req_complete);
8869}
8870
8871/*
8872 * Find any io_uring fd that this task has registered or done IO on, and cancel
8873 * requests.
8874 */
8875void __io_uring_task_cancel(void)
8876{
8877 struct io_uring_task *tctx = current->io_uring;
8878 DEFINE_WAIT(wait);
8879 long completions;
8880
8881 /* make sure overflow events are dropped */
8882 tctx->in_idle = true;
8883
8884 while (!io_uring_task_idle(tctx)) {
8885 /* read completions before cancelations */
8886 completions = atomic_long_read(&tctx->req_complete);
8887 __io_uring_files_cancel(NULL);
8888
8889 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8890
8891 /*
8892 * If we've seen completions, retry. This avoids a race where
8893 * a completion comes in before we did prepare_to_wait().
8894 */
8895 if (completions != atomic_long_read(&tctx->req_complete))
8896 continue;
8897 if (io_uring_task_idle(tctx))
8898 break;
8899 schedule();
8900 }
8901
8902 finish_wait(&tctx->wait, &wait);
8903 tctx->in_idle = false;
8904}
8905
Jens Axboefcb323c2019-10-24 12:39:47 -06008906static int io_uring_flush(struct file *file, void *data)
8907{
8908 struct io_ring_ctx *ctx = file->private_data;
8909
Jens Axboe6ab23142020-02-08 20:23:59 -07008910 /*
8911 * If the task is going away, cancel work it may have pending
8912 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008913 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008914 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008915
Jens Axboe0f212202020-09-13 13:09:39 -06008916 io_uring_cancel_task_requests(ctx, data);
8917 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008918 return 0;
8919}
8920
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008921static void *io_uring_validate_mmap_request(struct file *file,
8922 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008923{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008924 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008925 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008926 struct page *page;
8927 void *ptr;
8928
8929 switch (offset) {
8930 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008931 case IORING_OFF_CQ_RING:
8932 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008933 break;
8934 case IORING_OFF_SQES:
8935 ptr = ctx->sq_sqes;
8936 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008937 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008938 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008939 }
8940
8941 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008942 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008943 return ERR_PTR(-EINVAL);
8944
8945 return ptr;
8946}
8947
8948#ifdef CONFIG_MMU
8949
8950static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8951{
8952 size_t sz = vma->vm_end - vma->vm_start;
8953 unsigned long pfn;
8954 void *ptr;
8955
8956 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8957 if (IS_ERR(ptr))
8958 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008959
8960 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8961 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8962}
8963
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008964#else /* !CONFIG_MMU */
8965
8966static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8967{
8968 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8969}
8970
8971static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8972{
8973 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8974}
8975
8976static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8977 unsigned long addr, unsigned long len,
8978 unsigned long pgoff, unsigned long flags)
8979{
8980 void *ptr;
8981
8982 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8983 if (IS_ERR(ptr))
8984 return PTR_ERR(ptr);
8985
8986 return (unsigned long) ptr;
8987}
8988
8989#endif /* !CONFIG_MMU */
8990
Jens Axboe90554202020-09-03 12:12:41 -06008991static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8992{
8993 DEFINE_WAIT(wait);
8994
8995 do {
8996 if (!io_sqring_full(ctx))
8997 break;
8998
8999 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9000
9001 if (!io_sqring_full(ctx))
9002 break;
9003
9004 schedule();
9005 } while (!signal_pending(current));
9006
9007 finish_wait(&ctx->sqo_sq_wait, &wait);
9008}
9009
Jens Axboe2b188cc2019-01-07 10:46:33 -07009010SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
9011 u32, min_complete, u32, flags, const sigset_t __user *, sig,
9012 size_t, sigsz)
9013{
9014 struct io_ring_ctx *ctx;
9015 long ret = -EBADF;
9016 int submitted = 0;
9017 struct fd f;
9018
Jens Axboe4c6e2772020-07-01 11:29:10 -06009019 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009020
Jens Axboe90554202020-09-03 12:12:41 -06009021 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9022 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009023 return -EINVAL;
9024
9025 f = fdget(fd);
9026 if (!f.file)
9027 return -EBADF;
9028
9029 ret = -EOPNOTSUPP;
9030 if (f.file->f_op != &io_uring_fops)
9031 goto out_fput;
9032
9033 ret = -ENXIO;
9034 ctx = f.file->private_data;
9035 if (!percpu_ref_tryget(&ctx->refs))
9036 goto out_fput;
9037
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009038 ret = -EBADFD;
9039 if (ctx->flags & IORING_SETUP_R_DISABLED)
9040 goto out;
9041
Jens Axboe6c271ce2019-01-10 11:22:30 -07009042 /*
9043 * For SQ polling, the thread will do all submissions and completions.
9044 * Just return the requested submit count, and wake the thread if
9045 * we were asked to.
9046 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009047 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009048 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07009049 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06009050 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07009051 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009052 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06009053 if (flags & IORING_ENTER_SQ_WAIT)
9054 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07009055 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009056 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06009057 ret = io_uring_add_task_file(f.file);
9058 if (unlikely(ret))
9059 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009060 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009061 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009062 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009063
9064 if (submitted != to_submit)
9065 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009066 }
9067 if (flags & IORING_ENTER_GETEVENTS) {
9068 min_complete = min(min_complete, ctx->cq_entries);
9069
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009070 /*
9071 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9072 * space applications don't need to do io completion events
9073 * polling again, they can rely on io_sq_thread to do polling
9074 * work, which can reduce cpu usage and uring_lock contention.
9075 */
9076 if (ctx->flags & IORING_SETUP_IOPOLL &&
9077 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009078 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009079 } else {
9080 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
9081 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009082 }
9083
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009084out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009085 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009086out_fput:
9087 fdput(f);
9088 return submitted ? submitted : ret;
9089}
9090
Tobias Klauserbebdb652020-02-26 18:38:32 +01009091#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009092static int io_uring_show_cred(int id, void *p, void *data)
9093{
9094 const struct cred *cred = p;
9095 struct seq_file *m = data;
9096 struct user_namespace *uns = seq_user_ns(m);
9097 struct group_info *gi;
9098 kernel_cap_t cap;
9099 unsigned __capi;
9100 int g;
9101
9102 seq_printf(m, "%5d\n", id);
9103 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9104 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9105 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9106 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9107 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9108 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9109 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9110 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9111 seq_puts(m, "\n\tGroups:\t");
9112 gi = cred->group_info;
9113 for (g = 0; g < gi->ngroups; g++) {
9114 seq_put_decimal_ull(m, g ? " " : "",
9115 from_kgid_munged(uns, gi->gid[g]));
9116 }
9117 seq_puts(m, "\n\tCapEff:\t");
9118 cap = cred->cap_effective;
9119 CAP_FOR_EACH_U32(__capi)
9120 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9121 seq_putc(m, '\n');
9122 return 0;
9123}
9124
9125static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9126{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009127 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009128 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009129 int i;
9130
Jens Axboefad8e0d2020-09-28 08:57:48 -06009131 /*
9132 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9133 * since fdinfo case grabs it in the opposite direction of normal use
9134 * cases. If we fail to get the lock, we just don't iterate any
9135 * structures that could be going away outside the io_uring mutex.
9136 */
9137 has_lock = mutex_trylock(&ctx->uring_lock);
9138
Joseph Qidbbe9c62020-09-29 09:01:22 -06009139 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9140 sq = ctx->sq_data;
9141
9142 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9143 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009144 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009145 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009146 struct fixed_file_table *table;
9147 struct file *f;
9148
9149 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9150 f = table->files[i & IORING_FILE_TABLE_MASK];
9151 if (f)
9152 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9153 else
9154 seq_printf(m, "%5u: <none>\n", i);
9155 }
9156 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009157 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009158 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9159
9160 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9161 (unsigned int) buf->len);
9162 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009163 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009164 seq_printf(m, "Personalities:\n");
9165 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9166 }
Jens Axboed7718a92020-02-14 22:23:12 -07009167 seq_printf(m, "PollList:\n");
9168 spin_lock_irq(&ctx->completion_lock);
9169 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9170 struct hlist_head *list = &ctx->cancel_hash[i];
9171 struct io_kiocb *req;
9172
9173 hlist_for_each_entry(req, list, hash_node)
9174 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9175 req->task->task_works != NULL);
9176 }
9177 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009178 if (has_lock)
9179 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009180}
9181
9182static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9183{
9184 struct io_ring_ctx *ctx = f->private_data;
9185
9186 if (percpu_ref_tryget(&ctx->refs)) {
9187 __io_uring_show_fdinfo(ctx, m);
9188 percpu_ref_put(&ctx->refs);
9189 }
9190}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009191#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009192
Jens Axboe2b188cc2019-01-07 10:46:33 -07009193static const struct file_operations io_uring_fops = {
9194 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009195 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009196 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009197#ifndef CONFIG_MMU
9198 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9199 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9200#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009201 .poll = io_uring_poll,
9202 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009203#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009204 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009205#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009206};
9207
9208static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9209 struct io_uring_params *p)
9210{
Hristo Venev75b28af2019-08-26 17:23:46 +00009211 struct io_rings *rings;
9212 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009213
Jens Axboebd740482020-08-05 12:58:23 -06009214 /* make sure these are sane, as we already accounted them */
9215 ctx->sq_entries = p->sq_entries;
9216 ctx->cq_entries = p->cq_entries;
9217
Hristo Venev75b28af2019-08-26 17:23:46 +00009218 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9219 if (size == SIZE_MAX)
9220 return -EOVERFLOW;
9221
9222 rings = io_mem_alloc(size);
9223 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009224 return -ENOMEM;
9225
Hristo Venev75b28af2019-08-26 17:23:46 +00009226 ctx->rings = rings;
9227 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9228 rings->sq_ring_mask = p->sq_entries - 1;
9229 rings->cq_ring_mask = p->cq_entries - 1;
9230 rings->sq_ring_entries = p->sq_entries;
9231 rings->cq_ring_entries = p->cq_entries;
9232 ctx->sq_mask = rings->sq_ring_mask;
9233 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009234
9235 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009236 if (size == SIZE_MAX) {
9237 io_mem_free(ctx->rings);
9238 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009239 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009240 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009241
9242 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009243 if (!ctx->sq_sqes) {
9244 io_mem_free(ctx->rings);
9245 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009246 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009247 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009248
Jens Axboe2b188cc2019-01-07 10:46:33 -07009249 return 0;
9250}
9251
9252/*
9253 * Allocate an anonymous fd, this is what constitutes the application
9254 * visible backing of an io_uring instance. The application mmaps this
9255 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9256 * we have to tie this fd to a socket for file garbage collection purposes.
9257 */
9258static int io_uring_get_fd(struct io_ring_ctx *ctx)
9259{
9260 struct file *file;
9261 int ret;
9262
9263#if defined(CONFIG_UNIX)
9264 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9265 &ctx->ring_sock);
9266 if (ret)
9267 return ret;
9268#endif
9269
9270 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9271 if (ret < 0)
9272 goto err;
9273
9274 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9275 O_RDWR | O_CLOEXEC);
9276 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009277err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009278 put_unused_fd(ret);
9279 ret = PTR_ERR(file);
9280 goto err;
9281 }
9282
9283#if defined(CONFIG_UNIX)
9284 ctx->ring_sock->file = file;
9285#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009286 if (unlikely(io_uring_add_task_file(file))) {
9287 file = ERR_PTR(-ENOMEM);
9288 goto err_fd;
9289 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009290 fd_install(ret, file);
9291 return ret;
9292err:
9293#if defined(CONFIG_UNIX)
9294 sock_release(ctx->ring_sock);
9295 ctx->ring_sock = NULL;
9296#endif
9297 return ret;
9298}
9299
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009300static int io_uring_create(unsigned entries, struct io_uring_params *p,
9301 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009302{
9303 struct user_struct *user = NULL;
9304 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009305 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009306 int ret;
9307
Jens Axboe8110c1a2019-12-28 15:39:54 -07009308 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009309 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009310 if (entries > IORING_MAX_ENTRIES) {
9311 if (!(p->flags & IORING_SETUP_CLAMP))
9312 return -EINVAL;
9313 entries = IORING_MAX_ENTRIES;
9314 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009315
9316 /*
9317 * Use twice as many entries for the CQ ring. It's possible for the
9318 * application to drive a higher depth than the size of the SQ ring,
9319 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009320 * some flexibility in overcommitting a bit. If the application has
9321 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9322 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009323 */
9324 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009325 if (p->flags & IORING_SETUP_CQSIZE) {
9326 /*
9327 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9328 * to a power-of-two, if it isn't already. We do NOT impose
9329 * any cq vs sq ring sizing.
9330 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009331 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009332 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009333 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9334 if (!(p->flags & IORING_SETUP_CLAMP))
9335 return -EINVAL;
9336 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9337 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009338 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9339 } else {
9340 p->cq_entries = 2 * p->sq_entries;
9341 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009342
9343 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009344 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009345
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009346 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009347 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009348 ring_pages(p->sq_entries, p->cq_entries));
9349 if (ret) {
9350 free_uid(user);
9351 return ret;
9352 }
9353 }
9354
9355 ctx = io_ring_ctx_alloc(p);
9356 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009357 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009358 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009359 p->cq_entries));
9360 free_uid(user);
9361 return -ENOMEM;
9362 }
9363 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009364 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009365 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009366
Jens Axboe2aede0e2020-09-14 10:45:53 -06009367 ctx->sqo_task = get_task_struct(current);
9368
9369 /*
9370 * This is just grabbed for accounting purposes. When a process exits,
9371 * the mm is exited and dropped before the files, hence we need to hang
9372 * on to this mm purely for the purposes of being able to unaccount
9373 * memory (locked/pinned vm). It's not used for anything else.
9374 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009375 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009376 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009377
Dennis Zhou91d8f512020-09-16 13:41:05 -07009378#ifdef CONFIG_BLK_CGROUP
9379 /*
9380 * The sq thread will belong to the original cgroup it was inited in.
9381 * If the cgroup goes offline (e.g. disabling the io controller), then
9382 * issued bios will be associated with the closest cgroup later in the
9383 * block layer.
9384 */
9385 rcu_read_lock();
9386 ctx->sqo_blkcg_css = blkcg_css();
9387 ret = css_tryget_online(ctx->sqo_blkcg_css);
9388 rcu_read_unlock();
9389 if (!ret) {
9390 /* don't init against a dying cgroup, have the user try again */
9391 ctx->sqo_blkcg_css = NULL;
9392 ret = -ENODEV;
9393 goto err;
9394 }
9395#endif
9396
Jens Axboef74441e2020-08-05 13:00:44 -06009397 /*
9398 * Account memory _before_ installing the file descriptor. Once
9399 * the descriptor is installed, it can get closed at any time. Also
9400 * do this before hitting the general error path, as ring freeing
9401 * will un-account as well.
9402 */
9403 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9404 ACCT_LOCKED);
9405 ctx->limit_mem = limit_mem;
9406
Jens Axboe2b188cc2019-01-07 10:46:33 -07009407 ret = io_allocate_scq_urings(ctx, p);
9408 if (ret)
9409 goto err;
9410
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009411 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009412 if (ret)
9413 goto err;
9414
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009415 if (!(p->flags & IORING_SETUP_R_DISABLED))
9416 io_sq_offload_start(ctx);
9417
Jens Axboe2b188cc2019-01-07 10:46:33 -07009418 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009419 p->sq_off.head = offsetof(struct io_rings, sq.head);
9420 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9421 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9422 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9423 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9424 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9425 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009426
9427 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009428 p->cq_off.head = offsetof(struct io_rings, cq.head);
9429 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9430 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9431 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9432 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9433 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009434 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009435
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009436 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9437 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009438 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9439 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009440
9441 if (copy_to_user(params, p, sizeof(*p))) {
9442 ret = -EFAULT;
9443 goto err;
9444 }
Jens Axboed1719f72020-07-30 13:43:53 -06009445
9446 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009447 * Install ring fd as the very last thing, so we don't risk someone
9448 * having closed it before we finish setup
9449 */
9450 ret = io_uring_get_fd(ctx);
9451 if (ret < 0)
9452 goto err;
9453
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009454 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009455 return ret;
9456err:
9457 io_ring_ctx_wait_and_kill(ctx);
9458 return ret;
9459}
9460
9461/*
9462 * Sets up an aio uring context, and returns the fd. Applications asks for a
9463 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9464 * params structure passed in.
9465 */
9466static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9467{
9468 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009469 int i;
9470
9471 if (copy_from_user(&p, params, sizeof(p)))
9472 return -EFAULT;
9473 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9474 if (p.resv[i])
9475 return -EINVAL;
9476 }
9477
Jens Axboe6c271ce2019-01-10 11:22:30 -07009478 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009479 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009480 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9481 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009482 return -EINVAL;
9483
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009484 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009485}
9486
9487SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9488 struct io_uring_params __user *, params)
9489{
9490 return io_uring_setup(entries, params);
9491}
9492
Jens Axboe66f4af92020-01-16 15:36:52 -07009493static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9494{
9495 struct io_uring_probe *p;
9496 size_t size;
9497 int i, ret;
9498
9499 size = struct_size(p, ops, nr_args);
9500 if (size == SIZE_MAX)
9501 return -EOVERFLOW;
9502 p = kzalloc(size, GFP_KERNEL);
9503 if (!p)
9504 return -ENOMEM;
9505
9506 ret = -EFAULT;
9507 if (copy_from_user(p, arg, size))
9508 goto out;
9509 ret = -EINVAL;
9510 if (memchr_inv(p, 0, size))
9511 goto out;
9512
9513 p->last_op = IORING_OP_LAST - 1;
9514 if (nr_args > IORING_OP_LAST)
9515 nr_args = IORING_OP_LAST;
9516
9517 for (i = 0; i < nr_args; i++) {
9518 p->ops[i].op = i;
9519 if (!io_op_defs[i].not_supported)
9520 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9521 }
9522 p->ops_len = i;
9523
9524 ret = 0;
9525 if (copy_to_user(arg, p, size))
9526 ret = -EFAULT;
9527out:
9528 kfree(p);
9529 return ret;
9530}
9531
Jens Axboe071698e2020-01-28 10:04:42 -07009532static int io_register_personality(struct io_ring_ctx *ctx)
9533{
9534 const struct cred *creds = get_current_cred();
9535 int id;
9536
9537 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9538 USHRT_MAX, GFP_KERNEL);
9539 if (id < 0)
9540 put_cred(creds);
9541 return id;
9542}
9543
9544static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9545{
9546 const struct cred *old_creds;
9547
9548 old_creds = idr_remove(&ctx->personality_idr, id);
9549 if (old_creds) {
9550 put_cred(old_creds);
9551 return 0;
9552 }
9553
9554 return -EINVAL;
9555}
9556
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009557static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9558 unsigned int nr_args)
9559{
9560 struct io_uring_restriction *res;
9561 size_t size;
9562 int i, ret;
9563
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009564 /* Restrictions allowed only if rings started disabled */
9565 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9566 return -EBADFD;
9567
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009568 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009569 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009570 return -EBUSY;
9571
9572 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9573 return -EINVAL;
9574
9575 size = array_size(nr_args, sizeof(*res));
9576 if (size == SIZE_MAX)
9577 return -EOVERFLOW;
9578
9579 res = memdup_user(arg, size);
9580 if (IS_ERR(res))
9581 return PTR_ERR(res);
9582
9583 ret = 0;
9584
9585 for (i = 0; i < nr_args; i++) {
9586 switch (res[i].opcode) {
9587 case IORING_RESTRICTION_REGISTER_OP:
9588 if (res[i].register_op >= IORING_REGISTER_LAST) {
9589 ret = -EINVAL;
9590 goto out;
9591 }
9592
9593 __set_bit(res[i].register_op,
9594 ctx->restrictions.register_op);
9595 break;
9596 case IORING_RESTRICTION_SQE_OP:
9597 if (res[i].sqe_op >= IORING_OP_LAST) {
9598 ret = -EINVAL;
9599 goto out;
9600 }
9601
9602 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9603 break;
9604 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9605 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9606 break;
9607 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9608 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9609 break;
9610 default:
9611 ret = -EINVAL;
9612 goto out;
9613 }
9614 }
9615
9616out:
9617 /* Reset all restrictions if an error happened */
9618 if (ret != 0)
9619 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9620 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009621 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009622
9623 kfree(res);
9624 return ret;
9625}
9626
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009627static int io_register_enable_rings(struct io_ring_ctx *ctx)
9628{
9629 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9630 return -EBADFD;
9631
9632 if (ctx->restrictions.registered)
9633 ctx->restricted = 1;
9634
9635 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9636
9637 io_sq_offload_start(ctx);
9638
9639 return 0;
9640}
9641
Jens Axboe071698e2020-01-28 10:04:42 -07009642static bool io_register_op_must_quiesce(int op)
9643{
9644 switch (op) {
9645 case IORING_UNREGISTER_FILES:
9646 case IORING_REGISTER_FILES_UPDATE:
9647 case IORING_REGISTER_PROBE:
9648 case IORING_REGISTER_PERSONALITY:
9649 case IORING_UNREGISTER_PERSONALITY:
9650 return false;
9651 default:
9652 return true;
9653 }
9654}
9655
Jens Axboeedafcce2019-01-09 09:16:05 -07009656static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9657 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009658 __releases(ctx->uring_lock)
9659 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009660{
9661 int ret;
9662
Jens Axboe35fa71a2019-04-22 10:23:23 -06009663 /*
9664 * We're inside the ring mutex, if the ref is already dying, then
9665 * someone else killed the ctx or is already going through
9666 * io_uring_register().
9667 */
9668 if (percpu_ref_is_dying(&ctx->refs))
9669 return -ENXIO;
9670
Jens Axboe071698e2020-01-28 10:04:42 -07009671 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009672 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009673
Jens Axboe05f3fb32019-12-09 11:22:50 -07009674 /*
9675 * Drop uring mutex before waiting for references to exit. If
9676 * another thread is currently inside io_uring_enter() it might
9677 * need to grab the uring_lock to make progress. If we hold it
9678 * here across the drain wait, then we can deadlock. It's safe
9679 * to drop the mutex here, since no new references will come in
9680 * after we've killed the percpu ref.
9681 */
9682 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009683 do {
9684 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9685 if (!ret)
9686 break;
9687 if (io_run_task_work_sig() > 0)
9688 continue;
9689 } while (1);
9690
Jens Axboe05f3fb32019-12-09 11:22:50 -07009691 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009692
Jens Axboec1503682020-01-08 08:26:07 -07009693 if (ret) {
9694 percpu_ref_resurrect(&ctx->refs);
9695 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009696 goto out_quiesce;
9697 }
9698 }
9699
9700 if (ctx->restricted) {
9701 if (opcode >= IORING_REGISTER_LAST) {
9702 ret = -EINVAL;
9703 goto out;
9704 }
9705
9706 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9707 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009708 goto out;
9709 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009710 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009711
9712 switch (opcode) {
9713 case IORING_REGISTER_BUFFERS:
9714 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9715 break;
9716 case IORING_UNREGISTER_BUFFERS:
9717 ret = -EINVAL;
9718 if (arg || nr_args)
9719 break;
9720 ret = io_sqe_buffer_unregister(ctx);
9721 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009722 case IORING_REGISTER_FILES:
9723 ret = io_sqe_files_register(ctx, arg, nr_args);
9724 break;
9725 case IORING_UNREGISTER_FILES:
9726 ret = -EINVAL;
9727 if (arg || nr_args)
9728 break;
9729 ret = io_sqe_files_unregister(ctx);
9730 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009731 case IORING_REGISTER_FILES_UPDATE:
9732 ret = io_sqe_files_update(ctx, arg, nr_args);
9733 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009734 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009735 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009736 ret = -EINVAL;
9737 if (nr_args != 1)
9738 break;
9739 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009740 if (ret)
9741 break;
9742 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9743 ctx->eventfd_async = 1;
9744 else
9745 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009746 break;
9747 case IORING_UNREGISTER_EVENTFD:
9748 ret = -EINVAL;
9749 if (arg || nr_args)
9750 break;
9751 ret = io_eventfd_unregister(ctx);
9752 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009753 case IORING_REGISTER_PROBE:
9754 ret = -EINVAL;
9755 if (!arg || nr_args > 256)
9756 break;
9757 ret = io_probe(ctx, arg, nr_args);
9758 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009759 case IORING_REGISTER_PERSONALITY:
9760 ret = -EINVAL;
9761 if (arg || nr_args)
9762 break;
9763 ret = io_register_personality(ctx);
9764 break;
9765 case IORING_UNREGISTER_PERSONALITY:
9766 ret = -EINVAL;
9767 if (arg)
9768 break;
9769 ret = io_unregister_personality(ctx, nr_args);
9770 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009771 case IORING_REGISTER_ENABLE_RINGS:
9772 ret = -EINVAL;
9773 if (arg || nr_args)
9774 break;
9775 ret = io_register_enable_rings(ctx);
9776 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009777 case IORING_REGISTER_RESTRICTIONS:
9778 ret = io_register_restrictions(ctx, arg, nr_args);
9779 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009780 default:
9781 ret = -EINVAL;
9782 break;
9783 }
9784
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009785out:
Jens Axboe071698e2020-01-28 10:04:42 -07009786 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009787 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009788 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009789out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009790 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009791 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009792 return ret;
9793}
9794
9795SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9796 void __user *, arg, unsigned int, nr_args)
9797{
9798 struct io_ring_ctx *ctx;
9799 long ret = -EBADF;
9800 struct fd f;
9801
9802 f = fdget(fd);
9803 if (!f.file)
9804 return -EBADF;
9805
9806 ret = -EOPNOTSUPP;
9807 if (f.file->f_op != &io_uring_fops)
9808 goto out_fput;
9809
9810 ctx = f.file->private_data;
9811
9812 mutex_lock(&ctx->uring_lock);
9813 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9814 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009815 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9816 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009817out_fput:
9818 fdput(f);
9819 return ret;
9820}
9821
Jens Axboe2b188cc2019-01-07 10:46:33 -07009822static int __init io_uring_init(void)
9823{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009824#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9825 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9826 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9827} while (0)
9828
9829#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9830 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9831 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9832 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9833 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9834 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9835 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9836 BUILD_BUG_SQE_ELEM(8, __u64, off);
9837 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9838 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009839 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009840 BUILD_BUG_SQE_ELEM(24, __u32, len);
9841 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9842 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9843 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9844 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009845 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9846 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009847 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9848 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9849 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9850 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9851 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9852 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9853 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9854 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009855 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009856 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9857 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9858 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009859 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009860
Jens Axboed3656342019-12-18 09:50:26 -07009861 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009862 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009863 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9864 return 0;
9865};
9866__initcall(io_uring_init);