blob: 372be9caf3402f79ac1e5c2509d370c8bf7ea2c0 [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 Axboe4ea33a92020-10-15 13:46:44 -060084#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020086#define CREATE_TRACE_POINTS
87#include <trace/events/io_uring.h>
88
Jens Axboe2b188cc2019-01-07 10:46:33 -070089#include <uapi/linux/io_uring.h>
90
91#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060092#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070093
Daniel Xu5277dea2019-09-14 14:23:45 -070094#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060095#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060096
97/*
98 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
99 */
100#define IORING_FILE_TABLE_SHIFT 9
101#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
102#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
103#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200104#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
105 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700106
107struct io_uring {
108 u32 head ____cacheline_aligned_in_smp;
109 u32 tail ____cacheline_aligned_in_smp;
110};
111
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * This data is shared with the application through the mmap at offsets
114 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 *
116 * The offsets to the member fields are published through struct
117 * io_sqring_offsets when calling io_uring_setup.
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 /*
121 * Head and tail offsets into the ring; the offsets need to be
122 * masked to get valid indices.
123 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * The kernel controls head of the sq ring and the tail of the cq ring,
125 * and the application controls tail of the sq ring and the head of the
126 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000130 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 * ring_entries - 1)
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_ring_mask, cq_ring_mask;
134 /* Ring sizes (constant, power of 2) */
135 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 /*
137 * Number of invalid entries dropped by the kernel due to
138 * invalid index stored in array
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application (i.e. get number of "new events" by comparing to
142 * cached value).
143 *
144 * After a new SQ head value was read by the application this
145 * counter includes all submissions that were dropped reaching
146 * the new SQ head (and possibly more).
147 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000148 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200150 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 *
152 * Written by the kernel, shouldn't be modified by the
153 * application.
154 *
155 * The application needs a full memory barrier before checking
156 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200160 * Runtime CQ flags
161 *
162 * Written by the application, shouldn't be modified by the
163 * kernel.
164 */
165 u32 cq_flags;
166 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 * Number of completion events lost because the queue was full;
168 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800169 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200170 * the completion queue.
171 *
172 * Written by the kernel, shouldn't be modified by the
173 * application (i.e. get number of "new events" by comparing to
174 * cached value).
175 *
176 * As completion events come in out of order this counter is not
177 * ordered with any other data.
178 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000179 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200180 /*
181 * Ring buffer of completion events.
182 *
183 * The kernel writes completion events fresh every time they are
184 * produced, so the application is allowed to modify pending
185 * entries.
186 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000187 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700188};
189
Jens Axboeedafcce2019-01-09 09:16:05 -0700190struct io_mapped_ubuf {
191 u64 ubuf;
192 size_t len;
193 struct bio_vec *bvec;
194 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600195 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700196};
197
Jens Axboe65e19f52019-10-26 07:20:21 -0600198struct fixed_file_table {
199 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700200};
201
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202struct fixed_file_ref_node {
203 struct percpu_ref refs;
204 struct list_head node;
205 struct list_head file_list;
206 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600207 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000208 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800209};
210
Jens Axboe05f3fb32019-12-09 11:22:50 -0700211struct fixed_file_data {
212 struct fixed_file_table *table;
213 struct io_ring_ctx *ctx;
214
Pavel Begunkovb2e96852020-10-10 18:34:16 +0100215 struct fixed_file_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700217 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800218 struct list_head ref_list;
219 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700220};
221
Jens Axboe5a2e7452020-02-23 16:23:11 -0700222struct io_buffer {
223 struct list_head list;
224 __u64 addr;
225 __s32 len;
226 __u16 bid;
227};
228
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200229struct io_restriction {
230 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
231 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
232 u8 sqe_flags_allowed;
233 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200234 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200235};
236
Jens Axboe534ca6d2020-09-02 13:52:19 -0600237struct io_sq_data {
238 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600239 struct mutex lock;
240
241 /* ctx's that are using this sqd */
242 struct list_head ctx_list;
243 struct list_head ctx_new_list;
244 struct mutex ctx_lock;
245
Jens Axboe534ca6d2020-09-02 13:52:19 -0600246 struct task_struct *thread;
247 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800248
249 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600250};
251
Jens Axboe2b188cc2019-01-07 10:46:33 -0700252struct io_ring_ctx {
253 struct {
254 struct percpu_ref refs;
255 } ____cacheline_aligned_in_smp;
256
257 struct {
258 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800259 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700260 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800261 unsigned int cq_overflow_flushed: 1;
262 unsigned int drain_next: 1;
263 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200264 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000265 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700266
Hristo Venev75b28af2019-08-26 17:23:46 +0000267 /*
268 * Ring buffer of indices into array of io_uring_sqe, which is
269 * mmapped by the application using the IORING_OFF_SQES offset.
270 *
271 * This indirection could e.g. be used to assign fixed
272 * io_uring_sqe entries to operations and only submit them to
273 * the queue when needed.
274 *
275 * The kernel modifies neither the indices array nor the entries
276 * array.
277 */
278 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279 unsigned cached_sq_head;
280 unsigned sq_entries;
281 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700282 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600283 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100284 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700285 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600286
287 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600288 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700289 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290
Jens Axboead3eb2c2019-12-18 17:12:20 -0700291 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292 } ____cacheline_aligned_in_smp;
293
Hristo Venev75b28af2019-08-26 17:23:46 +0000294 struct io_rings *rings;
295
Jens Axboe2b188cc2019-01-07 10:46:33 -0700296 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600297 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600298
299 /*
300 * For SQPOLL usage - we hold a reference to the parent task, so we
301 * have access to the ->files
302 */
303 struct task_struct *sqo_task;
304
305 /* Only used for accounting purposes */
306 struct mm_struct *mm_account;
307
Dennis Zhou91d8f512020-09-16 13:41:05 -0700308#ifdef CONFIG_BLK_CGROUP
309 struct cgroup_subsys_state *sqo_blkcg_css;
310#endif
311
Jens Axboe534ca6d2020-09-02 13:52:19 -0600312 struct io_sq_data *sq_data; /* if using sq thread polling */
313
Jens Axboe90554202020-09-03 12:12:41 -0600314 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600315 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700316
Jens Axboe6b063142019-01-10 22:13:58 -0700317 /*
318 * If used, fixed file set. Writers must ensure that ->refs is dead,
319 * readers must ensure that ->refs is alive as long as the file* is
320 * used. Only updated through io_uring_register(2).
321 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700322 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700323 unsigned nr_user_files;
324
Jens Axboeedafcce2019-01-09 09:16:05 -0700325 /* if used, fixed mapped user buffers */
326 unsigned nr_user_bufs;
327 struct io_mapped_ubuf *user_bufs;
328
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329 struct user_struct *user;
330
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700331 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700332
Jens Axboe4ea33a92020-10-15 13:46:44 -0600333#ifdef CONFIG_AUDIT
334 kuid_t loginuid;
335 unsigned int sessionid;
336#endif
337
Jens Axboe0f158b42020-05-14 17:18:39 -0600338 struct completion ref_comp;
339 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700340
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700341 /* if all else fails... */
342 struct io_kiocb *fallback_req;
343
Jens Axboe206aefd2019-11-07 18:27:42 -0700344#if defined(CONFIG_UNIX)
345 struct socket *ring_sock;
346#endif
347
Jens Axboe5a2e7452020-02-23 16:23:11 -0700348 struct idr io_buffer_idr;
349
Jens Axboe071698e2020-01-28 10:04:42 -0700350 struct idr personality_idr;
351
Jens Axboe206aefd2019-11-07 18:27:42 -0700352 struct {
353 unsigned cached_cq_tail;
354 unsigned cq_entries;
355 unsigned cq_mask;
356 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700357 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700358 struct wait_queue_head cq_wait;
359 struct fasync_struct *cq_fasync;
360 struct eventfd_ctx *cq_ev_fd;
361 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362
363 struct {
364 struct mutex uring_lock;
365 wait_queue_head_t wait;
366 } ____cacheline_aligned_in_smp;
367
368 struct {
369 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700370
Jens Axboedef596e2019-01-09 08:59:42 -0700371 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300372 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700373 * io_uring instances that don't use IORING_SETUP_SQPOLL.
374 * For SQPOLL, only the single threaded io_sq_thread() will
375 * manipulate the list, hence no extra locking is needed there.
376 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300377 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700378 struct hlist_head *cancel_hash;
379 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700380 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600381
382 spinlock_t inflight_lock;
383 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700384 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600385
Jens Axboe4a38aed22020-05-14 17:21:15 -0600386 struct delayed_work file_put_work;
387 struct llist_head file_put_llist;
388
Jens Axboe85faa7b2020-04-09 18:14:00 -0600389 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200390 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700391};
392
Jens Axboe09bb8392019-03-13 12:39:28 -0600393/*
394 * First field must be the file pointer in all the
395 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
396 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700397struct io_poll_iocb {
398 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000399 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700400 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600401 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700402 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700403 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700404};
405
Pavel Begunkov018043b2020-10-27 23:17:18 +0000406struct io_poll_remove {
407 struct file *file;
408 u64 addr;
409};
410
Jens Axboeb5dba592019-12-11 14:02:38 -0700411struct io_close {
412 struct file *file;
413 struct file *put_file;
414 int fd;
415};
416
Jens Axboead8a48a2019-11-15 08:49:11 -0700417struct io_timeout_data {
418 struct io_kiocb *req;
419 struct hrtimer timer;
420 struct timespec64 ts;
421 enum hrtimer_mode mode;
422};
423
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700424struct io_accept {
425 struct file *file;
426 struct sockaddr __user *addr;
427 int __user *addr_len;
428 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600429 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700430};
431
432struct io_sync {
433 struct file *file;
434 loff_t len;
435 loff_t off;
436 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700437 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700438};
439
Jens Axboefbf23842019-12-17 18:45:56 -0700440struct io_cancel {
441 struct file *file;
442 u64 addr;
443};
444
Jens Axboeb29472e2019-12-17 18:50:29 -0700445struct io_timeout {
446 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300447 u32 off;
448 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300449 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000450 /* head of the link, used by linked timeouts only */
451 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700452};
453
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100454struct io_timeout_rem {
455 struct file *file;
456 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000457
458 /* timeout update */
459 struct timespec64 ts;
460 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100461};
462
Jens Axboe9adbd452019-12-20 08:45:55 -0700463struct io_rw {
464 /* NOTE: kiocb has the file as the first member, so don't do it here */
465 struct kiocb kiocb;
466 u64 addr;
467 u64 len;
468};
469
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700470struct io_connect {
471 struct file *file;
472 struct sockaddr __user *addr;
473 int addr_len;
474};
475
Jens Axboee47293f2019-12-20 08:58:21 -0700476struct io_sr_msg {
477 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700478 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300479 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700480 void __user *buf;
481 };
Jens Axboee47293f2019-12-20 08:58:21 -0700482 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700483 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700484 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700485 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700486};
487
Jens Axboe15b71ab2019-12-11 11:20:36 -0700488struct io_open {
489 struct file *file;
490 int dfd;
Jens Axboe944d1442020-11-13 16:48:44 -0700491 bool ignore_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700492 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700493 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600494 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700495};
496
Jens Axboe05f3fb32019-12-09 11:22:50 -0700497struct io_files_update {
498 struct file *file;
499 u64 arg;
500 u32 nr_args;
501 u32 offset;
502};
503
Jens Axboe4840e412019-12-25 22:03:45 -0700504struct io_fadvise {
505 struct file *file;
506 u64 offset;
507 u32 len;
508 u32 advice;
509};
510
Jens Axboec1ca7572019-12-25 22:18:28 -0700511struct io_madvise {
512 struct file *file;
513 u64 addr;
514 u32 len;
515 u32 advice;
516};
517
Jens Axboe3e4827b2020-01-08 15:18:09 -0700518struct io_epoll {
519 struct file *file;
520 int epfd;
521 int op;
522 int fd;
523 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700524};
525
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300526struct io_splice {
527 struct file *file_out;
528 struct file *file_in;
529 loff_t off_out;
530 loff_t off_in;
531 u64 len;
532 unsigned int flags;
533};
534
Jens Axboeddf0322d2020-02-23 16:41:33 -0700535struct io_provide_buf {
536 struct file *file;
537 __u64 addr;
538 __s32 len;
539 __u32 bgid;
540 __u16 nbufs;
541 __u16 bid;
542};
543
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700544struct io_statx {
545 struct file *file;
546 int dfd;
547 unsigned int mask;
548 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700549 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700550 struct statx __user *buffer;
551};
552
Jens Axboe36f4fa62020-09-05 11:14:22 -0600553struct io_shutdown {
554 struct file *file;
555 int how;
556};
557
Jens Axboe80a261f2020-09-28 14:23:58 -0600558struct io_rename {
559 struct file *file;
560 int old_dfd;
561 int new_dfd;
562 struct filename *oldpath;
563 struct filename *newpath;
564 int flags;
565};
566
Jens Axboe14a11432020-09-28 14:27:37 -0600567struct io_unlink {
568 struct file *file;
569 int dfd;
570 int flags;
571 struct filename *filename;
572};
573
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300574struct io_completion {
575 struct file *file;
576 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300577 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300578};
579
Jens Axboef499a022019-12-02 16:28:46 -0700580struct io_async_connect {
581 struct sockaddr_storage address;
582};
583
Jens Axboe03b12302019-12-02 18:50:25 -0700584struct io_async_msghdr {
585 struct iovec fast_iov[UIO_FASTIOV];
586 struct iovec *iov;
587 struct sockaddr __user *uaddr;
588 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700589 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700590};
591
Jens Axboef67676d2019-12-02 11:03:47 -0700592struct io_async_rw {
593 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600594 const struct iovec *free_iovec;
595 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600596 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600597 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700598};
599
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300600enum {
601 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
602 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
603 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
604 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
605 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700606 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300607
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300608 REQ_F_FAIL_LINK_BIT,
609 REQ_F_INFLIGHT_BIT,
610 REQ_F_CUR_POS_BIT,
611 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300612 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300613 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300614 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700615 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700616 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600617 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800618 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100619 REQ_F_LTIMEOUT_ACTIVE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700620
621 /* not a real bit, just to check we're not overflowing the space */
622 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300623};
624
625enum {
626 /* ctx owns file */
627 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
628 /* drain existing IO first */
629 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
630 /* linked sqes */
631 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
632 /* doesn't sever on completion < 0 */
633 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
634 /* IOSQE_ASYNC */
635 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700636 /* IOSQE_BUFFER_SELECT */
637 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300638
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300639 /* fail rest of links */
640 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
641 /* on inflight list */
642 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
643 /* read/write uses file position */
644 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
645 /* must not punt to workers */
646 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100647 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300648 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300649 /* regular file */
650 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300651 /* needs cleanup */
652 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700653 /* already went through poll handler */
654 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700655 /* buffer already selected */
656 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600657 /* doesn't need file table for this request */
658 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800659 /* io_wq_work is initialized */
660 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100661 /* linked timeout is active, i.e. prepared by link's head */
662 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700663};
664
665struct async_poll {
666 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600667 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300668};
669
Jens Axboe09bb8392019-03-13 12:39:28 -0600670/*
671 * NOTE! Each of the iocb union members has the file pointer
672 * as the first entry in their struct definition. So you can
673 * access the file pointer through any of the sub-structs,
674 * or directly as just 'ki_filp' in this struct.
675 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700676struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700677 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600678 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700679 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700680 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000681 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700682 struct io_accept accept;
683 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700684 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700685 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100686 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700687 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700688 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700689 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700690 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700691 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700692 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700693 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700694 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300695 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700696 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700697 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600698 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600699 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600700 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300701 /* use only after cleaning per-op data, see io_clean_op() */
702 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700703 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700704
Jens Axboee8c2bc12020-08-15 18:44:09 -0700705 /* opcode allocated if it needs to store data for async defer */
706 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700707 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800708 /* polled IO has completed */
709 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700710
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700711 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300712 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700713
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300714 struct io_ring_ctx *ctx;
715 unsigned int flags;
716 refcount_t refs;
717 struct task_struct *task;
718 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000720 struct io_kiocb *link;
Pavel Begunkov04157672020-10-27 23:25:38 +0000721 struct percpu_ref *fixed_file_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700722
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300723 /*
724 * 1. used with ctx->iopoll_list with reads/writes
725 * 2. to track reqs with ->files (see io_op_def::file_table)
726 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300727 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300728 struct callback_head task_work;
729 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
730 struct hlist_node hash_node;
731 struct async_poll *apoll;
732 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700733};
734
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300735struct io_defer_entry {
736 struct list_head list;
737 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300738 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300739};
740
Jens Axboedef596e2019-01-09 08:59:42 -0700741#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700742
Jens Axboe013538b2020-06-22 09:29:15 -0600743struct io_comp_state {
744 unsigned int nr;
745 struct list_head list;
746 struct io_ring_ctx *ctx;
747};
748
Jens Axboe9a56a232019-01-09 09:06:50 -0700749struct io_submit_state {
750 struct blk_plug plug;
751
752 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700753 * io_kiocb alloc cache
754 */
755 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300756 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700757
Jens Axboe27926b62020-10-28 09:33:23 -0600758 bool plug_started;
759
Jens Axboe2579f912019-01-09 09:10:43 -0700760 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600761 * Batch completion logic
762 */
763 struct io_comp_state comp;
764
765 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700766 * File reference cache
767 */
768 struct file *file;
769 unsigned int fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +0000770 unsigned int file_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700771 unsigned int ios_left;
772};
773
Jens Axboed3656342019-12-18 09:50:26 -0700774struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700775 /* needs req->file assigned */
776 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600777 /* don't fail if file grab fails */
778 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700779 /* hash wq insertion if file is a regular file */
780 unsigned hash_reg_file : 1;
781 /* unbound wq insertion if file is a non-regular file */
782 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700783 /* opcode is not supported by this kernel */
784 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700785 /* set if opcode supports polled "wait" */
786 unsigned pollin : 1;
787 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700788 /* op supports buffer selection */
789 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700790 /* must always have async data allocated */
791 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600792 /* should block plug */
793 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700794 /* size of async data needed, if any */
795 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600796 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700797};
798
Jens Axboe09186822020-10-13 15:01:40 -0600799static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300800 [IORING_OP_NOP] = {},
801 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700802 .needs_file = 1,
803 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700804 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700805 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700806 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600807 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700808 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600809 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700812 .needs_file = 1,
813 .hash_reg_file = 1,
814 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700815 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700816 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600817 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700818 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600819 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
820 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700823 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600824 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700825 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300826 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700827 .needs_file = 1,
828 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700829 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600830 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700831 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600832 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700833 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700835 .needs_file = 1,
836 .hash_reg_file = 1,
837 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700838 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600839 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700840 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600841 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
842 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700845 .needs_file = 1,
846 .unbound_nonreg_file = 1,
847 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_POLL_REMOVE] = {},
849 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700850 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600851 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700852 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300853 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700854 .needs_file = 1,
855 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700856 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700857 .needs_async_data = 1,
858 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000859 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700860 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300861 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700862 .needs_file = 1,
863 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700864 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700865 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700866 .needs_async_data = 1,
867 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000868 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700871 .needs_async_data = 1,
872 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600873 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700874 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000875 [IORING_OP_TIMEOUT_REMOVE] = {
876 /* used by timeout updates' prep() */
877 .work_flags = IO_WQ_WORK_MM,
878 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300879 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700880 .needs_file = 1,
881 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700882 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600883 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700884 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300885 [IORING_OP_ASYNC_CANCEL] = {},
886 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700887 .needs_async_data = 1,
888 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600889 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700890 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300891 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700892 .needs_file = 1,
893 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700894 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700895 .needs_async_data = 1,
896 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600897 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700898 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300899 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700900 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600901 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700902 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300903 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600904 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600905 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700906 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300907 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600908 .needs_file = 1,
909 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600910 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700911 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300912 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600913 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700914 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300915 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600916 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
917 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700918 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300919 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700920 .needs_file = 1,
921 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700922 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700923 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600924 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700925 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600926 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700927 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300928 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700929 .needs_file = 1,
930 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700931 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600932 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700933 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600934 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
935 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700936 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300937 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700938 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600939 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700940 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300941 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600942 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700945 .needs_file = 1,
946 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700947 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600948 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700949 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300950 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700951 .needs_file = 1,
952 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700953 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700954 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600955 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600958 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600959 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700960 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700961 [IORING_OP_EPOLL_CTL] = {
962 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600963 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700964 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300965 [IORING_OP_SPLICE] = {
966 .needs_file = 1,
967 .hash_reg_file = 1,
968 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600969 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700970 },
971 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700972 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300973 [IORING_OP_TEE] = {
974 .needs_file = 1,
975 .hash_reg_file = 1,
976 .unbound_nonreg_file = 1,
977 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600978 [IORING_OP_SHUTDOWN] = {
979 .needs_file = 1,
980 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600981 [IORING_OP_RENAMEAT] = {
982 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
983 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
984 },
Jens Axboe14a11432020-09-28 14:27:37 -0600985 [IORING_OP_UNLINKAT] = {
986 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
987 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
988 },
Jens Axboed3656342019-12-18 09:50:26 -0700989};
990
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700991enum io_mem_account {
992 ACCT_LOCKED,
993 ACCT_PINNED,
994};
995
Pavel Begunkov90df0852021-01-04 20:43:30 +0000996static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
997 struct task_struct *task);
998
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000999static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node);
1000static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
1001 struct io_ring_ctx *ctx);
1002
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001003static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
1004 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001005static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001006static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001007static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001008static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001009static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001010static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001011static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001012static int __io_sqe_files_update(struct io_ring_ctx *ctx,
1013 struct io_uring_files_update *ip,
1014 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001015static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001016static struct file *io_file_get(struct io_submit_state *state,
1017 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001018static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -06001019static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001020
Jens Axboeb63534c2020-06-04 11:28:00 -06001021static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1022 struct iovec **iovec, struct iov_iter *iter,
1023 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001024static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1025 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001026 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001027
1028static struct kmem_cache *req_cachep;
1029
Jens Axboe09186822020-10-13 15:01:40 -06001030static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031
1032struct sock *io_uring_get_socket(struct file *file)
1033{
1034#if defined(CONFIG_UNIX)
1035 if (file->f_op == &io_uring_fops) {
1036 struct io_ring_ctx *ctx = file->private_data;
1037
1038 return ctx->ring_sock->sk;
1039 }
1040#endif
1041 return NULL;
1042}
1043EXPORT_SYMBOL(io_uring_get_socket);
1044
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001045#define io_for_each_link(pos, head) \
1046 for (pos = (head); pos; pos = pos->link)
1047
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001048static inline void io_clean_op(struct io_kiocb *req)
1049{
Pavel Begunkovbb175342020-08-20 11:33:35 +03001050 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
1051 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001052 __io_clean_op(req);
1053}
1054
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001055static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001056{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001057 struct io_ring_ctx *ctx = req->ctx;
1058
1059 if (!req->fixed_file_refs) {
1060 req->fixed_file_refs = &ctx->file_data->node->refs;
1061 percpu_ref_get(req->fixed_file_refs);
1062 }
1063}
1064
Pavel Begunkov08d23632020-11-06 13:00:22 +00001065static bool io_match_task(struct io_kiocb *head,
1066 struct task_struct *task,
1067 struct files_struct *files)
1068{
1069 struct io_kiocb *req;
1070
1071 if (task && head->task != task)
1072 return false;
1073 if (!files)
1074 return true;
1075
1076 io_for_each_link(req, head) {
1077 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
1078 (req->work.flags & IO_WQ_WORK_FILES) &&
1079 req->work.identity->files == files)
1080 return true;
1081 }
1082 return false;
1083}
1084
Jens Axboe28cea78a2020-09-14 10:51:17 -06001085static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001086{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001087 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001088 struct mm_struct *mm = current->mm;
1089
1090 if (mm) {
1091 kthread_unuse_mm(mm);
1092 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001093 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001094 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001095 if (files) {
1096 struct nsproxy *nsproxy = current->nsproxy;
1097
1098 task_lock(current);
1099 current->files = NULL;
1100 current->nsproxy = NULL;
1101 task_unlock(current);
1102 put_files_struct(files);
1103 put_nsproxy(nsproxy);
1104 }
1105}
1106
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001107static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001108{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001109 if (current->flags & PF_EXITING)
1110 return -EFAULT;
1111
Jens Axboe28cea78a2020-09-14 10:51:17 -06001112 if (!current->files) {
1113 struct files_struct *files;
1114 struct nsproxy *nsproxy;
1115
1116 task_lock(ctx->sqo_task);
1117 files = ctx->sqo_task->files;
1118 if (!files) {
1119 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001120 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001121 }
1122 atomic_inc(&files->count);
1123 get_nsproxy(ctx->sqo_task->nsproxy);
1124 nsproxy = ctx->sqo_task->nsproxy;
1125 task_unlock(ctx->sqo_task);
1126
1127 task_lock(current);
1128 current->files = files;
1129 current->nsproxy = nsproxy;
1130 task_unlock(current);
1131 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001132 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001133}
1134
1135static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1136{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001137 struct mm_struct *mm;
1138
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001139 if (current->flags & PF_EXITING)
1140 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001141 if (current->mm)
1142 return 0;
1143
1144 /* Should never happen */
1145 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1146 return -EFAULT;
1147
1148 task_lock(ctx->sqo_task);
1149 mm = ctx->sqo_task->mm;
1150 if (unlikely(!mm || !mmget_not_zero(mm)))
1151 mm = NULL;
1152 task_unlock(ctx->sqo_task);
1153
1154 if (mm) {
1155 kthread_use_mm(mm);
1156 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001157 }
1158
Jens Axboe4b70cf92020-11-02 10:39:05 -07001159 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001160}
1161
Jens Axboe28cea78a2020-09-14 10:51:17 -06001162static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1163 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001164{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001165 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001166 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001167
1168 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001169 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001170 if (unlikely(ret))
1171 return ret;
1172 }
1173
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001174 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1175 ret = __io_sq_thread_acquire_files(ctx);
1176 if (unlikely(ret))
1177 return ret;
1178 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001179
1180 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001181}
1182
Dennis Zhou91d8f512020-09-16 13:41:05 -07001183static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1184 struct cgroup_subsys_state **cur_css)
1185
1186{
1187#ifdef CONFIG_BLK_CGROUP
1188 /* puts the old one when swapping */
1189 if (*cur_css != ctx->sqo_blkcg_css) {
1190 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1191 *cur_css = ctx->sqo_blkcg_css;
1192 }
1193#endif
1194}
1195
1196static void io_sq_thread_unassociate_blkcg(void)
1197{
1198#ifdef CONFIG_BLK_CGROUP
1199 kthread_associate_blkcg(NULL);
1200#endif
1201}
1202
Jens Axboec40f6372020-06-25 15:39:59 -06001203static inline void req_set_fail_links(struct io_kiocb *req)
1204{
1205 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1206 req->flags |= REQ_F_FAIL_LINK;
1207}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001208
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001209/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001210 * None of these are dereferenced, they are simply used to check if any of
1211 * them have changed. If we're under current and check they are still the
1212 * same, we're fine to grab references to them for actual out-of-line use.
1213 */
1214static void io_init_identity(struct io_identity *id)
1215{
1216 id->files = current->files;
1217 id->mm = current->mm;
1218#ifdef CONFIG_BLK_CGROUP
1219 rcu_read_lock();
1220 id->blkcg_css = blkcg_css();
1221 rcu_read_unlock();
1222#endif
1223 id->creds = current_cred();
1224 id->nsproxy = current->nsproxy;
1225 id->fs = current->fs;
1226 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001227#ifdef CONFIG_AUDIT
1228 id->loginuid = current->loginuid;
1229 id->sessionid = current->sessionid;
1230#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001231 refcount_set(&id->count, 1);
1232}
1233
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001234static inline void __io_req_init_async(struct io_kiocb *req)
1235{
1236 memset(&req->work, 0, sizeof(req->work));
1237 req->flags |= REQ_F_WORK_INITIALIZED;
1238}
1239
Jens Axboe1e6fa522020-10-15 08:46:24 -06001240/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001241 * Note: must call io_req_init_async() for the first time you
1242 * touch any members of io_wq_work.
1243 */
1244static inline void io_req_init_async(struct io_kiocb *req)
1245{
Jens Axboe500a3732020-10-15 17:38:03 -06001246 struct io_uring_task *tctx = current->io_uring;
1247
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001248 if (req->flags & REQ_F_WORK_INITIALIZED)
1249 return;
1250
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001251 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001252
1253 /* Grab a ref if this isn't our static identity */
1254 req->work.identity = tctx->identity;
1255 if (tctx->identity != &tctx->__identity)
1256 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001257}
1258
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001259static inline bool io_async_submit(struct io_ring_ctx *ctx)
1260{
1261 return ctx->flags & IORING_SETUP_SQPOLL;
1262}
1263
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1265{
1266 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1267
Jens Axboe0f158b42020-05-14 17:18:39 -06001268 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269}
1270
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001271static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1272{
1273 return !req->timeout.off;
1274}
1275
Jens Axboe2b188cc2019-01-07 10:46:33 -07001276static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1277{
1278 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001279 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280
1281 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1282 if (!ctx)
1283 return NULL;
1284
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001285 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1286 if (!ctx->fallback_req)
1287 goto err;
1288
Jens Axboe78076bb2019-12-04 19:56:40 -07001289 /*
1290 * Use 5 bits less than the max cq entries, that should give us around
1291 * 32 entries per hash list if totally full and uniformly spread.
1292 */
1293 hash_bits = ilog2(p->cq_entries);
1294 hash_bits -= 5;
1295 if (hash_bits <= 0)
1296 hash_bits = 1;
1297 ctx->cancel_hash_bits = hash_bits;
1298 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1299 GFP_KERNEL);
1300 if (!ctx->cancel_hash)
1301 goto err;
1302 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1303
Roman Gushchin21482892019-05-07 10:01:48 -07001304 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001305 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1306 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001307
1308 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001309 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001310 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001311 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001312 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001313 init_completion(&ctx->ref_comp);
1314 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001315 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001316 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001317 mutex_init(&ctx->uring_lock);
1318 init_waitqueue_head(&ctx->wait);
1319 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001320 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001321 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001322 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001323 spin_lock_init(&ctx->inflight_lock);
1324 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001325 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1326 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001327 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001328err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001329 if (ctx->fallback_req)
1330 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001331 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001332 kfree(ctx);
1333 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001334}
1335
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001336static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001337{
Jens Axboe2bc99302020-07-09 09:43:27 -06001338 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1339 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001340
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001341 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001342 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001343 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001344
Bob Liu9d858b22019-11-13 18:06:25 +08001345 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001346}
1347
Jens Axboede0617e2019-04-06 21:51:27 -06001348static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349{
Hristo Venev75b28af2019-08-26 17:23:46 +00001350 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001351
Pavel Begunkov07910152020-01-17 03:52:46 +03001352 /* order cqe stores with ring update */
1353 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001354}
1355
Jens Axboe5c3462c2020-10-15 09:02:33 -06001356static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001357{
Jens Axboe500a3732020-10-15 17:38:03 -06001358 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001359 return;
1360 if (refcount_dec_and_test(&req->work.identity->count))
1361 kfree(req->work.identity);
1362}
1363
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001364static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001365{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001366 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001367 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001368
1369 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001370
Jens Axboedfead8a2020-10-14 10:12:37 -06001371 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboe98447d62020-10-14 10:48:51 -06001372 mmdrop(req->work.identity->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001373 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001374 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001375#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001376 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Jens Axboe98447d62020-10-14 10:48:51 -06001377 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001378 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001379 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001380#endif
1381 if (req->work.flags & IO_WQ_WORK_CREDS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001382 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001383 req->work.flags &= ~IO_WQ_WORK_CREDS;
1384 }
1385 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001386 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001387
Jens Axboe98447d62020-10-14 10:48:51 -06001388 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001389 if (--fs->users)
1390 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001391 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001392 if (fs)
1393 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001394 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001395 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001396
Jens Axboe5c3462c2020-10-15 09:02:33 -06001397 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001398}
1399
1400/*
1401 * Create a private copy of io_identity, since some fields don't match
1402 * the current context.
1403 */
1404static bool io_identity_cow(struct io_kiocb *req)
1405{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001406 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001407 const struct cred *creds = NULL;
1408 struct io_identity *id;
1409
1410 if (req->work.flags & IO_WQ_WORK_CREDS)
1411 creds = req->work.identity->creds;
1412
1413 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1414 if (unlikely(!id)) {
1415 req->work.flags |= IO_WQ_WORK_CANCEL;
1416 return false;
1417 }
1418
1419 /*
1420 * We can safely just re-init the creds we copied Either the field
1421 * matches the current one, or we haven't grabbed it yet. The only
1422 * exception is ->creds, through registered personalities, so handle
1423 * that one separately.
1424 */
1425 io_init_identity(id);
1426 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001427 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001428
1429 /* add one for this request */
1430 refcount_inc(&id->count);
1431
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001432 /* drop tctx and req identity references, if needed */
1433 if (tctx->identity != &tctx->__identity &&
1434 refcount_dec_and_test(&tctx->identity->count))
1435 kfree(tctx->identity);
1436 if (req->work.identity != &tctx->__identity &&
1437 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001438 kfree(req->work.identity);
1439
1440 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001441 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001442 return true;
1443}
1444
1445static bool io_grab_identity(struct io_kiocb *req)
1446{
1447 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001448 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001449 struct io_ring_ctx *ctx = req->ctx;
1450
Jens Axboe69228332020-10-20 14:28:41 -06001451 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1452 if (id->fsize != rlimit(RLIMIT_FSIZE))
1453 return false;
1454 req->work.flags |= IO_WQ_WORK_FSIZE;
1455 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001456#ifdef CONFIG_BLK_CGROUP
1457 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1458 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1459 rcu_read_lock();
1460 if (id->blkcg_css != blkcg_css()) {
1461 rcu_read_unlock();
1462 return false;
1463 }
1464 /*
1465 * This should be rare, either the cgroup is dying or the task
1466 * is moving cgroups. Just punt to root for the handful of ios.
1467 */
1468 if (css_tryget_online(id->blkcg_css))
1469 req->work.flags |= IO_WQ_WORK_BLKCG;
1470 rcu_read_unlock();
1471 }
1472#endif
1473 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1474 if (id->creds != current_cred())
1475 return false;
1476 get_cred(id->creds);
1477 req->work.flags |= IO_WQ_WORK_CREDS;
1478 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001479#ifdef CONFIG_AUDIT
1480 if (!uid_eq(current->loginuid, id->loginuid) ||
1481 current->sessionid != id->sessionid)
1482 return false;
1483#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001484 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1485 (def->work_flags & IO_WQ_WORK_FS)) {
1486 if (current->fs != id->fs)
1487 return false;
1488 spin_lock(&id->fs->lock);
1489 if (!id->fs->in_exec) {
1490 id->fs->users++;
1491 req->work.flags |= IO_WQ_WORK_FS;
1492 } else {
1493 req->work.flags |= IO_WQ_WORK_CANCEL;
1494 }
1495 spin_unlock(&current->fs->lock);
1496 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001497 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1498 (def->work_flags & IO_WQ_WORK_FILES) &&
1499 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1500 if (id->files != current->files ||
1501 id->nsproxy != current->nsproxy)
1502 return false;
1503 atomic_inc(&id->files->count);
1504 get_nsproxy(id->nsproxy);
1505 req->flags |= REQ_F_INFLIGHT;
1506
1507 spin_lock_irq(&ctx->inflight_lock);
1508 list_add(&req->inflight_entry, &ctx->inflight_list);
1509 spin_unlock_irq(&ctx->inflight_lock);
1510 req->work.flags |= IO_WQ_WORK_FILES;
1511 }
Jens Axboe77788772020-12-29 10:50:46 -07001512 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1513 (def->work_flags & IO_WQ_WORK_MM)) {
1514 if (id->mm != current->mm)
1515 return false;
1516 mmgrab(id->mm);
1517 req->work.flags |= IO_WQ_WORK_MM;
1518 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001519
1520 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001521}
1522
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001523static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001524{
Jens Axboed3656342019-12-18 09:50:26 -07001525 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001526 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001527
Pavel Begunkov16d59802020-07-12 16:16:47 +03001528 io_req_init_async(req);
1529
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001530 if (req->flags & REQ_F_FORCE_ASYNC)
1531 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1532
Jens Axboed3656342019-12-18 09:50:26 -07001533 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001534 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001535 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001536 } else {
1537 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001538 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001539 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001540
Jens Axboe1e6fa522020-10-15 08:46:24 -06001541 /* if we fail grabbing identity, we must COW, regrab, and retry */
1542 if (io_grab_identity(req))
1543 return;
1544
1545 if (!io_identity_cow(req))
1546 return;
1547
1548 /* can't fail at this point */
1549 if (!io_grab_identity(req))
1550 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001551}
1552
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001553static void io_prep_async_link(struct io_kiocb *req)
1554{
1555 struct io_kiocb *cur;
1556
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001557 io_for_each_link(cur, req)
1558 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001559}
1560
Jens Axboe7271ef32020-08-10 09:55:22 -06001561static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001562{
Jackie Liua197f662019-11-08 08:09:12 -07001563 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001564 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001565
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001566 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1567 &req->work, req->flags);
1568 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001569 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001570}
1571
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001572static void io_queue_async_work(struct io_kiocb *req)
1573{
Jens Axboe7271ef32020-08-10 09:55:22 -06001574 struct io_kiocb *link;
1575
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001576 /* init ->work of the whole link before punting */
1577 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001578 link = __io_queue_async_work(req);
1579
1580 if (link)
1581 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001582}
1583
Jens Axboe5262f562019-09-17 12:26:57 -06001584static void io_kill_timeout(struct io_kiocb *req)
1585{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001586 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001587 int ret;
1588
Jens Axboee8c2bc12020-08-15 18:44:09 -07001589 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001590 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001591 atomic_set(&req->ctx->cq_timeouts,
1592 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001593 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001594 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001595 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001596 }
1597}
1598
Jens Axboe76e1b642020-09-26 15:05:03 -06001599/*
1600 * Returns true if we found and killed one or more timeouts
1601 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001602static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1603 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001604{
1605 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001606 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001607
1608 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001609 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001610 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001611 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001612 canceled++;
1613 }
Jens Axboef3606e32020-09-22 08:18:24 -06001614 }
Jens Axboe5262f562019-09-17 12:26:57 -06001615 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001616 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001617}
1618
Pavel Begunkov04518942020-05-26 20:34:05 +03001619static void __io_queue_deferred(struct io_ring_ctx *ctx)
1620{
1621 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001622 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1623 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001624 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001625
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001626 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001627 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001628 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001629 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001630 link = __io_queue_async_work(de->req);
1631 if (link) {
1632 __io_queue_linked_timeout(link);
1633 /* drop submission reference */
Pavel Begunkov216578e2020-10-13 09:44:00 +01001634 io_put_req_deferred(link, 1);
Jens Axboe7271ef32020-08-10 09:55:22 -06001635 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001636 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001637 } while (!list_empty(&ctx->defer_list));
1638}
1639
Pavel Begunkov360428f2020-05-30 14:54:17 +03001640static void io_flush_timeouts(struct io_ring_ctx *ctx)
1641{
1642 while (!list_empty(&ctx->timeout_list)) {
1643 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001644 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001645
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001646 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001647 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001648 if (req->timeout.target_seq != ctx->cached_cq_tail
1649 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001650 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001651
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001652 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001653 io_kill_timeout(req);
1654 }
1655}
1656
Jens Axboede0617e2019-04-06 21:51:27 -06001657static void io_commit_cqring(struct io_ring_ctx *ctx)
1658{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001659 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001660 __io_commit_cqring(ctx);
1661
Pavel Begunkov04518942020-05-26 20:34:05 +03001662 if (unlikely(!list_empty(&ctx->defer_list)))
1663 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001664}
1665
Jens Axboe90554202020-09-03 12:12:41 -06001666static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1667{
1668 struct io_rings *r = ctx->rings;
1669
1670 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1671}
1672
Jens Axboe2b188cc2019-01-07 10:46:33 -07001673static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1674{
Hristo Venev75b28af2019-08-26 17:23:46 +00001675 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676 unsigned tail;
1677
1678 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001679 /*
1680 * writes to the cq entry need to come after reading head; the
1681 * control dependency is enough as we're using WRITE_ONCE to
1682 * fill the cq entry
1683 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001684 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001685 return NULL;
1686
1687 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001688 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001689}
1690
Jens Axboef2842ab2020-01-08 11:04:00 -07001691static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1692{
Jens Axboef0b493e2020-02-01 21:30:11 -07001693 if (!ctx->cq_ev_fd)
1694 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001695 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1696 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001697 if (!ctx->eventfd_async)
1698 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001699 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001700}
1701
Pavel Begunkove23de152020-12-17 00:24:37 +00001702static inline unsigned __io_cqring_events(struct io_ring_ctx *ctx)
1703{
1704 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1705}
1706
Jens Axboeb41e9852020-02-17 09:52:41 -07001707static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001708{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001709 /* see waitqueue_active() comment */
1710 smp_mb();
1711
Jens Axboe8c838782019-03-12 15:48:16 -06001712 if (waitqueue_active(&ctx->wait))
1713 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001714 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1715 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001716 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001717 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001718 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001719 wake_up_interruptible(&ctx->cq_wait);
1720 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1721 }
Jens Axboe8c838782019-03-12 15:48:16 -06001722}
1723
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001724static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1725{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001726 /* see waitqueue_active() comment */
1727 smp_mb();
1728
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001729 if (ctx->flags & IORING_SETUP_SQPOLL) {
1730 if (waitqueue_active(&ctx->wait))
1731 wake_up(&ctx->wait);
1732 }
1733 if (io_should_trigger_evfd(ctx))
1734 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001735 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001736 wake_up_interruptible(&ctx->cq_wait);
1737 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1738 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001739}
1740
Jens Axboec4a2ed72019-11-21 21:01:26 -07001741/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001742static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1743 struct task_struct *tsk,
1744 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001746 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001747 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001748 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001749 unsigned long flags;
Pavel Begunkov09e88402020-12-17 00:24:38 +00001750 bool all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001751 LIST_HEAD(list);
1752
Pavel Begunkove23de152020-12-17 00:24:37 +00001753 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1754 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001755
1756 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001757 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001758 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001759 continue;
1760
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001761 cqe = io_get_cqring(ctx);
1762 if (!cqe && !force)
1763 break;
1764
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001765 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001766 if (cqe) {
1767 WRITE_ONCE(cqe->user_data, req->user_data);
1768 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001769 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001770 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001771 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001772 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001773 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001774 }
1775 }
1776
Pavel Begunkov09e88402020-12-17 00:24:38 +00001777 all_flushed = list_empty(&ctx->cq_overflow_list);
1778 if (all_flushed) {
1779 clear_bit(0, &ctx->sq_check_overflow);
1780 clear_bit(0, &ctx->cq_check_overflow);
1781 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1782 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001783
Pavel Begunkov09e88402020-12-17 00:24:38 +00001784 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001785 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1786 io_cqring_ev_posted(ctx);
1787
1788 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001789 req = list_first_entry(&list, struct io_kiocb, compl.list);
1790 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001791 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001792 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001793
Pavel Begunkov09e88402020-12-17 00:24:38 +00001794 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001795}
1796
Pavel Begunkov6c503152021-01-04 20:36:36 +00001797static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1798 struct task_struct *tsk,
1799 struct files_struct *files)
1800{
1801 if (test_bit(0, &ctx->cq_check_overflow)) {
1802 /* iopoll syncs against uring_lock, not completion_lock */
1803 if (ctx->flags & IORING_SETUP_IOPOLL)
1804 mutex_lock(&ctx->uring_lock);
1805 __io_cqring_overflow_flush(ctx, force, tsk, files);
1806 if (ctx->flags & IORING_SETUP_IOPOLL)
1807 mutex_unlock(&ctx->uring_lock);
1808 }
1809}
1810
Jens Axboebcda7ba2020-02-23 16:42:51 -07001811static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001812{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001813 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001814 struct io_uring_cqe *cqe;
1815
Jens Axboe78e19bb2019-11-06 15:21:34 -07001816 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001817
Jens Axboe2b188cc2019-01-07 10:46:33 -07001818 /*
1819 * If we can't get a cq entry, userspace overflowed the
1820 * submission (by quite a lot). Increment the overflow count in
1821 * the ring.
1822 */
1823 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001824 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001825 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001826 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001827 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001828 } else if (ctx->cq_overflow_flushed ||
1829 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001830 /*
1831 * If we're in ring overflow flush mode, or in task cancel mode,
1832 * then we cannot store the request for later flushing, we need
1833 * to drop it on the floor.
1834 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001835 ctx->cached_cq_overflow++;
1836 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001837 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001838 if (list_empty(&ctx->cq_overflow_list)) {
1839 set_bit(0, &ctx->sq_check_overflow);
1840 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001841 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001842 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001843 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001844 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001845 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001846 refcount_inc(&req->refs);
1847 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001848 }
1849}
1850
Jens Axboebcda7ba2020-02-23 16:42:51 -07001851static void io_cqring_fill_event(struct io_kiocb *req, long res)
1852{
1853 __io_cqring_fill_event(req, res, 0);
1854}
1855
Jens Axboee1e16092020-06-22 09:17:17 -06001856static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001858 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001859 unsigned long flags;
1860
1861 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001862 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863 io_commit_cqring(ctx);
1864 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1865
Jens Axboe8c838782019-03-12 15:48:16 -06001866 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867}
1868
Jens Axboe229a7b62020-06-22 10:13:11 -06001869static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001870{
Jens Axboe229a7b62020-06-22 10:13:11 -06001871 struct io_ring_ctx *ctx = cs->ctx;
1872
1873 spin_lock_irq(&ctx->completion_lock);
1874 while (!list_empty(&cs->list)) {
1875 struct io_kiocb *req;
1876
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001877 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1878 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001879 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001880
1881 /*
1882 * io_free_req() doesn't care about completion_lock unless one
1883 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1884 * because of a potential deadlock with req->work.fs->lock
1885 */
1886 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1887 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001888 spin_unlock_irq(&ctx->completion_lock);
1889 io_put_req(req);
1890 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001891 } else {
1892 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001893 }
1894 }
1895 io_commit_cqring(ctx);
1896 spin_unlock_irq(&ctx->completion_lock);
1897
1898 io_cqring_ev_posted(ctx);
1899 cs->nr = 0;
1900}
1901
1902static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1903 struct io_comp_state *cs)
1904{
1905 if (!cs) {
1906 io_cqring_add_event(req, res, cflags);
1907 io_put_req(req);
1908 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001909 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001910 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001911 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001912 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001913 if (++cs->nr >= 32)
1914 io_submit_flush_completions(cs);
1915 }
Jens Axboee1e16092020-06-22 09:17:17 -06001916}
1917
1918static void io_req_complete(struct io_kiocb *req, long res)
1919{
Jens Axboe229a7b62020-06-22 10:13:11 -06001920 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001921}
1922
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001923static inline bool io_is_fallback_req(struct io_kiocb *req)
1924{
1925 return req == (struct io_kiocb *)
1926 ((unsigned long) req->ctx->fallback_req & ~1UL);
1927}
1928
1929static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1930{
1931 struct io_kiocb *req;
1932
1933 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001934 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001935 return req;
1936
1937 return NULL;
1938}
1939
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001940static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1941 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001943 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001944 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001945 size_t sz;
1946 int ret;
1947
1948 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001949 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1950
1951 /*
1952 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1953 * retry single alloc to be on the safe side.
1954 */
1955 if (unlikely(ret <= 0)) {
1956 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1957 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001958 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001959 ret = 1;
1960 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001961 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001962 }
1963
Pavel Begunkov291b2822020-09-30 22:57:01 +03001964 state->free_reqs--;
1965 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001966fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001967 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001968}
1969
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001970static inline void io_put_file(struct io_kiocb *req, struct file *file,
1971 bool fixed)
1972{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001973 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001974 fput(file);
1975}
1976
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001977static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001978{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001979 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001980
Jens Axboee8c2bc12020-08-15 18:44:09 -07001981 if (req->async_data)
1982 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001983 if (req->file)
1984 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001985 if (req->fixed_file_refs)
1986 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001987 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001988}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001989
Pavel Begunkov216578e2020-10-13 09:44:00 +01001990static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001991{
Jens Axboe0f212202020-09-13 13:09:39 -06001992 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001993 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001994
Pavel Begunkov216578e2020-10-13 09:44:00 +01001995 io_dismantle_req(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001996
Jens Axboed8a6df12020-10-15 16:24:45 -06001997 percpu_counter_dec(&tctx->inflight);
Jens Axboefdaf0832020-10-30 09:37:30 -06001998 if (atomic_read(&tctx->in_idle))
Jens Axboe0f212202020-09-13 13:09:39 -06001999 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002000 put_task_struct(req->task);
2001
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03002002 if (likely(!io_is_fallback_req(req)))
2003 kmem_cache_free(req_cachep, req);
2004 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002005 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
2006 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002007}
2008
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002009static inline void io_remove_next_linked(struct io_kiocb *req)
2010{
2011 struct io_kiocb *nxt = req->link;
2012
2013 req->link = nxt->link;
2014 nxt->link = NULL;
2015}
2016
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002017static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002018{
Jackie Liua197f662019-11-08 08:09:12 -07002019 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002020 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002021 bool cancelled = false;
2022 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002023
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002024 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002025 link = req->link;
2026
Pavel Begunkov900fad42020-10-19 16:39:16 +01002027 /*
2028 * Can happen if a linked timeout fired and link had been like
2029 * req -> link t-out -> link t-out [-> ...]
2030 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002031 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2032 struct io_timeout_data *io = link->async_data;
2033 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002034
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002035 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002036 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002037 ret = hrtimer_try_to_cancel(&io->timer);
2038 if (ret != -1) {
2039 io_cqring_fill_event(link, -ECANCELED);
2040 io_commit_cqring(ctx);
2041 cancelled = true;
2042 }
2043 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002044 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002045 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002046
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002047 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002048 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002049 io_put_req(link);
2050 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002051}
2052
Jens Axboe4d7dd462019-11-20 13:03:52 -07002053
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002054static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002055{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002056 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002057 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002058 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002059
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002060 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002061 link = req->link;
2062 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002063
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002064 while (link) {
2065 nxt = link->link;
2066 link->link = NULL;
2067
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002068 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002069 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002070
2071 /*
2072 * It's ok to free under spinlock as they're not linked anymore,
2073 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2074 * work.fs->lock.
2075 */
2076 if (link->flags & REQ_F_WORK_INITIALIZED)
2077 io_put_req_deferred(link, 2);
2078 else
2079 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002080 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002081 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002082 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002083 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002084
Jens Axboe2665abf2019-11-05 12:40:47 -07002085 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002086}
2087
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002088static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002089{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002090 if (req->flags & REQ_F_LINK_TIMEOUT)
2091 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002092
Jens Axboe9e645e112019-05-10 16:07:28 -06002093 /*
2094 * If LINK is set, we have dependent requests in this chain. If we
2095 * didn't fail this request, queue the first one up, moving any other
2096 * dependencies to the next request. In case of failure, fail the rest
2097 * of the chain.
2098 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002099 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2100 struct io_kiocb *nxt = req->link;
2101
2102 req->link = NULL;
2103 return nxt;
2104 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002105 io_fail_links(req);
2106 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002107}
Jens Axboe2665abf2019-11-05 12:40:47 -07002108
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002109static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002110{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002111 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002112 return NULL;
2113 return __io_req_find_next(req);
2114}
2115
Jens Axboe355fb9e2020-10-22 20:19:35 -06002116static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002117{
2118 struct task_struct *tsk = req->task;
2119 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002120 enum task_work_notify_mode notify;
2121 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002122
Jens Axboe6200b0a2020-09-13 14:38:30 -06002123 if (tsk->flags & PF_EXITING)
2124 return -ESRCH;
2125
Jens Axboec2c4c832020-07-01 15:37:11 -06002126 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002127 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2128 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2129 * processing task_work. There's no reliable way to tell if TWA_RESUME
2130 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002131 */
Jens Axboe91989c72020-10-16 09:02:26 -06002132 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002133 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002134 notify = TWA_SIGNAL;
2135
Jens Axboe87c43112020-09-30 21:00:14 -06002136 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002137 if (!ret)
2138 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002139
Jens Axboec2c4c832020-07-01 15:37:11 -06002140 return ret;
2141}
2142
Jens Axboec40f6372020-06-25 15:39:59 -06002143static void __io_req_task_cancel(struct io_kiocb *req, int error)
2144{
2145 struct io_ring_ctx *ctx = req->ctx;
2146
2147 spin_lock_irq(&ctx->completion_lock);
2148 io_cqring_fill_event(req, error);
2149 io_commit_cqring(ctx);
2150 spin_unlock_irq(&ctx->completion_lock);
2151
2152 io_cqring_ev_posted(ctx);
2153 req_set_fail_links(req);
2154 io_double_put_req(req);
2155}
2156
2157static void io_req_task_cancel(struct callback_head *cb)
2158{
2159 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002160 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002161
2162 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002163 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002164}
2165
2166static void __io_req_task_submit(struct io_kiocb *req)
2167{
2168 struct io_ring_ctx *ctx = req->ctx;
2169
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002170 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002171 if (!ctx->sqo_dead &&
2172 !__io_sq_thread_acquire_mm(ctx) &&
2173 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002174 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002175 else
Jens Axboec40f6372020-06-25 15:39:59 -06002176 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002177 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002178}
2179
Jens Axboec40f6372020-06-25 15:39:59 -06002180static void io_req_task_submit(struct callback_head *cb)
2181{
2182 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002183 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002184
2185 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002186 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002187}
2188
2189static void io_req_task_queue(struct io_kiocb *req)
2190{
Jens Axboec40f6372020-06-25 15:39:59 -06002191 int ret;
2192
2193 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002194 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002195
Jens Axboe355fb9e2020-10-22 20:19:35 -06002196 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002197 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002198 struct task_struct *tsk;
2199
Jens Axboec40f6372020-06-25 15:39:59 -06002200 init_task_work(&req->task_work, io_req_task_cancel);
2201 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002202 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002203 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002204 }
Jens Axboec40f6372020-06-25 15:39:59 -06002205}
2206
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002207static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002208{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002209 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002210
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002211 if (nxt)
2212 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002213}
2214
Jens Axboe9e645e112019-05-10 16:07:28 -06002215static void io_free_req(struct io_kiocb *req)
2216{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002217 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002218 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002219}
2220
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002221struct req_batch {
2222 void *reqs[IO_IOPOLL_BATCH];
2223 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002224
2225 struct task_struct *task;
2226 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002227};
2228
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002229static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002230{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002231 rb->to_free = 0;
2232 rb->task_refs = 0;
2233 rb->task = NULL;
2234}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002235
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002236static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2237 struct req_batch *rb)
2238{
2239 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2240 percpu_ref_put_many(&ctx->refs, rb->to_free);
2241 rb->to_free = 0;
2242}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002243
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002244static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2245 struct req_batch *rb)
2246{
2247 if (rb->to_free)
2248 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002249 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002250 struct io_uring_task *tctx = rb->task->io_uring;
2251
2252 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002253 put_task_struct_many(rb->task, rb->task_refs);
2254 rb->task = NULL;
2255 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002256}
2257
2258static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2259{
2260 if (unlikely(io_is_fallback_req(req))) {
2261 io_free_req(req);
2262 return;
2263 }
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002264 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002265
Jens Axboee3bc8e92020-09-24 08:45:57 -06002266 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002267 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002268 struct io_uring_task *tctx = rb->task->io_uring;
2269
2270 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002271 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002272 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002273 rb->task = req->task;
2274 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002275 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002276 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002277
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002278 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002279 rb->reqs[rb->to_free++] = req;
2280 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2281 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002282}
2283
Jens Axboeba816ad2019-09-28 11:36:45 -06002284/*
2285 * Drop reference to request, return next in chain (if there is one) if this
2286 * was the last reference to this request.
2287 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002288static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002289{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002290 struct io_kiocb *nxt = NULL;
2291
Jens Axboe2a44f462020-02-25 13:25:41 -07002292 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002293 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002294 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002295 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002296 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002297}
2298
Jens Axboe2b188cc2019-01-07 10:46:33 -07002299static void io_put_req(struct io_kiocb *req)
2300{
Jens Axboedef596e2019-01-09 08:59:42 -07002301 if (refcount_dec_and_test(&req->refs))
2302 io_free_req(req);
2303}
2304
Pavel Begunkov216578e2020-10-13 09:44:00 +01002305static void io_put_req_deferred_cb(struct callback_head *cb)
2306{
2307 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2308
2309 io_free_req(req);
2310}
2311
2312static void io_free_req_deferred(struct io_kiocb *req)
2313{
2314 int ret;
2315
2316 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002317 ret = io_req_task_work_add(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002318 if (unlikely(ret)) {
2319 struct task_struct *tsk;
2320
2321 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002322 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002323 wake_up_process(tsk);
2324 }
2325}
2326
2327static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2328{
2329 if (refcount_sub_and_test(refs, &req->refs))
2330 io_free_req_deferred(req);
2331}
2332
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002333static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002334{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002335 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002336
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002337 /*
2338 * A ref is owned by io-wq in which context we're. So, if that's the
2339 * last one, it's safe to steal next work. False negatives are Ok,
2340 * it just will be re-punted async in io_put_work()
2341 */
2342 if (refcount_read(&req->refs) != 1)
2343 return NULL;
2344
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002345 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002346 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002347}
2348
Jens Axboe978db572019-11-14 22:39:04 -07002349static void io_double_put_req(struct io_kiocb *req)
2350{
2351 /* drop both submit and complete references */
2352 if (refcount_sub_and_test(2, &req->refs))
2353 io_free_req(req);
2354}
2355
Pavel Begunkov6c503152021-01-04 20:36:36 +00002356static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002357{
2358 /* See comment at the top of this file */
2359 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002360 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002361}
2362
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002363static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2364{
2365 struct io_rings *rings = ctx->rings;
2366
2367 /* make sure SQ entry isn't read before tail */
2368 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2369}
2370
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002371static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002372{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002373 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002374
Jens Axboebcda7ba2020-02-23 16:42:51 -07002375 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2376 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002377 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002378 kfree(kbuf);
2379 return cflags;
2380}
2381
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002382static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2383{
2384 struct io_buffer *kbuf;
2385
2386 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2387 return io_put_kbuf(req, kbuf);
2388}
2389
Jens Axboe4c6e2772020-07-01 11:29:10 -06002390static inline bool io_run_task_work(void)
2391{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002392 /*
2393 * Not safe to run on exiting task, and the task_work handling will
2394 * not add work to such a task.
2395 */
2396 if (unlikely(current->flags & PF_EXITING))
2397 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002398 if (current->task_works) {
2399 __set_current_state(TASK_RUNNING);
2400 task_work_run();
2401 return true;
2402 }
2403
2404 return false;
2405}
2406
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002407static void io_iopoll_queue(struct list_head *again)
2408{
2409 struct io_kiocb *req;
2410
2411 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002412 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2413 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002414 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002415 } while (!list_empty(again));
2416}
2417
Jens Axboedef596e2019-01-09 08:59:42 -07002418/*
2419 * Find and free completed poll iocbs
2420 */
2421static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2422 struct list_head *done)
2423{
Jens Axboe8237e042019-12-28 10:48:22 -07002424 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002425 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002426 LIST_HEAD(again);
2427
2428 /* order with ->result store in io_complete_rw_iopoll() */
2429 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002430
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002431 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002432 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002433 int cflags = 0;
2434
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002435 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002436 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002437 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002438 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002439 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002440 continue;
2441 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002442 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002443
Jens Axboebcda7ba2020-02-23 16:42:51 -07002444 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002445 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002446
2447 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002448 (*nr_events)++;
2449
Pavel Begunkovc3524382020-06-28 12:52:32 +03002450 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002451 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002452 }
Jens Axboedef596e2019-01-09 08:59:42 -07002453
Jens Axboe09bb8392019-03-13 12:39:28 -06002454 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002455 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002456 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002457
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002458 if (!list_empty(&again))
2459 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002460}
2461
Jens Axboedef596e2019-01-09 08:59:42 -07002462static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2463 long min)
2464{
2465 struct io_kiocb *req, *tmp;
2466 LIST_HEAD(done);
2467 bool spin;
2468 int ret;
2469
2470 /*
2471 * Only spin for completions if we don't have multiple devices hanging
2472 * off our complete list, and we're under the requested amount.
2473 */
2474 spin = !ctx->poll_multi_file && *nr_events < min;
2475
2476 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002477 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002478 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002479
2480 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002481 * Move completed and retryable entries to our local lists.
2482 * If we find a request that requires polling, break out
2483 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002484 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002485 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002486 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002487 continue;
2488 }
2489 if (!list_empty(&done))
2490 break;
2491
2492 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2493 if (ret < 0)
2494 break;
2495
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002496 /* iopoll may have completed current req */
2497 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002498 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002499
Jens Axboedef596e2019-01-09 08:59:42 -07002500 if (ret && spin)
2501 spin = false;
2502 ret = 0;
2503 }
2504
2505 if (!list_empty(&done))
2506 io_iopoll_complete(ctx, nr_events, &done);
2507
2508 return ret;
2509}
2510
2511/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002512 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002513 * non-spinning poll check - we'll still enter the driver poll loop, but only
2514 * as a non-spinning completion check.
2515 */
2516static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2517 long min)
2518{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002519 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002520 int ret;
2521
2522 ret = io_do_iopoll(ctx, nr_events, min);
2523 if (ret < 0)
2524 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002525 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002526 return 0;
2527 }
2528
2529 return 1;
2530}
2531
2532/*
2533 * We can't just wait for polled events to come to us, we have to actively
2534 * find and complete them.
2535 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002536static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002537{
2538 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2539 return;
2540
2541 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002542 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002543 unsigned int nr_events = 0;
2544
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002545 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002546
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002547 /* let it sleep and repeat later if can't complete a request */
2548 if (nr_events == 0)
2549 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002550 /*
2551 * Ensure we allow local-to-the-cpu processing to take place,
2552 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002553 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002554 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002555 if (need_resched()) {
2556 mutex_unlock(&ctx->uring_lock);
2557 cond_resched();
2558 mutex_lock(&ctx->uring_lock);
2559 }
Jens Axboedef596e2019-01-09 08:59:42 -07002560 }
2561 mutex_unlock(&ctx->uring_lock);
2562}
2563
Pavel Begunkov7668b922020-07-07 16:36:21 +03002564static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002565{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002566 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002567 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002568
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002569 /*
2570 * We disallow the app entering submit/complete with polling, but we
2571 * still need to lock the ring to prevent racing with polled issue
2572 * that got punted to a workqueue.
2573 */
2574 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002575 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002576 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002577 * Don't enter poll loop if we already have events pending.
2578 * If we do, we can potentially be spinning for commands that
2579 * already triggered a CQE (eg in error).
2580 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002581 if (test_bit(0, &ctx->cq_check_overflow))
2582 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2583 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002584 break;
2585
2586 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002587 * If a submit got punted to a workqueue, we can have the
2588 * application entering polling for a command before it gets
2589 * issued. That app will hold the uring_lock for the duration
2590 * of the poll right here, so we need to take a breather every
2591 * now and then to ensure that the issue has a chance to add
2592 * the poll to the issued list. Otherwise we can spin here
2593 * forever, while the workqueue is stuck trying to acquire the
2594 * very same mutex.
2595 */
2596 if (!(++iters & 7)) {
2597 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002598 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002599 mutex_lock(&ctx->uring_lock);
2600 }
2601
Pavel Begunkov7668b922020-07-07 16:36:21 +03002602 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002603 if (ret <= 0)
2604 break;
2605 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002606 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002607
Jens Axboe500f9fb2019-08-19 12:15:59 -06002608 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002609 return ret;
2610}
2611
Jens Axboe491381ce2019-10-17 09:20:46 -06002612static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002613{
Jens Axboe491381ce2019-10-17 09:20:46 -06002614 /*
2615 * Tell lockdep we inherited freeze protection from submission
2616 * thread.
2617 */
2618 if (req->flags & REQ_F_ISREG) {
2619 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002620
Jens Axboe491381ce2019-10-17 09:20:46 -06002621 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002622 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002623 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002624}
2625
Jens Axboea1d7c392020-06-22 11:09:46 -06002626static void io_complete_rw_common(struct kiocb *kiocb, long res,
2627 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002628{
Jens Axboe9adbd452019-12-20 08:45:55 -07002629 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002630 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631
Jens Axboe491381ce2019-10-17 09:20:46 -06002632 if (kiocb->ki_flags & IOCB_WRITE)
2633 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002635 if (res != req->result)
2636 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002637 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002638 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002639 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002640}
2641
Jens Axboeb63534c2020-06-04 11:28:00 -06002642#ifdef CONFIG_BLOCK
2643static bool io_resubmit_prep(struct io_kiocb *req, int error)
2644{
2645 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2646 ssize_t ret = -ECANCELED;
2647 struct iov_iter iter;
2648 int rw;
2649
2650 if (error) {
2651 ret = error;
2652 goto end_req;
2653 }
2654
2655 switch (req->opcode) {
2656 case IORING_OP_READV:
2657 case IORING_OP_READ_FIXED:
2658 case IORING_OP_READ:
2659 rw = READ;
2660 break;
2661 case IORING_OP_WRITEV:
2662 case IORING_OP_WRITE_FIXED:
2663 case IORING_OP_WRITE:
2664 rw = WRITE;
2665 break;
2666 default:
2667 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2668 req->opcode);
2669 goto end_req;
2670 }
2671
Jens Axboee8c2bc12020-08-15 18:44:09 -07002672 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002673 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2674 if (ret < 0)
2675 goto end_req;
2676 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2677 if (!ret)
2678 return true;
2679 kfree(iovec);
2680 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002681 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002682 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002683end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002684 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002685 return false;
2686}
Jens Axboeb63534c2020-06-04 11:28:00 -06002687#endif
2688
2689static bool io_rw_reissue(struct io_kiocb *req, long res)
2690{
2691#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002692 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002693 int ret;
2694
Jens Axboe355afae2020-09-02 09:30:31 -06002695 if (!S_ISBLK(mode) && !S_ISREG(mode))
2696 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002697 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2698 return false;
2699
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002700 lockdep_assert_held(&req->ctx->uring_lock);
2701
Jens Axboe28cea78a2020-09-14 10:51:17 -06002702 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002703
Jens Axboefdee9462020-08-27 16:46:24 -06002704 if (io_resubmit_prep(req, ret)) {
2705 refcount_inc(&req->refs);
2706 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002707 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002708 }
2709
Jens Axboeb63534c2020-06-04 11:28:00 -06002710#endif
2711 return false;
2712}
2713
Jens Axboea1d7c392020-06-22 11:09:46 -06002714static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2715 struct io_comp_state *cs)
2716{
2717 if (!io_rw_reissue(req, res))
2718 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002719}
2720
2721static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2722{
Jens Axboe9adbd452019-12-20 08:45:55 -07002723 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002724
Jens Axboea1d7c392020-06-22 11:09:46 -06002725 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002726}
2727
Jens Axboedef596e2019-01-09 08:59:42 -07002728static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2729{
Jens Axboe9adbd452019-12-20 08:45:55 -07002730 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002731
Jens Axboe491381ce2019-10-17 09:20:46 -06002732 if (kiocb->ki_flags & IOCB_WRITE)
2733 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002734
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002735 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002736 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002737
2738 WRITE_ONCE(req->result, res);
2739 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002740 smp_wmb();
2741 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002742}
2743
2744/*
2745 * After the iocb has been issued, it's safe to be found on the poll list.
2746 * Adding the kiocb to the list AFTER submission ensures that we don't
2747 * find it from a io_iopoll_getevents() thread before the issuer is done
2748 * accessing the kiocb cookie.
2749 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002750static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002751{
2752 struct io_ring_ctx *ctx = req->ctx;
2753
2754 /*
2755 * Track whether we have multiple files in our lists. This will impact
2756 * how we do polling eventually, not spinning if we're on potentially
2757 * different devices.
2758 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002759 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002760 ctx->poll_multi_file = false;
2761 } else if (!ctx->poll_multi_file) {
2762 struct io_kiocb *list_req;
2763
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002764 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002765 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002766 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002767 ctx->poll_multi_file = true;
2768 }
2769
2770 /*
2771 * For fast devices, IO may have already completed. If it has, add
2772 * it to the front so we find it first.
2773 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002774 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002775 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002776 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002777 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002778
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002779 /*
2780 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2781 * task context or in io worker task context. If current task context is
2782 * sq thread, we don't need to check whether should wake up sq thread.
2783 */
2784 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002785 wq_has_sleeper(&ctx->sq_data->wait))
2786 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002787}
2788
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002789static inline void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002790{
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002791 fput_many(state->file, state->file_refs);
2792 state->file_refs = 0;
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002793}
2794
2795static inline void io_state_file_put(struct io_submit_state *state)
2796{
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002797 if (state->file_refs)
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002798 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002799}
2800
2801/*
2802 * Get as many references to a file as we have IOs left in this submission,
2803 * assuming most submissions are for one file, or at least that each file
2804 * has more than one submission.
2805 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002806static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002807{
2808 if (!state)
2809 return fget(fd);
2810
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002811 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002812 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002813 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002814 return state->file;
2815 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002816 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002817 }
2818 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002819 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002820 return NULL;
2821
2822 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002823 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002824 return state->file;
2825}
2826
Jens Axboe4503b762020-06-01 10:00:27 -06002827static bool io_bdev_nowait(struct block_device *bdev)
2828{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002829 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002830}
2831
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832/*
2833 * If we tracked the file through the SCM inflight mechanism, we could support
2834 * any file. For now, just ensure that anything potentially problematic is done
2835 * inline.
2836 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002837static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002838{
2839 umode_t mode = file_inode(file)->i_mode;
2840
Jens Axboe4503b762020-06-01 10:00:27 -06002841 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002842 if (IS_ENABLED(CONFIG_BLOCK) &&
2843 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002844 return true;
2845 return false;
2846 }
2847 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002849 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002850 if (IS_ENABLED(CONFIG_BLOCK) &&
2851 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002852 file->f_op != &io_uring_fops)
2853 return true;
2854 return false;
2855 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002856
Jens Axboec5b85622020-06-09 19:23:05 -06002857 /* any ->read/write should understand O_NONBLOCK */
2858 if (file->f_flags & O_NONBLOCK)
2859 return true;
2860
Jens Axboeaf197f52020-04-28 13:15:06 -06002861 if (!(file->f_mode & FMODE_NOWAIT))
2862 return false;
2863
2864 if (rw == READ)
2865 return file->f_op->read_iter != NULL;
2866
2867 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002868}
2869
Pavel Begunkova88fc402020-09-30 22:57:53 +03002870static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002871{
Jens Axboedef596e2019-01-09 08:59:42 -07002872 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002873 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002874 unsigned ioprio;
2875 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002876
Jens Axboe491381ce2019-10-17 09:20:46 -06002877 if (S_ISREG(file_inode(req->file)->i_mode))
2878 req->flags |= REQ_F_ISREG;
2879
Jens Axboe2b188cc2019-01-07 10:46:33 -07002880 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002881 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2882 req->flags |= REQ_F_CUR_POS;
2883 kiocb->ki_pos = req->file->f_pos;
2884 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002885 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002886 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2887 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2888 if (unlikely(ret))
2889 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002890
2891 ioprio = READ_ONCE(sqe->ioprio);
2892 if (ioprio) {
2893 ret = ioprio_check_cap(ioprio);
2894 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002895 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002896
2897 kiocb->ki_ioprio = ioprio;
2898 } else
2899 kiocb->ki_ioprio = get_current_ioprio();
2900
Stefan Bühler8449eed2019-04-27 20:34:19 +02002901 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002902 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002903 req->flags |= REQ_F_NOWAIT;
2904
Jens Axboedef596e2019-01-09 08:59:42 -07002905 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002906 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2907 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002908 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002909
Jens Axboedef596e2019-01-09 08:59:42 -07002910 kiocb->ki_flags |= IOCB_HIPRI;
2911 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002912 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002913 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002914 if (kiocb->ki_flags & IOCB_HIPRI)
2915 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002916 kiocb->ki_complete = io_complete_rw;
2917 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002918
Jens Axboe3529d8c2019-12-19 18:24:38 -07002919 req->rw.addr = READ_ONCE(sqe->addr);
2920 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002921 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002922 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002923}
2924
2925static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2926{
2927 switch (ret) {
2928 case -EIOCBQUEUED:
2929 break;
2930 case -ERESTARTSYS:
2931 case -ERESTARTNOINTR:
2932 case -ERESTARTNOHAND:
2933 case -ERESTART_RESTARTBLOCK:
2934 /*
2935 * We can't just restart the syscall, since previously
2936 * submitted sqes may already be in progress. Just fail this
2937 * IO with EINTR.
2938 */
2939 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002940 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002941 default:
2942 kiocb->ki_complete(kiocb, ret, 0);
2943 }
2944}
2945
Jens Axboea1d7c392020-06-22 11:09:46 -06002946static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2947 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002948{
Jens Axboeba042912019-12-25 16:33:42 -07002949 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002950 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002951
Jens Axboe227c0c92020-08-13 11:51:40 -06002952 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002953 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002954 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002955 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002956 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002957 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002958 }
2959
Jens Axboeba042912019-12-25 16:33:42 -07002960 if (req->flags & REQ_F_CUR_POS)
2961 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002962 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002963 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002964 else
2965 io_rw_done(kiocb, ret);
2966}
2967
Jens Axboe9adbd452019-12-20 08:45:55 -07002968static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002969 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002970{
Jens Axboe9adbd452019-12-20 08:45:55 -07002971 struct io_ring_ctx *ctx = req->ctx;
2972 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002973 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002974 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002975 size_t offset;
2976 u64 buf_addr;
2977
Jens Axboeedafcce2019-01-09 09:16:05 -07002978 if (unlikely(buf_index >= ctx->nr_user_bufs))
2979 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002980 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2981 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002982 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002983
2984 /* overflow */
2985 if (buf_addr + len < buf_addr)
2986 return -EFAULT;
2987 /* not inside the mapped region */
2988 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2989 return -EFAULT;
2990
2991 /*
2992 * May not be a start of buffer, set size appropriately
2993 * and advance us to the beginning.
2994 */
2995 offset = buf_addr - imu->ubuf;
2996 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002997
2998 if (offset) {
2999 /*
3000 * Don't use iov_iter_advance() here, as it's really slow for
3001 * using the latter parts of a big fixed buffer - it iterates
3002 * over each segment manually. We can cheat a bit here, because
3003 * we know that:
3004 *
3005 * 1) it's a BVEC iter, we set it up
3006 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3007 * first and last bvec
3008 *
3009 * So just find our index, and adjust the iterator afterwards.
3010 * If the offset is within the first bvec (or the whole first
3011 * bvec, just use iov_iter_advance(). This makes it easier
3012 * since we can just skip the first segment, which may not
3013 * be PAGE_SIZE aligned.
3014 */
3015 const struct bio_vec *bvec = imu->bvec;
3016
3017 if (offset <= bvec->bv_len) {
3018 iov_iter_advance(iter, offset);
3019 } else {
3020 unsigned long seg_skip;
3021
3022 /* skip first vec */
3023 offset -= bvec->bv_len;
3024 seg_skip = 1 + (offset >> PAGE_SHIFT);
3025
3026 iter->bvec = bvec + seg_skip;
3027 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003028 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003029 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003030 }
3031 }
3032
Jens Axboe5e559562019-11-13 16:12:46 -07003033 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003034}
3035
Jens Axboebcda7ba2020-02-23 16:42:51 -07003036static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3037{
3038 if (needs_lock)
3039 mutex_unlock(&ctx->uring_lock);
3040}
3041
3042static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3043{
3044 /*
3045 * "Normal" inline submissions always hold the uring_lock, since we
3046 * grab it from the system call. Same is true for the SQPOLL offload.
3047 * The only exception is when we've detached the request and issue it
3048 * from an async worker thread, grab the lock for that case.
3049 */
3050 if (needs_lock)
3051 mutex_lock(&ctx->uring_lock);
3052}
3053
3054static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3055 int bgid, struct io_buffer *kbuf,
3056 bool needs_lock)
3057{
3058 struct io_buffer *head;
3059
3060 if (req->flags & REQ_F_BUFFER_SELECTED)
3061 return kbuf;
3062
3063 io_ring_submit_lock(req->ctx, needs_lock);
3064
3065 lockdep_assert_held(&req->ctx->uring_lock);
3066
3067 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3068 if (head) {
3069 if (!list_empty(&head->list)) {
3070 kbuf = list_last_entry(&head->list, struct io_buffer,
3071 list);
3072 list_del(&kbuf->list);
3073 } else {
3074 kbuf = head;
3075 idr_remove(&req->ctx->io_buffer_idr, bgid);
3076 }
3077 if (*len > kbuf->len)
3078 *len = kbuf->len;
3079 } else {
3080 kbuf = ERR_PTR(-ENOBUFS);
3081 }
3082
3083 io_ring_submit_unlock(req->ctx, needs_lock);
3084
3085 return kbuf;
3086}
3087
Jens Axboe4d954c22020-02-27 07:31:19 -07003088static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3089 bool needs_lock)
3090{
3091 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003092 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003093
3094 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003095 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003096 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3097 if (IS_ERR(kbuf))
3098 return kbuf;
3099 req->rw.addr = (u64) (unsigned long) kbuf;
3100 req->flags |= REQ_F_BUFFER_SELECTED;
3101 return u64_to_user_ptr(kbuf->addr);
3102}
3103
3104#ifdef CONFIG_COMPAT
3105static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3106 bool needs_lock)
3107{
3108 struct compat_iovec __user *uiov;
3109 compat_ssize_t clen;
3110 void __user *buf;
3111 ssize_t len;
3112
3113 uiov = u64_to_user_ptr(req->rw.addr);
3114 if (!access_ok(uiov, sizeof(*uiov)))
3115 return -EFAULT;
3116 if (__get_user(clen, &uiov->iov_len))
3117 return -EFAULT;
3118 if (clen < 0)
3119 return -EINVAL;
3120
3121 len = clen;
3122 buf = io_rw_buffer_select(req, &len, needs_lock);
3123 if (IS_ERR(buf))
3124 return PTR_ERR(buf);
3125 iov[0].iov_base = buf;
3126 iov[0].iov_len = (compat_size_t) len;
3127 return 0;
3128}
3129#endif
3130
3131static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3132 bool needs_lock)
3133{
3134 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3135 void __user *buf;
3136 ssize_t len;
3137
3138 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3139 return -EFAULT;
3140
3141 len = iov[0].iov_len;
3142 if (len < 0)
3143 return -EINVAL;
3144 buf = io_rw_buffer_select(req, &len, needs_lock);
3145 if (IS_ERR(buf))
3146 return PTR_ERR(buf);
3147 iov[0].iov_base = buf;
3148 iov[0].iov_len = len;
3149 return 0;
3150}
3151
3152static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3153 bool needs_lock)
3154{
Jens Axboedddb3e22020-06-04 11:27:01 -06003155 if (req->flags & REQ_F_BUFFER_SELECTED) {
3156 struct io_buffer *kbuf;
3157
3158 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3159 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3160 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003161 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003162 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003163 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003164 return -EINVAL;
3165
3166#ifdef CONFIG_COMPAT
3167 if (req->ctx->compat)
3168 return io_compat_import(req, iov, needs_lock);
3169#endif
3170
3171 return __io_iov_buffer_select(req, iov, needs_lock);
3172}
3173
Pavel Begunkov2846c482020-11-07 13:16:27 +00003174static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboe8452fd02020-08-18 13:58:33 -07003175 struct iovec **iovec, struct iov_iter *iter,
3176 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003177{
Jens Axboe9adbd452019-12-20 08:45:55 -07003178 void __user *buf = u64_to_user_ptr(req->rw.addr);
3179 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003180 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003181 u8 opcode;
3182
Jens Axboed625c6e2019-12-17 19:53:05 -07003183 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003184 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003185 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003186 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003187 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003188
Jens Axboebcda7ba2020-02-23 16:42:51 -07003189 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003190 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003191 return -EINVAL;
3192
Jens Axboe3a6820f2019-12-22 15:19:35 -07003193 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003194 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003195 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003196 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003197 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003198 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003199 }
3200
Jens Axboe3a6820f2019-12-22 15:19:35 -07003201 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3202 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003203 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003204 }
3205
Jens Axboe4d954c22020-02-27 07:31:19 -07003206 if (req->flags & REQ_F_BUFFER_SELECT) {
3207 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003208 if (!ret) {
3209 ret = (*iovec)->iov_len;
3210 iov_iter_init(iter, rw, *iovec, 1, ret);
3211 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003212 *iovec = NULL;
3213 return ret;
3214 }
3215
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003216 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3217 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003218}
3219
Jens Axboe0fef9482020-08-26 10:36:20 -06003220static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3221{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003222 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003223}
3224
Jens Axboe32960612019-09-23 11:05:34 -06003225/*
3226 * For files that don't have ->read_iter() and ->write_iter(), handle them
3227 * by looping over ->read() or ->write() manually.
3228 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003229static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003230{
Jens Axboe4017eb92020-10-22 14:14:12 -06003231 struct kiocb *kiocb = &req->rw.kiocb;
3232 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003233 ssize_t ret = 0;
3234
3235 /*
3236 * Don't support polled IO through this interface, and we can't
3237 * support non-blocking either. For the latter, this just causes
3238 * the kiocb to be handled from an async context.
3239 */
3240 if (kiocb->ki_flags & IOCB_HIPRI)
3241 return -EOPNOTSUPP;
3242 if (kiocb->ki_flags & IOCB_NOWAIT)
3243 return -EAGAIN;
3244
3245 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003246 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003247 ssize_t nr;
3248
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003249 if (!iov_iter_is_bvec(iter)) {
3250 iovec = iov_iter_iovec(iter);
3251 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003252 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3253 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003254 }
3255
Jens Axboe32960612019-09-23 11:05:34 -06003256 if (rw == READ) {
3257 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003258 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003259 } else {
3260 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003261 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003262 }
3263
3264 if (nr < 0) {
3265 if (!ret)
3266 ret = nr;
3267 break;
3268 }
3269 ret += nr;
3270 if (nr != iovec.iov_len)
3271 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003272 req->rw.len -= nr;
3273 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003274 iov_iter_advance(iter, nr);
3275 }
3276
3277 return ret;
3278}
3279
Jens Axboeff6165b2020-08-13 09:47:43 -06003280static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3281 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003282{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003283 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003284
Jens Axboeff6165b2020-08-13 09:47:43 -06003285 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003286 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003287 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003288 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003289 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003290 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003291 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003292 unsigned iov_off = 0;
3293
3294 rw->iter.iov = rw->fast_iov;
3295 if (iter->iov != fast_iov) {
3296 iov_off = iter->iov - fast_iov;
3297 rw->iter.iov += iov_off;
3298 }
3299 if (rw->fast_iov != fast_iov)
3300 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003301 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003302 } else {
3303 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003304 }
3305}
3306
Jens Axboee8c2bc12020-08-15 18:44:09 -07003307static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003308{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003309 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3310 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3311 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003312}
3313
Jens Axboee8c2bc12020-08-15 18:44:09 -07003314static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003315{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003316 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003317 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003318
Jens Axboee8c2bc12020-08-15 18:44:09 -07003319 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003320}
3321
Jens Axboeff6165b2020-08-13 09:47:43 -06003322static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3323 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003324 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003325{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003326 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003327 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003328 if (!req->async_data) {
3329 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003330 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003331
Jens Axboeff6165b2020-08-13 09:47:43 -06003332 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003333 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003334 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003335}
3336
Pavel Begunkov73debe62020-09-30 22:57:54 +03003337static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003338{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003339 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003340 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003341 ssize_t ret;
3342
Pavel Begunkov2846c482020-11-07 13:16:27 +00003343 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003344 if (unlikely(ret < 0))
3345 return ret;
3346
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003347 iorw->bytes_done = 0;
3348 iorw->free_iovec = iov;
3349 if (iov)
3350 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003351 return 0;
3352}
3353
Pavel Begunkov73debe62020-09-30 22:57:54 +03003354static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003355{
3356 ssize_t ret;
3357
Pavel Begunkova88fc402020-09-30 22:57:53 +03003358 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003359 if (ret)
3360 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003361
Jens Axboe3529d8c2019-12-19 18:24:38 -07003362 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3363 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003364
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003365 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003366 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003367 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003368 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003369}
3370
Jens Axboec1dd91d2020-08-03 16:43:59 -06003371/*
3372 * This is our waitqueue callback handler, registered through lock_page_async()
3373 * when we initially tried to do the IO with the iocb armed our waitqueue.
3374 * This gets called when the page is unlocked, and we generally expect that to
3375 * happen when the page IO is completed and the page is now uptodate. This will
3376 * queue a task_work based retry of the operation, attempting to copy the data
3377 * again. If the latter fails because the page was NOT uptodate, then we will
3378 * do a thread based blocking retry of the operation. That's the unexpected
3379 * slow path.
3380 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003381static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3382 int sync, void *arg)
3383{
3384 struct wait_page_queue *wpq;
3385 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003386 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003387 int ret;
3388
3389 wpq = container_of(wait, struct wait_page_queue, wait);
3390
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003391 if (!wake_page_match(wpq, key))
3392 return 0;
3393
Hao Xuc8d317a2020-09-29 20:00:45 +08003394 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003395 list_del_init(&wait->entry);
3396
Pavel Begunkove7375122020-07-12 20:42:04 +03003397 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003398 percpu_ref_get(&req->ctx->refs);
3399
Jens Axboebcf5a062020-05-22 09:24:42 -06003400 /* submit ref gets dropped, acquire a new one */
3401 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003402 ret = io_req_task_work_add(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003403 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003404 struct task_struct *tsk;
3405
Jens Axboebcf5a062020-05-22 09:24:42 -06003406 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003407 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003408 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003409 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003410 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003411 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003412 return 1;
3413}
3414
Jens Axboec1dd91d2020-08-03 16:43:59 -06003415/*
3416 * This controls whether a given IO request should be armed for async page
3417 * based retry. If we return false here, the request is handed to the async
3418 * worker threads for retry. If we're doing buffered reads on a regular file,
3419 * we prepare a private wait_page_queue entry and retry the operation. This
3420 * will either succeed because the page is now uptodate and unlocked, or it
3421 * will register a callback when the page is unlocked at IO completion. Through
3422 * that callback, io_uring uses task_work to setup a retry of the operation.
3423 * That retry will attempt the buffered read again. The retry will generally
3424 * succeed, or in rare cases where it fails, we then fall back to using the
3425 * async worker threads for a blocking retry.
3426 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003427static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003428{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003429 struct io_async_rw *rw = req->async_data;
3430 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003431 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003432
3433 /* never retry for NOWAIT, we just complete with -EAGAIN */
3434 if (req->flags & REQ_F_NOWAIT)
3435 return false;
3436
Jens Axboe227c0c92020-08-13 11:51:40 -06003437 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003438 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003439 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003440
Jens Axboebcf5a062020-05-22 09:24:42 -06003441 /*
3442 * just use poll if we can, and don't attempt if the fs doesn't
3443 * support callback based unlocks
3444 */
3445 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3446 return false;
3447
Jens Axboe3b2a4432020-08-16 10:58:43 -07003448 wait->wait.func = io_async_buf_func;
3449 wait->wait.private = req;
3450 wait->wait.flags = 0;
3451 INIT_LIST_HEAD(&wait->wait.entry);
3452 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003453 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003454 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003455 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003456}
3457
3458static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3459{
3460 if (req->file->f_op->read_iter)
3461 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003462 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003463 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003464 else
3465 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003466}
3467
Jens Axboea1d7c392020-06-22 11:09:46 -06003468static int io_read(struct io_kiocb *req, bool force_nonblock,
3469 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003470{
3471 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003472 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003473 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003474 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003475 ssize_t io_size, ret, ret2;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003476 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003477
Pavel Begunkov2846c482020-11-07 13:16:27 +00003478 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003479 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003480 iovec = NULL;
3481 } else {
3482 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3483 if (ret < 0)
3484 return ret;
3485 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003486 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003487 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003488 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003489
Jens Axboefd6c2e42019-12-18 12:19:41 -07003490 /* Ensure we clear previously set non-block flag */
3491 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003492 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003493 else
3494 kiocb->ki_flags |= IOCB_NOWAIT;
3495
Jens Axboefd6c2e42019-12-18 12:19:41 -07003496
Pavel Begunkov24c74672020-06-21 13:09:51 +03003497 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003498 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3499 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003500 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003501
Pavel Begunkov632546c2020-11-07 13:16:26 +00003502 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003503 if (unlikely(ret))
3504 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003505
Jens Axboe227c0c92020-08-13 11:51:40 -06003506 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003507
Jens Axboe227c0c92020-08-13 11:51:40 -06003508 if (!ret) {
3509 goto done;
3510 } else if (ret == -EIOCBQUEUED) {
3511 ret = 0;
3512 goto out_free;
3513 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003514 /* IOPOLL retry should happen for io-wq threads */
3515 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003516 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003517 /* no retry on NONBLOCK marked file */
3518 if (req->file->f_flags & O_NONBLOCK)
3519 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003520 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003521 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003522 ret = 0;
3523 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003524 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003525 /* make sure -ERESTARTSYS -> -EINTR is done */
3526 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003527 }
3528
3529 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003530 if (!iov_iter_count(iter) || !force_nonblock ||
3531 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003532 goto done;
3533
3534 io_size -= ret;
3535copy_iov:
3536 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3537 if (ret2) {
3538 ret = ret2;
3539 goto out_free;
3540 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003541 if (no_async)
3542 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003543 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003544 /* it's copied and will be cleaned with ->io */
3545 iovec = NULL;
3546 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003547 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003548retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003549 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003550 /* if we can retry, do so with the callbacks armed */
3551 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003552 kiocb->ki_flags &= ~IOCB_WAITQ;
3553 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003554 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003555
3556 /*
3557 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3558 * get -EIOCBQUEUED, then we'll get a notification when the desired
3559 * page gets unlocked. We can also get a partial read here, and if we
3560 * do, then just retry at the new offset.
3561 */
3562 ret = io_iter_do_read(req, iter);
3563 if (ret == -EIOCBQUEUED) {
3564 ret = 0;
3565 goto out_free;
3566 } else if (ret > 0 && ret < io_size) {
3567 /* we got some bytes, but not all. retry. */
3568 goto retry;
3569 }
3570done:
3571 kiocb_done(kiocb, ret, cs);
3572 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003573out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003574 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003575 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003576 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003577 return ret;
3578}
3579
Pavel Begunkov73debe62020-09-30 22:57:54 +03003580static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003581{
3582 ssize_t ret;
3583
Pavel Begunkova88fc402020-09-30 22:57:53 +03003584 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003585 if (ret)
3586 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003587
Jens Axboe3529d8c2019-12-19 18:24:38 -07003588 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3589 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003590
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003591 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003592 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003593 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003594 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003595}
3596
Jens Axboea1d7c392020-06-22 11:09:46 -06003597static int io_write(struct io_kiocb *req, bool force_nonblock,
3598 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003599{
3600 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003601 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003602 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003603 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003604 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003605
Pavel Begunkov2846c482020-11-07 13:16:27 +00003606 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003607 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003608 iovec = NULL;
3609 } else {
3610 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3611 if (ret < 0)
3612 return ret;
3613 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003614 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003615 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003616
Jens Axboefd6c2e42019-12-18 12:19:41 -07003617 /* Ensure we clear previously set non-block flag */
3618 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003619 kiocb->ki_flags &= ~IOCB_NOWAIT;
3620 else
3621 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003622
Pavel Begunkov24c74672020-06-21 13:09:51 +03003623 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003624 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003625 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003626
Jens Axboe10d59342019-12-09 20:16:22 -07003627 /* file path doesn't support NOWAIT for non-direct_IO */
3628 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3629 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003630 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003631
Pavel Begunkov632546c2020-11-07 13:16:26 +00003632 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003633 if (unlikely(ret))
3634 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003635
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003636 /*
3637 * Open-code file_start_write here to grab freeze protection,
3638 * which will be released by another thread in
3639 * io_complete_rw(). Fool lockdep by telling it the lock got
3640 * released so that it doesn't complain about the held lock when
3641 * we return to userspace.
3642 */
3643 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003644 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003645 __sb_writers_release(file_inode(req->file)->i_sb,
3646 SB_FREEZE_WRITE);
3647 }
3648 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003649
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003650 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003651 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003652 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003653 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003654 else
3655 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003656
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003657 /*
3658 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3659 * retry them without IOCB_NOWAIT.
3660 */
3661 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3662 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003663 /* no retry on NONBLOCK marked file */
3664 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3665 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003666 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003667 /* IOPOLL retry should happen for io-wq threads */
3668 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3669 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003670done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003671 kiocb_done(kiocb, ret2, cs);
3672 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003673copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003674 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003675 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003676 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003677 if (!ret)
3678 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003679 }
Jens Axboe31b51512019-01-18 22:56:34 -07003680out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003681 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003682 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003683 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003684 return ret;
3685}
3686
Jens Axboe80a261f2020-09-28 14:23:58 -06003687static int io_renameat_prep(struct io_kiocb *req,
3688 const struct io_uring_sqe *sqe)
3689{
3690 struct io_rename *ren = &req->rename;
3691 const char __user *oldf, *newf;
3692
3693 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3694 return -EBADF;
3695
3696 ren->old_dfd = READ_ONCE(sqe->fd);
3697 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3698 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3699 ren->new_dfd = READ_ONCE(sqe->len);
3700 ren->flags = READ_ONCE(sqe->rename_flags);
3701
3702 ren->oldpath = getname(oldf);
3703 if (IS_ERR(ren->oldpath))
3704 return PTR_ERR(ren->oldpath);
3705
3706 ren->newpath = getname(newf);
3707 if (IS_ERR(ren->newpath)) {
3708 putname(ren->oldpath);
3709 return PTR_ERR(ren->newpath);
3710 }
3711
3712 req->flags |= REQ_F_NEED_CLEANUP;
3713 return 0;
3714}
3715
3716static int io_renameat(struct io_kiocb *req, bool force_nonblock)
3717{
3718 struct io_rename *ren = &req->rename;
3719 int ret;
3720
3721 if (force_nonblock)
3722 return -EAGAIN;
3723
3724 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3725 ren->newpath, ren->flags);
3726
3727 req->flags &= ~REQ_F_NEED_CLEANUP;
3728 if (ret < 0)
3729 req_set_fail_links(req);
3730 io_req_complete(req, ret);
3731 return 0;
3732}
3733
Jens Axboe14a11432020-09-28 14:27:37 -06003734static int io_unlinkat_prep(struct io_kiocb *req,
3735 const struct io_uring_sqe *sqe)
3736{
3737 struct io_unlink *un = &req->unlink;
3738 const char __user *fname;
3739
3740 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3741 return -EBADF;
3742
3743 un->dfd = READ_ONCE(sqe->fd);
3744
3745 un->flags = READ_ONCE(sqe->unlink_flags);
3746 if (un->flags & ~AT_REMOVEDIR)
3747 return -EINVAL;
3748
3749 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3750 un->filename = getname(fname);
3751 if (IS_ERR(un->filename))
3752 return PTR_ERR(un->filename);
3753
3754 req->flags |= REQ_F_NEED_CLEANUP;
3755 return 0;
3756}
3757
3758static int io_unlinkat(struct io_kiocb *req, bool force_nonblock)
3759{
3760 struct io_unlink *un = &req->unlink;
3761 int ret;
3762
3763 if (force_nonblock)
3764 return -EAGAIN;
3765
3766 if (un->flags & AT_REMOVEDIR)
3767 ret = do_rmdir(un->dfd, un->filename);
3768 else
3769 ret = do_unlinkat(un->dfd, un->filename);
3770
3771 req->flags &= ~REQ_F_NEED_CLEANUP;
3772 if (ret < 0)
3773 req_set_fail_links(req);
3774 io_req_complete(req, ret);
3775 return 0;
3776}
3777
Jens Axboe36f4fa62020-09-05 11:14:22 -06003778static int io_shutdown_prep(struct io_kiocb *req,
3779 const struct io_uring_sqe *sqe)
3780{
3781#if defined(CONFIG_NET)
3782 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3783 return -EINVAL;
3784 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3785 sqe->buf_index)
3786 return -EINVAL;
3787
3788 req->shutdown.how = READ_ONCE(sqe->len);
3789 return 0;
3790#else
3791 return -EOPNOTSUPP;
3792#endif
3793}
3794
3795static int io_shutdown(struct io_kiocb *req, bool force_nonblock)
3796{
3797#if defined(CONFIG_NET)
3798 struct socket *sock;
3799 int ret;
3800
3801 if (force_nonblock)
3802 return -EAGAIN;
3803
Linus Torvalds48aba792020-12-16 12:44:05 -08003804 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003805 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003806 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003807
3808 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003809 if (ret < 0)
3810 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003811 io_req_complete(req, ret);
3812 return 0;
3813#else
3814 return -EOPNOTSUPP;
3815#endif
3816}
3817
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003818static int __io_splice_prep(struct io_kiocb *req,
3819 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003820{
3821 struct io_splice* sp = &req->splice;
3822 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003823
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003824 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3825 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003826
3827 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003828 sp->len = READ_ONCE(sqe->len);
3829 sp->flags = READ_ONCE(sqe->splice_flags);
3830
3831 if (unlikely(sp->flags & ~valid_flags))
3832 return -EINVAL;
3833
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003834 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3835 (sp->flags & SPLICE_F_FD_IN_FIXED));
3836 if (!sp->file_in)
3837 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003838 req->flags |= REQ_F_NEED_CLEANUP;
3839
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003840 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3841 /*
3842 * Splice operation will be punted aync, and here need to
3843 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3844 */
3845 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003846 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003847 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003848
3849 return 0;
3850}
3851
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003852static int io_tee_prep(struct io_kiocb *req,
3853 const struct io_uring_sqe *sqe)
3854{
3855 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3856 return -EINVAL;
3857 return __io_splice_prep(req, sqe);
3858}
3859
3860static int io_tee(struct io_kiocb *req, bool force_nonblock)
3861{
3862 struct io_splice *sp = &req->splice;
3863 struct file *in = sp->file_in;
3864 struct file *out = sp->file_out;
3865 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3866 long ret = 0;
3867
3868 if (force_nonblock)
3869 return -EAGAIN;
3870 if (sp->len)
3871 ret = do_tee(in, out, sp->len, flags);
3872
3873 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3874 req->flags &= ~REQ_F_NEED_CLEANUP;
3875
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003876 if (ret != sp->len)
3877 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003878 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003879 return 0;
3880}
3881
3882static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3883{
3884 struct io_splice* sp = &req->splice;
3885
3886 sp->off_in = READ_ONCE(sqe->splice_off_in);
3887 sp->off_out = READ_ONCE(sqe->off);
3888 return __io_splice_prep(req, sqe);
3889}
3890
Pavel Begunkov014db002020-03-03 21:33:12 +03003891static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003892{
3893 struct io_splice *sp = &req->splice;
3894 struct file *in = sp->file_in;
3895 struct file *out = sp->file_out;
3896 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3897 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003898 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003899
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003900 if (force_nonblock)
3901 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003902
3903 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3904 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003905
Jens Axboe948a7742020-05-17 14:21:38 -06003906 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003907 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003908
3909 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3910 req->flags &= ~REQ_F_NEED_CLEANUP;
3911
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003912 if (ret != sp->len)
3913 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003914 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003915 return 0;
3916}
3917
Jens Axboe2b188cc2019-01-07 10:46:33 -07003918/*
3919 * IORING_OP_NOP just posts a completion event, nothing else.
3920 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003921static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003922{
3923 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003924
Jens Axboedef596e2019-01-09 08:59:42 -07003925 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3926 return -EINVAL;
3927
Jens Axboe229a7b62020-06-22 10:13:11 -06003928 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003929 return 0;
3930}
3931
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003933{
Jens Axboe6b063142019-01-10 22:13:58 -07003934 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003935
Jens Axboe09bb8392019-03-13 12:39:28 -06003936 if (!req->file)
3937 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003938
Jens Axboe6b063142019-01-10 22:13:58 -07003939 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003940 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003941 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003942 return -EINVAL;
3943
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003944 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3945 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3946 return -EINVAL;
3947
3948 req->sync.off = READ_ONCE(sqe->off);
3949 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003950 return 0;
3951}
3952
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003953static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003954{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003955 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003956 int ret;
3957
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003958 /* fsync always requires a blocking context */
3959 if (force_nonblock)
3960 return -EAGAIN;
3961
Jens Axboe9adbd452019-12-20 08:45:55 -07003962 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003963 end > 0 ? end : LLONG_MAX,
3964 req->sync.flags & IORING_FSYNC_DATASYNC);
3965 if (ret < 0)
3966 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003967 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003968 return 0;
3969}
3970
Jens Axboed63d1b52019-12-10 10:38:56 -07003971static int io_fallocate_prep(struct io_kiocb *req,
3972 const struct io_uring_sqe *sqe)
3973{
3974 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3975 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003976 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3977 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003978
3979 req->sync.off = READ_ONCE(sqe->off);
3980 req->sync.len = READ_ONCE(sqe->addr);
3981 req->sync.mode = READ_ONCE(sqe->len);
3982 return 0;
3983}
3984
Pavel Begunkov014db002020-03-03 21:33:12 +03003985static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003986{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003987 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003988
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003989 /* fallocate always requiring blocking context */
3990 if (force_nonblock)
3991 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003992 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3993 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003994 if (ret < 0)
3995 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003996 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003997 return 0;
3998}
3999
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004000static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004001{
Jens Axboef8748882020-01-08 17:47:02 -07004002 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004003 int ret;
4004
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004005 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004006 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004007 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004008 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004009
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004010 /* open.how should be already initialised */
4011 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004012 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004013
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004014 req->open.dfd = READ_ONCE(sqe->fd);
4015 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004016 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004017 if (IS_ERR(req->open.filename)) {
4018 ret = PTR_ERR(req->open.filename);
4019 req->open.filename = NULL;
4020 return ret;
4021 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004022 req->open.nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe944d1442020-11-13 16:48:44 -07004023 req->open.ignore_nonblock = false;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004024 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004025 return 0;
4026}
4027
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004028static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4029{
4030 u64 flags, mode;
4031
Jens Axboe14587a462020-09-05 11:36:08 -06004032 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004033 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004034 mode = READ_ONCE(sqe->len);
4035 flags = READ_ONCE(sqe->open_flags);
4036 req->open.how = build_open_how(flags, mode);
4037 return __io_openat_prep(req, sqe);
4038}
4039
Jens Axboecebdb982020-01-08 17:59:24 -07004040static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4041{
4042 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004043 size_t len;
4044 int ret;
4045
Jens Axboe14587a462020-09-05 11:36:08 -06004046 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004047 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004048 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4049 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004050 if (len < OPEN_HOW_SIZE_VER0)
4051 return -EINVAL;
4052
4053 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4054 len);
4055 if (ret)
4056 return ret;
4057
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004058 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004059}
4060
Pavel Begunkov014db002020-03-03 21:33:12 +03004061static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004062{
4063 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004064 struct file *file;
4065 int ret;
4066
Jens Axboe944d1442020-11-13 16:48:44 -07004067 if (force_nonblock && !req->open.ignore_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004068 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004069
Jens Axboecebdb982020-01-08 17:59:24 -07004070 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004071 if (ret)
4072 goto err;
4073
Jens Axboe4022e7a2020-03-19 19:23:18 -06004074 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004075 if (ret < 0)
4076 goto err;
4077
4078 file = do_filp_open(req->open.dfd, req->open.filename, &op);
4079 if (IS_ERR(file)) {
4080 put_unused_fd(ret);
4081 ret = PTR_ERR(file);
Jens Axboe944d1442020-11-13 16:48:44 -07004082 /*
4083 * A work-around to ensure that /proc/self works that way
4084 * that it should - if we get -EOPNOTSUPP back, then assume
4085 * that proc_self_get_link() failed us because we're in async
4086 * context. We should be safe to retry this from the task
4087 * itself with force_nonblock == false set, as it should not
4088 * block on lookup. Would be nice to know this upfront and
4089 * avoid the async dance, but doesn't seem feasible.
4090 */
4091 if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) {
4092 req->open.ignore_nonblock = true;
4093 refcount_inc(&req->refs);
4094 io_req_task_queue(req);
4095 return 0;
4096 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004097 } else {
4098 fsnotify_open(file);
4099 fd_install(ret, file);
4100 }
4101err:
4102 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004103 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004104 if (ret < 0)
4105 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004106 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004107 return 0;
4108}
4109
Pavel Begunkov014db002020-03-03 21:33:12 +03004110static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07004111{
Pavel Begunkov014db002020-03-03 21:33:12 +03004112 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07004113}
4114
Jens Axboe067524e2020-03-02 16:32:28 -07004115static int io_remove_buffers_prep(struct io_kiocb *req,
4116 const struct io_uring_sqe *sqe)
4117{
4118 struct io_provide_buf *p = &req->pbuf;
4119 u64 tmp;
4120
4121 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4122 return -EINVAL;
4123
4124 tmp = READ_ONCE(sqe->fd);
4125 if (!tmp || tmp > USHRT_MAX)
4126 return -EINVAL;
4127
4128 memset(p, 0, sizeof(*p));
4129 p->nbufs = tmp;
4130 p->bgid = READ_ONCE(sqe->buf_group);
4131 return 0;
4132}
4133
4134static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4135 int bgid, unsigned nbufs)
4136{
4137 unsigned i = 0;
4138
4139 /* shouldn't happen */
4140 if (!nbufs)
4141 return 0;
4142
4143 /* the head kbuf is the list itself */
4144 while (!list_empty(&buf->list)) {
4145 struct io_buffer *nxt;
4146
4147 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4148 list_del(&nxt->list);
4149 kfree(nxt);
4150 if (++i == nbufs)
4151 return i;
4152 }
4153 i++;
4154 kfree(buf);
4155 idr_remove(&ctx->io_buffer_idr, bgid);
4156
4157 return i;
4158}
4159
Jens Axboe229a7b62020-06-22 10:13:11 -06004160static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
4161 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07004162{
4163 struct io_provide_buf *p = &req->pbuf;
4164 struct io_ring_ctx *ctx = req->ctx;
4165 struct io_buffer *head;
4166 int ret = 0;
4167
4168 io_ring_submit_lock(ctx, !force_nonblock);
4169
4170 lockdep_assert_held(&ctx->uring_lock);
4171
4172 ret = -ENOENT;
4173 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4174 if (head)
4175 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004176 if (ret < 0)
4177 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004178
4179 /* need to hold the lock to complete IOPOLL requests */
4180 if (ctx->flags & IORING_SETUP_IOPOLL) {
4181 __io_req_complete(req, ret, 0, cs);
4182 io_ring_submit_unlock(ctx, !force_nonblock);
4183 } else {
4184 io_ring_submit_unlock(ctx, !force_nonblock);
4185 __io_req_complete(req, ret, 0, cs);
4186 }
Jens Axboe067524e2020-03-02 16:32:28 -07004187 return 0;
4188}
4189
Jens Axboeddf0322d2020-02-23 16:41:33 -07004190static int io_provide_buffers_prep(struct io_kiocb *req,
4191 const struct io_uring_sqe *sqe)
4192{
4193 struct io_provide_buf *p = &req->pbuf;
4194 u64 tmp;
4195
4196 if (sqe->ioprio || sqe->rw_flags)
4197 return -EINVAL;
4198
4199 tmp = READ_ONCE(sqe->fd);
4200 if (!tmp || tmp > USHRT_MAX)
4201 return -E2BIG;
4202 p->nbufs = tmp;
4203 p->addr = READ_ONCE(sqe->addr);
4204 p->len = READ_ONCE(sqe->len);
4205
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004206 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004207 return -EFAULT;
4208
4209 p->bgid = READ_ONCE(sqe->buf_group);
4210 tmp = READ_ONCE(sqe->off);
4211 if (tmp > USHRT_MAX)
4212 return -E2BIG;
4213 p->bid = tmp;
4214 return 0;
4215}
4216
4217static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4218{
4219 struct io_buffer *buf;
4220 u64 addr = pbuf->addr;
4221 int i, bid = pbuf->bid;
4222
4223 for (i = 0; i < pbuf->nbufs; i++) {
4224 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4225 if (!buf)
4226 break;
4227
4228 buf->addr = addr;
4229 buf->len = pbuf->len;
4230 buf->bid = bid;
4231 addr += pbuf->len;
4232 bid++;
4233 if (!*head) {
4234 INIT_LIST_HEAD(&buf->list);
4235 *head = buf;
4236 } else {
4237 list_add_tail(&buf->list, &(*head)->list);
4238 }
4239 }
4240
4241 return i ? i : -ENOMEM;
4242}
4243
Jens Axboe229a7b62020-06-22 10:13:11 -06004244static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4245 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004246{
4247 struct io_provide_buf *p = &req->pbuf;
4248 struct io_ring_ctx *ctx = req->ctx;
4249 struct io_buffer *head, *list;
4250 int ret = 0;
4251
4252 io_ring_submit_lock(ctx, !force_nonblock);
4253
4254 lockdep_assert_held(&ctx->uring_lock);
4255
4256 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4257
4258 ret = io_add_buffers(p, &head);
4259 if (ret < 0)
4260 goto out;
4261
4262 if (!list) {
4263 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4264 GFP_KERNEL);
4265 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004266 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004267 goto out;
4268 }
4269 }
4270out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004271 if (ret < 0)
4272 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004273
4274 /* need to hold the lock to complete IOPOLL requests */
4275 if (ctx->flags & IORING_SETUP_IOPOLL) {
4276 __io_req_complete(req, ret, 0, cs);
4277 io_ring_submit_unlock(ctx, !force_nonblock);
4278 } else {
4279 io_ring_submit_unlock(ctx, !force_nonblock);
4280 __io_req_complete(req, ret, 0, cs);
4281 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004282 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004283}
4284
Jens Axboe3e4827b2020-01-08 15:18:09 -07004285static int io_epoll_ctl_prep(struct io_kiocb *req,
4286 const struct io_uring_sqe *sqe)
4287{
4288#if defined(CONFIG_EPOLL)
4289 if (sqe->ioprio || sqe->buf_index)
4290 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004291 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004292 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004293
4294 req->epoll.epfd = READ_ONCE(sqe->fd);
4295 req->epoll.op = READ_ONCE(sqe->len);
4296 req->epoll.fd = READ_ONCE(sqe->off);
4297
4298 if (ep_op_has_event(req->epoll.op)) {
4299 struct epoll_event __user *ev;
4300
4301 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4302 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4303 return -EFAULT;
4304 }
4305
4306 return 0;
4307#else
4308 return -EOPNOTSUPP;
4309#endif
4310}
4311
Jens Axboe229a7b62020-06-22 10:13:11 -06004312static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4313 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004314{
4315#if defined(CONFIG_EPOLL)
4316 struct io_epoll *ie = &req->epoll;
4317 int ret;
4318
4319 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4320 if (force_nonblock && ret == -EAGAIN)
4321 return -EAGAIN;
4322
4323 if (ret < 0)
4324 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004325 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004326 return 0;
4327#else
4328 return -EOPNOTSUPP;
4329#endif
4330}
4331
Jens Axboec1ca7572019-12-25 22:18:28 -07004332static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4333{
4334#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4335 if (sqe->ioprio || sqe->buf_index || sqe->off)
4336 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004337 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4338 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004339
4340 req->madvise.addr = READ_ONCE(sqe->addr);
4341 req->madvise.len = READ_ONCE(sqe->len);
4342 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4343 return 0;
4344#else
4345 return -EOPNOTSUPP;
4346#endif
4347}
4348
Pavel Begunkov014db002020-03-03 21:33:12 +03004349static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004350{
4351#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4352 struct io_madvise *ma = &req->madvise;
4353 int ret;
4354
4355 if (force_nonblock)
4356 return -EAGAIN;
4357
Minchan Kim0726b012020-10-17 16:14:50 -07004358 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004359 if (ret < 0)
4360 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004361 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004362 return 0;
4363#else
4364 return -EOPNOTSUPP;
4365#endif
4366}
4367
Jens Axboe4840e412019-12-25 22:03:45 -07004368static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4369{
4370 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4371 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004372 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4373 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004374
4375 req->fadvise.offset = READ_ONCE(sqe->off);
4376 req->fadvise.len = READ_ONCE(sqe->len);
4377 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4378 return 0;
4379}
4380
Pavel Begunkov014db002020-03-03 21:33:12 +03004381static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004382{
4383 struct io_fadvise *fa = &req->fadvise;
4384 int ret;
4385
Jens Axboe3e694262020-02-01 09:22:49 -07004386 if (force_nonblock) {
4387 switch (fa->advice) {
4388 case POSIX_FADV_NORMAL:
4389 case POSIX_FADV_RANDOM:
4390 case POSIX_FADV_SEQUENTIAL:
4391 break;
4392 default:
4393 return -EAGAIN;
4394 }
4395 }
Jens Axboe4840e412019-12-25 22:03:45 -07004396
4397 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4398 if (ret < 0)
4399 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004400 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004401 return 0;
4402}
4403
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004404static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4405{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004406 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004407 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004408 if (sqe->ioprio || sqe->buf_index)
4409 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004410 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004411 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004412
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004413 req->statx.dfd = READ_ONCE(sqe->fd);
4414 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004415 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004416 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4417 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004418
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004419 return 0;
4420}
4421
Pavel Begunkov014db002020-03-03 21:33:12 +03004422static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004423{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004424 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004425 int ret;
4426
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004427 if (force_nonblock) {
4428 /* only need file table for an actual valid fd */
4429 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4430 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004431 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004432 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004433
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004434 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4435 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004436
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004437 if (ret < 0)
4438 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004439 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004440 return 0;
4441}
4442
Jens Axboeb5dba592019-12-11 14:02:38 -07004443static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4444{
4445 /*
4446 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004447 * leave the 'file' in an undeterminate state, and here need to modify
4448 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004449 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004450 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004451 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4452
Jens Axboe14587a462020-09-05 11:36:08 -06004453 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004454 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004455 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4456 sqe->rw_flags || sqe->buf_index)
4457 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004458 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004459 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004460
4461 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004462 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004463 return -EBADF;
4464
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004465 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004466 return 0;
4467}
4468
Jens Axboe229a7b62020-06-22 10:13:11 -06004469static int io_close(struct io_kiocb *req, bool force_nonblock,
4470 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004471{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004472 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004473 int ret;
4474
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004475 /* might be already done during nonblock submission */
4476 if (!close->put_file) {
Eric W. Biederman9fe83c42020-11-20 17:14:40 -06004477 ret = close_fd_get_file(close->fd, &close->put_file);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004478 if (ret < 0)
4479 return (ret == -ENOENT) ? -EBADF : ret;
4480 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004481
4482 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004483 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004484 /* was never set, but play safe */
4485 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004486 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004487 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004488 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004489 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004490
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004491 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004492 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004493 if (ret < 0)
4494 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004495 fput(close->put_file);
4496 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004497 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004498 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004499}
4500
Jens Axboe3529d8c2019-12-19 18:24:38 -07004501static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004502{
4503 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004504
4505 if (!req->file)
4506 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004507
4508 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4509 return -EINVAL;
4510 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4511 return -EINVAL;
4512
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004513 req->sync.off = READ_ONCE(sqe->off);
4514 req->sync.len = READ_ONCE(sqe->len);
4515 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004516 return 0;
4517}
4518
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004519static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004520{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004521 int ret;
4522
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004523 /* sync_file_range always requires a blocking context */
4524 if (force_nonblock)
4525 return -EAGAIN;
4526
Jens Axboe9adbd452019-12-20 08:45:55 -07004527 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004528 req->sync.flags);
4529 if (ret < 0)
4530 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004531 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004532 return 0;
4533}
4534
YueHaibing469956e2020-03-04 15:53:52 +08004535#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004536static int io_setup_async_msg(struct io_kiocb *req,
4537 struct io_async_msghdr *kmsg)
4538{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004539 struct io_async_msghdr *async_msg = req->async_data;
4540
4541 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004542 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004543 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004544 if (kmsg->iov != kmsg->fast_iov)
4545 kfree(kmsg->iov);
4546 return -ENOMEM;
4547 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004548 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004549 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004550 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004551 return -EAGAIN;
4552}
4553
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004554static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4555 struct io_async_msghdr *iomsg)
4556{
4557 iomsg->iov = iomsg->fast_iov;
4558 iomsg->msg.msg_name = &iomsg->addr;
4559 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4560 req->sr_msg.msg_flags, &iomsg->iov);
4561}
4562
Jens Axboe3529d8c2019-12-19 18:24:38 -07004563static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004564{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004565 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004566 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004567 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004568
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004569 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4570 return -EINVAL;
4571
Jens Axboee47293f2019-12-20 08:58:21 -07004572 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004573 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004574 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004575
Jens Axboed8768362020-02-27 14:17:49 -07004576#ifdef CONFIG_COMPAT
4577 if (req->ctx->compat)
4578 sr->msg_flags |= MSG_CMSG_COMPAT;
4579#endif
4580
Jens Axboee8c2bc12020-08-15 18:44:09 -07004581 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004582 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004583 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004584 if (!ret)
4585 req->flags |= REQ_F_NEED_CLEANUP;
4586 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004587}
4588
Jens Axboe229a7b62020-06-22 10:13:11 -06004589static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4590 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004591{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004592 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004593 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004594 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004595 int ret;
4596
Florent Revestdba4a922020-12-04 12:36:04 +01004597 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004598 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004599 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004600
Jens Axboee8c2bc12020-08-15 18:44:09 -07004601 if (req->async_data) {
4602 kmsg = req->async_data;
4603 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004604 /* if iov is set, it's allocated already */
4605 if (!kmsg->iov)
4606 kmsg->iov = kmsg->fast_iov;
4607 kmsg->msg.msg_iter.iov = kmsg->iov;
4608 } else {
4609 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004610 if (ret)
4611 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004612 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004613 }
4614
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004615 flags = req->sr_msg.msg_flags;
4616 if (flags & MSG_DONTWAIT)
4617 req->flags |= REQ_F_NOWAIT;
4618 else if (force_nonblock)
4619 flags |= MSG_DONTWAIT;
4620
4621 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4622 if (force_nonblock && ret == -EAGAIN)
4623 return io_setup_async_msg(req, kmsg);
4624 if (ret == -ERESTARTSYS)
4625 ret = -EINTR;
4626
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004627 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004628 kfree(kmsg->iov);
4629 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004630 if (ret < 0)
4631 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004632 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004633 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004634}
4635
Jens Axboe229a7b62020-06-22 10:13:11 -06004636static int io_send(struct io_kiocb *req, bool force_nonblock,
4637 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004638{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004639 struct io_sr_msg *sr = &req->sr_msg;
4640 struct msghdr msg;
4641 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004642 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004643 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004644 int ret;
4645
Florent Revestdba4a922020-12-04 12:36:04 +01004646 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004647 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004648 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004649
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004650 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4651 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004652 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004653
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004654 msg.msg_name = NULL;
4655 msg.msg_control = NULL;
4656 msg.msg_controllen = 0;
4657 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004658
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004659 flags = req->sr_msg.msg_flags;
4660 if (flags & MSG_DONTWAIT)
4661 req->flags |= REQ_F_NOWAIT;
4662 else if (force_nonblock)
4663 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004664
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004665 msg.msg_flags = flags;
4666 ret = sock_sendmsg(sock, &msg);
4667 if (force_nonblock && ret == -EAGAIN)
4668 return -EAGAIN;
4669 if (ret == -ERESTARTSYS)
4670 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004671
Jens Axboe03b12302019-12-02 18:50:25 -07004672 if (ret < 0)
4673 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004674 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004675 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004676}
4677
Pavel Begunkov1400e692020-07-12 20:41:05 +03004678static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4679 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004680{
4681 struct io_sr_msg *sr = &req->sr_msg;
4682 struct iovec __user *uiov;
4683 size_t iov_len;
4684 int ret;
4685
Pavel Begunkov1400e692020-07-12 20:41:05 +03004686 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4687 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004688 if (ret)
4689 return ret;
4690
4691 if (req->flags & REQ_F_BUFFER_SELECT) {
4692 if (iov_len > 1)
4693 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004694 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004695 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004696 sr->len = iomsg->iov[0].iov_len;
4697 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004698 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004699 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004700 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004701 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4702 &iomsg->iov, &iomsg->msg.msg_iter,
4703 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004704 if (ret > 0)
4705 ret = 0;
4706 }
4707
4708 return ret;
4709}
4710
4711#ifdef CONFIG_COMPAT
4712static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004713 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004714{
4715 struct compat_msghdr __user *msg_compat;
4716 struct io_sr_msg *sr = &req->sr_msg;
4717 struct compat_iovec __user *uiov;
4718 compat_uptr_t ptr;
4719 compat_size_t len;
4720 int ret;
4721
Pavel Begunkov270a5942020-07-12 20:41:04 +03004722 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004723 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004724 &ptr, &len);
4725 if (ret)
4726 return ret;
4727
4728 uiov = compat_ptr(ptr);
4729 if (req->flags & REQ_F_BUFFER_SELECT) {
4730 compat_ssize_t clen;
4731
4732 if (len > 1)
4733 return -EINVAL;
4734 if (!access_ok(uiov, sizeof(*uiov)))
4735 return -EFAULT;
4736 if (__get_user(clen, &uiov->iov_len))
4737 return -EFAULT;
4738 if (clen < 0)
4739 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004740 sr->len = clen;
4741 iomsg->iov[0].iov_len = clen;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004742 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004743 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004744 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4745 UIO_FASTIOV, &iomsg->iov,
4746 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004747 if (ret < 0)
4748 return ret;
4749 }
4750
4751 return 0;
4752}
Jens Axboe03b12302019-12-02 18:50:25 -07004753#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004754
Pavel Begunkov1400e692020-07-12 20:41:05 +03004755static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4756 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004757{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004758 iomsg->msg.msg_name = &iomsg->addr;
4759 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004760
4761#ifdef CONFIG_COMPAT
4762 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004763 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004764#endif
4765
Pavel Begunkov1400e692020-07-12 20:41:05 +03004766 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004767}
4768
Jens Axboebcda7ba2020-02-23 16:42:51 -07004769static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004770 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004771{
4772 struct io_sr_msg *sr = &req->sr_msg;
4773 struct io_buffer *kbuf;
4774
Jens Axboebcda7ba2020-02-23 16:42:51 -07004775 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4776 if (IS_ERR(kbuf))
4777 return kbuf;
4778
4779 sr->kbuf = kbuf;
4780 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004781 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004782}
4783
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004784static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4785{
4786 return io_put_kbuf(req, req->sr_msg.kbuf);
4787}
4788
Jens Axboe3529d8c2019-12-19 18:24:38 -07004789static int io_recvmsg_prep(struct io_kiocb *req,
4790 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004791{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004792 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004793 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004794 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004795
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004796 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4797 return -EINVAL;
4798
Jens Axboe3529d8c2019-12-19 18:24:38 -07004799 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004800 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004801 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004802 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004803
Jens Axboed8768362020-02-27 14:17:49 -07004804#ifdef CONFIG_COMPAT
4805 if (req->ctx->compat)
4806 sr->msg_flags |= MSG_CMSG_COMPAT;
4807#endif
4808
Jens Axboee8c2bc12020-08-15 18:44:09 -07004809 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004810 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004811 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004812 if (!ret)
4813 req->flags |= REQ_F_NEED_CLEANUP;
4814 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004815}
4816
Jens Axboe229a7b62020-06-22 10:13:11 -06004817static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4818 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004819{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004820 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004821 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004822 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004823 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004824 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004825
Florent Revestdba4a922020-12-04 12:36:04 +01004826 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004827 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004828 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004829
Jens Axboee8c2bc12020-08-15 18:44:09 -07004830 if (req->async_data) {
4831 kmsg = req->async_data;
4832 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004833 /* if iov is set, it's allocated already */
4834 if (!kmsg->iov)
4835 kmsg->iov = kmsg->fast_iov;
4836 kmsg->msg.msg_iter.iov = kmsg->iov;
4837 } else {
4838 ret = io_recvmsg_copy_hdr(req, &iomsg);
4839 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004840 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004841 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004842 }
4843
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004844 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004845 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004846 if (IS_ERR(kbuf))
4847 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004848 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4849 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4850 1, req->sr_msg.len);
4851 }
4852
4853 flags = req->sr_msg.msg_flags;
4854 if (flags & MSG_DONTWAIT)
4855 req->flags |= REQ_F_NOWAIT;
4856 else if (force_nonblock)
4857 flags |= MSG_DONTWAIT;
4858
4859 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4860 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004861 if (force_nonblock && ret == -EAGAIN)
4862 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004863 if (ret == -ERESTARTSYS)
4864 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004865
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004866 if (req->flags & REQ_F_BUFFER_SELECTED)
4867 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004868 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004869 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004870 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004871 if (ret < 0)
4872 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004873 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004874 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004875}
4876
Jens Axboe229a7b62020-06-22 10:13:11 -06004877static int io_recv(struct io_kiocb *req, bool force_nonblock,
4878 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004879{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004880 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004881 struct io_sr_msg *sr = &req->sr_msg;
4882 struct msghdr msg;
4883 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004884 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004885 struct iovec iov;
4886 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004887 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004888
Florent Revestdba4a922020-12-04 12:36:04 +01004889 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004890 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004891 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004892
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004893 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004894 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004895 if (IS_ERR(kbuf))
4896 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004897 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004898 }
4899
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004900 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004901 if (unlikely(ret))
4902 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004903
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004904 msg.msg_name = NULL;
4905 msg.msg_control = NULL;
4906 msg.msg_controllen = 0;
4907 msg.msg_namelen = 0;
4908 msg.msg_iocb = NULL;
4909 msg.msg_flags = 0;
4910
4911 flags = req->sr_msg.msg_flags;
4912 if (flags & MSG_DONTWAIT)
4913 req->flags |= REQ_F_NOWAIT;
4914 else if (force_nonblock)
4915 flags |= MSG_DONTWAIT;
4916
4917 ret = sock_recvmsg(sock, &msg, flags);
4918 if (force_nonblock && ret == -EAGAIN)
4919 return -EAGAIN;
4920 if (ret == -ERESTARTSYS)
4921 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004922out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004923 if (req->flags & REQ_F_BUFFER_SELECTED)
4924 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004925 if (ret < 0)
4926 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004927 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004928 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004929}
4930
Jens Axboe3529d8c2019-12-19 18:24:38 -07004931static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004932{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004933 struct io_accept *accept = &req->accept;
4934
Jens Axboe14587a462020-09-05 11:36:08 -06004935 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004936 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004937 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004938 return -EINVAL;
4939
Jens Axboed55e5f52019-12-11 16:12:15 -07004940 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4941 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004942 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004943 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004944 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004945}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004946
Jens Axboe229a7b62020-06-22 10:13:11 -06004947static int io_accept(struct io_kiocb *req, bool force_nonblock,
4948 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004949{
4950 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004951 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004952 int ret;
4953
Jiufei Xuee697dee2020-06-10 13:41:59 +08004954 if (req->file->f_flags & O_NONBLOCK)
4955 req->flags |= REQ_F_NOWAIT;
4956
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004957 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004958 accept->addr_len, accept->flags,
4959 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004960 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004961 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004962 if (ret < 0) {
4963 if (ret == -ERESTARTSYS)
4964 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004965 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004966 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004967 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004968 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004969}
4970
Jens Axboe3529d8c2019-12-19 18:24:38 -07004971static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004972{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004973 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004974 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004975
Jens Axboe14587a462020-09-05 11:36:08 -06004976 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004977 return -EINVAL;
4978 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4979 return -EINVAL;
4980
Jens Axboe3529d8c2019-12-19 18:24:38 -07004981 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4982 conn->addr_len = READ_ONCE(sqe->addr2);
4983
4984 if (!io)
4985 return 0;
4986
4987 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004988 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004989}
4990
Jens Axboe229a7b62020-06-22 10:13:11 -06004991static int io_connect(struct io_kiocb *req, bool force_nonblock,
4992 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004993{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004994 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004995 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004996 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004997
Jens Axboee8c2bc12020-08-15 18:44:09 -07004998 if (req->async_data) {
4999 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005000 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005001 ret = move_addr_to_kernel(req->connect.addr,
5002 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005003 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005004 if (ret)
5005 goto out;
5006 io = &__io;
5007 }
5008
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005009 file_flags = force_nonblock ? O_NONBLOCK : 0;
5010
Jens Axboee8c2bc12020-08-15 18:44:09 -07005011 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005012 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005013 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005014 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005015 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005016 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005017 ret = -ENOMEM;
5018 goto out;
5019 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005020 io = req->async_data;
5021 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005022 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005023 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005024 if (ret == -ERESTARTSYS)
5025 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005026out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005027 if (ret < 0)
5028 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005029 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005030 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005031}
YueHaibing469956e2020-03-04 15:53:52 +08005032#else /* !CONFIG_NET */
5033static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5034{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005035 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005036}
5037
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005038static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
5039 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005040{
YueHaibing469956e2020-03-04 15:53:52 +08005041 return -EOPNOTSUPP;
5042}
5043
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005044static int io_send(struct io_kiocb *req, bool force_nonblock,
5045 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005046{
5047 return -EOPNOTSUPP;
5048}
5049
5050static int io_recvmsg_prep(struct io_kiocb *req,
5051 const struct io_uring_sqe *sqe)
5052{
5053 return -EOPNOTSUPP;
5054}
5055
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005056static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
5057 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005058{
5059 return -EOPNOTSUPP;
5060}
5061
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005062static int io_recv(struct io_kiocb *req, bool force_nonblock,
5063 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005064{
5065 return -EOPNOTSUPP;
5066}
5067
5068static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5069{
5070 return -EOPNOTSUPP;
5071}
5072
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005073static int io_accept(struct io_kiocb *req, bool force_nonblock,
5074 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005075{
5076 return -EOPNOTSUPP;
5077}
5078
5079static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5080{
5081 return -EOPNOTSUPP;
5082}
5083
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005084static int io_connect(struct io_kiocb *req, bool force_nonblock,
5085 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005086{
5087 return -EOPNOTSUPP;
5088}
5089#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005090
Jens Axboed7718a92020-02-14 22:23:12 -07005091struct io_poll_table {
5092 struct poll_table_struct pt;
5093 struct io_kiocb *req;
5094 int error;
5095};
5096
Jens Axboed7718a92020-02-14 22:23:12 -07005097static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5098 __poll_t mask, task_work_func_t func)
5099{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005100 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005101
5102 /* for instances that support it check for an event match first: */
5103 if (mask && !(mask & poll->events))
5104 return 0;
5105
5106 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5107
5108 list_del_init(&poll->wait.entry);
5109
Jens Axboed7718a92020-02-14 22:23:12 -07005110 req->result = mask;
5111 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005112 percpu_ref_get(&req->ctx->refs);
5113
Jens Axboed7718a92020-02-14 22:23:12 -07005114 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005115 * If this fails, then the task is exiting. When a task exits, the
5116 * work gets canceled, so just cancel this request as well instead
5117 * of executing it. We can't safely execute it anyway, as we may not
5118 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005119 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005120 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005121 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06005122 struct task_struct *tsk;
5123
Jens Axboee3aabf92020-05-18 11:04:17 -06005124 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005125 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06005126 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06005127 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005128 }
Jens Axboed7718a92020-02-14 22:23:12 -07005129 return 1;
5130}
5131
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005132static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5133 __acquires(&req->ctx->completion_lock)
5134{
5135 struct io_ring_ctx *ctx = req->ctx;
5136
5137 if (!req->result && !READ_ONCE(poll->canceled)) {
5138 struct poll_table_struct pt = { ._key = poll->events };
5139
5140 req->result = vfs_poll(req->file, &pt) & poll->events;
5141 }
5142
5143 spin_lock_irq(&ctx->completion_lock);
5144 if (!req->result && !READ_ONCE(poll->canceled)) {
5145 add_wait_queue(poll->head, &poll->wait);
5146 return true;
5147 }
5148
5149 return false;
5150}
5151
Jens Axboed4e7cd32020-08-15 11:44:50 -07005152static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005153{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005154 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005155 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005156 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005157 return req->apoll->double_poll;
5158}
5159
5160static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5161{
5162 if (req->opcode == IORING_OP_POLL_ADD)
5163 return &req->poll;
5164 return &req->apoll->poll;
5165}
5166
5167static void io_poll_remove_double(struct io_kiocb *req)
5168{
5169 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005170
5171 lockdep_assert_held(&req->ctx->completion_lock);
5172
5173 if (poll && poll->head) {
5174 struct wait_queue_head *head = poll->head;
5175
5176 spin_lock(&head->lock);
5177 list_del_init(&poll->wait.entry);
5178 if (poll->wait.private)
5179 refcount_dec(&req->refs);
5180 poll->head = NULL;
5181 spin_unlock(&head->lock);
5182 }
5183}
5184
5185static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5186{
5187 struct io_ring_ctx *ctx = req->ctx;
5188
Jens Axboed4e7cd32020-08-15 11:44:50 -07005189 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005190 req->poll.done = true;
5191 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5192 io_commit_cqring(ctx);
5193}
5194
Jens Axboe18bceab2020-05-15 11:56:54 -06005195static void io_poll_task_func(struct callback_head *cb)
5196{
5197 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005198 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005199 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005200
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005201 if (io_poll_rewait(req, &req->poll)) {
5202 spin_unlock_irq(&ctx->completion_lock);
5203 } else {
5204 hash_del(&req->hash_node);
5205 io_poll_complete(req, req->result, 0);
5206 spin_unlock_irq(&ctx->completion_lock);
5207
5208 nxt = io_put_req_find_next(req);
5209 io_cqring_ev_posted(ctx);
5210 if (nxt)
5211 __io_req_task_submit(nxt);
5212 }
5213
Jens Axboe6d816e02020-08-11 08:04:14 -06005214 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005215}
5216
5217static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5218 int sync, void *key)
5219{
5220 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005221 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005222 __poll_t mask = key_to_poll(key);
5223
5224 /* for instances that support it check for an event match first: */
5225 if (mask && !(mask & poll->events))
5226 return 0;
5227
Jens Axboe8706e042020-09-28 08:38:54 -06005228 list_del_init(&wait->entry);
5229
Jens Axboe807abcb2020-07-17 17:09:27 -06005230 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005231 bool done;
5232
Jens Axboe807abcb2020-07-17 17:09:27 -06005233 spin_lock(&poll->head->lock);
5234 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005235 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005236 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005237 /* make sure double remove sees this as being gone */
5238 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005239 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005240 if (!done) {
5241 /* use wait func handler, so it matches the rq type */
5242 poll->wait.func(&poll->wait, mode, sync, key);
5243 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005244 }
5245 refcount_dec(&req->refs);
5246 return 1;
5247}
5248
5249static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5250 wait_queue_func_t wake_func)
5251{
5252 poll->head = NULL;
5253 poll->done = false;
5254 poll->canceled = false;
5255 poll->events = events;
5256 INIT_LIST_HEAD(&poll->wait.entry);
5257 init_waitqueue_func_entry(&poll->wait, wake_func);
5258}
5259
5260static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005261 struct wait_queue_head *head,
5262 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005263{
5264 struct io_kiocb *req = pt->req;
5265
5266 /*
5267 * If poll->head is already set, it's because the file being polled
5268 * uses multiple waitqueues for poll handling (eg one for read, one
5269 * for write). Setup a separate io_poll_iocb if this happens.
5270 */
5271 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005272 struct io_poll_iocb *poll_one = poll;
5273
Jens Axboe18bceab2020-05-15 11:56:54 -06005274 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005275 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005276 pt->error = -EINVAL;
5277 return;
5278 }
5279 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5280 if (!poll) {
5281 pt->error = -ENOMEM;
5282 return;
5283 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005284 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005285 refcount_inc(&req->refs);
5286 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005287 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005288 }
5289
5290 pt->error = 0;
5291 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005292
5293 if (poll->events & EPOLLEXCLUSIVE)
5294 add_wait_queue_exclusive(head, &poll->wait);
5295 else
5296 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005297}
5298
5299static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5300 struct poll_table_struct *p)
5301{
5302 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005303 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005304
Jens Axboe807abcb2020-07-17 17:09:27 -06005305 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005306}
5307
Jens Axboed7718a92020-02-14 22:23:12 -07005308static void io_async_task_func(struct callback_head *cb)
5309{
5310 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5311 struct async_poll *apoll = req->apoll;
5312 struct io_ring_ctx *ctx = req->ctx;
5313
5314 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5315
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005316 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005317 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005318 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005319 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005320 }
5321
Jens Axboe31067252020-05-17 17:43:31 -06005322 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005323 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005324 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005325
Jens Axboed4e7cd32020-08-15 11:44:50 -07005326 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005327 spin_unlock_irq(&ctx->completion_lock);
5328
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005329 if (!READ_ONCE(apoll->poll.canceled))
5330 __io_req_task_submit(req);
5331 else
5332 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005333
Jens Axboe6d816e02020-08-11 08:04:14 -06005334 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005335 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005336 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005337}
5338
5339static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5340 void *key)
5341{
5342 struct io_kiocb *req = wait->private;
5343 struct io_poll_iocb *poll = &req->apoll->poll;
5344
5345 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5346 key_to_poll(key));
5347
5348 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5349}
5350
5351static void io_poll_req_insert(struct io_kiocb *req)
5352{
5353 struct io_ring_ctx *ctx = req->ctx;
5354 struct hlist_head *list;
5355
5356 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5357 hlist_add_head(&req->hash_node, list);
5358}
5359
5360static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5361 struct io_poll_iocb *poll,
5362 struct io_poll_table *ipt, __poll_t mask,
5363 wait_queue_func_t wake_func)
5364 __acquires(&ctx->completion_lock)
5365{
5366 struct io_ring_ctx *ctx = req->ctx;
5367 bool cancel = false;
5368
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005369 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005370 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005371 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005372 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005373
5374 ipt->pt._key = mask;
5375 ipt->req = req;
5376 ipt->error = -EINVAL;
5377
Jens Axboed7718a92020-02-14 22:23:12 -07005378 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5379
5380 spin_lock_irq(&ctx->completion_lock);
5381 if (likely(poll->head)) {
5382 spin_lock(&poll->head->lock);
5383 if (unlikely(list_empty(&poll->wait.entry))) {
5384 if (ipt->error)
5385 cancel = true;
5386 ipt->error = 0;
5387 mask = 0;
5388 }
5389 if (mask || ipt->error)
5390 list_del_init(&poll->wait.entry);
5391 else if (cancel)
5392 WRITE_ONCE(poll->canceled, true);
5393 else if (!poll->done) /* actually waiting for an event */
5394 io_poll_req_insert(req);
5395 spin_unlock(&poll->head->lock);
5396 }
5397
5398 return mask;
5399}
5400
5401static bool io_arm_poll_handler(struct io_kiocb *req)
5402{
5403 const struct io_op_def *def = &io_op_defs[req->opcode];
5404 struct io_ring_ctx *ctx = req->ctx;
5405 struct async_poll *apoll;
5406 struct io_poll_table ipt;
5407 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005408 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005409
5410 if (!req->file || !file_can_poll(req->file))
5411 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005412 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005413 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005414 if (def->pollin)
5415 rw = READ;
5416 else if (def->pollout)
5417 rw = WRITE;
5418 else
5419 return false;
5420 /* if we can't nonblock try, then no point in arming a poll handler */
5421 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005422 return false;
5423
5424 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5425 if (unlikely(!apoll))
5426 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005427 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005428
5429 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005430 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005431
Nathan Chancellor8755d972020-03-02 16:01:19 -07005432 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005433 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005434 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005435 if (def->pollout)
5436 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005437
5438 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5439 if ((req->opcode == IORING_OP_RECVMSG) &&
5440 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5441 mask &= ~POLLIN;
5442
Jens Axboed7718a92020-02-14 22:23:12 -07005443 mask |= POLLERR | POLLPRI;
5444
5445 ipt.pt._qproc = io_async_queue_proc;
5446
5447 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5448 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005449 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005450 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005451 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005452 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005453 kfree(apoll);
5454 return false;
5455 }
5456 spin_unlock_irq(&ctx->completion_lock);
5457 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5458 apoll->poll.events);
5459 return true;
5460}
5461
5462static bool __io_poll_remove_one(struct io_kiocb *req,
5463 struct io_poll_iocb *poll)
5464{
Jens Axboeb41e9852020-02-17 09:52:41 -07005465 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005466
5467 spin_lock(&poll->head->lock);
5468 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005469 if (!list_empty(&poll->wait.entry)) {
5470 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005471 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005472 }
5473 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005474 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005475 return do_complete;
5476}
5477
5478static bool io_poll_remove_one(struct io_kiocb *req)
5479{
5480 bool do_complete;
5481
Jens Axboed4e7cd32020-08-15 11:44:50 -07005482 io_poll_remove_double(req);
5483
Jens Axboed7718a92020-02-14 22:23:12 -07005484 if (req->opcode == IORING_OP_POLL_ADD) {
5485 do_complete = __io_poll_remove_one(req, &req->poll);
5486 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005487 struct async_poll *apoll = req->apoll;
5488
Jens Axboed7718a92020-02-14 22:23:12 -07005489 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005490 do_complete = __io_poll_remove_one(req, &apoll->poll);
5491 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005492 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005493 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005494 kfree(apoll);
5495 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005496 }
5497
Jens Axboeb41e9852020-02-17 09:52:41 -07005498 if (do_complete) {
5499 io_cqring_fill_event(req, -ECANCELED);
5500 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005501 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005502 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005503 }
5504
5505 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005506}
5507
Jens Axboe76e1b642020-09-26 15:05:03 -06005508/*
5509 * Returns true if we found and killed one or more poll requests
5510 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005511static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5512 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005513{
Jens Axboe78076bb2019-12-04 19:56:40 -07005514 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005515 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005516 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005517
5518 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005519 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5520 struct hlist_head *list;
5521
5522 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005523 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005524 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005525 posted += io_poll_remove_one(req);
5526 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005527 }
5528 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005529
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005530 if (posted)
5531 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005532
5533 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005534}
5535
Jens Axboe47f46762019-11-09 17:43:02 -07005536static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5537{
Jens Axboe78076bb2019-12-04 19:56:40 -07005538 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005539 struct io_kiocb *req;
5540
Jens Axboe78076bb2019-12-04 19:56:40 -07005541 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5542 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005543 if (sqe_addr != req->user_data)
5544 continue;
5545 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005546 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005547 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005548 }
5549
5550 return -ENOENT;
5551}
5552
Jens Axboe3529d8c2019-12-19 18:24:38 -07005553static int io_poll_remove_prep(struct io_kiocb *req,
5554 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005555{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005556 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5557 return -EINVAL;
5558 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5559 sqe->poll_events)
5560 return -EINVAL;
5561
Pavel Begunkov018043b2020-10-27 23:17:18 +00005562 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005563 return 0;
5564}
5565
5566/*
5567 * Find a running poll command that matches one specified in sqe->addr,
5568 * and remove it if found.
5569 */
5570static int io_poll_remove(struct io_kiocb *req)
5571{
5572 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005573 int ret;
5574
Jens Axboe221c5eb2019-01-17 09:41:58 -07005575 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005576 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005577 spin_unlock_irq(&ctx->completion_lock);
5578
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005579 if (ret < 0)
5580 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005581 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005582 return 0;
5583}
5584
Jens Axboe221c5eb2019-01-17 09:41:58 -07005585static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5586 void *key)
5587{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005588 struct io_kiocb *req = wait->private;
5589 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005590
Jens Axboed7718a92020-02-14 22:23:12 -07005591 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005592}
5593
Jens Axboe221c5eb2019-01-17 09:41:58 -07005594static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5595 struct poll_table_struct *p)
5596{
5597 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5598
Jens Axboee8c2bc12020-08-15 18:44:09 -07005599 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005600}
5601
Jens Axboe3529d8c2019-12-19 18:24:38 -07005602static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005603{
5604 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005605 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005606
5607 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5608 return -EINVAL;
5609 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5610 return -EINVAL;
5611
Jiufei Xue5769a352020-06-17 17:53:55 +08005612 events = READ_ONCE(sqe->poll32_events);
5613#ifdef __BIG_ENDIAN
5614 events = swahw32(events);
5615#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005616 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5617 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005618 return 0;
5619}
5620
Pavel Begunkov014db002020-03-03 21:33:12 +03005621static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005622{
5623 struct io_poll_iocb *poll = &req->poll;
5624 struct io_ring_ctx *ctx = req->ctx;
5625 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005626 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005627
Jens Axboed7718a92020-02-14 22:23:12 -07005628 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005629
Jens Axboed7718a92020-02-14 22:23:12 -07005630 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5631 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005632
Jens Axboe8c838782019-03-12 15:48:16 -06005633 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005634 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005635 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005636 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005637 spin_unlock_irq(&ctx->completion_lock);
5638
Jens Axboe8c838782019-03-12 15:48:16 -06005639 if (mask) {
5640 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005641 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005642 }
Jens Axboe8c838782019-03-12 15:48:16 -06005643 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005644}
5645
Jens Axboe5262f562019-09-17 12:26:57 -06005646static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5647{
Jens Axboead8a48a2019-11-15 08:49:11 -07005648 struct io_timeout_data *data = container_of(timer,
5649 struct io_timeout_data, timer);
5650 struct io_kiocb *req = data->req;
5651 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005652 unsigned long flags;
5653
Jens Axboe5262f562019-09-17 12:26:57 -06005654 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005655 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005656 atomic_set(&req->ctx->cq_timeouts,
5657 atomic_read(&req->ctx->cq_timeouts) + 1);
5658
Jens Axboe78e19bb2019-11-06 15:21:34 -07005659 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005660 io_commit_cqring(ctx);
5661 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5662
5663 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005664 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005665 io_put_req(req);
5666 return HRTIMER_NORESTART;
5667}
5668
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005669static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5670 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005671{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005672 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005673 struct io_kiocb *req;
5674 int ret = -ENOENT;
5675
5676 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5677 if (user_data == req->user_data) {
5678 ret = 0;
5679 break;
5680 }
5681 }
5682
5683 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005684 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005685
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005686 io = req->async_data;
5687 ret = hrtimer_try_to_cancel(&io->timer);
5688 if (ret == -1)
5689 return ERR_PTR(-EALREADY);
5690 list_del_init(&req->timeout.list);
5691 return req;
5692}
5693
5694static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5695{
5696 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5697
5698 if (IS_ERR(req))
5699 return PTR_ERR(req);
5700
5701 req_set_fail_links(req);
5702 io_cqring_fill_event(req, -ECANCELED);
5703 io_put_req_deferred(req, 1);
5704 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005705}
5706
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005707static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5708 struct timespec64 *ts, enum hrtimer_mode mode)
5709{
5710 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5711 struct io_timeout_data *data;
5712
5713 if (IS_ERR(req))
5714 return PTR_ERR(req);
5715
5716 req->timeout.off = 0; /* noseq */
5717 data = req->async_data;
5718 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5719 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5720 data->timer.function = io_timeout_fn;
5721 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5722 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005723}
5724
Jens Axboe3529d8c2019-12-19 18:24:38 -07005725static int io_timeout_remove_prep(struct io_kiocb *req,
5726 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005727{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005728 struct io_timeout_rem *tr = &req->timeout_rem;
5729
Jens Axboeb29472e2019-12-17 18:50:29 -07005730 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5731 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005732 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5733 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005734 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005735 return -EINVAL;
5736
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005737 tr->addr = READ_ONCE(sqe->addr);
5738 tr->flags = READ_ONCE(sqe->timeout_flags);
5739 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5740 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5741 return -EINVAL;
5742 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5743 return -EFAULT;
5744 } else if (tr->flags) {
5745 /* timeout removal doesn't support flags */
5746 return -EINVAL;
5747 }
5748
Jens Axboeb29472e2019-12-17 18:50:29 -07005749 return 0;
5750}
5751
Jens Axboe11365042019-10-16 09:08:32 -06005752/*
5753 * Remove or update an existing timeout command
5754 */
Jens Axboefc4df992019-12-10 14:38:45 -07005755static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005756{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005757 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005758 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005759 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005760
Jens Axboe11365042019-10-16 09:08:32 -06005761 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005762 if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) {
5763 enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS)
5764 ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
5765
5766 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
5767 } else {
5768 ret = io_timeout_cancel(ctx, tr->addr);
5769 }
Jens Axboe11365042019-10-16 09:08:32 -06005770
Jens Axboe47f46762019-11-09 17:43:02 -07005771 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005772 io_commit_cqring(ctx);
5773 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005774 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005775 if (ret < 0)
5776 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005777 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005778 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005779}
5780
Jens Axboe3529d8c2019-12-19 18:24:38 -07005781static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005782 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005783{
Jens Axboead8a48a2019-11-15 08:49:11 -07005784 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005785 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005786 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005787
Jens Axboead8a48a2019-11-15 08:49:11 -07005788 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005789 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005790 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005791 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005792 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005793 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005794 flags = READ_ONCE(sqe->timeout_flags);
5795 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005796 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005797
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005798 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005799
Jens Axboee8c2bc12020-08-15 18:44:09 -07005800 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005801 return -ENOMEM;
5802
Jens Axboee8c2bc12020-08-15 18:44:09 -07005803 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005804 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005805
5806 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005807 return -EFAULT;
5808
Jens Axboe11365042019-10-16 09:08:32 -06005809 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005810 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005811 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005812 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005813
Jens Axboead8a48a2019-11-15 08:49:11 -07005814 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5815 return 0;
5816}
5817
Jens Axboefc4df992019-12-10 14:38:45 -07005818static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005819{
Jens Axboead8a48a2019-11-15 08:49:11 -07005820 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005821 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005822 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005823 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005824
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005825 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005826
Jens Axboe5262f562019-09-17 12:26:57 -06005827 /*
5828 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005829 * timeout event to be satisfied. If it isn't set, then this is
5830 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005831 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005832 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005833 entry = ctx->timeout_list.prev;
5834 goto add;
5835 }
Jens Axboe5262f562019-09-17 12:26:57 -06005836
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005837 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5838 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005839
5840 /*
5841 * Insertion sort, ensuring the first entry in the list is always
5842 * the one we need first.
5843 */
Jens Axboe5262f562019-09-17 12:26:57 -06005844 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005845 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5846 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005847
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005848 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005849 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005850 /* nxt.seq is behind @tail, otherwise would've been completed */
5851 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005852 break;
5853 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005854add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005855 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005856 data->timer.function = io_timeout_fn;
5857 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005858 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005859 return 0;
5860}
5861
Jens Axboe62755e32019-10-28 21:49:21 -06005862static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005863{
Jens Axboe62755e32019-10-28 21:49:21 -06005864 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005865
Jens Axboe62755e32019-10-28 21:49:21 -06005866 return req->user_data == (unsigned long) data;
5867}
5868
Jens Axboee977d6d2019-11-05 12:39:45 -07005869static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005870{
Jens Axboe62755e32019-10-28 21:49:21 -06005871 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005872 int ret = 0;
5873
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005874 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005875 switch (cancel_ret) {
5876 case IO_WQ_CANCEL_OK:
5877 ret = 0;
5878 break;
5879 case IO_WQ_CANCEL_RUNNING:
5880 ret = -EALREADY;
5881 break;
5882 case IO_WQ_CANCEL_NOTFOUND:
5883 ret = -ENOENT;
5884 break;
5885 }
5886
Jens Axboee977d6d2019-11-05 12:39:45 -07005887 return ret;
5888}
5889
Jens Axboe47f46762019-11-09 17:43:02 -07005890static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5891 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005892 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005893{
5894 unsigned long flags;
5895 int ret;
5896
5897 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5898 if (ret != -ENOENT) {
5899 spin_lock_irqsave(&ctx->completion_lock, flags);
5900 goto done;
5901 }
5902
5903 spin_lock_irqsave(&ctx->completion_lock, flags);
5904 ret = io_timeout_cancel(ctx, sqe_addr);
5905 if (ret != -ENOENT)
5906 goto done;
5907 ret = io_poll_cancel(ctx, sqe_addr);
5908done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005909 if (!ret)
5910 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005911 io_cqring_fill_event(req, ret);
5912 io_commit_cqring(ctx);
5913 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5914 io_cqring_ev_posted(ctx);
5915
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005916 if (ret < 0)
5917 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005918 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005919}
5920
Jens Axboe3529d8c2019-12-19 18:24:38 -07005921static int io_async_cancel_prep(struct io_kiocb *req,
5922 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005923{
Jens Axboefbf23842019-12-17 18:45:56 -07005924 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005925 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005926 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5927 return -EINVAL;
5928 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005929 return -EINVAL;
5930
Jens Axboefbf23842019-12-17 18:45:56 -07005931 req->cancel.addr = READ_ONCE(sqe->addr);
5932 return 0;
5933}
5934
Pavel Begunkov014db002020-03-03 21:33:12 +03005935static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005936{
5937 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005938
Pavel Begunkov014db002020-03-03 21:33:12 +03005939 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005940 return 0;
5941}
5942
Jens Axboe05f3fb32019-12-09 11:22:50 -07005943static int io_files_update_prep(struct io_kiocb *req,
5944 const struct io_uring_sqe *sqe)
5945{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005946 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5947 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005948 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5949 return -EINVAL;
5950 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005951 return -EINVAL;
5952
5953 req->files_update.offset = READ_ONCE(sqe->off);
5954 req->files_update.nr_args = READ_ONCE(sqe->len);
5955 if (!req->files_update.nr_args)
5956 return -EINVAL;
5957 req->files_update.arg = READ_ONCE(sqe->addr);
5958 return 0;
5959}
5960
Jens Axboe229a7b62020-06-22 10:13:11 -06005961static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5962 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005963{
5964 struct io_ring_ctx *ctx = req->ctx;
5965 struct io_uring_files_update up;
5966 int ret;
5967
Jens Axboef86cd202020-01-29 13:46:44 -07005968 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005969 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005970
5971 up.offset = req->files_update.offset;
5972 up.fds = req->files_update.arg;
5973
5974 mutex_lock(&ctx->uring_lock);
5975 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5976 mutex_unlock(&ctx->uring_lock);
5977
5978 if (ret < 0)
5979 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005980 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005981 return 0;
5982}
5983
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005984static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005985{
Jens Axboed625c6e2019-12-17 19:53:05 -07005986 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005987 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005988 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005989 case IORING_OP_READV:
5990 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005991 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005992 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005993 case IORING_OP_WRITEV:
5994 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005995 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005996 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005997 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005998 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005999 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006000 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006001 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006002 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006003 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006004 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006005 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006006 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006007 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006008 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006009 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006010 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006011 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006012 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006013 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006014 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006015 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006016 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006017 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006018 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006019 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006020 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006021 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006022 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006023 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006024 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006025 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006026 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006027 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006028 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006029 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006030 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006031 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006032 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006033 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006034 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006035 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006036 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006037 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006038 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006039 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006040 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006041 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006042 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006043 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006044 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006045 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006046 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006047 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006048 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006049 case IORING_OP_SHUTDOWN:
6050 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006051 case IORING_OP_RENAMEAT:
6052 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006053 case IORING_OP_UNLINKAT:
6054 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006055 }
6056
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006057 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6058 req->opcode);
6059 return-EINVAL;
6060}
6061
Jens Axboedef596e2019-01-09 08:59:42 -07006062static int io_req_defer_prep(struct io_kiocb *req,
6063 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006064{
Jens Axboedef596e2019-01-09 08:59:42 -07006065 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006067 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006068 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006069 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006070}
6071
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006072static u32 io_get_sequence(struct io_kiocb *req)
6073{
6074 struct io_kiocb *pos;
6075 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006076 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006077
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006078 io_for_each_link(pos, req)
6079 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006080
6081 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6082 return total_submitted - nr_reqs;
6083}
6084
Jens Axboe3529d8c2019-12-19 18:24:38 -07006085static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006086{
6087 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006088 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006089 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006090 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006091
6092 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006093 if (likely(list_empty_careful(&ctx->defer_list) &&
6094 !(req->flags & REQ_F_IO_DRAIN)))
6095 return 0;
6096
6097 seq = io_get_sequence(req);
6098 /* Still a chance to pass the sequence check */
6099 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006100 return 0;
6101
Jens Axboee8c2bc12020-08-15 18:44:09 -07006102 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006103 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006104 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006105 return ret;
6106 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006107 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006108 de = kmalloc(sizeof(*de), GFP_KERNEL);
6109 if (!de)
6110 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006111
6112 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006113 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006114 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006115 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006116 io_queue_async_work(req);
6117 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006118 }
6119
6120 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006121 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006122 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006123 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006124 spin_unlock_irq(&ctx->completion_lock);
6125 return -EIOCBQUEUED;
6126}
Jens Axboeedafcce2019-01-09 09:16:05 -07006127
Jens Axboef573d382020-09-22 10:19:24 -06006128static void io_req_drop_files(struct io_kiocb *req)
6129{
6130 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc98de082020-11-15 12:56:32 +00006131 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboef573d382020-09-22 10:19:24 -06006132 unsigned long flags;
6133
Jens Axboe98447d62020-10-14 10:48:51 -06006134 put_files_struct(req->work.identity->files);
6135 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkovdfea9fc2020-12-18 13:12:21 +00006136 spin_lock_irqsave(&ctx->inflight_lock, flags);
6137 list_del(&req->inflight_entry);
6138 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
6139 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboedfead8a2020-10-14 10:12:37 -06006140 req->work.flags &= ~IO_WQ_WORK_FILES;
Pavel Begunkovdfea9fc2020-12-18 13:12:21 +00006141 if (atomic_read(&tctx->in_idle))
6142 wake_up(&tctx->wait);
Jens Axboef573d382020-09-22 10:19:24 -06006143}
6144
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006145static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006146{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006147 if (req->flags & REQ_F_BUFFER_SELECTED) {
6148 switch (req->opcode) {
6149 case IORING_OP_READV:
6150 case IORING_OP_READ_FIXED:
6151 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006152 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006153 break;
6154 case IORING_OP_RECVMSG:
6155 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006156 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006157 break;
6158 }
6159 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006160 }
6161
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006162 if (req->flags & REQ_F_NEED_CLEANUP) {
6163 switch (req->opcode) {
6164 case IORING_OP_READV:
6165 case IORING_OP_READ_FIXED:
6166 case IORING_OP_READ:
6167 case IORING_OP_WRITEV:
6168 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006169 case IORING_OP_WRITE: {
6170 struct io_async_rw *io = req->async_data;
6171 if (io->free_iovec)
6172 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006173 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006174 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006175 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006176 case IORING_OP_SENDMSG: {
6177 struct io_async_msghdr *io = req->async_data;
6178 if (io->iov != io->fast_iov)
6179 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006180 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006181 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006182 case IORING_OP_SPLICE:
6183 case IORING_OP_TEE:
6184 io_put_file(req, req->splice.file_in,
6185 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6186 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006187 case IORING_OP_OPENAT:
6188 case IORING_OP_OPENAT2:
6189 if (req->open.filename)
6190 putname(req->open.filename);
6191 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006192 case IORING_OP_RENAMEAT:
6193 putname(req->rename.oldpath);
6194 putname(req->rename.newpath);
6195 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006196 case IORING_OP_UNLINKAT:
6197 putname(req->unlink.filename);
6198 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006199 }
6200 req->flags &= ~REQ_F_NEED_CLEANUP;
6201 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03006202
Jens Axboef573d382020-09-22 10:19:24 -06006203 if (req->flags & REQ_F_INFLIGHT)
6204 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006205}
6206
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006207static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
6208 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006209{
Jens Axboeedafcce2019-01-09 09:16:05 -07006210 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006211 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006212
Jens Axboed625c6e2019-12-17 19:53:05 -07006213 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006214 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06006215 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006216 break;
6217 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006218 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006219 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06006220 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006221 break;
6222 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006224 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06006225 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006226 break;
6227 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03006228 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006229 break;
6230 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03006231 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006232 break;
6233 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07006234 ret = io_poll_remove(req);
6235 break;
6236 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006237 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07006238 break;
6239 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006240 ret = io_sendmsg(req, force_nonblock, cs);
6241 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006242 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006243 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006244 break;
6245 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006246 ret = io_recvmsg(req, force_nonblock, cs);
6247 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006248 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006249 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006250 break;
6251 case IORING_OP_TIMEOUT:
6252 ret = io_timeout(req);
6253 break;
6254 case IORING_OP_TIMEOUT_REMOVE:
6255 ret = io_timeout_remove(req);
6256 break;
6257 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006258 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006259 break;
6260 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006261 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006262 break;
6263 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006264 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006266 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006267 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006268 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006269 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006270 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006271 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006272 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006273 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006274 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006275 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006276 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006277 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006278 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006279 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006280 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006281 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006282 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006283 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006284 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006285 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006286 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006287 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006288 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006289 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006290 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006291 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006292 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006293 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006294 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006295 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006296 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006297 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006298 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006299 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006300 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006302 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006303 ret = io_tee(req, force_nonblock);
6304 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006305 case IORING_OP_SHUTDOWN:
6306 ret = io_shutdown(req, force_nonblock);
6307 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006308 case IORING_OP_RENAMEAT:
6309 ret = io_renameat(req, force_nonblock);
6310 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006311 case IORING_OP_UNLINKAT:
6312 ret = io_unlinkat(req, force_nonblock);
6313 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314 default:
6315 ret = -EINVAL;
6316 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006317 }
6318
6319 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006320 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321
Jens Axboeb5325762020-05-19 21:20:27 -06006322 /* If the op doesn't have a file, we're not polling for it */
6323 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006324 const bool in_async = io_wq_current_is_worker();
6325
Jens Axboe11ba8202020-01-15 21:51:17 -07006326 /* workqueue context doesn't hold uring_lock, grab it now */
6327 if (in_async)
6328 mutex_lock(&ctx->uring_lock);
6329
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006330 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006331
6332 if (in_async)
6333 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006334 }
6335
6336 return 0;
6337}
6338
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006339static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006340{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006341 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006342 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006343 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006344
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006345 timeout = io_prep_linked_timeout(req);
6346 if (timeout)
6347 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006348
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006349 /* if NO_CANCEL is set, we must still run the work */
6350 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6351 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006352 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006353 }
Jens Axboe31b51512019-01-18 22:56:34 -07006354
Jens Axboe561fb042019-10-24 07:25:42 -06006355 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006356 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006357 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006358 /*
6359 * We can get EAGAIN for polled IO even though we're
6360 * forcing a sync submission from here, since we can't
6361 * wait for request slots on the block side.
6362 */
6363 if (ret != -EAGAIN)
6364 break;
6365 cond_resched();
6366 } while (1);
6367 }
Jens Axboe31b51512019-01-18 22:56:34 -07006368
Jens Axboe561fb042019-10-24 07:25:42 -06006369 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006370 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006371
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006372 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6373 lock_ctx = req->ctx;
6374
6375 /*
6376 * io_iopoll_complete() does not hold completion_lock to
6377 * complete polled io, so here for polled io, we can not call
6378 * io_req_complete() directly, otherwise there maybe concurrent
6379 * access to cqring, defer_list, etc, which is not safe. Given
6380 * that io_iopoll_complete() is always called under uring_lock,
6381 * so here for polled io, we also get uring_lock to complete
6382 * it.
6383 */
6384 if (lock_ctx)
6385 mutex_lock(&lock_ctx->uring_lock);
6386
6387 req_set_fail_links(req);
6388 io_req_complete(req, ret);
6389
6390 if (lock_ctx)
6391 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006392 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006393
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006394 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006395}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006396
Jens Axboe65e19f52019-10-26 07:20:21 -06006397static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6398 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006399{
Jens Axboe65e19f52019-10-26 07:20:21 -06006400 struct fixed_file_table *table;
6401
Jens Axboe05f3fb32019-12-09 11:22:50 -07006402 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006403 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006404}
6405
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006406static struct file *io_file_get(struct io_submit_state *state,
6407 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006408{
6409 struct io_ring_ctx *ctx = req->ctx;
6410 struct file *file;
6411
6412 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006413 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006414 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006415 fd = array_index_nospec(fd, ctx->nr_user_files);
6416 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006417 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006418 } else {
6419 trace_io_uring_file_get(ctx, fd);
6420 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006421 }
6422
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006423 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006424}
6425
Jens Axboe2665abf2019-11-05 12:40:47 -07006426static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6427{
Jens Axboead8a48a2019-11-15 08:49:11 -07006428 struct io_timeout_data *data = container_of(timer,
6429 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006430 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006431 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006432 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006433
6434 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006435 prev = req->timeout.head;
6436 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006437
6438 /*
6439 * We don't expect the list to be empty, that will only happen if we
6440 * race with the completion of the linked work.
6441 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006442 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006443 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006444 else
6445 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006446 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6447
6448 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006449 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006450 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006451 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006452 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006453 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006454 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006455 return HRTIMER_NORESTART;
6456}
6457
Jens Axboe7271ef32020-08-10 09:55:22 -06006458static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006459{
Jens Axboe76a46e02019-11-10 23:34:16 -07006460 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006461 * If the back reference is NULL, then our linked request finished
6462 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006463 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006464 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006465 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006466
Jens Axboead8a48a2019-11-15 08:49:11 -07006467 data->timer.function = io_link_timeout_fn;
6468 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6469 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006470 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006471}
6472
6473static void io_queue_linked_timeout(struct io_kiocb *req)
6474{
6475 struct io_ring_ctx *ctx = req->ctx;
6476
6477 spin_lock_irq(&ctx->completion_lock);
6478 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006479 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006480
Jens Axboe2665abf2019-11-05 12:40:47 -07006481 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006482 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006483}
6484
Jens Axboead8a48a2019-11-15 08:49:11 -07006485static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006486{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006487 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006488
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006489 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6490 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006491 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006492
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006493 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006494 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006495 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006496 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006497}
6498
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006499static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006500{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006501 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006502 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006503 int ret;
6504
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006505again:
6506 linked_timeout = io_prep_linked_timeout(req);
6507
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006508 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6509 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006510 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006511 if (old_creds)
6512 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006513 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006514 old_creds = NULL; /* restored original creds */
6515 else
Jens Axboe98447d62020-10-14 10:48:51 -06006516 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006517 }
6518
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006519 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006520
6521 /*
6522 * We async punt it if the file wasn't marked NOWAIT, or if the file
6523 * doesn't support non-blocking read/write attempts
6524 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006525 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006526 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006527 /*
6528 * Queued up for async execution, worker will release
6529 * submit reference when the iocb is actually submitted.
6530 */
6531 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006532 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006533
Pavel Begunkovf063c542020-07-25 14:41:59 +03006534 if (linked_timeout)
6535 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006536 } else if (likely(!ret)) {
6537 /* drop submission reference */
6538 req = io_put_req_find_next(req);
6539 if (linked_timeout)
6540 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006541
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006542 if (req) {
6543 if (!(req->flags & REQ_F_FORCE_ASYNC))
6544 goto again;
6545 io_queue_async_work(req);
6546 }
6547 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006548 /* un-prep timeout, so it'll be killed as any other linked */
6549 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006550 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006551 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006552 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006553 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006554
Jens Axboe193155c2020-02-22 23:22:19 -07006555 if (old_creds)
6556 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006557}
6558
Jens Axboef13fad72020-06-22 09:34:30 -06006559static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6560 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006561{
6562 int ret;
6563
Jens Axboe3529d8c2019-12-19 18:24:38 -07006564 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006565 if (ret) {
6566 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006567fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006568 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006569 io_put_req(req);
6570 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006571 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006572 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006573 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006574 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006575 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006576 goto fail_req;
6577 }
Jens Axboece35a472019-12-17 08:04:44 -07006578 io_queue_async_work(req);
6579 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006580 if (sqe) {
6581 ret = io_req_prep(req, sqe);
6582 if (unlikely(ret))
6583 goto fail_req;
6584 }
6585 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006586 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006587}
6588
Jens Axboef13fad72020-06-22 09:34:30 -06006589static inline void io_queue_link_head(struct io_kiocb *req,
6590 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006591{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006592 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006593 io_put_req(req);
6594 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006595 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006596 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006597}
6598
Pavel Begunkov863e0562020-10-27 23:25:35 +00006599struct io_submit_link {
6600 struct io_kiocb *head;
6601 struct io_kiocb *last;
6602};
6603
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006604static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006605 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006606{
Jackie Liua197f662019-11-08 08:09:12 -07006607 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006608 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006609
Jens Axboe9e645e112019-05-10 16:07:28 -06006610 /*
6611 * If we already have a head request, queue this one for async
6612 * submittal once the head completes. If we don't have a head but
6613 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6614 * submitted sync once the chain is complete. If none of those
6615 * conditions are true (normal request), then just queue it.
6616 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006617 if (link->head) {
6618 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006619
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006620 /*
6621 * Taking sequential execution of a link, draining both sides
6622 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6623 * requests in the link. So, it drains the head and the
6624 * next after the link request. The last one is done via
6625 * drain_next flag to persist the effect across calls.
6626 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006627 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006628 head->flags |= REQ_F_IO_DRAIN;
6629 ctx->drain_next = 1;
6630 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006631 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006632 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006633 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006634 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006635 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006636 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006637 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006638 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006639 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006640
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006641 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006642 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006643 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006644 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006645 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006646 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006647 if (unlikely(ctx->drain_next)) {
6648 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006649 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006650 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006651 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006652 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006653 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006654 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006655 link->head = req;
6656 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006657 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006658 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006659 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006660 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006661
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006662 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006663}
6664
Jens Axboe9a56a232019-01-09 09:06:50 -07006665/*
6666 * Batched submission is done, ensure local IO is flushed out.
6667 */
6668static void io_submit_state_end(struct io_submit_state *state)
6669{
Jens Axboef13fad72020-06-22 09:34:30 -06006670 if (!list_empty(&state->comp.list))
6671 io_submit_flush_completions(&state->comp);
Jens Axboe27926b62020-10-28 09:33:23 -06006672 if (state->plug_started)
6673 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006674 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006675 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006676 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006677}
6678
6679/*
6680 * Start submission side cache.
6681 */
6682static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006683 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006684{
Jens Axboe27926b62020-10-28 09:33:23 -06006685 state->plug_started = false;
Jens Axboe013538b2020-06-22 09:29:15 -06006686 state->comp.nr = 0;
6687 INIT_LIST_HEAD(&state->comp.list);
6688 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006689 state->free_reqs = 0;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00006690 state->file_refs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006691 state->ios_left = max_ios;
6692}
6693
Jens Axboe2b188cc2019-01-07 10:46:33 -07006694static void io_commit_sqring(struct io_ring_ctx *ctx)
6695{
Hristo Venev75b28af2019-08-26 17:23:46 +00006696 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006697
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006698 /*
6699 * Ensure any loads from the SQEs are done at this point,
6700 * since once we write the new head, the application could
6701 * write new data to them.
6702 */
6703 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006704}
6705
6706/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006707 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006708 * that is mapped by userspace. This means that care needs to be taken to
6709 * ensure that reads are stable, as we cannot rely on userspace always
6710 * being a good citizen. If members of the sqe are validated and then later
6711 * used, it's important that those reads are done through READ_ONCE() to
6712 * prevent a re-load down the line.
6713 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006714static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006715{
Hristo Venev75b28af2019-08-26 17:23:46 +00006716 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006717 unsigned head;
6718
6719 /*
6720 * The cached sq head (or cq tail) serves two purposes:
6721 *
6722 * 1) allows us to batch the cost of updating the user visible
6723 * head updates.
6724 * 2) allows the kernel side to track the head on its own, even
6725 * though the application is the one updating it.
6726 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006727 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006728 if (likely(head < ctx->sq_entries))
6729 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006730
6731 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006732 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006733 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006734 return NULL;
6735}
6736
6737static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6738{
6739 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740}
6741
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006742/*
6743 * Check SQE restrictions (opcode and flags).
6744 *
6745 * Returns 'true' if SQE is allowed, 'false' otherwise.
6746 */
6747static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6748 struct io_kiocb *req,
6749 unsigned int sqe_flags)
6750{
6751 if (!ctx->restricted)
6752 return true;
6753
6754 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6755 return false;
6756
6757 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6758 ctx->restrictions.sqe_flags_required)
6759 return false;
6760
6761 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6762 ctx->restrictions.sqe_flags_required))
6763 return false;
6764
6765 return true;
6766}
6767
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006768#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6769 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6770 IOSQE_BUFFER_SELECT)
6771
6772static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6773 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006774 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006775{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006776 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006777 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006778
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006779 req->opcode = READ_ONCE(sqe->opcode);
6780 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006781 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006782 req->file = NULL;
6783 req->ctx = ctx;
6784 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006785 req->link = NULL;
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006786 req->fixed_file_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006787 /* one is dropped after submission, the other at completion */
6788 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006789 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006790 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006791
6792 if (unlikely(req->opcode >= IORING_OP_LAST))
6793 return -EINVAL;
6794
Jens Axboe28cea78a2020-09-14 10:51:17 -06006795 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006796 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006797
6798 sqe_flags = READ_ONCE(sqe->flags);
6799 /* enforce forwards compatibility on users */
6800 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6801 return -EINVAL;
6802
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006803 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6804 return -EACCES;
6805
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006806 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6807 !io_op_defs[req->opcode].buffer_select)
6808 return -EOPNOTSUPP;
6809
6810 id = READ_ONCE(sqe->personality);
6811 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006812 struct io_identity *iod;
6813
Jens Axboe1e6fa522020-10-15 08:46:24 -06006814 iod = idr_find(&ctx->personality_idr, id);
6815 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006816 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006817 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006818
6819 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006820 get_cred(iod->creds);
6821 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006822 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006823 }
6824
6825 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006826 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006827
Jens Axboe27926b62020-10-28 09:33:23 -06006828 /*
6829 * Plug now if we have more than 1 IO left after this, and the target
6830 * is potentially a read/write to block based storage.
6831 */
6832 if (!state->plug_started && state->ios_left > 1 &&
6833 io_op_defs[req->opcode].plug) {
6834 blk_start_plug(&state->plug);
6835 state->plug_started = true;
6836 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006837
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006838 ret = 0;
6839 if (io_op_defs[req->opcode].needs_file) {
6840 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006841
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006842 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
6843 if (unlikely(!req->file &&
6844 !io_op_defs[req->opcode].needs_file_no_error))
6845 ret = -EBADF;
6846 }
6847
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006848 state->ios_left--;
6849 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006850}
6851
Jens Axboe0f212202020-09-13 13:09:39 -06006852static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006853{
Jens Axboeac8691c2020-06-01 08:30:41 -06006854 struct io_submit_state state;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006855 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006856 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006857
Jens Axboec4a2ed72019-11-21 21:01:26 -07006858 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006859 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006860 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006861 return -EBUSY;
6862 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006863
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006864 /* make sure SQ entry isn't read before tail */
6865 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006866
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006867 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6868 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006869
Jens Axboed8a6df12020-10-15 16:24:45 -06006870 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006871 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006872
Jens Axboe6c271ce2019-01-10 11:22:30 -07006873 io_submit_state_start(&state, ctx, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006874 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006875
Jens Axboe6c271ce2019-01-10 11:22:30 -07006876 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006877 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006878 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006879 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006880
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006881 sqe = io_get_sqe(ctx);
6882 if (unlikely(!sqe)) {
6883 io_consume_sqe(ctx);
6884 break;
6885 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006886 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006887 if (unlikely(!req)) {
6888 if (!submitted)
6889 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006890 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006891 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006892 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006893 /* will complete beyond this point, count as submitted */
6894 submitted++;
6895
Pavel Begunkov692d8362020-10-10 18:34:13 +01006896 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006897 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006898fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006899 io_put_req(req);
6900 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006901 break;
6902 }
6903
Jens Axboe354420f2020-01-08 18:55:15 -07006904 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006905 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006906 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006907 if (err)
6908 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006909 }
6910
Pavel Begunkov9466f432020-01-25 22:34:01 +03006911 if (unlikely(submitted != nr)) {
6912 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006913 struct io_uring_task *tctx = current->io_uring;
6914 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006915
Jens Axboed8a6df12020-10-15 16:24:45 -06006916 percpu_ref_put_many(&ctx->refs, unused);
6917 percpu_counter_sub(&tctx->inflight, unused);
6918 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006919 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006920 if (link.head)
6921 io_queue_link_head(link.head, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006922 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006923
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006924 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6925 io_commit_sqring(ctx);
6926
Jens Axboe6c271ce2019-01-10 11:22:30 -07006927 return submitted;
6928}
6929
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006930static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6931{
6932 /* Tell userspace we may need a wakeup call */
6933 spin_lock_irq(&ctx->completion_lock);
6934 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6935 spin_unlock_irq(&ctx->completion_lock);
6936}
6937
6938static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6939{
6940 spin_lock_irq(&ctx->completion_lock);
6941 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6942 spin_unlock_irq(&ctx->completion_lock);
6943}
6944
Xiaoguang Wang08369242020-11-03 14:15:59 +08006945static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006946{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006947 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006948 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006949
Jens Axboec8d1ba52020-09-14 11:07:26 -06006950 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006951 /* if we're handling multiple rings, cap submit size for fairness */
6952 if (cap_entries && to_submit > 8)
6953 to_submit = 8;
6954
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006955 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6956 unsigned nr_events = 0;
6957
Xiaoguang Wang08369242020-11-03 14:15:59 +08006958 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006959 if (!list_empty(&ctx->iopoll_list))
6960 io_do_iopoll(ctx, &nr_events, 0);
6961
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006962 if (to_submit && !ctx->sqo_dead &&
6963 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006964 ret = io_submit_sqes(ctx, to_submit);
6965 mutex_unlock(&ctx->uring_lock);
6966 }
Jens Axboe90554202020-09-03 12:12:41 -06006967
6968 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6969 wake_up(&ctx->sqo_sq_wait);
6970
Xiaoguang Wang08369242020-11-03 14:15:59 +08006971 return ret;
6972}
6973
6974static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6975{
6976 struct io_ring_ctx *ctx;
6977 unsigned sq_thread_idle = 0;
6978
6979 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6980 if (sq_thread_idle < ctx->sq_thread_idle)
6981 sq_thread_idle = ctx->sq_thread_idle;
6982 }
6983
6984 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006985}
6986
Jens Axboe69fb2132020-09-14 11:16:23 -06006987static void io_sqd_init_new(struct io_sq_data *sqd)
6988{
6989 struct io_ring_ctx *ctx;
6990
6991 while (!list_empty(&sqd->ctx_new_list)) {
6992 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006993 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6994 complete(&ctx->sq_thread_comp);
6995 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006996
6997 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006998}
6999
Jens Axboe6c271ce2019-01-10 11:22:30 -07007000static int io_sq_thread(void *data)
7001{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007002 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007003 struct files_struct *old_files = current->files;
7004 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007005 const struct cred *old_cred = NULL;
7006 struct io_sq_data *sqd = data;
7007 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007008 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007009 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007010
Jens Axboe28cea78a2020-09-14 10:51:17 -06007011 task_lock(current);
7012 current->files = NULL;
7013 current->nsproxy = NULL;
7014 task_unlock(current);
7015
Jens Axboe69fb2132020-09-14 11:16:23 -06007016 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007017 int ret;
7018 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007019
7020 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007021 * Any changes to the sqd lists are synchronized through the
7022 * kthread parking. This synchronizes the thread vs users,
7023 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007024 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007025 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007026 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007027 /*
7028 * When sq thread is unparked, in case the previous park operation
7029 * comes from io_put_sq_data(), which means that sq thread is going
7030 * to be stopped, so here needs to have a check.
7031 */
7032 if (kthread_should_stop())
7033 break;
7034 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007035
Xiaoguang Wang08369242020-11-03 14:15:59 +08007036 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007037 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007038 timeout = jiffies + sqd->sq_thread_idle;
7039 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007040
Xiaoguang Wang08369242020-11-03 14:15:59 +08007041 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007042 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007043 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7044 if (current->cred != ctx->creds) {
7045 if (old_cred)
7046 revert_creds(old_cred);
7047 old_cred = override_creds(ctx->creds);
7048 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007049 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007050#ifdef CONFIG_AUDIT
7051 current->loginuid = ctx->loginuid;
7052 current->sessionid = ctx->sessionid;
7053#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007054
Xiaoguang Wang08369242020-11-03 14:15:59 +08007055 ret = __io_sq_thread(ctx, cap_entries);
7056 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7057 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007058
Jens Axboe28cea78a2020-09-14 10:51:17 -06007059 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007060 }
7061
Xiaoguang Wang08369242020-11-03 14:15:59 +08007062 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007063 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007064 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007065 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007066 if (sqt_spin)
7067 timeout = jiffies + sqd->sq_thread_idle;
7068 continue;
7069 }
7070
7071 if (kthread_should_park())
7072 continue;
7073
7074 needs_sched = true;
7075 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7076 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7077 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7078 !list_empty_careful(&ctx->iopoll_list)) {
7079 needs_sched = false;
7080 break;
7081 }
7082 if (io_sqring_entries(ctx)) {
7083 needs_sched = false;
7084 break;
7085 }
7086 }
7087
7088 if (needs_sched) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007089 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7090 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007091
Jens Axboe69fb2132020-09-14 11:16:23 -06007092 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007093 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7094 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007095 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007096
7097 finish_wait(&sqd->wait, &wait);
7098 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007099 }
7100
Jens Axboe4c6e2772020-07-01 11:29:10 -06007101 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007102 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007103
Dennis Zhou91d8f512020-09-16 13:41:05 -07007104 if (cur_css)
7105 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007106 if (old_cred)
7107 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007108
Jens Axboe28cea78a2020-09-14 10:51:17 -06007109 task_lock(current);
7110 current->files = old_files;
7111 current->nsproxy = old_nsproxy;
7112 task_unlock(current);
7113
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007114 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007115
Jens Axboe6c271ce2019-01-10 11:22:30 -07007116 return 0;
7117}
7118
Jens Axboebda52162019-09-24 13:47:15 -06007119struct io_wait_queue {
7120 struct wait_queue_entry wq;
7121 struct io_ring_ctx *ctx;
7122 unsigned to_wait;
7123 unsigned nr_timeouts;
7124};
7125
Pavel Begunkov6c503152021-01-04 20:36:36 +00007126static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007127{
7128 struct io_ring_ctx *ctx = iowq->ctx;
7129
7130 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007131 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007132 * started waiting. For timeouts, we always want to return to userspace,
7133 * regardless of event count.
7134 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007135 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007136 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7137}
7138
7139static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7140 int wake_flags, void *key)
7141{
7142 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7143 wq);
7144
Pavel Begunkov6c503152021-01-04 20:36:36 +00007145 /*
7146 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7147 * the task, and the next invocation will do it.
7148 */
7149 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7150 return autoremove_wake_function(curr, mode, wake_flags, key);
7151 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007152}
7153
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007154static int io_run_task_work_sig(void)
7155{
7156 if (io_run_task_work())
7157 return 1;
7158 if (!signal_pending(current))
7159 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007160 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7161 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007162 return -EINTR;
7163}
7164
Jens Axboe2b188cc2019-01-07 10:46:33 -07007165/*
7166 * Wait until events become available, if we don't already have some. The
7167 * application must reap them itself, as they reside on the shared cq ring.
7168 */
7169static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007170 const sigset_t __user *sig, size_t sigsz,
7171 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007172{
Jens Axboebda52162019-09-24 13:47:15 -06007173 struct io_wait_queue iowq = {
7174 .wq = {
7175 .private = current,
7176 .func = io_wake_function,
7177 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7178 },
7179 .ctx = ctx,
7180 .to_wait = min_events,
7181 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007182 struct io_rings *rings = ctx->rings;
Hao Xuc73ebb62020-11-03 10:54:37 +08007183 struct timespec64 ts;
7184 signed long timeout = 0;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08007185 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007186
Jens Axboeb41e9852020-02-17 09:52:41 -07007187 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007188 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7189 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007190 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007191 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007192 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007193 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007194
7195 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007196#ifdef CONFIG_COMPAT
7197 if (in_compat_syscall())
7198 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007199 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007200 else
7201#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007202 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007203
Jens Axboe2b188cc2019-01-07 10:46:33 -07007204 if (ret)
7205 return ret;
7206 }
7207
Hao Xuc73ebb62020-11-03 10:54:37 +08007208 if (uts) {
7209 if (get_timespec64(&ts, uts))
7210 return -EFAULT;
7211 timeout = timespec64_to_jiffies(&ts);
7212 }
7213
Jens Axboebda52162019-09-24 13:47:15 -06007214 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007215 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007216 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007217 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007218 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7219 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007220 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007221 ret = io_run_task_work_sig();
7222 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06007223 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007224 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007225 break;
Pavel Begunkov6c503152021-01-04 20:36:36 +00007226 if (io_should_wake(&iowq))
Jens Axboebda52162019-09-24 13:47:15 -06007227 break;
Pavel Begunkov6c503152021-01-04 20:36:36 +00007228 if (test_bit(0, &ctx->cq_check_overflow))
7229 continue;
Hao Xuc73ebb62020-11-03 10:54:37 +08007230 if (uts) {
7231 timeout = schedule_timeout(timeout);
7232 if (timeout == 0) {
7233 ret = -ETIME;
7234 break;
7235 }
7236 } else {
7237 schedule();
7238 }
Jens Axboebda52162019-09-24 13:47:15 -06007239 } while (1);
7240 finish_wait(&ctx->wait, &iowq.wq);
7241
Jens Axboeb7db41c2020-07-04 08:55:50 -06007242 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007243
Hristo Venev75b28af2019-08-26 17:23:46 +00007244 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007245}
7246
Jens Axboe6b063142019-01-10 22:13:58 -07007247static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7248{
7249#if defined(CONFIG_UNIX)
7250 if (ctx->ring_sock) {
7251 struct sock *sock = ctx->ring_sock->sk;
7252 struct sk_buff *skb;
7253
7254 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7255 kfree_skb(skb);
7256 }
7257#else
7258 int i;
7259
Jens Axboe65e19f52019-10-26 07:20:21 -06007260 for (i = 0; i < ctx->nr_user_files; i++) {
7261 struct file *file;
7262
7263 file = io_file_from_index(ctx, i);
7264 if (file)
7265 fput(file);
7266 }
Jens Axboe6b063142019-01-10 22:13:58 -07007267#endif
7268}
7269
Jens Axboe05f3fb32019-12-09 11:22:50 -07007270static void io_file_ref_kill(struct percpu_ref *ref)
7271{
7272 struct fixed_file_data *data;
7273
7274 data = container_of(ref, struct fixed_file_data, refs);
7275 complete(&data->done);
7276}
7277
Pavel Begunkov1642b442020-12-30 21:34:14 +00007278static void io_sqe_files_set_node(struct fixed_file_data *file_data,
7279 struct fixed_file_ref_node *ref_node)
7280{
7281 spin_lock_bh(&file_data->lock);
7282 file_data->node = ref_node;
7283 list_add_tail(&ref_node->node, &file_data->ref_list);
7284 spin_unlock_bh(&file_data->lock);
7285 percpu_ref_get(&file_data->refs);
7286}
7287
Jens Axboe6b063142019-01-10 22:13:58 -07007288static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7289{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007290 struct fixed_file_data *data = ctx->file_data;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007291 struct fixed_file_ref_node *backup_node, *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007292 unsigned nr_tables, i;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007293 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007294
Jens Axboe05f3fb32019-12-09 11:22:50 -07007295 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007296 return -ENXIO;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007297 backup_node = alloc_fixed_file_ref_node(ctx);
7298 if (!backup_node)
7299 return -ENOMEM;
Jens Axboe6b063142019-01-10 22:13:58 -07007300
Jens Axboeac0648a2020-11-23 09:37:51 -07007301 spin_lock_bh(&data->lock);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007302 ref_node = data->node;
Jens Axboeac0648a2020-11-23 09:37:51 -07007303 spin_unlock_bh(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007304 if (ref_node)
7305 percpu_ref_kill(&ref_node->refs);
7306
7307 percpu_ref_kill(&data->refs);
7308
7309 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007310 flush_delayed_work(&ctx->file_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007311 do {
7312 ret = wait_for_completion_interruptible(&data->done);
7313 if (!ret)
7314 break;
7315 ret = io_run_task_work_sig();
7316 if (ret < 0) {
7317 percpu_ref_resurrect(&data->refs);
7318 reinit_completion(&data->done);
7319 io_sqe_files_set_node(data, backup_node);
7320 return ret;
7321 }
7322 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007323
Jens Axboe6b063142019-01-10 22:13:58 -07007324 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007325 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7326 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007327 kfree(data->table[i].files);
7328 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007329 percpu_ref_exit(&data->refs);
7330 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007331 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007332 ctx->nr_user_files = 0;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007333 destroy_fixed_file_ref_node(backup_node);
Jens Axboe6b063142019-01-10 22:13:58 -07007334 return 0;
7335}
7336
Jens Axboe534ca6d2020-09-02 13:52:19 -06007337static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007338{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007339 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007340 /*
7341 * The park is a bit of a work-around, without it we get
7342 * warning spews on shutdown with SQPOLL set and affinity
7343 * set to a single CPU.
7344 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007345 if (sqd->thread) {
7346 kthread_park(sqd->thread);
7347 kthread_stop(sqd->thread);
7348 }
7349
7350 kfree(sqd);
7351 }
7352}
7353
Jens Axboeaa061652020-09-02 14:50:27 -06007354static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7355{
7356 struct io_ring_ctx *ctx_attach;
7357 struct io_sq_data *sqd;
7358 struct fd f;
7359
7360 f = fdget(p->wq_fd);
7361 if (!f.file)
7362 return ERR_PTR(-ENXIO);
7363 if (f.file->f_op != &io_uring_fops) {
7364 fdput(f);
7365 return ERR_PTR(-EINVAL);
7366 }
7367
7368 ctx_attach = f.file->private_data;
7369 sqd = ctx_attach->sq_data;
7370 if (!sqd) {
7371 fdput(f);
7372 return ERR_PTR(-EINVAL);
7373 }
7374
7375 refcount_inc(&sqd->refs);
7376 fdput(f);
7377 return sqd;
7378}
7379
Jens Axboe534ca6d2020-09-02 13:52:19 -06007380static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7381{
7382 struct io_sq_data *sqd;
7383
Jens Axboeaa061652020-09-02 14:50:27 -06007384 if (p->flags & IORING_SETUP_ATTACH_WQ)
7385 return io_attach_sq_data(p);
7386
Jens Axboe534ca6d2020-09-02 13:52:19 -06007387 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7388 if (!sqd)
7389 return ERR_PTR(-ENOMEM);
7390
7391 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007392 INIT_LIST_HEAD(&sqd->ctx_list);
7393 INIT_LIST_HEAD(&sqd->ctx_new_list);
7394 mutex_init(&sqd->ctx_lock);
7395 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007396 init_waitqueue_head(&sqd->wait);
7397 return sqd;
7398}
7399
Jens Axboe69fb2132020-09-14 11:16:23 -06007400static void io_sq_thread_unpark(struct io_sq_data *sqd)
7401 __releases(&sqd->lock)
7402{
7403 if (!sqd->thread)
7404 return;
7405 kthread_unpark(sqd->thread);
7406 mutex_unlock(&sqd->lock);
7407}
7408
7409static void io_sq_thread_park(struct io_sq_data *sqd)
7410 __acquires(&sqd->lock)
7411{
7412 if (!sqd->thread)
7413 return;
7414 mutex_lock(&sqd->lock);
7415 kthread_park(sqd->thread);
7416}
7417
Jens Axboe534ca6d2020-09-02 13:52:19 -06007418static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7419{
7420 struct io_sq_data *sqd = ctx->sq_data;
7421
7422 if (sqd) {
7423 if (sqd->thread) {
7424 /*
7425 * We may arrive here from the error branch in
7426 * io_sq_offload_create() where the kthread is created
7427 * without being waked up, thus wake it up now to make
7428 * sure the wait will complete.
7429 */
7430 wake_up_process(sqd->thread);
7431 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007432
7433 io_sq_thread_park(sqd);
7434 }
7435
7436 mutex_lock(&sqd->ctx_lock);
7437 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007438 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007439 mutex_unlock(&sqd->ctx_lock);
7440
Xiaoguang Wang08369242020-11-03 14:15:59 +08007441 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007442 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007443
7444 io_put_sq_data(sqd);
7445 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007446 }
7447}
7448
Jens Axboe6b063142019-01-10 22:13:58 -07007449static void io_finish_async(struct io_ring_ctx *ctx)
7450{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007451 io_sq_thread_stop(ctx);
7452
Jens Axboe561fb042019-10-24 07:25:42 -06007453 if (ctx->io_wq) {
7454 io_wq_destroy(ctx->io_wq);
7455 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007456 }
7457}
7458
7459#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007460/*
7461 * Ensure the UNIX gc is aware of our file set, so we are certain that
7462 * the io_uring can be safely unregistered on process exit, even if we have
7463 * loops in the file referencing.
7464 */
7465static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7466{
7467 struct sock *sk = ctx->ring_sock->sk;
7468 struct scm_fp_list *fpl;
7469 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007470 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007471
Jens Axboe6b063142019-01-10 22:13:58 -07007472 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7473 if (!fpl)
7474 return -ENOMEM;
7475
7476 skb = alloc_skb(0, GFP_KERNEL);
7477 if (!skb) {
7478 kfree(fpl);
7479 return -ENOMEM;
7480 }
7481
7482 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007483
Jens Axboe08a45172019-10-03 08:11:03 -06007484 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007485 fpl->user = get_uid(ctx->user);
7486 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007487 struct file *file = io_file_from_index(ctx, i + offset);
7488
7489 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007490 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007491 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007492 unix_inflight(fpl->user, fpl->fp[nr_files]);
7493 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007494 }
7495
Jens Axboe08a45172019-10-03 08:11:03 -06007496 if (nr_files) {
7497 fpl->max = SCM_MAX_FD;
7498 fpl->count = nr_files;
7499 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007500 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007501 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7502 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007503
Jens Axboe08a45172019-10-03 08:11:03 -06007504 for (i = 0; i < nr_files; i++)
7505 fput(fpl->fp[i]);
7506 } else {
7507 kfree_skb(skb);
7508 kfree(fpl);
7509 }
Jens Axboe6b063142019-01-10 22:13:58 -07007510
7511 return 0;
7512}
7513
7514/*
7515 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7516 * causes regular reference counting to break down. We rely on the UNIX
7517 * garbage collection to take care of this problem for us.
7518 */
7519static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7520{
7521 unsigned left, total;
7522 int ret = 0;
7523
7524 total = 0;
7525 left = ctx->nr_user_files;
7526 while (left) {
7527 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007528
7529 ret = __io_sqe_files_scm(ctx, this_files, total);
7530 if (ret)
7531 break;
7532 left -= this_files;
7533 total += this_files;
7534 }
7535
7536 if (!ret)
7537 return 0;
7538
7539 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007540 struct file *file = io_file_from_index(ctx, total);
7541
7542 if (file)
7543 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007544 total++;
7545 }
7546
7547 return ret;
7548}
7549#else
7550static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7551{
7552 return 0;
7553}
7554#endif
7555
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007556static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7557 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007558{
7559 int i;
7560
7561 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007562 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007563 unsigned this_files;
7564
7565 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7566 table->files = kcalloc(this_files, sizeof(struct file *),
7567 GFP_KERNEL);
7568 if (!table->files)
7569 break;
7570 nr_files -= this_files;
7571 }
7572
7573 if (i == nr_tables)
7574 return 0;
7575
7576 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007577 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007578 kfree(table->files);
7579 }
7580 return 1;
7581}
7582
Jens Axboe05f3fb32019-12-09 11:22:50 -07007583static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007584{
7585#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007586 struct sock *sock = ctx->ring_sock->sk;
7587 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7588 struct sk_buff *skb;
7589 int i;
7590
7591 __skb_queue_head_init(&list);
7592
7593 /*
7594 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7595 * remove this entry and rearrange the file array.
7596 */
7597 skb = skb_dequeue(head);
7598 while (skb) {
7599 struct scm_fp_list *fp;
7600
7601 fp = UNIXCB(skb).fp;
7602 for (i = 0; i < fp->count; i++) {
7603 int left;
7604
7605 if (fp->fp[i] != file)
7606 continue;
7607
7608 unix_notinflight(fp->user, fp->fp[i]);
7609 left = fp->count - 1 - i;
7610 if (left) {
7611 memmove(&fp->fp[i], &fp->fp[i + 1],
7612 left * sizeof(struct file *));
7613 }
7614 fp->count--;
7615 if (!fp->count) {
7616 kfree_skb(skb);
7617 skb = NULL;
7618 } else {
7619 __skb_queue_tail(&list, skb);
7620 }
7621 fput(file);
7622 file = NULL;
7623 break;
7624 }
7625
7626 if (!file)
7627 break;
7628
7629 __skb_queue_tail(&list, skb);
7630
7631 skb = skb_dequeue(head);
7632 }
7633
7634 if (skb_peek(&list)) {
7635 spin_lock_irq(&head->lock);
7636 while ((skb = __skb_dequeue(&list)) != NULL)
7637 __skb_queue_tail(head, skb);
7638 spin_unlock_irq(&head->lock);
7639 }
7640#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007641 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007642#endif
7643}
7644
Jens Axboe05f3fb32019-12-09 11:22:50 -07007645struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007646 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007647 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007648};
7649
Jens Axboe4a38aed22020-05-14 17:21:15 -06007650static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007651{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007652 struct fixed_file_data *file_data = ref_node->file_data;
7653 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007654 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007655
7656 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007657 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007658 io_ring_file_put(ctx, pfile->file);
7659 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007660 }
7661
Xiaoguang Wang05589552020-03-31 14:05:18 +08007662 percpu_ref_exit(&ref_node->refs);
7663 kfree(ref_node);
7664 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007665}
7666
Jens Axboe4a38aed22020-05-14 17:21:15 -06007667static void io_file_put_work(struct work_struct *work)
7668{
7669 struct io_ring_ctx *ctx;
7670 struct llist_node *node;
7671
7672 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7673 node = llist_del_all(&ctx->file_put_llist);
7674
7675 while (node) {
7676 struct fixed_file_ref_node *ref_node;
7677 struct llist_node *next = node->next;
7678
7679 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7680 __io_file_put_work(ref_node);
7681 node = next;
7682 }
7683}
7684
Jens Axboe05f3fb32019-12-09 11:22:50 -07007685static void io_file_data_ref_zero(struct percpu_ref *ref)
7686{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007687 struct fixed_file_ref_node *ref_node;
Pavel Begunkove2978222020-11-18 14:56:26 +00007688 struct fixed_file_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007689 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007690 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007691 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007692
Xiaoguang Wang05589552020-03-31 14:05:18 +08007693 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Pavel Begunkove2978222020-11-18 14:56:26 +00007694 data = ref_node->file_data;
7695 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007696
Jens Axboeac0648a2020-11-23 09:37:51 -07007697 spin_lock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007698 ref_node->done = true;
7699
7700 while (!list_empty(&data->ref_list)) {
7701 ref_node = list_first_entry(&data->ref_list,
7702 struct fixed_file_ref_node, node);
7703 /* recycle ref nodes in order */
7704 if (!ref_node->done)
7705 break;
7706 list_del(&ref_node->node);
7707 first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist);
7708 }
Jens Axboeac0648a2020-11-23 09:37:51 -07007709 spin_unlock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007710
7711 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007712 delay = 0;
7713
Jens Axboe4a38aed22020-05-14 17:21:15 -06007714 if (!delay)
7715 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7716 else if (first_add)
7717 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007718}
7719
7720static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7721 struct io_ring_ctx *ctx)
7722{
7723 struct fixed_file_ref_node *ref_node;
7724
7725 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7726 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007727 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007728
7729 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7730 0, GFP_KERNEL)) {
7731 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007732 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007733 }
7734 INIT_LIST_HEAD(&ref_node->node);
7735 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007736 ref_node->file_data = ctx->file_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007737 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007738 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007739}
7740
7741static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7742{
7743 percpu_ref_exit(&ref_node->refs);
7744 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007745}
7746
7747static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7748 unsigned nr_args)
7749{
7750 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007751 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007752 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007753 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007754 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007755 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007756
7757 if (ctx->file_data)
7758 return -EBUSY;
7759 if (!nr_args)
7760 return -EINVAL;
7761 if (nr_args > IORING_MAX_FIXED_FILES)
7762 return -EMFILE;
7763
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007764 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7765 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007766 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007767 file_data->ctx = ctx;
7768 init_completion(&file_data->done);
7769 INIT_LIST_HEAD(&file_data->ref_list);
7770 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007771
7772 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007773 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007774 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007775 if (!file_data->table)
7776 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007777
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007778 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007779 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7780 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007781
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007782 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7783 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007784 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007785
7786 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7787 struct fixed_file_table *table;
7788 unsigned index;
7789
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007790 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7791 ret = -EFAULT;
7792 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007793 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007794 /* allow sparse sets */
7795 if (fd == -1)
7796 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007797
Jens Axboe05f3fb32019-12-09 11:22:50 -07007798 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007799 ret = -EBADF;
7800 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007801 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007802
7803 /*
7804 * Don't allow io_uring instances to be registered. If UNIX
7805 * isn't enabled, then this causes a reference cycle and this
7806 * instance can never get freed. If UNIX is enabled we'll
7807 * handle it just fine, but there's still no point in allowing
7808 * a ring fd as it doesn't support regular read/write anyway.
7809 */
7810 if (file->f_op == &io_uring_fops) {
7811 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007812 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007813 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007814 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7815 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007816 table->files[index] = file;
7817 }
7818
Jens Axboe05f3fb32019-12-09 11:22:50 -07007819 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007820 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007821 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007822 return ret;
7823 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007824
Xiaoguang Wang05589552020-03-31 14:05:18 +08007825 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007826 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007827 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007828 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007829 }
7830
Pavel Begunkov1642b442020-12-30 21:34:14 +00007831 io_sqe_files_set_node(file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007832 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007833out_fput:
7834 for (i = 0; i < ctx->nr_user_files; i++) {
7835 file = io_file_from_index(ctx, i);
7836 if (file)
7837 fput(file);
7838 }
7839 for (i = 0; i < nr_tables; i++)
7840 kfree(file_data->table[i].files);
7841 ctx->nr_user_files = 0;
7842out_ref:
7843 percpu_ref_exit(&file_data->refs);
7844out_free:
7845 kfree(file_data->table);
7846 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007847 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007848 return ret;
7849}
7850
Jens Axboec3a31e62019-10-03 13:59:56 -06007851static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7852 int index)
7853{
7854#if defined(CONFIG_UNIX)
7855 struct sock *sock = ctx->ring_sock->sk;
7856 struct sk_buff_head *head = &sock->sk_receive_queue;
7857 struct sk_buff *skb;
7858
7859 /*
7860 * See if we can merge this file into an existing skb SCM_RIGHTS
7861 * file set. If there's no room, fall back to allocating a new skb
7862 * and filling it in.
7863 */
7864 spin_lock_irq(&head->lock);
7865 skb = skb_peek(head);
7866 if (skb) {
7867 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7868
7869 if (fpl->count < SCM_MAX_FD) {
7870 __skb_unlink(skb, head);
7871 spin_unlock_irq(&head->lock);
7872 fpl->fp[fpl->count] = get_file(file);
7873 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7874 fpl->count++;
7875 spin_lock_irq(&head->lock);
7876 __skb_queue_head(head, skb);
7877 } else {
7878 skb = NULL;
7879 }
7880 }
7881 spin_unlock_irq(&head->lock);
7882
7883 if (skb) {
7884 fput(file);
7885 return 0;
7886 }
7887
7888 return __io_sqe_files_scm(ctx, 1, index);
7889#else
7890 return 0;
7891#endif
7892}
7893
Hillf Dantona5318d32020-03-23 17:47:15 +08007894static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007895 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007896{
Hillf Dantona5318d32020-03-23 17:47:15 +08007897 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007898 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007899
Jens Axboe05f3fb32019-12-09 11:22:50 -07007900 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007901 if (!pfile)
7902 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007903
7904 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007905 list_add(&pfile->list, &ref_node->file_list);
7906
Hillf Dantona5318d32020-03-23 17:47:15 +08007907 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007908}
7909
7910static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7911 struct io_uring_files_update *up,
7912 unsigned nr_args)
7913{
7914 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007915 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007916 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007917 __s32 __user *fds;
7918 int fd, i, err;
7919 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007920 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007921
Jens Axboe05f3fb32019-12-09 11:22:50 -07007922 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007923 return -EOVERFLOW;
7924 if (done > ctx->nr_user_files)
7925 return -EINVAL;
7926
Xiaoguang Wang05589552020-03-31 14:05:18 +08007927 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007928 if (!ref_node)
7929 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007930
Jens Axboec3a31e62019-10-03 13:59:56 -06007931 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007932 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007933 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007934 struct fixed_file_table *table;
7935 unsigned index;
7936
Jens Axboec3a31e62019-10-03 13:59:56 -06007937 err = 0;
7938 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7939 err = -EFAULT;
7940 break;
7941 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007942 i = array_index_nospec(up->offset, ctx->nr_user_files);
7943 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007944 index = i & IORING_FILE_TABLE_MASK;
7945 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007946 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007947 err = io_queue_file_removal(data, file);
7948 if (err)
7949 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007950 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007951 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007952 }
7953 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007954 file = fget(fd);
7955 if (!file) {
7956 err = -EBADF;
7957 break;
7958 }
7959 /*
7960 * Don't allow io_uring instances to be registered. If
7961 * UNIX isn't enabled, then this causes a reference
7962 * cycle and this instance can never get freed. If UNIX
7963 * is enabled we'll handle it just fine, but there's
7964 * still no point in allowing a ring fd as it doesn't
7965 * support regular read/write anyway.
7966 */
7967 if (file->f_op == &io_uring_fops) {
7968 fput(file);
7969 err = -EBADF;
7970 break;
7971 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007972 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007973 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007974 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007975 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007976 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007977 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007978 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007979 }
7980 nr_args--;
7981 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007982 up->offset++;
7983 }
7984
Xiaoguang Wang05589552020-03-31 14:05:18 +08007985 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007986 percpu_ref_kill(&data->node->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007987 io_sqe_files_set_node(data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007988 } else
7989 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007990
7991 return done ? done : err;
7992}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007993
Jens Axboe05f3fb32019-12-09 11:22:50 -07007994static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7995 unsigned nr_args)
7996{
7997 struct io_uring_files_update up;
7998
7999 if (!ctx->file_data)
8000 return -ENXIO;
8001 if (!nr_args)
8002 return -EINVAL;
8003 if (copy_from_user(&up, arg, sizeof(up)))
8004 return -EFAULT;
8005 if (up.resv)
8006 return -EINVAL;
8007
8008 return __io_sqe_files_update(ctx, &up, nr_args);
8009}
Jens Axboec3a31e62019-10-03 13:59:56 -06008010
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008011static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008012{
8013 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8014
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008015 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07008016 io_put_req(req);
8017}
8018
Pavel Begunkov24369c22020-01-28 03:15:48 +03008019static int io_init_wq_offload(struct io_ring_ctx *ctx,
8020 struct io_uring_params *p)
8021{
8022 struct io_wq_data data;
8023 struct fd f;
8024 struct io_ring_ctx *ctx_attach;
8025 unsigned int concurrency;
8026 int ret = 0;
8027
8028 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008029 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008030 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008031
8032 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8033 /* Do QD, or 4 * CPUS, whatever is smallest */
8034 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8035
8036 ctx->io_wq = io_wq_create(concurrency, &data);
8037 if (IS_ERR(ctx->io_wq)) {
8038 ret = PTR_ERR(ctx->io_wq);
8039 ctx->io_wq = NULL;
8040 }
8041 return ret;
8042 }
8043
8044 f = fdget(p->wq_fd);
8045 if (!f.file)
8046 return -EBADF;
8047
8048 if (f.file->f_op != &io_uring_fops) {
8049 ret = -EINVAL;
8050 goto out_fput;
8051 }
8052
8053 ctx_attach = f.file->private_data;
8054 /* @io_wq is protected by holding the fd */
8055 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8056 ret = -EINVAL;
8057 goto out_fput;
8058 }
8059
8060 ctx->io_wq = ctx_attach->io_wq;
8061out_fput:
8062 fdput(f);
8063 return ret;
8064}
8065
Jens Axboe0f212202020-09-13 13:09:39 -06008066static int io_uring_alloc_task_context(struct task_struct *task)
8067{
8068 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008069 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008070
8071 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8072 if (unlikely(!tctx))
8073 return -ENOMEM;
8074
Jens Axboed8a6df12020-10-15 16:24:45 -06008075 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8076 if (unlikely(ret)) {
8077 kfree(tctx);
8078 return ret;
8079 }
8080
Jens Axboe0f212202020-09-13 13:09:39 -06008081 xa_init(&tctx->xa);
8082 init_waitqueue_head(&tctx->wait);
8083 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008084 atomic_set(&tctx->in_idle, 0);
8085 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008086 io_init_identity(&tctx->__identity);
8087 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008088 task->io_uring = tctx;
8089 return 0;
8090}
8091
8092void __io_uring_free(struct task_struct *tsk)
8093{
8094 struct io_uring_task *tctx = tsk->io_uring;
8095
8096 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008097 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8098 if (tctx->identity != &tctx->__identity)
8099 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008100 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008101 kfree(tctx);
8102 tsk->io_uring = NULL;
8103}
8104
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008105static int io_sq_offload_create(struct io_ring_ctx *ctx,
8106 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008107{
8108 int ret;
8109
Jens Axboe6c271ce2019-01-10 11:22:30 -07008110 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008111 struct io_sq_data *sqd;
8112
Jens Axboe3ec482d2019-04-08 10:51:01 -06008113 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008114 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008115 goto err;
8116
Jens Axboe534ca6d2020-09-02 13:52:19 -06008117 sqd = io_get_sq_data(p);
8118 if (IS_ERR(sqd)) {
8119 ret = PTR_ERR(sqd);
8120 goto err;
8121 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008122
Jens Axboe534ca6d2020-09-02 13:52:19 -06008123 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008124 io_sq_thread_park(sqd);
8125 mutex_lock(&sqd->ctx_lock);
8126 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8127 mutex_unlock(&sqd->ctx_lock);
8128 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008129
Jens Axboe917257d2019-04-13 09:28:55 -06008130 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8131 if (!ctx->sq_thread_idle)
8132 ctx->sq_thread_idle = HZ;
8133
Jens Axboeaa061652020-09-02 14:50:27 -06008134 if (sqd->thread)
8135 goto done;
8136
Jens Axboe6c271ce2019-01-10 11:22:30 -07008137 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008138 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008139
Jens Axboe917257d2019-04-13 09:28:55 -06008140 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008141 if (cpu >= nr_cpu_ids)
8142 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008143 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008144 goto err;
8145
Jens Axboe69fb2132020-09-14 11:16:23 -06008146 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008147 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008148 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008149 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008150 "io_uring-sq");
8151 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008152 if (IS_ERR(sqd->thread)) {
8153 ret = PTR_ERR(sqd->thread);
8154 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008155 goto err;
8156 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008157 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008158 if (ret)
8159 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008160 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8161 /* Can't have SQ_AFF without SQPOLL */
8162 ret = -EINVAL;
8163 goto err;
8164 }
8165
Jens Axboeaa061652020-09-02 14:50:27 -06008166done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008167 ret = io_init_wq_offload(ctx, p);
8168 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008169 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008170
8171 return 0;
8172err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008173 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008174 return ret;
8175}
8176
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008177static void io_sq_offload_start(struct io_ring_ctx *ctx)
8178{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008179 struct io_sq_data *sqd = ctx->sq_data;
8180
8181 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8182 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008183}
8184
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008185static inline void __io_unaccount_mem(struct user_struct *user,
8186 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008187{
8188 atomic_long_sub(nr_pages, &user->locked_vm);
8189}
8190
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008191static inline int __io_account_mem(struct user_struct *user,
8192 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008193{
8194 unsigned long page_limit, cur_pages, new_pages;
8195
8196 /* Don't allow more pages than we can safely lock */
8197 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8198
8199 do {
8200 cur_pages = atomic_long_read(&user->locked_vm);
8201 new_pages = cur_pages + nr_pages;
8202 if (new_pages > page_limit)
8203 return -ENOMEM;
8204 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8205 new_pages) != cur_pages);
8206
8207 return 0;
8208}
8209
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008210static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8211 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008212{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008213 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008214 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008215
Jens Axboe2aede0e2020-09-14 10:45:53 -06008216 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008217 if (acct == ACCT_LOCKED) {
8218 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008219 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008220 mmap_write_unlock(ctx->mm_account);
8221 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008222 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008223 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008224 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008225}
8226
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008227static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8228 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008229{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008230 int ret;
8231
8232 if (ctx->limit_mem) {
8233 ret = __io_account_mem(ctx->user, nr_pages);
8234 if (ret)
8235 return ret;
8236 }
8237
Jens Axboe2aede0e2020-09-14 10:45:53 -06008238 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008239 if (acct == ACCT_LOCKED) {
8240 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008241 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008242 mmap_write_unlock(ctx->mm_account);
8243 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008244 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008245 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008246 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008247
8248 return 0;
8249}
8250
Jens Axboe2b188cc2019-01-07 10:46:33 -07008251static void io_mem_free(void *ptr)
8252{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008253 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008254
Mark Rutland52e04ef2019-04-30 17:30:21 +01008255 if (!ptr)
8256 return;
8257
8258 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008259 if (put_page_testzero(page))
8260 free_compound_page(page);
8261}
8262
8263static void *io_mem_alloc(size_t size)
8264{
8265 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8266 __GFP_NORETRY;
8267
8268 return (void *) __get_free_pages(gfp_flags, get_order(size));
8269}
8270
Hristo Venev75b28af2019-08-26 17:23:46 +00008271static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8272 size_t *sq_offset)
8273{
8274 struct io_rings *rings;
8275 size_t off, sq_array_size;
8276
8277 off = struct_size(rings, cqes, cq_entries);
8278 if (off == SIZE_MAX)
8279 return SIZE_MAX;
8280
8281#ifdef CONFIG_SMP
8282 off = ALIGN(off, SMP_CACHE_BYTES);
8283 if (off == 0)
8284 return SIZE_MAX;
8285#endif
8286
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008287 if (sq_offset)
8288 *sq_offset = off;
8289
Hristo Venev75b28af2019-08-26 17:23:46 +00008290 sq_array_size = array_size(sizeof(u32), sq_entries);
8291 if (sq_array_size == SIZE_MAX)
8292 return SIZE_MAX;
8293
8294 if (check_add_overflow(off, sq_array_size, &off))
8295 return SIZE_MAX;
8296
Hristo Venev75b28af2019-08-26 17:23:46 +00008297 return off;
8298}
8299
Jens Axboe2b188cc2019-01-07 10:46:33 -07008300static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8301{
Hristo Venev75b28af2019-08-26 17:23:46 +00008302 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008303
Hristo Venev75b28af2019-08-26 17:23:46 +00008304 pages = (size_t)1 << get_order(
8305 rings_size(sq_entries, cq_entries, NULL));
8306 pages += (size_t)1 << get_order(
8307 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008308
Hristo Venev75b28af2019-08-26 17:23:46 +00008309 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008310}
8311
Jens Axboeedafcce2019-01-09 09:16:05 -07008312static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8313{
8314 int i, j;
8315
8316 if (!ctx->user_bufs)
8317 return -ENXIO;
8318
8319 for (i = 0; i < ctx->nr_user_bufs; i++) {
8320 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8321
8322 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008323 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008324
Jens Axboede293932020-09-17 16:19:16 -06008325 if (imu->acct_pages)
8326 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008327 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008328 imu->nr_bvecs = 0;
8329 }
8330
8331 kfree(ctx->user_bufs);
8332 ctx->user_bufs = NULL;
8333 ctx->nr_user_bufs = 0;
8334 return 0;
8335}
8336
8337static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8338 void __user *arg, unsigned index)
8339{
8340 struct iovec __user *src;
8341
8342#ifdef CONFIG_COMPAT
8343 if (ctx->compat) {
8344 struct compat_iovec __user *ciovs;
8345 struct compat_iovec ciov;
8346
8347 ciovs = (struct compat_iovec __user *) arg;
8348 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8349 return -EFAULT;
8350
Jens Axboed55e5f52019-12-11 16:12:15 -07008351 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008352 dst->iov_len = ciov.iov_len;
8353 return 0;
8354 }
8355#endif
8356 src = (struct iovec __user *) arg;
8357 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8358 return -EFAULT;
8359 return 0;
8360}
8361
Jens Axboede293932020-09-17 16:19:16 -06008362/*
8363 * Not super efficient, but this is just a registration time. And we do cache
8364 * the last compound head, so generally we'll only do a full search if we don't
8365 * match that one.
8366 *
8367 * We check if the given compound head page has already been accounted, to
8368 * avoid double accounting it. This allows us to account the full size of the
8369 * page, not just the constituent pages of a huge page.
8370 */
8371static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8372 int nr_pages, struct page *hpage)
8373{
8374 int i, j;
8375
8376 /* check current page array */
8377 for (i = 0; i < nr_pages; i++) {
8378 if (!PageCompound(pages[i]))
8379 continue;
8380 if (compound_head(pages[i]) == hpage)
8381 return true;
8382 }
8383
8384 /* check previously registered pages */
8385 for (i = 0; i < ctx->nr_user_bufs; i++) {
8386 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8387
8388 for (j = 0; j < imu->nr_bvecs; j++) {
8389 if (!PageCompound(imu->bvec[j].bv_page))
8390 continue;
8391 if (compound_head(imu->bvec[j].bv_page) == hpage)
8392 return true;
8393 }
8394 }
8395
8396 return false;
8397}
8398
8399static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8400 int nr_pages, struct io_mapped_ubuf *imu,
8401 struct page **last_hpage)
8402{
8403 int i, ret;
8404
8405 for (i = 0; i < nr_pages; i++) {
8406 if (!PageCompound(pages[i])) {
8407 imu->acct_pages++;
8408 } else {
8409 struct page *hpage;
8410
8411 hpage = compound_head(pages[i]);
8412 if (hpage == *last_hpage)
8413 continue;
8414 *last_hpage = hpage;
8415 if (headpage_already_acct(ctx, pages, i, hpage))
8416 continue;
8417 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8418 }
8419 }
8420
8421 if (!imu->acct_pages)
8422 return 0;
8423
8424 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8425 if (ret)
8426 imu->acct_pages = 0;
8427 return ret;
8428}
8429
Jens Axboeedafcce2019-01-09 09:16:05 -07008430static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8431 unsigned nr_args)
8432{
8433 struct vm_area_struct **vmas = NULL;
8434 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008435 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008436 int i, j, got_pages = 0;
8437 int ret = -EINVAL;
8438
8439 if (ctx->user_bufs)
8440 return -EBUSY;
8441 if (!nr_args || nr_args > UIO_MAXIOV)
8442 return -EINVAL;
8443
8444 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8445 GFP_KERNEL);
8446 if (!ctx->user_bufs)
8447 return -ENOMEM;
8448
8449 for (i = 0; i < nr_args; i++) {
8450 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8451 unsigned long off, start, end, ubuf;
8452 int pret, nr_pages;
8453 struct iovec iov;
8454 size_t size;
8455
8456 ret = io_copy_iov(ctx, &iov, arg, i);
8457 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008458 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008459
8460 /*
8461 * Don't impose further limits on the size and buffer
8462 * constraints here, we'll -EINVAL later when IO is
8463 * submitted if they are wrong.
8464 */
8465 ret = -EFAULT;
8466 if (!iov.iov_base || !iov.iov_len)
8467 goto err;
8468
8469 /* arbitrary limit, but we need something */
8470 if (iov.iov_len > SZ_1G)
8471 goto err;
8472
8473 ubuf = (unsigned long) iov.iov_base;
8474 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8475 start = ubuf >> PAGE_SHIFT;
8476 nr_pages = end - start;
8477
Jens Axboeedafcce2019-01-09 09:16:05 -07008478 ret = 0;
8479 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008480 kvfree(vmas);
8481 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008482 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008483 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008484 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008485 sizeof(struct vm_area_struct *),
8486 GFP_KERNEL);
8487 if (!pages || !vmas) {
8488 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008489 goto err;
8490 }
8491 got_pages = nr_pages;
8492 }
8493
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008494 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008495 GFP_KERNEL);
8496 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008497 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008498 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008499
8500 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008501 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008502 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008503 FOLL_WRITE | FOLL_LONGTERM,
8504 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008505 if (pret == nr_pages) {
8506 /* don't support file backed memory */
8507 for (j = 0; j < nr_pages; j++) {
8508 struct vm_area_struct *vma = vmas[j];
8509
8510 if (vma->vm_file &&
8511 !is_file_hugepages(vma->vm_file)) {
8512 ret = -EOPNOTSUPP;
8513 break;
8514 }
8515 }
8516 } else {
8517 ret = pret < 0 ? pret : -EFAULT;
8518 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008519 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008520 if (ret) {
8521 /*
8522 * if we did partial map, or found file backed vmas,
8523 * release any pages we did get
8524 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008525 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008526 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008527 kvfree(imu->bvec);
8528 goto err;
8529 }
8530
8531 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8532 if (ret) {
8533 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008534 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008535 goto err;
8536 }
8537
8538 off = ubuf & ~PAGE_MASK;
8539 size = iov.iov_len;
8540 for (j = 0; j < nr_pages; j++) {
8541 size_t vec_len;
8542
8543 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8544 imu->bvec[j].bv_page = pages[j];
8545 imu->bvec[j].bv_len = vec_len;
8546 imu->bvec[j].bv_offset = off;
8547 off = 0;
8548 size -= vec_len;
8549 }
8550 /* store original address for later verification */
8551 imu->ubuf = ubuf;
8552 imu->len = iov.iov_len;
8553 imu->nr_bvecs = nr_pages;
8554
8555 ctx->nr_user_bufs++;
8556 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008557 kvfree(pages);
8558 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008559 return 0;
8560err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008561 kvfree(pages);
8562 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008563 io_sqe_buffer_unregister(ctx);
8564 return ret;
8565}
8566
Jens Axboe9b402842019-04-11 11:45:41 -06008567static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8568{
8569 __s32 __user *fds = arg;
8570 int fd;
8571
8572 if (ctx->cq_ev_fd)
8573 return -EBUSY;
8574
8575 if (copy_from_user(&fd, fds, sizeof(*fds)))
8576 return -EFAULT;
8577
8578 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8579 if (IS_ERR(ctx->cq_ev_fd)) {
8580 int ret = PTR_ERR(ctx->cq_ev_fd);
8581 ctx->cq_ev_fd = NULL;
8582 return ret;
8583 }
8584
8585 return 0;
8586}
8587
8588static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8589{
8590 if (ctx->cq_ev_fd) {
8591 eventfd_ctx_put(ctx->cq_ev_fd);
8592 ctx->cq_ev_fd = NULL;
8593 return 0;
8594 }
8595
8596 return -ENXIO;
8597}
8598
Jens Axboe5a2e7452020-02-23 16:23:11 -07008599static int __io_destroy_buffers(int id, void *p, void *data)
8600{
8601 struct io_ring_ctx *ctx = data;
8602 struct io_buffer *buf = p;
8603
Jens Axboe067524e2020-03-02 16:32:28 -07008604 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008605 return 0;
8606}
8607
8608static void io_destroy_buffers(struct io_ring_ctx *ctx)
8609{
8610 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8611 idr_destroy(&ctx->io_buffer_idr);
8612}
8613
Jens Axboe2b188cc2019-01-07 10:46:33 -07008614static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8615{
Jens Axboe6b063142019-01-10 22:13:58 -07008616 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008617 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008618
8619 if (ctx->sqo_task) {
8620 put_task_struct(ctx->sqo_task);
8621 ctx->sqo_task = NULL;
8622 mmdrop(ctx->mm_account);
8623 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008624 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008625
Dennis Zhou91d8f512020-09-16 13:41:05 -07008626#ifdef CONFIG_BLK_CGROUP
8627 if (ctx->sqo_blkcg_css)
8628 css_put(ctx->sqo_blkcg_css);
8629#endif
8630
Jens Axboe6b063142019-01-10 22:13:58 -07008631 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008632 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008633 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008634 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008635
Jens Axboe2b188cc2019-01-07 10:46:33 -07008636#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008637 if (ctx->ring_sock) {
8638 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008639 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008640 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008641#endif
8642
Hristo Venev75b28af2019-08-26 17:23:46 +00008643 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008644 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008645
8646 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008647 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008648 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008649 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008650 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008651 kfree(ctx);
8652}
8653
8654static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8655{
8656 struct io_ring_ctx *ctx = file->private_data;
8657 __poll_t mask = 0;
8658
8659 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008660 /*
8661 * synchronizes with barrier from wq_has_sleeper call in
8662 * io_commit_cqring
8663 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008664 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008665 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008666 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008667 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8668 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008669 mask |= EPOLLIN | EPOLLRDNORM;
8670
8671 return mask;
8672}
8673
8674static int io_uring_fasync(int fd, struct file *file, int on)
8675{
8676 struct io_ring_ctx *ctx = file->private_data;
8677
8678 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8679}
8680
Jens Axboe071698e2020-01-28 10:04:42 -07008681static int io_remove_personalities(int id, void *p, void *data)
8682{
8683 struct io_ring_ctx *ctx = data;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008684 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008685
Jens Axboe1e6fa522020-10-15 08:46:24 -06008686 iod = idr_remove(&ctx->personality_idr, id);
8687 if (iod) {
8688 put_cred(iod->creds);
8689 if (refcount_dec_and_test(&iod->count))
8690 kfree(iod);
8691 }
Jens Axboe071698e2020-01-28 10:04:42 -07008692 return 0;
8693}
8694
Jens Axboe85faa7b2020-04-09 18:14:00 -06008695static void io_ring_exit_work(struct work_struct *work)
8696{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008697 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8698 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008699
Jens Axboe56952e92020-06-17 15:00:04 -06008700 /*
8701 * If we're doing polled IO and end up having requests being
8702 * submitted async (out-of-line), then completions can come in while
8703 * we're waiting for refs to drop. We need to reap these manually,
8704 * as nobody else will be looking for them.
8705 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008706 do {
Pavel Begunkov90df0852021-01-04 20:43:30 +00008707 __io_uring_cancel_task_requests(ctx, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008708 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008709 io_ring_ctx_free(ctx);
8710}
8711
Jens Axboe00c18642020-12-20 10:45:02 -07008712static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8713{
8714 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8715
8716 return req->ctx == data;
8717}
8718
Jens Axboe2b188cc2019-01-07 10:46:33 -07008719static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8720{
8721 mutex_lock(&ctx->uring_lock);
8722 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008723
8724 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8725 ctx->sqo_dead = 1;
8726
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008727 /* if force is set, the ring is going away. always drop after that */
8728 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008729 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008730 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008731 mutex_unlock(&ctx->uring_lock);
8732
Pavel Begunkov6b819282020-11-06 13:00:25 +00008733 io_kill_timeouts(ctx, NULL, NULL);
8734 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008735
8736 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008737 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008738
Jens Axboe15dff282019-11-13 09:09:23 -07008739 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008740 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008741 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008742
8743 /*
8744 * Do this upfront, so we won't have a grace period where the ring
8745 * is closed but resources aren't reaped yet. This can cause
8746 * spurious failure in setting up a new ring.
8747 */
Jens Axboe760618f2020-07-24 12:53:31 -06008748 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8749 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008750
Jens Axboe85faa7b2020-04-09 18:14:00 -06008751 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008752 /*
8753 * Use system_unbound_wq to avoid spawning tons of event kworkers
8754 * if we're exiting a ton of rings at the same time. It just adds
8755 * noise and overhead, there's no discernable change in runtime
8756 * over using system_wq.
8757 */
8758 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008759}
8760
8761static int io_uring_release(struct inode *inode, struct file *file)
8762{
8763 struct io_ring_ctx *ctx = file->private_data;
8764
8765 file->private_data = NULL;
8766 io_ring_ctx_wait_and_kill(ctx);
8767 return 0;
8768}
8769
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008770struct io_task_cancel {
8771 struct task_struct *task;
8772 struct files_struct *files;
8773};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008774
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008775static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008776{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008777 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008778 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008779 bool ret;
8780
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008781 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008782 unsigned long flags;
8783 struct io_ring_ctx *ctx = req->ctx;
8784
8785 /* protect against races with linked timeouts */
8786 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008787 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008788 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8789 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008790 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008791 }
8792 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008793}
8794
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008795static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008796 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008797 struct files_struct *files)
8798{
8799 struct io_defer_entry *de = NULL;
8800 LIST_HEAD(list);
8801
8802 spin_lock_irq(&ctx->completion_lock);
8803 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008804 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008805 list_cut_position(&list, &ctx->defer_list, &de->list);
8806 break;
8807 }
8808 }
8809 spin_unlock_irq(&ctx->completion_lock);
8810
8811 while (!list_empty(&list)) {
8812 de = list_first_entry(&list, struct io_defer_entry, list);
8813 list_del_init(&de->list);
8814 req_set_fail_links(de->req);
8815 io_put_req(de->req);
8816 io_req_complete(de->req, -ECANCELED);
8817 kfree(de);
8818 }
8819}
8820
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008821static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008822 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008823 struct files_struct *files)
8824{
Jens Axboefcb323c2019-10-24 12:39:47 -06008825 while (!list_empty_careful(&ctx->inflight_list)) {
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008826 struct io_task_cancel cancel = { .task = task, .files = files };
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008827 struct io_kiocb *req;
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008828 DEFINE_WAIT(wait);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008829 bool found = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06008830
8831 spin_lock_irq(&ctx->inflight_lock);
8832 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008833 if (req->task != task ||
Jens Axboe98447d62020-10-14 10:48:51 -06008834 req->work.identity->files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008835 continue;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008836 found = true;
Jens Axboe768134d2019-11-10 20:30:53 -07008837 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008838 }
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008839 if (found)
Pavel Begunkovc98de082020-11-15 12:56:32 +00008840 prepare_to_wait(&task->io_uring->wait, &wait,
8841 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008842 spin_unlock_irq(&ctx->inflight_lock);
8843
Jens Axboe768134d2019-11-10 20:30:53 -07008844 /* We need to keep going until we don't find a matching req */
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008845 if (!found)
Jens Axboefcb323c2019-10-24 12:39:47 -06008846 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008847
8848 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
8849 io_poll_remove_all(ctx, task, files);
8850 io_kill_timeouts(ctx, task, files);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008851 /* cancellations _may_ trigger task work */
8852 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008853 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008854 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008855 }
8856}
8857
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008858static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8859 struct task_struct *task)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008860{
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008861 while (1) {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008862 struct io_task_cancel cancel = { .task = task, .files = NULL, };
Jens Axboe0f212202020-09-13 13:09:39 -06008863 enum io_wq_cancel cret;
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008864 bool ret = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008865
Pavel Begunkov90df0852021-01-04 20:43:30 +00008866 if (ctx->io_wq) {
8867 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8868 &cancel, true);
8869 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8870 }
Jens Axboe0f212202020-09-13 13:09:39 -06008871
8872 /* SQPOLL thread does its own polling */
8873 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8874 while (!list_empty_careful(&ctx->iopoll_list)) {
8875 io_iopoll_try_reap_events(ctx);
8876 ret = true;
8877 }
8878 }
8879
Pavel Begunkov6b819282020-11-06 13:00:25 +00008880 ret |= io_poll_remove_all(ctx, task, NULL);
8881 ret |= io_kill_timeouts(ctx, task, NULL);
Pavel Begunkov55583d72020-12-20 13:21:43 +00008882 ret |= io_run_task_work();
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008883 if (!ret)
8884 break;
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008885 cond_resched();
Jens Axboe0f212202020-09-13 13:09:39 -06008886 }
Jens Axboe0f212202020-09-13 13:09:39 -06008887}
8888
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008889static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8890{
8891 WARN_ON_ONCE(ctx->sqo_task != current);
8892
8893 mutex_lock(&ctx->uring_lock);
8894 ctx->sqo_dead = 1;
8895 mutex_unlock(&ctx->uring_lock);
8896
8897 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008898 if (ctx->rings)
8899 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008900}
8901
Jens Axboe0f212202020-09-13 13:09:39 -06008902/*
8903 * We need to iteratively cancel requests, in case a request has dependent
8904 * hard links. These persist even for failure of cancelations, hence keep
8905 * looping until none are found.
8906 */
8907static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8908 struct files_struct *files)
8909{
8910 struct task_struct *task = current;
8911
Jens Axboefdaf0832020-10-30 09:37:30 -06008912 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008913 /* for SQPOLL only sqo_task has task notes */
8914 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008915 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008916 atomic_inc(&task->io_uring->in_idle);
8917 io_sq_thread_park(ctx->sq_data);
8918 }
Jens Axboe0f212202020-09-13 13:09:39 -06008919
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008920 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008921 io_cqring_overflow_flush(ctx, true, task, files);
8922
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008923 if (!files)
8924 __io_uring_cancel_task_requests(ctx, task);
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008925 else
8926 io_uring_cancel_files(ctx, task, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008927
8928 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8929 atomic_dec(&task->io_uring->in_idle);
8930 /*
8931 * If the files that are going away are the ones in the thread
8932 * identity, clear them out.
8933 */
8934 if (task->io_uring->identity->files == files)
8935 task->io_uring->identity->files = NULL;
8936 io_sq_thread_unpark(ctx->sq_data);
8937 }
Jens Axboe0f212202020-09-13 13:09:39 -06008938}
8939
8940/*
8941 * Note that this task has used io_uring. We use it for cancelation purposes.
8942 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008943static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008944{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008945 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008946 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008947
8948 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008949 ret = io_uring_alloc_task_context(current);
8950 if (unlikely(ret))
8951 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008952 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008953 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008954 if (tctx->last != file) {
8955 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008956
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008957 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008958 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008959 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8960 file, GFP_KERNEL));
8961 if (ret) {
8962 fput(file);
8963 return ret;
8964 }
Jens Axboe0f212202020-09-13 13:09:39 -06008965 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008966 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008967 }
8968
Jens Axboefdaf0832020-10-30 09:37:30 -06008969 /*
8970 * This is race safe in that the task itself is doing this, hence it
8971 * cannot be going through the exit/cancel paths at the same time.
8972 * This cannot be modified while exit/cancel is running.
8973 */
8974 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8975 tctx->sqpoll = true;
8976
Jens Axboe0f212202020-09-13 13:09:39 -06008977 return 0;
8978}
8979
8980/*
8981 * Remove this io_uring_file -> task mapping.
8982 */
8983static void io_uring_del_task_file(struct file *file)
8984{
8985 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008986
8987 if (tctx->last == file)
8988 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008989 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008990 if (file)
8991 fput(file);
8992}
8993
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008994static void io_uring_remove_task_files(struct io_uring_task *tctx)
8995{
8996 struct file *file;
8997 unsigned long index;
8998
8999 xa_for_each(&tctx->xa, index, file)
9000 io_uring_del_task_file(file);
9001}
9002
Jens Axboe0f212202020-09-13 13:09:39 -06009003void __io_uring_files_cancel(struct files_struct *files)
9004{
9005 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009006 struct file *file;
9007 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009008
9009 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009010 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009011 xa_for_each(&tctx->xa, index, file)
9012 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009013 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009014
9015 if (files)
9016 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009017}
9018
9019static s64 tctx_inflight(struct io_uring_task *tctx)
9020{
9021 unsigned long index;
9022 struct file *file;
9023 s64 inflight;
9024
9025 inflight = percpu_counter_sum(&tctx->inflight);
9026 if (!tctx->sqpoll)
9027 return inflight;
9028
9029 /*
9030 * If we have SQPOLL rings, then we need to iterate and find them, and
9031 * add the pending count for those.
9032 */
9033 xa_for_each(&tctx->xa, index, file) {
9034 struct io_ring_ctx *ctx = file->private_data;
9035
9036 if (ctx->flags & IORING_SETUP_SQPOLL) {
9037 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
9038
9039 inflight += percpu_counter_sum(&__tctx->inflight);
9040 }
9041 }
9042
9043 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009044}
9045
Jens Axboe0f212202020-09-13 13:09:39 -06009046/*
9047 * Find any io_uring fd that this task has registered or done IO on, and cancel
9048 * requests.
9049 */
9050void __io_uring_task_cancel(void)
9051{
9052 struct io_uring_task *tctx = current->io_uring;
9053 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009054 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009055
9056 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009057 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009058
Jens Axboed8a6df12020-10-15 16:24:45 -06009059 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009060 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009061 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009062 if (!inflight)
9063 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009064 __io_uring_files_cancel(NULL);
9065
9066 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9067
9068 /*
9069 * If we've seen completions, retry. This avoids a race where
9070 * a completion comes in before we did prepare_to_wait().
9071 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009072 if (inflight != tctx_inflight(tctx))
Jens Axboe0f212202020-09-13 13:09:39 -06009073 continue;
Jens Axboe0f212202020-09-13 13:09:39 -06009074 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009075 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009076 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009077
Jens Axboefdaf0832020-10-30 09:37:30 -06009078 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009079
9080 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009081}
9082
Jens Axboefcb323c2019-10-24 12:39:47 -06009083static int io_uring_flush(struct file *file, void *data)
9084{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009085 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009086 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009087
9088 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009089 return 0;
9090
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009091 /* we should have cancelled and erased it before PF_EXITING */
9092 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9093 xa_load(&tctx->xa, (unsigned long)file));
9094
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009095 /*
9096 * fput() is pending, will be 2 if the only other ref is our potential
9097 * task file note. If the task is exiting, drop regardless of count.
9098 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009099 if (atomic_long_read(&file->f_count) != 2)
9100 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009101
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009102 if (ctx->flags & IORING_SETUP_SQPOLL) {
9103 /* there is only one file note, which is owned by sqo_task */
9104 WARN_ON_ONCE((ctx->sqo_task == current) ==
9105 !xa_load(&tctx->xa, (unsigned long)file));
9106
9107 io_disable_sqo_submit(ctx);
9108 }
9109
9110 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9111 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009112 return 0;
9113}
9114
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009115static void *io_uring_validate_mmap_request(struct file *file,
9116 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009117{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009118 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009119 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009120 struct page *page;
9121 void *ptr;
9122
9123 switch (offset) {
9124 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009125 case IORING_OFF_CQ_RING:
9126 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009127 break;
9128 case IORING_OFF_SQES:
9129 ptr = ctx->sq_sqes;
9130 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009131 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009132 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009133 }
9134
9135 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009136 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009137 return ERR_PTR(-EINVAL);
9138
9139 return ptr;
9140}
9141
9142#ifdef CONFIG_MMU
9143
9144static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9145{
9146 size_t sz = vma->vm_end - vma->vm_start;
9147 unsigned long pfn;
9148 void *ptr;
9149
9150 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9151 if (IS_ERR(ptr))
9152 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009153
9154 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9155 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9156}
9157
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009158#else /* !CONFIG_MMU */
9159
9160static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9161{
9162 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9163}
9164
9165static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9166{
9167 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9168}
9169
9170static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9171 unsigned long addr, unsigned long len,
9172 unsigned long pgoff, unsigned long flags)
9173{
9174 void *ptr;
9175
9176 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9177 if (IS_ERR(ptr))
9178 return PTR_ERR(ptr);
9179
9180 return (unsigned long) ptr;
9181}
9182
9183#endif /* !CONFIG_MMU */
9184
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009185static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009186{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009187 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009188 DEFINE_WAIT(wait);
9189
9190 do {
9191 if (!io_sqring_full(ctx))
9192 break;
9193
9194 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9195
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009196 if (unlikely(ctx->sqo_dead)) {
9197 ret = -EOWNERDEAD;
9198 goto out;
9199 }
9200
Jens Axboe90554202020-09-03 12:12:41 -06009201 if (!io_sqring_full(ctx))
9202 break;
9203
9204 schedule();
9205 } while (!signal_pending(current));
9206
9207 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009208out:
9209 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009210}
9211
Hao Xuc73ebb62020-11-03 10:54:37 +08009212static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9213 struct __kernel_timespec __user **ts,
9214 const sigset_t __user **sig)
9215{
9216 struct io_uring_getevents_arg arg;
9217
9218 /*
9219 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9220 * is just a pointer to the sigset_t.
9221 */
9222 if (!(flags & IORING_ENTER_EXT_ARG)) {
9223 *sig = (const sigset_t __user *) argp;
9224 *ts = NULL;
9225 return 0;
9226 }
9227
9228 /*
9229 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9230 * timespec and sigset_t pointers if good.
9231 */
9232 if (*argsz != sizeof(arg))
9233 return -EINVAL;
9234 if (copy_from_user(&arg, argp, sizeof(arg)))
9235 return -EFAULT;
9236 *sig = u64_to_user_ptr(arg.sigmask);
9237 *argsz = arg.sigmask_sz;
9238 *ts = u64_to_user_ptr(arg.ts);
9239 return 0;
9240}
9241
Jens Axboe2b188cc2019-01-07 10:46:33 -07009242SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009243 u32, min_complete, u32, flags, const void __user *, argp,
9244 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009245{
9246 struct io_ring_ctx *ctx;
9247 long ret = -EBADF;
9248 int submitted = 0;
9249 struct fd f;
9250
Jens Axboe4c6e2772020-07-01 11:29:10 -06009251 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009252
Jens Axboe90554202020-09-03 12:12:41 -06009253 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009254 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009255 return -EINVAL;
9256
9257 f = fdget(fd);
9258 if (!f.file)
9259 return -EBADF;
9260
9261 ret = -EOPNOTSUPP;
9262 if (f.file->f_op != &io_uring_fops)
9263 goto out_fput;
9264
9265 ret = -ENXIO;
9266 ctx = f.file->private_data;
9267 if (!percpu_ref_tryget(&ctx->refs))
9268 goto out_fput;
9269
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009270 ret = -EBADFD;
9271 if (ctx->flags & IORING_SETUP_R_DISABLED)
9272 goto out;
9273
Jens Axboe6c271ce2019-01-10 11:22:30 -07009274 /*
9275 * For SQ polling, the thread will do all submissions and completions.
9276 * Just return the requested submit count, and wake the thread if
9277 * we were asked to.
9278 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009279 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009280 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009281 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009282
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009283 ret = -EOWNERDEAD;
9284 if (unlikely(ctx->sqo_dead))
9285 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009286 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009287 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009288 if (flags & IORING_ENTER_SQ_WAIT) {
9289 ret = io_sqpoll_wait_sq(ctx);
9290 if (ret)
9291 goto out;
9292 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009293 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009294 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009295 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009296 if (unlikely(ret))
9297 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009298 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009299 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009300 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009301
9302 if (submitted != to_submit)
9303 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009304 }
9305 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009306 const sigset_t __user *sig;
9307 struct __kernel_timespec __user *ts;
9308
9309 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9310 if (unlikely(ret))
9311 goto out;
9312
Jens Axboe2b188cc2019-01-07 10:46:33 -07009313 min_complete = min(min_complete, ctx->cq_entries);
9314
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009315 /*
9316 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9317 * space applications don't need to do io completion events
9318 * polling again, they can rely on io_sq_thread to do polling
9319 * work, which can reduce cpu usage and uring_lock contention.
9320 */
9321 if (ctx->flags & IORING_SETUP_IOPOLL &&
9322 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009323 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009324 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009325 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009326 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009327 }
9328
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009329out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009330 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009331out_fput:
9332 fdput(f);
9333 return submitted ? submitted : ret;
9334}
9335
Tobias Klauserbebdb652020-02-26 18:38:32 +01009336#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009337static int io_uring_show_cred(int id, void *p, void *data)
9338{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009339 struct io_identity *iod = p;
9340 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009341 struct seq_file *m = data;
9342 struct user_namespace *uns = seq_user_ns(m);
9343 struct group_info *gi;
9344 kernel_cap_t cap;
9345 unsigned __capi;
9346 int g;
9347
9348 seq_printf(m, "%5d\n", id);
9349 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9350 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9351 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9352 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9353 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9354 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9355 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9356 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9357 seq_puts(m, "\n\tGroups:\t");
9358 gi = cred->group_info;
9359 for (g = 0; g < gi->ngroups; g++) {
9360 seq_put_decimal_ull(m, g ? " " : "",
9361 from_kgid_munged(uns, gi->gid[g]));
9362 }
9363 seq_puts(m, "\n\tCapEff:\t");
9364 cap = cred->cap_effective;
9365 CAP_FOR_EACH_U32(__capi)
9366 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9367 seq_putc(m, '\n');
9368 return 0;
9369}
9370
9371static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9372{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009373 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009374 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009375 int i;
9376
Jens Axboefad8e0d2020-09-28 08:57:48 -06009377 /*
9378 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9379 * since fdinfo case grabs it in the opposite direction of normal use
9380 * cases. If we fail to get the lock, we just don't iterate any
9381 * structures that could be going away outside the io_uring mutex.
9382 */
9383 has_lock = mutex_trylock(&ctx->uring_lock);
9384
Joseph Qidbbe9c62020-09-29 09:01:22 -06009385 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9386 sq = ctx->sq_data;
9387
9388 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9389 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009390 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009391 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009392 struct fixed_file_table *table;
9393 struct file *f;
9394
9395 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9396 f = table->files[i & IORING_FILE_TABLE_MASK];
9397 if (f)
9398 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9399 else
9400 seq_printf(m, "%5u: <none>\n", i);
9401 }
9402 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009403 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009404 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9405
9406 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9407 (unsigned int) buf->len);
9408 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009409 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009410 seq_printf(m, "Personalities:\n");
9411 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9412 }
Jens Axboed7718a92020-02-14 22:23:12 -07009413 seq_printf(m, "PollList:\n");
9414 spin_lock_irq(&ctx->completion_lock);
9415 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9416 struct hlist_head *list = &ctx->cancel_hash[i];
9417 struct io_kiocb *req;
9418
9419 hlist_for_each_entry(req, list, hash_node)
9420 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9421 req->task->task_works != NULL);
9422 }
9423 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009424 if (has_lock)
9425 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009426}
9427
9428static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9429{
9430 struct io_ring_ctx *ctx = f->private_data;
9431
9432 if (percpu_ref_tryget(&ctx->refs)) {
9433 __io_uring_show_fdinfo(ctx, m);
9434 percpu_ref_put(&ctx->refs);
9435 }
9436}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009437#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009438
Jens Axboe2b188cc2019-01-07 10:46:33 -07009439static const struct file_operations io_uring_fops = {
9440 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009441 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009442 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009443#ifndef CONFIG_MMU
9444 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9445 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9446#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009447 .poll = io_uring_poll,
9448 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009449#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009450 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009451#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009452};
9453
9454static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9455 struct io_uring_params *p)
9456{
Hristo Venev75b28af2019-08-26 17:23:46 +00009457 struct io_rings *rings;
9458 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009459
Jens Axboebd740482020-08-05 12:58:23 -06009460 /* make sure these are sane, as we already accounted them */
9461 ctx->sq_entries = p->sq_entries;
9462 ctx->cq_entries = p->cq_entries;
9463
Hristo Venev75b28af2019-08-26 17:23:46 +00009464 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9465 if (size == SIZE_MAX)
9466 return -EOVERFLOW;
9467
9468 rings = io_mem_alloc(size);
9469 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009470 return -ENOMEM;
9471
Hristo Venev75b28af2019-08-26 17:23:46 +00009472 ctx->rings = rings;
9473 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9474 rings->sq_ring_mask = p->sq_entries - 1;
9475 rings->cq_ring_mask = p->cq_entries - 1;
9476 rings->sq_ring_entries = p->sq_entries;
9477 rings->cq_ring_entries = p->cq_entries;
9478 ctx->sq_mask = rings->sq_ring_mask;
9479 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009480
9481 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009482 if (size == SIZE_MAX) {
9483 io_mem_free(ctx->rings);
9484 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009485 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009486 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009487
9488 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009489 if (!ctx->sq_sqes) {
9490 io_mem_free(ctx->rings);
9491 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009492 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009493 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009494
Jens Axboe2b188cc2019-01-07 10:46:33 -07009495 return 0;
9496}
9497
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009498static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9499{
9500 int ret, fd;
9501
9502 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9503 if (fd < 0)
9504 return fd;
9505
9506 ret = io_uring_add_task_file(ctx, file);
9507 if (ret) {
9508 put_unused_fd(fd);
9509 return ret;
9510 }
9511 fd_install(fd, file);
9512 return fd;
9513}
9514
Jens Axboe2b188cc2019-01-07 10:46:33 -07009515/*
9516 * Allocate an anonymous fd, this is what constitutes the application
9517 * visible backing of an io_uring instance. The application mmaps this
9518 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9519 * we have to tie this fd to a socket for file garbage collection purposes.
9520 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009521static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009522{
9523 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009524#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009525 int ret;
9526
Jens Axboe2b188cc2019-01-07 10:46:33 -07009527 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9528 &ctx->ring_sock);
9529 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009530 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009531#endif
9532
Jens Axboe2b188cc2019-01-07 10:46:33 -07009533 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9534 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009535#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009536 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009537 sock_release(ctx->ring_sock);
9538 ctx->ring_sock = NULL;
9539 } else {
9540 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009541 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009542#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009543 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009544}
9545
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009546static int io_uring_create(unsigned entries, struct io_uring_params *p,
9547 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009548{
9549 struct user_struct *user = NULL;
9550 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009551 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009552 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009553 int ret;
9554
Jens Axboe8110c1a2019-12-28 15:39:54 -07009555 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009556 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009557 if (entries > IORING_MAX_ENTRIES) {
9558 if (!(p->flags & IORING_SETUP_CLAMP))
9559 return -EINVAL;
9560 entries = IORING_MAX_ENTRIES;
9561 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009562
9563 /*
9564 * Use twice as many entries for the CQ ring. It's possible for the
9565 * application to drive a higher depth than the size of the SQ ring,
9566 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009567 * some flexibility in overcommitting a bit. If the application has
9568 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9569 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009570 */
9571 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009572 if (p->flags & IORING_SETUP_CQSIZE) {
9573 /*
9574 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9575 * to a power-of-two, if it isn't already. We do NOT impose
9576 * any cq vs sq ring sizing.
9577 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009578 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009579 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009580 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9581 if (!(p->flags & IORING_SETUP_CLAMP))
9582 return -EINVAL;
9583 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9584 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009585 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9586 if (p->cq_entries < p->sq_entries)
9587 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009588 } else {
9589 p->cq_entries = 2 * p->sq_entries;
9590 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009591
9592 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009593 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009594
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009595 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009596 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009597 ring_pages(p->sq_entries, p->cq_entries));
9598 if (ret) {
9599 free_uid(user);
9600 return ret;
9601 }
9602 }
9603
9604 ctx = io_ring_ctx_alloc(p);
9605 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009606 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009607 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009608 p->cq_entries));
9609 free_uid(user);
9610 return -ENOMEM;
9611 }
9612 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009613 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009614 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009615#ifdef CONFIG_AUDIT
9616 ctx->loginuid = current->loginuid;
9617 ctx->sessionid = current->sessionid;
9618#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009619 ctx->sqo_task = get_task_struct(current);
9620
9621 /*
9622 * This is just grabbed for accounting purposes. When a process exits,
9623 * the mm is exited and dropped before the files, hence we need to hang
9624 * on to this mm purely for the purposes of being able to unaccount
9625 * memory (locked/pinned vm). It's not used for anything else.
9626 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009627 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009628 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009629
Dennis Zhou91d8f512020-09-16 13:41:05 -07009630#ifdef CONFIG_BLK_CGROUP
9631 /*
9632 * The sq thread will belong to the original cgroup it was inited in.
9633 * If the cgroup goes offline (e.g. disabling the io controller), then
9634 * issued bios will be associated with the closest cgroup later in the
9635 * block layer.
9636 */
9637 rcu_read_lock();
9638 ctx->sqo_blkcg_css = blkcg_css();
9639 ret = css_tryget_online(ctx->sqo_blkcg_css);
9640 rcu_read_unlock();
9641 if (!ret) {
9642 /* don't init against a dying cgroup, have the user try again */
9643 ctx->sqo_blkcg_css = NULL;
9644 ret = -ENODEV;
9645 goto err;
9646 }
9647#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009648
Jens Axboe2b188cc2019-01-07 10:46:33 -07009649 /*
9650 * Account memory _before_ installing the file descriptor. Once
9651 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009652 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009653 * will un-account as well.
9654 */
9655 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9656 ACCT_LOCKED);
9657 ctx->limit_mem = limit_mem;
9658
9659 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009660 if (ret)
9661 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009662
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009663 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009664 if (ret)
9665 goto err;
9666
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009667 if (!(p->flags & IORING_SETUP_R_DISABLED))
9668 io_sq_offload_start(ctx);
9669
Jens Axboe2b188cc2019-01-07 10:46:33 -07009670 memset(&p->sq_off, 0, sizeof(p->sq_off));
9671 p->sq_off.head = offsetof(struct io_rings, sq.head);
9672 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9673 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9674 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9675 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9676 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9677 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9678
9679 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009680 p->cq_off.head = offsetof(struct io_rings, cq.head);
9681 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9682 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9683 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9684 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9685 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009686 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009687
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009688 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9689 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009690 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009691 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9692 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009693
9694 if (copy_to_user(params, p, sizeof(*p))) {
9695 ret = -EFAULT;
9696 goto err;
9697 }
Jens Axboed1719f72020-07-30 13:43:53 -06009698
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009699 file = io_uring_get_file(ctx);
9700 if (IS_ERR(file)) {
9701 ret = PTR_ERR(file);
9702 goto err;
9703 }
9704
Jens Axboed1719f72020-07-30 13:43:53 -06009705 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009706 * Install ring fd as the very last thing, so we don't risk someone
9707 * having closed it before we finish setup
9708 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009709 ret = io_uring_install_fd(ctx, file);
9710 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009711 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009712 /* fput will clean it up */
9713 fput(file);
9714 return ret;
9715 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009716
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009717 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009718 return ret;
9719err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009720 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009721 io_ring_ctx_wait_and_kill(ctx);
9722 return ret;
9723}
9724
9725/*
9726 * Sets up an aio uring context, and returns the fd. Applications asks for a
9727 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9728 * params structure passed in.
9729 */
9730static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9731{
9732 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009733 int i;
9734
9735 if (copy_from_user(&p, params, sizeof(p)))
9736 return -EFAULT;
9737 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9738 if (p.resv[i])
9739 return -EINVAL;
9740 }
9741
Jens Axboe6c271ce2019-01-10 11:22:30 -07009742 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009743 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009744 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9745 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009746 return -EINVAL;
9747
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009748 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009749}
9750
9751SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9752 struct io_uring_params __user *, params)
9753{
9754 return io_uring_setup(entries, params);
9755}
9756
Jens Axboe66f4af92020-01-16 15:36:52 -07009757static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9758{
9759 struct io_uring_probe *p;
9760 size_t size;
9761 int i, ret;
9762
9763 size = struct_size(p, ops, nr_args);
9764 if (size == SIZE_MAX)
9765 return -EOVERFLOW;
9766 p = kzalloc(size, GFP_KERNEL);
9767 if (!p)
9768 return -ENOMEM;
9769
9770 ret = -EFAULT;
9771 if (copy_from_user(p, arg, size))
9772 goto out;
9773 ret = -EINVAL;
9774 if (memchr_inv(p, 0, size))
9775 goto out;
9776
9777 p->last_op = IORING_OP_LAST - 1;
9778 if (nr_args > IORING_OP_LAST)
9779 nr_args = IORING_OP_LAST;
9780
9781 for (i = 0; i < nr_args; i++) {
9782 p->ops[i].op = i;
9783 if (!io_op_defs[i].not_supported)
9784 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9785 }
9786 p->ops_len = i;
9787
9788 ret = 0;
9789 if (copy_to_user(arg, p, size))
9790 ret = -EFAULT;
9791out:
9792 kfree(p);
9793 return ret;
9794}
9795
Jens Axboe071698e2020-01-28 10:04:42 -07009796static int io_register_personality(struct io_ring_ctx *ctx)
9797{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009798 struct io_identity *id;
9799 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009800
Jens Axboe1e6fa522020-10-15 08:46:24 -06009801 id = kmalloc(sizeof(*id), GFP_KERNEL);
9802 if (unlikely(!id))
9803 return -ENOMEM;
9804
9805 io_init_identity(id);
9806 id->creds = get_current_cred();
9807
9808 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9809 if (ret < 0) {
9810 put_cred(id->creds);
9811 kfree(id);
9812 }
9813 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009814}
9815
9816static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9817{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009818 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07009819
Jens Axboe1e6fa522020-10-15 08:46:24 -06009820 iod = idr_remove(&ctx->personality_idr, id);
9821 if (iod) {
9822 put_cred(iod->creds);
9823 if (refcount_dec_and_test(&iod->count))
9824 kfree(iod);
Jens Axboe071698e2020-01-28 10:04:42 -07009825 return 0;
9826 }
9827
9828 return -EINVAL;
9829}
9830
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009831static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9832 unsigned int nr_args)
9833{
9834 struct io_uring_restriction *res;
9835 size_t size;
9836 int i, ret;
9837
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009838 /* Restrictions allowed only if rings started disabled */
9839 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9840 return -EBADFD;
9841
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009842 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009843 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009844 return -EBUSY;
9845
9846 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9847 return -EINVAL;
9848
9849 size = array_size(nr_args, sizeof(*res));
9850 if (size == SIZE_MAX)
9851 return -EOVERFLOW;
9852
9853 res = memdup_user(arg, size);
9854 if (IS_ERR(res))
9855 return PTR_ERR(res);
9856
9857 ret = 0;
9858
9859 for (i = 0; i < nr_args; i++) {
9860 switch (res[i].opcode) {
9861 case IORING_RESTRICTION_REGISTER_OP:
9862 if (res[i].register_op >= IORING_REGISTER_LAST) {
9863 ret = -EINVAL;
9864 goto out;
9865 }
9866
9867 __set_bit(res[i].register_op,
9868 ctx->restrictions.register_op);
9869 break;
9870 case IORING_RESTRICTION_SQE_OP:
9871 if (res[i].sqe_op >= IORING_OP_LAST) {
9872 ret = -EINVAL;
9873 goto out;
9874 }
9875
9876 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9877 break;
9878 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9879 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9880 break;
9881 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9882 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9883 break;
9884 default:
9885 ret = -EINVAL;
9886 goto out;
9887 }
9888 }
9889
9890out:
9891 /* Reset all restrictions if an error happened */
9892 if (ret != 0)
9893 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9894 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009895 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009896
9897 kfree(res);
9898 return ret;
9899}
9900
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009901static int io_register_enable_rings(struct io_ring_ctx *ctx)
9902{
9903 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9904 return -EBADFD;
9905
9906 if (ctx->restrictions.registered)
9907 ctx->restricted = 1;
9908
9909 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9910
9911 io_sq_offload_start(ctx);
9912
9913 return 0;
9914}
9915
Jens Axboe071698e2020-01-28 10:04:42 -07009916static bool io_register_op_must_quiesce(int op)
9917{
9918 switch (op) {
9919 case IORING_UNREGISTER_FILES:
9920 case IORING_REGISTER_FILES_UPDATE:
9921 case IORING_REGISTER_PROBE:
9922 case IORING_REGISTER_PERSONALITY:
9923 case IORING_UNREGISTER_PERSONALITY:
9924 return false;
9925 default:
9926 return true;
9927 }
9928}
9929
Jens Axboeedafcce2019-01-09 09:16:05 -07009930static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9931 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009932 __releases(ctx->uring_lock)
9933 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009934{
9935 int ret;
9936
Jens Axboe35fa71a2019-04-22 10:23:23 -06009937 /*
9938 * We're inside the ring mutex, if the ref is already dying, then
9939 * someone else killed the ctx or is already going through
9940 * io_uring_register().
9941 */
9942 if (percpu_ref_is_dying(&ctx->refs))
9943 return -ENXIO;
9944
Jens Axboe071698e2020-01-28 10:04:42 -07009945 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009946 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009947
Jens Axboe05f3fb32019-12-09 11:22:50 -07009948 /*
9949 * Drop uring mutex before waiting for references to exit. If
9950 * another thread is currently inside io_uring_enter() it might
9951 * need to grab the uring_lock to make progress. If we hold it
9952 * here across the drain wait, then we can deadlock. It's safe
9953 * to drop the mutex here, since no new references will come in
9954 * after we've killed the percpu ref.
9955 */
9956 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009957 do {
9958 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9959 if (!ret)
9960 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009961 ret = io_run_task_work_sig();
9962 if (ret < 0)
9963 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009964 } while (1);
9965
Jens Axboe05f3fb32019-12-09 11:22:50 -07009966 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009967
Jens Axboec1503682020-01-08 08:26:07 -07009968 if (ret) {
9969 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009970 goto out_quiesce;
9971 }
9972 }
9973
9974 if (ctx->restricted) {
9975 if (opcode >= IORING_REGISTER_LAST) {
9976 ret = -EINVAL;
9977 goto out;
9978 }
9979
9980 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9981 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009982 goto out;
9983 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009984 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009985
9986 switch (opcode) {
9987 case IORING_REGISTER_BUFFERS:
9988 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9989 break;
9990 case IORING_UNREGISTER_BUFFERS:
9991 ret = -EINVAL;
9992 if (arg || nr_args)
9993 break;
9994 ret = io_sqe_buffer_unregister(ctx);
9995 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009996 case IORING_REGISTER_FILES:
9997 ret = io_sqe_files_register(ctx, arg, nr_args);
9998 break;
9999 case IORING_UNREGISTER_FILES:
10000 ret = -EINVAL;
10001 if (arg || nr_args)
10002 break;
10003 ret = io_sqe_files_unregister(ctx);
10004 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010005 case IORING_REGISTER_FILES_UPDATE:
10006 ret = io_sqe_files_update(ctx, arg, nr_args);
10007 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010008 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010009 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010010 ret = -EINVAL;
10011 if (nr_args != 1)
10012 break;
10013 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010014 if (ret)
10015 break;
10016 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10017 ctx->eventfd_async = 1;
10018 else
10019 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010020 break;
10021 case IORING_UNREGISTER_EVENTFD:
10022 ret = -EINVAL;
10023 if (arg || nr_args)
10024 break;
10025 ret = io_eventfd_unregister(ctx);
10026 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010027 case IORING_REGISTER_PROBE:
10028 ret = -EINVAL;
10029 if (!arg || nr_args > 256)
10030 break;
10031 ret = io_probe(ctx, arg, nr_args);
10032 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010033 case IORING_REGISTER_PERSONALITY:
10034 ret = -EINVAL;
10035 if (arg || nr_args)
10036 break;
10037 ret = io_register_personality(ctx);
10038 break;
10039 case IORING_UNREGISTER_PERSONALITY:
10040 ret = -EINVAL;
10041 if (arg)
10042 break;
10043 ret = io_unregister_personality(ctx, nr_args);
10044 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010045 case IORING_REGISTER_ENABLE_RINGS:
10046 ret = -EINVAL;
10047 if (arg || nr_args)
10048 break;
10049 ret = io_register_enable_rings(ctx);
10050 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010051 case IORING_REGISTER_RESTRICTIONS:
10052 ret = io_register_restrictions(ctx, arg, nr_args);
10053 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010054 default:
10055 ret = -EINVAL;
10056 break;
10057 }
10058
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010059out:
Jens Axboe071698e2020-01-28 10:04:42 -070010060 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010061 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010062 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010063out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010064 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010065 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010066 return ret;
10067}
10068
10069SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10070 void __user *, arg, unsigned int, nr_args)
10071{
10072 struct io_ring_ctx *ctx;
10073 long ret = -EBADF;
10074 struct fd f;
10075
10076 f = fdget(fd);
10077 if (!f.file)
10078 return -EBADF;
10079
10080 ret = -EOPNOTSUPP;
10081 if (f.file->f_op != &io_uring_fops)
10082 goto out_fput;
10083
10084 ctx = f.file->private_data;
10085
10086 mutex_lock(&ctx->uring_lock);
10087 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10088 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010089 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10090 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010091out_fput:
10092 fdput(f);
10093 return ret;
10094}
10095
Jens Axboe2b188cc2019-01-07 10:46:33 -070010096static int __init io_uring_init(void)
10097{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010098#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10099 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10100 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10101} while (0)
10102
10103#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10104 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10105 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10106 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10107 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10108 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10109 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10110 BUILD_BUG_SQE_ELEM(8, __u64, off);
10111 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10112 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010113 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010114 BUILD_BUG_SQE_ELEM(24, __u32, len);
10115 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10116 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10117 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10118 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010119 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10120 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010121 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10122 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10123 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10124 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10125 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10126 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10127 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10128 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010129 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010130 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10131 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10132 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010133 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010134
Jens Axboed3656342019-12-18 09:50:26 -070010135 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010136 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010137 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10138 return 0;
10139};
10140__initcall(io_uring_init);