blob: 3a65bcba5a7bb89f5d3d9b50807c64778bccc427 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020085#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
Jens Axboe2b188cc2019-01-07 10:46:33 -070088#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060091#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
Daniel Xu5277dea2019-09-14 14:23:45 -070093#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060094#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060095
96/*
97 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
98 */
99#define IORING_FILE_TABLE_SHIFT 9
100#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
101#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
102#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200103#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
104 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700105
106struct io_uring {
107 u32 head ____cacheline_aligned_in_smp;
108 u32 tail ____cacheline_aligned_in_smp;
109};
110
Stefan Bühler1e84b972019-04-24 23:54:16 +0200111/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000112 * This data is shared with the application through the mmap at offsets
113 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 *
115 * The offsets to the member fields are published through struct
116 * io_sqring_offsets when calling io_uring_setup.
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 /*
120 * Head and tail offsets into the ring; the offsets need to be
121 * masked to get valid indices.
122 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 * The kernel controls head of the sq ring and the tail of the cq ring,
124 * and the application controls tail of the sq ring and the head of the
125 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000129 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 * ring_entries - 1)
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 u32 sq_ring_mask, cq_ring_mask;
133 /* Ring sizes (constant, power of 2) */
134 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Number of invalid entries dropped by the kernel due to
137 * invalid index stored in array
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * After a new SQ head value was read by the application this
144 * counter includes all submissions that were dropped reaching
145 * the new SQ head (and possibly more).
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200149 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application.
153 *
154 * The application needs a full memory barrier before checking
155 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200159 * Runtime CQ flags
160 *
161 * Written by the application, shouldn't be modified by the
162 * kernel.
163 */
164 u32 cq_flags;
165 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200166 * Number of completion events lost because the queue was full;
167 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800168 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200169 * the completion queue.
170 *
171 * Written by the kernel, shouldn't be modified by the
172 * application (i.e. get number of "new events" by comparing to
173 * cached value).
174 *
175 * As completion events come in out of order this counter is not
176 * ordered with any other data.
177 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000178 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200179 /*
180 * Ring buffer of completion events.
181 *
182 * The kernel writes completion events fresh every time they are
183 * produced, so the application is allowed to modify pending
184 * entries.
185 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000186 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700187};
188
Jens Axboeedafcce2019-01-09 09:16:05 -0700189struct io_mapped_ubuf {
190 u64 ubuf;
191 size_t len;
192 struct bio_vec *bvec;
193 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600194 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700195};
196
Jens Axboe65e19f52019-10-26 07:20:21 -0600197struct fixed_file_table {
198 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700199};
200
Xiaoguang Wang05589552020-03-31 14:05:18 +0800201struct fixed_file_ref_node {
202 struct percpu_ref refs;
203 struct list_head node;
204 struct list_head file_list;
205 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600206 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800207};
208
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209struct fixed_file_data {
210 struct fixed_file_table *table;
211 struct io_ring_ctx *ctx;
212
Xiaoguang Wang05589552020-03-31 14:05:18 +0800213 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700214 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700215 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800216 struct list_head ref_list;
217 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700218};
219
Jens Axboe5a2e7452020-02-23 16:23:11 -0700220struct io_buffer {
221 struct list_head list;
222 __u64 addr;
223 __s32 len;
224 __u16 bid;
225};
226
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200227struct io_restriction {
228 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
229 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
230 u8 sqe_flags_allowed;
231 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200232 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200233};
234
Jens Axboe534ca6d2020-09-02 13:52:19 -0600235struct io_sq_data {
236 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600237 struct mutex lock;
238
239 /* ctx's that are using this sqd */
240 struct list_head ctx_list;
241 struct list_head ctx_new_list;
242 struct mutex ctx_lock;
243
Jens Axboe534ca6d2020-09-02 13:52:19 -0600244 struct task_struct *thread;
245 struct wait_queue_head wait;
246};
247
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248struct io_ring_ctx {
249 struct {
250 struct percpu_ref refs;
251 } ____cacheline_aligned_in_smp;
252
253 struct {
254 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800255 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700256 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800257 unsigned int cq_overflow_flushed: 1;
258 unsigned int drain_next: 1;
259 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200260 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261
Hristo Venev75b28af2019-08-26 17:23:46 +0000262 /*
263 * Ring buffer of indices into array of io_uring_sqe, which is
264 * mmapped by the application using the IORING_OFF_SQES offset.
265 *
266 * This indirection could e.g. be used to assign fixed
267 * io_uring_sqe entries to operations and only submit them to
268 * the queue when needed.
269 *
270 * The kernel modifies neither the indices array nor the entries
271 * array.
272 */
273 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700274 unsigned cached_sq_head;
275 unsigned sq_entries;
276 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700277 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600278 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700280 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600281
282 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600283 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700284 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285
Jens Axboefcb323c2019-10-24 12:39:47 -0600286 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700287 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700288 } ____cacheline_aligned_in_smp;
289
Hristo Venev75b28af2019-08-26 17:23:46 +0000290 struct io_rings *rings;
291
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600293 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600294
295 /*
296 * For SQPOLL usage - we hold a reference to the parent task, so we
297 * have access to the ->files
298 */
299 struct task_struct *sqo_task;
300
301 /* Only used for accounting purposes */
302 struct mm_struct *mm_account;
303
Dennis Zhou91d8f512020-09-16 13:41:05 -0700304#ifdef CONFIG_BLK_CGROUP
305 struct cgroup_subsys_state *sqo_blkcg_css;
306#endif
307
Jens Axboe534ca6d2020-09-02 13:52:19 -0600308 struct io_sq_data *sq_data; /* if using sq thread polling */
309
Jens Axboe90554202020-09-03 12:12:41 -0600310 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600311 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600312 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
Jens Axboe6b063142019-01-10 22:13:58 -0700314 /*
315 * If used, fixed file set. Writers must ensure that ->refs is dead,
316 * readers must ensure that ->refs is alive as long as the file* is
317 * used. Only updated through io_uring_register(2).
318 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700319 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700320 unsigned nr_user_files;
321
Jens Axboeedafcce2019-01-09 09:16:05 -0700322 /* if used, fixed mapped user buffers */
323 unsigned nr_user_bufs;
324 struct io_mapped_ubuf *user_bufs;
325
Jens Axboe2b188cc2019-01-07 10:46:33 -0700326 struct user_struct *user;
327
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700328 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700329
Jens Axboe0f158b42020-05-14 17:18:39 -0600330 struct completion ref_comp;
331 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700332
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700333 /* if all else fails... */
334 struct io_kiocb *fallback_req;
335
Jens Axboe206aefd2019-11-07 18:27:42 -0700336#if defined(CONFIG_UNIX)
337 struct socket *ring_sock;
338#endif
339
Jens Axboe5a2e7452020-02-23 16:23:11 -0700340 struct idr io_buffer_idr;
341
Jens Axboe071698e2020-01-28 10:04:42 -0700342 struct idr personality_idr;
343
Jens Axboe206aefd2019-11-07 18:27:42 -0700344 struct {
345 unsigned cached_cq_tail;
346 unsigned cq_entries;
347 unsigned cq_mask;
348 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700349 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700350 struct wait_queue_head cq_wait;
351 struct fasync_struct *cq_fasync;
352 struct eventfd_ctx *cq_ev_fd;
353 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700354
355 struct {
356 struct mutex uring_lock;
357 wait_queue_head_t wait;
358 } ____cacheline_aligned_in_smp;
359
360 struct {
361 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700362
Jens Axboedef596e2019-01-09 08:59:42 -0700363 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300364 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700365 * io_uring instances that don't use IORING_SETUP_SQPOLL.
366 * For SQPOLL, only the single threaded io_sq_thread() will
367 * manipulate the list, hence no extra locking is needed there.
368 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300369 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700370 struct hlist_head *cancel_hash;
371 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700372 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600373
374 spinlock_t inflight_lock;
375 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600377
Jens Axboe4a38aed22020-05-14 17:21:15 -0600378 struct delayed_work file_put_work;
379 struct llist_head file_put_llist;
380
Jens Axboe85faa7b2020-04-09 18:14:00 -0600381 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200382 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383};
384
Jens Axboe09bb8392019-03-13 12:39:28 -0600385/*
386 * First field must be the file pointer in all the
387 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
388 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700389struct io_poll_iocb {
390 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700391 union {
392 struct wait_queue_head *head;
393 u64 addr;
394 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700395 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600396 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700397 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700398 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700399};
400
Jens Axboeb5dba592019-12-11 14:02:38 -0700401struct io_close {
402 struct file *file;
403 struct file *put_file;
404 int fd;
405};
406
Jens Axboead8a48a2019-11-15 08:49:11 -0700407struct io_timeout_data {
408 struct io_kiocb *req;
409 struct hrtimer timer;
410 struct timespec64 ts;
411 enum hrtimer_mode mode;
412};
413
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700414struct io_accept {
415 struct file *file;
416 struct sockaddr __user *addr;
417 int __user *addr_len;
418 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600419 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700420};
421
422struct io_sync {
423 struct file *file;
424 loff_t len;
425 loff_t off;
426 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700427 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700428};
429
Jens Axboefbf23842019-12-17 18:45:56 -0700430struct io_cancel {
431 struct file *file;
432 u64 addr;
433};
434
Jens Axboeb29472e2019-12-17 18:50:29 -0700435struct io_timeout {
436 struct file *file;
437 u64 addr;
438 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300439 u32 off;
440 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300441 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700442};
443
Jens Axboe9adbd452019-12-20 08:45:55 -0700444struct io_rw {
445 /* NOTE: kiocb has the file as the first member, so don't do it here */
446 struct kiocb kiocb;
447 u64 addr;
448 u64 len;
449};
450
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700451struct io_connect {
452 struct file *file;
453 struct sockaddr __user *addr;
454 int addr_len;
455};
456
Jens Axboee47293f2019-12-20 08:58:21 -0700457struct io_sr_msg {
458 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700459 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300460 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700461 void __user *buf;
462 };
Jens Axboee47293f2019-12-20 08:58:21 -0700463 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700464 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700465 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700466 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700467};
468
Jens Axboe15b71ab2019-12-11 11:20:36 -0700469struct io_open {
470 struct file *file;
471 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700472 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700473 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600474 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700475};
476
Jens Axboe05f3fb32019-12-09 11:22:50 -0700477struct io_files_update {
478 struct file *file;
479 u64 arg;
480 u32 nr_args;
481 u32 offset;
482};
483
Jens Axboe4840e412019-12-25 22:03:45 -0700484struct io_fadvise {
485 struct file *file;
486 u64 offset;
487 u32 len;
488 u32 advice;
489};
490
Jens Axboec1ca7572019-12-25 22:18:28 -0700491struct io_madvise {
492 struct file *file;
493 u64 addr;
494 u32 len;
495 u32 advice;
496};
497
Jens Axboe3e4827b2020-01-08 15:18:09 -0700498struct io_epoll {
499 struct file *file;
500 int epfd;
501 int op;
502 int fd;
503 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700504};
505
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300506struct io_splice {
507 struct file *file_out;
508 struct file *file_in;
509 loff_t off_out;
510 loff_t off_in;
511 u64 len;
512 unsigned int flags;
513};
514
Jens Axboeddf0322d2020-02-23 16:41:33 -0700515struct io_provide_buf {
516 struct file *file;
517 __u64 addr;
518 __s32 len;
519 __u32 bgid;
520 __u16 nbufs;
521 __u16 bid;
522};
523
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700524struct io_statx {
525 struct file *file;
526 int dfd;
527 unsigned int mask;
528 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700529 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700530 struct statx __user *buffer;
531};
532
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300533struct io_completion {
534 struct file *file;
535 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300536 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300537};
538
Jens Axboef499a022019-12-02 16:28:46 -0700539struct io_async_connect {
540 struct sockaddr_storage address;
541};
542
Jens Axboe03b12302019-12-02 18:50:25 -0700543struct io_async_msghdr {
544 struct iovec fast_iov[UIO_FASTIOV];
545 struct iovec *iov;
546 struct sockaddr __user *uaddr;
547 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700548 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700549};
550
Jens Axboef67676d2019-12-02 11:03:47 -0700551struct io_async_rw {
552 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600553 const struct iovec *free_iovec;
554 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600555 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600556 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700557};
558
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300559enum {
560 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
561 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
562 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
563 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
564 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700565 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300566
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300567 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300568 REQ_F_FAIL_LINK_BIT,
569 REQ_F_INFLIGHT_BIT,
570 REQ_F_CUR_POS_BIT,
571 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300572 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300573 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300574 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300575 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700576 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700577 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600578 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800579 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700580
581 /* not a real bit, just to check we're not overflowing the space */
582 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300583};
584
585enum {
586 /* ctx owns file */
587 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
588 /* drain existing IO first */
589 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
590 /* linked sqes */
591 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
592 /* doesn't sever on completion < 0 */
593 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
594 /* IOSQE_ASYNC */
595 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700596 /* IOSQE_BUFFER_SELECT */
597 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300598
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300599 /* head of a link */
600 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300601 /* fail rest of links */
602 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
603 /* on inflight list */
604 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
605 /* read/write uses file position */
606 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
607 /* must not punt to workers */
608 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609 /* has linked timeout */
610 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300611 /* regular file */
612 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300613 /* completion under lock */
614 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300615 /* needs cleanup */
616 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700617 /* already went through poll handler */
618 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700619 /* buffer already selected */
620 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600621 /* doesn't need file table for this request */
622 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800623 /* io_wq_work is initialized */
624 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700625};
626
627struct async_poll {
628 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600629 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300630};
631
Jens Axboe09bb8392019-03-13 12:39:28 -0600632/*
633 * NOTE! Each of the iocb union members has the file pointer
634 * as the first entry in their struct definition. So you can
635 * access the file pointer through any of the sub-structs,
636 * or directly as just 'ki_filp' in this struct.
637 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700639 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600640 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700641 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700642 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700643 struct io_accept accept;
644 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700645 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700646 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700647 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700648 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700649 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700650 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700651 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700652 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700653 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700654 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300655 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700656 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700657 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300658 /* use only after cleaning per-op data, see io_clean_op() */
659 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700660 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700661
Jens Axboee8c2bc12020-08-15 18:44:09 -0700662 /* opcode allocated if it needs to store data for async defer */
663 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700664 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800665 /* polled IO has completed */
666 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700667
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700668 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300669 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700670
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300671 struct io_ring_ctx *ctx;
672 unsigned int flags;
673 refcount_t refs;
674 struct task_struct *task;
675 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700676
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300677 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700678
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300679 /*
680 * 1. used with ctx->iopoll_list with reads/writes
681 * 2. to track reqs with ->files (see io_op_def::file_table)
682 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300683 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600684
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300685 struct percpu_ref *fixed_file_refs;
686 struct callback_head task_work;
687 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
688 struct hlist_node hash_node;
689 struct async_poll *apoll;
690 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700691};
692
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300693struct io_defer_entry {
694 struct list_head list;
695 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300696 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300697};
698
Jens Axboedef596e2019-01-09 08:59:42 -0700699#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700700
Jens Axboe013538b2020-06-22 09:29:15 -0600701struct io_comp_state {
702 unsigned int nr;
703 struct list_head list;
704 struct io_ring_ctx *ctx;
705};
706
Jens Axboe9a56a232019-01-09 09:06:50 -0700707struct io_submit_state {
708 struct blk_plug plug;
709
710 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700711 * io_kiocb alloc cache
712 */
713 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300714 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700715
716 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600717 * Batch completion logic
718 */
719 struct io_comp_state comp;
720
721 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700722 * File reference cache
723 */
724 struct file *file;
725 unsigned int fd;
726 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700727 unsigned int ios_left;
728};
729
Jens Axboed3656342019-12-18 09:50:26 -0700730struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700731 /* needs current->mm setup, does mm access */
732 unsigned needs_mm : 1;
733 /* needs req->file assigned */
734 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600735 /* don't fail if file grab fails */
736 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700737 /* hash wq insertion if file is a regular file */
738 unsigned hash_reg_file : 1;
739 /* unbound wq insertion if file is a non-regular file */
740 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700741 /* opcode is not supported by this kernel */
742 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700743 /* needs file table */
744 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700745 /* needs ->fs */
746 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700747 /* set if opcode supports polled "wait" */
748 unsigned pollin : 1;
749 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700750 /* op supports buffer selection */
751 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700752 /* needs rlimit(RLIMIT_FSIZE) assigned */
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300753 unsigned needs_fsize : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700754 /* must always have async data allocated */
755 unsigned needs_async_data : 1;
Dennis Zhou91d8f512020-09-16 13:41:05 -0700756 /* needs blkcg context, issues async io potentially */
757 unsigned needs_blkcg : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700758 /* size of async data needed, if any */
759 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700760};
761
Jens Axboe738277a2020-09-03 05:54:56 -0600762static const struct io_op_def io_op_defs[] __read_mostly = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_NOP] = {},
764 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700765 .needs_mm = 1,
766 .needs_file = 1,
767 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700768 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700769 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700770 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700771 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700772 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_mm = 1,
776 .needs_file = 1,
777 .hash_reg_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700779 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300780 .needs_fsize = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700781 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700782 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700783 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700787 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700790 .needs_file = 1,
791 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700792 .pollin = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700793 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700794 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_file = 1,
798 .hash_reg_file = 1,
799 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700800 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300801 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700802 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700803 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700804 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300805 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700806 .needs_file = 1,
807 .unbound_nonreg_file = 1,
808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_POLL_REMOVE] = {},
810 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700811 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700812 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700815 .needs_mm = 1,
816 .needs_file = 1,
817 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700818 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700819 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700820 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700821 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700822 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700823 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300824 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700825 .needs_mm = 1,
826 .needs_file = 1,
827 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700828 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700829 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700830 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700831 .needs_async_data = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700832 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700833 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700837 .needs_async_data = 1,
838 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_TIMEOUT_REMOVE] = {},
841 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700842 .needs_mm = 1,
843 .needs_file = 1,
844 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700845 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700846 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700847 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_ASYNC_CANCEL] = {},
849 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700850 .needs_mm = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700851 .needs_async_data = 1,
852 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700855 .needs_mm = 1,
856 .needs_file = 1,
857 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700858 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700859 .needs_async_data = 1,
860 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700863 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300864 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700865 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700868 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700869 .needs_fs = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700870 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700871 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300872 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600873 .needs_file = 1,
874 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700875 .file_table = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700876 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700879 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700880 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700883 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700884 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600885 .file_table = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700886 .needs_blkcg = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700889 .needs_mm = 1,
890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700892 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700893 .buffer_select = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700894 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700895 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700898 .needs_mm = 1,
899 .needs_file = 1,
900 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700901 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300902 .needs_fsize = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700903 .needs_blkcg = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700904 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700907 .needs_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700908 .needs_blkcg = 1,
Jens Axboe4840e412019-12-25 22:03:45 -0700909 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300910 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700911 .needs_mm = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700912 .needs_blkcg = 1,
Jens Axboec1ca7572019-12-25 22:18:28 -0700913 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300914 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700915 .needs_mm = 1,
916 .needs_file = 1,
917 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700918 .pollout = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700919 .needs_blkcg = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700922 .needs_mm = 1,
923 .needs_file = 1,
924 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700925 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700926 .buffer_select = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700927 .needs_blkcg = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700928 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300929 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700930 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700931 .needs_fs = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700932 .needs_blkcg = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700933 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700934 [IORING_OP_EPOLL_CTL] = {
935 .unbound_nonreg_file = 1,
936 .file_table = 1,
937 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300938 [IORING_OP_SPLICE] = {
939 .needs_file = 1,
940 .hash_reg_file = 1,
941 .unbound_nonreg_file = 1,
Dennis Zhou91d8f512020-09-16 13:41:05 -0700942 .needs_blkcg = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700943 },
944 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700945 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300946 [IORING_OP_TEE] = {
947 .needs_file = 1,
948 .hash_reg_file = 1,
949 .unbound_nonreg_file = 1,
950 },
Jens Axboed3656342019-12-18 09:50:26 -0700951};
952
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700953enum io_mem_account {
954 ACCT_LOCKED,
955 ACCT_PINNED,
956};
957
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300958static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
959 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700960static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800961static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600962static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700963static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700964static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600965static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700966static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700967static int __io_sqe_files_update(struct io_ring_ctx *ctx,
968 struct io_uring_files_update *ip,
969 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300970static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700971static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
972 int fd, struct file **out_file, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +0300973static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600974static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600975
Jens Axboeb63534c2020-06-04 11:28:00 -0600976static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
977 struct iovec **iovec, struct iov_iter *iter,
978 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600979static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
980 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600981 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700982
983static struct kmem_cache *req_cachep;
984
Jens Axboe738277a2020-09-03 05:54:56 -0600985static const struct file_operations io_uring_fops __read_mostly;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700986
987struct sock *io_uring_get_socket(struct file *file)
988{
989#if defined(CONFIG_UNIX)
990 if (file->f_op == &io_uring_fops) {
991 struct io_ring_ctx *ctx = file->private_data;
992
993 return ctx->ring_sock->sk;
994 }
995#endif
996 return NULL;
997}
998EXPORT_SYMBOL(io_uring_get_socket);
999
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001000static inline void io_clean_op(struct io_kiocb *req)
1001{
Pavel Begunkovbb175342020-08-20 11:33:35 +03001002 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
1003 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001004 __io_clean_op(req);
1005}
1006
Jens Axboe4349f302020-07-09 15:07:01 -06001007static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001008{
1009 struct mm_struct *mm = current->mm;
1010
1011 if (mm) {
1012 kthread_unuse_mm(mm);
1013 mmput(mm);
1014 }
1015}
1016
1017static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1018{
1019 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +03001020 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -06001021 !ctx->sqo_task->mm ||
1022 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -06001023 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -06001024 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -06001025 }
1026
1027 return 0;
1028}
1029
1030static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1031 struct io_kiocb *req)
1032{
1033 if (!io_op_defs[req->opcode].needs_mm)
1034 return 0;
1035 return __io_sq_thread_acquire_mm(ctx);
1036}
1037
Dennis Zhou91d8f512020-09-16 13:41:05 -07001038static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1039 struct cgroup_subsys_state **cur_css)
1040
1041{
1042#ifdef CONFIG_BLK_CGROUP
1043 /* puts the old one when swapping */
1044 if (*cur_css != ctx->sqo_blkcg_css) {
1045 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1046 *cur_css = ctx->sqo_blkcg_css;
1047 }
1048#endif
1049}
1050
1051static void io_sq_thread_unassociate_blkcg(void)
1052{
1053#ifdef CONFIG_BLK_CGROUP
1054 kthread_associate_blkcg(NULL);
1055#endif
1056}
1057
Jens Axboec40f6372020-06-25 15:39:59 -06001058static inline void req_set_fail_links(struct io_kiocb *req)
1059{
1060 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1061 req->flags |= REQ_F_FAIL_LINK;
1062}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001063
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001064/*
1065 * Note: must call io_req_init_async() for the first time you
1066 * touch any members of io_wq_work.
1067 */
1068static inline void io_req_init_async(struct io_kiocb *req)
1069{
1070 if (req->flags & REQ_F_WORK_INITIALIZED)
1071 return;
1072
1073 memset(&req->work, 0, sizeof(req->work));
1074 req->flags |= REQ_F_WORK_INITIALIZED;
1075}
1076
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001077static inline bool io_async_submit(struct io_ring_ctx *ctx)
1078{
1079 return ctx->flags & IORING_SETUP_SQPOLL;
1080}
1081
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1083{
1084 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1085
Jens Axboe0f158b42020-05-14 17:18:39 -06001086 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087}
1088
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001089static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1090{
1091 return !req->timeout.off;
1092}
1093
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1095{
1096 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001097 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098
1099 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1100 if (!ctx)
1101 return NULL;
1102
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001103 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1104 if (!ctx->fallback_req)
1105 goto err;
1106
Jens Axboe78076bb2019-12-04 19:56:40 -07001107 /*
1108 * Use 5 bits less than the max cq entries, that should give us around
1109 * 32 entries per hash list if totally full and uniformly spread.
1110 */
1111 hash_bits = ilog2(p->cq_entries);
1112 hash_bits -= 5;
1113 if (hash_bits <= 0)
1114 hash_bits = 1;
1115 ctx->cancel_hash_bits = hash_bits;
1116 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1117 GFP_KERNEL);
1118 if (!ctx->cancel_hash)
1119 goto err;
1120 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1121
Roman Gushchin21482892019-05-07 10:01:48 -07001122 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001123 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1124 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125
1126 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001127 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001128 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001130 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001131 init_completion(&ctx->ref_comp);
1132 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001133 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001134 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001135 mutex_init(&ctx->uring_lock);
1136 init_waitqueue_head(&ctx->wait);
1137 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001138 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001139 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001140 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001141 init_waitqueue_head(&ctx->inflight_wait);
1142 spin_lock_init(&ctx->inflight_lock);
1143 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001144 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1145 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001147err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001148 if (ctx->fallback_req)
1149 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001150 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001151 kfree(ctx);
1152 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001153}
1154
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001155static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001156{
Jens Axboe2bc99302020-07-09 09:43:27 -06001157 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1158 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001159
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001160 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001161 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001162 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001163
Bob Liu9d858b22019-11-13 18:06:25 +08001164 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001165}
1166
Jens Axboede0617e2019-04-06 21:51:27 -06001167static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168{
Hristo Venev75b28af2019-08-26 17:23:46 +00001169 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170
Pavel Begunkov07910152020-01-17 03:52:46 +03001171 /* order cqe stores with ring update */
1172 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173
Pavel Begunkov07910152020-01-17 03:52:46 +03001174 if (wq_has_sleeper(&ctx->cq_wait)) {
1175 wake_up_interruptible(&ctx->cq_wait);
1176 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177 }
1178}
1179
Jens Axboe51a4cc12020-08-10 10:55:56 -06001180/*
1181 * Returns true if we need to defer file table putting. This can only happen
1182 * from the error path with REQ_F_COMP_LOCKED set.
1183 */
1184static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001185{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001186 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001187 return false;
1188
1189 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001190
Jens Axboecccf0ee2020-01-27 16:34:48 -07001191 if (req->work.mm) {
1192 mmdrop(req->work.mm);
1193 req->work.mm = NULL;
1194 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001195#ifdef CONFIG_BLK_CGROUP
1196 if (req->work.blkcg_css)
1197 css_put(req->work.blkcg_css);
1198#endif
Jens Axboecccf0ee2020-01-27 16:34:48 -07001199 if (req->work.creds) {
1200 put_cred(req->work.creds);
1201 req->work.creds = NULL;
1202 }
Jens Axboeff002b32020-02-07 16:05:21 -07001203 if (req->work.fs) {
1204 struct fs_struct *fs = req->work.fs;
1205
Jens Axboe51a4cc12020-08-10 10:55:56 -06001206 if (req->flags & REQ_F_COMP_LOCKED)
1207 return true;
1208
Jens Axboeff002b32020-02-07 16:05:21 -07001209 spin_lock(&req->work.fs->lock);
1210 if (--fs->users)
1211 fs = NULL;
1212 spin_unlock(&req->work.fs->lock);
1213 if (fs)
1214 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001215 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001216 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001217
1218 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001219}
1220
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001221static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001222{
Jens Axboed3656342019-12-18 09:50:26 -07001223 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001224 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001225
Pavel Begunkov16d59802020-07-12 16:16:47 +03001226 io_req_init_async(req);
1227
Jens Axboed3656342019-12-18 09:50:26 -07001228 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001229 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001230 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001231 } else {
1232 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001233 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001234 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001235 if (!req->work.files && io_op_defs[req->opcode].file_table &&
1236 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1237 req->work.files = get_files_struct(current);
1238 get_nsproxy(current->nsproxy);
1239 req->work.nsproxy = current->nsproxy;
1240 req->flags |= REQ_F_INFLIGHT;
1241
1242 spin_lock_irq(&ctx->inflight_lock);
1243 list_add(&req->inflight_entry, &ctx->inflight_list);
1244 spin_unlock_irq(&ctx->inflight_lock);
1245 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001246 if (!req->work.mm && def->needs_mm) {
1247 mmgrab(current->mm);
1248 req->work.mm = current->mm;
1249 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001250#ifdef CONFIG_BLK_CGROUP
1251 if (!req->work.blkcg_css && def->needs_blkcg) {
1252 rcu_read_lock();
1253 req->work.blkcg_css = blkcg_css();
1254 /*
1255 * This should be rare, either the cgroup is dying or the task
1256 * is moving cgroups. Just punt to root for the handful of ios.
1257 */
1258 if (!css_tryget_online(req->work.blkcg_css))
1259 req->work.blkcg_css = NULL;
1260 rcu_read_unlock();
1261 }
1262#endif
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001263 if (!req->work.creds)
1264 req->work.creds = get_current_cred();
1265 if (!req->work.fs && def->needs_fs) {
1266 spin_lock(&current->fs->lock);
1267 if (!current->fs->in_exec) {
1268 req->work.fs = current->fs;
1269 req->work.fs->users++;
1270 } else {
1271 req->work.flags |= IO_WQ_WORK_CANCEL;
1272 }
1273 spin_unlock(&current->fs->lock);
1274 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001275 if (def->needs_fsize)
1276 req->work.fsize = rlimit(RLIMIT_FSIZE);
1277 else
1278 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001279}
1280
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001281static void io_prep_async_link(struct io_kiocb *req)
1282{
1283 struct io_kiocb *cur;
1284
1285 io_prep_async_work(req);
1286 if (req->flags & REQ_F_LINK_HEAD)
1287 list_for_each_entry(cur, &req->link_list, link_list)
1288 io_prep_async_work(cur);
1289}
1290
Jens Axboe7271ef32020-08-10 09:55:22 -06001291static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001292{
Jackie Liua197f662019-11-08 08:09:12 -07001293 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001294 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001295
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001296 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1297 &req->work, req->flags);
1298 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001299 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001300}
1301
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001302static void io_queue_async_work(struct io_kiocb *req)
1303{
Jens Axboe7271ef32020-08-10 09:55:22 -06001304 struct io_kiocb *link;
1305
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001306 /* init ->work of the whole link before punting */
1307 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001308 link = __io_queue_async_work(req);
1309
1310 if (link)
1311 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001312}
1313
Jens Axboe5262f562019-09-17 12:26:57 -06001314static void io_kill_timeout(struct io_kiocb *req)
1315{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001316 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001317 int ret;
1318
Jens Axboee8c2bc12020-08-15 18:44:09 -07001319 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001320 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001321 atomic_set(&req->ctx->cq_timeouts,
1322 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001323 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001324 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001325 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001326 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001327 }
1328}
1329
Jens Axboef3606e32020-09-22 08:18:24 -06001330static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1331{
1332 struct io_ring_ctx *ctx = req->ctx;
1333
1334 if (!tsk || req->task == tsk)
1335 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001336 if (ctx->flags & IORING_SETUP_SQPOLL) {
1337 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1338 return true;
1339 }
Jens Axboef3606e32020-09-22 08:18:24 -06001340 return false;
1341}
1342
Jens Axboe76e1b642020-09-26 15:05:03 -06001343/*
1344 * Returns true if we found and killed one or more timeouts
1345 */
1346static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001347{
1348 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001349 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001350
1351 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001352 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001353 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001354 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001355 canceled++;
1356 }
Jens Axboef3606e32020-09-22 08:18:24 -06001357 }
Jens Axboe5262f562019-09-17 12:26:57 -06001358 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001359 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001360}
1361
Pavel Begunkov04518942020-05-26 20:34:05 +03001362static void __io_queue_deferred(struct io_ring_ctx *ctx)
1363{
1364 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001365 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1366 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001367 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001368
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001369 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001370 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001371 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001372 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001373 link = __io_queue_async_work(de->req);
1374 if (link) {
1375 __io_queue_linked_timeout(link);
1376 /* drop submission reference */
1377 link->flags |= REQ_F_COMP_LOCKED;
1378 io_put_req(link);
1379 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001380 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001381 } while (!list_empty(&ctx->defer_list));
1382}
1383
Pavel Begunkov360428f2020-05-30 14:54:17 +03001384static void io_flush_timeouts(struct io_ring_ctx *ctx)
1385{
1386 while (!list_empty(&ctx->timeout_list)) {
1387 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001388 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001389
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001390 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001391 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001392 if (req->timeout.target_seq != ctx->cached_cq_tail
1393 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001394 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001395
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001396 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001397 io_kill_timeout(req);
1398 }
1399}
1400
Jens Axboede0617e2019-04-06 21:51:27 -06001401static void io_commit_cqring(struct io_ring_ctx *ctx)
1402{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001403 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001404 __io_commit_cqring(ctx);
1405
Pavel Begunkov04518942020-05-26 20:34:05 +03001406 if (unlikely(!list_empty(&ctx->defer_list)))
1407 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001408}
1409
Jens Axboe90554202020-09-03 12:12:41 -06001410static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1411{
1412 struct io_rings *r = ctx->rings;
1413
1414 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1415}
1416
Jens Axboe2b188cc2019-01-07 10:46:33 -07001417static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1418{
Hristo Venev75b28af2019-08-26 17:23:46 +00001419 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 unsigned tail;
1421
1422 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001423 /*
1424 * writes to the cq entry need to come after reading head; the
1425 * control dependency is enough as we're using WRITE_ONCE to
1426 * fill the cq entry
1427 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001428 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429 return NULL;
1430
1431 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001432 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001433}
1434
Jens Axboef2842ab2020-01-08 11:04:00 -07001435static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1436{
Jens Axboef0b493e2020-02-01 21:30:11 -07001437 if (!ctx->cq_ev_fd)
1438 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001439 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1440 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001441 if (!ctx->eventfd_async)
1442 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001443 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001444}
1445
Jens Axboeb41e9852020-02-17 09:52:41 -07001446static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001447{
1448 if (waitqueue_active(&ctx->wait))
1449 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001450 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1451 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001452 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001453 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001454}
1455
Pavel Begunkov46930142020-07-30 18:43:49 +03001456static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1457{
1458 if (list_empty(&ctx->cq_overflow_list)) {
1459 clear_bit(0, &ctx->sq_check_overflow);
1460 clear_bit(0, &ctx->cq_check_overflow);
1461 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1462 }
1463}
1464
Jens Axboee6c8aa92020-09-28 13:10:13 -06001465static inline bool io_match_files(struct io_kiocb *req,
1466 struct files_struct *files)
1467{
1468 if (!files)
1469 return true;
1470 if (req->flags & REQ_F_WORK_INITIALIZED)
1471 return req->work.files == files;
1472 return false;
1473}
1474
Jens Axboec4a2ed72019-11-21 21:01:26 -07001475/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001476static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1477 struct task_struct *tsk,
1478 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001480 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001481 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001482 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001483 unsigned long flags;
1484 LIST_HEAD(list);
1485
1486 if (!force) {
1487 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001488 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001489 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1490 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001491 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001492 }
1493
1494 spin_lock_irqsave(&ctx->completion_lock, flags);
1495
1496 /* if force is set, the ring is going away. always drop after that */
1497 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001498 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001499
Jens Axboec4a2ed72019-11-21 21:01:26 -07001500 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001501 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1502 if (tsk && req->task != tsk)
1503 continue;
1504 if (!io_match_files(req, files))
1505 continue;
1506
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001507 cqe = io_get_cqring(ctx);
1508 if (!cqe && !force)
1509 break;
1510
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001511 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001512 if (cqe) {
1513 WRITE_ONCE(cqe->user_data, req->user_data);
1514 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001515 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001516 } else {
1517 WRITE_ONCE(ctx->rings->cq_overflow,
1518 atomic_inc_return(&ctx->cached_cq_overflow));
1519 }
1520 }
1521
1522 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001523 io_cqring_mark_overflow(ctx);
1524
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001525 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1526 io_cqring_ev_posted(ctx);
1527
1528 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001529 req = list_first_entry(&list, struct io_kiocb, compl.list);
1530 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001531 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001532 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001533
1534 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001535}
1536
Jens Axboebcda7ba2020-02-23 16:42:51 -07001537static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001539 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001540 struct io_uring_cqe *cqe;
1541
Jens Axboe78e19bb2019-11-06 15:21:34 -07001542 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001543
Jens Axboe2b188cc2019-01-07 10:46:33 -07001544 /*
1545 * If we can't get a cq entry, userspace overflowed the
1546 * submission (by quite a lot). Increment the overflow count in
1547 * the ring.
1548 */
1549 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001550 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001551 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001552 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001553 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001554 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1555 /*
1556 * If we're in ring overflow flush mode, or in task cancel mode,
1557 * then we cannot store the request for later flushing, we need
1558 * to drop it on the floor.
1559 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001560 WRITE_ONCE(ctx->rings->cq_overflow,
1561 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001562 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001563 if (list_empty(&ctx->cq_overflow_list)) {
1564 set_bit(0, &ctx->sq_check_overflow);
1565 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001566 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001567 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001568 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001569 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001570 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001571 refcount_inc(&req->refs);
1572 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001573 }
1574}
1575
Jens Axboebcda7ba2020-02-23 16:42:51 -07001576static void io_cqring_fill_event(struct io_kiocb *req, long res)
1577{
1578 __io_cqring_fill_event(req, res, 0);
1579}
1580
Jens Axboee1e16092020-06-22 09:17:17 -06001581static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001582{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001583 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001584 unsigned long flags;
1585
1586 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001587 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001588 io_commit_cqring(ctx);
1589 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1590
Jens Axboe8c838782019-03-12 15:48:16 -06001591 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001592}
1593
Jens Axboe229a7b62020-06-22 10:13:11 -06001594static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001595{
Jens Axboe229a7b62020-06-22 10:13:11 -06001596 struct io_ring_ctx *ctx = cs->ctx;
1597
1598 spin_lock_irq(&ctx->completion_lock);
1599 while (!list_empty(&cs->list)) {
1600 struct io_kiocb *req;
1601
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001602 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1603 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001604 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001605 if (!(req->flags & REQ_F_LINK_HEAD)) {
1606 req->flags |= REQ_F_COMP_LOCKED;
1607 io_put_req(req);
1608 } else {
1609 spin_unlock_irq(&ctx->completion_lock);
1610 io_put_req(req);
1611 spin_lock_irq(&ctx->completion_lock);
1612 }
1613 }
1614 io_commit_cqring(ctx);
1615 spin_unlock_irq(&ctx->completion_lock);
1616
1617 io_cqring_ev_posted(ctx);
1618 cs->nr = 0;
1619}
1620
1621static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1622 struct io_comp_state *cs)
1623{
1624 if (!cs) {
1625 io_cqring_add_event(req, res, cflags);
1626 io_put_req(req);
1627 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001628 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001629 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001630 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001631 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001632 if (++cs->nr >= 32)
1633 io_submit_flush_completions(cs);
1634 }
Jens Axboee1e16092020-06-22 09:17:17 -06001635}
1636
1637static void io_req_complete(struct io_kiocb *req, long res)
1638{
Jens Axboe229a7b62020-06-22 10:13:11 -06001639 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001640}
1641
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001642static inline bool io_is_fallback_req(struct io_kiocb *req)
1643{
1644 return req == (struct io_kiocb *)
1645 ((unsigned long) req->ctx->fallback_req & ~1UL);
1646}
1647
1648static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1649{
1650 struct io_kiocb *req;
1651
1652 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001653 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001654 return req;
1655
1656 return NULL;
1657}
1658
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001659static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1660 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001661{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001662 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001663 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001664 size_t sz;
1665 int ret;
1666
1667 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001668 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1669
1670 /*
1671 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1672 * retry single alloc to be on the safe side.
1673 */
1674 if (unlikely(ret <= 0)) {
1675 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1676 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001677 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001678 ret = 1;
1679 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001680 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681 }
1682
Pavel Begunkov291b2822020-09-30 22:57:01 +03001683 state->free_reqs--;
1684 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001685fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001686 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001687}
1688
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001689static inline void io_put_file(struct io_kiocb *req, struct file *file,
1690 bool fixed)
1691{
1692 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001693 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001694 else
1695 fput(file);
1696}
1697
Jens Axboe51a4cc12020-08-10 10:55:56 -06001698static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001699{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001700 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001701
Jens Axboee8c2bc12020-08-15 18:44:09 -07001702 if (req->async_data)
1703 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001704 if (req->file)
1705 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001706
Jens Axboe51a4cc12020-08-10 10:55:56 -06001707 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001708}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001709
Jens Axboe51a4cc12020-08-10 10:55:56 -06001710static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001711{
Jens Axboe0f212202020-09-13 13:09:39 -06001712 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001713 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001714
Jens Axboe0f212202020-09-13 13:09:39 -06001715 atomic_long_inc(&tctx->req_complete);
1716 if (tctx->in_idle)
1717 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001718 put_task_struct(req->task);
1719
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001720 if (likely(!io_is_fallback_req(req)))
1721 kmem_cache_free(req_cachep, req);
1722 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001723 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1724 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001725}
1726
Jens Axboe51a4cc12020-08-10 10:55:56 -06001727static void io_req_task_file_table_put(struct callback_head *cb)
1728{
1729 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1730 struct fs_struct *fs = req->work.fs;
1731
1732 spin_lock(&req->work.fs->lock);
1733 if (--fs->users)
1734 fs = NULL;
1735 spin_unlock(&req->work.fs->lock);
1736 if (fs)
1737 free_fs_struct(fs);
1738 req->work.fs = NULL;
1739 __io_free_req_finish(req);
1740}
1741
1742static void __io_free_req(struct io_kiocb *req)
1743{
1744 if (!io_dismantle_req(req)) {
1745 __io_free_req_finish(req);
1746 } else {
1747 int ret;
1748
1749 init_task_work(&req->task_work, io_req_task_file_table_put);
1750 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1751 if (unlikely(ret)) {
1752 struct task_struct *tsk;
1753
1754 tsk = io_wq_get_task(req->ctx->io_wq);
1755 task_work_add(tsk, &req->task_work, 0);
1756 }
1757 }
1758}
1759
Jackie Liua197f662019-11-08 08:09:12 -07001760static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001761{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001762 struct io_timeout_data *io = req->async_data;
Jackie Liua197f662019-11-08 08:09:12 -07001763 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001764 int ret;
1765
Jens Axboee8c2bc12020-08-15 18:44:09 -07001766 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001767 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001768 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001769 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001770 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001771 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001772 return true;
1773 }
1774
1775 return false;
1776}
1777
Jens Axboeab0b6452020-06-30 08:43:15 -06001778static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001779{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001780 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001781 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001782
1783 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001784 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001785 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1786 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001787 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001788
1789 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001790 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001791 wake_ev = io_link_cancel_timeout(link);
1792 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001793 return wake_ev;
1794}
1795
1796static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001797{
Jens Axboe2665abf2019-11-05 12:40:47 -07001798 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001799 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001800
Jens Axboeab0b6452020-06-30 08:43:15 -06001801 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1802 unsigned long flags;
1803
1804 spin_lock_irqsave(&ctx->completion_lock, flags);
1805 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001806 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001807 } else {
1808 wake_ev = __io_kill_linked_timeout(req);
1809 }
1810
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001811 if (wake_ev)
1812 io_cqring_ev_posted(ctx);
1813}
1814
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001815static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001816{
1817 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001818
Jens Axboe9e645e112019-05-10 16:07:28 -06001819 /*
1820 * The list should never be empty when we are called here. But could
1821 * potentially happen if the chain is messed up, check to be on the
1822 * safe side.
1823 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001824 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001825 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001826
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001827 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1828 list_del_init(&req->link_list);
1829 if (!list_empty(&nxt->link_list))
1830 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001831 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001832}
1833
1834/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001835 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001836 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001837static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001838{
Jens Axboe2665abf2019-11-05 12:40:47 -07001839 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001840
1841 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001842 struct io_kiocb *link = list_first_entry(&req->link_list,
1843 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001844
Pavel Begunkov44932332019-12-05 16:16:35 +03001845 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001846 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001847
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001848 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001849 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001850 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001851 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001852 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001853
1854 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001855 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001856}
1857
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001858static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001859{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001860 struct io_ring_ctx *ctx = req->ctx;
1861
1862 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1863 unsigned long flags;
1864
1865 spin_lock_irqsave(&ctx->completion_lock, flags);
1866 __io_fail_links(req);
1867 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1868 } else {
1869 __io_fail_links(req);
1870 }
1871
Jens Axboe9e645e112019-05-10 16:07:28 -06001872 io_cqring_ev_posted(ctx);
1873}
1874
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001875static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001876{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001877 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001878 if (req->flags & REQ_F_LINK_TIMEOUT)
1879 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001880
Jens Axboe9e645e112019-05-10 16:07:28 -06001881 /*
1882 * If LINK is set, we have dependent requests in this chain. If we
1883 * didn't fail this request, queue the first one up, moving any other
1884 * dependencies to the next request. In case of failure, fail the rest
1885 * of the chain.
1886 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001887 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1888 return io_req_link_next(req);
1889 io_fail_links(req);
1890 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001891}
Jens Axboe2665abf2019-11-05 12:40:47 -07001892
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001893static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1894{
1895 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1896 return NULL;
1897 return __io_req_find_next(req);
1898}
1899
Jens Axboe87c43112020-09-30 21:00:14 -06001900static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001901{
1902 struct task_struct *tsk = req->task;
1903 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001904 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001905
Jens Axboe6200b0a2020-09-13 14:38:30 -06001906 if (tsk->flags & PF_EXITING)
1907 return -ESRCH;
1908
Jens Axboec2c4c832020-07-01 15:37:11 -06001909 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001910 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1911 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1912 * processing task_work. There's no reliable way to tell if TWA_RESUME
1913 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001914 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001915 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001916 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001917 notify = TWA_SIGNAL;
1918
Jens Axboe87c43112020-09-30 21:00:14 -06001919 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001920 if (!ret)
1921 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001922
Jens Axboec2c4c832020-07-01 15:37:11 -06001923 return ret;
1924}
1925
Jens Axboec40f6372020-06-25 15:39:59 -06001926static void __io_req_task_cancel(struct io_kiocb *req, int error)
1927{
1928 struct io_ring_ctx *ctx = req->ctx;
1929
1930 spin_lock_irq(&ctx->completion_lock);
1931 io_cqring_fill_event(req, error);
1932 io_commit_cqring(ctx);
1933 spin_unlock_irq(&ctx->completion_lock);
1934
1935 io_cqring_ev_posted(ctx);
1936 req_set_fail_links(req);
1937 io_double_put_req(req);
1938}
1939
1940static void io_req_task_cancel(struct callback_head *cb)
1941{
1942 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001943 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001944
1945 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001946 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001947}
1948
1949static void __io_req_task_submit(struct io_kiocb *req)
1950{
1951 struct io_ring_ctx *ctx = req->ctx;
1952
Jens Axboec40f6372020-06-25 15:39:59 -06001953 if (!__io_sq_thread_acquire_mm(ctx)) {
1954 mutex_lock(&ctx->uring_lock);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001955 __io_queue_sqe(req, NULL);
Jens Axboec40f6372020-06-25 15:39:59 -06001956 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001957 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001958 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001959 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001960}
1961
Jens Axboec40f6372020-06-25 15:39:59 -06001962static void io_req_task_submit(struct callback_head *cb)
1963{
1964 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001965 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001966
1967 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001968 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001969}
1970
1971static void io_req_task_queue(struct io_kiocb *req)
1972{
Jens Axboec40f6372020-06-25 15:39:59 -06001973 int ret;
1974
1975 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001976 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001977
Jens Axboe87c43112020-09-30 21:00:14 -06001978 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001979 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001980 struct task_struct *tsk;
1981
Jens Axboec40f6372020-06-25 15:39:59 -06001982 init_task_work(&req->task_work, io_req_task_cancel);
1983 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001984 task_work_add(tsk, &req->task_work, 0);
1985 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001986 }
Jens Axboec40f6372020-06-25 15:39:59 -06001987}
1988
Pavel Begunkovc3524382020-06-28 12:52:32 +03001989static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001990{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001991 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001992
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001993 if (nxt)
1994 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001995}
1996
Jens Axboe9e645e112019-05-10 16:07:28 -06001997static void io_free_req(struct io_kiocb *req)
1998{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001999 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002000 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002001}
2002
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002003struct req_batch {
2004 void *reqs[IO_IOPOLL_BATCH];
2005 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002006
2007 struct task_struct *task;
2008 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002009};
2010
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002011static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002012{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002013 rb->to_free = 0;
2014 rb->task_refs = 0;
2015 rb->task = NULL;
2016}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002017
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002018static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2019 struct req_batch *rb)
2020{
2021 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2022 percpu_ref_put_many(&ctx->refs, rb->to_free);
2023 rb->to_free = 0;
2024}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002025
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002026static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2027 struct req_batch *rb)
2028{
2029 if (rb->to_free)
2030 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002031 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002032 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002033 put_task_struct_many(rb->task, rb->task_refs);
2034 rb->task = NULL;
2035 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002036}
2037
2038static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2039{
2040 if (unlikely(io_is_fallback_req(req))) {
2041 io_free_req(req);
2042 return;
2043 }
2044 if (req->flags & REQ_F_LINK_HEAD)
2045 io_queue_next(req);
2046
Jens Axboee3bc8e92020-09-24 08:45:57 -06002047 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002048 if (rb->task) {
2049 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002050 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06002051 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002052 rb->task = req->task;
2053 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002054 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002055 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002056
Jens Axboe51a4cc12020-08-10 10:55:56 -06002057 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002058 rb->reqs[rb->to_free++] = req;
2059 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2060 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002061}
2062
Jens Axboeba816ad2019-09-28 11:36:45 -06002063/*
2064 * Drop reference to request, return next in chain (if there is one) if this
2065 * was the last reference to this request.
2066 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002067static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002068{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002069 struct io_kiocb *nxt = NULL;
2070
Jens Axboe2a44f462020-02-25 13:25:41 -07002071 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002072 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002073 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002074 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002075 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002076}
2077
Jens Axboe2b188cc2019-01-07 10:46:33 -07002078static void io_put_req(struct io_kiocb *req)
2079{
Jens Axboedef596e2019-01-09 08:59:42 -07002080 if (refcount_dec_and_test(&req->refs))
2081 io_free_req(req);
2082}
2083
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002084static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002085{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002086 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002087
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002088 /*
2089 * A ref is owned by io-wq in which context we're. So, if that's the
2090 * last one, it's safe to steal next work. False negatives are Ok,
2091 * it just will be re-punted async in io_put_work()
2092 */
2093 if (refcount_read(&req->refs) != 1)
2094 return NULL;
2095
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002096 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002097 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002098}
2099
Jens Axboe978db572019-11-14 22:39:04 -07002100/*
2101 * Must only be used if we don't need to care about links, usually from
2102 * within the completion handling itself.
2103 */
2104static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002105{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002106 /* drop both submit and complete references */
2107 if (refcount_sub_and_test(2, &req->refs))
2108 __io_free_req(req);
2109}
2110
Jens Axboe978db572019-11-14 22:39:04 -07002111static void io_double_put_req(struct io_kiocb *req)
2112{
2113 /* drop both submit and complete references */
2114 if (refcount_sub_and_test(2, &req->refs))
2115 io_free_req(req);
2116}
2117
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002118static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002119{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002120 struct io_rings *rings = ctx->rings;
2121
Jens Axboead3eb2c2019-12-18 17:12:20 -07002122 if (test_bit(0, &ctx->cq_check_overflow)) {
2123 /*
2124 * noflush == true is from the waitqueue handler, just ensure
2125 * we wake up the task, and the next invocation will flush the
2126 * entries. We cannot safely to it from here.
2127 */
2128 if (noflush && !list_empty(&ctx->cq_overflow_list))
2129 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002130
Jens Axboee6c8aa92020-09-28 13:10:13 -06002131 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002132 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002133
Jens Axboea3a0e432019-08-20 11:03:11 -06002134 /* See comment at the top of this file */
2135 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002136 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002137}
2138
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002139static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2140{
2141 struct io_rings *rings = ctx->rings;
2142
2143 /* make sure SQ entry isn't read before tail */
2144 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2145}
2146
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002147static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002148{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002149 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002150
Jens Axboebcda7ba2020-02-23 16:42:51 -07002151 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2152 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002153 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002154 kfree(kbuf);
2155 return cflags;
2156}
2157
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002158static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2159{
2160 struct io_buffer *kbuf;
2161
2162 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2163 return io_put_kbuf(req, kbuf);
2164}
2165
Jens Axboe4c6e2772020-07-01 11:29:10 -06002166static inline bool io_run_task_work(void)
2167{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002168 /*
2169 * Not safe to run on exiting task, and the task_work handling will
2170 * not add work to such a task.
2171 */
2172 if (unlikely(current->flags & PF_EXITING))
2173 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002174 if (current->task_works) {
2175 __set_current_state(TASK_RUNNING);
2176 task_work_run();
2177 return true;
2178 }
2179
2180 return false;
2181}
2182
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002183static void io_iopoll_queue(struct list_head *again)
2184{
2185 struct io_kiocb *req;
2186
2187 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002188 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2189 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002190 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002191 } while (!list_empty(again));
2192}
2193
Jens Axboedef596e2019-01-09 08:59:42 -07002194/*
2195 * Find and free completed poll iocbs
2196 */
2197static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2198 struct list_head *done)
2199{
Jens Axboe8237e042019-12-28 10:48:22 -07002200 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002201 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002202 LIST_HEAD(again);
2203
2204 /* order with ->result store in io_complete_rw_iopoll() */
2205 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002206
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002207 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002208 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002209 int cflags = 0;
2210
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002211 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002212 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002213 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002214 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002215 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002216 continue;
2217 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002218 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002219
Jens Axboebcda7ba2020-02-23 16:42:51 -07002220 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002221 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002222
2223 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002224 (*nr_events)++;
2225
Pavel Begunkovc3524382020-06-28 12:52:32 +03002226 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002227 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002228 }
Jens Axboedef596e2019-01-09 08:59:42 -07002229
Jens Axboe09bb8392019-03-13 12:39:28 -06002230 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002231 if (ctx->flags & IORING_SETUP_SQPOLL)
2232 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002233 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002234
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002235 if (!list_empty(&again))
2236 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002237}
2238
Jens Axboedef596e2019-01-09 08:59:42 -07002239static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2240 long min)
2241{
2242 struct io_kiocb *req, *tmp;
2243 LIST_HEAD(done);
2244 bool spin;
2245 int ret;
2246
2247 /*
2248 * Only spin for completions if we don't have multiple devices hanging
2249 * off our complete list, and we're under the requested amount.
2250 */
2251 spin = !ctx->poll_multi_file && *nr_events < min;
2252
2253 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002254 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002255 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002256
2257 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002258 * Move completed and retryable entries to our local lists.
2259 * If we find a request that requires polling, break out
2260 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002261 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002262 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002263 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002264 continue;
2265 }
2266 if (!list_empty(&done))
2267 break;
2268
2269 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2270 if (ret < 0)
2271 break;
2272
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002273 /* iopoll may have completed current req */
2274 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002275 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002276
Jens Axboedef596e2019-01-09 08:59:42 -07002277 if (ret && spin)
2278 spin = false;
2279 ret = 0;
2280 }
2281
2282 if (!list_empty(&done))
2283 io_iopoll_complete(ctx, nr_events, &done);
2284
2285 return ret;
2286}
2287
2288/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002289 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002290 * non-spinning poll check - we'll still enter the driver poll loop, but only
2291 * as a non-spinning completion check.
2292 */
2293static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2294 long min)
2295{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002296 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002297 int ret;
2298
2299 ret = io_do_iopoll(ctx, nr_events, min);
2300 if (ret < 0)
2301 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002302 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002303 return 0;
2304 }
2305
2306 return 1;
2307}
2308
2309/*
2310 * We can't just wait for polled events to come to us, we have to actively
2311 * find and complete them.
2312 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002313static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002314{
2315 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2316 return;
2317
2318 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002319 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002320 unsigned int nr_events = 0;
2321
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002322 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002323
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002324 /* let it sleep and repeat later if can't complete a request */
2325 if (nr_events == 0)
2326 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002327 /*
2328 * Ensure we allow local-to-the-cpu processing to take place,
2329 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002330 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002331 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002332 if (need_resched()) {
2333 mutex_unlock(&ctx->uring_lock);
2334 cond_resched();
2335 mutex_lock(&ctx->uring_lock);
2336 }
Jens Axboedef596e2019-01-09 08:59:42 -07002337 }
2338 mutex_unlock(&ctx->uring_lock);
2339}
2340
Pavel Begunkov7668b922020-07-07 16:36:21 +03002341static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002342{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002343 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002344 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002345
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002346 /*
2347 * We disallow the app entering submit/complete with polling, but we
2348 * still need to lock the ring to prevent racing with polled issue
2349 * that got punted to a workqueue.
2350 */
2351 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002352 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002353 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002354 * Don't enter poll loop if we already have events pending.
2355 * If we do, we can potentially be spinning for commands that
2356 * already triggered a CQE (eg in error).
2357 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002358 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002359 break;
2360
2361 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002362 * If a submit got punted to a workqueue, we can have the
2363 * application entering polling for a command before it gets
2364 * issued. That app will hold the uring_lock for the duration
2365 * of the poll right here, so we need to take a breather every
2366 * now and then to ensure that the issue has a chance to add
2367 * the poll to the issued list. Otherwise we can spin here
2368 * forever, while the workqueue is stuck trying to acquire the
2369 * very same mutex.
2370 */
2371 if (!(++iters & 7)) {
2372 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002373 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002374 mutex_lock(&ctx->uring_lock);
2375 }
2376
Pavel Begunkov7668b922020-07-07 16:36:21 +03002377 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002378 if (ret <= 0)
2379 break;
2380 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002381 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002382
Jens Axboe500f9fb2019-08-19 12:15:59 -06002383 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002384 return ret;
2385}
2386
Jens Axboe491381ce2019-10-17 09:20:46 -06002387static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002388{
Jens Axboe491381ce2019-10-17 09:20:46 -06002389 /*
2390 * Tell lockdep we inherited freeze protection from submission
2391 * thread.
2392 */
2393 if (req->flags & REQ_F_ISREG) {
2394 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002395
Jens Axboe491381ce2019-10-17 09:20:46 -06002396 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002397 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002398 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002399}
2400
Jens Axboea1d7c392020-06-22 11:09:46 -06002401static void io_complete_rw_common(struct kiocb *kiocb, long res,
2402 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002403{
Jens Axboe9adbd452019-12-20 08:45:55 -07002404 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002405 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002406
Jens Axboe491381ce2019-10-17 09:20:46 -06002407 if (kiocb->ki_flags & IOCB_WRITE)
2408 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002409
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002410 if (res != req->result)
2411 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002412 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002413 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002414 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002415}
2416
Jens Axboeb63534c2020-06-04 11:28:00 -06002417#ifdef CONFIG_BLOCK
2418static bool io_resubmit_prep(struct io_kiocb *req, int error)
2419{
2420 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2421 ssize_t ret = -ECANCELED;
2422 struct iov_iter iter;
2423 int rw;
2424
2425 if (error) {
2426 ret = error;
2427 goto end_req;
2428 }
2429
2430 switch (req->opcode) {
2431 case IORING_OP_READV:
2432 case IORING_OP_READ_FIXED:
2433 case IORING_OP_READ:
2434 rw = READ;
2435 break;
2436 case IORING_OP_WRITEV:
2437 case IORING_OP_WRITE_FIXED:
2438 case IORING_OP_WRITE:
2439 rw = WRITE;
2440 break;
2441 default:
2442 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2443 req->opcode);
2444 goto end_req;
2445 }
2446
Jens Axboee8c2bc12020-08-15 18:44:09 -07002447 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002448 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2449 if (ret < 0)
2450 goto end_req;
2451 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2452 if (!ret)
2453 return true;
2454 kfree(iovec);
2455 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002456 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002457 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002458end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002459 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002460 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002461 return false;
2462}
Jens Axboeb63534c2020-06-04 11:28:00 -06002463#endif
2464
2465static bool io_rw_reissue(struct io_kiocb *req, long res)
2466{
2467#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002468 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002469 int ret;
2470
Jens Axboe355afae2020-09-02 09:30:31 -06002471 if (!S_ISBLK(mode) && !S_ISREG(mode))
2472 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002473 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2474 return false;
2475
Jens Axboefdee9462020-08-27 16:46:24 -06002476 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002477
Jens Axboefdee9462020-08-27 16:46:24 -06002478 if (io_resubmit_prep(req, ret)) {
2479 refcount_inc(&req->refs);
2480 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002481 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002482 }
2483
Jens Axboeb63534c2020-06-04 11:28:00 -06002484#endif
2485 return false;
2486}
2487
Jens Axboea1d7c392020-06-22 11:09:46 -06002488static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2489 struct io_comp_state *cs)
2490{
2491 if (!io_rw_reissue(req, res))
2492 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002493}
2494
2495static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2496{
Jens Axboe9adbd452019-12-20 08:45:55 -07002497 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002498
Jens Axboea1d7c392020-06-22 11:09:46 -06002499 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002500}
2501
Jens Axboedef596e2019-01-09 08:59:42 -07002502static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2503{
Jens Axboe9adbd452019-12-20 08:45:55 -07002504 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002505
Jens Axboe491381ce2019-10-17 09:20:46 -06002506 if (kiocb->ki_flags & IOCB_WRITE)
2507 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002508
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002509 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002510 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002511
2512 WRITE_ONCE(req->result, res);
2513 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002514 smp_wmb();
2515 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002516}
2517
2518/*
2519 * After the iocb has been issued, it's safe to be found on the poll list.
2520 * Adding the kiocb to the list AFTER submission ensures that we don't
2521 * find it from a io_iopoll_getevents() thread before the issuer is done
2522 * accessing the kiocb cookie.
2523 */
2524static void io_iopoll_req_issued(struct io_kiocb *req)
2525{
2526 struct io_ring_ctx *ctx = req->ctx;
2527
2528 /*
2529 * Track whether we have multiple files in our lists. This will impact
2530 * how we do polling eventually, not spinning if we're on potentially
2531 * different devices.
2532 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002533 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002534 ctx->poll_multi_file = false;
2535 } else if (!ctx->poll_multi_file) {
2536 struct io_kiocb *list_req;
2537
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002538 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002539 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002540 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002541 ctx->poll_multi_file = true;
2542 }
2543
2544 /*
2545 * For fast devices, IO may have already completed. If it has, add
2546 * it to the front so we find it first.
2547 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002548 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002549 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002550 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002551 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002552
Jens Axboe534ca6d2020-09-02 13:52:19 -06002553 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2554 wq_has_sleeper(&ctx->sq_data->wait))
2555 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002556}
2557
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002558static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002559{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002560 if (state->has_refs)
2561 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002562 state->file = NULL;
2563}
2564
2565static inline void io_state_file_put(struct io_submit_state *state)
2566{
2567 if (state->file)
2568 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002569}
2570
2571/*
2572 * Get as many references to a file as we have IOs left in this submission,
2573 * assuming most submissions are for one file, or at least that each file
2574 * has more than one submission.
2575 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002576static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002577{
2578 if (!state)
2579 return fget(fd);
2580
2581 if (state->file) {
2582 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002583 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002584 state->ios_left--;
2585 return state->file;
2586 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002587 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002588 }
2589 state->file = fget_many(fd, state->ios_left);
2590 if (!state->file)
2591 return NULL;
2592
2593 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002594 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002595 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002596 return state->file;
2597}
2598
Jens Axboe4503b762020-06-01 10:00:27 -06002599static bool io_bdev_nowait(struct block_device *bdev)
2600{
2601#ifdef CONFIG_BLOCK
2602 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2603#else
2604 return true;
2605#endif
2606}
2607
Jens Axboe2b188cc2019-01-07 10:46:33 -07002608/*
2609 * If we tracked the file through the SCM inflight mechanism, we could support
2610 * any file. For now, just ensure that anything potentially problematic is done
2611 * inline.
2612 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002613static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002614{
2615 umode_t mode = file_inode(file)->i_mode;
2616
Jens Axboe4503b762020-06-01 10:00:27 -06002617 if (S_ISBLK(mode)) {
2618 if (io_bdev_nowait(file->f_inode->i_bdev))
2619 return true;
2620 return false;
2621 }
2622 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002623 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002624 if (S_ISREG(mode)) {
2625 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2626 file->f_op != &io_uring_fops)
2627 return true;
2628 return false;
2629 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002630
Jens Axboec5b85622020-06-09 19:23:05 -06002631 /* any ->read/write should understand O_NONBLOCK */
2632 if (file->f_flags & O_NONBLOCK)
2633 return true;
2634
Jens Axboeaf197f52020-04-28 13:15:06 -06002635 if (!(file->f_mode & FMODE_NOWAIT))
2636 return false;
2637
2638 if (rw == READ)
2639 return file->f_op->read_iter != NULL;
2640
2641 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002642}
2643
Pavel Begunkova88fc402020-09-30 22:57:53 +03002644static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002645{
Jens Axboedef596e2019-01-09 08:59:42 -07002646 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002647 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002648 unsigned ioprio;
2649 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650
Jens Axboe491381ce2019-10-17 09:20:46 -06002651 if (S_ISREG(file_inode(req->file)->i_mode))
2652 req->flags |= REQ_F_ISREG;
2653
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002655 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2656 req->flags |= REQ_F_CUR_POS;
2657 kiocb->ki_pos = req->file->f_pos;
2658 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002659 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002660 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2661 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2662 if (unlikely(ret))
2663 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002664
2665 ioprio = READ_ONCE(sqe->ioprio);
2666 if (ioprio) {
2667 ret = ioprio_check_cap(ioprio);
2668 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002669 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002670
2671 kiocb->ki_ioprio = ioprio;
2672 } else
2673 kiocb->ki_ioprio = get_current_ioprio();
2674
Stefan Bühler8449eed2019-04-27 20:34:19 +02002675 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002676 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002677 req->flags |= REQ_F_NOWAIT;
2678
Jens Axboedef596e2019-01-09 08:59:42 -07002679 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002680 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2681 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002682 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683
Jens Axboedef596e2019-01-09 08:59:42 -07002684 kiocb->ki_flags |= IOCB_HIPRI;
2685 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002686 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002687 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002688 if (kiocb->ki_flags & IOCB_HIPRI)
2689 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002690 kiocb->ki_complete = io_complete_rw;
2691 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002692
Jens Axboe3529d8c2019-12-19 18:24:38 -07002693 req->rw.addr = READ_ONCE(sqe->addr);
2694 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002695 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002696 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002697}
2698
2699static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2700{
2701 switch (ret) {
2702 case -EIOCBQUEUED:
2703 break;
2704 case -ERESTARTSYS:
2705 case -ERESTARTNOINTR:
2706 case -ERESTARTNOHAND:
2707 case -ERESTART_RESTARTBLOCK:
2708 /*
2709 * We can't just restart the syscall, since previously
2710 * submitted sqes may already be in progress. Just fail this
2711 * IO with EINTR.
2712 */
2713 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002714 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002715 default:
2716 kiocb->ki_complete(kiocb, ret, 0);
2717 }
2718}
2719
Jens Axboea1d7c392020-06-22 11:09:46 -06002720static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2721 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002722{
Jens Axboeba042912019-12-25 16:33:42 -07002723 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002724 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002725
Jens Axboe227c0c92020-08-13 11:51:40 -06002726 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002727 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002728 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002729 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002730 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002731 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002732 }
2733
Jens Axboeba042912019-12-25 16:33:42 -07002734 if (req->flags & REQ_F_CUR_POS)
2735 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002736 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002737 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002738 else
2739 io_rw_done(kiocb, ret);
2740}
2741
Jens Axboe9adbd452019-12-20 08:45:55 -07002742static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002743 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002744{
Jens Axboe9adbd452019-12-20 08:45:55 -07002745 struct io_ring_ctx *ctx = req->ctx;
2746 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002747 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002748 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002749 size_t offset;
2750 u64 buf_addr;
2751
Jens Axboeedafcce2019-01-09 09:16:05 -07002752 if (unlikely(buf_index >= ctx->nr_user_bufs))
2753 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002754 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2755 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002756 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002757
2758 /* overflow */
2759 if (buf_addr + len < buf_addr)
2760 return -EFAULT;
2761 /* not inside the mapped region */
2762 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2763 return -EFAULT;
2764
2765 /*
2766 * May not be a start of buffer, set size appropriately
2767 * and advance us to the beginning.
2768 */
2769 offset = buf_addr - imu->ubuf;
2770 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002771
2772 if (offset) {
2773 /*
2774 * Don't use iov_iter_advance() here, as it's really slow for
2775 * using the latter parts of a big fixed buffer - it iterates
2776 * over each segment manually. We can cheat a bit here, because
2777 * we know that:
2778 *
2779 * 1) it's a BVEC iter, we set it up
2780 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2781 * first and last bvec
2782 *
2783 * So just find our index, and adjust the iterator afterwards.
2784 * If the offset is within the first bvec (or the whole first
2785 * bvec, just use iov_iter_advance(). This makes it easier
2786 * since we can just skip the first segment, which may not
2787 * be PAGE_SIZE aligned.
2788 */
2789 const struct bio_vec *bvec = imu->bvec;
2790
2791 if (offset <= bvec->bv_len) {
2792 iov_iter_advance(iter, offset);
2793 } else {
2794 unsigned long seg_skip;
2795
2796 /* skip first vec */
2797 offset -= bvec->bv_len;
2798 seg_skip = 1 + (offset >> PAGE_SHIFT);
2799
2800 iter->bvec = bvec + seg_skip;
2801 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002802 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002803 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002804 }
2805 }
2806
Jens Axboe5e559562019-11-13 16:12:46 -07002807 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002808}
2809
Jens Axboebcda7ba2020-02-23 16:42:51 -07002810static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2811{
2812 if (needs_lock)
2813 mutex_unlock(&ctx->uring_lock);
2814}
2815
2816static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2817{
2818 /*
2819 * "Normal" inline submissions always hold the uring_lock, since we
2820 * grab it from the system call. Same is true for the SQPOLL offload.
2821 * The only exception is when we've detached the request and issue it
2822 * from an async worker thread, grab the lock for that case.
2823 */
2824 if (needs_lock)
2825 mutex_lock(&ctx->uring_lock);
2826}
2827
2828static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2829 int bgid, struct io_buffer *kbuf,
2830 bool needs_lock)
2831{
2832 struct io_buffer *head;
2833
2834 if (req->flags & REQ_F_BUFFER_SELECTED)
2835 return kbuf;
2836
2837 io_ring_submit_lock(req->ctx, needs_lock);
2838
2839 lockdep_assert_held(&req->ctx->uring_lock);
2840
2841 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2842 if (head) {
2843 if (!list_empty(&head->list)) {
2844 kbuf = list_last_entry(&head->list, struct io_buffer,
2845 list);
2846 list_del(&kbuf->list);
2847 } else {
2848 kbuf = head;
2849 idr_remove(&req->ctx->io_buffer_idr, bgid);
2850 }
2851 if (*len > kbuf->len)
2852 *len = kbuf->len;
2853 } else {
2854 kbuf = ERR_PTR(-ENOBUFS);
2855 }
2856
2857 io_ring_submit_unlock(req->ctx, needs_lock);
2858
2859 return kbuf;
2860}
2861
Jens Axboe4d954c22020-02-27 07:31:19 -07002862static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2863 bool needs_lock)
2864{
2865 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002866 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002867
2868 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002869 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002870 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2871 if (IS_ERR(kbuf))
2872 return kbuf;
2873 req->rw.addr = (u64) (unsigned long) kbuf;
2874 req->flags |= REQ_F_BUFFER_SELECTED;
2875 return u64_to_user_ptr(kbuf->addr);
2876}
2877
2878#ifdef CONFIG_COMPAT
2879static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2880 bool needs_lock)
2881{
2882 struct compat_iovec __user *uiov;
2883 compat_ssize_t clen;
2884 void __user *buf;
2885 ssize_t len;
2886
2887 uiov = u64_to_user_ptr(req->rw.addr);
2888 if (!access_ok(uiov, sizeof(*uiov)))
2889 return -EFAULT;
2890 if (__get_user(clen, &uiov->iov_len))
2891 return -EFAULT;
2892 if (clen < 0)
2893 return -EINVAL;
2894
2895 len = clen;
2896 buf = io_rw_buffer_select(req, &len, needs_lock);
2897 if (IS_ERR(buf))
2898 return PTR_ERR(buf);
2899 iov[0].iov_base = buf;
2900 iov[0].iov_len = (compat_size_t) len;
2901 return 0;
2902}
2903#endif
2904
2905static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2906 bool needs_lock)
2907{
2908 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2909 void __user *buf;
2910 ssize_t len;
2911
2912 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2913 return -EFAULT;
2914
2915 len = iov[0].iov_len;
2916 if (len < 0)
2917 return -EINVAL;
2918 buf = io_rw_buffer_select(req, &len, needs_lock);
2919 if (IS_ERR(buf))
2920 return PTR_ERR(buf);
2921 iov[0].iov_base = buf;
2922 iov[0].iov_len = len;
2923 return 0;
2924}
2925
2926static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2927 bool needs_lock)
2928{
Jens Axboedddb3e22020-06-04 11:27:01 -06002929 if (req->flags & REQ_F_BUFFER_SELECTED) {
2930 struct io_buffer *kbuf;
2931
2932 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2933 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2934 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002935 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002936 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002937 if (!req->rw.len)
2938 return 0;
2939 else if (req->rw.len > 1)
2940 return -EINVAL;
2941
2942#ifdef CONFIG_COMPAT
2943 if (req->ctx->compat)
2944 return io_compat_import(req, iov, needs_lock);
2945#endif
2946
2947 return __io_iov_buffer_select(req, iov, needs_lock);
2948}
2949
Jens Axboe8452fd02020-08-18 13:58:33 -07002950static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2951 struct iovec **iovec, struct iov_iter *iter,
2952 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002953{
Jens Axboe9adbd452019-12-20 08:45:55 -07002954 void __user *buf = u64_to_user_ptr(req->rw.addr);
2955 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002956 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002957 u8 opcode;
2958
Jens Axboed625c6e2019-12-17 19:53:05 -07002959 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002960 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002961 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002962 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002963 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002964
Jens Axboebcda7ba2020-02-23 16:42:51 -07002965 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002966 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002967 return -EINVAL;
2968
Jens Axboe3a6820f2019-12-22 15:19:35 -07002969 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002970 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002971 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002972 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002973 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002974 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002975 }
2976
Jens Axboe3a6820f2019-12-22 15:19:35 -07002977 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2978 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002979 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002980 }
2981
Jens Axboe4d954c22020-02-27 07:31:19 -07002982 if (req->flags & REQ_F_BUFFER_SELECT) {
2983 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002984 if (!ret) {
2985 ret = (*iovec)->iov_len;
2986 iov_iter_init(iter, rw, *iovec, 1, ret);
2987 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002988 *iovec = NULL;
2989 return ret;
2990 }
2991
Jens Axboe2b188cc2019-01-07 10:46:33 -07002992#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002993 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002994 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2995 iovec, iter);
2996#endif
2997
2998 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2999}
3000
Jens Axboe8452fd02020-08-18 13:58:33 -07003001static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
3002 struct iovec **iovec, struct iov_iter *iter,
3003 bool needs_lock)
3004{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003005 struct io_async_rw *iorw = req->async_data;
3006
3007 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07003008 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3009 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003010 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07003011}
3012
Jens Axboe0fef9482020-08-26 10:36:20 -06003013static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3014{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003015 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003016}
3017
Jens Axboe32960612019-09-23 11:05:34 -06003018/*
3019 * For files that don't have ->read_iter() and ->write_iter(), handle them
3020 * by looping over ->read() or ->write() manually.
3021 */
3022static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
3023 struct iov_iter *iter)
3024{
3025 ssize_t ret = 0;
3026
3027 /*
3028 * Don't support polled IO through this interface, and we can't
3029 * support non-blocking either. For the latter, this just causes
3030 * the kiocb to be handled from an async context.
3031 */
3032 if (kiocb->ki_flags & IOCB_HIPRI)
3033 return -EOPNOTSUPP;
3034 if (kiocb->ki_flags & IOCB_NOWAIT)
3035 return -EAGAIN;
3036
3037 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003038 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003039 ssize_t nr;
3040
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003041 if (!iov_iter_is_bvec(iter)) {
3042 iovec = iov_iter_iovec(iter);
3043 } else {
3044 /* fixed buffers import bvec */
3045 iovec.iov_base = kmap(iter->bvec->bv_page)
3046 + iter->iov_offset;
3047 iovec.iov_len = min(iter->count,
3048 iter->bvec->bv_len - iter->iov_offset);
3049 }
3050
Jens Axboe32960612019-09-23 11:05:34 -06003051 if (rw == READ) {
3052 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003053 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003054 } else {
3055 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003056 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003057 }
3058
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003059 if (iov_iter_is_bvec(iter))
3060 kunmap(iter->bvec->bv_page);
3061
Jens Axboe32960612019-09-23 11:05:34 -06003062 if (nr < 0) {
3063 if (!ret)
3064 ret = nr;
3065 break;
3066 }
3067 ret += nr;
3068 if (nr != iovec.iov_len)
3069 break;
3070 iov_iter_advance(iter, nr);
3071 }
3072
3073 return ret;
3074}
3075
Jens Axboeff6165b2020-08-13 09:47:43 -06003076static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3077 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003078{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003079 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003080
Jens Axboeff6165b2020-08-13 09:47:43 -06003081 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003082 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003083 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003084 /* can only be fixed buffers, no need to do anything */
3085 if (iter->type == ITER_BVEC)
3086 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003087 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003088 unsigned iov_off = 0;
3089
3090 rw->iter.iov = rw->fast_iov;
3091 if (iter->iov != fast_iov) {
3092 iov_off = iter->iov - fast_iov;
3093 rw->iter.iov += iov_off;
3094 }
3095 if (rw->fast_iov != fast_iov)
3096 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003097 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003098 } else {
3099 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003100 }
3101}
3102
Jens Axboee8c2bc12020-08-15 18:44:09 -07003103static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003104{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003105 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3106 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3107 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003108}
3109
Jens Axboee8c2bc12020-08-15 18:44:09 -07003110static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003111{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003112 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003113 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003114
Jens Axboee8c2bc12020-08-15 18:44:09 -07003115 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003116}
3117
Jens Axboeff6165b2020-08-13 09:47:43 -06003118static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3119 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003120 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003121{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003122 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003123 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003124 if (!req->async_data) {
3125 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003126 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003127
Jens Axboeff6165b2020-08-13 09:47:43 -06003128 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003129 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003130 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003131}
3132
Pavel Begunkov73debe62020-09-30 22:57:54 +03003133static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003134{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003135 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003136 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003137 ssize_t ret;
3138
Pavel Begunkov73debe62020-09-30 22:57:54 +03003139 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003140 if (unlikely(ret < 0))
3141 return ret;
3142
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003143 iorw->bytes_done = 0;
3144 iorw->free_iovec = iov;
3145 if (iov)
3146 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003147 return 0;
3148}
3149
Pavel Begunkov73debe62020-09-30 22:57:54 +03003150static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003151{
3152 ssize_t ret;
3153
Pavel Begunkova88fc402020-09-30 22:57:53 +03003154 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003155 if (ret)
3156 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003157
Jens Axboe3529d8c2019-12-19 18:24:38 -07003158 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3159 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003160
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003161 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003162 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003163 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003164 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003165}
3166
Jens Axboec1dd91d2020-08-03 16:43:59 -06003167/*
3168 * This is our waitqueue callback handler, registered through lock_page_async()
3169 * when we initially tried to do the IO with the iocb armed our waitqueue.
3170 * This gets called when the page is unlocked, and we generally expect that to
3171 * happen when the page IO is completed and the page is now uptodate. This will
3172 * queue a task_work based retry of the operation, attempting to copy the data
3173 * again. If the latter fails because the page was NOT uptodate, then we will
3174 * do a thread based blocking retry of the operation. That's the unexpected
3175 * slow path.
3176 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003177static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3178 int sync, void *arg)
3179{
3180 struct wait_page_queue *wpq;
3181 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003182 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003183 int ret;
3184
3185 wpq = container_of(wait, struct wait_page_queue, wait);
3186
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003187 if (!wake_page_match(wpq, key))
3188 return 0;
3189
Hao Xuc8d317a2020-09-29 20:00:45 +08003190 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003191 list_del_init(&wait->entry);
3192
Pavel Begunkove7375122020-07-12 20:42:04 +03003193 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003194 percpu_ref_get(&req->ctx->refs);
3195
Jens Axboebcf5a062020-05-22 09:24:42 -06003196 /* submit ref gets dropped, acquire a new one */
3197 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003198 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003199 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003200 struct task_struct *tsk;
3201
Jens Axboebcf5a062020-05-22 09:24:42 -06003202 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003203 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003204 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003205 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003206 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003207 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003208 return 1;
3209}
3210
Jens Axboec1dd91d2020-08-03 16:43:59 -06003211/*
3212 * This controls whether a given IO request should be armed for async page
3213 * based retry. If we return false here, the request is handed to the async
3214 * worker threads for retry. If we're doing buffered reads on a regular file,
3215 * we prepare a private wait_page_queue entry and retry the operation. This
3216 * will either succeed because the page is now uptodate and unlocked, or it
3217 * will register a callback when the page is unlocked at IO completion. Through
3218 * that callback, io_uring uses task_work to setup a retry of the operation.
3219 * That retry will attempt the buffered read again. The retry will generally
3220 * succeed, or in rare cases where it fails, we then fall back to using the
3221 * async worker threads for a blocking retry.
3222 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003223static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003224{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003225 struct io_async_rw *rw = req->async_data;
3226 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003227 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003228
3229 /* never retry for NOWAIT, we just complete with -EAGAIN */
3230 if (req->flags & REQ_F_NOWAIT)
3231 return false;
3232
Jens Axboe227c0c92020-08-13 11:51:40 -06003233 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003234 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003235 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003236
Jens Axboebcf5a062020-05-22 09:24:42 -06003237 /*
3238 * just use poll if we can, and don't attempt if the fs doesn't
3239 * support callback based unlocks
3240 */
3241 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3242 return false;
3243
Jens Axboe3b2a4432020-08-16 10:58:43 -07003244 wait->wait.func = io_async_buf_func;
3245 wait->wait.private = req;
3246 wait->wait.flags = 0;
3247 INIT_LIST_HEAD(&wait->wait.entry);
3248 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003249 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003250 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003251 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003252}
3253
3254static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3255{
3256 if (req->file->f_op->read_iter)
3257 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003258 else if (req->file->f_op->read)
3259 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3260 else
3261 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003262}
3263
Jens Axboea1d7c392020-06-22 11:09:46 -06003264static int io_read(struct io_kiocb *req, bool force_nonblock,
3265 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266{
3267 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003268 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003269 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003270 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003271 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003272 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003273 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003274
Jens Axboee8c2bc12020-08-15 18:44:09 -07003275 if (rw)
3276 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003277
3278 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003279 if (ret < 0)
3280 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003281 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003282 io_size = ret;
3283 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003284 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003285
Jens Axboefd6c2e42019-12-18 12:19:41 -07003286 /* Ensure we clear previously set non-block flag */
3287 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003288 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003289 else
3290 kiocb->ki_flags |= IOCB_NOWAIT;
3291
Jens Axboefd6c2e42019-12-18 12:19:41 -07003292
Pavel Begunkov24c74672020-06-21 13:09:51 +03003293 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003294 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3295 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003296 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003297
Jens Axboe0fef9482020-08-26 10:36:20 -06003298 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003299 if (unlikely(ret))
3300 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003301
Jens Axboe227c0c92020-08-13 11:51:40 -06003302 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003303
Jens Axboe227c0c92020-08-13 11:51:40 -06003304 if (!ret) {
3305 goto done;
3306 } else if (ret == -EIOCBQUEUED) {
3307 ret = 0;
3308 goto out_free;
3309 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003310 /* IOPOLL retry should happen for io-wq threads */
3311 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003312 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003313 /* no retry on NONBLOCK marked file */
3314 if (req->file->f_flags & O_NONBLOCK)
3315 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003316 /* some cases will consume bytes even on error returns */
3317 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003318 ret = 0;
3319 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003320 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003321 /* make sure -ERESTARTSYS -> -EINTR is done */
3322 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003323 }
3324
3325 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003326 if (!iov_iter_count(iter) || !force_nonblock ||
3327 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003328 goto done;
3329
3330 io_size -= ret;
3331copy_iov:
3332 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3333 if (ret2) {
3334 ret = ret2;
3335 goto out_free;
3336 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003337 if (no_async)
3338 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003339 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003340 /* it's copied and will be cleaned with ->io */
3341 iovec = NULL;
3342 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003343 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003344retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003345 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003346 /* if we can retry, do so with the callbacks armed */
3347 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003348 kiocb->ki_flags &= ~IOCB_WAITQ;
3349 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003350 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003351
3352 /*
3353 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3354 * get -EIOCBQUEUED, then we'll get a notification when the desired
3355 * page gets unlocked. We can also get a partial read here, and if we
3356 * do, then just retry at the new offset.
3357 */
3358 ret = io_iter_do_read(req, iter);
3359 if (ret == -EIOCBQUEUED) {
3360 ret = 0;
3361 goto out_free;
3362 } else if (ret > 0 && ret < io_size) {
3363 /* we got some bytes, but not all. retry. */
3364 goto retry;
3365 }
3366done:
3367 kiocb_done(kiocb, ret, cs);
3368 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003369out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003370 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003371 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003372 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003373 return ret;
3374}
3375
Pavel Begunkov73debe62020-09-30 22:57:54 +03003376static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003377{
3378 ssize_t ret;
3379
Pavel Begunkova88fc402020-09-30 22:57:53 +03003380 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003381 if (ret)
3382 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003383
Jens Axboe3529d8c2019-12-19 18:24:38 -07003384 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3385 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003386
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003387 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003388 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003389 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003390 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003391}
3392
Jens Axboea1d7c392020-06-22 11:09:46 -06003393static int io_write(struct io_kiocb *req, bool force_nonblock,
3394 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003395{
3396 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003397 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003398 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003399 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003400 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003401 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003402
Jens Axboee8c2bc12020-08-15 18:44:09 -07003403 if (rw)
3404 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003405
3406 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003407 if (ret < 0)
3408 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003409 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003410 io_size = ret;
3411 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003412
Jens Axboefd6c2e42019-12-18 12:19:41 -07003413 /* Ensure we clear previously set non-block flag */
3414 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003415 kiocb->ki_flags &= ~IOCB_NOWAIT;
3416 else
3417 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003418
Pavel Begunkov24c74672020-06-21 13:09:51 +03003419 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003420 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003421 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003422
Jens Axboe10d59342019-12-09 20:16:22 -07003423 /* file path doesn't support NOWAIT for non-direct_IO */
3424 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3425 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003426 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003427
Jens Axboe0fef9482020-08-26 10:36:20 -06003428 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003429 if (unlikely(ret))
3430 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003431
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003432 /*
3433 * Open-code file_start_write here to grab freeze protection,
3434 * which will be released by another thread in
3435 * io_complete_rw(). Fool lockdep by telling it the lock got
3436 * released so that it doesn't complain about the held lock when
3437 * we return to userspace.
3438 */
3439 if (req->flags & REQ_F_ISREG) {
3440 __sb_start_write(file_inode(req->file)->i_sb,
3441 SB_FREEZE_WRITE, true);
3442 __sb_writers_release(file_inode(req->file)->i_sb,
3443 SB_FREEZE_WRITE);
3444 }
3445 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003446
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003447 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003448 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003449 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003450 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003451 else
3452 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003453
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003454 /*
3455 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3456 * retry them without IOCB_NOWAIT.
3457 */
3458 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3459 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003460 /* no retry on NONBLOCK marked file */
3461 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3462 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003463 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003464 /* IOPOLL retry should happen for io-wq threads */
3465 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3466 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003467done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003468 kiocb_done(kiocb, ret2, cs);
3469 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003470copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003471 /* some cases will consume bytes even on error returns */
3472 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003473 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003474 if (!ret)
3475 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003476 }
Jens Axboe31b51512019-01-18 22:56:34 -07003477out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003478 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003479 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003480 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003481 return ret;
3482}
3483
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003484static int __io_splice_prep(struct io_kiocb *req,
3485 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003486{
3487 struct io_splice* sp = &req->splice;
3488 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3489 int ret;
3490
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003491 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3492 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003493
3494 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003495 sp->len = READ_ONCE(sqe->len);
3496 sp->flags = READ_ONCE(sqe->splice_flags);
3497
3498 if (unlikely(sp->flags & ~valid_flags))
3499 return -EINVAL;
3500
3501 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3502 (sp->flags & SPLICE_F_FD_IN_FIXED));
3503 if (ret)
3504 return ret;
3505 req->flags |= REQ_F_NEED_CLEANUP;
3506
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003507 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3508 /*
3509 * Splice operation will be punted aync, and here need to
3510 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3511 */
3512 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003513 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003514 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003515
3516 return 0;
3517}
3518
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003519static int io_tee_prep(struct io_kiocb *req,
3520 const struct io_uring_sqe *sqe)
3521{
3522 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3523 return -EINVAL;
3524 return __io_splice_prep(req, sqe);
3525}
3526
3527static int io_tee(struct io_kiocb *req, bool force_nonblock)
3528{
3529 struct io_splice *sp = &req->splice;
3530 struct file *in = sp->file_in;
3531 struct file *out = sp->file_out;
3532 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3533 long ret = 0;
3534
3535 if (force_nonblock)
3536 return -EAGAIN;
3537 if (sp->len)
3538 ret = do_tee(in, out, sp->len, flags);
3539
3540 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3541 req->flags &= ~REQ_F_NEED_CLEANUP;
3542
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003543 if (ret != sp->len)
3544 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003545 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003546 return 0;
3547}
3548
3549static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3550{
3551 struct io_splice* sp = &req->splice;
3552
3553 sp->off_in = READ_ONCE(sqe->splice_off_in);
3554 sp->off_out = READ_ONCE(sqe->off);
3555 return __io_splice_prep(req, sqe);
3556}
3557
Pavel Begunkov014db002020-03-03 21:33:12 +03003558static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003559{
3560 struct io_splice *sp = &req->splice;
3561 struct file *in = sp->file_in;
3562 struct file *out = sp->file_out;
3563 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3564 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003565 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003566
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003567 if (force_nonblock)
3568 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003569
3570 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3571 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003572
Jens Axboe948a7742020-05-17 14:21:38 -06003573 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003574 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003575
3576 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3577 req->flags &= ~REQ_F_NEED_CLEANUP;
3578
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003579 if (ret != sp->len)
3580 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003581 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003582 return 0;
3583}
3584
Jens Axboe2b188cc2019-01-07 10:46:33 -07003585/*
3586 * IORING_OP_NOP just posts a completion event, nothing else.
3587 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003588static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003589{
3590 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003591
Jens Axboedef596e2019-01-09 08:59:42 -07003592 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3593 return -EINVAL;
3594
Jens Axboe229a7b62020-06-22 10:13:11 -06003595 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003596 return 0;
3597}
3598
Jens Axboe3529d8c2019-12-19 18:24:38 -07003599static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003600{
Jens Axboe6b063142019-01-10 22:13:58 -07003601 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003602
Jens Axboe09bb8392019-03-13 12:39:28 -06003603 if (!req->file)
3604 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003605
Jens Axboe6b063142019-01-10 22:13:58 -07003606 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003607 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003608 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003609 return -EINVAL;
3610
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003611 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3612 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3613 return -EINVAL;
3614
3615 req->sync.off = READ_ONCE(sqe->off);
3616 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003617 return 0;
3618}
3619
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003620static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003621{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003622 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003623 int ret;
3624
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003625 /* fsync always requires a blocking context */
3626 if (force_nonblock)
3627 return -EAGAIN;
3628
Jens Axboe9adbd452019-12-20 08:45:55 -07003629 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003630 end > 0 ? end : LLONG_MAX,
3631 req->sync.flags & IORING_FSYNC_DATASYNC);
3632 if (ret < 0)
3633 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003634 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003635 return 0;
3636}
3637
Jens Axboed63d1b52019-12-10 10:38:56 -07003638static int io_fallocate_prep(struct io_kiocb *req,
3639 const struct io_uring_sqe *sqe)
3640{
3641 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3642 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003643 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3644 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003645
3646 req->sync.off = READ_ONCE(sqe->off);
3647 req->sync.len = READ_ONCE(sqe->addr);
3648 req->sync.mode = READ_ONCE(sqe->len);
3649 return 0;
3650}
3651
Pavel Begunkov014db002020-03-03 21:33:12 +03003652static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003653{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003654 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003655
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003656 /* fallocate always requiring blocking context */
3657 if (force_nonblock)
3658 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003659 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3660 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003661 if (ret < 0)
3662 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003663 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003664 return 0;
3665}
3666
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003667static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003668{
Jens Axboef8748882020-01-08 17:47:02 -07003669 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003670 int ret;
3671
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003672 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003673 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003674 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003675 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003676
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003677 /* open.how should be already initialised */
3678 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003679 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003680
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003681 req->open.dfd = READ_ONCE(sqe->fd);
3682 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003683 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003684 if (IS_ERR(req->open.filename)) {
3685 ret = PTR_ERR(req->open.filename);
3686 req->open.filename = NULL;
3687 return ret;
3688 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003689 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003690 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003691 return 0;
3692}
3693
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003694static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3695{
3696 u64 flags, mode;
3697
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003698 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3699 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003700 mode = READ_ONCE(sqe->len);
3701 flags = READ_ONCE(sqe->open_flags);
3702 req->open.how = build_open_how(flags, mode);
3703 return __io_openat_prep(req, sqe);
3704}
3705
Jens Axboecebdb982020-01-08 17:59:24 -07003706static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3707{
3708 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003709 size_t len;
3710 int ret;
3711
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003712 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3713 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003714 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3715 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003716 if (len < OPEN_HOW_SIZE_VER0)
3717 return -EINVAL;
3718
3719 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3720 len);
3721 if (ret)
3722 return ret;
3723
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003724 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003725}
3726
Pavel Begunkov014db002020-03-03 21:33:12 +03003727static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003728{
3729 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003730 struct file *file;
3731 int ret;
3732
Jens Axboef86cd202020-01-29 13:46:44 -07003733 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003734 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003735
Jens Axboecebdb982020-01-08 17:59:24 -07003736 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003737 if (ret)
3738 goto err;
3739
Jens Axboe4022e7a2020-03-19 19:23:18 -06003740 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003741 if (ret < 0)
3742 goto err;
3743
3744 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3745 if (IS_ERR(file)) {
3746 put_unused_fd(ret);
3747 ret = PTR_ERR(file);
3748 } else {
3749 fsnotify_open(file);
3750 fd_install(ret, file);
3751 }
3752err:
3753 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003754 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003755 if (ret < 0)
3756 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003757 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003758 return 0;
3759}
3760
Pavel Begunkov014db002020-03-03 21:33:12 +03003761static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003762{
Pavel Begunkov014db002020-03-03 21:33:12 +03003763 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003764}
3765
Jens Axboe067524e2020-03-02 16:32:28 -07003766static int io_remove_buffers_prep(struct io_kiocb *req,
3767 const struct io_uring_sqe *sqe)
3768{
3769 struct io_provide_buf *p = &req->pbuf;
3770 u64 tmp;
3771
3772 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3773 return -EINVAL;
3774
3775 tmp = READ_ONCE(sqe->fd);
3776 if (!tmp || tmp > USHRT_MAX)
3777 return -EINVAL;
3778
3779 memset(p, 0, sizeof(*p));
3780 p->nbufs = tmp;
3781 p->bgid = READ_ONCE(sqe->buf_group);
3782 return 0;
3783}
3784
3785static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3786 int bgid, unsigned nbufs)
3787{
3788 unsigned i = 0;
3789
3790 /* shouldn't happen */
3791 if (!nbufs)
3792 return 0;
3793
3794 /* the head kbuf is the list itself */
3795 while (!list_empty(&buf->list)) {
3796 struct io_buffer *nxt;
3797
3798 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3799 list_del(&nxt->list);
3800 kfree(nxt);
3801 if (++i == nbufs)
3802 return i;
3803 }
3804 i++;
3805 kfree(buf);
3806 idr_remove(&ctx->io_buffer_idr, bgid);
3807
3808 return i;
3809}
3810
Jens Axboe229a7b62020-06-22 10:13:11 -06003811static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3812 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003813{
3814 struct io_provide_buf *p = &req->pbuf;
3815 struct io_ring_ctx *ctx = req->ctx;
3816 struct io_buffer *head;
3817 int ret = 0;
3818
3819 io_ring_submit_lock(ctx, !force_nonblock);
3820
3821 lockdep_assert_held(&ctx->uring_lock);
3822
3823 ret = -ENOENT;
3824 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3825 if (head)
3826 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3827
3828 io_ring_submit_lock(ctx, !force_nonblock);
3829 if (ret < 0)
3830 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003831 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003832 return 0;
3833}
3834
Jens Axboeddf0322d2020-02-23 16:41:33 -07003835static int io_provide_buffers_prep(struct io_kiocb *req,
3836 const struct io_uring_sqe *sqe)
3837{
3838 struct io_provide_buf *p = &req->pbuf;
3839 u64 tmp;
3840
3841 if (sqe->ioprio || sqe->rw_flags)
3842 return -EINVAL;
3843
3844 tmp = READ_ONCE(sqe->fd);
3845 if (!tmp || tmp > USHRT_MAX)
3846 return -E2BIG;
3847 p->nbufs = tmp;
3848 p->addr = READ_ONCE(sqe->addr);
3849 p->len = READ_ONCE(sqe->len);
3850
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003851 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003852 return -EFAULT;
3853
3854 p->bgid = READ_ONCE(sqe->buf_group);
3855 tmp = READ_ONCE(sqe->off);
3856 if (tmp > USHRT_MAX)
3857 return -E2BIG;
3858 p->bid = tmp;
3859 return 0;
3860}
3861
3862static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3863{
3864 struct io_buffer *buf;
3865 u64 addr = pbuf->addr;
3866 int i, bid = pbuf->bid;
3867
3868 for (i = 0; i < pbuf->nbufs; i++) {
3869 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3870 if (!buf)
3871 break;
3872
3873 buf->addr = addr;
3874 buf->len = pbuf->len;
3875 buf->bid = bid;
3876 addr += pbuf->len;
3877 bid++;
3878 if (!*head) {
3879 INIT_LIST_HEAD(&buf->list);
3880 *head = buf;
3881 } else {
3882 list_add_tail(&buf->list, &(*head)->list);
3883 }
3884 }
3885
3886 return i ? i : -ENOMEM;
3887}
3888
Jens Axboe229a7b62020-06-22 10:13:11 -06003889static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3890 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003891{
3892 struct io_provide_buf *p = &req->pbuf;
3893 struct io_ring_ctx *ctx = req->ctx;
3894 struct io_buffer *head, *list;
3895 int ret = 0;
3896
3897 io_ring_submit_lock(ctx, !force_nonblock);
3898
3899 lockdep_assert_held(&ctx->uring_lock);
3900
3901 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3902
3903 ret = io_add_buffers(p, &head);
3904 if (ret < 0)
3905 goto out;
3906
3907 if (!list) {
3908 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3909 GFP_KERNEL);
3910 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003911 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003912 goto out;
3913 }
3914 }
3915out:
3916 io_ring_submit_unlock(ctx, !force_nonblock);
3917 if (ret < 0)
3918 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003919 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003920 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003921}
3922
Jens Axboe3e4827b2020-01-08 15:18:09 -07003923static int io_epoll_ctl_prep(struct io_kiocb *req,
3924 const struct io_uring_sqe *sqe)
3925{
3926#if defined(CONFIG_EPOLL)
3927 if (sqe->ioprio || sqe->buf_index)
3928 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003929 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003930 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003931
3932 req->epoll.epfd = READ_ONCE(sqe->fd);
3933 req->epoll.op = READ_ONCE(sqe->len);
3934 req->epoll.fd = READ_ONCE(sqe->off);
3935
3936 if (ep_op_has_event(req->epoll.op)) {
3937 struct epoll_event __user *ev;
3938
3939 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3940 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3941 return -EFAULT;
3942 }
3943
3944 return 0;
3945#else
3946 return -EOPNOTSUPP;
3947#endif
3948}
3949
Jens Axboe229a7b62020-06-22 10:13:11 -06003950static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3951 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003952{
3953#if defined(CONFIG_EPOLL)
3954 struct io_epoll *ie = &req->epoll;
3955 int ret;
3956
3957 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3958 if (force_nonblock && ret == -EAGAIN)
3959 return -EAGAIN;
3960
3961 if (ret < 0)
3962 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003963 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003964 return 0;
3965#else
3966 return -EOPNOTSUPP;
3967#endif
3968}
3969
Jens Axboec1ca7572019-12-25 22:18:28 -07003970static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3971{
3972#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3973 if (sqe->ioprio || sqe->buf_index || sqe->off)
3974 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003975 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3976 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003977
3978 req->madvise.addr = READ_ONCE(sqe->addr);
3979 req->madvise.len = READ_ONCE(sqe->len);
3980 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3981 return 0;
3982#else
3983 return -EOPNOTSUPP;
3984#endif
3985}
3986
Pavel Begunkov014db002020-03-03 21:33:12 +03003987static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003988{
3989#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3990 struct io_madvise *ma = &req->madvise;
3991 int ret;
3992
3993 if (force_nonblock)
3994 return -EAGAIN;
3995
3996 ret = do_madvise(ma->addr, ma->len, ma->advice);
3997 if (ret < 0)
3998 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003999 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004000 return 0;
4001#else
4002 return -EOPNOTSUPP;
4003#endif
4004}
4005
Jens Axboe4840e412019-12-25 22:03:45 -07004006static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4007{
4008 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4009 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004010 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4011 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004012
4013 req->fadvise.offset = READ_ONCE(sqe->off);
4014 req->fadvise.len = READ_ONCE(sqe->len);
4015 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4016 return 0;
4017}
4018
Pavel Begunkov014db002020-03-03 21:33:12 +03004019static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004020{
4021 struct io_fadvise *fa = &req->fadvise;
4022 int ret;
4023
Jens Axboe3e694262020-02-01 09:22:49 -07004024 if (force_nonblock) {
4025 switch (fa->advice) {
4026 case POSIX_FADV_NORMAL:
4027 case POSIX_FADV_RANDOM:
4028 case POSIX_FADV_SEQUENTIAL:
4029 break;
4030 default:
4031 return -EAGAIN;
4032 }
4033 }
Jens Axboe4840e412019-12-25 22:03:45 -07004034
4035 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4036 if (ret < 0)
4037 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004038 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004039 return 0;
4040}
4041
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004042static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4043{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004044 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004045 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004046 if (sqe->ioprio || sqe->buf_index)
4047 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004048 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004049 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004050
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004051 req->statx.dfd = READ_ONCE(sqe->fd);
4052 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004053 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004054 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4055 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004056
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004057 return 0;
4058}
4059
Pavel Begunkov014db002020-03-03 21:33:12 +03004060static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004061{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004062 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004063 int ret;
4064
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004065 if (force_nonblock) {
4066 /* only need file table for an actual valid fd */
4067 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4068 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004069 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004070 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004071
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004072 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4073 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004074
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004075 if (ret < 0)
4076 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004077 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004078 return 0;
4079}
4080
Jens Axboeb5dba592019-12-11 14:02:38 -07004081static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4082{
4083 /*
4084 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004085 * leave the 'file' in an undeterminate state, and here need to modify
4086 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004087 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004088 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004089 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4090
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004091 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4092 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004093 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4094 sqe->rw_flags || sqe->buf_index)
4095 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004096 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004097 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004098
4099 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004100 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004101 return -EBADF;
4102
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004103 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004104 return 0;
4105}
4106
Jens Axboe229a7b62020-06-22 10:13:11 -06004107static int io_close(struct io_kiocb *req, bool force_nonblock,
4108 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004109{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004110 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004111 int ret;
4112
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004113 /* might be already done during nonblock submission */
4114 if (!close->put_file) {
4115 ret = __close_fd_get_file(close->fd, &close->put_file);
4116 if (ret < 0)
4117 return (ret == -ENOENT) ? -EBADF : ret;
4118 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004119
4120 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004121 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004122 /* was never set, but play safe */
4123 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004124 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004125 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004126 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004127 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004128
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004129 /* No ->flush() or already async, safely close from here */
4130 ret = filp_close(close->put_file, req->work.files);
4131 if (ret < 0)
4132 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004133 fput(close->put_file);
4134 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004135 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004136 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004137}
4138
Jens Axboe3529d8c2019-12-19 18:24:38 -07004139static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004140{
4141 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004142
4143 if (!req->file)
4144 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004145
4146 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4147 return -EINVAL;
4148 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4149 return -EINVAL;
4150
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004151 req->sync.off = READ_ONCE(sqe->off);
4152 req->sync.len = READ_ONCE(sqe->len);
4153 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004154 return 0;
4155}
4156
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004157static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004158{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004159 int ret;
4160
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004161 /* sync_file_range always requires a blocking context */
4162 if (force_nonblock)
4163 return -EAGAIN;
4164
Jens Axboe9adbd452019-12-20 08:45:55 -07004165 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004166 req->sync.flags);
4167 if (ret < 0)
4168 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004169 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004170 return 0;
4171}
4172
YueHaibing469956e2020-03-04 15:53:52 +08004173#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004174static int io_setup_async_msg(struct io_kiocb *req,
4175 struct io_async_msghdr *kmsg)
4176{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004177 struct io_async_msghdr *async_msg = req->async_data;
4178
4179 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004180 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004181 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004182 if (kmsg->iov != kmsg->fast_iov)
4183 kfree(kmsg->iov);
4184 return -ENOMEM;
4185 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004186 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004187 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004188 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004189 return -EAGAIN;
4190}
4191
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004192static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4193 struct io_async_msghdr *iomsg)
4194{
4195 iomsg->iov = iomsg->fast_iov;
4196 iomsg->msg.msg_name = &iomsg->addr;
4197 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4198 req->sr_msg.msg_flags, &iomsg->iov);
4199}
4200
Jens Axboe3529d8c2019-12-19 18:24:38 -07004201static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004202{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004203 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004204 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004205 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004206
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004207 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4208 return -EINVAL;
4209
Jens Axboee47293f2019-12-20 08:58:21 -07004210 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004211 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004212 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004213
Jens Axboed8768362020-02-27 14:17:49 -07004214#ifdef CONFIG_COMPAT
4215 if (req->ctx->compat)
4216 sr->msg_flags |= MSG_CMSG_COMPAT;
4217#endif
4218
Jens Axboee8c2bc12020-08-15 18:44:09 -07004219 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004220 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004221 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004222 if (!ret)
4223 req->flags |= REQ_F_NEED_CLEANUP;
4224 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004225}
4226
Jens Axboe229a7b62020-06-22 10:13:11 -06004227static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4228 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004229{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004230 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004231 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004232 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004233 int ret;
4234
Jens Axboe03b12302019-12-02 18:50:25 -07004235 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004236 if (unlikely(!sock))
4237 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004238
Jens Axboee8c2bc12020-08-15 18:44:09 -07004239 if (req->async_data) {
4240 kmsg = req->async_data;
4241 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004242 /* if iov is set, it's allocated already */
4243 if (!kmsg->iov)
4244 kmsg->iov = kmsg->fast_iov;
4245 kmsg->msg.msg_iter.iov = kmsg->iov;
4246 } else {
4247 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004248 if (ret)
4249 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004250 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004251 }
4252
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004253 flags = req->sr_msg.msg_flags;
4254 if (flags & MSG_DONTWAIT)
4255 req->flags |= REQ_F_NOWAIT;
4256 else if (force_nonblock)
4257 flags |= MSG_DONTWAIT;
4258
4259 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4260 if (force_nonblock && ret == -EAGAIN)
4261 return io_setup_async_msg(req, kmsg);
4262 if (ret == -ERESTARTSYS)
4263 ret = -EINTR;
4264
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004265 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004266 kfree(kmsg->iov);
4267 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004268 if (ret < 0)
4269 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004270 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004271 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004272}
4273
Jens Axboe229a7b62020-06-22 10:13:11 -06004274static int io_send(struct io_kiocb *req, bool force_nonblock,
4275 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004276{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004277 struct io_sr_msg *sr = &req->sr_msg;
4278 struct msghdr msg;
4279 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004280 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004281 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004282 int ret;
4283
4284 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004285 if (unlikely(!sock))
4286 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004287
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004288 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4289 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004290 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004291
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004292 msg.msg_name = NULL;
4293 msg.msg_control = NULL;
4294 msg.msg_controllen = 0;
4295 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004296
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004297 flags = req->sr_msg.msg_flags;
4298 if (flags & MSG_DONTWAIT)
4299 req->flags |= REQ_F_NOWAIT;
4300 else if (force_nonblock)
4301 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004302
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004303 msg.msg_flags = flags;
4304 ret = sock_sendmsg(sock, &msg);
4305 if (force_nonblock && ret == -EAGAIN)
4306 return -EAGAIN;
4307 if (ret == -ERESTARTSYS)
4308 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004309
Jens Axboe03b12302019-12-02 18:50:25 -07004310 if (ret < 0)
4311 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004312 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004313 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004314}
4315
Pavel Begunkov1400e692020-07-12 20:41:05 +03004316static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4317 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004318{
4319 struct io_sr_msg *sr = &req->sr_msg;
4320 struct iovec __user *uiov;
4321 size_t iov_len;
4322 int ret;
4323
Pavel Begunkov1400e692020-07-12 20:41:05 +03004324 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4325 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004326 if (ret)
4327 return ret;
4328
4329 if (req->flags & REQ_F_BUFFER_SELECT) {
4330 if (iov_len > 1)
4331 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004332 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004333 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004334 sr->len = iomsg->iov[0].iov_len;
4335 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004336 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004337 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004338 } else {
4339 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004340 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004341 if (ret > 0)
4342 ret = 0;
4343 }
4344
4345 return ret;
4346}
4347
4348#ifdef CONFIG_COMPAT
4349static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004350 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004351{
4352 struct compat_msghdr __user *msg_compat;
4353 struct io_sr_msg *sr = &req->sr_msg;
4354 struct compat_iovec __user *uiov;
4355 compat_uptr_t ptr;
4356 compat_size_t len;
4357 int ret;
4358
Pavel Begunkov270a5942020-07-12 20:41:04 +03004359 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004360 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004361 &ptr, &len);
4362 if (ret)
4363 return ret;
4364
4365 uiov = compat_ptr(ptr);
4366 if (req->flags & REQ_F_BUFFER_SELECT) {
4367 compat_ssize_t clen;
4368
4369 if (len > 1)
4370 return -EINVAL;
4371 if (!access_ok(uiov, sizeof(*uiov)))
4372 return -EFAULT;
4373 if (__get_user(clen, &uiov->iov_len))
4374 return -EFAULT;
4375 if (clen < 0)
4376 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004377 sr->len = iomsg->iov[0].iov_len;
4378 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004379 } else {
4380 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004381 &iomsg->iov,
4382 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004383 if (ret < 0)
4384 return ret;
4385 }
4386
4387 return 0;
4388}
Jens Axboe03b12302019-12-02 18:50:25 -07004389#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004390
Pavel Begunkov1400e692020-07-12 20:41:05 +03004391static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4392 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004393{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004394 iomsg->msg.msg_name = &iomsg->addr;
4395 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004396
4397#ifdef CONFIG_COMPAT
4398 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004399 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004400#endif
4401
Pavel Begunkov1400e692020-07-12 20:41:05 +03004402 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004403}
4404
Jens Axboebcda7ba2020-02-23 16:42:51 -07004405static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004406 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004407{
4408 struct io_sr_msg *sr = &req->sr_msg;
4409 struct io_buffer *kbuf;
4410
Jens Axboebcda7ba2020-02-23 16:42:51 -07004411 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4412 if (IS_ERR(kbuf))
4413 return kbuf;
4414
4415 sr->kbuf = kbuf;
4416 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004417 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004418}
4419
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004420static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4421{
4422 return io_put_kbuf(req, req->sr_msg.kbuf);
4423}
4424
Jens Axboe3529d8c2019-12-19 18:24:38 -07004425static int io_recvmsg_prep(struct io_kiocb *req,
4426 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004427{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004428 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004429 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004430 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004431
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004432 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4433 return -EINVAL;
4434
Jens Axboe3529d8c2019-12-19 18:24:38 -07004435 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004436 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004437 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004438 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004439
Jens Axboed8768362020-02-27 14:17:49 -07004440#ifdef CONFIG_COMPAT
4441 if (req->ctx->compat)
4442 sr->msg_flags |= MSG_CMSG_COMPAT;
4443#endif
4444
Jens Axboee8c2bc12020-08-15 18:44:09 -07004445 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004446 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004447 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004448 if (!ret)
4449 req->flags |= REQ_F_NEED_CLEANUP;
4450 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004451}
4452
Jens Axboe229a7b62020-06-22 10:13:11 -06004453static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4454 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004455{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004456 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004457 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004458 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004459 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004460 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004461
Jens Axboe0fa03c62019-04-19 13:34:07 -06004462 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004463 if (unlikely(!sock))
4464 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004465
Jens Axboee8c2bc12020-08-15 18:44:09 -07004466 if (req->async_data) {
4467 kmsg = req->async_data;
4468 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004469 /* if iov is set, it's allocated already */
4470 if (!kmsg->iov)
4471 kmsg->iov = kmsg->fast_iov;
4472 kmsg->msg.msg_iter.iov = kmsg->iov;
4473 } else {
4474 ret = io_recvmsg_copy_hdr(req, &iomsg);
4475 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004476 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004477 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004478 }
4479
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004480 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004481 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004482 if (IS_ERR(kbuf))
4483 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004484 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4485 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4486 1, req->sr_msg.len);
4487 }
4488
4489 flags = req->sr_msg.msg_flags;
4490 if (flags & MSG_DONTWAIT)
4491 req->flags |= REQ_F_NOWAIT;
4492 else if (force_nonblock)
4493 flags |= MSG_DONTWAIT;
4494
4495 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4496 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004497 if (force_nonblock && ret == -EAGAIN)
4498 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004499 if (ret == -ERESTARTSYS)
4500 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004501
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004502 if (req->flags & REQ_F_BUFFER_SELECTED)
4503 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004504 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004505 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004506 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004507 if (ret < 0)
4508 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004509 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004510 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004511}
4512
Jens Axboe229a7b62020-06-22 10:13:11 -06004513static int io_recv(struct io_kiocb *req, bool force_nonblock,
4514 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004515{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004516 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004517 struct io_sr_msg *sr = &req->sr_msg;
4518 struct msghdr msg;
4519 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004520 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004521 struct iovec iov;
4522 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004523 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004524
Jens Axboefddafac2020-01-04 20:19:44 -07004525 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004526 if (unlikely(!sock))
4527 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004528
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004529 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004530 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004531 if (IS_ERR(kbuf))
4532 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004533 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004534 }
4535
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004536 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004537 if (unlikely(ret))
4538 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004539
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004540 msg.msg_name = NULL;
4541 msg.msg_control = NULL;
4542 msg.msg_controllen = 0;
4543 msg.msg_namelen = 0;
4544 msg.msg_iocb = NULL;
4545 msg.msg_flags = 0;
4546
4547 flags = req->sr_msg.msg_flags;
4548 if (flags & MSG_DONTWAIT)
4549 req->flags |= REQ_F_NOWAIT;
4550 else if (force_nonblock)
4551 flags |= MSG_DONTWAIT;
4552
4553 ret = sock_recvmsg(sock, &msg, flags);
4554 if (force_nonblock && ret == -EAGAIN)
4555 return -EAGAIN;
4556 if (ret == -ERESTARTSYS)
4557 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004558out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004559 if (req->flags & REQ_F_BUFFER_SELECTED)
4560 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004561 if (ret < 0)
4562 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004563 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004564 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004565}
4566
Jens Axboe3529d8c2019-12-19 18:24:38 -07004567static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004568{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004569 struct io_accept *accept = &req->accept;
4570
Jens Axboe17f2fe32019-10-17 14:42:58 -06004571 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4572 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004573 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004574 return -EINVAL;
4575
Jens Axboed55e5f52019-12-11 16:12:15 -07004576 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4577 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004578 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004579 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004580 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004581}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004582
Jens Axboe229a7b62020-06-22 10:13:11 -06004583static int io_accept(struct io_kiocb *req, bool force_nonblock,
4584 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004585{
4586 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004587 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004588 int ret;
4589
Jiufei Xuee697dee2020-06-10 13:41:59 +08004590 if (req->file->f_flags & O_NONBLOCK)
4591 req->flags |= REQ_F_NOWAIT;
4592
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004593 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004594 accept->addr_len, accept->flags,
4595 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004596 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004597 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004598 if (ret < 0) {
4599 if (ret == -ERESTARTSYS)
4600 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004601 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004602 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004603 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004604 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004605}
4606
Jens Axboe3529d8c2019-12-19 18:24:38 -07004607static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004608{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004609 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004610 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004611
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004612 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4613 return -EINVAL;
4614 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4615 return -EINVAL;
4616
Jens Axboe3529d8c2019-12-19 18:24:38 -07004617 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4618 conn->addr_len = READ_ONCE(sqe->addr2);
4619
4620 if (!io)
4621 return 0;
4622
4623 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004624 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004625}
4626
Jens Axboe229a7b62020-06-22 10:13:11 -06004627static int io_connect(struct io_kiocb *req, bool force_nonblock,
4628 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004629{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004630 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004631 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004632 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004633
Jens Axboee8c2bc12020-08-15 18:44:09 -07004634 if (req->async_data) {
4635 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004636 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004637 ret = move_addr_to_kernel(req->connect.addr,
4638 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004639 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004640 if (ret)
4641 goto out;
4642 io = &__io;
4643 }
4644
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004645 file_flags = force_nonblock ? O_NONBLOCK : 0;
4646
Jens Axboee8c2bc12020-08-15 18:44:09 -07004647 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004648 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004649 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004650 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004651 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004652 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004653 ret = -ENOMEM;
4654 goto out;
4655 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004656 io = req->async_data;
4657 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004658 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004659 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004660 if (ret == -ERESTARTSYS)
4661 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004662out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004663 if (ret < 0)
4664 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004665 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004666 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004667}
YueHaibing469956e2020-03-04 15:53:52 +08004668#else /* !CONFIG_NET */
4669static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4670{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004671 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004672}
4673
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004674static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4675 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004676{
YueHaibing469956e2020-03-04 15:53:52 +08004677 return -EOPNOTSUPP;
4678}
4679
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004680static int io_send(struct io_kiocb *req, bool force_nonblock,
4681 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004682{
4683 return -EOPNOTSUPP;
4684}
4685
4686static int io_recvmsg_prep(struct io_kiocb *req,
4687 const struct io_uring_sqe *sqe)
4688{
4689 return -EOPNOTSUPP;
4690}
4691
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004692static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4693 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004694{
4695 return -EOPNOTSUPP;
4696}
4697
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004698static int io_recv(struct io_kiocb *req, bool force_nonblock,
4699 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004700{
4701 return -EOPNOTSUPP;
4702}
4703
4704static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4705{
4706 return -EOPNOTSUPP;
4707}
4708
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004709static int io_accept(struct io_kiocb *req, bool force_nonblock,
4710 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004711{
4712 return -EOPNOTSUPP;
4713}
4714
4715static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4716{
4717 return -EOPNOTSUPP;
4718}
4719
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004720static int io_connect(struct io_kiocb *req, bool force_nonblock,
4721 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004722{
4723 return -EOPNOTSUPP;
4724}
4725#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004726
Jens Axboed7718a92020-02-14 22:23:12 -07004727struct io_poll_table {
4728 struct poll_table_struct pt;
4729 struct io_kiocb *req;
4730 int error;
4731};
4732
Jens Axboed7718a92020-02-14 22:23:12 -07004733static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4734 __poll_t mask, task_work_func_t func)
4735{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004736 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004737 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004738
4739 /* for instances that support it check for an event match first: */
4740 if (mask && !(mask & poll->events))
4741 return 0;
4742
4743 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4744
4745 list_del_init(&poll->wait.entry);
4746
Jens Axboed7718a92020-02-14 22:23:12 -07004747 req->result = mask;
4748 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004749 percpu_ref_get(&req->ctx->refs);
4750
Jens Axboed7718a92020-02-14 22:23:12 -07004751 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004752 * If we using the signalfd wait_queue_head for this wakeup, then
4753 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4754 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4755 * either, as the normal wakeup will suffice.
4756 */
4757 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4758
4759 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004760 * If this fails, then the task is exiting. When a task exits, the
4761 * work gets canceled, so just cancel this request as well instead
4762 * of executing it. We can't safely execute it anyway, as we may not
4763 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004764 */
Jens Axboe87c43112020-09-30 21:00:14 -06004765 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004766 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004767 struct task_struct *tsk;
4768
Jens Axboee3aabf92020-05-18 11:04:17 -06004769 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004770 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004771 task_work_add(tsk, &req->task_work, 0);
4772 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004773 }
Jens Axboed7718a92020-02-14 22:23:12 -07004774 return 1;
4775}
4776
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004777static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4778 __acquires(&req->ctx->completion_lock)
4779{
4780 struct io_ring_ctx *ctx = req->ctx;
4781
4782 if (!req->result && !READ_ONCE(poll->canceled)) {
4783 struct poll_table_struct pt = { ._key = poll->events };
4784
4785 req->result = vfs_poll(req->file, &pt) & poll->events;
4786 }
4787
4788 spin_lock_irq(&ctx->completion_lock);
4789 if (!req->result && !READ_ONCE(poll->canceled)) {
4790 add_wait_queue(poll->head, &poll->wait);
4791 return true;
4792 }
4793
4794 return false;
4795}
4796
Jens Axboed4e7cd32020-08-15 11:44:50 -07004797static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004798{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004799 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004800 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004801 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004802 return req->apoll->double_poll;
4803}
4804
4805static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4806{
4807 if (req->opcode == IORING_OP_POLL_ADD)
4808 return &req->poll;
4809 return &req->apoll->poll;
4810}
4811
4812static void io_poll_remove_double(struct io_kiocb *req)
4813{
4814 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004815
4816 lockdep_assert_held(&req->ctx->completion_lock);
4817
4818 if (poll && poll->head) {
4819 struct wait_queue_head *head = poll->head;
4820
4821 spin_lock(&head->lock);
4822 list_del_init(&poll->wait.entry);
4823 if (poll->wait.private)
4824 refcount_dec(&req->refs);
4825 poll->head = NULL;
4826 spin_unlock(&head->lock);
4827 }
4828}
4829
4830static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4831{
4832 struct io_ring_ctx *ctx = req->ctx;
4833
Jens Axboed4e7cd32020-08-15 11:44:50 -07004834 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004835 req->poll.done = true;
4836 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4837 io_commit_cqring(ctx);
4838}
4839
4840static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4841{
4842 struct io_ring_ctx *ctx = req->ctx;
4843
4844 if (io_poll_rewait(req, &req->poll)) {
4845 spin_unlock_irq(&ctx->completion_lock);
4846 return;
4847 }
4848
4849 hash_del(&req->hash_node);
4850 io_poll_complete(req, req->result, 0);
4851 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004852 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004853 spin_unlock_irq(&ctx->completion_lock);
4854
4855 io_cqring_ev_posted(ctx);
4856}
4857
4858static void io_poll_task_func(struct callback_head *cb)
4859{
4860 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004861 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004862 struct io_kiocb *nxt = NULL;
4863
4864 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004865 if (nxt)
4866 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004867 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004868}
4869
4870static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4871 int sync, void *key)
4872{
4873 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004874 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004875 __poll_t mask = key_to_poll(key);
4876
4877 /* for instances that support it check for an event match first: */
4878 if (mask && !(mask & poll->events))
4879 return 0;
4880
Jens Axboe8706e042020-09-28 08:38:54 -06004881 list_del_init(&wait->entry);
4882
Jens Axboe807abcb2020-07-17 17:09:27 -06004883 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004884 bool done;
4885
Jens Axboe807abcb2020-07-17 17:09:27 -06004886 spin_lock(&poll->head->lock);
4887 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004888 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004889 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004890 /* make sure double remove sees this as being gone */
4891 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004892 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004893 if (!done)
4894 __io_async_wake(req, poll, mask, io_poll_task_func);
4895 }
4896 refcount_dec(&req->refs);
4897 return 1;
4898}
4899
4900static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4901 wait_queue_func_t wake_func)
4902{
4903 poll->head = NULL;
4904 poll->done = false;
4905 poll->canceled = false;
4906 poll->events = events;
4907 INIT_LIST_HEAD(&poll->wait.entry);
4908 init_waitqueue_func_entry(&poll->wait, wake_func);
4909}
4910
4911static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004912 struct wait_queue_head *head,
4913 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004914{
4915 struct io_kiocb *req = pt->req;
4916
4917 /*
4918 * If poll->head is already set, it's because the file being polled
4919 * uses multiple waitqueues for poll handling (eg one for read, one
4920 * for write). Setup a separate io_poll_iocb if this happens.
4921 */
4922 if (unlikely(poll->head)) {
4923 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004924 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004925 pt->error = -EINVAL;
4926 return;
4927 }
4928 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4929 if (!poll) {
4930 pt->error = -ENOMEM;
4931 return;
4932 }
4933 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4934 refcount_inc(&req->refs);
4935 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004936 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004937 }
4938
4939 pt->error = 0;
4940 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004941
4942 if (poll->events & EPOLLEXCLUSIVE)
4943 add_wait_queue_exclusive(head, &poll->wait);
4944 else
4945 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004946}
4947
4948static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4949 struct poll_table_struct *p)
4950{
4951 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004952 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004953
Jens Axboe807abcb2020-07-17 17:09:27 -06004954 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004955}
4956
Jens Axboed7718a92020-02-14 22:23:12 -07004957static void io_async_task_func(struct callback_head *cb)
4958{
4959 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4960 struct async_poll *apoll = req->apoll;
4961 struct io_ring_ctx *ctx = req->ctx;
4962
4963 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4964
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004965 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004966 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004967 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004968 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004969 }
4970
Jens Axboe31067252020-05-17 17:43:31 -06004971 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004972 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004973 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004974
Jens Axboed4e7cd32020-08-15 11:44:50 -07004975 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004976 spin_unlock_irq(&ctx->completion_lock);
4977
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004978 if (!READ_ONCE(apoll->poll.canceled))
4979 __io_req_task_submit(req);
4980 else
4981 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004982
Jens Axboe6d816e02020-08-11 08:04:14 -06004983 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004984 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004985 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004986}
4987
4988static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4989 void *key)
4990{
4991 struct io_kiocb *req = wait->private;
4992 struct io_poll_iocb *poll = &req->apoll->poll;
4993
4994 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4995 key_to_poll(key));
4996
4997 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4998}
4999
5000static void io_poll_req_insert(struct io_kiocb *req)
5001{
5002 struct io_ring_ctx *ctx = req->ctx;
5003 struct hlist_head *list;
5004
5005 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5006 hlist_add_head(&req->hash_node, list);
5007}
5008
5009static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5010 struct io_poll_iocb *poll,
5011 struct io_poll_table *ipt, __poll_t mask,
5012 wait_queue_func_t wake_func)
5013 __acquires(&ctx->completion_lock)
5014{
5015 struct io_ring_ctx *ctx = req->ctx;
5016 bool cancel = false;
5017
Jens Axboe18bceab2020-05-15 11:56:54 -06005018 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005019 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005020 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005021
5022 ipt->pt._key = mask;
5023 ipt->req = req;
5024 ipt->error = -EINVAL;
5025
Jens Axboed7718a92020-02-14 22:23:12 -07005026 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5027
5028 spin_lock_irq(&ctx->completion_lock);
5029 if (likely(poll->head)) {
5030 spin_lock(&poll->head->lock);
5031 if (unlikely(list_empty(&poll->wait.entry))) {
5032 if (ipt->error)
5033 cancel = true;
5034 ipt->error = 0;
5035 mask = 0;
5036 }
5037 if (mask || ipt->error)
5038 list_del_init(&poll->wait.entry);
5039 else if (cancel)
5040 WRITE_ONCE(poll->canceled, true);
5041 else if (!poll->done) /* actually waiting for an event */
5042 io_poll_req_insert(req);
5043 spin_unlock(&poll->head->lock);
5044 }
5045
5046 return mask;
5047}
5048
5049static bool io_arm_poll_handler(struct io_kiocb *req)
5050{
5051 const struct io_op_def *def = &io_op_defs[req->opcode];
5052 struct io_ring_ctx *ctx = req->ctx;
5053 struct async_poll *apoll;
5054 struct io_poll_table ipt;
5055 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005056 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005057
5058 if (!req->file || !file_can_poll(req->file))
5059 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005060 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005061 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005062 if (def->pollin)
5063 rw = READ;
5064 else if (def->pollout)
5065 rw = WRITE;
5066 else
5067 return false;
5068 /* if we can't nonblock try, then no point in arming a poll handler */
5069 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005070 return false;
5071
5072 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5073 if (unlikely(!apoll))
5074 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005075 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005076
5077 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005078 req->apoll = apoll;
5079 INIT_HLIST_NODE(&req->hash_node);
5080
Nathan Chancellor8755d972020-03-02 16:01:19 -07005081 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005082 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005083 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005084 if (def->pollout)
5085 mask |= POLLOUT | POLLWRNORM;
5086 mask |= POLLERR | POLLPRI;
5087
5088 ipt.pt._qproc = io_async_queue_proc;
5089
5090 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5091 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005092 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005093 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005094 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005095 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005096 kfree(apoll);
5097 return false;
5098 }
5099 spin_unlock_irq(&ctx->completion_lock);
5100 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5101 apoll->poll.events);
5102 return true;
5103}
5104
5105static bool __io_poll_remove_one(struct io_kiocb *req,
5106 struct io_poll_iocb *poll)
5107{
Jens Axboeb41e9852020-02-17 09:52:41 -07005108 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005109
5110 spin_lock(&poll->head->lock);
5111 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005112 if (!list_empty(&poll->wait.entry)) {
5113 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005114 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005115 }
5116 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005117 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005118 return do_complete;
5119}
5120
5121static bool io_poll_remove_one(struct io_kiocb *req)
5122{
5123 bool do_complete;
5124
Jens Axboed4e7cd32020-08-15 11:44:50 -07005125 io_poll_remove_double(req);
5126
Jens Axboed7718a92020-02-14 22:23:12 -07005127 if (req->opcode == IORING_OP_POLL_ADD) {
5128 do_complete = __io_poll_remove_one(req, &req->poll);
5129 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005130 struct async_poll *apoll = req->apoll;
5131
Jens Axboed7718a92020-02-14 22:23:12 -07005132 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005133 do_complete = __io_poll_remove_one(req, &apoll->poll);
5134 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005135 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005136 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005137 kfree(apoll);
5138 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005139 }
5140
Jens Axboeb41e9852020-02-17 09:52:41 -07005141 if (do_complete) {
5142 io_cqring_fill_event(req, -ECANCELED);
5143 io_commit_cqring(req->ctx);
5144 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005145 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005146 io_put_req(req);
5147 }
5148
5149 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005150}
5151
Jens Axboe76e1b642020-09-26 15:05:03 -06005152/*
5153 * Returns true if we found and killed one or more poll requests
5154 */
5155static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005156{
Jens Axboe78076bb2019-12-04 19:56:40 -07005157 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005158 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005159 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005160
5161 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005162 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5163 struct hlist_head *list;
5164
5165 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005166 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5167 if (io_task_match(req, tsk))
5168 posted += io_poll_remove_one(req);
5169 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005170 }
5171 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005172
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005173 if (posted)
5174 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005175
5176 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005177}
5178
Jens Axboe47f46762019-11-09 17:43:02 -07005179static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5180{
Jens Axboe78076bb2019-12-04 19:56:40 -07005181 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005182 struct io_kiocb *req;
5183
Jens Axboe78076bb2019-12-04 19:56:40 -07005184 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5185 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005186 if (sqe_addr != req->user_data)
5187 continue;
5188 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005189 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005190 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005191 }
5192
5193 return -ENOENT;
5194}
5195
Jens Axboe3529d8c2019-12-19 18:24:38 -07005196static int io_poll_remove_prep(struct io_kiocb *req,
5197 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005198{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005199 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5200 return -EINVAL;
5201 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5202 sqe->poll_events)
5203 return -EINVAL;
5204
Jens Axboe0969e782019-12-17 18:40:57 -07005205 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005206 return 0;
5207}
5208
5209/*
5210 * Find a running poll command that matches one specified in sqe->addr,
5211 * and remove it if found.
5212 */
5213static int io_poll_remove(struct io_kiocb *req)
5214{
5215 struct io_ring_ctx *ctx = req->ctx;
5216 u64 addr;
5217 int ret;
5218
Jens Axboe0969e782019-12-17 18:40:57 -07005219 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005220 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005221 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005222 spin_unlock_irq(&ctx->completion_lock);
5223
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005224 if (ret < 0)
5225 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005226 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005227 return 0;
5228}
5229
Jens Axboe221c5eb2019-01-17 09:41:58 -07005230static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5231 void *key)
5232{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005233 struct io_kiocb *req = wait->private;
5234 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005235
Jens Axboed7718a92020-02-14 22:23:12 -07005236 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005237}
5238
Jens Axboe221c5eb2019-01-17 09:41:58 -07005239static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5240 struct poll_table_struct *p)
5241{
5242 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5243
Jens Axboee8c2bc12020-08-15 18:44:09 -07005244 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005245}
5246
Jens Axboe3529d8c2019-12-19 18:24:38 -07005247static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005248{
5249 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005250 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005251
5252 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5253 return -EINVAL;
5254 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5255 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005256 if (!poll->file)
5257 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005258
Jiufei Xue5769a352020-06-17 17:53:55 +08005259 events = READ_ONCE(sqe->poll32_events);
5260#ifdef __BIG_ENDIAN
5261 events = swahw32(events);
5262#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005263 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5264 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005265 return 0;
5266}
5267
Pavel Begunkov014db002020-03-03 21:33:12 +03005268static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005269{
5270 struct io_poll_iocb *poll = &req->poll;
5271 struct io_ring_ctx *ctx = req->ctx;
5272 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005273 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005274
Jens Axboe78076bb2019-12-04 19:56:40 -07005275 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005276 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005277
Jens Axboed7718a92020-02-14 22:23:12 -07005278 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5279 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005280
Jens Axboe8c838782019-03-12 15:48:16 -06005281 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005282 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005283 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005284 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005285 spin_unlock_irq(&ctx->completion_lock);
5286
Jens Axboe8c838782019-03-12 15:48:16 -06005287 if (mask) {
5288 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005289 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005290 }
Jens Axboe8c838782019-03-12 15:48:16 -06005291 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005292}
5293
Jens Axboe5262f562019-09-17 12:26:57 -06005294static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5295{
Jens Axboead8a48a2019-11-15 08:49:11 -07005296 struct io_timeout_data *data = container_of(timer,
5297 struct io_timeout_data, timer);
5298 struct io_kiocb *req = data->req;
5299 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005300 unsigned long flags;
5301
Jens Axboe5262f562019-09-17 12:26:57 -06005302 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005303 atomic_set(&req->ctx->cq_timeouts,
5304 atomic_read(&req->ctx->cq_timeouts) + 1);
5305
zhangyi (F)ef036812019-10-23 15:10:08 +08005306 /*
Jens Axboe11365042019-10-16 09:08:32 -06005307 * We could be racing with timeout deletion. If the list is empty,
5308 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005309 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005310 if (!list_empty(&req->timeout.list))
5311 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005312
Jens Axboe78e19bb2019-11-06 15:21:34 -07005313 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005314 io_commit_cqring(ctx);
5315 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5316
5317 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005318 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005319 io_put_req(req);
5320 return HRTIMER_NORESTART;
5321}
5322
Jens Axboef254ac02020-08-12 17:33:30 -06005323static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005324{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005325 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005326 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005327
Jens Axboef254ac02020-08-12 17:33:30 -06005328 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005329
Jens Axboee8c2bc12020-08-15 18:44:09 -07005330 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005331 if (ret == -1)
5332 return -EALREADY;
5333
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005334 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005335 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005336 io_cqring_fill_event(req, -ECANCELED);
5337 io_put_req(req);
5338 return 0;
5339}
5340
Jens Axboef254ac02020-08-12 17:33:30 -06005341static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5342{
5343 struct io_kiocb *req;
5344 int ret = -ENOENT;
5345
5346 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5347 if (user_data == req->user_data) {
5348 ret = 0;
5349 break;
5350 }
5351 }
5352
5353 if (ret == -ENOENT)
5354 return ret;
5355
5356 return __io_timeout_cancel(req);
5357}
5358
Jens Axboe3529d8c2019-12-19 18:24:38 -07005359static int io_timeout_remove_prep(struct io_kiocb *req,
5360 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005361{
Jens Axboeb29472e2019-12-17 18:50:29 -07005362 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5363 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005364 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5365 return -EINVAL;
5366 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005367 return -EINVAL;
5368
5369 req->timeout.addr = READ_ONCE(sqe->addr);
5370 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5371 if (req->timeout.flags)
5372 return -EINVAL;
5373
Jens Axboeb29472e2019-12-17 18:50:29 -07005374 return 0;
5375}
5376
Jens Axboe11365042019-10-16 09:08:32 -06005377/*
5378 * Remove or update an existing timeout command
5379 */
Jens Axboefc4df992019-12-10 14:38:45 -07005380static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005381{
5382 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005383 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005384
Jens Axboe11365042019-10-16 09:08:32 -06005385 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005386 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005387
Jens Axboe47f46762019-11-09 17:43:02 -07005388 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005389 io_commit_cqring(ctx);
5390 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005391 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005392 if (ret < 0)
5393 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005394 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005395 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005396}
5397
Jens Axboe3529d8c2019-12-19 18:24:38 -07005398static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005399 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005400{
Jens Axboead8a48a2019-11-15 08:49:11 -07005401 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005402 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005403 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005404
Jens Axboead8a48a2019-11-15 08:49:11 -07005405 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005406 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005407 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005408 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005409 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005410 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005411 flags = READ_ONCE(sqe->timeout_flags);
5412 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005413 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005414
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005415 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005416
Jens Axboee8c2bc12020-08-15 18:44:09 -07005417 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005418 return -ENOMEM;
5419
Jens Axboee8c2bc12020-08-15 18:44:09 -07005420 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005421 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005422
5423 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005424 return -EFAULT;
5425
Jens Axboe11365042019-10-16 09:08:32 -06005426 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005427 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005428 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005429 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005430
Jens Axboead8a48a2019-11-15 08:49:11 -07005431 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5432 return 0;
5433}
5434
Jens Axboefc4df992019-12-10 14:38:45 -07005435static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005436{
Jens Axboead8a48a2019-11-15 08:49:11 -07005437 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005438 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005439 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005440 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005441
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005442 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005443
Jens Axboe5262f562019-09-17 12:26:57 -06005444 /*
5445 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005446 * timeout event to be satisfied. If it isn't set, then this is
5447 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005448 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005449 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005450 entry = ctx->timeout_list.prev;
5451 goto add;
5452 }
Jens Axboe5262f562019-09-17 12:26:57 -06005453
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005454 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5455 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005456
5457 /*
5458 * Insertion sort, ensuring the first entry in the list is always
5459 * the one we need first.
5460 */
Jens Axboe5262f562019-09-17 12:26:57 -06005461 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005462 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5463 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005464
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005465 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005466 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005467 /* nxt.seq is behind @tail, otherwise would've been completed */
5468 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005469 break;
5470 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005471add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005472 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005473 data->timer.function = io_timeout_fn;
5474 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005475 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005476 return 0;
5477}
5478
Jens Axboe62755e32019-10-28 21:49:21 -06005479static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005480{
Jens Axboe62755e32019-10-28 21:49:21 -06005481 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005482
Jens Axboe62755e32019-10-28 21:49:21 -06005483 return req->user_data == (unsigned long) data;
5484}
5485
Jens Axboee977d6d2019-11-05 12:39:45 -07005486static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005487{
Jens Axboe62755e32019-10-28 21:49:21 -06005488 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005489 int ret = 0;
5490
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005491 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005492 switch (cancel_ret) {
5493 case IO_WQ_CANCEL_OK:
5494 ret = 0;
5495 break;
5496 case IO_WQ_CANCEL_RUNNING:
5497 ret = -EALREADY;
5498 break;
5499 case IO_WQ_CANCEL_NOTFOUND:
5500 ret = -ENOENT;
5501 break;
5502 }
5503
Jens Axboee977d6d2019-11-05 12:39:45 -07005504 return ret;
5505}
5506
Jens Axboe47f46762019-11-09 17:43:02 -07005507static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5508 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005509 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005510{
5511 unsigned long flags;
5512 int ret;
5513
5514 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5515 if (ret != -ENOENT) {
5516 spin_lock_irqsave(&ctx->completion_lock, flags);
5517 goto done;
5518 }
5519
5520 spin_lock_irqsave(&ctx->completion_lock, flags);
5521 ret = io_timeout_cancel(ctx, sqe_addr);
5522 if (ret != -ENOENT)
5523 goto done;
5524 ret = io_poll_cancel(ctx, sqe_addr);
5525done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005526 if (!ret)
5527 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005528 io_cqring_fill_event(req, ret);
5529 io_commit_cqring(ctx);
5530 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5531 io_cqring_ev_posted(ctx);
5532
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005533 if (ret < 0)
5534 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005535 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005536}
5537
Jens Axboe3529d8c2019-12-19 18:24:38 -07005538static int io_async_cancel_prep(struct io_kiocb *req,
5539 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005540{
Jens Axboefbf23842019-12-17 18:45:56 -07005541 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005542 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005543 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5544 return -EINVAL;
5545 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005546 return -EINVAL;
5547
Jens Axboefbf23842019-12-17 18:45:56 -07005548 req->cancel.addr = READ_ONCE(sqe->addr);
5549 return 0;
5550}
5551
Pavel Begunkov014db002020-03-03 21:33:12 +03005552static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005553{
5554 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005555
Pavel Begunkov014db002020-03-03 21:33:12 +03005556 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005557 return 0;
5558}
5559
Jens Axboe05f3fb32019-12-09 11:22:50 -07005560static int io_files_update_prep(struct io_kiocb *req,
5561 const struct io_uring_sqe *sqe)
5562{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005563 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5564 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005565 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5566 return -EINVAL;
5567 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005568 return -EINVAL;
5569
5570 req->files_update.offset = READ_ONCE(sqe->off);
5571 req->files_update.nr_args = READ_ONCE(sqe->len);
5572 if (!req->files_update.nr_args)
5573 return -EINVAL;
5574 req->files_update.arg = READ_ONCE(sqe->addr);
5575 return 0;
5576}
5577
Jens Axboe229a7b62020-06-22 10:13:11 -06005578static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5579 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005580{
5581 struct io_ring_ctx *ctx = req->ctx;
5582 struct io_uring_files_update up;
5583 int ret;
5584
Jens Axboef86cd202020-01-29 13:46:44 -07005585 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005586 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005587
5588 up.offset = req->files_update.offset;
5589 up.fds = req->files_update.arg;
5590
5591 mutex_lock(&ctx->uring_lock);
5592 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5593 mutex_unlock(&ctx->uring_lock);
5594
5595 if (ret < 0)
5596 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005597 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005598 return 0;
5599}
5600
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005601static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5602{
5603 switch (req->opcode) {
5604 case IORING_OP_NOP:
5605 return 0;
5606 case IORING_OP_READV:
5607 case IORING_OP_READ_FIXED:
5608 case IORING_OP_READ:
5609 return io_read_prep(req, sqe);
5610 case IORING_OP_WRITEV:
5611 case IORING_OP_WRITE_FIXED:
5612 case IORING_OP_WRITE:
5613 return io_write_prep(req, sqe);
5614 case IORING_OP_POLL_ADD:
5615 return io_poll_add_prep(req, sqe);
5616 case IORING_OP_POLL_REMOVE:
5617 return io_poll_remove_prep(req, sqe);
5618 case IORING_OP_FSYNC:
5619 return io_prep_fsync(req, sqe);
5620 case IORING_OP_SYNC_FILE_RANGE:
5621 return io_prep_sfr(req, sqe);
5622 case IORING_OP_SENDMSG:
5623 case IORING_OP_SEND:
5624 return io_sendmsg_prep(req, sqe);
5625 case IORING_OP_RECVMSG:
5626 case IORING_OP_RECV:
5627 return io_recvmsg_prep(req, sqe);
5628 case IORING_OP_CONNECT:
5629 return io_connect_prep(req, sqe);
5630 case IORING_OP_TIMEOUT:
5631 return io_timeout_prep(req, sqe, false);
5632 case IORING_OP_TIMEOUT_REMOVE:
5633 return io_timeout_remove_prep(req, sqe);
5634 case IORING_OP_ASYNC_CANCEL:
5635 return io_async_cancel_prep(req, sqe);
5636 case IORING_OP_LINK_TIMEOUT:
5637 return io_timeout_prep(req, sqe, true);
5638 case IORING_OP_ACCEPT:
5639 return io_accept_prep(req, sqe);
5640 case IORING_OP_FALLOCATE:
5641 return io_fallocate_prep(req, sqe);
5642 case IORING_OP_OPENAT:
5643 return io_openat_prep(req, sqe);
5644 case IORING_OP_CLOSE:
5645 return io_close_prep(req, sqe);
5646 case IORING_OP_FILES_UPDATE:
5647 return io_files_update_prep(req, sqe);
5648 case IORING_OP_STATX:
5649 return io_statx_prep(req, sqe);
5650 case IORING_OP_FADVISE:
5651 return io_fadvise_prep(req, sqe);
5652 case IORING_OP_MADVISE:
5653 return io_madvise_prep(req, sqe);
5654 case IORING_OP_OPENAT2:
5655 return io_openat2_prep(req, sqe);
5656 case IORING_OP_EPOLL_CTL:
5657 return io_epoll_ctl_prep(req, sqe);
5658 case IORING_OP_SPLICE:
5659 return io_splice_prep(req, sqe);
5660 case IORING_OP_PROVIDE_BUFFERS:
5661 return io_provide_buffers_prep(req, sqe);
5662 case IORING_OP_REMOVE_BUFFERS:
5663 return io_remove_buffers_prep(req, sqe);
5664 case IORING_OP_TEE:
5665 return io_tee_prep(req, sqe);
5666 }
5667
5668 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5669 req->opcode);
5670 return-EINVAL;
5671}
5672
Jens Axboe3529d8c2019-12-19 18:24:38 -07005673static int io_req_defer_prep(struct io_kiocb *req,
5674 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005675{
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005676 if (!sqe)
5677 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005678 if (io_alloc_async_data(req))
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005679 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005680 return io_req_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005681}
5682
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005683static u32 io_get_sequence(struct io_kiocb *req)
5684{
5685 struct io_kiocb *pos;
5686 struct io_ring_ctx *ctx = req->ctx;
5687 u32 total_submitted, nr_reqs = 1;
5688
5689 if (req->flags & REQ_F_LINK_HEAD)
5690 list_for_each_entry(pos, &req->link_list, link_list)
5691 nr_reqs++;
5692
5693 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5694 return total_submitted - nr_reqs;
5695}
5696
Jens Axboe3529d8c2019-12-19 18:24:38 -07005697static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005698{
Jackie Liua197f662019-11-08 08:09:12 -07005699 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005700 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005701 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005702 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005703
Bob Liu9d858b22019-11-13 18:06:25 +08005704 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005705 if (likely(list_empty_careful(&ctx->defer_list) &&
5706 !(req->flags & REQ_F_IO_DRAIN)))
5707 return 0;
5708
5709 seq = io_get_sequence(req);
5710 /* Still a chance to pass the sequence check */
5711 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005712 return 0;
5713
Jens Axboee8c2bc12020-08-15 18:44:09 -07005714 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005715 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005716 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005717 return ret;
5718 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005719 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005720 de = kmalloc(sizeof(*de), GFP_KERNEL);
5721 if (!de)
5722 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005723
Jens Axboede0617e2019-04-06 21:51:27 -06005724 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005725 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005726 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005727 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005728 io_queue_async_work(req);
5729 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005730 }
5731
Jens Axboe915967f2019-11-21 09:01:20 -07005732 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005733 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005734 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005735 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005736 spin_unlock_irq(&ctx->completion_lock);
5737 return -EIOCBQUEUED;
5738}
5739
Jens Axboef573d382020-09-22 10:19:24 -06005740static void io_req_drop_files(struct io_kiocb *req)
5741{
5742 struct io_ring_ctx *ctx = req->ctx;
5743 unsigned long flags;
5744
5745 spin_lock_irqsave(&ctx->inflight_lock, flags);
5746 list_del(&req->inflight_entry);
5747 if (waitqueue_active(&ctx->inflight_wait))
5748 wake_up(&ctx->inflight_wait);
5749 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5750 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005751 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005752 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005753 req->work.files = NULL;
5754}
5755
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005756static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005757{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005758 if (req->flags & REQ_F_BUFFER_SELECTED) {
5759 switch (req->opcode) {
5760 case IORING_OP_READV:
5761 case IORING_OP_READ_FIXED:
5762 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005763 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005764 break;
5765 case IORING_OP_RECVMSG:
5766 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005767 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005768 break;
5769 }
5770 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005771 }
5772
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005773 if (req->flags & REQ_F_NEED_CLEANUP) {
5774 switch (req->opcode) {
5775 case IORING_OP_READV:
5776 case IORING_OP_READ_FIXED:
5777 case IORING_OP_READ:
5778 case IORING_OP_WRITEV:
5779 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005780 case IORING_OP_WRITE: {
5781 struct io_async_rw *io = req->async_data;
5782 if (io->free_iovec)
5783 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005784 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005785 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005786 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005787 case IORING_OP_SENDMSG: {
5788 struct io_async_msghdr *io = req->async_data;
5789 if (io->iov != io->fast_iov)
5790 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005791 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005792 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005793 case IORING_OP_SPLICE:
5794 case IORING_OP_TEE:
5795 io_put_file(req, req->splice.file_in,
5796 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5797 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005798 case IORING_OP_OPENAT:
5799 case IORING_OP_OPENAT2:
5800 if (req->open.filename)
5801 putname(req->open.filename);
5802 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005803 }
5804 req->flags &= ~REQ_F_NEED_CLEANUP;
5805 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005806
Jens Axboef573d382020-09-22 10:19:24 -06005807 if (req->flags & REQ_F_INFLIGHT)
5808 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005809}
5810
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005811static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
5812 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005813{
Jackie Liua197f662019-11-08 08:09:12 -07005814 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005815 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005816
Jens Axboed625c6e2019-12-17 19:53:05 -07005817 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005818 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005819 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005820 break;
5821 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005822 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005823 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06005824 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005825 break;
5826 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005827 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005828 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06005829 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005830 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005831 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03005832 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005833 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005834 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03005835 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005836 break;
5837 case IORING_OP_POLL_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07005838 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005839 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005840 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005841 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005842 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005843 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005844 case IORING_OP_SEND:
Jens Axboefddafac2020-01-04 20:19:44 -07005845 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005846 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005847 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005848 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005849 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005850 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005851 case IORING_OP_RECV:
Jens Axboefddafac2020-01-04 20:19:44 -07005852 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005853 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005854 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005855 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005856 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005857 case IORING_OP_TIMEOUT:
Jens Axboefc4df992019-12-10 14:38:45 -07005858 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005859 break;
Jens Axboe11365042019-10-16 09:08:32 -06005860 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07005861 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005862 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005863 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005864 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005865 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005866 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005867 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005868 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005869 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03005870 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005871 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005872 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005873 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005874 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005875 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03005876 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005877 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005878 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06005879 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005880 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005881 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06005882 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005883 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005884 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03005885 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005886 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005887 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005888 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005889 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005890 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005891 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005892 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005893 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03005894 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005895 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005896 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06005897 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005898 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005899 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005900 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005901 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005902 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06005903 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005904 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005905 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06005906 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005907 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005908 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005909 ret = io_tee(req, force_nonblock);
5910 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005911 default:
5912 ret = -EINVAL;
5913 break;
5914 }
5915
5916 if (ret)
5917 return ret;
5918
Jens Axboeb5325762020-05-19 21:20:27 -06005919 /* If the op doesn't have a file, we're not polling for it */
5920 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005921 const bool in_async = io_wq_current_is_worker();
5922
Jens Axboe11ba8202020-01-15 21:51:17 -07005923 /* workqueue context doesn't hold uring_lock, grab it now */
5924 if (in_async)
5925 mutex_lock(&ctx->uring_lock);
5926
Jens Axboe2b188cc2019-01-07 10:46:33 -07005927 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005928
5929 if (in_async)
5930 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005931 }
5932
5933 return 0;
5934}
5935
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005936static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005937{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005938 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005939 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005940 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005941
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005942 timeout = io_prep_linked_timeout(req);
5943 if (timeout)
5944 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005945
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005946 /* if NO_CANCEL is set, we must still run the work */
5947 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5948 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005949 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005950 }
Jens Axboe31b51512019-01-18 22:56:34 -07005951
Jens Axboe561fb042019-10-24 07:25:42 -06005952 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005953 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005954 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005955 /*
5956 * We can get EAGAIN for polled IO even though we're
5957 * forcing a sync submission from here, since we can't
5958 * wait for request slots on the block side.
5959 */
5960 if (ret != -EAGAIN)
5961 break;
5962 cond_resched();
5963 } while (1);
5964 }
Jens Axboe31b51512019-01-18 22:56:34 -07005965
Jens Axboe561fb042019-10-24 07:25:42 -06005966 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005967 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005968 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005969 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005970
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005971 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005972}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005973
Jens Axboe65e19f52019-10-26 07:20:21 -06005974static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5975 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005976{
Jens Axboe65e19f52019-10-26 07:20:21 -06005977 struct fixed_file_table *table;
5978
Jens Axboe05f3fb32019-12-09 11:22:50 -07005979 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005980 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005981}
5982
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005983static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5984 int fd, struct file **out_file, bool fixed)
5985{
5986 struct io_ring_ctx *ctx = req->ctx;
5987 struct file *file;
5988
5989 if (fixed) {
5990 if (unlikely(!ctx->file_data ||
5991 (unsigned) fd >= ctx->nr_user_files))
5992 return -EBADF;
5993 fd = array_index_nospec(fd, ctx->nr_user_files);
5994 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005995 if (file) {
5996 req->fixed_file_refs = ctx->file_data->cur_refs;
5997 percpu_ref_get(req->fixed_file_refs);
5998 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005999 } else {
6000 trace_io_uring_file_get(ctx, fd);
6001 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006002 }
6003
Jens Axboefd2206e2020-06-02 16:40:47 -06006004 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6005 *out_file = file;
6006 return 0;
6007 }
6008 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006009}
6010
Jens Axboe3529d8c2019-12-19 18:24:38 -07006011static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006012 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006013{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006014 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006015
Jens Axboe63ff8222020-05-07 14:56:15 -06006016 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006017 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006018 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006019
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006020 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006021}
6022
Jens Axboe2665abf2019-11-05 12:40:47 -07006023static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6024{
Jens Axboead8a48a2019-11-15 08:49:11 -07006025 struct io_timeout_data *data = container_of(timer,
6026 struct io_timeout_data, timer);
6027 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006028 struct io_ring_ctx *ctx = req->ctx;
6029 struct io_kiocb *prev = NULL;
6030 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006031
6032 spin_lock_irqsave(&ctx->completion_lock, flags);
6033
6034 /*
6035 * We don't expect the list to be empty, that will only happen if we
6036 * race with the completion of the linked work.
6037 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006038 if (!list_empty(&req->link_list)) {
6039 prev = list_entry(req->link_list.prev, struct io_kiocb,
6040 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006041 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006042 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006043 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6044 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006045 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006046 }
6047
6048 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6049
6050 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006051 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006052 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006053 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006054 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006055 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006056 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006057 return HRTIMER_NORESTART;
6058}
6059
Jens Axboe7271ef32020-08-10 09:55:22 -06006060static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006061{
Jens Axboe76a46e02019-11-10 23:34:16 -07006062 /*
6063 * If the list is now empty, then our linked request finished before
6064 * we got a chance to setup the timer
6065 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006066 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006067 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006068
Jens Axboead8a48a2019-11-15 08:49:11 -07006069 data->timer.function = io_link_timeout_fn;
6070 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6071 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006072 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006073}
6074
6075static void io_queue_linked_timeout(struct io_kiocb *req)
6076{
6077 struct io_ring_ctx *ctx = req->ctx;
6078
6079 spin_lock_irq(&ctx->completion_lock);
6080 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006081 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006082
Jens Axboe2665abf2019-11-05 12:40:47 -07006083 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006084 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006085}
6086
Jens Axboead8a48a2019-11-15 08:49:11 -07006087static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006088{
6089 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006090
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006091 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006092 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006093 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006094 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006095
Pavel Begunkov44932332019-12-05 16:16:35 +03006096 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6097 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006098 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006099 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006100
Jens Axboe76a46e02019-11-10 23:34:16 -07006101 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006102 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006103}
6104
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006105static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006106{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006107 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006108 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006109 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110 int ret;
6111
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006112again:
6113 linked_timeout = io_prep_linked_timeout(req);
6114
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006115 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6116 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006117 if (old_creds)
6118 revert_creds(old_creds);
6119 if (old_creds == req->work.creds)
6120 old_creds = NULL; /* restored original creds */
6121 else
6122 old_creds = override_creds(req->work.creds);
6123 }
6124
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006125 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006126
6127 /*
6128 * We async punt it if the file wasn't marked NOWAIT, or if the file
6129 * doesn't support non-blocking read/write attempts
6130 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006131 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006132 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006133punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006134 /*
6135 * Queued up for async execution, worker will release
6136 * submit reference when the iocb is actually submitted.
6137 */
6138 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006139 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006140
Pavel Begunkovf063c542020-07-25 14:41:59 +03006141 if (linked_timeout)
6142 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006143 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006144 }
Jens Axboee65ef562019-03-12 10:16:44 -06006145
Pavel Begunkov652532a2020-07-03 22:15:07 +03006146 if (unlikely(ret)) {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006147 /* un-prep timeout, so it'll be killed as any other linked */
6148 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006149 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006150 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006151 io_req_complete(req, ret);
6152 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006153 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006154
Jens Axboe6c271ce2019-01-10 11:22:30 -07006155 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006156 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006157 if (linked_timeout)
6158 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006159
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006160 if (nxt) {
6161 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006162
6163 if (req->flags & REQ_F_FORCE_ASYNC)
6164 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006165 goto again;
6166 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006167exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006168 if (old_creds)
6169 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006170}
6171
Jens Axboef13fad72020-06-22 09:34:30 -06006172static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6173 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006174{
6175 int ret;
6176
Jens Axboe3529d8c2019-12-19 18:24:38 -07006177 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006178 if (ret) {
6179 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006180fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006181 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006182 io_put_req(req);
6183 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006184 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006185 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006186 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006187 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006188 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006189 goto fail_req;
6190 }
6191
Jens Axboece35a472019-12-17 08:04:44 -07006192 /*
6193 * Never try inline submit of IOSQE_ASYNC is set, go straight
6194 * to async execution.
6195 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006196 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006197 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6198 io_queue_async_work(req);
6199 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006200 if (sqe) {
6201 ret = io_req_prep(req, sqe);
6202 if (unlikely(ret))
6203 goto fail_req;
6204 }
6205 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006206 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006207}
6208
Jens Axboef13fad72020-06-22 09:34:30 -06006209static inline void io_queue_link_head(struct io_kiocb *req,
6210 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006211{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006212 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006213 io_put_req(req);
6214 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006215 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006216 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006217}
6218
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006219static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006220 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006221{
Jackie Liua197f662019-11-08 08:09:12 -07006222 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006223 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006224
Jens Axboe9e645e112019-05-10 16:07:28 -06006225 /*
6226 * If we already have a head request, queue this one for async
6227 * submittal once the head completes. If we don't have a head but
6228 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6229 * submitted sync once the chain is complete. If none of those
6230 * conditions are true (normal request), then just queue it.
6231 */
6232 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006233 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006234
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006235 /*
6236 * Taking sequential execution of a link, draining both sides
6237 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6238 * requests in the link. So, it drains the head and the
6239 * next after the link request. The last one is done via
6240 * drain_next flag to persist the effect across calls.
6241 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006242 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006243 head->flags |= REQ_F_IO_DRAIN;
6244 ctx->drain_next = 1;
6245 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006246 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006247 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006248 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006249 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006250 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006251 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006252 trace_io_uring_link(ctx, req, head);
6253 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006254
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006255 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006256 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006257 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006258 *link = NULL;
6259 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006260 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006261 if (unlikely(ctx->drain_next)) {
6262 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006263 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006264 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006265 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006266 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006267 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006268
Pavel Begunkov711be032020-01-17 03:57:59 +03006269 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006270 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006271 req->flags |= REQ_F_FAIL_LINK;
6272 *link = req;
6273 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006274 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006275 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006276 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006277
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006278 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006279}
6280
Jens Axboe9a56a232019-01-09 09:06:50 -07006281/*
6282 * Batched submission is done, ensure local IO is flushed out.
6283 */
6284static void io_submit_state_end(struct io_submit_state *state)
6285{
Jens Axboef13fad72020-06-22 09:34:30 -06006286 if (!list_empty(&state->comp.list))
6287 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006288 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006289 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006290 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006291 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006292}
6293
6294/*
6295 * Start submission side cache.
6296 */
6297static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006298 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006299{
6300 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006301 state->comp.nr = 0;
6302 INIT_LIST_HEAD(&state->comp.list);
6303 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006304 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006305 state->file = NULL;
6306 state->ios_left = max_ios;
6307}
6308
Jens Axboe2b188cc2019-01-07 10:46:33 -07006309static void io_commit_sqring(struct io_ring_ctx *ctx)
6310{
Hristo Venev75b28af2019-08-26 17:23:46 +00006311 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006312
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006313 /*
6314 * Ensure any loads from the SQEs are done at this point,
6315 * since once we write the new head, the application could
6316 * write new data to them.
6317 */
6318 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006319}
6320
6321/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006322 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006323 * that is mapped by userspace. This means that care needs to be taken to
6324 * ensure that reads are stable, as we cannot rely on userspace always
6325 * being a good citizen. If members of the sqe are validated and then later
6326 * used, it's important that those reads are done through READ_ONCE() to
6327 * prevent a re-load down the line.
6328 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006329static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006330{
Hristo Venev75b28af2019-08-26 17:23:46 +00006331 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006332 unsigned head;
6333
6334 /*
6335 * The cached sq head (or cq tail) serves two purposes:
6336 *
6337 * 1) allows us to batch the cost of updating the user visible
6338 * head updates.
6339 * 2) allows the kernel side to track the head on its own, even
6340 * though the application is the one updating it.
6341 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006342 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006343 if (likely(head < ctx->sq_entries))
6344 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006345
6346 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006347 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006348 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006349 return NULL;
6350}
6351
6352static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6353{
6354 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006355}
6356
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006357/*
6358 * Check SQE restrictions (opcode and flags).
6359 *
6360 * Returns 'true' if SQE is allowed, 'false' otherwise.
6361 */
6362static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6363 struct io_kiocb *req,
6364 unsigned int sqe_flags)
6365{
6366 if (!ctx->restricted)
6367 return true;
6368
6369 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6370 return false;
6371
6372 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6373 ctx->restrictions.sqe_flags_required)
6374 return false;
6375
6376 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6377 ctx->restrictions.sqe_flags_required))
6378 return false;
6379
6380 return true;
6381}
6382
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006383#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6384 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6385 IOSQE_BUFFER_SELECT)
6386
6387static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6388 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006389 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006390{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006391 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006392 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006393
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006394 req->opcode = READ_ONCE(sqe->opcode);
6395 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006396 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006397 req->file = NULL;
6398 req->ctx = ctx;
6399 req->flags = 0;
6400 /* one is dropped after submission, the other at completion */
6401 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006402 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006403 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006404
6405 if (unlikely(req->opcode >= IORING_OP_LAST))
6406 return -EINVAL;
6407
Jens Axboe9d8426a2020-06-16 18:42:49 -06006408 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6409 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006410
6411 sqe_flags = READ_ONCE(sqe->flags);
6412 /* enforce forwards compatibility on users */
6413 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6414 return -EINVAL;
6415
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006416 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6417 return -EACCES;
6418
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006419 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6420 !io_op_defs[req->opcode].buffer_select)
6421 return -EOPNOTSUPP;
6422
6423 id = READ_ONCE(sqe->personality);
6424 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006425 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006426 req->work.creds = idr_find(&ctx->personality_idr, id);
6427 if (unlikely(!req->work.creds))
6428 return -EINVAL;
6429 get_cred(req->work.creds);
6430 }
6431
6432 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006433 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006434
Jens Axboe63ff8222020-05-07 14:56:15 -06006435 if (!io_op_defs[req->opcode].needs_file)
6436 return 0;
6437
6438 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006439}
6440
Jens Axboe0f212202020-09-13 13:09:39 -06006441static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006442{
Jens Axboeac8691c2020-06-01 08:30:41 -06006443 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006444 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006445 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006446
Jens Axboec4a2ed72019-11-21 21:01:26 -07006447 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006448 if (test_bit(0, &ctx->sq_check_overflow)) {
6449 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006450 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006451 return -EBUSY;
6452 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006453
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006454 /* make sure SQ entry isn't read before tail */
6455 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006456
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006457 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6458 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006459
Jens Axboefaf7b512020-10-07 12:48:53 -06006460 atomic_long_add(nr, &current->io_uring->req_issue);
6461 refcount_add(nr, &current->usage);
6462
Jens Axboe013538b2020-06-22 09:29:15 -06006463 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006464
6465 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006466 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006467 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006468 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006469
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006470 sqe = io_get_sqe(ctx);
6471 if (unlikely(!sqe)) {
6472 io_consume_sqe(ctx);
6473 break;
6474 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006475 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006476 if (unlikely(!req)) {
6477 if (!submitted)
6478 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006479 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006480 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006481
Jens Axboeac8691c2020-06-01 08:30:41 -06006482 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006483 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006484 /* will complete beyond this point, count as submitted */
6485 submitted++;
6486
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006487 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006488fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006489 io_put_req(req);
6490 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006491 break;
6492 }
6493
Jens Axboe354420f2020-01-08 18:55:15 -07006494 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006495 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006496 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006497 if (err)
6498 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006499 }
6500
Pavel Begunkov9466f432020-01-25 22:34:01 +03006501 if (unlikely(submitted != nr)) {
6502 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6503
6504 percpu_ref_put_many(&ctx->refs, nr - ref_used);
Jens Axboefaf7b512020-10-07 12:48:53 -06006505 atomic_long_sub(nr - ref_used, &current->io_uring->req_issue);
6506 put_task_struct_many(current, nr - ref_used);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006507 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006508 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006509 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006510 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006511
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006512 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6513 io_commit_sqring(ctx);
6514
Jens Axboe6c271ce2019-01-10 11:22:30 -07006515 return submitted;
6516}
6517
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006518static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6519{
6520 /* Tell userspace we may need a wakeup call */
6521 spin_lock_irq(&ctx->completion_lock);
6522 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6523 spin_unlock_irq(&ctx->completion_lock);
6524}
6525
6526static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6527{
6528 spin_lock_irq(&ctx->completion_lock);
6529 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6530 spin_unlock_irq(&ctx->completion_lock);
6531}
6532
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006533static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6534 int sync, void *key)
6535{
6536 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6537 int ret;
6538
6539 ret = autoremove_wake_function(wqe, mode, sync, key);
6540 if (ret) {
6541 unsigned long flags;
6542
6543 spin_lock_irqsave(&ctx->completion_lock, flags);
6544 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6545 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6546 }
6547 return ret;
6548}
6549
Jens Axboec8d1ba52020-09-14 11:07:26 -06006550enum sq_ret {
6551 SQT_IDLE = 1,
6552 SQT_SPIN = 2,
6553 SQT_DID_WORK = 4,
6554};
6555
6556static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006557 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006558{
6559 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006560 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006561 unsigned int to_submit;
6562 int ret = 0;
6563
6564again:
6565 if (!list_empty(&ctx->iopoll_list)) {
6566 unsigned nr_events = 0;
6567
6568 mutex_lock(&ctx->uring_lock);
6569 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6570 io_do_iopoll(ctx, &nr_events, 0);
6571 mutex_unlock(&ctx->uring_lock);
6572 }
6573
6574 to_submit = io_sqring_entries(ctx);
6575
6576 /*
6577 * If submit got -EBUSY, flag us as needing the application
6578 * to enter the kernel to reap and flush events.
6579 */
6580 if (!to_submit || ret == -EBUSY || need_resched()) {
6581 /*
6582 * Drop cur_mm before scheduling, we can't hold it for
6583 * long periods (or over schedule()). Do this before
6584 * adding ourselves to the waitqueue, as the unuse/drop
6585 * may sleep.
6586 */
6587 io_sq_thread_drop_mm();
6588
6589 /*
6590 * We're polling. If we're within the defined idle
6591 * period, then let us spin without work before going
6592 * to sleep. The exception is if we got EBUSY doing
6593 * more IO, we should wait for the application to
6594 * reap events and wake us up.
6595 */
6596 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6597 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6598 !percpu_ref_is_dying(&ctx->refs)))
6599 return SQT_SPIN;
6600
Jens Axboe534ca6d2020-09-02 13:52:19 -06006601 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006602 TASK_INTERRUPTIBLE);
6603
6604 /*
6605 * While doing polled IO, before going to sleep, we need
6606 * to check if there are new reqs added to iopoll_list,
6607 * it is because reqs may have been punted to io worker
6608 * and will be added to iopoll_list later, hence check
6609 * the iopoll_list again.
6610 */
6611 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6612 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006613 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006614 goto again;
6615 }
6616
Jens Axboec8d1ba52020-09-14 11:07:26 -06006617 to_submit = io_sqring_entries(ctx);
6618 if (!to_submit || ret == -EBUSY)
6619 return SQT_IDLE;
6620 }
6621
Jens Axboe534ca6d2020-09-02 13:52:19 -06006622 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006623 io_ring_clear_wakeup_flag(ctx);
6624
Jens Axboee95eee22020-09-08 09:11:32 -06006625 /* if we're handling multiple rings, cap submit size for fairness */
6626 if (cap_entries && to_submit > 8)
6627 to_submit = 8;
6628
Jens Axboec8d1ba52020-09-14 11:07:26 -06006629 mutex_lock(&ctx->uring_lock);
6630 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6631 ret = io_submit_sqes(ctx, to_submit);
6632 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006633
6634 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6635 wake_up(&ctx->sqo_sq_wait);
6636
Jens Axboec8d1ba52020-09-14 11:07:26 -06006637 return SQT_DID_WORK;
6638}
6639
Jens Axboe69fb2132020-09-14 11:16:23 -06006640static void io_sqd_init_new(struct io_sq_data *sqd)
6641{
6642 struct io_ring_ctx *ctx;
6643
6644 while (!list_empty(&sqd->ctx_new_list)) {
6645 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6646 init_wait(&ctx->sqo_wait_entry);
6647 ctx->sqo_wait_entry.func = io_sq_wake_function;
6648 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6649 complete(&ctx->sq_thread_comp);
6650 }
6651}
6652
Jens Axboe6c271ce2019-01-10 11:22:30 -07006653static int io_sq_thread(void *data)
6654{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006655 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006656 const struct cred *old_cred = NULL;
6657 struct io_sq_data *sqd = data;
6658 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006659 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006660
Jens Axboec8d1ba52020-09-14 11:07:26 -06006661 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006662 while (!kthread_should_stop()) {
6663 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006664 bool cap_entries;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006665
Jens Axboe69fb2132020-09-14 11:16:23 -06006666 /*
6667 * Any changes to the sqd lists are synchronized through the
6668 * kthread parking. This synchronizes the thread vs users,
6669 * the users are synchronized on the sqd->ctx_lock.
6670 */
6671 if (kthread_should_park())
6672 kthread_parkme();
6673
6674 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6675 io_sqd_init_new(sqd);
6676
Jens Axboee95eee22020-09-08 09:11:32 -06006677 cap_entries = !list_is_singular(&sqd->ctx_list);
6678
Jens Axboe69fb2132020-09-14 11:16:23 -06006679 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6680 if (current->cred != ctx->creds) {
6681 if (old_cred)
6682 revert_creds(old_cred);
6683 old_cred = override_creds(ctx->creds);
6684 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006685 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe69fb2132020-09-14 11:16:23 -06006686
Jens Axboee95eee22020-09-08 09:11:32 -06006687 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006688
6689 io_sq_thread_drop_mm();
6690 }
6691
6692 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006693 io_run_task_work();
6694 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006695 } else if (ret == SQT_IDLE) {
6696 if (kthread_should_park())
6697 continue;
6698 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6699 io_ring_set_wakeup_flag(ctx);
6700 schedule();
6701 start_jiffies = jiffies;
6702 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6703 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006704 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006705 }
6706
Jens Axboe4c6e2772020-07-01 11:29:10 -06006707 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006708
Dennis Zhou91d8f512020-09-16 13:41:05 -07006709 if (cur_css)
6710 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006711 if (old_cred)
6712 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006713
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006714 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006715
Jens Axboe6c271ce2019-01-10 11:22:30 -07006716 return 0;
6717}
6718
Jens Axboebda52162019-09-24 13:47:15 -06006719struct io_wait_queue {
6720 struct wait_queue_entry wq;
6721 struct io_ring_ctx *ctx;
6722 unsigned to_wait;
6723 unsigned nr_timeouts;
6724};
6725
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006726static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006727{
6728 struct io_ring_ctx *ctx = iowq->ctx;
6729
6730 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006731 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006732 * started waiting. For timeouts, we always want to return to userspace,
6733 * regardless of event count.
6734 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006735 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006736 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6737}
6738
6739static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6740 int wake_flags, void *key)
6741{
6742 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6743 wq);
6744
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006745 /* use noflush == true, as we can't safely rely on locking context */
6746 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006747 return -1;
6748
6749 return autoremove_wake_function(curr, mode, wake_flags, key);
6750}
6751
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006752static int io_run_task_work_sig(void)
6753{
6754 if (io_run_task_work())
6755 return 1;
6756 if (!signal_pending(current))
6757 return 0;
6758 if (current->jobctl & JOBCTL_TASK_WORK) {
6759 spin_lock_irq(&current->sighand->siglock);
6760 current->jobctl &= ~JOBCTL_TASK_WORK;
6761 recalc_sigpending();
6762 spin_unlock_irq(&current->sighand->siglock);
6763 return 1;
6764 }
6765 return -EINTR;
6766}
6767
Jens Axboe2b188cc2019-01-07 10:46:33 -07006768/*
6769 * Wait until events become available, if we don't already have some. The
6770 * application must reap them itself, as they reside on the shared cq ring.
6771 */
6772static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6773 const sigset_t __user *sig, size_t sigsz)
6774{
Jens Axboebda52162019-09-24 13:47:15 -06006775 struct io_wait_queue iowq = {
6776 .wq = {
6777 .private = current,
6778 .func = io_wake_function,
6779 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6780 },
6781 .ctx = ctx,
6782 .to_wait = min_events,
6783 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006784 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006785 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006786
Jens Axboeb41e9852020-02-17 09:52:41 -07006787 do {
6788 if (io_cqring_events(ctx, false) >= min_events)
6789 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006790 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006791 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006792 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006793
6794 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006795#ifdef CONFIG_COMPAT
6796 if (in_compat_syscall())
6797 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006798 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006799 else
6800#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006801 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006802
Jens Axboe2b188cc2019-01-07 10:46:33 -07006803 if (ret)
6804 return ret;
6805 }
6806
Jens Axboebda52162019-09-24 13:47:15 -06006807 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006808 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006809 do {
6810 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6811 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006812 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006813 ret = io_run_task_work_sig();
6814 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06006815 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006816 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06006817 break;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006818 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006819 break;
6820 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006821 } while (1);
6822 finish_wait(&ctx->wait, &iowq.wq);
6823
Jens Axboeb7db41c2020-07-04 08:55:50 -06006824 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006825
Hristo Venev75b28af2019-08-26 17:23:46 +00006826 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006827}
6828
Jens Axboe6b063142019-01-10 22:13:58 -07006829static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6830{
6831#if defined(CONFIG_UNIX)
6832 if (ctx->ring_sock) {
6833 struct sock *sock = ctx->ring_sock->sk;
6834 struct sk_buff *skb;
6835
6836 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6837 kfree_skb(skb);
6838 }
6839#else
6840 int i;
6841
Jens Axboe65e19f52019-10-26 07:20:21 -06006842 for (i = 0; i < ctx->nr_user_files; i++) {
6843 struct file *file;
6844
6845 file = io_file_from_index(ctx, i);
6846 if (file)
6847 fput(file);
6848 }
Jens Axboe6b063142019-01-10 22:13:58 -07006849#endif
6850}
6851
Jens Axboe05f3fb32019-12-09 11:22:50 -07006852static void io_file_ref_kill(struct percpu_ref *ref)
6853{
6854 struct fixed_file_data *data;
6855
6856 data = container_of(ref, struct fixed_file_data, refs);
6857 complete(&data->done);
6858}
6859
Jens Axboe6b063142019-01-10 22:13:58 -07006860static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6861{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006862 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006863 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006864 unsigned nr_tables, i;
6865
Jens Axboe05f3fb32019-12-09 11:22:50 -07006866 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006867 return -ENXIO;
6868
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006869 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006870 if (!list_empty(&data->ref_list))
6871 ref_node = list_first_entry(&data->ref_list,
6872 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006873 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006874 if (ref_node)
6875 percpu_ref_kill(&ref_node->refs);
6876
6877 percpu_ref_kill(&data->refs);
6878
6879 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006880 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006881 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006882
Jens Axboe6b063142019-01-10 22:13:58 -07006883 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006884 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6885 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006886 kfree(data->table[i].files);
6887 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006888 percpu_ref_exit(&data->refs);
6889 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006890 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006891 ctx->nr_user_files = 0;
6892 return 0;
6893}
6894
Jens Axboe534ca6d2020-09-02 13:52:19 -06006895static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006896{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006897 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006898 /*
6899 * The park is a bit of a work-around, without it we get
6900 * warning spews on shutdown with SQPOLL set and affinity
6901 * set to a single CPU.
6902 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06006903 if (sqd->thread) {
6904 kthread_park(sqd->thread);
6905 kthread_stop(sqd->thread);
6906 }
6907
6908 kfree(sqd);
6909 }
6910}
6911
Jens Axboeaa061652020-09-02 14:50:27 -06006912static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
6913{
6914 struct io_ring_ctx *ctx_attach;
6915 struct io_sq_data *sqd;
6916 struct fd f;
6917
6918 f = fdget(p->wq_fd);
6919 if (!f.file)
6920 return ERR_PTR(-ENXIO);
6921 if (f.file->f_op != &io_uring_fops) {
6922 fdput(f);
6923 return ERR_PTR(-EINVAL);
6924 }
6925
6926 ctx_attach = f.file->private_data;
6927 sqd = ctx_attach->sq_data;
6928 if (!sqd) {
6929 fdput(f);
6930 return ERR_PTR(-EINVAL);
6931 }
6932
6933 refcount_inc(&sqd->refs);
6934 fdput(f);
6935 return sqd;
6936}
6937
Jens Axboe534ca6d2020-09-02 13:52:19 -06006938static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
6939{
6940 struct io_sq_data *sqd;
6941
Jens Axboeaa061652020-09-02 14:50:27 -06006942 if (p->flags & IORING_SETUP_ATTACH_WQ)
6943 return io_attach_sq_data(p);
6944
Jens Axboe534ca6d2020-09-02 13:52:19 -06006945 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
6946 if (!sqd)
6947 return ERR_PTR(-ENOMEM);
6948
6949 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06006950 INIT_LIST_HEAD(&sqd->ctx_list);
6951 INIT_LIST_HEAD(&sqd->ctx_new_list);
6952 mutex_init(&sqd->ctx_lock);
6953 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06006954 init_waitqueue_head(&sqd->wait);
6955 return sqd;
6956}
6957
Jens Axboe69fb2132020-09-14 11:16:23 -06006958static void io_sq_thread_unpark(struct io_sq_data *sqd)
6959 __releases(&sqd->lock)
6960{
6961 if (!sqd->thread)
6962 return;
6963 kthread_unpark(sqd->thread);
6964 mutex_unlock(&sqd->lock);
6965}
6966
6967static void io_sq_thread_park(struct io_sq_data *sqd)
6968 __acquires(&sqd->lock)
6969{
6970 if (!sqd->thread)
6971 return;
6972 mutex_lock(&sqd->lock);
6973 kthread_park(sqd->thread);
6974}
6975
Jens Axboe534ca6d2020-09-02 13:52:19 -06006976static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6977{
6978 struct io_sq_data *sqd = ctx->sq_data;
6979
6980 if (sqd) {
6981 if (sqd->thread) {
6982 /*
6983 * We may arrive here from the error branch in
6984 * io_sq_offload_create() where the kthread is created
6985 * without being waked up, thus wake it up now to make
6986 * sure the wait will complete.
6987 */
6988 wake_up_process(sqd->thread);
6989 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06006990
6991 io_sq_thread_park(sqd);
6992 }
6993
6994 mutex_lock(&sqd->ctx_lock);
6995 list_del(&ctx->sqd_list);
6996 mutex_unlock(&sqd->ctx_lock);
6997
6998 if (sqd->thread) {
6999 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7000 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007001 }
7002
7003 io_put_sq_data(sqd);
7004 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007005 }
7006}
7007
Jens Axboe6b063142019-01-10 22:13:58 -07007008static void io_finish_async(struct io_ring_ctx *ctx)
7009{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007010 io_sq_thread_stop(ctx);
7011
Jens Axboe561fb042019-10-24 07:25:42 -06007012 if (ctx->io_wq) {
7013 io_wq_destroy(ctx->io_wq);
7014 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007015 }
7016}
7017
7018#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007019/*
7020 * Ensure the UNIX gc is aware of our file set, so we are certain that
7021 * the io_uring can be safely unregistered on process exit, even if we have
7022 * loops in the file referencing.
7023 */
7024static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7025{
7026 struct sock *sk = ctx->ring_sock->sk;
7027 struct scm_fp_list *fpl;
7028 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007029 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007030
Jens Axboe6b063142019-01-10 22:13:58 -07007031 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7032 if (!fpl)
7033 return -ENOMEM;
7034
7035 skb = alloc_skb(0, GFP_KERNEL);
7036 if (!skb) {
7037 kfree(fpl);
7038 return -ENOMEM;
7039 }
7040
7041 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007042
Jens Axboe08a45172019-10-03 08:11:03 -06007043 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007044 fpl->user = get_uid(ctx->user);
7045 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007046 struct file *file = io_file_from_index(ctx, i + offset);
7047
7048 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007049 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007050 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007051 unix_inflight(fpl->user, fpl->fp[nr_files]);
7052 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007053 }
7054
Jens Axboe08a45172019-10-03 08:11:03 -06007055 if (nr_files) {
7056 fpl->max = SCM_MAX_FD;
7057 fpl->count = nr_files;
7058 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007059 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007060 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7061 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007062
Jens Axboe08a45172019-10-03 08:11:03 -06007063 for (i = 0; i < nr_files; i++)
7064 fput(fpl->fp[i]);
7065 } else {
7066 kfree_skb(skb);
7067 kfree(fpl);
7068 }
Jens Axboe6b063142019-01-10 22:13:58 -07007069
7070 return 0;
7071}
7072
7073/*
7074 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7075 * causes regular reference counting to break down. We rely on the UNIX
7076 * garbage collection to take care of this problem for us.
7077 */
7078static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7079{
7080 unsigned left, total;
7081 int ret = 0;
7082
7083 total = 0;
7084 left = ctx->nr_user_files;
7085 while (left) {
7086 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007087
7088 ret = __io_sqe_files_scm(ctx, this_files, total);
7089 if (ret)
7090 break;
7091 left -= this_files;
7092 total += this_files;
7093 }
7094
7095 if (!ret)
7096 return 0;
7097
7098 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007099 struct file *file = io_file_from_index(ctx, total);
7100
7101 if (file)
7102 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007103 total++;
7104 }
7105
7106 return ret;
7107}
7108#else
7109static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7110{
7111 return 0;
7112}
7113#endif
7114
Jens Axboe65e19f52019-10-26 07:20:21 -06007115static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7116 unsigned nr_files)
7117{
7118 int i;
7119
7120 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007121 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007122 unsigned this_files;
7123
7124 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7125 table->files = kcalloc(this_files, sizeof(struct file *),
7126 GFP_KERNEL);
7127 if (!table->files)
7128 break;
7129 nr_files -= this_files;
7130 }
7131
7132 if (i == nr_tables)
7133 return 0;
7134
7135 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007136 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007137 kfree(table->files);
7138 }
7139 return 1;
7140}
7141
Jens Axboe05f3fb32019-12-09 11:22:50 -07007142static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007143{
7144#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007145 struct sock *sock = ctx->ring_sock->sk;
7146 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7147 struct sk_buff *skb;
7148 int i;
7149
7150 __skb_queue_head_init(&list);
7151
7152 /*
7153 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7154 * remove this entry and rearrange the file array.
7155 */
7156 skb = skb_dequeue(head);
7157 while (skb) {
7158 struct scm_fp_list *fp;
7159
7160 fp = UNIXCB(skb).fp;
7161 for (i = 0; i < fp->count; i++) {
7162 int left;
7163
7164 if (fp->fp[i] != file)
7165 continue;
7166
7167 unix_notinflight(fp->user, fp->fp[i]);
7168 left = fp->count - 1 - i;
7169 if (left) {
7170 memmove(&fp->fp[i], &fp->fp[i + 1],
7171 left * sizeof(struct file *));
7172 }
7173 fp->count--;
7174 if (!fp->count) {
7175 kfree_skb(skb);
7176 skb = NULL;
7177 } else {
7178 __skb_queue_tail(&list, skb);
7179 }
7180 fput(file);
7181 file = NULL;
7182 break;
7183 }
7184
7185 if (!file)
7186 break;
7187
7188 __skb_queue_tail(&list, skb);
7189
7190 skb = skb_dequeue(head);
7191 }
7192
7193 if (skb_peek(&list)) {
7194 spin_lock_irq(&head->lock);
7195 while ((skb = __skb_dequeue(&list)) != NULL)
7196 __skb_queue_tail(head, skb);
7197 spin_unlock_irq(&head->lock);
7198 }
7199#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007200 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007201#endif
7202}
7203
Jens Axboe05f3fb32019-12-09 11:22:50 -07007204struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007205 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007206 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007207};
7208
Jens Axboe4a38aed22020-05-14 17:21:15 -06007209static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007210{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007211 struct fixed_file_data *file_data = ref_node->file_data;
7212 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007213 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007214
7215 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007216 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007217 io_ring_file_put(ctx, pfile->file);
7218 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007219 }
7220
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007221 spin_lock(&file_data->lock);
7222 list_del(&ref_node->node);
7223 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007224
Xiaoguang Wang05589552020-03-31 14:05:18 +08007225 percpu_ref_exit(&ref_node->refs);
7226 kfree(ref_node);
7227 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007228}
7229
Jens Axboe4a38aed22020-05-14 17:21:15 -06007230static void io_file_put_work(struct work_struct *work)
7231{
7232 struct io_ring_ctx *ctx;
7233 struct llist_node *node;
7234
7235 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7236 node = llist_del_all(&ctx->file_put_llist);
7237
7238 while (node) {
7239 struct fixed_file_ref_node *ref_node;
7240 struct llist_node *next = node->next;
7241
7242 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7243 __io_file_put_work(ref_node);
7244 node = next;
7245 }
7246}
7247
Jens Axboe05f3fb32019-12-09 11:22:50 -07007248static void io_file_data_ref_zero(struct percpu_ref *ref)
7249{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007250 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007251 struct io_ring_ctx *ctx;
7252 bool first_add;
7253 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007254
Xiaoguang Wang05589552020-03-31 14:05:18 +08007255 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007256 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007257
Jens Axboe4a38aed22020-05-14 17:21:15 -06007258 if (percpu_ref_is_dying(&ctx->file_data->refs))
7259 delay = 0;
7260
7261 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7262 if (!delay)
7263 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7264 else if (first_add)
7265 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007266}
7267
7268static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7269 struct io_ring_ctx *ctx)
7270{
7271 struct fixed_file_ref_node *ref_node;
7272
7273 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7274 if (!ref_node)
7275 return ERR_PTR(-ENOMEM);
7276
7277 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7278 0, GFP_KERNEL)) {
7279 kfree(ref_node);
7280 return ERR_PTR(-ENOMEM);
7281 }
7282 INIT_LIST_HEAD(&ref_node->node);
7283 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007284 ref_node->file_data = ctx->file_data;
7285 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007286}
7287
7288static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7289{
7290 percpu_ref_exit(&ref_node->refs);
7291 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007292}
7293
7294static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7295 unsigned nr_args)
7296{
7297 __s32 __user *fds = (__s32 __user *) arg;
7298 unsigned nr_tables;
7299 struct file *file;
7300 int fd, ret = 0;
7301 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007302 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007303
7304 if (ctx->file_data)
7305 return -EBUSY;
7306 if (!nr_args)
7307 return -EINVAL;
7308 if (nr_args > IORING_MAX_FIXED_FILES)
7309 return -EMFILE;
7310
7311 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7312 if (!ctx->file_data)
7313 return -ENOMEM;
7314 ctx->file_data->ctx = ctx;
7315 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007316 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007317 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007318
7319 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7320 ctx->file_data->table = kcalloc(nr_tables,
7321 sizeof(struct fixed_file_table),
7322 GFP_KERNEL);
7323 if (!ctx->file_data->table) {
7324 kfree(ctx->file_data);
7325 ctx->file_data = NULL;
7326 return -ENOMEM;
7327 }
7328
Xiaoguang Wang05589552020-03-31 14:05:18 +08007329 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007330 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7331 kfree(ctx->file_data->table);
7332 kfree(ctx->file_data);
7333 ctx->file_data = NULL;
7334 return -ENOMEM;
7335 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007336
7337 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7338 percpu_ref_exit(&ctx->file_data->refs);
7339 kfree(ctx->file_data->table);
7340 kfree(ctx->file_data);
7341 ctx->file_data = NULL;
7342 return -ENOMEM;
7343 }
7344
7345 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7346 struct fixed_file_table *table;
7347 unsigned index;
7348
7349 ret = -EFAULT;
7350 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7351 break;
7352 /* allow sparse sets */
7353 if (fd == -1) {
7354 ret = 0;
7355 continue;
7356 }
7357
7358 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7359 index = i & IORING_FILE_TABLE_MASK;
7360 file = fget(fd);
7361
7362 ret = -EBADF;
7363 if (!file)
7364 break;
7365
7366 /*
7367 * Don't allow io_uring instances to be registered. If UNIX
7368 * isn't enabled, then this causes a reference cycle and this
7369 * instance can never get freed. If UNIX is enabled we'll
7370 * handle it just fine, but there's still no point in allowing
7371 * a ring fd as it doesn't support regular read/write anyway.
7372 */
7373 if (file->f_op == &io_uring_fops) {
7374 fput(file);
7375 break;
7376 }
7377 ret = 0;
7378 table->files[index] = file;
7379 }
7380
7381 if (ret) {
7382 for (i = 0; i < ctx->nr_user_files; i++) {
7383 file = io_file_from_index(ctx, i);
7384 if (file)
7385 fput(file);
7386 }
7387 for (i = 0; i < nr_tables; i++)
7388 kfree(ctx->file_data->table[i].files);
7389
Yang Yingliang667e57d2020-07-10 14:14:20 +00007390 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007391 kfree(ctx->file_data->table);
7392 kfree(ctx->file_data);
7393 ctx->file_data = NULL;
7394 ctx->nr_user_files = 0;
7395 return ret;
7396 }
7397
7398 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007399 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007400 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007401 return ret;
7402 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007403
Xiaoguang Wang05589552020-03-31 14:05:18 +08007404 ref_node = alloc_fixed_file_ref_node(ctx);
7405 if (IS_ERR(ref_node)) {
7406 io_sqe_files_unregister(ctx);
7407 return PTR_ERR(ref_node);
7408 }
7409
7410 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007411 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007412 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007413 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007414 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007415 return ret;
7416}
7417
Jens Axboec3a31e62019-10-03 13:59:56 -06007418static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7419 int index)
7420{
7421#if defined(CONFIG_UNIX)
7422 struct sock *sock = ctx->ring_sock->sk;
7423 struct sk_buff_head *head = &sock->sk_receive_queue;
7424 struct sk_buff *skb;
7425
7426 /*
7427 * See if we can merge this file into an existing skb SCM_RIGHTS
7428 * file set. If there's no room, fall back to allocating a new skb
7429 * and filling it in.
7430 */
7431 spin_lock_irq(&head->lock);
7432 skb = skb_peek(head);
7433 if (skb) {
7434 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7435
7436 if (fpl->count < SCM_MAX_FD) {
7437 __skb_unlink(skb, head);
7438 spin_unlock_irq(&head->lock);
7439 fpl->fp[fpl->count] = get_file(file);
7440 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7441 fpl->count++;
7442 spin_lock_irq(&head->lock);
7443 __skb_queue_head(head, skb);
7444 } else {
7445 skb = NULL;
7446 }
7447 }
7448 spin_unlock_irq(&head->lock);
7449
7450 if (skb) {
7451 fput(file);
7452 return 0;
7453 }
7454
7455 return __io_sqe_files_scm(ctx, 1, index);
7456#else
7457 return 0;
7458#endif
7459}
7460
Hillf Dantona5318d32020-03-23 17:47:15 +08007461static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007462 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007463{
Hillf Dantona5318d32020-03-23 17:47:15 +08007464 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007465 struct percpu_ref *refs = data->cur_refs;
7466 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007467
Jens Axboe05f3fb32019-12-09 11:22:50 -07007468 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007469 if (!pfile)
7470 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007471
Xiaoguang Wang05589552020-03-31 14:05:18 +08007472 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007473 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007474 list_add(&pfile->list, &ref_node->file_list);
7475
Hillf Dantona5318d32020-03-23 17:47:15 +08007476 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007477}
7478
7479static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7480 struct io_uring_files_update *up,
7481 unsigned nr_args)
7482{
7483 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007484 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007485 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007486 __s32 __user *fds;
7487 int fd, i, err;
7488 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007489 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007490
Jens Axboe05f3fb32019-12-09 11:22:50 -07007491 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007492 return -EOVERFLOW;
7493 if (done > ctx->nr_user_files)
7494 return -EINVAL;
7495
Xiaoguang Wang05589552020-03-31 14:05:18 +08007496 ref_node = alloc_fixed_file_ref_node(ctx);
7497 if (IS_ERR(ref_node))
7498 return PTR_ERR(ref_node);
7499
Jens Axboec3a31e62019-10-03 13:59:56 -06007500 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007501 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007502 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007503 struct fixed_file_table *table;
7504 unsigned index;
7505
Jens Axboec3a31e62019-10-03 13:59:56 -06007506 err = 0;
7507 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7508 err = -EFAULT;
7509 break;
7510 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007511 i = array_index_nospec(up->offset, ctx->nr_user_files);
7512 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007513 index = i & IORING_FILE_TABLE_MASK;
7514 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007515 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007516 err = io_queue_file_removal(data, file);
7517 if (err)
7518 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007519 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007520 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007521 }
7522 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007523 file = fget(fd);
7524 if (!file) {
7525 err = -EBADF;
7526 break;
7527 }
7528 /*
7529 * Don't allow io_uring instances to be registered. If
7530 * UNIX isn't enabled, then this causes a reference
7531 * cycle and this instance can never get freed. If UNIX
7532 * is enabled we'll handle it just fine, but there's
7533 * still no point in allowing a ring fd as it doesn't
7534 * support regular read/write anyway.
7535 */
7536 if (file->f_op == &io_uring_fops) {
7537 fput(file);
7538 err = -EBADF;
7539 break;
7540 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007541 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007542 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007543 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007544 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007545 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007546 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007547 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007548 }
7549 nr_args--;
7550 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007551 up->offset++;
7552 }
7553
Xiaoguang Wang05589552020-03-31 14:05:18 +08007554 if (needs_switch) {
7555 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007556 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007557 list_add(&ref_node->node, &data->ref_list);
7558 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007559 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007560 percpu_ref_get(&ctx->file_data->refs);
7561 } else
7562 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007563
7564 return done ? done : err;
7565}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007566
Jens Axboe05f3fb32019-12-09 11:22:50 -07007567static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7568 unsigned nr_args)
7569{
7570 struct io_uring_files_update up;
7571
7572 if (!ctx->file_data)
7573 return -ENXIO;
7574 if (!nr_args)
7575 return -EINVAL;
7576 if (copy_from_user(&up, arg, sizeof(up)))
7577 return -EFAULT;
7578 if (up.resv)
7579 return -EINVAL;
7580
7581 return __io_sqe_files_update(ctx, &up, nr_args);
7582}
Jens Axboec3a31e62019-10-03 13:59:56 -06007583
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007584static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007585{
7586 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7587
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007588 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007589 io_put_req(req);
7590}
7591
Pavel Begunkov24369c22020-01-28 03:15:48 +03007592static int io_init_wq_offload(struct io_ring_ctx *ctx,
7593 struct io_uring_params *p)
7594{
7595 struct io_wq_data data;
7596 struct fd f;
7597 struct io_ring_ctx *ctx_attach;
7598 unsigned int concurrency;
7599 int ret = 0;
7600
7601 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007602 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007603 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007604
7605 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7606 /* Do QD, or 4 * CPUS, whatever is smallest */
7607 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7608
7609 ctx->io_wq = io_wq_create(concurrency, &data);
7610 if (IS_ERR(ctx->io_wq)) {
7611 ret = PTR_ERR(ctx->io_wq);
7612 ctx->io_wq = NULL;
7613 }
7614 return ret;
7615 }
7616
7617 f = fdget(p->wq_fd);
7618 if (!f.file)
7619 return -EBADF;
7620
7621 if (f.file->f_op != &io_uring_fops) {
7622 ret = -EINVAL;
7623 goto out_fput;
7624 }
7625
7626 ctx_attach = f.file->private_data;
7627 /* @io_wq is protected by holding the fd */
7628 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7629 ret = -EINVAL;
7630 goto out_fput;
7631 }
7632
7633 ctx->io_wq = ctx_attach->io_wq;
7634out_fput:
7635 fdput(f);
7636 return ret;
7637}
7638
Jens Axboe0f212202020-09-13 13:09:39 -06007639static int io_uring_alloc_task_context(struct task_struct *task)
7640{
7641 struct io_uring_task *tctx;
7642
7643 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7644 if (unlikely(!tctx))
7645 return -ENOMEM;
7646
7647 xa_init(&tctx->xa);
7648 init_waitqueue_head(&tctx->wait);
7649 tctx->last = NULL;
7650 tctx->in_idle = 0;
7651 atomic_long_set(&tctx->req_issue, 0);
7652 atomic_long_set(&tctx->req_complete, 0);
7653 task->io_uring = tctx;
7654 return 0;
7655}
7656
7657void __io_uring_free(struct task_struct *tsk)
7658{
7659 struct io_uring_task *tctx = tsk->io_uring;
7660
7661 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe0f212202020-09-13 13:09:39 -06007662 kfree(tctx);
7663 tsk->io_uring = NULL;
7664}
7665
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007666static int io_sq_offload_create(struct io_ring_ctx *ctx,
7667 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007668{
7669 int ret;
7670
Jens Axboe6c271ce2019-01-10 11:22:30 -07007671 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007672 struct io_sq_data *sqd;
7673
Jens Axboe3ec482d2019-04-08 10:51:01 -06007674 ret = -EPERM;
7675 if (!capable(CAP_SYS_ADMIN))
7676 goto err;
7677
Jens Axboe534ca6d2020-09-02 13:52:19 -06007678 sqd = io_get_sq_data(p);
7679 if (IS_ERR(sqd)) {
7680 ret = PTR_ERR(sqd);
7681 goto err;
7682 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007683
Jens Axboe534ca6d2020-09-02 13:52:19 -06007684 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007685 io_sq_thread_park(sqd);
7686 mutex_lock(&sqd->ctx_lock);
7687 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7688 mutex_unlock(&sqd->ctx_lock);
7689 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007690
Jens Axboe917257d2019-04-13 09:28:55 -06007691 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7692 if (!ctx->sq_thread_idle)
7693 ctx->sq_thread_idle = HZ;
7694
Jens Axboeaa061652020-09-02 14:50:27 -06007695 if (sqd->thread)
7696 goto done;
7697
Jens Axboe6c271ce2019-01-10 11:22:30 -07007698 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007699 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007700
Jens Axboe917257d2019-04-13 09:28:55 -06007701 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007702 if (cpu >= nr_cpu_ids)
7703 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007704 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007705 goto err;
7706
Jens Axboe69fb2132020-09-14 11:16:23 -06007707 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007708 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007709 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007710 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007711 "io_uring-sq");
7712 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007713 if (IS_ERR(sqd->thread)) {
7714 ret = PTR_ERR(sqd->thread);
7715 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007716 goto err;
7717 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007718 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007719 if (ret)
7720 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007721 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7722 /* Can't have SQ_AFF without SQPOLL */
7723 ret = -EINVAL;
7724 goto err;
7725 }
7726
Jens Axboeaa061652020-09-02 14:50:27 -06007727done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007728 ret = io_init_wq_offload(ctx, p);
7729 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007730 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007731
7732 return 0;
7733err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007734 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007735 return ret;
7736}
7737
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007738static void io_sq_offload_start(struct io_ring_ctx *ctx)
7739{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007740 struct io_sq_data *sqd = ctx->sq_data;
7741
7742 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7743 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007744}
7745
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007746static inline void __io_unaccount_mem(struct user_struct *user,
7747 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007748{
7749 atomic_long_sub(nr_pages, &user->locked_vm);
7750}
7751
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007752static inline int __io_account_mem(struct user_struct *user,
7753 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007754{
7755 unsigned long page_limit, cur_pages, new_pages;
7756
7757 /* Don't allow more pages than we can safely lock */
7758 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7759
7760 do {
7761 cur_pages = atomic_long_read(&user->locked_vm);
7762 new_pages = cur_pages + nr_pages;
7763 if (new_pages > page_limit)
7764 return -ENOMEM;
7765 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7766 new_pages) != cur_pages);
7767
7768 return 0;
7769}
7770
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007771static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7772 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007773{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007774 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007775 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007776
Jens Axboe2aede0e2020-09-14 10:45:53 -06007777 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007778 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007779 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007780 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007781 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007782 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007783}
7784
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007785static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7786 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007787{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007788 int ret;
7789
7790 if (ctx->limit_mem) {
7791 ret = __io_account_mem(ctx->user, nr_pages);
7792 if (ret)
7793 return ret;
7794 }
7795
Jens Axboe2aede0e2020-09-14 10:45:53 -06007796 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007797 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007798 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007799 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007800 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007801 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007802
7803 return 0;
7804}
7805
Jens Axboe2b188cc2019-01-07 10:46:33 -07007806static void io_mem_free(void *ptr)
7807{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007808 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007809
Mark Rutland52e04ef2019-04-30 17:30:21 +01007810 if (!ptr)
7811 return;
7812
7813 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007814 if (put_page_testzero(page))
7815 free_compound_page(page);
7816}
7817
7818static void *io_mem_alloc(size_t size)
7819{
7820 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7821 __GFP_NORETRY;
7822
7823 return (void *) __get_free_pages(gfp_flags, get_order(size));
7824}
7825
Hristo Venev75b28af2019-08-26 17:23:46 +00007826static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7827 size_t *sq_offset)
7828{
7829 struct io_rings *rings;
7830 size_t off, sq_array_size;
7831
7832 off = struct_size(rings, cqes, cq_entries);
7833 if (off == SIZE_MAX)
7834 return SIZE_MAX;
7835
7836#ifdef CONFIG_SMP
7837 off = ALIGN(off, SMP_CACHE_BYTES);
7838 if (off == 0)
7839 return SIZE_MAX;
7840#endif
7841
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007842 if (sq_offset)
7843 *sq_offset = off;
7844
Hristo Venev75b28af2019-08-26 17:23:46 +00007845 sq_array_size = array_size(sizeof(u32), sq_entries);
7846 if (sq_array_size == SIZE_MAX)
7847 return SIZE_MAX;
7848
7849 if (check_add_overflow(off, sq_array_size, &off))
7850 return SIZE_MAX;
7851
Hristo Venev75b28af2019-08-26 17:23:46 +00007852 return off;
7853}
7854
Jens Axboe2b188cc2019-01-07 10:46:33 -07007855static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7856{
Hristo Venev75b28af2019-08-26 17:23:46 +00007857 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007858
Hristo Venev75b28af2019-08-26 17:23:46 +00007859 pages = (size_t)1 << get_order(
7860 rings_size(sq_entries, cq_entries, NULL));
7861 pages += (size_t)1 << get_order(
7862 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007863
Hristo Venev75b28af2019-08-26 17:23:46 +00007864 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007865}
7866
Jens Axboeedafcce2019-01-09 09:16:05 -07007867static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7868{
7869 int i, j;
7870
7871 if (!ctx->user_bufs)
7872 return -ENXIO;
7873
7874 for (i = 0; i < ctx->nr_user_bufs; i++) {
7875 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7876
7877 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007878 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007879
Jens Axboede293932020-09-17 16:19:16 -06007880 if (imu->acct_pages)
7881 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007882 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007883 imu->nr_bvecs = 0;
7884 }
7885
7886 kfree(ctx->user_bufs);
7887 ctx->user_bufs = NULL;
7888 ctx->nr_user_bufs = 0;
7889 return 0;
7890}
7891
7892static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7893 void __user *arg, unsigned index)
7894{
7895 struct iovec __user *src;
7896
7897#ifdef CONFIG_COMPAT
7898 if (ctx->compat) {
7899 struct compat_iovec __user *ciovs;
7900 struct compat_iovec ciov;
7901
7902 ciovs = (struct compat_iovec __user *) arg;
7903 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7904 return -EFAULT;
7905
Jens Axboed55e5f52019-12-11 16:12:15 -07007906 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007907 dst->iov_len = ciov.iov_len;
7908 return 0;
7909 }
7910#endif
7911 src = (struct iovec __user *) arg;
7912 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7913 return -EFAULT;
7914 return 0;
7915}
7916
Jens Axboede293932020-09-17 16:19:16 -06007917/*
7918 * Not super efficient, but this is just a registration time. And we do cache
7919 * the last compound head, so generally we'll only do a full search if we don't
7920 * match that one.
7921 *
7922 * We check if the given compound head page has already been accounted, to
7923 * avoid double accounting it. This allows us to account the full size of the
7924 * page, not just the constituent pages of a huge page.
7925 */
7926static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
7927 int nr_pages, struct page *hpage)
7928{
7929 int i, j;
7930
7931 /* check current page array */
7932 for (i = 0; i < nr_pages; i++) {
7933 if (!PageCompound(pages[i]))
7934 continue;
7935 if (compound_head(pages[i]) == hpage)
7936 return true;
7937 }
7938
7939 /* check previously registered pages */
7940 for (i = 0; i < ctx->nr_user_bufs; i++) {
7941 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7942
7943 for (j = 0; j < imu->nr_bvecs; j++) {
7944 if (!PageCompound(imu->bvec[j].bv_page))
7945 continue;
7946 if (compound_head(imu->bvec[j].bv_page) == hpage)
7947 return true;
7948 }
7949 }
7950
7951 return false;
7952}
7953
7954static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
7955 int nr_pages, struct io_mapped_ubuf *imu,
7956 struct page **last_hpage)
7957{
7958 int i, ret;
7959
7960 for (i = 0; i < nr_pages; i++) {
7961 if (!PageCompound(pages[i])) {
7962 imu->acct_pages++;
7963 } else {
7964 struct page *hpage;
7965
7966 hpage = compound_head(pages[i]);
7967 if (hpage == *last_hpage)
7968 continue;
7969 *last_hpage = hpage;
7970 if (headpage_already_acct(ctx, pages, i, hpage))
7971 continue;
7972 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
7973 }
7974 }
7975
7976 if (!imu->acct_pages)
7977 return 0;
7978
7979 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
7980 if (ret)
7981 imu->acct_pages = 0;
7982 return ret;
7983}
7984
Jens Axboeedafcce2019-01-09 09:16:05 -07007985static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7986 unsigned nr_args)
7987{
7988 struct vm_area_struct **vmas = NULL;
7989 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06007990 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07007991 int i, j, got_pages = 0;
7992 int ret = -EINVAL;
7993
7994 if (ctx->user_bufs)
7995 return -EBUSY;
7996 if (!nr_args || nr_args > UIO_MAXIOV)
7997 return -EINVAL;
7998
7999 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8000 GFP_KERNEL);
8001 if (!ctx->user_bufs)
8002 return -ENOMEM;
8003
8004 for (i = 0; i < nr_args; i++) {
8005 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8006 unsigned long off, start, end, ubuf;
8007 int pret, nr_pages;
8008 struct iovec iov;
8009 size_t size;
8010
8011 ret = io_copy_iov(ctx, &iov, arg, i);
8012 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008013 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008014
8015 /*
8016 * Don't impose further limits on the size and buffer
8017 * constraints here, we'll -EINVAL later when IO is
8018 * submitted if they are wrong.
8019 */
8020 ret = -EFAULT;
8021 if (!iov.iov_base || !iov.iov_len)
8022 goto err;
8023
8024 /* arbitrary limit, but we need something */
8025 if (iov.iov_len > SZ_1G)
8026 goto err;
8027
8028 ubuf = (unsigned long) iov.iov_base;
8029 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8030 start = ubuf >> PAGE_SHIFT;
8031 nr_pages = end - start;
8032
Jens Axboeedafcce2019-01-09 09:16:05 -07008033 ret = 0;
8034 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008035 kvfree(vmas);
8036 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008037 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008038 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008039 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008040 sizeof(struct vm_area_struct *),
8041 GFP_KERNEL);
8042 if (!pages || !vmas) {
8043 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008044 goto err;
8045 }
8046 got_pages = nr_pages;
8047 }
8048
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008049 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008050 GFP_KERNEL);
8051 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008052 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008053 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008054
8055 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008056 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008057 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008058 FOLL_WRITE | FOLL_LONGTERM,
8059 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008060 if (pret == nr_pages) {
8061 /* don't support file backed memory */
8062 for (j = 0; j < nr_pages; j++) {
8063 struct vm_area_struct *vma = vmas[j];
8064
8065 if (vma->vm_file &&
8066 !is_file_hugepages(vma->vm_file)) {
8067 ret = -EOPNOTSUPP;
8068 break;
8069 }
8070 }
8071 } else {
8072 ret = pret < 0 ? pret : -EFAULT;
8073 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008074 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008075 if (ret) {
8076 /*
8077 * if we did partial map, or found file backed vmas,
8078 * release any pages we did get
8079 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008080 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008081 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008082 kvfree(imu->bvec);
8083 goto err;
8084 }
8085
8086 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8087 if (ret) {
8088 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008089 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008090 goto err;
8091 }
8092
8093 off = ubuf & ~PAGE_MASK;
8094 size = iov.iov_len;
8095 for (j = 0; j < nr_pages; j++) {
8096 size_t vec_len;
8097
8098 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8099 imu->bvec[j].bv_page = pages[j];
8100 imu->bvec[j].bv_len = vec_len;
8101 imu->bvec[j].bv_offset = off;
8102 off = 0;
8103 size -= vec_len;
8104 }
8105 /* store original address for later verification */
8106 imu->ubuf = ubuf;
8107 imu->len = iov.iov_len;
8108 imu->nr_bvecs = nr_pages;
8109
8110 ctx->nr_user_bufs++;
8111 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008112 kvfree(pages);
8113 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008114 return 0;
8115err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008116 kvfree(pages);
8117 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008118 io_sqe_buffer_unregister(ctx);
8119 return ret;
8120}
8121
Jens Axboe9b402842019-04-11 11:45:41 -06008122static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8123{
8124 __s32 __user *fds = arg;
8125 int fd;
8126
8127 if (ctx->cq_ev_fd)
8128 return -EBUSY;
8129
8130 if (copy_from_user(&fd, fds, sizeof(*fds)))
8131 return -EFAULT;
8132
8133 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8134 if (IS_ERR(ctx->cq_ev_fd)) {
8135 int ret = PTR_ERR(ctx->cq_ev_fd);
8136 ctx->cq_ev_fd = NULL;
8137 return ret;
8138 }
8139
8140 return 0;
8141}
8142
8143static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8144{
8145 if (ctx->cq_ev_fd) {
8146 eventfd_ctx_put(ctx->cq_ev_fd);
8147 ctx->cq_ev_fd = NULL;
8148 return 0;
8149 }
8150
8151 return -ENXIO;
8152}
8153
Jens Axboe5a2e7452020-02-23 16:23:11 -07008154static int __io_destroy_buffers(int id, void *p, void *data)
8155{
8156 struct io_ring_ctx *ctx = data;
8157 struct io_buffer *buf = p;
8158
Jens Axboe067524e2020-03-02 16:32:28 -07008159 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008160 return 0;
8161}
8162
8163static void io_destroy_buffers(struct io_ring_ctx *ctx)
8164{
8165 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8166 idr_destroy(&ctx->io_buffer_idr);
8167}
8168
Jens Axboe2b188cc2019-01-07 10:46:33 -07008169static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8170{
Jens Axboe6b063142019-01-10 22:13:58 -07008171 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008172 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008173
8174 if (ctx->sqo_task) {
8175 put_task_struct(ctx->sqo_task);
8176 ctx->sqo_task = NULL;
8177 mmdrop(ctx->mm_account);
8178 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008179 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008180
Dennis Zhou91d8f512020-09-16 13:41:05 -07008181#ifdef CONFIG_BLK_CGROUP
8182 if (ctx->sqo_blkcg_css)
8183 css_put(ctx->sqo_blkcg_css);
8184#endif
8185
Jens Axboe6b063142019-01-10 22:13:58 -07008186 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008187 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008188 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008189 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008190
Jens Axboe2b188cc2019-01-07 10:46:33 -07008191#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008192 if (ctx->ring_sock) {
8193 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008194 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008195 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008196#endif
8197
Hristo Venev75b28af2019-08-26 17:23:46 +00008198 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008199 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008200
8201 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008202 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008203 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008204 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008205 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008206 kfree(ctx);
8207}
8208
8209static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8210{
8211 struct io_ring_ctx *ctx = file->private_data;
8212 __poll_t mask = 0;
8213
8214 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008215 /*
8216 * synchronizes with barrier from wq_has_sleeper call in
8217 * io_commit_cqring
8218 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008219 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008220 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008221 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008222 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008223 mask |= EPOLLIN | EPOLLRDNORM;
8224
8225 return mask;
8226}
8227
8228static int io_uring_fasync(int fd, struct file *file, int on)
8229{
8230 struct io_ring_ctx *ctx = file->private_data;
8231
8232 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8233}
8234
Jens Axboe071698e2020-01-28 10:04:42 -07008235static int io_remove_personalities(int id, void *p, void *data)
8236{
8237 struct io_ring_ctx *ctx = data;
8238 const struct cred *cred;
8239
8240 cred = idr_remove(&ctx->personality_idr, id);
8241 if (cred)
8242 put_cred(cred);
8243 return 0;
8244}
8245
Jens Axboe85faa7b2020-04-09 18:14:00 -06008246static void io_ring_exit_work(struct work_struct *work)
8247{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008248 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8249 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008250
Jens Axboe56952e92020-06-17 15:00:04 -06008251 /*
8252 * If we're doing polled IO and end up having requests being
8253 * submitted async (out-of-line), then completions can come in while
8254 * we're waiting for refs to drop. We need to reap these manually,
8255 * as nobody else will be looking for them.
8256 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008257 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008258 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008259 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008260 io_iopoll_try_reap_events(ctx);
8261 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008262 io_ring_ctx_free(ctx);
8263}
8264
Jens Axboe2b188cc2019-01-07 10:46:33 -07008265static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8266{
8267 mutex_lock(&ctx->uring_lock);
8268 percpu_ref_kill(&ctx->refs);
8269 mutex_unlock(&ctx->uring_lock);
8270
Jens Axboef3606e32020-09-22 08:18:24 -06008271 io_kill_timeouts(ctx, NULL);
8272 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008273
8274 if (ctx->io_wq)
8275 io_wq_cancel_all(ctx->io_wq);
8276
Jens Axboe15dff282019-11-13 09:09:23 -07008277 /* if we failed setting up the ctx, we might not have any rings */
8278 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008279 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008280 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008281 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008282
8283 /*
8284 * Do this upfront, so we won't have a grace period where the ring
8285 * is closed but resources aren't reaped yet. This can cause
8286 * spurious failure in setting up a new ring.
8287 */
Jens Axboe760618f2020-07-24 12:53:31 -06008288 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8289 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008290
Jens Axboe85faa7b2020-04-09 18:14:00 -06008291 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008292 /*
8293 * Use system_unbound_wq to avoid spawning tons of event kworkers
8294 * if we're exiting a ton of rings at the same time. It just adds
8295 * noise and overhead, there's no discernable change in runtime
8296 * over using system_wq.
8297 */
8298 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008299}
8300
8301static int io_uring_release(struct inode *inode, struct file *file)
8302{
8303 struct io_ring_ctx *ctx = file->private_data;
8304
8305 file->private_data = NULL;
8306 io_ring_ctx_wait_and_kill(ctx);
8307 return 0;
8308}
8309
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008310static bool io_wq_files_match(struct io_wq_work *work, void *data)
8311{
8312 struct files_struct *files = data;
8313
Jens Axboe0f212202020-09-13 13:09:39 -06008314 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008315}
8316
Jens Axboef254ac02020-08-12 17:33:30 -06008317/*
8318 * Returns true if 'preq' is the link parent of 'req'
8319 */
8320static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8321{
8322 struct io_kiocb *link;
8323
8324 if (!(preq->flags & REQ_F_LINK_HEAD))
8325 return false;
8326
8327 list_for_each_entry(link, &preq->link_list, link_list) {
8328 if (link == req)
8329 return true;
8330 }
8331
8332 return false;
8333}
8334
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008335static bool io_match_link_files(struct io_kiocb *req,
8336 struct files_struct *files)
8337{
8338 struct io_kiocb *link;
8339
8340 if (io_match_files(req, files))
8341 return true;
8342 if (req->flags & REQ_F_LINK_HEAD) {
8343 list_for_each_entry(link, &req->link_list, link_list) {
8344 if (io_match_files(link, files))
8345 return true;
8346 }
8347 }
8348 return false;
8349}
8350
Jens Axboef254ac02020-08-12 17:33:30 -06008351/*
8352 * We're looking to cancel 'req' because it's holding on to our files, but
8353 * 'req' could be a link to another request. See if it is, and cancel that
8354 * parent request if so.
8355 */
8356static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8357{
8358 struct hlist_node *tmp;
8359 struct io_kiocb *preq;
8360 bool found = false;
8361 int i;
8362
8363 spin_lock_irq(&ctx->completion_lock);
8364 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8365 struct hlist_head *list;
8366
8367 list = &ctx->cancel_hash[i];
8368 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8369 found = io_match_link(preq, req);
8370 if (found) {
8371 io_poll_remove_one(preq);
8372 break;
8373 }
8374 }
8375 }
8376 spin_unlock_irq(&ctx->completion_lock);
8377 return found;
8378}
8379
8380static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8381 struct io_kiocb *req)
8382{
8383 struct io_kiocb *preq;
8384 bool found = false;
8385
8386 spin_lock_irq(&ctx->completion_lock);
8387 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8388 found = io_match_link(preq, req);
8389 if (found) {
8390 __io_timeout_cancel(preq);
8391 break;
8392 }
8393 }
8394 spin_unlock_irq(&ctx->completion_lock);
8395 return found;
8396}
8397
Jens Axboeb711d4e2020-08-16 08:23:05 -07008398static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8399{
8400 return io_match_link(container_of(work, struct io_kiocb, work), data);
8401}
8402
8403static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8404{
8405 enum io_wq_cancel cret;
8406
8407 /* cancel this particular work, if it's running */
8408 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8409 if (cret != IO_WQ_CANCEL_NOTFOUND)
8410 return;
8411
8412 /* find links that hold this pending, cancel those */
8413 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8414 if (cret != IO_WQ_CANCEL_NOTFOUND)
8415 return;
8416
8417 /* if we have a poll link holding this pending, cancel that */
8418 if (io_poll_remove_link(ctx, req))
8419 return;
8420
8421 /* final option, timeout link is holding this req pending */
8422 io_timeout_remove_link(ctx, req);
8423}
8424
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008425static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8426 struct files_struct *files)
8427{
8428 struct io_defer_entry *de = NULL;
8429 LIST_HEAD(list);
8430
8431 spin_lock_irq(&ctx->completion_lock);
8432 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008433 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008434 list_cut_position(&list, &ctx->defer_list, &de->list);
8435 break;
8436 }
8437 }
8438 spin_unlock_irq(&ctx->completion_lock);
8439
8440 while (!list_empty(&list)) {
8441 de = list_first_entry(&list, struct io_defer_entry, list);
8442 list_del_init(&de->list);
8443 req_set_fail_links(de->req);
8444 io_put_req(de->req);
8445 io_req_complete(de->req, -ECANCELED);
8446 kfree(de);
8447 }
8448}
8449
Jens Axboe76e1b642020-09-26 15:05:03 -06008450/*
8451 * Returns true if we found and killed one or more files pinning requests
8452 */
8453static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008454 struct files_struct *files)
8455{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008456 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008457 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008458
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008459 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008460 /* cancel all at once, should be faster than doing it one by one*/
8461 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8462
Jens Axboefcb323c2019-10-24 12:39:47 -06008463 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008464 struct io_kiocb *cancel_req = NULL, *req;
8465 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008466
8467 spin_lock_irq(&ctx->inflight_lock);
8468 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008469 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008470 continue;
8471 /* req is being completed, ignore */
8472 if (!refcount_inc_not_zero(&req->refs))
8473 continue;
8474 cancel_req = req;
8475 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008476 }
Jens Axboe768134d2019-11-10 20:30:53 -07008477 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008478 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008479 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008480 spin_unlock_irq(&ctx->inflight_lock);
8481
Jens Axboe768134d2019-11-10 20:30:53 -07008482 /* We need to keep going until we don't find a matching req */
8483 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008484 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008485 /* cancel this request, or head link requests */
8486 io_attempt_cancel(ctx, cancel_req);
8487 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008488 /* cancellations _may_ trigger task work */
8489 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008490 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008491 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008492 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008493
8494 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008495}
8496
Pavel Begunkov801dd572020-06-15 10:33:14 +03008497static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008498{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008499 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8500 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008501
Jens Axboef3606e32020-09-22 08:18:24 -06008502 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008503}
8504
Jens Axboe0f212202020-09-13 13:09:39 -06008505static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8506 struct task_struct *task,
8507 struct files_struct *files)
8508{
8509 bool ret;
8510
8511 ret = io_uring_cancel_files(ctx, files);
8512 if (!files) {
8513 enum io_wq_cancel cret;
8514
8515 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8516 if (cret != IO_WQ_CANCEL_NOTFOUND)
8517 ret = true;
8518
8519 /* SQPOLL thread does its own polling */
8520 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8521 while (!list_empty_careful(&ctx->iopoll_list)) {
8522 io_iopoll_try_reap_events(ctx);
8523 ret = true;
8524 }
8525 }
8526
8527 ret |= io_poll_remove_all(ctx, task);
8528 ret |= io_kill_timeouts(ctx, task);
8529 }
8530
8531 return ret;
8532}
8533
8534/*
8535 * We need to iteratively cancel requests, in case a request has dependent
8536 * hard links. These persist even for failure of cancelations, hence keep
8537 * looping until none are found.
8538 */
8539static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8540 struct files_struct *files)
8541{
8542 struct task_struct *task = current;
8543
Jens Axboe534ca6d2020-09-02 13:52:19 -06008544 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8545 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008546
8547 io_cqring_overflow_flush(ctx, true, task, files);
8548
8549 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8550 io_run_task_work();
8551 cond_resched();
8552 }
8553}
8554
8555/*
8556 * Note that this task has used io_uring. We use it for cancelation purposes.
8557 */
8558static int io_uring_add_task_file(struct file *file)
8559{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008560 struct io_uring_task *tctx = current->io_uring;
8561
8562 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008563 int ret;
8564
8565 ret = io_uring_alloc_task_context(current);
8566 if (unlikely(ret))
8567 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008568 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008569 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008570 if (tctx->last != file) {
8571 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008572
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008573 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008574 get_file(file);
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008575 xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008576 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008577 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008578 }
8579
8580 return 0;
8581}
8582
8583/*
8584 * Remove this io_uring_file -> task mapping.
8585 */
8586static void io_uring_del_task_file(struct file *file)
8587{
8588 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008589
8590 if (tctx->last == file)
8591 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008592 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008593 if (file)
8594 fput(file);
8595}
8596
8597static void __io_uring_attempt_task_drop(struct file *file)
8598{
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008599 struct file *old = xa_load(&current->io_uring->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008600
8601 if (old == file)
8602 io_uring_del_task_file(file);
8603}
8604
8605/*
8606 * Drop task note for this file if we're the only ones that hold it after
8607 * pending fput()
8608 */
8609static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8610{
8611 if (!current->io_uring)
8612 return;
8613 /*
8614 * fput() is pending, will be 2 if the only other ref is our potential
8615 * task file note. If the task is exiting, drop regardless of count.
8616 */
8617 if (!exiting && atomic_long_read(&file->f_count) != 2)
8618 return;
8619
8620 __io_uring_attempt_task_drop(file);
8621}
8622
8623void __io_uring_files_cancel(struct files_struct *files)
8624{
8625 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008626 struct file *file;
8627 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008628
8629 /* make sure overflow events are dropped */
8630 tctx->in_idle = true;
8631
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008632 xa_for_each(&tctx->xa, index, file) {
8633 struct io_ring_ctx *ctx = file->private_data;
Jens Axboe0f212202020-09-13 13:09:39 -06008634
8635 io_uring_cancel_task_requests(ctx, files);
8636 if (files)
8637 io_uring_del_task_file(file);
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008638 }
Jens Axboe0f212202020-09-13 13:09:39 -06008639}
8640
8641static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8642{
8643 return atomic_long_read(&tctx->req_issue) ==
8644 atomic_long_read(&tctx->req_complete);
8645}
8646
8647/*
8648 * Find any io_uring fd that this task has registered or done IO on, and cancel
8649 * requests.
8650 */
8651void __io_uring_task_cancel(void)
8652{
8653 struct io_uring_task *tctx = current->io_uring;
8654 DEFINE_WAIT(wait);
8655 long completions;
8656
8657 /* make sure overflow events are dropped */
8658 tctx->in_idle = true;
8659
8660 while (!io_uring_task_idle(tctx)) {
8661 /* read completions before cancelations */
8662 completions = atomic_long_read(&tctx->req_complete);
8663 __io_uring_files_cancel(NULL);
8664
8665 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8666
8667 /*
8668 * If we've seen completions, retry. This avoids a race where
8669 * a completion comes in before we did prepare_to_wait().
8670 */
8671 if (completions != atomic_long_read(&tctx->req_complete))
8672 continue;
8673 if (io_uring_task_idle(tctx))
8674 break;
8675 schedule();
8676 }
8677
8678 finish_wait(&tctx->wait, &wait);
8679 tctx->in_idle = false;
8680}
8681
Jens Axboefcb323c2019-10-24 12:39:47 -06008682static int io_uring_flush(struct file *file, void *data)
8683{
8684 struct io_ring_ctx *ctx = file->private_data;
8685
Jens Axboe6ab23142020-02-08 20:23:59 -07008686 /*
8687 * If the task is going away, cancel work it may have pending
8688 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008689 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008690 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008691
Jens Axboe0f212202020-09-13 13:09:39 -06008692 io_uring_cancel_task_requests(ctx, data);
8693 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008694 return 0;
8695}
8696
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008697static void *io_uring_validate_mmap_request(struct file *file,
8698 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008699{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008700 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008701 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008702 struct page *page;
8703 void *ptr;
8704
8705 switch (offset) {
8706 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008707 case IORING_OFF_CQ_RING:
8708 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008709 break;
8710 case IORING_OFF_SQES:
8711 ptr = ctx->sq_sqes;
8712 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008713 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008714 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008715 }
8716
8717 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008718 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008719 return ERR_PTR(-EINVAL);
8720
8721 return ptr;
8722}
8723
8724#ifdef CONFIG_MMU
8725
8726static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8727{
8728 size_t sz = vma->vm_end - vma->vm_start;
8729 unsigned long pfn;
8730 void *ptr;
8731
8732 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8733 if (IS_ERR(ptr))
8734 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008735
8736 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8737 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8738}
8739
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008740#else /* !CONFIG_MMU */
8741
8742static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8743{
8744 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8745}
8746
8747static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8748{
8749 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8750}
8751
8752static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8753 unsigned long addr, unsigned long len,
8754 unsigned long pgoff, unsigned long flags)
8755{
8756 void *ptr;
8757
8758 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8759 if (IS_ERR(ptr))
8760 return PTR_ERR(ptr);
8761
8762 return (unsigned long) ptr;
8763}
8764
8765#endif /* !CONFIG_MMU */
8766
Jens Axboe90554202020-09-03 12:12:41 -06008767static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8768{
8769 DEFINE_WAIT(wait);
8770
8771 do {
8772 if (!io_sqring_full(ctx))
8773 break;
8774
8775 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8776
8777 if (!io_sqring_full(ctx))
8778 break;
8779
8780 schedule();
8781 } while (!signal_pending(current));
8782
8783 finish_wait(&ctx->sqo_sq_wait, &wait);
8784}
8785
Jens Axboe2b188cc2019-01-07 10:46:33 -07008786SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8787 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8788 size_t, sigsz)
8789{
8790 struct io_ring_ctx *ctx;
8791 long ret = -EBADF;
8792 int submitted = 0;
8793 struct fd f;
8794
Jens Axboe4c6e2772020-07-01 11:29:10 -06008795 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008796
Jens Axboe90554202020-09-03 12:12:41 -06008797 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8798 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008799 return -EINVAL;
8800
8801 f = fdget(fd);
8802 if (!f.file)
8803 return -EBADF;
8804
8805 ret = -EOPNOTSUPP;
8806 if (f.file->f_op != &io_uring_fops)
8807 goto out_fput;
8808
8809 ret = -ENXIO;
8810 ctx = f.file->private_data;
8811 if (!percpu_ref_tryget(&ctx->refs))
8812 goto out_fput;
8813
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008814 ret = -EBADFD;
8815 if (ctx->flags & IORING_SETUP_R_DISABLED)
8816 goto out;
8817
Jens Axboe6c271ce2019-01-10 11:22:30 -07008818 /*
8819 * For SQ polling, the thread will do all submissions and completions.
8820 * Just return the requested submit count, and wake the thread if
8821 * we were asked to.
8822 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008823 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008824 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008825 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008826 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008827 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008828 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008829 if (flags & IORING_ENTER_SQ_WAIT)
8830 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008831 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008832 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008833 ret = io_uring_add_task_file(f.file);
8834 if (unlikely(ret))
8835 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008836 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008837 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008838 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008839
8840 if (submitted != to_submit)
8841 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008842 }
8843 if (flags & IORING_ENTER_GETEVENTS) {
8844 min_complete = min(min_complete, ctx->cq_entries);
8845
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008846 /*
8847 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8848 * space applications don't need to do io completion events
8849 * polling again, they can rely on io_sq_thread to do polling
8850 * work, which can reduce cpu usage and uring_lock contention.
8851 */
8852 if (ctx->flags & IORING_SETUP_IOPOLL &&
8853 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008854 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008855 } else {
8856 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8857 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008858 }
8859
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008860out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008861 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008862out_fput:
8863 fdput(f);
8864 return submitted ? submitted : ret;
8865}
8866
Tobias Klauserbebdb652020-02-26 18:38:32 +01008867#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008868static int io_uring_show_cred(int id, void *p, void *data)
8869{
8870 const struct cred *cred = p;
8871 struct seq_file *m = data;
8872 struct user_namespace *uns = seq_user_ns(m);
8873 struct group_info *gi;
8874 kernel_cap_t cap;
8875 unsigned __capi;
8876 int g;
8877
8878 seq_printf(m, "%5d\n", id);
8879 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8880 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8881 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8882 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8883 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8884 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8885 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8886 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8887 seq_puts(m, "\n\tGroups:\t");
8888 gi = cred->group_info;
8889 for (g = 0; g < gi->ngroups; g++) {
8890 seq_put_decimal_ull(m, g ? " " : "",
8891 from_kgid_munged(uns, gi->gid[g]));
8892 }
8893 seq_puts(m, "\n\tCapEff:\t");
8894 cap = cred->cap_effective;
8895 CAP_FOR_EACH_U32(__capi)
8896 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8897 seq_putc(m, '\n');
8898 return 0;
8899}
8900
8901static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8902{
Joseph Qidbbe9c62020-09-29 09:01:22 -06008903 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06008904 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008905 int i;
8906
Jens Axboefad8e0d2020-09-28 08:57:48 -06008907 /*
8908 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8909 * since fdinfo case grabs it in the opposite direction of normal use
8910 * cases. If we fail to get the lock, we just don't iterate any
8911 * structures that could be going away outside the io_uring mutex.
8912 */
8913 has_lock = mutex_trylock(&ctx->uring_lock);
8914
Joseph Qidbbe9c62020-09-29 09:01:22 -06008915 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
8916 sq = ctx->sq_data;
8917
8918 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
8919 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07008920 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008921 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008922 struct fixed_file_table *table;
8923 struct file *f;
8924
8925 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8926 f = table->files[i & IORING_FILE_TABLE_MASK];
8927 if (f)
8928 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8929 else
8930 seq_printf(m, "%5u: <none>\n", i);
8931 }
8932 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008933 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008934 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8935
8936 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8937 (unsigned int) buf->len);
8938 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008939 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008940 seq_printf(m, "Personalities:\n");
8941 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8942 }
Jens Axboed7718a92020-02-14 22:23:12 -07008943 seq_printf(m, "PollList:\n");
8944 spin_lock_irq(&ctx->completion_lock);
8945 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8946 struct hlist_head *list = &ctx->cancel_hash[i];
8947 struct io_kiocb *req;
8948
8949 hlist_for_each_entry(req, list, hash_node)
8950 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8951 req->task->task_works != NULL);
8952 }
8953 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008954 if (has_lock)
8955 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008956}
8957
8958static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8959{
8960 struct io_ring_ctx *ctx = f->private_data;
8961
8962 if (percpu_ref_tryget(&ctx->refs)) {
8963 __io_uring_show_fdinfo(ctx, m);
8964 percpu_ref_put(&ctx->refs);
8965 }
8966}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008967#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008968
Jens Axboe2b188cc2019-01-07 10:46:33 -07008969static const struct file_operations io_uring_fops = {
8970 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008971 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008972 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008973#ifndef CONFIG_MMU
8974 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8975 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8976#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008977 .poll = io_uring_poll,
8978 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008979#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008980 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008981#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008982};
8983
8984static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8985 struct io_uring_params *p)
8986{
Hristo Venev75b28af2019-08-26 17:23:46 +00008987 struct io_rings *rings;
8988 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008989
Jens Axboebd740482020-08-05 12:58:23 -06008990 /* make sure these are sane, as we already accounted them */
8991 ctx->sq_entries = p->sq_entries;
8992 ctx->cq_entries = p->cq_entries;
8993
Hristo Venev75b28af2019-08-26 17:23:46 +00008994 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8995 if (size == SIZE_MAX)
8996 return -EOVERFLOW;
8997
8998 rings = io_mem_alloc(size);
8999 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009000 return -ENOMEM;
9001
Hristo Venev75b28af2019-08-26 17:23:46 +00009002 ctx->rings = rings;
9003 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9004 rings->sq_ring_mask = p->sq_entries - 1;
9005 rings->cq_ring_mask = p->cq_entries - 1;
9006 rings->sq_ring_entries = p->sq_entries;
9007 rings->cq_ring_entries = p->cq_entries;
9008 ctx->sq_mask = rings->sq_ring_mask;
9009 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009010
9011 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009012 if (size == SIZE_MAX) {
9013 io_mem_free(ctx->rings);
9014 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009015 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009016 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009017
9018 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009019 if (!ctx->sq_sqes) {
9020 io_mem_free(ctx->rings);
9021 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009022 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009023 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009024
Jens Axboe2b188cc2019-01-07 10:46:33 -07009025 return 0;
9026}
9027
9028/*
9029 * Allocate an anonymous fd, this is what constitutes the application
9030 * visible backing of an io_uring instance. The application mmaps this
9031 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9032 * we have to tie this fd to a socket for file garbage collection purposes.
9033 */
9034static int io_uring_get_fd(struct io_ring_ctx *ctx)
9035{
9036 struct file *file;
9037 int ret;
9038
9039#if defined(CONFIG_UNIX)
9040 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9041 &ctx->ring_sock);
9042 if (ret)
9043 return ret;
9044#endif
9045
9046 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9047 if (ret < 0)
9048 goto err;
9049
9050 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9051 O_RDWR | O_CLOEXEC);
9052 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009053err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009054 put_unused_fd(ret);
9055 ret = PTR_ERR(file);
9056 goto err;
9057 }
9058
9059#if defined(CONFIG_UNIX)
9060 ctx->ring_sock->file = file;
9061#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009062 if (unlikely(io_uring_add_task_file(file))) {
9063 file = ERR_PTR(-ENOMEM);
9064 goto err_fd;
9065 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009066 fd_install(ret, file);
9067 return ret;
9068err:
9069#if defined(CONFIG_UNIX)
9070 sock_release(ctx->ring_sock);
9071 ctx->ring_sock = NULL;
9072#endif
9073 return ret;
9074}
9075
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009076static int io_uring_create(unsigned entries, struct io_uring_params *p,
9077 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009078{
9079 struct user_struct *user = NULL;
9080 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009081 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009082 int ret;
9083
Jens Axboe8110c1a2019-12-28 15:39:54 -07009084 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009085 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009086 if (entries > IORING_MAX_ENTRIES) {
9087 if (!(p->flags & IORING_SETUP_CLAMP))
9088 return -EINVAL;
9089 entries = IORING_MAX_ENTRIES;
9090 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009091
9092 /*
9093 * Use twice as many entries for the CQ ring. It's possible for the
9094 * application to drive a higher depth than the size of the SQ ring,
9095 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009096 * some flexibility in overcommitting a bit. If the application has
9097 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9098 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009099 */
9100 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009101 if (p->flags & IORING_SETUP_CQSIZE) {
9102 /*
9103 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9104 * to a power-of-two, if it isn't already. We do NOT impose
9105 * any cq vs sq ring sizing.
9106 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009107 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009108 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009109 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9110 if (!(p->flags & IORING_SETUP_CLAMP))
9111 return -EINVAL;
9112 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9113 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009114 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9115 } else {
9116 p->cq_entries = 2 * p->sq_entries;
9117 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009118
9119 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009120 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009121
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009122 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009123 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009124 ring_pages(p->sq_entries, p->cq_entries));
9125 if (ret) {
9126 free_uid(user);
9127 return ret;
9128 }
9129 }
9130
9131 ctx = io_ring_ctx_alloc(p);
9132 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009133 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009134 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009135 p->cq_entries));
9136 free_uid(user);
9137 return -ENOMEM;
9138 }
9139 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009140 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009141 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009142
Jens Axboe2aede0e2020-09-14 10:45:53 -06009143 ctx->sqo_task = get_task_struct(current);
9144
9145 /*
9146 * This is just grabbed for accounting purposes. When a process exits,
9147 * the mm is exited and dropped before the files, hence we need to hang
9148 * on to this mm purely for the purposes of being able to unaccount
9149 * memory (locked/pinned vm). It's not used for anything else.
9150 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009151 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009152 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009153
Dennis Zhou91d8f512020-09-16 13:41:05 -07009154#ifdef CONFIG_BLK_CGROUP
9155 /*
9156 * The sq thread will belong to the original cgroup it was inited in.
9157 * If the cgroup goes offline (e.g. disabling the io controller), then
9158 * issued bios will be associated with the closest cgroup later in the
9159 * block layer.
9160 */
9161 rcu_read_lock();
9162 ctx->sqo_blkcg_css = blkcg_css();
9163 ret = css_tryget_online(ctx->sqo_blkcg_css);
9164 rcu_read_unlock();
9165 if (!ret) {
9166 /* don't init against a dying cgroup, have the user try again */
9167 ctx->sqo_blkcg_css = NULL;
9168 ret = -ENODEV;
9169 goto err;
9170 }
9171#endif
9172
Jens Axboef74441e2020-08-05 13:00:44 -06009173 /*
9174 * Account memory _before_ installing the file descriptor. Once
9175 * the descriptor is installed, it can get closed at any time. Also
9176 * do this before hitting the general error path, as ring freeing
9177 * will un-account as well.
9178 */
9179 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9180 ACCT_LOCKED);
9181 ctx->limit_mem = limit_mem;
9182
Jens Axboe2b188cc2019-01-07 10:46:33 -07009183 ret = io_allocate_scq_urings(ctx, p);
9184 if (ret)
9185 goto err;
9186
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009187 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009188 if (ret)
9189 goto err;
9190
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009191 if (!(p->flags & IORING_SETUP_R_DISABLED))
9192 io_sq_offload_start(ctx);
9193
Jens Axboe2b188cc2019-01-07 10:46:33 -07009194 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009195 p->sq_off.head = offsetof(struct io_rings, sq.head);
9196 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9197 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9198 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9199 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9200 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9201 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009202
9203 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009204 p->cq_off.head = offsetof(struct io_rings, cq.head);
9205 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9206 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9207 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9208 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9209 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009210 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009211
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009212 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9213 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009214 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9215 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009216
9217 if (copy_to_user(params, p, sizeof(*p))) {
9218 ret = -EFAULT;
9219 goto err;
9220 }
Jens Axboed1719f72020-07-30 13:43:53 -06009221
9222 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009223 * Install ring fd as the very last thing, so we don't risk someone
9224 * having closed it before we finish setup
9225 */
9226 ret = io_uring_get_fd(ctx);
9227 if (ret < 0)
9228 goto err;
9229
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009230 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009231 return ret;
9232err:
9233 io_ring_ctx_wait_and_kill(ctx);
9234 return ret;
9235}
9236
9237/*
9238 * Sets up an aio uring context, and returns the fd. Applications asks for a
9239 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9240 * params structure passed in.
9241 */
9242static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9243{
9244 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009245 int i;
9246
9247 if (copy_from_user(&p, params, sizeof(p)))
9248 return -EFAULT;
9249 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9250 if (p.resv[i])
9251 return -EINVAL;
9252 }
9253
Jens Axboe6c271ce2019-01-10 11:22:30 -07009254 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009255 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009256 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9257 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009258 return -EINVAL;
9259
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009260 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009261}
9262
9263SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9264 struct io_uring_params __user *, params)
9265{
9266 return io_uring_setup(entries, params);
9267}
9268
Jens Axboe66f4af92020-01-16 15:36:52 -07009269static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9270{
9271 struct io_uring_probe *p;
9272 size_t size;
9273 int i, ret;
9274
9275 size = struct_size(p, ops, nr_args);
9276 if (size == SIZE_MAX)
9277 return -EOVERFLOW;
9278 p = kzalloc(size, GFP_KERNEL);
9279 if (!p)
9280 return -ENOMEM;
9281
9282 ret = -EFAULT;
9283 if (copy_from_user(p, arg, size))
9284 goto out;
9285 ret = -EINVAL;
9286 if (memchr_inv(p, 0, size))
9287 goto out;
9288
9289 p->last_op = IORING_OP_LAST - 1;
9290 if (nr_args > IORING_OP_LAST)
9291 nr_args = IORING_OP_LAST;
9292
9293 for (i = 0; i < nr_args; i++) {
9294 p->ops[i].op = i;
9295 if (!io_op_defs[i].not_supported)
9296 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9297 }
9298 p->ops_len = i;
9299
9300 ret = 0;
9301 if (copy_to_user(arg, p, size))
9302 ret = -EFAULT;
9303out:
9304 kfree(p);
9305 return ret;
9306}
9307
Jens Axboe071698e2020-01-28 10:04:42 -07009308static int io_register_personality(struct io_ring_ctx *ctx)
9309{
9310 const struct cred *creds = get_current_cred();
9311 int id;
9312
9313 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9314 USHRT_MAX, GFP_KERNEL);
9315 if (id < 0)
9316 put_cred(creds);
9317 return id;
9318}
9319
9320static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9321{
9322 const struct cred *old_creds;
9323
9324 old_creds = idr_remove(&ctx->personality_idr, id);
9325 if (old_creds) {
9326 put_cred(old_creds);
9327 return 0;
9328 }
9329
9330 return -EINVAL;
9331}
9332
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009333static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9334 unsigned int nr_args)
9335{
9336 struct io_uring_restriction *res;
9337 size_t size;
9338 int i, ret;
9339
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009340 /* Restrictions allowed only if rings started disabled */
9341 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9342 return -EBADFD;
9343
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009344 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009345 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009346 return -EBUSY;
9347
9348 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9349 return -EINVAL;
9350
9351 size = array_size(nr_args, sizeof(*res));
9352 if (size == SIZE_MAX)
9353 return -EOVERFLOW;
9354
9355 res = memdup_user(arg, size);
9356 if (IS_ERR(res))
9357 return PTR_ERR(res);
9358
9359 ret = 0;
9360
9361 for (i = 0; i < nr_args; i++) {
9362 switch (res[i].opcode) {
9363 case IORING_RESTRICTION_REGISTER_OP:
9364 if (res[i].register_op >= IORING_REGISTER_LAST) {
9365 ret = -EINVAL;
9366 goto out;
9367 }
9368
9369 __set_bit(res[i].register_op,
9370 ctx->restrictions.register_op);
9371 break;
9372 case IORING_RESTRICTION_SQE_OP:
9373 if (res[i].sqe_op >= IORING_OP_LAST) {
9374 ret = -EINVAL;
9375 goto out;
9376 }
9377
9378 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9379 break;
9380 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9381 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9382 break;
9383 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9384 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9385 break;
9386 default:
9387 ret = -EINVAL;
9388 goto out;
9389 }
9390 }
9391
9392out:
9393 /* Reset all restrictions if an error happened */
9394 if (ret != 0)
9395 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9396 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009397 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009398
9399 kfree(res);
9400 return ret;
9401}
9402
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009403static int io_register_enable_rings(struct io_ring_ctx *ctx)
9404{
9405 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9406 return -EBADFD;
9407
9408 if (ctx->restrictions.registered)
9409 ctx->restricted = 1;
9410
9411 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9412
9413 io_sq_offload_start(ctx);
9414
9415 return 0;
9416}
9417
Jens Axboe071698e2020-01-28 10:04:42 -07009418static bool io_register_op_must_quiesce(int op)
9419{
9420 switch (op) {
9421 case IORING_UNREGISTER_FILES:
9422 case IORING_REGISTER_FILES_UPDATE:
9423 case IORING_REGISTER_PROBE:
9424 case IORING_REGISTER_PERSONALITY:
9425 case IORING_UNREGISTER_PERSONALITY:
9426 return false;
9427 default:
9428 return true;
9429 }
9430}
9431
Jens Axboeedafcce2019-01-09 09:16:05 -07009432static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9433 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009434 __releases(ctx->uring_lock)
9435 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009436{
9437 int ret;
9438
Jens Axboe35fa71a2019-04-22 10:23:23 -06009439 /*
9440 * We're inside the ring mutex, if the ref is already dying, then
9441 * someone else killed the ctx or is already going through
9442 * io_uring_register().
9443 */
9444 if (percpu_ref_is_dying(&ctx->refs))
9445 return -ENXIO;
9446
Jens Axboe071698e2020-01-28 10:04:42 -07009447 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009448 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009449
Jens Axboe05f3fb32019-12-09 11:22:50 -07009450 /*
9451 * Drop uring mutex before waiting for references to exit. If
9452 * another thread is currently inside io_uring_enter() it might
9453 * need to grab the uring_lock to make progress. If we hold it
9454 * here across the drain wait, then we can deadlock. It's safe
9455 * to drop the mutex here, since no new references will come in
9456 * after we've killed the percpu ref.
9457 */
9458 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009459 do {
9460 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9461 if (!ret)
9462 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009463 ret = io_run_task_work_sig();
9464 if (ret < 0)
9465 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009466 } while (1);
9467
Jens Axboe05f3fb32019-12-09 11:22:50 -07009468 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009469
Jens Axboec1503682020-01-08 08:26:07 -07009470 if (ret) {
9471 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009472 goto out_quiesce;
9473 }
9474 }
9475
9476 if (ctx->restricted) {
9477 if (opcode >= IORING_REGISTER_LAST) {
9478 ret = -EINVAL;
9479 goto out;
9480 }
9481
9482 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9483 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009484 goto out;
9485 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009486 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009487
9488 switch (opcode) {
9489 case IORING_REGISTER_BUFFERS:
9490 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9491 break;
9492 case IORING_UNREGISTER_BUFFERS:
9493 ret = -EINVAL;
9494 if (arg || nr_args)
9495 break;
9496 ret = io_sqe_buffer_unregister(ctx);
9497 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009498 case IORING_REGISTER_FILES:
9499 ret = io_sqe_files_register(ctx, arg, nr_args);
9500 break;
9501 case IORING_UNREGISTER_FILES:
9502 ret = -EINVAL;
9503 if (arg || nr_args)
9504 break;
9505 ret = io_sqe_files_unregister(ctx);
9506 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009507 case IORING_REGISTER_FILES_UPDATE:
9508 ret = io_sqe_files_update(ctx, arg, nr_args);
9509 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009510 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009511 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009512 ret = -EINVAL;
9513 if (nr_args != 1)
9514 break;
9515 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009516 if (ret)
9517 break;
9518 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9519 ctx->eventfd_async = 1;
9520 else
9521 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009522 break;
9523 case IORING_UNREGISTER_EVENTFD:
9524 ret = -EINVAL;
9525 if (arg || nr_args)
9526 break;
9527 ret = io_eventfd_unregister(ctx);
9528 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009529 case IORING_REGISTER_PROBE:
9530 ret = -EINVAL;
9531 if (!arg || nr_args > 256)
9532 break;
9533 ret = io_probe(ctx, arg, nr_args);
9534 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009535 case IORING_REGISTER_PERSONALITY:
9536 ret = -EINVAL;
9537 if (arg || nr_args)
9538 break;
9539 ret = io_register_personality(ctx);
9540 break;
9541 case IORING_UNREGISTER_PERSONALITY:
9542 ret = -EINVAL;
9543 if (arg)
9544 break;
9545 ret = io_unregister_personality(ctx, nr_args);
9546 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009547 case IORING_REGISTER_ENABLE_RINGS:
9548 ret = -EINVAL;
9549 if (arg || nr_args)
9550 break;
9551 ret = io_register_enable_rings(ctx);
9552 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009553 case IORING_REGISTER_RESTRICTIONS:
9554 ret = io_register_restrictions(ctx, arg, nr_args);
9555 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009556 default:
9557 ret = -EINVAL;
9558 break;
9559 }
9560
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009561out:
Jens Axboe071698e2020-01-28 10:04:42 -07009562 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009563 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009564 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009565out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009566 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009567 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009568 return ret;
9569}
9570
9571SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9572 void __user *, arg, unsigned int, nr_args)
9573{
9574 struct io_ring_ctx *ctx;
9575 long ret = -EBADF;
9576 struct fd f;
9577
9578 f = fdget(fd);
9579 if (!f.file)
9580 return -EBADF;
9581
9582 ret = -EOPNOTSUPP;
9583 if (f.file->f_op != &io_uring_fops)
9584 goto out_fput;
9585
9586 ctx = f.file->private_data;
9587
9588 mutex_lock(&ctx->uring_lock);
9589 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9590 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009591 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9592 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009593out_fput:
9594 fdput(f);
9595 return ret;
9596}
9597
Jens Axboe2b188cc2019-01-07 10:46:33 -07009598static int __init io_uring_init(void)
9599{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009600#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9601 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9602 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9603} while (0)
9604
9605#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9606 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9607 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9608 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9609 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9610 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9611 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9612 BUILD_BUG_SQE_ELEM(8, __u64, off);
9613 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9614 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009615 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009616 BUILD_BUG_SQE_ELEM(24, __u32, len);
9617 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9618 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9619 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9620 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009621 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9622 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009623 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9624 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9625 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9626 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9627 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9628 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9629 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9630 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009631 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009632 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9633 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9634 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009635 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009636
Jens Axboed3656342019-12-18 09:50:26 -07009637 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009638 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009639 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9640 return 0;
9641};
9642__initcall(io_uring_init);