blob: c1f3980945e478d001e9b170ec99a5c59aa800be [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;
248};
249
Jens Axboe2b188cc2019-01-07 10:46:33 -0700250struct io_ring_ctx {
251 struct {
252 struct percpu_ref refs;
253 } ____cacheline_aligned_in_smp;
254
255 struct {
256 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800257 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700258 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800259 unsigned int cq_overflow_flushed: 1;
260 unsigned int drain_next: 1;
261 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200262 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263
Hristo Venev75b28af2019-08-26 17:23:46 +0000264 /*
265 * Ring buffer of indices into array of io_uring_sqe, which is
266 * mmapped by the application using the IORING_OFF_SQES offset.
267 *
268 * This indirection could e.g. be used to assign fixed
269 * io_uring_sqe entries to operations and only submit them to
270 * the queue when needed.
271 *
272 * The kernel modifies neither the indices array nor the entries
273 * array.
274 */
275 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276 unsigned cached_sq_head;
277 unsigned sq_entries;
278 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700279 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600280 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100281 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700282 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600283
284 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600285 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700286 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700287
Jens Axboefcb323c2019-10-24 12:39:47 -0600288 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700289 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290 } ____cacheline_aligned_in_smp;
291
Hristo Venev75b28af2019-08-26 17:23:46 +0000292 struct io_rings *rings;
293
Jens Axboe2b188cc2019-01-07 10:46:33 -0700294 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600295 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600296
297 /*
298 * For SQPOLL usage - we hold a reference to the parent task, so we
299 * have access to the ->files
300 */
301 struct task_struct *sqo_task;
302
303 /* Only used for accounting purposes */
304 struct mm_struct *mm_account;
305
Dennis Zhou91d8f512020-09-16 13:41:05 -0700306#ifdef CONFIG_BLK_CGROUP
307 struct cgroup_subsys_state *sqo_blkcg_css;
308#endif
309
Jens Axboe534ca6d2020-09-02 13:52:19 -0600310 struct io_sq_data *sq_data; /* if using sq thread polling */
311
Jens Axboe90554202020-09-03 12:12:41 -0600312 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600313 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600314 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700315
Jens Axboe6b063142019-01-10 22:13:58 -0700316 /*
317 * If used, fixed file set. Writers must ensure that ->refs is dead,
318 * readers must ensure that ->refs is alive as long as the file* is
319 * used. Only updated through io_uring_register(2).
320 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700321 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700322 unsigned nr_user_files;
323
Jens Axboeedafcce2019-01-09 09:16:05 -0700324 /* if used, fixed mapped user buffers */
325 unsigned nr_user_bufs;
326 struct io_mapped_ubuf *user_bufs;
327
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 struct user_struct *user;
329
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700330 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700331
Jens Axboe4ea33a92020-10-15 13:46:44 -0600332#ifdef CONFIG_AUDIT
333 kuid_t loginuid;
334 unsigned int sessionid;
335#endif
336
Jens Axboe0f158b42020-05-14 17:18:39 -0600337 struct completion ref_comp;
338 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700339
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700340 /* if all else fails... */
341 struct io_kiocb *fallback_req;
342
Jens Axboe206aefd2019-11-07 18:27:42 -0700343#if defined(CONFIG_UNIX)
344 struct socket *ring_sock;
345#endif
346
Jens Axboe5a2e7452020-02-23 16:23:11 -0700347 struct idr io_buffer_idr;
348
Jens Axboe071698e2020-01-28 10:04:42 -0700349 struct idr personality_idr;
350
Jens Axboe206aefd2019-11-07 18:27:42 -0700351 struct {
352 unsigned cached_cq_tail;
353 unsigned cq_entries;
354 unsigned cq_mask;
355 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700356 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700357 struct wait_queue_head cq_wait;
358 struct fasync_struct *cq_fasync;
359 struct eventfd_ctx *cq_ev_fd;
360 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700361
362 struct {
363 struct mutex uring_lock;
364 wait_queue_head_t wait;
365 } ____cacheline_aligned_in_smp;
366
367 struct {
368 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700369
Jens Axboedef596e2019-01-09 08:59:42 -0700370 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300371 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700372 * io_uring instances that don't use IORING_SETUP_SQPOLL.
373 * For SQPOLL, only the single threaded io_sq_thread() will
374 * manipulate the list, hence no extra locking is needed there.
375 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300376 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700377 struct hlist_head *cancel_hash;
378 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700379 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600380
381 spinlock_t inflight_lock;
382 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600384
Jens Axboe4a38aed22020-05-14 17:21:15 -0600385 struct delayed_work file_put_work;
386 struct llist_head file_put_llist;
387
Jens Axboe85faa7b2020-04-09 18:14:00 -0600388 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200389 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700390};
391
Jens Axboe09bb8392019-03-13 12:39:28 -0600392/*
393 * First field must be the file pointer in all the
394 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
395 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700396struct io_poll_iocb {
397 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700398 union {
399 struct wait_queue_head *head;
400 u64 addr;
401 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700402 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600403 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700404 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700405 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700406};
407
Jens Axboeb5dba592019-12-11 14:02:38 -0700408struct io_close {
409 struct file *file;
410 struct file *put_file;
411 int fd;
412};
413
Jens Axboead8a48a2019-11-15 08:49:11 -0700414struct io_timeout_data {
415 struct io_kiocb *req;
416 struct hrtimer timer;
417 struct timespec64 ts;
418 enum hrtimer_mode mode;
419};
420
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700421struct io_accept {
422 struct file *file;
423 struct sockaddr __user *addr;
424 int __user *addr_len;
425 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600426 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700427};
428
429struct io_sync {
430 struct file *file;
431 loff_t len;
432 loff_t off;
433 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700434 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700435};
436
Jens Axboefbf23842019-12-17 18:45:56 -0700437struct io_cancel {
438 struct file *file;
439 u64 addr;
440};
441
Jens Axboeb29472e2019-12-17 18:50:29 -0700442struct io_timeout {
443 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300444 u32 off;
445 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300446 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700447};
448
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100449struct io_timeout_rem {
450 struct file *file;
451 u64 addr;
452};
453
Jens Axboe9adbd452019-12-20 08:45:55 -0700454struct io_rw {
455 /* NOTE: kiocb has the file as the first member, so don't do it here */
456 struct kiocb kiocb;
457 u64 addr;
458 u64 len;
459};
460
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700461struct io_connect {
462 struct file *file;
463 struct sockaddr __user *addr;
464 int addr_len;
465};
466
Jens Axboee47293f2019-12-20 08:58:21 -0700467struct io_sr_msg {
468 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700469 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300470 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700471 void __user *buf;
472 };
Jens Axboee47293f2019-12-20 08:58:21 -0700473 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700474 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700475 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700476 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700477};
478
Jens Axboe15b71ab2019-12-11 11:20:36 -0700479struct io_open {
480 struct file *file;
481 int dfd;
Jens Axboe944d1442020-11-13 16:48:44 -0700482 bool ignore_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700483 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700484 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600485 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700486};
487
Jens Axboe05f3fb32019-12-09 11:22:50 -0700488struct io_files_update {
489 struct file *file;
490 u64 arg;
491 u32 nr_args;
492 u32 offset;
493};
494
Jens Axboe4840e412019-12-25 22:03:45 -0700495struct io_fadvise {
496 struct file *file;
497 u64 offset;
498 u32 len;
499 u32 advice;
500};
501
Jens Axboec1ca7572019-12-25 22:18:28 -0700502struct io_madvise {
503 struct file *file;
504 u64 addr;
505 u32 len;
506 u32 advice;
507};
508
Jens Axboe3e4827b2020-01-08 15:18:09 -0700509struct io_epoll {
510 struct file *file;
511 int epfd;
512 int op;
513 int fd;
514 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700515};
516
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300517struct io_splice {
518 struct file *file_out;
519 struct file *file_in;
520 loff_t off_out;
521 loff_t off_in;
522 u64 len;
523 unsigned int flags;
524};
525
Jens Axboeddf0322d2020-02-23 16:41:33 -0700526struct io_provide_buf {
527 struct file *file;
528 __u64 addr;
529 __s32 len;
530 __u32 bgid;
531 __u16 nbufs;
532 __u16 bid;
533};
534
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700535struct io_statx {
536 struct file *file;
537 int dfd;
538 unsigned int mask;
539 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700540 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700541 struct statx __user *buffer;
542};
543
Jens Axboe36f4fa62020-09-05 11:14:22 -0600544struct io_shutdown {
545 struct file *file;
546 int how;
547};
548
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300549struct io_completion {
550 struct file *file;
551 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300552 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300553};
554
Jens Axboef499a022019-12-02 16:28:46 -0700555struct io_async_connect {
556 struct sockaddr_storage address;
557};
558
Jens Axboe03b12302019-12-02 18:50:25 -0700559struct io_async_msghdr {
560 struct iovec fast_iov[UIO_FASTIOV];
561 struct iovec *iov;
562 struct sockaddr __user *uaddr;
563 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700564 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700565};
566
Jens Axboef67676d2019-12-02 11:03:47 -0700567struct io_async_rw {
568 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600569 const struct iovec *free_iovec;
570 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600571 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600572 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700573};
574
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575enum {
576 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
577 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
578 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
579 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
580 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700581 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300582
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300583 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300584 REQ_F_FAIL_LINK_BIT,
585 REQ_F_INFLIGHT_BIT,
586 REQ_F_CUR_POS_BIT,
587 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300588 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300589 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300590 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700591 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700592 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600593 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800594 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100595 REQ_F_LTIMEOUT_ACTIVE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700596
597 /* not a real bit, just to check we're not overflowing the space */
598 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300599};
600
601enum {
602 /* ctx owns file */
603 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
604 /* drain existing IO first */
605 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
606 /* linked sqes */
607 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
608 /* doesn't sever on completion < 0 */
609 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
610 /* IOSQE_ASYNC */
611 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700612 /* IOSQE_BUFFER_SELECT */
613 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300614
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300615 /* head of a link */
616 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300617 /* fail rest of links */
618 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
619 /* on inflight list */
620 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
621 /* read/write uses file position */
622 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
623 /* must not punt to workers */
624 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100625 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300626 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300627 /* regular file */
628 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300629 /* needs cleanup */
630 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700631 /* already went through poll handler */
632 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700633 /* buffer already selected */
634 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600635 /* doesn't need file table for this request */
636 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800637 /* io_wq_work is initialized */
638 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100639 /* linked timeout is active, i.e. prepared by link's head */
640 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700641};
642
643struct async_poll {
644 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600645 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300646};
647
Jens Axboe09bb8392019-03-13 12:39:28 -0600648/*
649 * NOTE! Each of the iocb union members has the file pointer
650 * as the first entry in their struct definition. So you can
651 * access the file pointer through any of the sub-structs,
652 * or directly as just 'ki_filp' in this struct.
653 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700654struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700655 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600656 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700657 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700658 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700659 struct io_accept accept;
660 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700661 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700662 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100663 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700664 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700665 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700666 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700667 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700668 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700669 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700670 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700671 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300672 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700673 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700674 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600675 struct io_shutdown shutdown;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300676 /* use only after cleaning per-op data, see io_clean_op() */
677 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700678 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700679
Jens Axboee8c2bc12020-08-15 18:44:09 -0700680 /* opcode allocated if it needs to store data for async defer */
681 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700682 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800683 /* polled IO has completed */
684 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700685
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700686 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300687 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700688
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300689 struct io_ring_ctx *ctx;
690 unsigned int flags;
691 refcount_t refs;
692 struct task_struct *task;
693 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700694
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300695 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700696
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300697 /*
698 * 1. used with ctx->iopoll_list with reads/writes
699 * 2. to track reqs with ->files (see io_op_def::file_table)
700 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300701 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600702
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300703 struct percpu_ref *fixed_file_refs;
704 struct callback_head task_work;
705 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
706 struct hlist_node hash_node;
707 struct async_poll *apoll;
708 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709};
710
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300711struct io_defer_entry {
712 struct list_head list;
713 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300714 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300715};
716
Jens Axboedef596e2019-01-09 08:59:42 -0700717#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700718
Jens Axboe013538b2020-06-22 09:29:15 -0600719struct io_comp_state {
720 unsigned int nr;
721 struct list_head list;
722 struct io_ring_ctx *ctx;
723};
724
Jens Axboe9a56a232019-01-09 09:06:50 -0700725struct io_submit_state {
726 struct blk_plug plug;
727
728 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700729 * io_kiocb alloc cache
730 */
731 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300732 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700733
734 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600735 * Batch completion logic
736 */
737 struct io_comp_state comp;
738
739 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700740 * File reference cache
741 */
742 struct file *file;
743 unsigned int fd;
744 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700745 unsigned int ios_left;
746};
747
Jens Axboed3656342019-12-18 09:50:26 -0700748struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700749 /* needs req->file assigned */
750 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600751 /* don't fail if file grab fails */
752 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700753 /* hash wq insertion if file is a regular file */
754 unsigned hash_reg_file : 1;
755 /* unbound wq insertion if file is a non-regular file */
756 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700757 /* opcode is not supported by this kernel */
758 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700759 /* set if opcode supports polled "wait" */
760 unsigned pollin : 1;
761 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700762 /* op supports buffer selection */
763 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700764 /* must always have async data allocated */
765 unsigned needs_async_data : 1;
766 /* size of async data needed, if any */
767 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600768 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700769};
770
Jens Axboe09186822020-10-13 15:01:40 -0600771static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300772 [IORING_OP_NOP] = {},
773 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700774 .needs_file = 1,
775 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700776 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700777 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700778 .needs_async_data = 1,
779 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600780 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700783 .needs_file = 1,
784 .hash_reg_file = 1,
785 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700786 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700787 .needs_async_data = 1,
788 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600789 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
790 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700793 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600794 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_file = 1,
798 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700799 .pollin = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700800 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600801 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700804 .needs_file = 1,
805 .hash_reg_file = 1,
806 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700807 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700808 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600809 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
810 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700811 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300812 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700813 .needs_file = 1,
814 .unbound_nonreg_file = 1,
815 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_POLL_REMOVE] = {},
817 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700818 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600819 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700820 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300821 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700822 .needs_file = 1,
823 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700824 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700825 .needs_async_data = 1,
826 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600827 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
828 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700831 .needs_file = 1,
832 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700833 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700834 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700835 .needs_async_data = 1,
836 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600837 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
838 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700841 .needs_async_data = 1,
842 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600843 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_TIMEOUT_REMOVE] = {},
846 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700847 .needs_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700849 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600850 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_ASYNC_CANCEL] = {},
853 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700854 .needs_async_data = 1,
855 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600856 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700857 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300858 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700859 .needs_file = 1,
860 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700861 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700862 .needs_async_data = 1,
863 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600864 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700867 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600868 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600871 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
872 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700873 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300874 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600875 .needs_file = 1,
876 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600877 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700878 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300879 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600880 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600883 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
884 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700885 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300886 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700887 .needs_file = 1,
888 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700889 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700890 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700891 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600892 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700893 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300894 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700895 .needs_file = 1,
896 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700897 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700898 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600899 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
900 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700901 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300902 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700903 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600904 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600907 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700908 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300909 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700910 .needs_file = 1,
911 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700912 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600913 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700914 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300915 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700916 .needs_file = 1,
917 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700918 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700919 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600920 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700921 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300922 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600923 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
924 IO_WQ_WORK_BLKCG,
Jens Axboecebdb982020-01-08 17:59:24 -0700925 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700926 [IORING_OP_EPOLL_CTL] = {
927 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600928 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700929 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300930 [IORING_OP_SPLICE] = {
931 .needs_file = 1,
932 .hash_reg_file = 1,
933 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600934 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700935 },
936 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700937 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300938 [IORING_OP_TEE] = {
939 .needs_file = 1,
940 .hash_reg_file = 1,
941 .unbound_nonreg_file = 1,
942 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600943 [IORING_OP_SHUTDOWN] = {
944 .needs_file = 1,
945 },
Jens Axboed3656342019-12-18 09:50:26 -0700946};
947
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700948enum io_mem_account {
949 ACCT_LOCKED,
950 ACCT_PINNED,
951};
952
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300953static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
954 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700955static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800956static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +0100957static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -0600958static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700959static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600960static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700961static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700962static int __io_sqe_files_update(struct io_ring_ctx *ctx,
963 struct io_uring_files_update *ip,
964 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300965static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +0100966static struct file *io_file_get(struct io_submit_state *state,
967 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +0300968static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600969static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600970
Jens Axboeb63534c2020-06-04 11:28:00 -0600971static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
972 struct iovec **iovec, struct iov_iter *iter,
973 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600974static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
975 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600976 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700977
978static struct kmem_cache *req_cachep;
979
Jens Axboe09186822020-10-13 15:01:40 -0600980static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700981
982struct sock *io_uring_get_socket(struct file *file)
983{
984#if defined(CONFIG_UNIX)
985 if (file->f_op == &io_uring_fops) {
986 struct io_ring_ctx *ctx = file->private_data;
987
988 return ctx->ring_sock->sk;
989 }
990#endif
991 return NULL;
992}
993EXPORT_SYMBOL(io_uring_get_socket);
994
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300995static inline void io_clean_op(struct io_kiocb *req)
996{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300997 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
998 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300999 __io_clean_op(req);
1000}
1001
Jens Axboe28cea78a2020-09-14 10:51:17 -06001002static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001003{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001004 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001005 struct mm_struct *mm = current->mm;
1006
1007 if (mm) {
1008 kthread_unuse_mm(mm);
1009 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001010 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001011 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001012 if (files) {
1013 struct nsproxy *nsproxy = current->nsproxy;
1014
1015 task_lock(current);
1016 current->files = NULL;
1017 current->nsproxy = NULL;
1018 task_unlock(current);
1019 put_files_struct(files);
1020 put_nsproxy(nsproxy);
1021 }
1022}
1023
1024static void __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
1025{
1026 if (!current->files) {
1027 struct files_struct *files;
1028 struct nsproxy *nsproxy;
1029
1030 task_lock(ctx->sqo_task);
1031 files = ctx->sqo_task->files;
1032 if (!files) {
1033 task_unlock(ctx->sqo_task);
1034 return;
1035 }
1036 atomic_inc(&files->count);
1037 get_nsproxy(ctx->sqo_task->nsproxy);
1038 nsproxy = ctx->sqo_task->nsproxy;
1039 task_unlock(ctx->sqo_task);
1040
1041 task_lock(current);
1042 current->files = files;
1043 current->nsproxy = nsproxy;
1044 task_unlock(current);
1045 }
Jens Axboec40f6372020-06-25 15:39:59 -06001046}
1047
1048static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1049{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001050 struct mm_struct *mm;
1051
1052 if (current->mm)
1053 return 0;
1054
1055 /* Should never happen */
1056 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1057 return -EFAULT;
1058
1059 task_lock(ctx->sqo_task);
1060 mm = ctx->sqo_task->mm;
1061 if (unlikely(!mm || !mmget_not_zero(mm)))
1062 mm = NULL;
1063 task_unlock(ctx->sqo_task);
1064
1065 if (mm) {
1066 kthread_use_mm(mm);
1067 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001068 }
1069
Jens Axboe4b70cf92020-11-02 10:39:05 -07001070 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001071}
1072
Jens Axboe28cea78a2020-09-14 10:51:17 -06001073static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1074 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001075{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001076 const struct io_op_def *def = &io_op_defs[req->opcode];
1077
1078 if (def->work_flags & IO_WQ_WORK_MM) {
1079 int ret = __io_sq_thread_acquire_mm(ctx);
1080 if (unlikely(ret))
1081 return ret;
1082 }
1083
1084 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES))
1085 __io_sq_thread_acquire_files(ctx);
1086
1087 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001088}
1089
Dennis Zhou91d8f512020-09-16 13:41:05 -07001090static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1091 struct cgroup_subsys_state **cur_css)
1092
1093{
1094#ifdef CONFIG_BLK_CGROUP
1095 /* puts the old one when swapping */
1096 if (*cur_css != ctx->sqo_blkcg_css) {
1097 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1098 *cur_css = ctx->sqo_blkcg_css;
1099 }
1100#endif
1101}
1102
1103static void io_sq_thread_unassociate_blkcg(void)
1104{
1105#ifdef CONFIG_BLK_CGROUP
1106 kthread_associate_blkcg(NULL);
1107#endif
1108}
1109
Jens Axboec40f6372020-06-25 15:39:59 -06001110static inline void req_set_fail_links(struct io_kiocb *req)
1111{
1112 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1113 req->flags |= REQ_F_FAIL_LINK;
1114}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001115
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001116/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001117 * None of these are dereferenced, they are simply used to check if any of
1118 * them have changed. If we're under current and check they are still the
1119 * same, we're fine to grab references to them for actual out-of-line use.
1120 */
1121static void io_init_identity(struct io_identity *id)
1122{
1123 id->files = current->files;
1124 id->mm = current->mm;
1125#ifdef CONFIG_BLK_CGROUP
1126 rcu_read_lock();
1127 id->blkcg_css = blkcg_css();
1128 rcu_read_unlock();
1129#endif
1130 id->creds = current_cred();
1131 id->nsproxy = current->nsproxy;
1132 id->fs = current->fs;
1133 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001134#ifdef CONFIG_AUDIT
1135 id->loginuid = current->loginuid;
1136 id->sessionid = current->sessionid;
1137#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001138 refcount_set(&id->count, 1);
1139}
1140
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001141static inline void __io_req_init_async(struct io_kiocb *req)
1142{
1143 memset(&req->work, 0, sizeof(req->work));
1144 req->flags |= REQ_F_WORK_INITIALIZED;
1145}
1146
Jens Axboe1e6fa522020-10-15 08:46:24 -06001147/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001148 * Note: must call io_req_init_async() for the first time you
1149 * touch any members of io_wq_work.
1150 */
1151static inline void io_req_init_async(struct io_kiocb *req)
1152{
Jens Axboe500a3732020-10-15 17:38:03 -06001153 struct io_uring_task *tctx = current->io_uring;
1154
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001155 if (req->flags & REQ_F_WORK_INITIALIZED)
1156 return;
1157
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001158 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001159
1160 /* Grab a ref if this isn't our static identity */
1161 req->work.identity = tctx->identity;
1162 if (tctx->identity != &tctx->__identity)
1163 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001164}
1165
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001166static inline bool io_async_submit(struct io_ring_ctx *ctx)
1167{
1168 return ctx->flags & IORING_SETUP_SQPOLL;
1169}
1170
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1172{
1173 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1174
Jens Axboe0f158b42020-05-14 17:18:39 -06001175 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001176}
1177
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001178static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1179{
1180 return !req->timeout.off;
1181}
1182
Jens Axboe2b188cc2019-01-07 10:46:33 -07001183static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1184{
1185 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001186 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001187
1188 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1189 if (!ctx)
1190 return NULL;
1191
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001192 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1193 if (!ctx->fallback_req)
1194 goto err;
1195
Jens Axboe78076bb2019-12-04 19:56:40 -07001196 /*
1197 * Use 5 bits less than the max cq entries, that should give us around
1198 * 32 entries per hash list if totally full and uniformly spread.
1199 */
1200 hash_bits = ilog2(p->cq_entries);
1201 hash_bits -= 5;
1202 if (hash_bits <= 0)
1203 hash_bits = 1;
1204 ctx->cancel_hash_bits = hash_bits;
1205 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1206 GFP_KERNEL);
1207 if (!ctx->cancel_hash)
1208 goto err;
1209 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1210
Roman Gushchin21482892019-05-07 10:01:48 -07001211 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001212 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1213 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001214
1215 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001216 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001217 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001218 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001219 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001220 init_completion(&ctx->ref_comp);
1221 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001222 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001223 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224 mutex_init(&ctx->uring_lock);
1225 init_waitqueue_head(&ctx->wait);
1226 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001227 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001228 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001229 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001230 init_waitqueue_head(&ctx->inflight_wait);
1231 spin_lock_init(&ctx->inflight_lock);
1232 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001233 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1234 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001235 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001236err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001237 if (ctx->fallback_req)
1238 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001239 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001240 kfree(ctx);
1241 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242}
1243
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001244static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001245{
Jens Axboe2bc99302020-07-09 09:43:27 -06001246 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1247 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001248
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001249 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001250 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001251 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001252
Bob Liu9d858b22019-11-13 18:06:25 +08001253 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001254}
1255
Jens Axboede0617e2019-04-06 21:51:27 -06001256static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257{
Hristo Venev75b28af2019-08-26 17:23:46 +00001258 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259
Pavel Begunkov07910152020-01-17 03:52:46 +03001260 /* order cqe stores with ring update */
1261 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262
Pavel Begunkov07910152020-01-17 03:52:46 +03001263 if (wq_has_sleeper(&ctx->cq_wait)) {
1264 wake_up_interruptible(&ctx->cq_wait);
1265 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266 }
1267}
1268
Jens Axboe5c3462c2020-10-15 09:02:33 -06001269static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001270{
Jens Axboe500a3732020-10-15 17:38:03 -06001271 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001272 return;
1273 if (refcount_dec_and_test(&req->work.identity->count))
1274 kfree(req->work.identity);
1275}
1276
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001277static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001278{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001279 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001280 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001281
1282 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001283
Jens Axboedfead8a2020-10-14 10:12:37 -06001284 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboe98447d62020-10-14 10:48:51 -06001285 mmdrop(req->work.identity->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001286 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001287 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001288#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001289 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Jens Axboe98447d62020-10-14 10:48:51 -06001290 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001291 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001292 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001293#endif
1294 if (req->work.flags & IO_WQ_WORK_CREDS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001295 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001296 req->work.flags &= ~IO_WQ_WORK_CREDS;
1297 }
1298 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001299 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001300
Jens Axboe98447d62020-10-14 10:48:51 -06001301 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001302 if (--fs->users)
1303 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001304 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001305 if (fs)
1306 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001307 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001308 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001309
Jens Axboe5c3462c2020-10-15 09:02:33 -06001310 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001311}
1312
1313/*
1314 * Create a private copy of io_identity, since some fields don't match
1315 * the current context.
1316 */
1317static bool io_identity_cow(struct io_kiocb *req)
1318{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001319 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001320 const struct cred *creds = NULL;
1321 struct io_identity *id;
1322
1323 if (req->work.flags & IO_WQ_WORK_CREDS)
1324 creds = req->work.identity->creds;
1325
1326 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1327 if (unlikely(!id)) {
1328 req->work.flags |= IO_WQ_WORK_CANCEL;
1329 return false;
1330 }
1331
1332 /*
1333 * We can safely just re-init the creds we copied Either the field
1334 * matches the current one, or we haven't grabbed it yet. The only
1335 * exception is ->creds, through registered personalities, so handle
1336 * that one separately.
1337 */
1338 io_init_identity(id);
1339 if (creds)
1340 req->work.identity->creds = creds;
1341
1342 /* add one for this request */
1343 refcount_inc(&id->count);
1344
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001345 /* drop tctx and req identity references, if needed */
1346 if (tctx->identity != &tctx->__identity &&
1347 refcount_dec_and_test(&tctx->identity->count))
1348 kfree(tctx->identity);
1349 if (req->work.identity != &tctx->__identity &&
1350 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001351 kfree(req->work.identity);
1352
1353 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001354 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001355 return true;
1356}
1357
1358static bool io_grab_identity(struct io_kiocb *req)
1359{
1360 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001361 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001362 struct io_ring_ctx *ctx = req->ctx;
1363
Jens Axboe69228332020-10-20 14:28:41 -06001364 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1365 if (id->fsize != rlimit(RLIMIT_FSIZE))
1366 return false;
1367 req->work.flags |= IO_WQ_WORK_FSIZE;
1368 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001369
1370 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1371 (def->work_flags & IO_WQ_WORK_FILES) &&
1372 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1373 if (id->files != current->files ||
1374 id->nsproxy != current->nsproxy)
1375 return false;
1376 atomic_inc(&id->files->count);
1377 get_nsproxy(id->nsproxy);
1378 req->flags |= REQ_F_INFLIGHT;
1379
1380 spin_lock_irq(&ctx->inflight_lock);
1381 list_add(&req->inflight_entry, &ctx->inflight_list);
1382 spin_unlock_irq(&ctx->inflight_lock);
1383 req->work.flags |= IO_WQ_WORK_FILES;
1384 }
1385#ifdef CONFIG_BLK_CGROUP
1386 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1387 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1388 rcu_read_lock();
1389 if (id->blkcg_css != blkcg_css()) {
1390 rcu_read_unlock();
1391 return false;
1392 }
1393 /*
1394 * This should be rare, either the cgroup is dying or the task
1395 * is moving cgroups. Just punt to root for the handful of ios.
1396 */
1397 if (css_tryget_online(id->blkcg_css))
1398 req->work.flags |= IO_WQ_WORK_BLKCG;
1399 rcu_read_unlock();
1400 }
1401#endif
1402 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1403 if (id->creds != current_cred())
1404 return false;
1405 get_cred(id->creds);
1406 req->work.flags |= IO_WQ_WORK_CREDS;
1407 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001408#ifdef CONFIG_AUDIT
1409 if (!uid_eq(current->loginuid, id->loginuid) ||
1410 current->sessionid != id->sessionid)
1411 return false;
1412#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001413 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1414 (def->work_flags & IO_WQ_WORK_FS)) {
1415 if (current->fs != id->fs)
1416 return false;
1417 spin_lock(&id->fs->lock);
1418 if (!id->fs->in_exec) {
1419 id->fs->users++;
1420 req->work.flags |= IO_WQ_WORK_FS;
1421 } else {
1422 req->work.flags |= IO_WQ_WORK_CANCEL;
1423 }
1424 spin_unlock(&current->fs->lock);
1425 }
1426
1427 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001428}
1429
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001430static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001431{
Jens Axboed3656342019-12-18 09:50:26 -07001432 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001433 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5c3462c2020-10-15 09:02:33 -06001434 struct io_identity *id;
Jens Axboe54a91f32019-09-10 09:15:04 -06001435
Pavel Begunkov16d59802020-07-12 16:16:47 +03001436 io_req_init_async(req);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001437 id = req->work.identity;
Pavel Begunkov16d59802020-07-12 16:16:47 +03001438
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001439 if (req->flags & REQ_F_FORCE_ASYNC)
1440 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1441
Jens Axboed3656342019-12-18 09:50:26 -07001442 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001443 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001444 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001445 } else {
1446 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001447 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001448 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001449
Jens Axboe1e6fa522020-10-15 08:46:24 -06001450 /* ->mm can never change on us */
Jens Axboedfead8a2020-10-14 10:12:37 -06001451 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1452 (def->work_flags & IO_WQ_WORK_MM)) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06001453 mmgrab(id->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001454 req->work.flags |= IO_WQ_WORK_MM;
Pavel Begunkov23329512020-10-10 18:34:06 +01001455 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001456
1457 /* if we fail grabbing identity, we must COW, regrab, and retry */
1458 if (io_grab_identity(req))
1459 return;
1460
1461 if (!io_identity_cow(req))
1462 return;
1463
1464 /* can't fail at this point */
1465 if (!io_grab_identity(req))
1466 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001467}
1468
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001469static void io_prep_async_link(struct io_kiocb *req)
1470{
1471 struct io_kiocb *cur;
1472
1473 io_prep_async_work(req);
1474 if (req->flags & REQ_F_LINK_HEAD)
1475 list_for_each_entry(cur, &req->link_list, link_list)
1476 io_prep_async_work(cur);
1477}
1478
Jens Axboe7271ef32020-08-10 09:55:22 -06001479static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001480{
Jackie Liua197f662019-11-08 08:09:12 -07001481 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001482 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001483
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001484 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1485 &req->work, req->flags);
1486 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001487 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001488}
1489
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001490static void io_queue_async_work(struct io_kiocb *req)
1491{
Jens Axboe7271ef32020-08-10 09:55:22 -06001492 struct io_kiocb *link;
1493
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001494 /* init ->work of the whole link before punting */
1495 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001496 link = __io_queue_async_work(req);
1497
1498 if (link)
1499 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001500}
1501
Jens Axboe5262f562019-09-17 12:26:57 -06001502static void io_kill_timeout(struct io_kiocb *req)
1503{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001504 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001505 int ret;
1506
Jens Axboee8c2bc12020-08-15 18:44:09 -07001507 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001508 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001509 atomic_set(&req->ctx->cq_timeouts,
1510 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001511 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001512 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001513 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001514 }
1515}
1516
Jens Axboef3606e32020-09-22 08:18:24 -06001517static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1518{
1519 struct io_ring_ctx *ctx = req->ctx;
1520
1521 if (!tsk || req->task == tsk)
1522 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001523 if (ctx->flags & IORING_SETUP_SQPOLL) {
1524 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1525 return true;
1526 }
Jens Axboef3606e32020-09-22 08:18:24 -06001527 return false;
1528}
1529
Jens Axboe76e1b642020-09-26 15:05:03 -06001530/*
1531 * Returns true if we found and killed one or more timeouts
1532 */
1533static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001534{
1535 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001536 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001537
1538 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001539 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001540 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001541 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001542 canceled++;
1543 }
Jens Axboef3606e32020-09-22 08:18:24 -06001544 }
Jens Axboe5262f562019-09-17 12:26:57 -06001545 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001546 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001547}
1548
Pavel Begunkov04518942020-05-26 20:34:05 +03001549static void __io_queue_deferred(struct io_ring_ctx *ctx)
1550{
1551 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001552 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1553 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001554 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001555
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001556 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001557 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001558 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001559 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001560 link = __io_queue_async_work(de->req);
1561 if (link) {
1562 __io_queue_linked_timeout(link);
1563 /* drop submission reference */
Pavel Begunkov216578e2020-10-13 09:44:00 +01001564 io_put_req_deferred(link, 1);
Jens Axboe7271ef32020-08-10 09:55:22 -06001565 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001566 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001567 } while (!list_empty(&ctx->defer_list));
1568}
1569
Pavel Begunkov360428f2020-05-30 14:54:17 +03001570static void io_flush_timeouts(struct io_ring_ctx *ctx)
1571{
1572 while (!list_empty(&ctx->timeout_list)) {
1573 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001574 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001575
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001576 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001577 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001578 if (req->timeout.target_seq != ctx->cached_cq_tail
1579 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001580 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001581
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001582 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001583 io_kill_timeout(req);
1584 }
1585}
1586
Jens Axboede0617e2019-04-06 21:51:27 -06001587static void io_commit_cqring(struct io_ring_ctx *ctx)
1588{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001589 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001590 __io_commit_cqring(ctx);
1591
Pavel Begunkov04518942020-05-26 20:34:05 +03001592 if (unlikely(!list_empty(&ctx->defer_list)))
1593 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001594}
1595
Jens Axboe90554202020-09-03 12:12:41 -06001596static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1597{
1598 struct io_rings *r = ctx->rings;
1599
1600 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1601}
1602
Jens Axboe2b188cc2019-01-07 10:46:33 -07001603static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1604{
Hristo Venev75b28af2019-08-26 17:23:46 +00001605 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606 unsigned tail;
1607
1608 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001609 /*
1610 * writes to the cq entry need to come after reading head; the
1611 * control dependency is enough as we're using WRITE_ONCE to
1612 * fill the cq entry
1613 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001614 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001615 return NULL;
1616
1617 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001618 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001619}
1620
Jens Axboef2842ab2020-01-08 11:04:00 -07001621static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1622{
Jens Axboef0b493e2020-02-01 21:30:11 -07001623 if (!ctx->cq_ev_fd)
1624 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001625 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1626 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001627 if (!ctx->eventfd_async)
1628 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001629 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001630}
1631
Jens Axboeb41e9852020-02-17 09:52:41 -07001632static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001633{
1634 if (waitqueue_active(&ctx->wait))
1635 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001636 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1637 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001638 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001639 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001640}
1641
Pavel Begunkov46930142020-07-30 18:43:49 +03001642static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1643{
1644 if (list_empty(&ctx->cq_overflow_list)) {
1645 clear_bit(0, &ctx->sq_check_overflow);
1646 clear_bit(0, &ctx->cq_check_overflow);
1647 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1648 }
1649}
1650
Pavel Begunkov99b32802020-11-04 13:39:31 +00001651static inline bool __io_match_files(struct io_kiocb *req,
1652 struct files_struct *files)
Jens Axboee6c8aa92020-09-28 13:10:13 -06001653{
Pavel Begunkov99b32802020-11-04 13:39:31 +00001654 return ((req->flags & REQ_F_WORK_INITIALIZED) &&
1655 (req->work.flags & IO_WQ_WORK_FILES)) &&
1656 req->work.identity->files == files;
1657}
1658
1659static bool io_match_files(struct io_kiocb *req,
1660 struct files_struct *files)
1661{
1662 struct io_kiocb *link;
1663
Jens Axboee6c8aa92020-09-28 13:10:13 -06001664 if (!files)
1665 return true;
Pavel Begunkov99b32802020-11-04 13:39:31 +00001666 if (__io_match_files(req, files))
1667 return true;
1668 if (req->flags & REQ_F_LINK_HEAD) {
1669 list_for_each_entry(link, &req->link_list, link_list) {
1670 if (__io_match_files(link, files))
1671 return true;
1672 }
1673 }
Jens Axboee6c8aa92020-09-28 13:10:13 -06001674 return false;
1675}
1676
Jens Axboec4a2ed72019-11-21 21:01:26 -07001677/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001678static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1679 struct task_struct *tsk,
1680 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001682 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001683 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001684 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001685 unsigned long flags;
1686 LIST_HEAD(list);
1687
1688 if (!force) {
1689 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001690 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001691 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1692 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001693 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001694 }
1695
1696 spin_lock_irqsave(&ctx->completion_lock, flags);
1697
1698 /* if force is set, the ring is going away. always drop after that */
1699 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001700 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001701
Jens Axboec4a2ed72019-11-21 21:01:26 -07001702 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001703 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1704 if (tsk && req->task != tsk)
1705 continue;
1706 if (!io_match_files(req, files))
1707 continue;
1708
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001709 cqe = io_get_cqring(ctx);
1710 if (!cqe && !force)
1711 break;
1712
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001713 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001714 if (cqe) {
1715 WRITE_ONCE(cqe->user_data, req->user_data);
1716 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001717 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001718 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001719 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001720 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001721 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001722 }
1723 }
1724
1725 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001726 io_cqring_mark_overflow(ctx);
1727
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001728 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1729 io_cqring_ev_posted(ctx);
1730
1731 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001732 req = list_first_entry(&list, struct io_kiocb, compl.list);
1733 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001734 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001735 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001736
1737 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001738}
1739
Jens Axboebcda7ba2020-02-23 16:42:51 -07001740static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001742 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743 struct io_uring_cqe *cqe;
1744
Jens Axboe78e19bb2019-11-06 15:21:34 -07001745 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001746
Jens Axboe2b188cc2019-01-07 10:46:33 -07001747 /*
1748 * If we can't get a cq entry, userspace overflowed the
1749 * submission (by quite a lot). Increment the overflow count in
1750 * the ring.
1751 */
1752 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001753 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001754 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001755 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001756 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001757 } else if (ctx->cq_overflow_flushed ||
1758 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001759 /*
1760 * If we're in ring overflow flush mode, or in task cancel mode,
1761 * then we cannot store the request for later flushing, we need
1762 * to drop it on the floor.
1763 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001764 ctx->cached_cq_overflow++;
1765 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001766 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001767 if (list_empty(&ctx->cq_overflow_list)) {
1768 set_bit(0, &ctx->sq_check_overflow);
1769 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001770 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001771 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001772 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001773 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001774 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001775 refcount_inc(&req->refs);
1776 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777 }
1778}
1779
Jens Axboebcda7ba2020-02-23 16:42:51 -07001780static void io_cqring_fill_event(struct io_kiocb *req, long res)
1781{
1782 __io_cqring_fill_event(req, res, 0);
1783}
1784
Jens Axboee1e16092020-06-22 09:17:17 -06001785static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001787 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788 unsigned long flags;
1789
1790 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001791 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792 io_commit_cqring(ctx);
1793 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1794
Jens Axboe8c838782019-03-12 15:48:16 -06001795 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001796}
1797
Jens Axboe229a7b62020-06-22 10:13:11 -06001798static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001799{
Jens Axboe229a7b62020-06-22 10:13:11 -06001800 struct io_ring_ctx *ctx = cs->ctx;
1801
1802 spin_lock_irq(&ctx->completion_lock);
1803 while (!list_empty(&cs->list)) {
1804 struct io_kiocb *req;
1805
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001806 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1807 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001808 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001809
1810 /*
1811 * io_free_req() doesn't care about completion_lock unless one
1812 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1813 * because of a potential deadlock with req->work.fs->lock
1814 */
1815 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1816 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001817 spin_unlock_irq(&ctx->completion_lock);
1818 io_put_req(req);
1819 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001820 } else {
1821 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001822 }
1823 }
1824 io_commit_cqring(ctx);
1825 spin_unlock_irq(&ctx->completion_lock);
1826
1827 io_cqring_ev_posted(ctx);
1828 cs->nr = 0;
1829}
1830
1831static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1832 struct io_comp_state *cs)
1833{
1834 if (!cs) {
1835 io_cqring_add_event(req, res, cflags);
1836 io_put_req(req);
1837 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001838 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001839 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001840 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001841 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001842 if (++cs->nr >= 32)
1843 io_submit_flush_completions(cs);
1844 }
Jens Axboee1e16092020-06-22 09:17:17 -06001845}
1846
1847static void io_req_complete(struct io_kiocb *req, long res)
1848{
Jens Axboe229a7b62020-06-22 10:13:11 -06001849 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001850}
1851
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001852static inline bool io_is_fallback_req(struct io_kiocb *req)
1853{
1854 return req == (struct io_kiocb *)
1855 ((unsigned long) req->ctx->fallback_req & ~1UL);
1856}
1857
1858static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1859{
1860 struct io_kiocb *req;
1861
1862 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001863 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001864 return req;
1865
1866 return NULL;
1867}
1868
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001869static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1870 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001871{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001872 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001873 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001874 size_t sz;
1875 int ret;
1876
1877 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001878 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1879
1880 /*
1881 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1882 * retry single alloc to be on the safe side.
1883 */
1884 if (unlikely(ret <= 0)) {
1885 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1886 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001887 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001888 ret = 1;
1889 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001890 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891 }
1892
Pavel Begunkov291b2822020-09-30 22:57:01 +03001893 state->free_reqs--;
1894 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001895fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001896 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897}
1898
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001899static inline void io_put_file(struct io_kiocb *req, struct file *file,
1900 bool fixed)
1901{
1902 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001903 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001904 else
1905 fput(file);
1906}
1907
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001908static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001910 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001911
Jens Axboee8c2bc12020-08-15 18:44:09 -07001912 if (req->async_data)
1913 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001914 if (req->file)
1915 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001916
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001917 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001918}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001919
Pavel Begunkov216578e2020-10-13 09:44:00 +01001920static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001921{
Jens Axboe0f212202020-09-13 13:09:39 -06001922 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001923 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001924
Pavel Begunkov216578e2020-10-13 09:44:00 +01001925 io_dismantle_req(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001926
Jens Axboed8a6df12020-10-15 16:24:45 -06001927 percpu_counter_dec(&tctx->inflight);
Jens Axboefdaf0832020-10-30 09:37:30 -06001928 if (atomic_read(&tctx->in_idle))
Jens Axboe0f212202020-09-13 13:09:39 -06001929 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001930 put_task_struct(req->task);
1931
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001932 if (likely(!io_is_fallback_req(req)))
1933 kmem_cache_free(req_cachep, req);
1934 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001935 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1936 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001937}
1938
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001939static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001940{
Jackie Liua197f662019-11-08 08:09:12 -07001941 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001942 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001943 bool cancelled = false;
1944 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001945
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001946 spin_lock_irqsave(&ctx->completion_lock, flags);
1947 link = list_first_entry_or_null(&req->link_list, struct io_kiocb,
1948 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01001949 /*
1950 * Can happen if a linked timeout fired and link had been like
1951 * req -> link t-out -> link t-out [-> ...]
1952 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001953 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1954 struct io_timeout_data *io = link->async_data;
1955 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001956
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001957 list_del_init(&link->link_list);
1958 ret = hrtimer_try_to_cancel(&io->timer);
1959 if (ret != -1) {
1960 io_cqring_fill_event(link, -ECANCELED);
1961 io_commit_cqring(ctx);
1962 cancelled = true;
1963 }
1964 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001965 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001966 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001967
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001968 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001969 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001970 io_put_req(link);
1971 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001972}
1973
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001974static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001975{
1976 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001977
Jens Axboe9e645e112019-05-10 16:07:28 -06001978 /*
1979 * The list should never be empty when we are called here. But could
1980 * potentially happen if the chain is messed up, check to be on the
1981 * safe side.
1982 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001983 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001984 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001985
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001986 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1987 list_del_init(&req->link_list);
1988 if (!list_empty(&nxt->link_list))
1989 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001990 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001991}
1992
1993/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001994 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001995 */
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001996static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001997{
Jens Axboe2665abf2019-11-05 12:40:47 -07001998 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001999 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002000
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002001 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06002002 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03002003 struct io_kiocb *link = list_first_entry(&req->link_list,
2004 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06002005
Pavel Begunkov44932332019-12-05 16:16:35 +03002006 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002007 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07002008
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002009 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002010
2011 /*
2012 * It's ok to free under spinlock as they're not linked anymore,
2013 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2014 * work.fs->lock.
2015 */
2016 if (link->flags & REQ_F_WORK_INITIALIZED)
2017 io_put_req_deferred(link, 2);
2018 else
2019 io_double_put_req(link);
Jens Axboe9e645e112019-05-10 16:07:28 -06002020 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002021
2022 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002023 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002024
Jens Axboe2665abf2019-11-05 12:40:47 -07002025 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002026}
2027
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002028static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002029{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03002030 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002031 if (req->flags & REQ_F_LINK_TIMEOUT)
2032 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002033
Jens Axboe9e645e112019-05-10 16:07:28 -06002034 /*
2035 * If LINK is set, we have dependent requests in this chain. If we
2036 * didn't fail this request, queue the first one up, moving any other
2037 * dependencies to the next request. In case of failure, fail the rest
2038 * of the chain.
2039 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002040 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
2041 return io_req_link_next(req);
2042 io_fail_links(req);
2043 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002044}
Jens Axboe2665abf2019-11-05 12:40:47 -07002045
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002046static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
2047{
2048 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
2049 return NULL;
2050 return __io_req_find_next(req);
2051}
2052
Jens Axboe87c43112020-09-30 21:00:14 -06002053static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06002054{
2055 struct task_struct *tsk = req->task;
2056 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002057 enum task_work_notify_mode notify;
2058 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002059
Jens Axboe6200b0a2020-09-13 14:38:30 -06002060 if (tsk->flags & PF_EXITING)
2061 return -ESRCH;
2062
Jens Axboec2c4c832020-07-01 15:37:11 -06002063 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002064 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2065 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2066 * processing task_work. There's no reliable way to tell if TWA_RESUME
2067 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002068 */
Jens Axboe91989c72020-10-16 09:02:26 -06002069 notify = TWA_NONE;
Jens Axboefd7d6de2020-08-23 11:00:37 -06002070 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06002071 notify = TWA_SIGNAL;
2072
Jens Axboe87c43112020-09-30 21:00:14 -06002073 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002074 if (!ret)
2075 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002076
Jens Axboec2c4c832020-07-01 15:37:11 -06002077 return ret;
2078}
2079
Jens Axboec40f6372020-06-25 15:39:59 -06002080static void __io_req_task_cancel(struct io_kiocb *req, int error)
2081{
2082 struct io_ring_ctx *ctx = req->ctx;
2083
2084 spin_lock_irq(&ctx->completion_lock);
2085 io_cqring_fill_event(req, error);
2086 io_commit_cqring(ctx);
2087 spin_unlock_irq(&ctx->completion_lock);
2088
2089 io_cqring_ev_posted(ctx);
2090 req_set_fail_links(req);
2091 io_double_put_req(req);
2092}
2093
2094static void io_req_task_cancel(struct callback_head *cb)
2095{
2096 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002097 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002098
2099 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002100 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002101}
2102
2103static void __io_req_task_submit(struct io_kiocb *req)
2104{
2105 struct io_ring_ctx *ctx = req->ctx;
2106
Jens Axboec40f6372020-06-25 15:39:59 -06002107 if (!__io_sq_thread_acquire_mm(ctx)) {
Jens Axboe28cea78a2020-09-14 10:51:17 -06002108 __io_sq_thread_acquire_files(ctx);
Jens Axboec40f6372020-06-25 15:39:59 -06002109 mutex_lock(&ctx->uring_lock);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002110 __io_queue_sqe(req, NULL);
Jens Axboec40f6372020-06-25 15:39:59 -06002111 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002112 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06002113 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07002114 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002115}
2116
Jens Axboec40f6372020-06-25 15:39:59 -06002117static void io_req_task_submit(struct callback_head *cb)
2118{
2119 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002120 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002121
2122 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002123 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002124}
2125
2126static void io_req_task_queue(struct io_kiocb *req)
2127{
Jens Axboec40f6372020-06-25 15:39:59 -06002128 int ret;
2129
2130 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002131 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002132
Jens Axboe87c43112020-09-30 21:00:14 -06002133 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06002134 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002135 struct task_struct *tsk;
2136
Jens Axboec40f6372020-06-25 15:39:59 -06002137 init_task_work(&req->task_work, io_req_task_cancel);
2138 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002139 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002140 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002141 }
Jens Axboec40f6372020-06-25 15:39:59 -06002142}
2143
Pavel Begunkovc3524382020-06-28 12:52:32 +03002144static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002145{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002146 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002147
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002148 if (nxt)
2149 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002150}
2151
Jens Axboe9e645e112019-05-10 16:07:28 -06002152static void io_free_req(struct io_kiocb *req)
2153{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002154 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002155 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002156}
2157
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002158struct req_batch {
2159 void *reqs[IO_IOPOLL_BATCH];
2160 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002161
2162 struct task_struct *task;
2163 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002164};
2165
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002166static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002167{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002168 rb->to_free = 0;
2169 rb->task_refs = 0;
2170 rb->task = NULL;
2171}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002172
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002173static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2174 struct req_batch *rb)
2175{
2176 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2177 percpu_ref_put_many(&ctx->refs, rb->to_free);
2178 rb->to_free = 0;
2179}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002180
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002181static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2182 struct req_batch *rb)
2183{
2184 if (rb->to_free)
2185 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002186 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002187 struct io_uring_task *tctx = rb->task->io_uring;
2188
2189 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002190 put_task_struct_many(rb->task, rb->task_refs);
2191 rb->task = NULL;
2192 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002193}
2194
2195static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2196{
2197 if (unlikely(io_is_fallback_req(req))) {
2198 io_free_req(req);
2199 return;
2200 }
2201 if (req->flags & REQ_F_LINK_HEAD)
2202 io_queue_next(req);
2203
Jens Axboee3bc8e92020-09-24 08:45:57 -06002204 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002205 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002206 struct io_uring_task *tctx = rb->task->io_uring;
2207
2208 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002209 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002210 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002211 rb->task = req->task;
2212 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002213 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002214 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002215
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002216 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002217 rb->reqs[rb->to_free++] = req;
2218 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2219 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002220}
2221
Jens Axboeba816ad2019-09-28 11:36:45 -06002222/*
2223 * Drop reference to request, return next in chain (if there is one) if this
2224 * was the last reference to this request.
2225 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002226static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002227{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002228 struct io_kiocb *nxt = NULL;
2229
Jens Axboe2a44f462020-02-25 13:25:41 -07002230 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002231 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002232 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002233 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002234 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235}
2236
Jens Axboe2b188cc2019-01-07 10:46:33 -07002237static void io_put_req(struct io_kiocb *req)
2238{
Jens Axboedef596e2019-01-09 08:59:42 -07002239 if (refcount_dec_and_test(&req->refs))
2240 io_free_req(req);
2241}
2242
Pavel Begunkov216578e2020-10-13 09:44:00 +01002243static void io_put_req_deferred_cb(struct callback_head *cb)
2244{
2245 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2246
2247 io_free_req(req);
2248}
2249
2250static void io_free_req_deferred(struct io_kiocb *req)
2251{
2252 int ret;
2253
2254 init_task_work(&req->task_work, io_put_req_deferred_cb);
2255 ret = io_req_task_work_add(req, true);
2256 if (unlikely(ret)) {
2257 struct task_struct *tsk;
2258
2259 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002260 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002261 wake_up_process(tsk);
2262 }
2263}
2264
2265static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2266{
2267 if (refcount_sub_and_test(refs, &req->refs))
2268 io_free_req_deferred(req);
2269}
2270
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002271static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002272{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002273 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002274
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002275 /*
2276 * A ref is owned by io-wq in which context we're. So, if that's the
2277 * last one, it's safe to steal next work. False negatives are Ok,
2278 * it just will be re-punted async in io_put_work()
2279 */
2280 if (refcount_read(&req->refs) != 1)
2281 return NULL;
2282
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002283 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002284 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002285}
2286
Jens Axboe978db572019-11-14 22:39:04 -07002287static void io_double_put_req(struct io_kiocb *req)
2288{
2289 /* drop both submit and complete references */
2290 if (refcount_sub_and_test(2, &req->refs))
2291 io_free_req(req);
2292}
2293
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002294static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002295{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002296 struct io_rings *rings = ctx->rings;
2297
Jens Axboead3eb2c2019-12-18 17:12:20 -07002298 if (test_bit(0, &ctx->cq_check_overflow)) {
2299 /*
2300 * noflush == true is from the waitqueue handler, just ensure
2301 * we wake up the task, and the next invocation will flush the
2302 * entries. We cannot safely to it from here.
2303 */
2304 if (noflush && !list_empty(&ctx->cq_overflow_list))
2305 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002306
Jens Axboee6c8aa92020-09-28 13:10:13 -06002307 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002308 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002309
Jens Axboea3a0e432019-08-20 11:03:11 -06002310 /* See comment at the top of this file */
2311 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002312 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002313}
2314
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002315static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2316{
2317 struct io_rings *rings = ctx->rings;
2318
2319 /* make sure SQ entry isn't read before tail */
2320 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2321}
2322
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002323static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002324{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002325 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002326
Jens Axboebcda7ba2020-02-23 16:42:51 -07002327 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2328 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002329 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002330 kfree(kbuf);
2331 return cflags;
2332}
2333
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002334static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2335{
2336 struct io_buffer *kbuf;
2337
2338 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2339 return io_put_kbuf(req, kbuf);
2340}
2341
Jens Axboe4c6e2772020-07-01 11:29:10 -06002342static inline bool io_run_task_work(void)
2343{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002344 /*
2345 * Not safe to run on exiting task, and the task_work handling will
2346 * not add work to such a task.
2347 */
2348 if (unlikely(current->flags & PF_EXITING))
2349 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002350 if (current->task_works) {
2351 __set_current_state(TASK_RUNNING);
2352 task_work_run();
2353 return true;
2354 }
2355
2356 return false;
2357}
2358
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002359static void io_iopoll_queue(struct list_head *again)
2360{
2361 struct io_kiocb *req;
2362
2363 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002364 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2365 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002366 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002367 } while (!list_empty(again));
2368}
2369
Jens Axboedef596e2019-01-09 08:59:42 -07002370/*
2371 * Find and free completed poll iocbs
2372 */
2373static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2374 struct list_head *done)
2375{
Jens Axboe8237e042019-12-28 10:48:22 -07002376 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002377 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002378 LIST_HEAD(again);
2379
2380 /* order with ->result store in io_complete_rw_iopoll() */
2381 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002382
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002383 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002384 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002385 int cflags = 0;
2386
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002387 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002388 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002389 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002390 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002391 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002392 continue;
2393 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002394 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002395
Jens Axboebcda7ba2020-02-23 16:42:51 -07002396 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002397 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002398
2399 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002400 (*nr_events)++;
2401
Pavel Begunkovc3524382020-06-28 12:52:32 +03002402 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002403 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002404 }
Jens Axboedef596e2019-01-09 08:59:42 -07002405
Jens Axboe09bb8392019-03-13 12:39:28 -06002406 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002407 if (ctx->flags & IORING_SETUP_SQPOLL)
2408 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002409 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002410
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002411 if (!list_empty(&again))
2412 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002413}
2414
Jens Axboedef596e2019-01-09 08:59:42 -07002415static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2416 long min)
2417{
2418 struct io_kiocb *req, *tmp;
2419 LIST_HEAD(done);
2420 bool spin;
2421 int ret;
2422
2423 /*
2424 * Only spin for completions if we don't have multiple devices hanging
2425 * off our complete list, and we're under the requested amount.
2426 */
2427 spin = !ctx->poll_multi_file && *nr_events < min;
2428
2429 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002430 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002431 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002432
2433 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002434 * Move completed and retryable entries to our local lists.
2435 * If we find a request that requires polling, break out
2436 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002437 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002438 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002439 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002440 continue;
2441 }
2442 if (!list_empty(&done))
2443 break;
2444
2445 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2446 if (ret < 0)
2447 break;
2448
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002449 /* iopoll may have completed current req */
2450 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002451 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002452
Jens Axboedef596e2019-01-09 08:59:42 -07002453 if (ret && spin)
2454 spin = false;
2455 ret = 0;
2456 }
2457
2458 if (!list_empty(&done))
2459 io_iopoll_complete(ctx, nr_events, &done);
2460
2461 return ret;
2462}
2463
2464/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002465 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002466 * non-spinning poll check - we'll still enter the driver poll loop, but only
2467 * as a non-spinning completion check.
2468 */
2469static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2470 long min)
2471{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002472 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002473 int ret;
2474
2475 ret = io_do_iopoll(ctx, nr_events, min);
2476 if (ret < 0)
2477 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002478 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002479 return 0;
2480 }
2481
2482 return 1;
2483}
2484
2485/*
2486 * We can't just wait for polled events to come to us, we have to actively
2487 * find and complete them.
2488 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002489static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002490{
2491 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2492 return;
2493
2494 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002495 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002496 unsigned int nr_events = 0;
2497
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002498 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002499
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002500 /* let it sleep and repeat later if can't complete a request */
2501 if (nr_events == 0)
2502 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002503 /*
2504 * Ensure we allow local-to-the-cpu processing to take place,
2505 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002506 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002507 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002508 if (need_resched()) {
2509 mutex_unlock(&ctx->uring_lock);
2510 cond_resched();
2511 mutex_lock(&ctx->uring_lock);
2512 }
Jens Axboedef596e2019-01-09 08:59:42 -07002513 }
2514 mutex_unlock(&ctx->uring_lock);
2515}
2516
Pavel Begunkov7668b922020-07-07 16:36:21 +03002517static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002518{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002519 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002520 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002521
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002522 /*
2523 * We disallow the app entering submit/complete with polling, but we
2524 * still need to lock the ring to prevent racing with polled issue
2525 * that got punted to a workqueue.
2526 */
2527 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002528 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002529 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002530 * Don't enter poll loop if we already have events pending.
2531 * If we do, we can potentially be spinning for commands that
2532 * already triggered a CQE (eg in error).
2533 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002534 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002535 break;
2536
2537 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002538 * If a submit got punted to a workqueue, we can have the
2539 * application entering polling for a command before it gets
2540 * issued. That app will hold the uring_lock for the duration
2541 * of the poll right here, so we need to take a breather every
2542 * now and then to ensure that the issue has a chance to add
2543 * the poll to the issued list. Otherwise we can spin here
2544 * forever, while the workqueue is stuck trying to acquire the
2545 * very same mutex.
2546 */
2547 if (!(++iters & 7)) {
2548 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002549 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002550 mutex_lock(&ctx->uring_lock);
2551 }
2552
Pavel Begunkov7668b922020-07-07 16:36:21 +03002553 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002554 if (ret <= 0)
2555 break;
2556 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002557 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002558
Jens Axboe500f9fb2019-08-19 12:15:59 -06002559 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002560 return ret;
2561}
2562
Jens Axboe491381ce2019-10-17 09:20:46 -06002563static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002564{
Jens Axboe491381ce2019-10-17 09:20:46 -06002565 /*
2566 * Tell lockdep we inherited freeze protection from submission
2567 * thread.
2568 */
2569 if (req->flags & REQ_F_ISREG) {
2570 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002571
Jens Axboe491381ce2019-10-17 09:20:46 -06002572 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002573 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002574 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575}
2576
Jens Axboea1d7c392020-06-22 11:09:46 -06002577static void io_complete_rw_common(struct kiocb *kiocb, long res,
2578 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002579{
Jens Axboe9adbd452019-12-20 08:45:55 -07002580 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002581 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002582
Jens Axboe491381ce2019-10-17 09:20:46 -06002583 if (kiocb->ki_flags & IOCB_WRITE)
2584 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002585
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002586 if (res != req->result)
2587 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002588 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002589 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002590 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002591}
2592
Jens Axboeb63534c2020-06-04 11:28:00 -06002593#ifdef CONFIG_BLOCK
2594static bool io_resubmit_prep(struct io_kiocb *req, int error)
2595{
2596 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2597 ssize_t ret = -ECANCELED;
2598 struct iov_iter iter;
2599 int rw;
2600
2601 if (error) {
2602 ret = error;
2603 goto end_req;
2604 }
2605
2606 switch (req->opcode) {
2607 case IORING_OP_READV:
2608 case IORING_OP_READ_FIXED:
2609 case IORING_OP_READ:
2610 rw = READ;
2611 break;
2612 case IORING_OP_WRITEV:
2613 case IORING_OP_WRITE_FIXED:
2614 case IORING_OP_WRITE:
2615 rw = WRITE;
2616 break;
2617 default:
2618 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2619 req->opcode);
2620 goto end_req;
2621 }
2622
Jens Axboee8c2bc12020-08-15 18:44:09 -07002623 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002624 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2625 if (ret < 0)
2626 goto end_req;
2627 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2628 if (!ret)
2629 return true;
2630 kfree(iovec);
2631 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002632 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002633 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002634end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002635 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002636 return false;
2637}
Jens Axboeb63534c2020-06-04 11:28:00 -06002638#endif
2639
2640static bool io_rw_reissue(struct io_kiocb *req, long res)
2641{
2642#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002643 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002644 int ret;
2645
Jens Axboe355afae2020-09-02 09:30:31 -06002646 if (!S_ISBLK(mode) && !S_ISREG(mode))
2647 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002648 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2649 return false;
2650
Jens Axboe28cea78a2020-09-14 10:51:17 -06002651 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002652
Jens Axboefdee9462020-08-27 16:46:24 -06002653 if (io_resubmit_prep(req, ret)) {
2654 refcount_inc(&req->refs);
2655 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002656 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002657 }
2658
Jens Axboeb63534c2020-06-04 11:28:00 -06002659#endif
2660 return false;
2661}
2662
Jens Axboea1d7c392020-06-22 11:09:46 -06002663static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2664 struct io_comp_state *cs)
2665{
2666 if (!io_rw_reissue(req, res))
2667 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002668}
2669
2670static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2671{
Jens Axboe9adbd452019-12-20 08:45:55 -07002672 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002673
Jens Axboea1d7c392020-06-22 11:09:46 -06002674 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002675}
2676
Jens Axboedef596e2019-01-09 08:59:42 -07002677static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2678{
Jens Axboe9adbd452019-12-20 08:45:55 -07002679 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002680
Jens Axboe491381ce2019-10-17 09:20:46 -06002681 if (kiocb->ki_flags & IOCB_WRITE)
2682 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002683
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002684 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002685 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002686
2687 WRITE_ONCE(req->result, res);
2688 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002689 smp_wmb();
2690 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002691}
2692
2693/*
2694 * After the iocb has been issued, it's safe to be found on the poll list.
2695 * Adding the kiocb to the list AFTER submission ensures that we don't
2696 * find it from a io_iopoll_getevents() thread before the issuer is done
2697 * accessing the kiocb cookie.
2698 */
2699static void io_iopoll_req_issued(struct io_kiocb *req)
2700{
2701 struct io_ring_ctx *ctx = req->ctx;
2702
2703 /*
2704 * Track whether we have multiple files in our lists. This will impact
2705 * how we do polling eventually, not spinning if we're on potentially
2706 * different devices.
2707 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002708 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002709 ctx->poll_multi_file = false;
2710 } else if (!ctx->poll_multi_file) {
2711 struct io_kiocb *list_req;
2712
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002713 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002714 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002715 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002716 ctx->poll_multi_file = true;
2717 }
2718
2719 /*
2720 * For fast devices, IO may have already completed. If it has, add
2721 * it to the front so we find it first.
2722 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002723 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002724 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002725 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002726 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002727
2728 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002729 wq_has_sleeper(&ctx->sq_data->wait))
2730 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002731}
2732
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002733static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002734{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002735 if (state->has_refs)
2736 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002737 state->file = NULL;
2738}
2739
2740static inline void io_state_file_put(struct io_submit_state *state)
2741{
2742 if (state->file)
2743 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002744}
2745
2746/*
2747 * Get as many references to a file as we have IOs left in this submission,
2748 * assuming most submissions are for one file, or at least that each file
2749 * has more than one submission.
2750 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002751static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002752{
2753 if (!state)
2754 return fget(fd);
2755
2756 if (state->file) {
2757 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002758 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002759 return state->file;
2760 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002761 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002762 }
2763 state->file = fget_many(fd, state->ios_left);
2764 if (!state->file)
2765 return NULL;
2766
2767 state->fd = fd;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01002768 state->has_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002769 return state->file;
2770}
2771
Jens Axboe4503b762020-06-01 10:00:27 -06002772static bool io_bdev_nowait(struct block_device *bdev)
2773{
2774#ifdef CONFIG_BLOCK
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002775 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002776#else
2777 return true;
2778#endif
2779}
2780
Jens Axboe2b188cc2019-01-07 10:46:33 -07002781/*
2782 * If we tracked the file through the SCM inflight mechanism, we could support
2783 * any file. For now, just ensure that anything potentially problematic is done
2784 * inline.
2785 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002786static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002787{
2788 umode_t mode = file_inode(file)->i_mode;
2789
Jens Axboe4503b762020-06-01 10:00:27 -06002790 if (S_ISBLK(mode)) {
2791 if (io_bdev_nowait(file->f_inode->i_bdev))
2792 return true;
2793 return false;
2794 }
2795 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002796 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002797 if (S_ISREG(mode)) {
2798 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2799 file->f_op != &io_uring_fops)
2800 return true;
2801 return false;
2802 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002803
Jens Axboec5b85622020-06-09 19:23:05 -06002804 /* any ->read/write should understand O_NONBLOCK */
2805 if (file->f_flags & O_NONBLOCK)
2806 return true;
2807
Jens Axboeaf197f52020-04-28 13:15:06 -06002808 if (!(file->f_mode & FMODE_NOWAIT))
2809 return false;
2810
2811 if (rw == READ)
2812 return file->f_op->read_iter != NULL;
2813
2814 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002815}
2816
Pavel Begunkova88fc402020-09-30 22:57:53 +03002817static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002818{
Jens Axboedef596e2019-01-09 08:59:42 -07002819 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002820 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002821 unsigned ioprio;
2822 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002823
Jens Axboe491381ce2019-10-17 09:20:46 -06002824 if (S_ISREG(file_inode(req->file)->i_mode))
2825 req->flags |= REQ_F_ISREG;
2826
Jens Axboe2b188cc2019-01-07 10:46:33 -07002827 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002828 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2829 req->flags |= REQ_F_CUR_POS;
2830 kiocb->ki_pos = req->file->f_pos;
2831 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002833 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2834 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2835 if (unlikely(ret))
2836 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002837
2838 ioprio = READ_ONCE(sqe->ioprio);
2839 if (ioprio) {
2840 ret = ioprio_check_cap(ioprio);
2841 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002842 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002843
2844 kiocb->ki_ioprio = ioprio;
2845 } else
2846 kiocb->ki_ioprio = get_current_ioprio();
2847
Stefan Bühler8449eed2019-04-27 20:34:19 +02002848 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002849 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002850 req->flags |= REQ_F_NOWAIT;
2851
Jens Axboedef596e2019-01-09 08:59:42 -07002852 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002853 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2854 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002855 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002856
Jens Axboedef596e2019-01-09 08:59:42 -07002857 kiocb->ki_flags |= IOCB_HIPRI;
2858 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002859 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002860 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002861 if (kiocb->ki_flags & IOCB_HIPRI)
2862 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002863 kiocb->ki_complete = io_complete_rw;
2864 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002865
Jens Axboe3529d8c2019-12-19 18:24:38 -07002866 req->rw.addr = READ_ONCE(sqe->addr);
2867 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002868 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002869 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002870}
2871
2872static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2873{
2874 switch (ret) {
2875 case -EIOCBQUEUED:
2876 break;
2877 case -ERESTARTSYS:
2878 case -ERESTARTNOINTR:
2879 case -ERESTARTNOHAND:
2880 case -ERESTART_RESTARTBLOCK:
2881 /*
2882 * We can't just restart the syscall, since previously
2883 * submitted sqes may already be in progress. Just fail this
2884 * IO with EINTR.
2885 */
2886 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002887 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002888 default:
2889 kiocb->ki_complete(kiocb, ret, 0);
2890 }
2891}
2892
Jens Axboea1d7c392020-06-22 11:09:46 -06002893static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2894 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002895{
Jens Axboeba042912019-12-25 16:33:42 -07002896 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002897 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002898
Jens Axboe227c0c92020-08-13 11:51:40 -06002899 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002900 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002901 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002902 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002903 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002904 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002905 }
2906
Jens Axboeba042912019-12-25 16:33:42 -07002907 if (req->flags & REQ_F_CUR_POS)
2908 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002909 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002910 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002911 else
2912 io_rw_done(kiocb, ret);
2913}
2914
Jens Axboe9adbd452019-12-20 08:45:55 -07002915static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002916 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002917{
Jens Axboe9adbd452019-12-20 08:45:55 -07002918 struct io_ring_ctx *ctx = req->ctx;
2919 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002920 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002921 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002922 size_t offset;
2923 u64 buf_addr;
2924
Jens Axboeedafcce2019-01-09 09:16:05 -07002925 if (unlikely(buf_index >= ctx->nr_user_bufs))
2926 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002927 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2928 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002929 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002930
2931 /* overflow */
2932 if (buf_addr + len < buf_addr)
2933 return -EFAULT;
2934 /* not inside the mapped region */
2935 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2936 return -EFAULT;
2937
2938 /*
2939 * May not be a start of buffer, set size appropriately
2940 * and advance us to the beginning.
2941 */
2942 offset = buf_addr - imu->ubuf;
2943 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002944
2945 if (offset) {
2946 /*
2947 * Don't use iov_iter_advance() here, as it's really slow for
2948 * using the latter parts of a big fixed buffer - it iterates
2949 * over each segment manually. We can cheat a bit here, because
2950 * we know that:
2951 *
2952 * 1) it's a BVEC iter, we set it up
2953 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2954 * first and last bvec
2955 *
2956 * So just find our index, and adjust the iterator afterwards.
2957 * If the offset is within the first bvec (or the whole first
2958 * bvec, just use iov_iter_advance(). This makes it easier
2959 * since we can just skip the first segment, which may not
2960 * be PAGE_SIZE aligned.
2961 */
2962 const struct bio_vec *bvec = imu->bvec;
2963
2964 if (offset <= bvec->bv_len) {
2965 iov_iter_advance(iter, offset);
2966 } else {
2967 unsigned long seg_skip;
2968
2969 /* skip first vec */
2970 offset -= bvec->bv_len;
2971 seg_skip = 1 + (offset >> PAGE_SHIFT);
2972
2973 iter->bvec = bvec + seg_skip;
2974 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002975 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002976 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002977 }
2978 }
2979
Jens Axboe5e559562019-11-13 16:12:46 -07002980 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002981}
2982
Jens Axboebcda7ba2020-02-23 16:42:51 -07002983static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2984{
2985 if (needs_lock)
2986 mutex_unlock(&ctx->uring_lock);
2987}
2988
2989static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2990{
2991 /*
2992 * "Normal" inline submissions always hold the uring_lock, since we
2993 * grab it from the system call. Same is true for the SQPOLL offload.
2994 * The only exception is when we've detached the request and issue it
2995 * from an async worker thread, grab the lock for that case.
2996 */
2997 if (needs_lock)
2998 mutex_lock(&ctx->uring_lock);
2999}
3000
3001static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3002 int bgid, struct io_buffer *kbuf,
3003 bool needs_lock)
3004{
3005 struct io_buffer *head;
3006
3007 if (req->flags & REQ_F_BUFFER_SELECTED)
3008 return kbuf;
3009
3010 io_ring_submit_lock(req->ctx, needs_lock);
3011
3012 lockdep_assert_held(&req->ctx->uring_lock);
3013
3014 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3015 if (head) {
3016 if (!list_empty(&head->list)) {
3017 kbuf = list_last_entry(&head->list, struct io_buffer,
3018 list);
3019 list_del(&kbuf->list);
3020 } else {
3021 kbuf = head;
3022 idr_remove(&req->ctx->io_buffer_idr, bgid);
3023 }
3024 if (*len > kbuf->len)
3025 *len = kbuf->len;
3026 } else {
3027 kbuf = ERR_PTR(-ENOBUFS);
3028 }
3029
3030 io_ring_submit_unlock(req->ctx, needs_lock);
3031
3032 return kbuf;
3033}
3034
Jens Axboe4d954c22020-02-27 07:31:19 -07003035static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3036 bool needs_lock)
3037{
3038 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003039 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003040
3041 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003042 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003043 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3044 if (IS_ERR(kbuf))
3045 return kbuf;
3046 req->rw.addr = (u64) (unsigned long) kbuf;
3047 req->flags |= REQ_F_BUFFER_SELECTED;
3048 return u64_to_user_ptr(kbuf->addr);
3049}
3050
3051#ifdef CONFIG_COMPAT
3052static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3053 bool needs_lock)
3054{
3055 struct compat_iovec __user *uiov;
3056 compat_ssize_t clen;
3057 void __user *buf;
3058 ssize_t len;
3059
3060 uiov = u64_to_user_ptr(req->rw.addr);
3061 if (!access_ok(uiov, sizeof(*uiov)))
3062 return -EFAULT;
3063 if (__get_user(clen, &uiov->iov_len))
3064 return -EFAULT;
3065 if (clen < 0)
3066 return -EINVAL;
3067
3068 len = clen;
3069 buf = io_rw_buffer_select(req, &len, needs_lock);
3070 if (IS_ERR(buf))
3071 return PTR_ERR(buf);
3072 iov[0].iov_base = buf;
3073 iov[0].iov_len = (compat_size_t) len;
3074 return 0;
3075}
3076#endif
3077
3078static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3079 bool needs_lock)
3080{
3081 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3082 void __user *buf;
3083 ssize_t len;
3084
3085 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3086 return -EFAULT;
3087
3088 len = iov[0].iov_len;
3089 if (len < 0)
3090 return -EINVAL;
3091 buf = io_rw_buffer_select(req, &len, needs_lock);
3092 if (IS_ERR(buf))
3093 return PTR_ERR(buf);
3094 iov[0].iov_base = buf;
3095 iov[0].iov_len = len;
3096 return 0;
3097}
3098
3099static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3100 bool needs_lock)
3101{
Jens Axboedddb3e22020-06-04 11:27:01 -06003102 if (req->flags & REQ_F_BUFFER_SELECTED) {
3103 struct io_buffer *kbuf;
3104
3105 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3106 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3107 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003108 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003109 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003110 if (!req->rw.len)
3111 return 0;
3112 else if (req->rw.len > 1)
3113 return -EINVAL;
3114
3115#ifdef CONFIG_COMPAT
3116 if (req->ctx->compat)
3117 return io_compat_import(req, iov, needs_lock);
3118#endif
3119
3120 return __io_iov_buffer_select(req, iov, needs_lock);
3121}
3122
Jens Axboe8452fd02020-08-18 13:58:33 -07003123static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
3124 struct iovec **iovec, struct iov_iter *iter,
3125 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003126{
Jens Axboe9adbd452019-12-20 08:45:55 -07003127 void __user *buf = u64_to_user_ptr(req->rw.addr);
3128 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003129 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003130 u8 opcode;
3131
Jens Axboed625c6e2019-12-17 19:53:05 -07003132 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003133 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003134 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003135 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003136 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003137
Jens Axboebcda7ba2020-02-23 16:42:51 -07003138 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003139 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003140 return -EINVAL;
3141
Jens Axboe3a6820f2019-12-22 15:19:35 -07003142 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003143 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003144 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003145 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003146 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003147 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003148 }
3149
Jens Axboe3a6820f2019-12-22 15:19:35 -07003150 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3151 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07003152 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003153 }
3154
Jens Axboe4d954c22020-02-27 07:31:19 -07003155 if (req->flags & REQ_F_BUFFER_SELECT) {
3156 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003157 if (!ret) {
3158 ret = (*iovec)->iov_len;
3159 iov_iter_init(iter, rw, *iovec, 1, ret);
3160 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003161 *iovec = NULL;
3162 return ret;
3163 }
3164
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003165 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3166 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003167}
3168
Jens Axboe8452fd02020-08-18 13:58:33 -07003169static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
3170 struct iovec **iovec, struct iov_iter *iter,
3171 bool needs_lock)
3172{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003173 struct io_async_rw *iorw = req->async_data;
3174
3175 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07003176 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3177 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003178 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07003179}
3180
Jens Axboe0fef9482020-08-26 10:36:20 -06003181static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3182{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003183 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003184}
3185
Jens Axboe32960612019-09-23 11:05:34 -06003186/*
3187 * For files that don't have ->read_iter() and ->write_iter(), handle them
3188 * by looping over ->read() or ->write() manually.
3189 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003190static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003191{
Jens Axboe4017eb92020-10-22 14:14:12 -06003192 struct kiocb *kiocb = &req->rw.kiocb;
3193 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003194 ssize_t ret = 0;
3195
3196 /*
3197 * Don't support polled IO through this interface, and we can't
3198 * support non-blocking either. For the latter, this just causes
3199 * the kiocb to be handled from an async context.
3200 */
3201 if (kiocb->ki_flags & IOCB_HIPRI)
3202 return -EOPNOTSUPP;
3203 if (kiocb->ki_flags & IOCB_NOWAIT)
3204 return -EAGAIN;
3205
3206 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003207 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003208 ssize_t nr;
3209
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003210 if (!iov_iter_is_bvec(iter)) {
3211 iovec = iov_iter_iovec(iter);
3212 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003213 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3214 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003215 }
3216
Jens Axboe32960612019-09-23 11:05:34 -06003217 if (rw == READ) {
3218 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003219 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003220 } else {
3221 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003222 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003223 }
3224
3225 if (nr < 0) {
3226 if (!ret)
3227 ret = nr;
3228 break;
3229 }
3230 ret += nr;
3231 if (nr != iovec.iov_len)
3232 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003233 req->rw.len -= nr;
3234 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003235 iov_iter_advance(iter, nr);
3236 }
3237
3238 return ret;
3239}
3240
Jens Axboeff6165b2020-08-13 09:47:43 -06003241static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3242 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003243{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003244 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003245
Jens Axboeff6165b2020-08-13 09:47:43 -06003246 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003247 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003248 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003249 /* can only be fixed buffers, no need to do anything */
3250 if (iter->type == ITER_BVEC)
3251 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003252 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003253 unsigned iov_off = 0;
3254
3255 rw->iter.iov = rw->fast_iov;
3256 if (iter->iov != fast_iov) {
3257 iov_off = iter->iov - fast_iov;
3258 rw->iter.iov += iov_off;
3259 }
3260 if (rw->fast_iov != fast_iov)
3261 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003262 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003263 } else {
3264 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003265 }
3266}
3267
Jens Axboee8c2bc12020-08-15 18:44:09 -07003268static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003269{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003270 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3271 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3272 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003273}
3274
Jens Axboee8c2bc12020-08-15 18:44:09 -07003275static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003276{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003277 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003278 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003279
Jens Axboee8c2bc12020-08-15 18:44:09 -07003280 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003281}
3282
Jens Axboeff6165b2020-08-13 09:47:43 -06003283static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3284 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003285 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003286{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003287 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003288 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003289 if (!req->async_data) {
3290 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003291 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003292
Jens Axboeff6165b2020-08-13 09:47:43 -06003293 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003294 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003295 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003296}
3297
Pavel Begunkov73debe62020-09-30 22:57:54 +03003298static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003299{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003300 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003301 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003302 ssize_t ret;
3303
Pavel Begunkov73debe62020-09-30 22:57:54 +03003304 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003305 if (unlikely(ret < 0))
3306 return ret;
3307
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003308 iorw->bytes_done = 0;
3309 iorw->free_iovec = iov;
3310 if (iov)
3311 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003312 return 0;
3313}
3314
Pavel Begunkov73debe62020-09-30 22:57:54 +03003315static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003316{
3317 ssize_t ret;
3318
Pavel Begunkova88fc402020-09-30 22:57:53 +03003319 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003320 if (ret)
3321 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003322
Jens Axboe3529d8c2019-12-19 18:24:38 -07003323 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3324 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003325
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003326 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003327 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003328 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003329 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003330}
3331
Jens Axboec1dd91d2020-08-03 16:43:59 -06003332/*
3333 * This is our waitqueue callback handler, registered through lock_page_async()
3334 * when we initially tried to do the IO with the iocb armed our waitqueue.
3335 * This gets called when the page is unlocked, and we generally expect that to
3336 * happen when the page IO is completed and the page is now uptodate. This will
3337 * queue a task_work based retry of the operation, attempting to copy the data
3338 * again. If the latter fails because the page was NOT uptodate, then we will
3339 * do a thread based blocking retry of the operation. That's the unexpected
3340 * slow path.
3341 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003342static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3343 int sync, void *arg)
3344{
3345 struct wait_page_queue *wpq;
3346 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003347 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003348 int ret;
3349
3350 wpq = container_of(wait, struct wait_page_queue, wait);
3351
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003352 if (!wake_page_match(wpq, key))
3353 return 0;
3354
Hao Xuc8d317a2020-09-29 20:00:45 +08003355 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003356 list_del_init(&wait->entry);
3357
Pavel Begunkove7375122020-07-12 20:42:04 +03003358 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003359 percpu_ref_get(&req->ctx->refs);
3360
Jens Axboebcf5a062020-05-22 09:24:42 -06003361 /* submit ref gets dropped, acquire a new one */
3362 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003363 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003364 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003365 struct task_struct *tsk;
3366
Jens Axboebcf5a062020-05-22 09:24:42 -06003367 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003368 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003369 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003370 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003371 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003372 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003373 return 1;
3374}
3375
Jens Axboec1dd91d2020-08-03 16:43:59 -06003376/*
3377 * This controls whether a given IO request should be armed for async page
3378 * based retry. If we return false here, the request is handed to the async
3379 * worker threads for retry. If we're doing buffered reads on a regular file,
3380 * we prepare a private wait_page_queue entry and retry the operation. This
3381 * will either succeed because the page is now uptodate and unlocked, or it
3382 * will register a callback when the page is unlocked at IO completion. Through
3383 * that callback, io_uring uses task_work to setup a retry of the operation.
3384 * That retry will attempt the buffered read again. The retry will generally
3385 * succeed, or in rare cases where it fails, we then fall back to using the
3386 * async worker threads for a blocking retry.
3387 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003388static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003389{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003390 struct io_async_rw *rw = req->async_data;
3391 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003392 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003393
3394 /* never retry for NOWAIT, we just complete with -EAGAIN */
3395 if (req->flags & REQ_F_NOWAIT)
3396 return false;
3397
Jens Axboe227c0c92020-08-13 11:51:40 -06003398 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003399 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003400 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003401
Jens Axboebcf5a062020-05-22 09:24:42 -06003402 /*
3403 * just use poll if we can, and don't attempt if the fs doesn't
3404 * support callback based unlocks
3405 */
3406 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3407 return false;
3408
Jens Axboe3b2a4432020-08-16 10:58:43 -07003409 wait->wait.func = io_async_buf_func;
3410 wait->wait.private = req;
3411 wait->wait.flags = 0;
3412 INIT_LIST_HEAD(&wait->wait.entry);
3413 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003414 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003415 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003416 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003417}
3418
3419static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3420{
3421 if (req->file->f_op->read_iter)
3422 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003423 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003424 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003425 else
3426 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003427}
3428
Jens Axboea1d7c392020-06-22 11:09:46 -06003429static int io_read(struct io_kiocb *req, bool force_nonblock,
3430 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003431{
3432 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003433 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003434 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003435 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003436 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003437 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003438 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003439
Jens Axboee8c2bc12020-08-15 18:44:09 -07003440 if (rw)
3441 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003442
3443 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003444 if (ret < 0)
3445 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003446 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003447 io_size = ret;
3448 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003449 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003450
Jens Axboefd6c2e42019-12-18 12:19:41 -07003451 /* Ensure we clear previously set non-block flag */
3452 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003453 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003454 else
3455 kiocb->ki_flags |= IOCB_NOWAIT;
3456
Jens Axboefd6c2e42019-12-18 12:19:41 -07003457
Pavel Begunkov24c74672020-06-21 13:09:51 +03003458 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003459 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3460 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003461 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003462
Jens Axboe0fef9482020-08-26 10:36:20 -06003463 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003464 if (unlikely(ret))
3465 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003466
Jens Axboe227c0c92020-08-13 11:51:40 -06003467 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003468
Jens Axboe227c0c92020-08-13 11:51:40 -06003469 if (!ret) {
3470 goto done;
3471 } else if (ret == -EIOCBQUEUED) {
3472 ret = 0;
3473 goto out_free;
3474 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003475 /* IOPOLL retry should happen for io-wq threads */
3476 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003477 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003478 /* no retry on NONBLOCK marked file */
3479 if (req->file->f_flags & O_NONBLOCK)
3480 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003481 /* some cases will consume bytes even on error returns */
3482 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003483 ret = 0;
3484 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003485 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003486 /* make sure -ERESTARTSYS -> -EINTR is done */
3487 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003488 }
3489
3490 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003491 if (!iov_iter_count(iter) || !force_nonblock ||
3492 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003493 goto done;
3494
3495 io_size -= ret;
3496copy_iov:
3497 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3498 if (ret2) {
3499 ret = ret2;
3500 goto out_free;
3501 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003502 if (no_async)
3503 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003504 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003505 /* it's copied and will be cleaned with ->io */
3506 iovec = NULL;
3507 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003508 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003509retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003510 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003511 /* if we can retry, do so with the callbacks armed */
3512 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003513 kiocb->ki_flags &= ~IOCB_WAITQ;
3514 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003515 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003516
3517 /*
3518 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3519 * get -EIOCBQUEUED, then we'll get a notification when the desired
3520 * page gets unlocked. We can also get a partial read here, and if we
3521 * do, then just retry at the new offset.
3522 */
3523 ret = io_iter_do_read(req, iter);
3524 if (ret == -EIOCBQUEUED) {
3525 ret = 0;
3526 goto out_free;
3527 } else if (ret > 0 && ret < io_size) {
3528 /* we got some bytes, but not all. retry. */
3529 goto retry;
3530 }
3531done:
3532 kiocb_done(kiocb, ret, cs);
3533 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003534out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003535 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003536 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003537 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003538 return ret;
3539}
3540
Pavel Begunkov73debe62020-09-30 22:57:54 +03003541static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003542{
3543 ssize_t ret;
3544
Pavel Begunkova88fc402020-09-30 22:57:53 +03003545 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003546 if (ret)
3547 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003548
Jens Axboe3529d8c2019-12-19 18:24:38 -07003549 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3550 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003551
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003552 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003553 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003554 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003555 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003556}
3557
Jens Axboea1d7c392020-06-22 11:09:46 -06003558static int io_write(struct io_kiocb *req, bool force_nonblock,
3559 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003560{
3561 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003562 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003563 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003564 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003565 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003566 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003567
Jens Axboee8c2bc12020-08-15 18:44:09 -07003568 if (rw)
3569 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003570
3571 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003572 if (ret < 0)
3573 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003574 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003575 io_size = ret;
3576 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003577
Jens Axboefd6c2e42019-12-18 12:19:41 -07003578 /* Ensure we clear previously set non-block flag */
3579 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003580 kiocb->ki_flags &= ~IOCB_NOWAIT;
3581 else
3582 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003583
Pavel Begunkov24c74672020-06-21 13:09:51 +03003584 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003585 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003586 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003587
Jens Axboe10d59342019-12-09 20:16:22 -07003588 /* file path doesn't support NOWAIT for non-direct_IO */
3589 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3590 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003591 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003592
Jens Axboe0fef9482020-08-26 10:36:20 -06003593 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003594 if (unlikely(ret))
3595 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003596
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003597 /*
3598 * Open-code file_start_write here to grab freeze protection,
3599 * which will be released by another thread in
3600 * io_complete_rw(). Fool lockdep by telling it the lock got
3601 * released so that it doesn't complain about the held lock when
3602 * we return to userspace.
3603 */
3604 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003605 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003606 __sb_writers_release(file_inode(req->file)->i_sb,
3607 SB_FREEZE_WRITE);
3608 }
3609 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003610
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003611 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003612 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003613 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003614 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003615 else
3616 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003617
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003618 /*
3619 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3620 * retry them without IOCB_NOWAIT.
3621 */
3622 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3623 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003624 /* no retry on NONBLOCK marked file */
3625 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3626 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003627 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003628 /* IOPOLL retry should happen for io-wq threads */
3629 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3630 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003631done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003632 kiocb_done(kiocb, ret2, cs);
3633 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003634copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003635 /* some cases will consume bytes even on error returns */
3636 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003637 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003638 if (!ret)
3639 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003640 }
Jens Axboe31b51512019-01-18 22:56:34 -07003641out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003642 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003643 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003644 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003645 return ret;
3646}
3647
Jens Axboe36f4fa62020-09-05 11:14:22 -06003648static int io_shutdown_prep(struct io_kiocb *req,
3649 const struct io_uring_sqe *sqe)
3650{
3651#if defined(CONFIG_NET)
3652 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3653 return -EINVAL;
3654 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3655 sqe->buf_index)
3656 return -EINVAL;
3657
3658 req->shutdown.how = READ_ONCE(sqe->len);
3659 return 0;
3660#else
3661 return -EOPNOTSUPP;
3662#endif
3663}
3664
3665static int io_shutdown(struct io_kiocb *req, bool force_nonblock)
3666{
3667#if defined(CONFIG_NET)
3668 struct socket *sock;
3669 int ret;
3670
3671 if (force_nonblock)
3672 return -EAGAIN;
3673
3674 sock = sock_from_file(req->file, &ret);
3675 if (unlikely(!sock))
3676 return ret;
3677
3678 ret = __sys_shutdown_sock(sock, req->shutdown.how);
3679 io_req_complete(req, ret);
3680 return 0;
3681#else
3682 return -EOPNOTSUPP;
3683#endif
3684}
3685
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003686static int __io_splice_prep(struct io_kiocb *req,
3687 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003688{
3689 struct io_splice* sp = &req->splice;
3690 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003691
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003692 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3693 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003694
3695 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003696 sp->len = READ_ONCE(sqe->len);
3697 sp->flags = READ_ONCE(sqe->splice_flags);
3698
3699 if (unlikely(sp->flags & ~valid_flags))
3700 return -EINVAL;
3701
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003702 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3703 (sp->flags & SPLICE_F_FD_IN_FIXED));
3704 if (!sp->file_in)
3705 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003706 req->flags |= REQ_F_NEED_CLEANUP;
3707
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003708 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3709 /*
3710 * Splice operation will be punted aync, and here need to
3711 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3712 */
3713 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003714 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003715 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003716
3717 return 0;
3718}
3719
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003720static int io_tee_prep(struct io_kiocb *req,
3721 const struct io_uring_sqe *sqe)
3722{
3723 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3724 return -EINVAL;
3725 return __io_splice_prep(req, sqe);
3726}
3727
3728static int io_tee(struct io_kiocb *req, bool force_nonblock)
3729{
3730 struct io_splice *sp = &req->splice;
3731 struct file *in = sp->file_in;
3732 struct file *out = sp->file_out;
3733 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3734 long ret = 0;
3735
3736 if (force_nonblock)
3737 return -EAGAIN;
3738 if (sp->len)
3739 ret = do_tee(in, out, sp->len, flags);
3740
3741 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3742 req->flags &= ~REQ_F_NEED_CLEANUP;
3743
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003744 if (ret != sp->len)
3745 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003746 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003747 return 0;
3748}
3749
3750static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3751{
3752 struct io_splice* sp = &req->splice;
3753
3754 sp->off_in = READ_ONCE(sqe->splice_off_in);
3755 sp->off_out = READ_ONCE(sqe->off);
3756 return __io_splice_prep(req, sqe);
3757}
3758
Pavel Begunkov014db002020-03-03 21:33:12 +03003759static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003760{
3761 struct io_splice *sp = &req->splice;
3762 struct file *in = sp->file_in;
3763 struct file *out = sp->file_out;
3764 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3765 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003766 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003767
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003768 if (force_nonblock)
3769 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003770
3771 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3772 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003773
Jens Axboe948a7742020-05-17 14:21:38 -06003774 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003775 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003776
3777 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3778 req->flags &= ~REQ_F_NEED_CLEANUP;
3779
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003780 if (ret != sp->len)
3781 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003782 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003783 return 0;
3784}
3785
Jens Axboe2b188cc2019-01-07 10:46:33 -07003786/*
3787 * IORING_OP_NOP just posts a completion event, nothing else.
3788 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003789static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003790{
3791 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003792
Jens Axboedef596e2019-01-09 08:59:42 -07003793 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3794 return -EINVAL;
3795
Jens Axboe229a7b62020-06-22 10:13:11 -06003796 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003797 return 0;
3798}
3799
Jens Axboe3529d8c2019-12-19 18:24:38 -07003800static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003801{
Jens Axboe6b063142019-01-10 22:13:58 -07003802 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003803
Jens Axboe09bb8392019-03-13 12:39:28 -06003804 if (!req->file)
3805 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003806
Jens Axboe6b063142019-01-10 22:13:58 -07003807 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003808 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003809 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003810 return -EINVAL;
3811
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003812 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3813 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3814 return -EINVAL;
3815
3816 req->sync.off = READ_ONCE(sqe->off);
3817 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003818 return 0;
3819}
3820
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003821static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003822{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003823 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003824 int ret;
3825
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003826 /* fsync always requires a blocking context */
3827 if (force_nonblock)
3828 return -EAGAIN;
3829
Jens Axboe9adbd452019-12-20 08:45:55 -07003830 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003831 end > 0 ? end : LLONG_MAX,
3832 req->sync.flags & IORING_FSYNC_DATASYNC);
3833 if (ret < 0)
3834 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003835 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003836 return 0;
3837}
3838
Jens Axboed63d1b52019-12-10 10:38:56 -07003839static int io_fallocate_prep(struct io_kiocb *req,
3840 const struct io_uring_sqe *sqe)
3841{
3842 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3843 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003844 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3845 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003846
3847 req->sync.off = READ_ONCE(sqe->off);
3848 req->sync.len = READ_ONCE(sqe->addr);
3849 req->sync.mode = READ_ONCE(sqe->len);
3850 return 0;
3851}
3852
Pavel Begunkov014db002020-03-03 21:33:12 +03003853static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003854{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003855 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003856
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003857 /* fallocate always requiring blocking context */
3858 if (force_nonblock)
3859 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003860 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3861 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003862 if (ret < 0)
3863 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003864 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003865 return 0;
3866}
3867
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003868static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003869{
Jens Axboef8748882020-01-08 17:47:02 -07003870 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003871 int ret;
3872
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003873 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003874 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003875 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003876 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003877
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003878 /* open.how should be already initialised */
3879 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003880 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003881
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003882 req->open.dfd = READ_ONCE(sqe->fd);
3883 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003884 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003885 if (IS_ERR(req->open.filename)) {
3886 ret = PTR_ERR(req->open.filename);
3887 req->open.filename = NULL;
3888 return ret;
3889 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003890 req->open.nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe944d1442020-11-13 16:48:44 -07003891 req->open.ignore_nonblock = false;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003892 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003893 return 0;
3894}
3895
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003896static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3897{
3898 u64 flags, mode;
3899
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003900 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3901 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003902 mode = READ_ONCE(sqe->len);
3903 flags = READ_ONCE(sqe->open_flags);
3904 req->open.how = build_open_how(flags, mode);
3905 return __io_openat_prep(req, sqe);
3906}
3907
Jens Axboecebdb982020-01-08 17:59:24 -07003908static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3909{
3910 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003911 size_t len;
3912 int ret;
3913
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003914 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3915 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003916 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3917 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003918 if (len < OPEN_HOW_SIZE_VER0)
3919 return -EINVAL;
3920
3921 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3922 len);
3923 if (ret)
3924 return ret;
3925
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003926 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003927}
3928
Pavel Begunkov014db002020-03-03 21:33:12 +03003929static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003930{
3931 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003932 struct file *file;
3933 int ret;
3934
Jens Axboe944d1442020-11-13 16:48:44 -07003935 if (force_nonblock && !req->open.ignore_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003936 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003937
Jens Axboecebdb982020-01-08 17:59:24 -07003938 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003939 if (ret)
3940 goto err;
3941
Jens Axboe4022e7a2020-03-19 19:23:18 -06003942 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003943 if (ret < 0)
3944 goto err;
3945
3946 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3947 if (IS_ERR(file)) {
3948 put_unused_fd(ret);
3949 ret = PTR_ERR(file);
Jens Axboe944d1442020-11-13 16:48:44 -07003950 /*
3951 * A work-around to ensure that /proc/self works that way
3952 * that it should - if we get -EOPNOTSUPP back, then assume
3953 * that proc_self_get_link() failed us because we're in async
3954 * context. We should be safe to retry this from the task
3955 * itself with force_nonblock == false set, as it should not
3956 * block on lookup. Would be nice to know this upfront and
3957 * avoid the async dance, but doesn't seem feasible.
3958 */
3959 if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) {
3960 req->open.ignore_nonblock = true;
3961 refcount_inc(&req->refs);
3962 io_req_task_queue(req);
3963 return 0;
3964 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003965 } else {
3966 fsnotify_open(file);
3967 fd_install(ret, file);
3968 }
3969err:
3970 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003971 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003972 if (ret < 0)
3973 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003974 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003975 return 0;
3976}
3977
Pavel Begunkov014db002020-03-03 21:33:12 +03003978static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003979{
Pavel Begunkov014db002020-03-03 21:33:12 +03003980 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003981}
3982
Jens Axboe067524e2020-03-02 16:32:28 -07003983static int io_remove_buffers_prep(struct io_kiocb *req,
3984 const struct io_uring_sqe *sqe)
3985{
3986 struct io_provide_buf *p = &req->pbuf;
3987 u64 tmp;
3988
3989 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3990 return -EINVAL;
3991
3992 tmp = READ_ONCE(sqe->fd);
3993 if (!tmp || tmp > USHRT_MAX)
3994 return -EINVAL;
3995
3996 memset(p, 0, sizeof(*p));
3997 p->nbufs = tmp;
3998 p->bgid = READ_ONCE(sqe->buf_group);
3999 return 0;
4000}
4001
4002static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4003 int bgid, unsigned nbufs)
4004{
4005 unsigned i = 0;
4006
4007 /* shouldn't happen */
4008 if (!nbufs)
4009 return 0;
4010
4011 /* the head kbuf is the list itself */
4012 while (!list_empty(&buf->list)) {
4013 struct io_buffer *nxt;
4014
4015 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4016 list_del(&nxt->list);
4017 kfree(nxt);
4018 if (++i == nbufs)
4019 return i;
4020 }
4021 i++;
4022 kfree(buf);
4023 idr_remove(&ctx->io_buffer_idr, bgid);
4024
4025 return i;
4026}
4027
Jens Axboe229a7b62020-06-22 10:13:11 -06004028static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
4029 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07004030{
4031 struct io_provide_buf *p = &req->pbuf;
4032 struct io_ring_ctx *ctx = req->ctx;
4033 struct io_buffer *head;
4034 int ret = 0;
4035
4036 io_ring_submit_lock(ctx, !force_nonblock);
4037
4038 lockdep_assert_held(&ctx->uring_lock);
4039
4040 ret = -ENOENT;
4041 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4042 if (head)
4043 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
4044
4045 io_ring_submit_lock(ctx, !force_nonblock);
4046 if (ret < 0)
4047 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004048 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07004049 return 0;
4050}
4051
Jens Axboeddf0322d2020-02-23 16:41:33 -07004052static int io_provide_buffers_prep(struct io_kiocb *req,
4053 const struct io_uring_sqe *sqe)
4054{
4055 struct io_provide_buf *p = &req->pbuf;
4056 u64 tmp;
4057
4058 if (sqe->ioprio || sqe->rw_flags)
4059 return -EINVAL;
4060
4061 tmp = READ_ONCE(sqe->fd);
4062 if (!tmp || tmp > USHRT_MAX)
4063 return -E2BIG;
4064 p->nbufs = tmp;
4065 p->addr = READ_ONCE(sqe->addr);
4066 p->len = READ_ONCE(sqe->len);
4067
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004068 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004069 return -EFAULT;
4070
4071 p->bgid = READ_ONCE(sqe->buf_group);
4072 tmp = READ_ONCE(sqe->off);
4073 if (tmp > USHRT_MAX)
4074 return -E2BIG;
4075 p->bid = tmp;
4076 return 0;
4077}
4078
4079static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4080{
4081 struct io_buffer *buf;
4082 u64 addr = pbuf->addr;
4083 int i, bid = pbuf->bid;
4084
4085 for (i = 0; i < pbuf->nbufs; i++) {
4086 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4087 if (!buf)
4088 break;
4089
4090 buf->addr = addr;
4091 buf->len = pbuf->len;
4092 buf->bid = bid;
4093 addr += pbuf->len;
4094 bid++;
4095 if (!*head) {
4096 INIT_LIST_HEAD(&buf->list);
4097 *head = buf;
4098 } else {
4099 list_add_tail(&buf->list, &(*head)->list);
4100 }
4101 }
4102
4103 return i ? i : -ENOMEM;
4104}
4105
Jens Axboe229a7b62020-06-22 10:13:11 -06004106static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4107 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004108{
4109 struct io_provide_buf *p = &req->pbuf;
4110 struct io_ring_ctx *ctx = req->ctx;
4111 struct io_buffer *head, *list;
4112 int ret = 0;
4113
4114 io_ring_submit_lock(ctx, !force_nonblock);
4115
4116 lockdep_assert_held(&ctx->uring_lock);
4117
4118 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4119
4120 ret = io_add_buffers(p, &head);
4121 if (ret < 0)
4122 goto out;
4123
4124 if (!list) {
4125 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4126 GFP_KERNEL);
4127 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004128 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004129 goto out;
4130 }
4131 }
4132out:
4133 io_ring_submit_unlock(ctx, !force_nonblock);
4134 if (ret < 0)
4135 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004136 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004137 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004138}
4139
Jens Axboe3e4827b2020-01-08 15:18:09 -07004140static int io_epoll_ctl_prep(struct io_kiocb *req,
4141 const struct io_uring_sqe *sqe)
4142{
4143#if defined(CONFIG_EPOLL)
4144 if (sqe->ioprio || sqe->buf_index)
4145 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004146 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004147 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004148
4149 req->epoll.epfd = READ_ONCE(sqe->fd);
4150 req->epoll.op = READ_ONCE(sqe->len);
4151 req->epoll.fd = READ_ONCE(sqe->off);
4152
4153 if (ep_op_has_event(req->epoll.op)) {
4154 struct epoll_event __user *ev;
4155
4156 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4157 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4158 return -EFAULT;
4159 }
4160
4161 return 0;
4162#else
4163 return -EOPNOTSUPP;
4164#endif
4165}
4166
Jens Axboe229a7b62020-06-22 10:13:11 -06004167static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4168 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004169{
4170#if defined(CONFIG_EPOLL)
4171 struct io_epoll *ie = &req->epoll;
4172 int ret;
4173
4174 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4175 if (force_nonblock && ret == -EAGAIN)
4176 return -EAGAIN;
4177
4178 if (ret < 0)
4179 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004180 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004181 return 0;
4182#else
4183 return -EOPNOTSUPP;
4184#endif
4185}
4186
Jens Axboec1ca7572019-12-25 22:18:28 -07004187static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4188{
4189#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4190 if (sqe->ioprio || sqe->buf_index || sqe->off)
4191 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004192 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4193 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004194
4195 req->madvise.addr = READ_ONCE(sqe->addr);
4196 req->madvise.len = READ_ONCE(sqe->len);
4197 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4198 return 0;
4199#else
4200 return -EOPNOTSUPP;
4201#endif
4202}
4203
Pavel Begunkov014db002020-03-03 21:33:12 +03004204static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004205{
4206#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4207 struct io_madvise *ma = &req->madvise;
4208 int ret;
4209
4210 if (force_nonblock)
4211 return -EAGAIN;
4212
Minchan Kim0726b012020-10-17 16:14:50 -07004213 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004214 if (ret < 0)
4215 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004216 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004217 return 0;
4218#else
4219 return -EOPNOTSUPP;
4220#endif
4221}
4222
Jens Axboe4840e412019-12-25 22:03:45 -07004223static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4224{
4225 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4226 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004227 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4228 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004229
4230 req->fadvise.offset = READ_ONCE(sqe->off);
4231 req->fadvise.len = READ_ONCE(sqe->len);
4232 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4233 return 0;
4234}
4235
Pavel Begunkov014db002020-03-03 21:33:12 +03004236static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004237{
4238 struct io_fadvise *fa = &req->fadvise;
4239 int ret;
4240
Jens Axboe3e694262020-02-01 09:22:49 -07004241 if (force_nonblock) {
4242 switch (fa->advice) {
4243 case POSIX_FADV_NORMAL:
4244 case POSIX_FADV_RANDOM:
4245 case POSIX_FADV_SEQUENTIAL:
4246 break;
4247 default:
4248 return -EAGAIN;
4249 }
4250 }
Jens Axboe4840e412019-12-25 22:03:45 -07004251
4252 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4253 if (ret < 0)
4254 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004255 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004256 return 0;
4257}
4258
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004259static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4260{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004261 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004262 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004263 if (sqe->ioprio || sqe->buf_index)
4264 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004265 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004266 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004267
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004268 req->statx.dfd = READ_ONCE(sqe->fd);
4269 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004270 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004271 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4272 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004273
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004274 return 0;
4275}
4276
Pavel Begunkov014db002020-03-03 21:33:12 +03004277static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004278{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004279 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004280 int ret;
4281
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004282 if (force_nonblock) {
4283 /* only need file table for an actual valid fd */
4284 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4285 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004286 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004287 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004288
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004289 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4290 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004291
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004292 if (ret < 0)
4293 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004294 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004295 return 0;
4296}
4297
Jens Axboeb5dba592019-12-11 14:02:38 -07004298static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4299{
4300 /*
4301 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004302 * leave the 'file' in an undeterminate state, and here need to modify
4303 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004304 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004305 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004306 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4307
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004308 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4309 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004310 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4311 sqe->rw_flags || sqe->buf_index)
4312 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004313 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004314 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004315
4316 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004317 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004318 return -EBADF;
4319
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004320 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004321 return 0;
4322}
4323
Jens Axboe229a7b62020-06-22 10:13:11 -06004324static int io_close(struct io_kiocb *req, bool force_nonblock,
4325 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004326{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004327 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004328 int ret;
4329
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004330 /* might be already done during nonblock submission */
4331 if (!close->put_file) {
4332 ret = __close_fd_get_file(close->fd, &close->put_file);
4333 if (ret < 0)
4334 return (ret == -ENOENT) ? -EBADF : ret;
4335 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004336
4337 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004338 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004339 /* was never set, but play safe */
4340 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004341 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004342 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004343 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004344 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004345
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004346 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004347 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004348 if (ret < 0)
4349 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004350 fput(close->put_file);
4351 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004352 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004353 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004354}
4355
Jens Axboe3529d8c2019-12-19 18:24:38 -07004356static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004357{
4358 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004359
4360 if (!req->file)
4361 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004362
4363 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4364 return -EINVAL;
4365 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4366 return -EINVAL;
4367
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004368 req->sync.off = READ_ONCE(sqe->off);
4369 req->sync.len = READ_ONCE(sqe->len);
4370 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004371 return 0;
4372}
4373
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004374static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004375{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004376 int ret;
4377
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004378 /* sync_file_range always requires a blocking context */
4379 if (force_nonblock)
4380 return -EAGAIN;
4381
Jens Axboe9adbd452019-12-20 08:45:55 -07004382 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004383 req->sync.flags);
4384 if (ret < 0)
4385 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004386 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004387 return 0;
4388}
4389
YueHaibing469956e2020-03-04 15:53:52 +08004390#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004391static int io_setup_async_msg(struct io_kiocb *req,
4392 struct io_async_msghdr *kmsg)
4393{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004394 struct io_async_msghdr *async_msg = req->async_data;
4395
4396 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004397 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004398 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004399 if (kmsg->iov != kmsg->fast_iov)
4400 kfree(kmsg->iov);
4401 return -ENOMEM;
4402 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004403 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004404 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004405 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004406 return -EAGAIN;
4407}
4408
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004409static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4410 struct io_async_msghdr *iomsg)
4411{
4412 iomsg->iov = iomsg->fast_iov;
4413 iomsg->msg.msg_name = &iomsg->addr;
4414 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4415 req->sr_msg.msg_flags, &iomsg->iov);
4416}
4417
Jens Axboe3529d8c2019-12-19 18:24:38 -07004418static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004419{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004420 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004421 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004422 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004423
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004424 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4425 return -EINVAL;
4426
Jens Axboee47293f2019-12-20 08:58:21 -07004427 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004428 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004429 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004430
Jens Axboed8768362020-02-27 14:17:49 -07004431#ifdef CONFIG_COMPAT
4432 if (req->ctx->compat)
4433 sr->msg_flags |= MSG_CMSG_COMPAT;
4434#endif
4435
Jens Axboee8c2bc12020-08-15 18:44:09 -07004436 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004437 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004438 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004439 if (!ret)
4440 req->flags |= REQ_F_NEED_CLEANUP;
4441 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004442}
4443
Jens Axboe229a7b62020-06-22 10:13:11 -06004444static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4445 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004446{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004447 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004448 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004449 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004450 int ret;
4451
Jens Axboe03b12302019-12-02 18:50:25 -07004452 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004453 if (unlikely(!sock))
4454 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004455
Jens Axboee8c2bc12020-08-15 18:44:09 -07004456 if (req->async_data) {
4457 kmsg = req->async_data;
4458 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004459 /* if iov is set, it's allocated already */
4460 if (!kmsg->iov)
4461 kmsg->iov = kmsg->fast_iov;
4462 kmsg->msg.msg_iter.iov = kmsg->iov;
4463 } else {
4464 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004465 if (ret)
4466 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004467 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004468 }
4469
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004470 flags = req->sr_msg.msg_flags;
4471 if (flags & MSG_DONTWAIT)
4472 req->flags |= REQ_F_NOWAIT;
4473 else if (force_nonblock)
4474 flags |= MSG_DONTWAIT;
4475
4476 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4477 if (force_nonblock && ret == -EAGAIN)
4478 return io_setup_async_msg(req, kmsg);
4479 if (ret == -ERESTARTSYS)
4480 ret = -EINTR;
4481
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004482 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004483 kfree(kmsg->iov);
4484 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004485 if (ret < 0)
4486 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004487 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004488 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004489}
4490
Jens Axboe229a7b62020-06-22 10:13:11 -06004491static int io_send(struct io_kiocb *req, bool force_nonblock,
4492 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004493{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004494 struct io_sr_msg *sr = &req->sr_msg;
4495 struct msghdr msg;
4496 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004497 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004498 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004499 int ret;
4500
4501 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004502 if (unlikely(!sock))
4503 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004504
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004505 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4506 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004507 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004508
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004509 msg.msg_name = NULL;
4510 msg.msg_control = NULL;
4511 msg.msg_controllen = 0;
4512 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004513
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004514 flags = req->sr_msg.msg_flags;
4515 if (flags & MSG_DONTWAIT)
4516 req->flags |= REQ_F_NOWAIT;
4517 else if (force_nonblock)
4518 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004519
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004520 msg.msg_flags = flags;
4521 ret = sock_sendmsg(sock, &msg);
4522 if (force_nonblock && ret == -EAGAIN)
4523 return -EAGAIN;
4524 if (ret == -ERESTARTSYS)
4525 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004526
Jens Axboe03b12302019-12-02 18:50:25 -07004527 if (ret < 0)
4528 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004529 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004530 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004531}
4532
Pavel Begunkov1400e692020-07-12 20:41:05 +03004533static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4534 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004535{
4536 struct io_sr_msg *sr = &req->sr_msg;
4537 struct iovec __user *uiov;
4538 size_t iov_len;
4539 int ret;
4540
Pavel Begunkov1400e692020-07-12 20:41:05 +03004541 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4542 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004543 if (ret)
4544 return ret;
4545
4546 if (req->flags & REQ_F_BUFFER_SELECT) {
4547 if (iov_len > 1)
4548 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004549 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004550 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004551 sr->len = iomsg->iov[0].iov_len;
4552 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004553 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004554 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004555 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004556 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4557 &iomsg->iov, &iomsg->msg.msg_iter,
4558 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004559 if (ret > 0)
4560 ret = 0;
4561 }
4562
4563 return ret;
4564}
4565
4566#ifdef CONFIG_COMPAT
4567static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004568 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004569{
4570 struct compat_msghdr __user *msg_compat;
4571 struct io_sr_msg *sr = &req->sr_msg;
4572 struct compat_iovec __user *uiov;
4573 compat_uptr_t ptr;
4574 compat_size_t len;
4575 int ret;
4576
Pavel Begunkov270a5942020-07-12 20:41:04 +03004577 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004578 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004579 &ptr, &len);
4580 if (ret)
4581 return ret;
4582
4583 uiov = compat_ptr(ptr);
4584 if (req->flags & REQ_F_BUFFER_SELECT) {
4585 compat_ssize_t clen;
4586
4587 if (len > 1)
4588 return -EINVAL;
4589 if (!access_ok(uiov, sizeof(*uiov)))
4590 return -EFAULT;
4591 if (__get_user(clen, &uiov->iov_len))
4592 return -EFAULT;
4593 if (clen < 0)
4594 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004595 sr->len = iomsg->iov[0].iov_len;
4596 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004597 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004598 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4599 UIO_FASTIOV, &iomsg->iov,
4600 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004601 if (ret < 0)
4602 return ret;
4603 }
4604
4605 return 0;
4606}
Jens Axboe03b12302019-12-02 18:50:25 -07004607#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004608
Pavel Begunkov1400e692020-07-12 20:41:05 +03004609static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4610 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004611{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004612 iomsg->msg.msg_name = &iomsg->addr;
4613 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004614
4615#ifdef CONFIG_COMPAT
4616 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004617 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004618#endif
4619
Pavel Begunkov1400e692020-07-12 20:41:05 +03004620 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004621}
4622
Jens Axboebcda7ba2020-02-23 16:42:51 -07004623static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004624 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004625{
4626 struct io_sr_msg *sr = &req->sr_msg;
4627 struct io_buffer *kbuf;
4628
Jens Axboebcda7ba2020-02-23 16:42:51 -07004629 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4630 if (IS_ERR(kbuf))
4631 return kbuf;
4632
4633 sr->kbuf = kbuf;
4634 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004635 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004636}
4637
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004638static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4639{
4640 return io_put_kbuf(req, req->sr_msg.kbuf);
4641}
4642
Jens Axboe3529d8c2019-12-19 18:24:38 -07004643static int io_recvmsg_prep(struct io_kiocb *req,
4644 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004645{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004646 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004647 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004648 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004649
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004650 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4651 return -EINVAL;
4652
Jens Axboe3529d8c2019-12-19 18:24:38 -07004653 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004654 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004655 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004656 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004657
Jens Axboed8768362020-02-27 14:17:49 -07004658#ifdef CONFIG_COMPAT
4659 if (req->ctx->compat)
4660 sr->msg_flags |= MSG_CMSG_COMPAT;
4661#endif
4662
Jens Axboee8c2bc12020-08-15 18:44:09 -07004663 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004664 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004665 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004666 if (!ret)
4667 req->flags |= REQ_F_NEED_CLEANUP;
4668 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004669}
4670
Jens Axboe229a7b62020-06-22 10:13:11 -06004671static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4672 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004673{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004674 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004675 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004676 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004677 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004678 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004679
Jens Axboe0fa03c62019-04-19 13:34:07 -06004680 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004681 if (unlikely(!sock))
4682 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004683
Jens Axboee8c2bc12020-08-15 18:44:09 -07004684 if (req->async_data) {
4685 kmsg = req->async_data;
4686 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004687 /* if iov is set, it's allocated already */
4688 if (!kmsg->iov)
4689 kmsg->iov = kmsg->fast_iov;
4690 kmsg->msg.msg_iter.iov = kmsg->iov;
4691 } else {
4692 ret = io_recvmsg_copy_hdr(req, &iomsg);
4693 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004694 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004695 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004696 }
4697
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004698 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004699 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004700 if (IS_ERR(kbuf))
4701 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004702 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4703 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4704 1, req->sr_msg.len);
4705 }
4706
4707 flags = req->sr_msg.msg_flags;
4708 if (flags & MSG_DONTWAIT)
4709 req->flags |= REQ_F_NOWAIT;
4710 else if (force_nonblock)
4711 flags |= MSG_DONTWAIT;
4712
4713 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4714 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004715 if (force_nonblock && ret == -EAGAIN)
4716 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004717 if (ret == -ERESTARTSYS)
4718 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004719
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004720 if (req->flags & REQ_F_BUFFER_SELECTED)
4721 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004722 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004723 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004724 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004725 if (ret < 0)
4726 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004727 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004728 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004729}
4730
Jens Axboe229a7b62020-06-22 10:13:11 -06004731static int io_recv(struct io_kiocb *req, bool force_nonblock,
4732 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004733{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004734 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004735 struct io_sr_msg *sr = &req->sr_msg;
4736 struct msghdr msg;
4737 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004738 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004739 struct iovec iov;
4740 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004741 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004742
Jens Axboefddafac2020-01-04 20:19:44 -07004743 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004744 if (unlikely(!sock))
4745 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004746
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004747 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004748 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004749 if (IS_ERR(kbuf))
4750 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004751 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004752 }
4753
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004754 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004755 if (unlikely(ret))
4756 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004757
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004758 msg.msg_name = NULL;
4759 msg.msg_control = NULL;
4760 msg.msg_controllen = 0;
4761 msg.msg_namelen = 0;
4762 msg.msg_iocb = NULL;
4763 msg.msg_flags = 0;
4764
4765 flags = req->sr_msg.msg_flags;
4766 if (flags & MSG_DONTWAIT)
4767 req->flags |= REQ_F_NOWAIT;
4768 else if (force_nonblock)
4769 flags |= MSG_DONTWAIT;
4770
4771 ret = sock_recvmsg(sock, &msg, flags);
4772 if (force_nonblock && ret == -EAGAIN)
4773 return -EAGAIN;
4774 if (ret == -ERESTARTSYS)
4775 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004776out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004777 if (req->flags & REQ_F_BUFFER_SELECTED)
4778 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004779 if (ret < 0)
4780 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004781 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004782 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004783}
4784
Jens Axboe3529d8c2019-12-19 18:24:38 -07004785static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004786{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004787 struct io_accept *accept = &req->accept;
4788
Jens Axboe17f2fe32019-10-17 14:42:58 -06004789 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4790 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004791 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004792 return -EINVAL;
4793
Jens Axboed55e5f52019-12-11 16:12:15 -07004794 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4795 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004796 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004797 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004798 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004799}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004800
Jens Axboe229a7b62020-06-22 10:13:11 -06004801static int io_accept(struct io_kiocb *req, bool force_nonblock,
4802 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004803{
4804 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004805 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004806 int ret;
4807
Jiufei Xuee697dee2020-06-10 13:41:59 +08004808 if (req->file->f_flags & O_NONBLOCK)
4809 req->flags |= REQ_F_NOWAIT;
4810
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004811 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004812 accept->addr_len, accept->flags,
4813 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004814 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004815 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004816 if (ret < 0) {
4817 if (ret == -ERESTARTSYS)
4818 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004819 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004820 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004821 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004822 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004823}
4824
Jens Axboe3529d8c2019-12-19 18:24:38 -07004825static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004826{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004827 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004828 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004829
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004830 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4831 return -EINVAL;
4832 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4833 return -EINVAL;
4834
Jens Axboe3529d8c2019-12-19 18:24:38 -07004835 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4836 conn->addr_len = READ_ONCE(sqe->addr2);
4837
4838 if (!io)
4839 return 0;
4840
4841 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004842 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004843}
4844
Jens Axboe229a7b62020-06-22 10:13:11 -06004845static int io_connect(struct io_kiocb *req, bool force_nonblock,
4846 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004847{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004848 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004849 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004850 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004851
Jens Axboee8c2bc12020-08-15 18:44:09 -07004852 if (req->async_data) {
4853 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004854 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004855 ret = move_addr_to_kernel(req->connect.addr,
4856 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004857 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004858 if (ret)
4859 goto out;
4860 io = &__io;
4861 }
4862
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004863 file_flags = force_nonblock ? O_NONBLOCK : 0;
4864
Jens Axboee8c2bc12020-08-15 18:44:09 -07004865 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004866 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004867 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004868 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004869 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004870 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004871 ret = -ENOMEM;
4872 goto out;
4873 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004874 io = req->async_data;
4875 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004876 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004877 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004878 if (ret == -ERESTARTSYS)
4879 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004880out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004881 if (ret < 0)
4882 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004883 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004884 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004885}
YueHaibing469956e2020-03-04 15:53:52 +08004886#else /* !CONFIG_NET */
4887static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4888{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004889 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004890}
4891
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004892static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4893 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004894{
YueHaibing469956e2020-03-04 15:53:52 +08004895 return -EOPNOTSUPP;
4896}
4897
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004898static int io_send(struct io_kiocb *req, bool force_nonblock,
4899 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004900{
4901 return -EOPNOTSUPP;
4902}
4903
4904static int io_recvmsg_prep(struct io_kiocb *req,
4905 const struct io_uring_sqe *sqe)
4906{
4907 return -EOPNOTSUPP;
4908}
4909
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004910static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4911 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004912{
4913 return -EOPNOTSUPP;
4914}
4915
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004916static int io_recv(struct io_kiocb *req, bool force_nonblock,
4917 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004918{
4919 return -EOPNOTSUPP;
4920}
4921
4922static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4923{
4924 return -EOPNOTSUPP;
4925}
4926
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004927static int io_accept(struct io_kiocb *req, bool force_nonblock,
4928 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004929{
4930 return -EOPNOTSUPP;
4931}
4932
4933static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4934{
4935 return -EOPNOTSUPP;
4936}
4937
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004938static int io_connect(struct io_kiocb *req, bool force_nonblock,
4939 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004940{
4941 return -EOPNOTSUPP;
4942}
4943#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004944
Jens Axboed7718a92020-02-14 22:23:12 -07004945struct io_poll_table {
4946 struct poll_table_struct pt;
4947 struct io_kiocb *req;
4948 int error;
4949};
4950
Jens Axboed7718a92020-02-14 22:23:12 -07004951static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4952 __poll_t mask, task_work_func_t func)
4953{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004954 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004955 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004956
4957 /* for instances that support it check for an event match first: */
4958 if (mask && !(mask & poll->events))
4959 return 0;
4960
4961 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4962
4963 list_del_init(&poll->wait.entry);
4964
Jens Axboed7718a92020-02-14 22:23:12 -07004965 req->result = mask;
4966 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004967 percpu_ref_get(&req->ctx->refs);
4968
Jens Axboed7718a92020-02-14 22:23:12 -07004969 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004970 * If we using the signalfd wait_queue_head for this wakeup, then
4971 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4972 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4973 * either, as the normal wakeup will suffice.
4974 */
4975 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4976
4977 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004978 * If this fails, then the task is exiting. When a task exits, the
4979 * work gets canceled, so just cancel this request as well instead
4980 * of executing it. We can't safely execute it anyway, as we may not
4981 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004982 */
Jens Axboe87c43112020-09-30 21:00:14 -06004983 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004984 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004985 struct task_struct *tsk;
4986
Jens Axboee3aabf92020-05-18 11:04:17 -06004987 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004988 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06004989 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06004990 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004991 }
Jens Axboed7718a92020-02-14 22:23:12 -07004992 return 1;
4993}
4994
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004995static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4996 __acquires(&req->ctx->completion_lock)
4997{
4998 struct io_ring_ctx *ctx = req->ctx;
4999
5000 if (!req->result && !READ_ONCE(poll->canceled)) {
5001 struct poll_table_struct pt = { ._key = poll->events };
5002
5003 req->result = vfs_poll(req->file, &pt) & poll->events;
5004 }
5005
5006 spin_lock_irq(&ctx->completion_lock);
5007 if (!req->result && !READ_ONCE(poll->canceled)) {
5008 add_wait_queue(poll->head, &poll->wait);
5009 return true;
5010 }
5011
5012 return false;
5013}
5014
Jens Axboed4e7cd32020-08-15 11:44:50 -07005015static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005016{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005017 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005018 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005019 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005020 return req->apoll->double_poll;
5021}
5022
5023static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5024{
5025 if (req->opcode == IORING_OP_POLL_ADD)
5026 return &req->poll;
5027 return &req->apoll->poll;
5028}
5029
5030static void io_poll_remove_double(struct io_kiocb *req)
5031{
5032 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005033
5034 lockdep_assert_held(&req->ctx->completion_lock);
5035
5036 if (poll && poll->head) {
5037 struct wait_queue_head *head = poll->head;
5038
5039 spin_lock(&head->lock);
5040 list_del_init(&poll->wait.entry);
5041 if (poll->wait.private)
5042 refcount_dec(&req->refs);
5043 poll->head = NULL;
5044 spin_unlock(&head->lock);
5045 }
5046}
5047
5048static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5049{
5050 struct io_ring_ctx *ctx = req->ctx;
5051
Jens Axboed4e7cd32020-08-15 11:44:50 -07005052 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005053 req->poll.done = true;
5054 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5055 io_commit_cqring(ctx);
5056}
5057
Jens Axboe18bceab2020-05-15 11:56:54 -06005058static void io_poll_task_func(struct callback_head *cb)
5059{
5060 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005061 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005062 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005063
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005064 if (io_poll_rewait(req, &req->poll)) {
5065 spin_unlock_irq(&ctx->completion_lock);
5066 } else {
5067 hash_del(&req->hash_node);
5068 io_poll_complete(req, req->result, 0);
5069 spin_unlock_irq(&ctx->completion_lock);
5070
5071 nxt = io_put_req_find_next(req);
5072 io_cqring_ev_posted(ctx);
5073 if (nxt)
5074 __io_req_task_submit(nxt);
5075 }
5076
Jens Axboe6d816e02020-08-11 08:04:14 -06005077 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005078}
5079
5080static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5081 int sync, void *key)
5082{
5083 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005084 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005085 __poll_t mask = key_to_poll(key);
5086
5087 /* for instances that support it check for an event match first: */
5088 if (mask && !(mask & poll->events))
5089 return 0;
5090
Jens Axboe8706e042020-09-28 08:38:54 -06005091 list_del_init(&wait->entry);
5092
Jens Axboe807abcb2020-07-17 17:09:27 -06005093 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005094 bool done;
5095
Jens Axboe807abcb2020-07-17 17:09:27 -06005096 spin_lock(&poll->head->lock);
5097 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005098 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005099 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005100 /* make sure double remove sees this as being gone */
5101 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005102 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005103 if (!done) {
5104 /* use wait func handler, so it matches the rq type */
5105 poll->wait.func(&poll->wait, mode, sync, key);
5106 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005107 }
5108 refcount_dec(&req->refs);
5109 return 1;
5110}
5111
5112static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5113 wait_queue_func_t wake_func)
5114{
5115 poll->head = NULL;
5116 poll->done = false;
5117 poll->canceled = false;
5118 poll->events = events;
5119 INIT_LIST_HEAD(&poll->wait.entry);
5120 init_waitqueue_func_entry(&poll->wait, wake_func);
5121}
5122
5123static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005124 struct wait_queue_head *head,
5125 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005126{
5127 struct io_kiocb *req = pt->req;
5128
5129 /*
5130 * If poll->head is already set, it's because the file being polled
5131 * uses multiple waitqueues for poll handling (eg one for read, one
5132 * for write). Setup a separate io_poll_iocb if this happens.
5133 */
5134 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005135 struct io_poll_iocb *poll_one = poll;
5136
Jens Axboe18bceab2020-05-15 11:56:54 -06005137 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005138 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005139 pt->error = -EINVAL;
5140 return;
5141 }
5142 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5143 if (!poll) {
5144 pt->error = -ENOMEM;
5145 return;
5146 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005147 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005148 refcount_inc(&req->refs);
5149 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005150 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005151 }
5152
5153 pt->error = 0;
5154 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005155
5156 if (poll->events & EPOLLEXCLUSIVE)
5157 add_wait_queue_exclusive(head, &poll->wait);
5158 else
5159 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005160}
5161
5162static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5163 struct poll_table_struct *p)
5164{
5165 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005166 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005167
Jens Axboe807abcb2020-07-17 17:09:27 -06005168 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005169}
5170
Jens Axboed7718a92020-02-14 22:23:12 -07005171static void io_async_task_func(struct callback_head *cb)
5172{
5173 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5174 struct async_poll *apoll = req->apoll;
5175 struct io_ring_ctx *ctx = req->ctx;
5176
5177 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5178
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005179 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005180 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005181 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005182 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005183 }
5184
Jens Axboe31067252020-05-17 17:43:31 -06005185 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005186 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005187 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005188
Jens Axboed4e7cd32020-08-15 11:44:50 -07005189 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005190 spin_unlock_irq(&ctx->completion_lock);
5191
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005192 if (!READ_ONCE(apoll->poll.canceled))
5193 __io_req_task_submit(req);
5194 else
5195 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005196
Jens Axboe6d816e02020-08-11 08:04:14 -06005197 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005198 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005199 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005200}
5201
5202static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5203 void *key)
5204{
5205 struct io_kiocb *req = wait->private;
5206 struct io_poll_iocb *poll = &req->apoll->poll;
5207
5208 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5209 key_to_poll(key));
5210
5211 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5212}
5213
5214static void io_poll_req_insert(struct io_kiocb *req)
5215{
5216 struct io_ring_ctx *ctx = req->ctx;
5217 struct hlist_head *list;
5218
5219 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5220 hlist_add_head(&req->hash_node, list);
5221}
5222
5223static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5224 struct io_poll_iocb *poll,
5225 struct io_poll_table *ipt, __poll_t mask,
5226 wait_queue_func_t wake_func)
5227 __acquires(&ctx->completion_lock)
5228{
5229 struct io_ring_ctx *ctx = req->ctx;
5230 bool cancel = false;
5231
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005232 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005233 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005234 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005235 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005236
5237 ipt->pt._key = mask;
5238 ipt->req = req;
5239 ipt->error = -EINVAL;
5240
Jens Axboed7718a92020-02-14 22:23:12 -07005241 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5242
5243 spin_lock_irq(&ctx->completion_lock);
5244 if (likely(poll->head)) {
5245 spin_lock(&poll->head->lock);
5246 if (unlikely(list_empty(&poll->wait.entry))) {
5247 if (ipt->error)
5248 cancel = true;
5249 ipt->error = 0;
5250 mask = 0;
5251 }
5252 if (mask || ipt->error)
5253 list_del_init(&poll->wait.entry);
5254 else if (cancel)
5255 WRITE_ONCE(poll->canceled, true);
5256 else if (!poll->done) /* actually waiting for an event */
5257 io_poll_req_insert(req);
5258 spin_unlock(&poll->head->lock);
5259 }
5260
5261 return mask;
5262}
5263
5264static bool io_arm_poll_handler(struct io_kiocb *req)
5265{
5266 const struct io_op_def *def = &io_op_defs[req->opcode];
5267 struct io_ring_ctx *ctx = req->ctx;
5268 struct async_poll *apoll;
5269 struct io_poll_table ipt;
5270 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005271 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005272
5273 if (!req->file || !file_can_poll(req->file))
5274 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005275 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005276 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005277 if (def->pollin)
5278 rw = READ;
5279 else if (def->pollout)
5280 rw = WRITE;
5281 else
5282 return false;
5283 /* if we can't nonblock try, then no point in arming a poll handler */
5284 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005285 return false;
5286
5287 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5288 if (unlikely(!apoll))
5289 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005290 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005291
5292 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005293 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005294
Nathan Chancellor8755d972020-03-02 16:01:19 -07005295 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005296 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005297 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005298 if (def->pollout)
5299 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005300
5301 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5302 if ((req->opcode == IORING_OP_RECVMSG) &&
5303 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5304 mask &= ~POLLIN;
5305
Jens Axboed7718a92020-02-14 22:23:12 -07005306 mask |= POLLERR | POLLPRI;
5307
5308 ipt.pt._qproc = io_async_queue_proc;
5309
5310 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5311 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005312 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005313 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005314 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005315 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005316 kfree(apoll);
5317 return false;
5318 }
5319 spin_unlock_irq(&ctx->completion_lock);
5320 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5321 apoll->poll.events);
5322 return true;
5323}
5324
5325static bool __io_poll_remove_one(struct io_kiocb *req,
5326 struct io_poll_iocb *poll)
5327{
Jens Axboeb41e9852020-02-17 09:52:41 -07005328 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005329
5330 spin_lock(&poll->head->lock);
5331 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005332 if (!list_empty(&poll->wait.entry)) {
5333 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005334 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005335 }
5336 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005337 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005338 return do_complete;
5339}
5340
5341static bool io_poll_remove_one(struct io_kiocb *req)
5342{
5343 bool do_complete;
5344
Jens Axboed4e7cd32020-08-15 11:44:50 -07005345 io_poll_remove_double(req);
5346
Jens Axboed7718a92020-02-14 22:23:12 -07005347 if (req->opcode == IORING_OP_POLL_ADD) {
5348 do_complete = __io_poll_remove_one(req, &req->poll);
5349 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005350 struct async_poll *apoll = req->apoll;
5351
Jens Axboed7718a92020-02-14 22:23:12 -07005352 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005353 do_complete = __io_poll_remove_one(req, &apoll->poll);
5354 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005355 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005356 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005357 kfree(apoll);
5358 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005359 }
5360
Jens Axboeb41e9852020-02-17 09:52:41 -07005361 if (do_complete) {
5362 io_cqring_fill_event(req, -ECANCELED);
5363 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005364 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005365 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005366 }
5367
5368 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005369}
5370
Jens Axboe76e1b642020-09-26 15:05:03 -06005371/*
5372 * Returns true if we found and killed one or more poll requests
5373 */
5374static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005375{
Jens Axboe78076bb2019-12-04 19:56:40 -07005376 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005377 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005378 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005379
5380 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005381 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5382 struct hlist_head *list;
5383
5384 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005385 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5386 if (io_task_match(req, tsk))
5387 posted += io_poll_remove_one(req);
5388 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005389 }
5390 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005391
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005392 if (posted)
5393 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005394
5395 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005396}
5397
Jens Axboe47f46762019-11-09 17:43:02 -07005398static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5399{
Jens Axboe78076bb2019-12-04 19:56:40 -07005400 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005401 struct io_kiocb *req;
5402
Jens Axboe78076bb2019-12-04 19:56:40 -07005403 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5404 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005405 if (sqe_addr != req->user_data)
5406 continue;
5407 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005408 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005409 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005410 }
5411
5412 return -ENOENT;
5413}
5414
Jens Axboe3529d8c2019-12-19 18:24:38 -07005415static int io_poll_remove_prep(struct io_kiocb *req,
5416 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005417{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005418 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5419 return -EINVAL;
5420 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5421 sqe->poll_events)
5422 return -EINVAL;
5423
Jens Axboe0969e782019-12-17 18:40:57 -07005424 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005425 return 0;
5426}
5427
5428/*
5429 * Find a running poll command that matches one specified in sqe->addr,
5430 * and remove it if found.
5431 */
5432static int io_poll_remove(struct io_kiocb *req)
5433{
5434 struct io_ring_ctx *ctx = req->ctx;
5435 u64 addr;
5436 int ret;
5437
Jens Axboe0969e782019-12-17 18:40:57 -07005438 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005439 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005440 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005441 spin_unlock_irq(&ctx->completion_lock);
5442
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005443 if (ret < 0)
5444 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005445 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005446 return 0;
5447}
5448
Jens Axboe221c5eb2019-01-17 09:41:58 -07005449static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5450 void *key)
5451{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005452 struct io_kiocb *req = wait->private;
5453 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005454
Jens Axboed7718a92020-02-14 22:23:12 -07005455 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005456}
5457
Jens Axboe221c5eb2019-01-17 09:41:58 -07005458static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5459 struct poll_table_struct *p)
5460{
5461 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5462
Jens Axboee8c2bc12020-08-15 18:44:09 -07005463 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005464}
5465
Jens Axboe3529d8c2019-12-19 18:24:38 -07005466static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005467{
5468 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005469 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005470
5471 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5472 return -EINVAL;
5473 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5474 return -EINVAL;
5475
Jiufei Xue5769a352020-06-17 17:53:55 +08005476 events = READ_ONCE(sqe->poll32_events);
5477#ifdef __BIG_ENDIAN
5478 events = swahw32(events);
5479#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005480 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5481 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005482 return 0;
5483}
5484
Pavel Begunkov014db002020-03-03 21:33:12 +03005485static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005486{
5487 struct io_poll_iocb *poll = &req->poll;
5488 struct io_ring_ctx *ctx = req->ctx;
5489 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005490 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005491
Jens Axboed7718a92020-02-14 22:23:12 -07005492 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005493
Jens Axboed7718a92020-02-14 22:23:12 -07005494 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5495 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005496
Jens Axboe8c838782019-03-12 15:48:16 -06005497 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005498 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005499 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005500 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005501 spin_unlock_irq(&ctx->completion_lock);
5502
Jens Axboe8c838782019-03-12 15:48:16 -06005503 if (mask) {
5504 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005505 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005506 }
Jens Axboe8c838782019-03-12 15:48:16 -06005507 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005508}
5509
Jens Axboe5262f562019-09-17 12:26:57 -06005510static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5511{
Jens Axboead8a48a2019-11-15 08:49:11 -07005512 struct io_timeout_data *data = container_of(timer,
5513 struct io_timeout_data, timer);
5514 struct io_kiocb *req = data->req;
5515 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005516 unsigned long flags;
5517
Jens Axboe5262f562019-09-17 12:26:57 -06005518 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005519 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005520 atomic_set(&req->ctx->cq_timeouts,
5521 atomic_read(&req->ctx->cq_timeouts) + 1);
5522
Jens Axboe78e19bb2019-11-06 15:21:34 -07005523 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005524 io_commit_cqring(ctx);
5525 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5526
5527 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005528 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005529 io_put_req(req);
5530 return HRTIMER_NORESTART;
5531}
5532
Jens Axboef254ac02020-08-12 17:33:30 -06005533static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005534{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005535 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005536 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005537
Jens Axboee8c2bc12020-08-15 18:44:09 -07005538 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005539 if (ret == -1)
5540 return -EALREADY;
Pavel Begunkova71976f2020-10-10 18:34:11 +01005541 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005542
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005543 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005544 io_cqring_fill_event(req, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005545 io_put_req_deferred(req, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07005546 return 0;
5547}
5548
Jens Axboef254ac02020-08-12 17:33:30 -06005549static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5550{
5551 struct io_kiocb *req;
5552 int ret = -ENOENT;
5553
5554 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5555 if (user_data == req->user_data) {
5556 ret = 0;
5557 break;
5558 }
5559 }
5560
5561 if (ret == -ENOENT)
5562 return ret;
5563
5564 return __io_timeout_cancel(req);
5565}
5566
Jens Axboe3529d8c2019-12-19 18:24:38 -07005567static int io_timeout_remove_prep(struct io_kiocb *req,
5568 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005569{
Jens Axboeb29472e2019-12-17 18:50:29 -07005570 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5571 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005572 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5573 return -EINVAL;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005574 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags)
Jens Axboeb29472e2019-12-17 18:50:29 -07005575 return -EINVAL;
5576
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005577 req->timeout_rem.addr = READ_ONCE(sqe->addr);
Jens Axboeb29472e2019-12-17 18:50:29 -07005578 return 0;
5579}
5580
Jens Axboe11365042019-10-16 09:08:32 -06005581/*
5582 * Remove or update an existing timeout command
5583 */
Jens Axboefc4df992019-12-10 14:38:45 -07005584static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005585{
5586 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005587 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005588
Jens Axboe11365042019-10-16 09:08:32 -06005589 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005590 ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005591
Jens Axboe47f46762019-11-09 17:43:02 -07005592 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005593 io_commit_cqring(ctx);
5594 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005595 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005596 if (ret < 0)
5597 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005598 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005599 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005600}
5601
Jens Axboe3529d8c2019-12-19 18:24:38 -07005602static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005603 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005604{
Jens Axboead8a48a2019-11-15 08:49:11 -07005605 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005606 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005607 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005608
Jens Axboead8a48a2019-11-15 08:49:11 -07005609 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005610 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005611 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005612 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005613 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005614 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005615 flags = READ_ONCE(sqe->timeout_flags);
5616 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005617 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005618
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005619 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005620
Jens Axboee8c2bc12020-08-15 18:44:09 -07005621 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005622 return -ENOMEM;
5623
Jens Axboee8c2bc12020-08-15 18:44:09 -07005624 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005625 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005626
5627 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005628 return -EFAULT;
5629
Jens Axboe11365042019-10-16 09:08:32 -06005630 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005631 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005632 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005633 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005634
Jens Axboead8a48a2019-11-15 08:49:11 -07005635 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5636 return 0;
5637}
5638
Jens Axboefc4df992019-12-10 14:38:45 -07005639static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005640{
Jens Axboead8a48a2019-11-15 08:49:11 -07005641 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005642 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005643 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005644 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005645
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005646 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005647
Jens Axboe5262f562019-09-17 12:26:57 -06005648 /*
5649 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005650 * timeout event to be satisfied. If it isn't set, then this is
5651 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005652 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005653 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005654 entry = ctx->timeout_list.prev;
5655 goto add;
5656 }
Jens Axboe5262f562019-09-17 12:26:57 -06005657
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005658 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5659 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005660
5661 /*
5662 * Insertion sort, ensuring the first entry in the list is always
5663 * the one we need first.
5664 */
Jens Axboe5262f562019-09-17 12:26:57 -06005665 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005666 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5667 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005668
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005669 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005670 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005671 /* nxt.seq is behind @tail, otherwise would've been completed */
5672 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005673 break;
5674 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005675add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005676 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005677 data->timer.function = io_timeout_fn;
5678 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005679 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005680 return 0;
5681}
5682
Jens Axboe62755e32019-10-28 21:49:21 -06005683static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005684{
Jens Axboe62755e32019-10-28 21:49:21 -06005685 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005686
Jens Axboe62755e32019-10-28 21:49:21 -06005687 return req->user_data == (unsigned long) data;
5688}
5689
Jens Axboee977d6d2019-11-05 12:39:45 -07005690static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005691{
Jens Axboe62755e32019-10-28 21:49:21 -06005692 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005693 int ret = 0;
5694
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005695 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005696 switch (cancel_ret) {
5697 case IO_WQ_CANCEL_OK:
5698 ret = 0;
5699 break;
5700 case IO_WQ_CANCEL_RUNNING:
5701 ret = -EALREADY;
5702 break;
5703 case IO_WQ_CANCEL_NOTFOUND:
5704 ret = -ENOENT;
5705 break;
5706 }
5707
Jens Axboee977d6d2019-11-05 12:39:45 -07005708 return ret;
5709}
5710
Jens Axboe47f46762019-11-09 17:43:02 -07005711static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5712 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005713 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005714{
5715 unsigned long flags;
5716 int ret;
5717
5718 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5719 if (ret != -ENOENT) {
5720 spin_lock_irqsave(&ctx->completion_lock, flags);
5721 goto done;
5722 }
5723
5724 spin_lock_irqsave(&ctx->completion_lock, flags);
5725 ret = io_timeout_cancel(ctx, sqe_addr);
5726 if (ret != -ENOENT)
5727 goto done;
5728 ret = io_poll_cancel(ctx, sqe_addr);
5729done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005730 if (!ret)
5731 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005732 io_cqring_fill_event(req, ret);
5733 io_commit_cqring(ctx);
5734 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5735 io_cqring_ev_posted(ctx);
5736
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005737 if (ret < 0)
5738 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005739 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005740}
5741
Jens Axboe3529d8c2019-12-19 18:24:38 -07005742static int io_async_cancel_prep(struct io_kiocb *req,
5743 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005744{
Jens Axboefbf23842019-12-17 18:45:56 -07005745 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005746 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005747 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5748 return -EINVAL;
5749 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005750 return -EINVAL;
5751
Jens Axboefbf23842019-12-17 18:45:56 -07005752 req->cancel.addr = READ_ONCE(sqe->addr);
5753 return 0;
5754}
5755
Pavel Begunkov014db002020-03-03 21:33:12 +03005756static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005757{
5758 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005759
Pavel Begunkov014db002020-03-03 21:33:12 +03005760 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005761 return 0;
5762}
5763
Jens Axboe05f3fb32019-12-09 11:22:50 -07005764static int io_files_update_prep(struct io_kiocb *req,
5765 const struct io_uring_sqe *sqe)
5766{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005767 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5768 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005769 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5770 return -EINVAL;
5771 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005772 return -EINVAL;
5773
5774 req->files_update.offset = READ_ONCE(sqe->off);
5775 req->files_update.nr_args = READ_ONCE(sqe->len);
5776 if (!req->files_update.nr_args)
5777 return -EINVAL;
5778 req->files_update.arg = READ_ONCE(sqe->addr);
5779 return 0;
5780}
5781
Jens Axboe229a7b62020-06-22 10:13:11 -06005782static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5783 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005784{
5785 struct io_ring_ctx *ctx = req->ctx;
5786 struct io_uring_files_update up;
5787 int ret;
5788
Jens Axboef86cd202020-01-29 13:46:44 -07005789 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005790 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005791
5792 up.offset = req->files_update.offset;
5793 up.fds = req->files_update.arg;
5794
5795 mutex_lock(&ctx->uring_lock);
5796 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5797 mutex_unlock(&ctx->uring_lock);
5798
5799 if (ret < 0)
5800 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005801 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005802 return 0;
5803}
5804
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005805static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005806{
Jens Axboed625c6e2019-12-17 19:53:05 -07005807 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005808 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005809 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005810 case IORING_OP_READV:
5811 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005812 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005813 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005814 case IORING_OP_WRITEV:
5815 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005816 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005817 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005818 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005819 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005820 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005821 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005822 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005823 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005824 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005825 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005826 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005827 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005828 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005829 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005830 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005831 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005832 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005833 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005834 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005835 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005836 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005837 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005838 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005839 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005840 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005841 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005842 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005843 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005844 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005845 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005846 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005847 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005848 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005849 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005850 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005851 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005852 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005853 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005854 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005855 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005856 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005857 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005858 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005859 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005860 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005861 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005862 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005863 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005864 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005865 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005866 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005867 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005868 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005869 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06005870 case IORING_OP_SHUTDOWN:
5871 return io_shutdown_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005872 }
5873
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005874 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5875 req->opcode);
5876 return-EINVAL;
5877}
5878
Jens Axboedef596e2019-01-09 08:59:42 -07005879static int io_req_defer_prep(struct io_kiocb *req,
5880 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07005881{
Jens Axboedef596e2019-01-09 08:59:42 -07005882 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005883 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005884 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005885 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005886 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005887}
5888
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005889static u32 io_get_sequence(struct io_kiocb *req)
5890{
5891 struct io_kiocb *pos;
5892 struct io_ring_ctx *ctx = req->ctx;
5893 u32 total_submitted, nr_reqs = 1;
5894
5895 if (req->flags & REQ_F_LINK_HEAD)
5896 list_for_each_entry(pos, &req->link_list, link_list)
5897 nr_reqs++;
5898
5899 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5900 return total_submitted - nr_reqs;
5901}
5902
Jens Axboe3529d8c2019-12-19 18:24:38 -07005903static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005904{
5905 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005906 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005907 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005908 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005909
5910 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005911 if (likely(list_empty_careful(&ctx->defer_list) &&
5912 !(req->flags & REQ_F_IO_DRAIN)))
5913 return 0;
5914
5915 seq = io_get_sequence(req);
5916 /* Still a chance to pass the sequence check */
5917 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005918 return 0;
5919
Jens Axboee8c2bc12020-08-15 18:44:09 -07005920 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005921 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005922 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005923 return ret;
5924 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005925 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005926 de = kmalloc(sizeof(*de), GFP_KERNEL);
5927 if (!de)
5928 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005929
5930 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005931 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005932 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005933 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005934 io_queue_async_work(req);
5935 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005936 }
5937
5938 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005939 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005940 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005941 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005942 spin_unlock_irq(&ctx->completion_lock);
5943 return -EIOCBQUEUED;
5944}
Jens Axboeedafcce2019-01-09 09:16:05 -07005945
Jens Axboef573d382020-09-22 10:19:24 -06005946static void io_req_drop_files(struct io_kiocb *req)
5947{
5948 struct io_ring_ctx *ctx = req->ctx;
5949 unsigned long flags;
5950
5951 spin_lock_irqsave(&ctx->inflight_lock, flags);
5952 list_del(&req->inflight_entry);
5953 if (waitqueue_active(&ctx->inflight_wait))
5954 wake_up(&ctx->inflight_wait);
5955 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5956 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe98447d62020-10-14 10:48:51 -06005957 put_files_struct(req->work.identity->files);
5958 put_nsproxy(req->work.identity->nsproxy);
Jens Axboedfead8a2020-10-14 10:12:37 -06005959 req->work.flags &= ~IO_WQ_WORK_FILES;
Jens Axboef573d382020-09-22 10:19:24 -06005960}
5961
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005962static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005963{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005964 if (req->flags & REQ_F_BUFFER_SELECTED) {
5965 switch (req->opcode) {
5966 case IORING_OP_READV:
5967 case IORING_OP_READ_FIXED:
5968 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005969 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005970 break;
5971 case IORING_OP_RECVMSG:
5972 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005973 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005974 break;
5975 }
5976 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005977 }
5978
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005979 if (req->flags & REQ_F_NEED_CLEANUP) {
5980 switch (req->opcode) {
5981 case IORING_OP_READV:
5982 case IORING_OP_READ_FIXED:
5983 case IORING_OP_READ:
5984 case IORING_OP_WRITEV:
5985 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005986 case IORING_OP_WRITE: {
5987 struct io_async_rw *io = req->async_data;
5988 if (io->free_iovec)
5989 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005990 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005991 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005992 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005993 case IORING_OP_SENDMSG: {
5994 struct io_async_msghdr *io = req->async_data;
5995 if (io->iov != io->fast_iov)
5996 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005997 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005998 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005999 case IORING_OP_SPLICE:
6000 case IORING_OP_TEE:
6001 io_put_file(req, req->splice.file_in,
6002 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6003 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006004 case IORING_OP_OPENAT:
6005 case IORING_OP_OPENAT2:
6006 if (req->open.filename)
6007 putname(req->open.filename);
6008 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006009 }
6010 req->flags &= ~REQ_F_NEED_CLEANUP;
6011 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03006012
Jens Axboef573d382020-09-22 10:19:24 -06006013 if (req->flags & REQ_F_INFLIGHT)
6014 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006015}
6016
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006017static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
6018 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006019{
Jens Axboeedafcce2019-01-09 09:16:05 -07006020 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006021 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006022
Jens Axboed625c6e2019-12-17 19:53:05 -07006023 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006024 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06006025 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006026 break;
6027 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006028 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006029 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06006030 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031 break;
6032 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006033 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006034 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06006035 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006036 break;
6037 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03006038 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006039 break;
6040 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03006041 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006042 break;
6043 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07006044 ret = io_poll_remove(req);
6045 break;
6046 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006047 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07006048 break;
6049 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006050 ret = io_sendmsg(req, force_nonblock, cs);
6051 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006052 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006053 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006054 break;
6055 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006056 ret = io_recvmsg(req, force_nonblock, cs);
6057 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006058 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006059 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006060 break;
6061 case IORING_OP_TIMEOUT:
6062 ret = io_timeout(req);
6063 break;
6064 case IORING_OP_TIMEOUT_REMOVE:
6065 ret = io_timeout_remove(req);
6066 break;
6067 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006068 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006069 break;
6070 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006071 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006072 break;
6073 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006074 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006075 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006076 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006077 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006078 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006079 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006080 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006081 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006082 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006083 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006084 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006085 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006086 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006087 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006088 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006089 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006090 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006091 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006092 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006093 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006094 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006095 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006096 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006097 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006098 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006099 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006100 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006101 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006102 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006103 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006104 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006105 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006106 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006107 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006108 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006109 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006110 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006111 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006112 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006113 ret = io_tee(req, force_nonblock);
6114 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006115 case IORING_OP_SHUTDOWN:
6116 ret = io_shutdown(req, force_nonblock);
6117 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006118 default:
6119 ret = -EINVAL;
6120 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006121 }
6122
6123 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006124 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006125
Jens Axboeb5325762020-05-19 21:20:27 -06006126 /* If the op doesn't have a file, we're not polling for it */
6127 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006128 const bool in_async = io_wq_current_is_worker();
6129
Jens Axboe11ba8202020-01-15 21:51:17 -07006130 /* workqueue context doesn't hold uring_lock, grab it now */
6131 if (in_async)
6132 mutex_lock(&ctx->uring_lock);
6133
Jens Axboe2b188cc2019-01-07 10:46:33 -07006134 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006135
6136 if (in_async)
6137 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006138 }
6139
6140 return 0;
6141}
6142
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006143static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006144{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006145 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006146 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006147 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006148
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006149 timeout = io_prep_linked_timeout(req);
6150 if (timeout)
6151 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006152
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006153 /* if NO_CANCEL is set, we must still run the work */
6154 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6155 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006156 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006157 }
Jens Axboe31b51512019-01-18 22:56:34 -07006158
Jens Axboe561fb042019-10-24 07:25:42 -06006159 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006160 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006161 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006162 /*
6163 * We can get EAGAIN for polled IO even though we're
6164 * forcing a sync submission from here, since we can't
6165 * wait for request slots on the block side.
6166 */
6167 if (ret != -EAGAIN)
6168 break;
6169 cond_resched();
6170 } while (1);
6171 }
Jens Axboe31b51512019-01-18 22:56:34 -07006172
Jens Axboe561fb042019-10-24 07:25:42 -06006173 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006174 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006175 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006176 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006177
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006178 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006179}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180
Jens Axboe65e19f52019-10-26 07:20:21 -06006181static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6182 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006183{
Jens Axboe65e19f52019-10-26 07:20:21 -06006184 struct fixed_file_table *table;
6185
Jens Axboe05f3fb32019-12-09 11:22:50 -07006186 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006187 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006188}
6189
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006190static struct file *io_file_get(struct io_submit_state *state,
6191 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006192{
6193 struct io_ring_ctx *ctx = req->ctx;
6194 struct file *file;
6195
6196 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006197 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006198 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006199 fd = array_index_nospec(fd, ctx->nr_user_files);
6200 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006201 if (file) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01006202 req->fixed_file_refs = &ctx->file_data->node->refs;
Jens Axboefd2206e2020-06-02 16:40:47 -06006203 percpu_ref_get(req->fixed_file_refs);
6204 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006205 } else {
6206 trace_io_uring_file_get(ctx, fd);
6207 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006208 }
6209
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006210 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006211}
6212
Jens Axboe3529d8c2019-12-19 18:24:38 -07006213static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006214 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006215{
Jens Axboe28cea78a2020-09-14 10:51:17 -06006216 req->file = io_file_get(state, req, fd, req->flags & REQ_F_FIXED_FILE);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006217 if (req->file || io_op_defs[req->opcode].needs_file_no_error)
Jens Axboef86cd202020-01-29 13:46:44 -07006218 return 0;
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006219 return -EBADF;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006220}
6221
Jens Axboe2665abf2019-11-05 12:40:47 -07006222static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6223{
Jens Axboead8a48a2019-11-15 08:49:11 -07006224 struct io_timeout_data *data = container_of(timer,
6225 struct io_timeout_data, timer);
6226 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006227 struct io_ring_ctx *ctx = req->ctx;
6228 struct io_kiocb *prev = NULL;
6229 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006230
6231 spin_lock_irqsave(&ctx->completion_lock, flags);
6232
6233 /*
6234 * We don't expect the list to be empty, that will only happen if we
6235 * race with the completion of the linked work.
6236 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006237 if (!list_empty(&req->link_list)) {
6238 prev = list_entry(req->link_list.prev, struct io_kiocb,
6239 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006240 if (refcount_inc_not_zero(&prev->refs))
Pavel Begunkov44932332019-12-05 16:16:35 +03006241 list_del_init(&req->link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006242 else
Jens Axboe76a46e02019-11-10 23:34:16 -07006243 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006244 }
6245
6246 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6247
6248 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006249 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006250 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006251 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006252 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006253 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006254 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006255 return HRTIMER_NORESTART;
6256}
6257
Jens Axboe7271ef32020-08-10 09:55:22 -06006258static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006259{
Jens Axboe76a46e02019-11-10 23:34:16 -07006260 /*
6261 * If the list is now empty, then our linked request finished before
6262 * we got a chance to setup the timer
6263 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006264 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006265 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006266
Jens Axboead8a48a2019-11-15 08:49:11 -07006267 data->timer.function = io_link_timeout_fn;
6268 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6269 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006270 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006271}
6272
6273static void io_queue_linked_timeout(struct io_kiocb *req)
6274{
6275 struct io_ring_ctx *ctx = req->ctx;
6276
6277 spin_lock_irq(&ctx->completion_lock);
6278 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006279 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006280
Jens Axboe2665abf2019-11-05 12:40:47 -07006281 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006282 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006283}
6284
Jens Axboead8a48a2019-11-15 08:49:11 -07006285static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006286{
6287 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006288
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006289 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006290 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006291 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006292 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006293
Pavel Begunkov44932332019-12-05 16:16:35 +03006294 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6295 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006296 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006297 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006298
Pavel Begunkov900fad42020-10-19 16:39:16 +01006299 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006300 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006301 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006302}
6303
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006304static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006306 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006307 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006308 int ret;
6309
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006310again:
6311 linked_timeout = io_prep_linked_timeout(req);
6312
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006313 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6314 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006315 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006316 if (old_creds)
6317 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006318 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006319 old_creds = NULL; /* restored original creds */
6320 else
Jens Axboe98447d62020-10-14 10:48:51 -06006321 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006322 }
6323
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006324 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006325
6326 /*
6327 * We async punt it if the file wasn't marked NOWAIT, or if the file
6328 * doesn't support non-blocking read/write attempts
6329 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006330 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006331 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006332 /*
6333 * Queued up for async execution, worker will release
6334 * submit reference when the iocb is actually submitted.
6335 */
6336 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006337 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006338
Pavel Begunkovf063c542020-07-25 14:41:59 +03006339 if (linked_timeout)
6340 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006341 } else if (likely(!ret)) {
6342 /* drop submission reference */
6343 req = io_put_req_find_next(req);
6344 if (linked_timeout)
6345 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006346
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006347 if (req) {
6348 if (!(req->flags & REQ_F_FORCE_ASYNC))
6349 goto again;
6350 io_queue_async_work(req);
6351 }
6352 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006353 /* un-prep timeout, so it'll be killed as any other linked */
6354 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006355 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006356 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006357 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006358 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006359
Jens Axboe193155c2020-02-22 23:22:19 -07006360 if (old_creds)
6361 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006362}
6363
Jens Axboef13fad72020-06-22 09:34:30 -06006364static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6365 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006366{
6367 int ret;
6368
Jens Axboe3529d8c2019-12-19 18:24:38 -07006369 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006370 if (ret) {
6371 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006372fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006373 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006374 io_put_req(req);
6375 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006376 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006377 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006378 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006379 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006380 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006381 goto fail_req;
6382 }
Jens Axboece35a472019-12-17 08:04:44 -07006383 io_queue_async_work(req);
6384 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006385 if (sqe) {
6386 ret = io_req_prep(req, sqe);
6387 if (unlikely(ret))
6388 goto fail_req;
6389 }
6390 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006391 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006392}
6393
Jens Axboef13fad72020-06-22 09:34:30 -06006394static inline void io_queue_link_head(struct io_kiocb *req,
6395 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006396{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006397 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006398 io_put_req(req);
6399 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006400 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006401 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006402}
6403
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006404static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006405 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006406{
Jackie Liua197f662019-11-08 08:09:12 -07006407 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006408 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006409
Jens Axboe9e645e112019-05-10 16:07:28 -06006410 /*
6411 * If we already have a head request, queue this one for async
6412 * submittal once the head completes. If we don't have a head but
6413 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6414 * submitted sync once the chain is complete. If none of those
6415 * conditions are true (normal request), then just queue it.
6416 */
6417 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006418 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006419
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006420 /*
6421 * Taking sequential execution of a link, draining both sides
6422 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6423 * requests in the link. So, it drains the head and the
6424 * next after the link request. The last one is done via
6425 * drain_next flag to persist the effect across calls.
6426 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006427 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006428 head->flags |= REQ_F_IO_DRAIN;
6429 ctx->drain_next = 1;
6430 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006431 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006432 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006433 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006434 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006435 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006436 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006437 trace_io_uring_link(ctx, req, head);
6438 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006439
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006440 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006441 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006442 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006443 *link = NULL;
6444 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006445 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006446 if (unlikely(ctx->drain_next)) {
6447 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006448 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006449 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006450 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006451 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006452 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006453
Pavel Begunkov711be032020-01-17 03:57:59 +03006454 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006455 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006456 req->flags |= REQ_F_FAIL_LINK;
6457 *link = req;
6458 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006459 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006460 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006461 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006462
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006463 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006464}
6465
Jens Axboe9a56a232019-01-09 09:06:50 -07006466/*
6467 * Batched submission is done, ensure local IO is flushed out.
6468 */
6469static void io_submit_state_end(struct io_submit_state *state)
6470{
Jens Axboef13fad72020-06-22 09:34:30 -06006471 if (!list_empty(&state->comp.list))
6472 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006473 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006474 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006475 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006476 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006477}
6478
6479/*
6480 * Start submission side cache.
6481 */
6482static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006483 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006484{
6485 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006486 state->comp.nr = 0;
6487 INIT_LIST_HEAD(&state->comp.list);
6488 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006489 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006490 state->file = NULL;
6491 state->ios_left = max_ios;
6492}
6493
Jens Axboe2b188cc2019-01-07 10:46:33 -07006494static void io_commit_sqring(struct io_ring_ctx *ctx)
6495{
Hristo Venev75b28af2019-08-26 17:23:46 +00006496 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006497
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006498 /*
6499 * Ensure any loads from the SQEs are done at this point,
6500 * since once we write the new head, the application could
6501 * write new data to them.
6502 */
6503 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006504}
6505
6506/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006507 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006508 * that is mapped by userspace. This means that care needs to be taken to
6509 * ensure that reads are stable, as we cannot rely on userspace always
6510 * being a good citizen. If members of the sqe are validated and then later
6511 * used, it's important that those reads are done through READ_ONCE() to
6512 * prevent a re-load down the line.
6513 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006514static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006515{
Hristo Venev75b28af2019-08-26 17:23:46 +00006516 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006517 unsigned head;
6518
6519 /*
6520 * The cached sq head (or cq tail) serves two purposes:
6521 *
6522 * 1) allows us to batch the cost of updating the user visible
6523 * head updates.
6524 * 2) allows the kernel side to track the head on its own, even
6525 * though the application is the one updating it.
6526 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006527 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006528 if (likely(head < ctx->sq_entries))
6529 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006530
6531 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006532 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006533 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006534 return NULL;
6535}
6536
6537static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6538{
6539 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006540}
6541
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006542/*
6543 * Check SQE restrictions (opcode and flags).
6544 *
6545 * Returns 'true' if SQE is allowed, 'false' otherwise.
6546 */
6547static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6548 struct io_kiocb *req,
6549 unsigned int sqe_flags)
6550{
6551 if (!ctx->restricted)
6552 return true;
6553
6554 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6555 return false;
6556
6557 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6558 ctx->restrictions.sqe_flags_required)
6559 return false;
6560
6561 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6562 ctx->restrictions.sqe_flags_required))
6563 return false;
6564
6565 return true;
6566}
6567
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006568#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6569 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6570 IOSQE_BUFFER_SELECT)
6571
6572static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6573 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006574 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006575{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006576 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006577 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006578
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006579 req->opcode = READ_ONCE(sqe->opcode);
6580 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006581 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006582 req->file = NULL;
6583 req->ctx = ctx;
6584 req->flags = 0;
6585 /* one is dropped after submission, the other at completion */
6586 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006587 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006588 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006589
6590 if (unlikely(req->opcode >= IORING_OP_LAST))
6591 return -EINVAL;
6592
Jens Axboe28cea78a2020-09-14 10:51:17 -06006593 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006594 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006595
6596 sqe_flags = READ_ONCE(sqe->flags);
6597 /* enforce forwards compatibility on users */
6598 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6599 return -EINVAL;
6600
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006601 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6602 return -EACCES;
6603
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006604 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6605 !io_op_defs[req->opcode].buffer_select)
6606 return -EOPNOTSUPP;
6607
6608 id = READ_ONCE(sqe->personality);
6609 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006610 struct io_identity *iod;
6611
Jens Axboe1e6fa522020-10-15 08:46:24 -06006612 iod = idr_find(&ctx->personality_idr, id);
6613 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006614 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006615 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006616
6617 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006618 get_cred(iod->creds);
6619 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006620 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006621 }
6622
6623 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006624 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006625
Jens Axboe63ff8222020-05-07 14:56:15 -06006626 if (!io_op_defs[req->opcode].needs_file)
6627 return 0;
6628
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006629 ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
6630 state->ios_left--;
6631 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006632}
6633
Jens Axboe0f212202020-09-13 13:09:39 -06006634static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006635{
Jens Axboeac8691c2020-06-01 08:30:41 -06006636 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006637 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006638 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006639
Jens Axboec4a2ed72019-11-21 21:01:26 -07006640 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006641 if (test_bit(0, &ctx->sq_check_overflow)) {
6642 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006643 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006644 return -EBUSY;
6645 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006646
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006647 /* make sure SQ entry isn't read before tail */
6648 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006649
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006650 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6651 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006652
Jens Axboed8a6df12020-10-15 16:24:45 -06006653 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006654 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006655
Jens Axboe6c271ce2019-01-10 11:22:30 -07006656 io_submit_state_start(&state, ctx, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006657
Jens Axboe6c271ce2019-01-10 11:22:30 -07006658 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006659 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006660 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006661 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006662
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006663 sqe = io_get_sqe(ctx);
6664 if (unlikely(!sqe)) {
6665 io_consume_sqe(ctx);
6666 break;
6667 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006668 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006669 if (unlikely(!req)) {
6670 if (!submitted)
6671 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006672 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006673 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006674 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006675 /* will complete beyond this point, count as submitted */
6676 submitted++;
6677
Pavel Begunkov692d8362020-10-10 18:34:13 +01006678 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006679 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006680fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006681 io_put_req(req);
6682 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006683 break;
6684 }
6685
Jens Axboe354420f2020-01-08 18:55:15 -07006686 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006687 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006688 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006689 if (err)
6690 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006691 }
6692
Pavel Begunkov9466f432020-01-25 22:34:01 +03006693 if (unlikely(submitted != nr)) {
6694 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006695 struct io_uring_task *tctx = current->io_uring;
6696 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006697
Jens Axboed8a6df12020-10-15 16:24:45 -06006698 percpu_ref_put_many(&ctx->refs, unused);
6699 percpu_counter_sub(&tctx->inflight, unused);
6700 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006701 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006702 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006703 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006704 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006705
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006706 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6707 io_commit_sqring(ctx);
6708
Jens Axboe6c271ce2019-01-10 11:22:30 -07006709 return submitted;
6710}
6711
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006712static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6713{
6714 /* Tell userspace we may need a wakeup call */
6715 spin_lock_irq(&ctx->completion_lock);
6716 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6717 spin_unlock_irq(&ctx->completion_lock);
6718}
6719
6720static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6721{
6722 spin_lock_irq(&ctx->completion_lock);
6723 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6724 spin_unlock_irq(&ctx->completion_lock);
6725}
6726
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006727static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6728 int sync, void *key)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006729{
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006730 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6731 int ret;
6732
6733 ret = autoremove_wake_function(wqe, mode, sync, key);
6734 if (ret) {
6735 unsigned long flags;
6736
6737 spin_lock_irqsave(&ctx->completion_lock, flags);
6738 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6739 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6740 }
6741 return ret;
6742}
6743
Jens Axboec8d1ba52020-09-14 11:07:26 -06006744enum sq_ret {
6745 SQT_IDLE = 1,
6746 SQT_SPIN = 2,
6747 SQT_DID_WORK = 4,
6748};
6749
6750static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006751 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006752{
6753 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006754 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006755 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006756 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006757
Jens Axboec8d1ba52020-09-14 11:07:26 -06006758again:
6759 if (!list_empty(&ctx->iopoll_list)) {
6760 unsigned nr_events = 0;
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006761
Jens Axboec8d1ba52020-09-14 11:07:26 -06006762 mutex_lock(&ctx->uring_lock);
6763 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6764 io_do_iopoll(ctx, &nr_events, 0);
6765 mutex_unlock(&ctx->uring_lock);
6766 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006767
Jens Axboec8d1ba52020-09-14 11:07:26 -06006768 to_submit = io_sqring_entries(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006769
Jens Axboec8d1ba52020-09-14 11:07:26 -06006770 /*
6771 * If submit got -EBUSY, flag us as needing the application
6772 * to enter the kernel to reap and flush events.
6773 */
6774 if (!to_submit || ret == -EBUSY || need_resched()) {
6775 /*
6776 * Drop cur_mm before scheduling, we can't hold it for
6777 * long periods (or over schedule()). Do this before
6778 * adding ourselves to the waitqueue, as the unuse/drop
6779 * may sleep.
6780 */
Jens Axboe28cea78a2020-09-14 10:51:17 -06006781 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006782
Jens Axboec8d1ba52020-09-14 11:07:26 -06006783 /*
6784 * We're polling. If we're within the defined idle
6785 * period, then let us spin without work before going
6786 * to sleep. The exception is if we got EBUSY doing
6787 * more IO, we should wait for the application to
6788 * reap events and wake us up.
6789 */
6790 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6791 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6792 !percpu_ref_is_dying(&ctx->refs)))
6793 return SQT_SPIN;
6794
Jens Axboe534ca6d2020-09-02 13:52:19 -06006795 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006796 TASK_INTERRUPTIBLE);
6797
6798 /*
6799 * While doing polled IO, before going to sleep, we need
6800 * to check if there are new reqs added to iopoll_list,
6801 * it is because reqs may have been punted to io worker
6802 * and will be added to iopoll_list later, hence check
6803 * the iopoll_list again.
6804 */
6805 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6806 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006807 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006808 goto again;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006809 }
6810
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006811 to_submit = io_sqring_entries(ctx);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006812 if (!to_submit || ret == -EBUSY)
6813 return SQT_IDLE;
6814 }
6815
Jens Axboe534ca6d2020-09-02 13:52:19 -06006816 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006817 io_ring_clear_wakeup_flag(ctx);
6818
Jens Axboee95eee22020-09-08 09:11:32 -06006819 /* if we're handling multiple rings, cap submit size for fairness */
6820 if (cap_entries && to_submit > 8)
6821 to_submit = 8;
6822
Jens Axboec8d1ba52020-09-14 11:07:26 -06006823 mutex_lock(&ctx->uring_lock);
6824 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6825 ret = io_submit_sqes(ctx, to_submit);
6826 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006827
6828 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6829 wake_up(&ctx->sqo_sq_wait);
6830
Jens Axboec8d1ba52020-09-14 11:07:26 -06006831 return SQT_DID_WORK;
6832}
6833
Jens Axboe69fb2132020-09-14 11:16:23 -06006834static void io_sqd_init_new(struct io_sq_data *sqd)
6835{
6836 struct io_ring_ctx *ctx;
6837
6838 while (!list_empty(&sqd->ctx_new_list)) {
6839 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6840 init_wait(&ctx->sqo_wait_entry);
6841 ctx->sqo_wait_entry.func = io_sq_wake_function;
6842 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6843 complete(&ctx->sq_thread_comp);
6844 }
6845}
6846
Jens Axboe6c271ce2019-01-10 11:22:30 -07006847static int io_sq_thread(void *data)
6848{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006849 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006850 struct files_struct *old_files = current->files;
6851 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006852 const struct cred *old_cred = NULL;
6853 struct io_sq_data *sqd = data;
6854 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006855 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006856
Jens Axboe28cea78a2020-09-14 10:51:17 -06006857 task_lock(current);
6858 current->files = NULL;
6859 current->nsproxy = NULL;
6860 task_unlock(current);
6861
Jens Axboec8d1ba52020-09-14 11:07:26 -06006862 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006863 while (!kthread_should_stop()) {
6864 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006865 bool cap_entries;
Jens Axboec1edbf52019-11-10 16:56:04 -07006866
6867 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006868 * Any changes to the sqd lists are synchronized through the
6869 * kthread parking. This synchronizes the thread vs users,
6870 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006871 */
Jens Axboe69fb2132020-09-14 11:16:23 -06006872 if (kthread_should_park())
6873 kthread_parkme();
6874
6875 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6876 io_sqd_init_new(sqd);
6877
Jens Axboee95eee22020-09-08 09:11:32 -06006878 cap_entries = !list_is_singular(&sqd->ctx_list);
6879
Jens Axboe69fb2132020-09-14 11:16:23 -06006880 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6881 if (current->cred != ctx->creds) {
6882 if (old_cred)
6883 revert_creds(old_cred);
6884 old_cred = override_creds(ctx->creds);
6885 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006886 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06006887#ifdef CONFIG_AUDIT
6888 current->loginuid = ctx->loginuid;
6889 current->sessionid = ctx->sessionid;
6890#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06006891
Jens Axboee95eee22020-09-08 09:11:32 -06006892 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006893
Jens Axboe28cea78a2020-09-14 10:51:17 -06006894 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006895 }
6896
Jens Axboe69fb2132020-09-14 11:16:23 -06006897 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006898 io_run_task_work();
6899 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006900 } else if (ret == SQT_IDLE) {
6901 if (kthread_should_park())
6902 continue;
6903 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6904 io_ring_set_wakeup_flag(ctx);
6905 schedule();
6906 start_jiffies = jiffies;
6907 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6908 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006909 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006910 }
6911
Jens Axboe4c6e2772020-07-01 11:29:10 -06006912 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006913
Dennis Zhou91d8f512020-09-16 13:41:05 -07006914 if (cur_css)
6915 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006916 if (old_cred)
6917 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006918
Jens Axboe28cea78a2020-09-14 10:51:17 -06006919 task_lock(current);
6920 current->files = old_files;
6921 current->nsproxy = old_nsproxy;
6922 task_unlock(current);
6923
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006924 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006925
Jens Axboe6c271ce2019-01-10 11:22:30 -07006926 return 0;
6927}
6928
Jens Axboebda52162019-09-24 13:47:15 -06006929struct io_wait_queue {
6930 struct wait_queue_entry wq;
6931 struct io_ring_ctx *ctx;
6932 unsigned to_wait;
6933 unsigned nr_timeouts;
6934};
6935
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006936static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006937{
6938 struct io_ring_ctx *ctx = iowq->ctx;
6939
6940 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006941 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006942 * started waiting. For timeouts, we always want to return to userspace,
6943 * regardless of event count.
6944 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006945 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006946 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6947}
6948
6949static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6950 int wake_flags, void *key)
6951{
6952 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6953 wq);
6954
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006955 /* use noflush == true, as we can't safely rely on locking context */
6956 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006957 return -1;
6958
6959 return autoremove_wake_function(curr, mode, wake_flags, key);
6960}
6961
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006962static int io_run_task_work_sig(void)
6963{
6964 if (io_run_task_work())
6965 return 1;
6966 if (!signal_pending(current))
6967 return 0;
6968 if (current->jobctl & JOBCTL_TASK_WORK) {
6969 spin_lock_irq(&current->sighand->siglock);
6970 current->jobctl &= ~JOBCTL_TASK_WORK;
6971 recalc_sigpending();
6972 spin_unlock_irq(&current->sighand->siglock);
6973 return 1;
6974 }
6975 return -EINTR;
6976}
6977
Jens Axboe2b188cc2019-01-07 10:46:33 -07006978/*
6979 * Wait until events become available, if we don't already have some. The
6980 * application must reap them itself, as they reside on the shared cq ring.
6981 */
6982static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6983 const sigset_t __user *sig, size_t sigsz)
6984{
Jens Axboebda52162019-09-24 13:47:15 -06006985 struct io_wait_queue iowq = {
6986 .wq = {
6987 .private = current,
6988 .func = io_wake_function,
6989 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6990 },
6991 .ctx = ctx,
6992 .to_wait = min_events,
6993 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006994 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006995 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006996
Jens Axboeb41e9852020-02-17 09:52:41 -07006997 do {
6998 if (io_cqring_events(ctx, false) >= min_events)
6999 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007000 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007001 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007002 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007003
7004 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007005#ifdef CONFIG_COMPAT
7006 if (in_compat_syscall())
7007 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007008 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007009 else
7010#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007011 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007012
Jens Axboe2b188cc2019-01-07 10:46:33 -07007013 if (ret)
7014 return ret;
7015 }
7016
Jens Axboebda52162019-09-24 13:47:15 -06007017 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007018 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007019 do {
7020 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7021 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007022 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007023 ret = io_run_task_work_sig();
7024 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06007025 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007026 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007027 break;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07007028 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06007029 break;
7030 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06007031 } while (1);
7032 finish_wait(&ctx->wait, &iowq.wq);
7033
Jens Axboeb7db41c2020-07-04 08:55:50 -06007034 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007035
Hristo Venev75b28af2019-08-26 17:23:46 +00007036 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007037}
7038
Jens Axboe6b063142019-01-10 22:13:58 -07007039static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7040{
7041#if defined(CONFIG_UNIX)
7042 if (ctx->ring_sock) {
7043 struct sock *sock = ctx->ring_sock->sk;
7044 struct sk_buff *skb;
7045
7046 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7047 kfree_skb(skb);
7048 }
7049#else
7050 int i;
7051
Jens Axboe65e19f52019-10-26 07:20:21 -06007052 for (i = 0; i < ctx->nr_user_files; i++) {
7053 struct file *file;
7054
7055 file = io_file_from_index(ctx, i);
7056 if (file)
7057 fput(file);
7058 }
Jens Axboe6b063142019-01-10 22:13:58 -07007059#endif
7060}
7061
Jens Axboe05f3fb32019-12-09 11:22:50 -07007062static void io_file_ref_kill(struct percpu_ref *ref)
7063{
7064 struct fixed_file_data *data;
7065
7066 data = container_of(ref, struct fixed_file_data, refs);
7067 complete(&data->done);
7068}
7069
Jens Axboe6b063142019-01-10 22:13:58 -07007070static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7071{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007072 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007073 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007074 unsigned nr_tables, i;
7075
Jens Axboe05f3fb32019-12-09 11:22:50 -07007076 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007077 return -ENXIO;
7078
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007079 spin_lock(&data->lock);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007080 ref_node = data->node;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007081 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007082 if (ref_node)
7083 percpu_ref_kill(&ref_node->refs);
7084
7085 percpu_ref_kill(&data->refs);
7086
7087 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007088 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07007089 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007090
Jens Axboe6b063142019-01-10 22:13:58 -07007091 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007092 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7093 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007094 kfree(data->table[i].files);
7095 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007096 percpu_ref_exit(&data->refs);
7097 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007098 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007099 ctx->nr_user_files = 0;
7100 return 0;
7101}
7102
Jens Axboe534ca6d2020-09-02 13:52:19 -06007103static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007104{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007105 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007106 /*
7107 * The park is a bit of a work-around, without it we get
7108 * warning spews on shutdown with SQPOLL set and affinity
7109 * set to a single CPU.
7110 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007111 if (sqd->thread) {
7112 kthread_park(sqd->thread);
7113 kthread_stop(sqd->thread);
7114 }
7115
7116 kfree(sqd);
7117 }
7118}
7119
Jens Axboeaa061652020-09-02 14:50:27 -06007120static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7121{
7122 struct io_ring_ctx *ctx_attach;
7123 struct io_sq_data *sqd;
7124 struct fd f;
7125
7126 f = fdget(p->wq_fd);
7127 if (!f.file)
7128 return ERR_PTR(-ENXIO);
7129 if (f.file->f_op != &io_uring_fops) {
7130 fdput(f);
7131 return ERR_PTR(-EINVAL);
7132 }
7133
7134 ctx_attach = f.file->private_data;
7135 sqd = ctx_attach->sq_data;
7136 if (!sqd) {
7137 fdput(f);
7138 return ERR_PTR(-EINVAL);
7139 }
7140
7141 refcount_inc(&sqd->refs);
7142 fdput(f);
7143 return sqd;
7144}
7145
Jens Axboe534ca6d2020-09-02 13:52:19 -06007146static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7147{
7148 struct io_sq_data *sqd;
7149
Jens Axboeaa061652020-09-02 14:50:27 -06007150 if (p->flags & IORING_SETUP_ATTACH_WQ)
7151 return io_attach_sq_data(p);
7152
Jens Axboe534ca6d2020-09-02 13:52:19 -06007153 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7154 if (!sqd)
7155 return ERR_PTR(-ENOMEM);
7156
7157 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007158 INIT_LIST_HEAD(&sqd->ctx_list);
7159 INIT_LIST_HEAD(&sqd->ctx_new_list);
7160 mutex_init(&sqd->ctx_lock);
7161 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007162 init_waitqueue_head(&sqd->wait);
7163 return sqd;
7164}
7165
Jens Axboe69fb2132020-09-14 11:16:23 -06007166static void io_sq_thread_unpark(struct io_sq_data *sqd)
7167 __releases(&sqd->lock)
7168{
7169 if (!sqd->thread)
7170 return;
7171 kthread_unpark(sqd->thread);
7172 mutex_unlock(&sqd->lock);
7173}
7174
7175static void io_sq_thread_park(struct io_sq_data *sqd)
7176 __acquires(&sqd->lock)
7177{
7178 if (!sqd->thread)
7179 return;
7180 mutex_lock(&sqd->lock);
7181 kthread_park(sqd->thread);
7182}
7183
Jens Axboe534ca6d2020-09-02 13:52:19 -06007184static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7185{
7186 struct io_sq_data *sqd = ctx->sq_data;
7187
7188 if (sqd) {
7189 if (sqd->thread) {
7190 /*
7191 * We may arrive here from the error branch in
7192 * io_sq_offload_create() where the kthread is created
7193 * without being waked up, thus wake it up now to make
7194 * sure the wait will complete.
7195 */
7196 wake_up_process(sqd->thread);
7197 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007198
7199 io_sq_thread_park(sqd);
7200 }
7201
7202 mutex_lock(&sqd->ctx_lock);
7203 list_del(&ctx->sqd_list);
7204 mutex_unlock(&sqd->ctx_lock);
7205
7206 if (sqd->thread) {
7207 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7208 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007209 }
7210
7211 io_put_sq_data(sqd);
7212 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007213 }
7214}
7215
Jens Axboe6b063142019-01-10 22:13:58 -07007216static void io_finish_async(struct io_ring_ctx *ctx)
7217{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007218 io_sq_thread_stop(ctx);
7219
Jens Axboe561fb042019-10-24 07:25:42 -06007220 if (ctx->io_wq) {
7221 io_wq_destroy(ctx->io_wq);
7222 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007223 }
7224}
7225
7226#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007227/*
7228 * Ensure the UNIX gc is aware of our file set, so we are certain that
7229 * the io_uring can be safely unregistered on process exit, even if we have
7230 * loops in the file referencing.
7231 */
7232static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7233{
7234 struct sock *sk = ctx->ring_sock->sk;
7235 struct scm_fp_list *fpl;
7236 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007237 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007238
Jens Axboe6b063142019-01-10 22:13:58 -07007239 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7240 if (!fpl)
7241 return -ENOMEM;
7242
7243 skb = alloc_skb(0, GFP_KERNEL);
7244 if (!skb) {
7245 kfree(fpl);
7246 return -ENOMEM;
7247 }
7248
7249 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007250
Jens Axboe08a45172019-10-03 08:11:03 -06007251 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007252 fpl->user = get_uid(ctx->user);
7253 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007254 struct file *file = io_file_from_index(ctx, i + offset);
7255
7256 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007257 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007258 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007259 unix_inflight(fpl->user, fpl->fp[nr_files]);
7260 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007261 }
7262
Jens Axboe08a45172019-10-03 08:11:03 -06007263 if (nr_files) {
7264 fpl->max = SCM_MAX_FD;
7265 fpl->count = nr_files;
7266 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007267 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007268 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7269 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007270
Jens Axboe08a45172019-10-03 08:11:03 -06007271 for (i = 0; i < nr_files; i++)
7272 fput(fpl->fp[i]);
7273 } else {
7274 kfree_skb(skb);
7275 kfree(fpl);
7276 }
Jens Axboe6b063142019-01-10 22:13:58 -07007277
7278 return 0;
7279}
7280
7281/*
7282 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7283 * causes regular reference counting to break down. We rely on the UNIX
7284 * garbage collection to take care of this problem for us.
7285 */
7286static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7287{
7288 unsigned left, total;
7289 int ret = 0;
7290
7291 total = 0;
7292 left = ctx->nr_user_files;
7293 while (left) {
7294 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007295
7296 ret = __io_sqe_files_scm(ctx, this_files, total);
7297 if (ret)
7298 break;
7299 left -= this_files;
7300 total += this_files;
7301 }
7302
7303 if (!ret)
7304 return 0;
7305
7306 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007307 struct file *file = io_file_from_index(ctx, total);
7308
7309 if (file)
7310 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007311 total++;
7312 }
7313
7314 return ret;
7315}
7316#else
7317static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7318{
7319 return 0;
7320}
7321#endif
7322
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007323static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7324 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007325{
7326 int i;
7327
7328 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007329 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007330 unsigned this_files;
7331
7332 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7333 table->files = kcalloc(this_files, sizeof(struct file *),
7334 GFP_KERNEL);
7335 if (!table->files)
7336 break;
7337 nr_files -= this_files;
7338 }
7339
7340 if (i == nr_tables)
7341 return 0;
7342
7343 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007344 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007345 kfree(table->files);
7346 }
7347 return 1;
7348}
7349
Jens Axboe05f3fb32019-12-09 11:22:50 -07007350static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007351{
7352#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007353 struct sock *sock = ctx->ring_sock->sk;
7354 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7355 struct sk_buff *skb;
7356 int i;
7357
7358 __skb_queue_head_init(&list);
7359
7360 /*
7361 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7362 * remove this entry and rearrange the file array.
7363 */
7364 skb = skb_dequeue(head);
7365 while (skb) {
7366 struct scm_fp_list *fp;
7367
7368 fp = UNIXCB(skb).fp;
7369 for (i = 0; i < fp->count; i++) {
7370 int left;
7371
7372 if (fp->fp[i] != file)
7373 continue;
7374
7375 unix_notinflight(fp->user, fp->fp[i]);
7376 left = fp->count - 1 - i;
7377 if (left) {
7378 memmove(&fp->fp[i], &fp->fp[i + 1],
7379 left * sizeof(struct file *));
7380 }
7381 fp->count--;
7382 if (!fp->count) {
7383 kfree_skb(skb);
7384 skb = NULL;
7385 } else {
7386 __skb_queue_tail(&list, skb);
7387 }
7388 fput(file);
7389 file = NULL;
7390 break;
7391 }
7392
7393 if (!file)
7394 break;
7395
7396 __skb_queue_tail(&list, skb);
7397
7398 skb = skb_dequeue(head);
7399 }
7400
7401 if (skb_peek(&list)) {
7402 spin_lock_irq(&head->lock);
7403 while ((skb = __skb_dequeue(&list)) != NULL)
7404 __skb_queue_tail(head, skb);
7405 spin_unlock_irq(&head->lock);
7406 }
7407#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007408 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007409#endif
7410}
7411
Jens Axboe05f3fb32019-12-09 11:22:50 -07007412struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007413 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007414 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007415};
7416
Jens Axboe4a38aed22020-05-14 17:21:15 -06007417static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007418{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007419 struct fixed_file_data *file_data = ref_node->file_data;
7420 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007421 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007422
7423 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007424 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007425 io_ring_file_put(ctx, pfile->file);
7426 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007427 }
7428
Xiaoguang Wang05589552020-03-31 14:05:18 +08007429 percpu_ref_exit(&ref_node->refs);
7430 kfree(ref_node);
7431 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007432}
7433
Jens Axboe4a38aed22020-05-14 17:21:15 -06007434static void io_file_put_work(struct work_struct *work)
7435{
7436 struct io_ring_ctx *ctx;
7437 struct llist_node *node;
7438
7439 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7440 node = llist_del_all(&ctx->file_put_llist);
7441
7442 while (node) {
7443 struct fixed_file_ref_node *ref_node;
7444 struct llist_node *next = node->next;
7445
7446 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7447 __io_file_put_work(ref_node);
7448 node = next;
7449 }
7450}
7451
Jens Axboe05f3fb32019-12-09 11:22:50 -07007452static void io_file_data_ref_zero(struct percpu_ref *ref)
7453{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007454 struct fixed_file_ref_node *ref_node;
Pavel Begunkove2978222020-11-18 14:56:26 +00007455 struct fixed_file_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007456 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007457 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007458 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007459
Xiaoguang Wang05589552020-03-31 14:05:18 +08007460 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Pavel Begunkove2978222020-11-18 14:56:26 +00007461 data = ref_node->file_data;
7462 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007463
Pavel Begunkove2978222020-11-18 14:56:26 +00007464 spin_lock(&data->lock);
7465 ref_node->done = true;
7466
7467 while (!list_empty(&data->ref_list)) {
7468 ref_node = list_first_entry(&data->ref_list,
7469 struct fixed_file_ref_node, node);
7470 /* recycle ref nodes in order */
7471 if (!ref_node->done)
7472 break;
7473 list_del(&ref_node->node);
7474 first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist);
7475 }
7476 spin_unlock(&data->lock);
7477
7478 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007479 delay = 0;
7480
Jens Axboe4a38aed22020-05-14 17:21:15 -06007481 if (!delay)
7482 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7483 else if (first_add)
7484 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007485}
7486
7487static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7488 struct io_ring_ctx *ctx)
7489{
7490 struct fixed_file_ref_node *ref_node;
7491
7492 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7493 if (!ref_node)
7494 return ERR_PTR(-ENOMEM);
7495
7496 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7497 0, GFP_KERNEL)) {
7498 kfree(ref_node);
7499 return ERR_PTR(-ENOMEM);
7500 }
7501 INIT_LIST_HEAD(&ref_node->node);
7502 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007503 ref_node->file_data = ctx->file_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007504 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007505 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007506}
7507
7508static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7509{
7510 percpu_ref_exit(&ref_node->refs);
7511 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007512}
7513
7514static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7515 unsigned nr_args)
7516{
7517 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007518 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007519 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007520 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007521 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007522 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007523
7524 if (ctx->file_data)
7525 return -EBUSY;
7526 if (!nr_args)
7527 return -EINVAL;
7528 if (nr_args > IORING_MAX_FIXED_FILES)
7529 return -EMFILE;
7530
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007531 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7532 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007533 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007534 file_data->ctx = ctx;
7535 init_completion(&file_data->done);
7536 INIT_LIST_HEAD(&file_data->ref_list);
7537 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007538
7539 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007540 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007541 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007542 if (!file_data->table)
7543 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007544
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007545 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007546 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7547 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007548
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007549 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7550 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007551 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007552
7553 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7554 struct fixed_file_table *table;
7555 unsigned index;
7556
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007557 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7558 ret = -EFAULT;
7559 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007560 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007561 /* allow sparse sets */
7562 if (fd == -1)
7563 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007564
Jens Axboe05f3fb32019-12-09 11:22:50 -07007565 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007566 ret = -EBADF;
7567 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007568 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007569
7570 /*
7571 * Don't allow io_uring instances to be registered. If UNIX
7572 * isn't enabled, then this causes a reference cycle and this
7573 * instance can never get freed. If UNIX is enabled we'll
7574 * handle it just fine, but there's still no point in allowing
7575 * a ring fd as it doesn't support regular read/write anyway.
7576 */
7577 if (file->f_op == &io_uring_fops) {
7578 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007579 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007580 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007581 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7582 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007583 table->files[index] = file;
7584 }
7585
Jens Axboe05f3fb32019-12-09 11:22:50 -07007586 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007587 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007588 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007589 return ret;
7590 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007591
Xiaoguang Wang05589552020-03-31 14:05:18 +08007592 ref_node = alloc_fixed_file_ref_node(ctx);
7593 if (IS_ERR(ref_node)) {
7594 io_sqe_files_unregister(ctx);
7595 return PTR_ERR(ref_node);
7596 }
7597
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007598 file_data->node = ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007599 spin_lock(&file_data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007600 list_add_tail(&ref_node->node, &file_data->ref_list);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007601 spin_unlock(&file_data->lock);
7602 percpu_ref_get(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007603 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007604out_fput:
7605 for (i = 0; i < ctx->nr_user_files; i++) {
7606 file = io_file_from_index(ctx, i);
7607 if (file)
7608 fput(file);
7609 }
7610 for (i = 0; i < nr_tables; i++)
7611 kfree(file_data->table[i].files);
7612 ctx->nr_user_files = 0;
7613out_ref:
7614 percpu_ref_exit(&file_data->refs);
7615out_free:
7616 kfree(file_data->table);
7617 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007618 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007619 return ret;
7620}
7621
Jens Axboec3a31e62019-10-03 13:59:56 -06007622static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7623 int index)
7624{
7625#if defined(CONFIG_UNIX)
7626 struct sock *sock = ctx->ring_sock->sk;
7627 struct sk_buff_head *head = &sock->sk_receive_queue;
7628 struct sk_buff *skb;
7629
7630 /*
7631 * See if we can merge this file into an existing skb SCM_RIGHTS
7632 * file set. If there's no room, fall back to allocating a new skb
7633 * and filling it in.
7634 */
7635 spin_lock_irq(&head->lock);
7636 skb = skb_peek(head);
7637 if (skb) {
7638 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7639
7640 if (fpl->count < SCM_MAX_FD) {
7641 __skb_unlink(skb, head);
7642 spin_unlock_irq(&head->lock);
7643 fpl->fp[fpl->count] = get_file(file);
7644 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7645 fpl->count++;
7646 spin_lock_irq(&head->lock);
7647 __skb_queue_head(head, skb);
7648 } else {
7649 skb = NULL;
7650 }
7651 }
7652 spin_unlock_irq(&head->lock);
7653
7654 if (skb) {
7655 fput(file);
7656 return 0;
7657 }
7658
7659 return __io_sqe_files_scm(ctx, 1, index);
7660#else
7661 return 0;
7662#endif
7663}
7664
Hillf Dantona5318d32020-03-23 17:47:15 +08007665static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007666 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007667{
Hillf Dantona5318d32020-03-23 17:47:15 +08007668 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007669 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007670
Jens Axboe05f3fb32019-12-09 11:22:50 -07007671 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007672 if (!pfile)
7673 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007674
7675 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007676 list_add(&pfile->list, &ref_node->file_list);
7677
Hillf Dantona5318d32020-03-23 17:47:15 +08007678 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007679}
7680
7681static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7682 struct io_uring_files_update *up,
7683 unsigned nr_args)
7684{
7685 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007686 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007687 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007688 __s32 __user *fds;
7689 int fd, i, err;
7690 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007691 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007692
Jens Axboe05f3fb32019-12-09 11:22:50 -07007693 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007694 return -EOVERFLOW;
7695 if (done > ctx->nr_user_files)
7696 return -EINVAL;
7697
Xiaoguang Wang05589552020-03-31 14:05:18 +08007698 ref_node = alloc_fixed_file_ref_node(ctx);
7699 if (IS_ERR(ref_node))
7700 return PTR_ERR(ref_node);
7701
Jens Axboec3a31e62019-10-03 13:59:56 -06007702 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007703 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007704 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007705 struct fixed_file_table *table;
7706 unsigned index;
7707
Jens Axboec3a31e62019-10-03 13:59:56 -06007708 err = 0;
7709 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7710 err = -EFAULT;
7711 break;
7712 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007713 i = array_index_nospec(up->offset, ctx->nr_user_files);
7714 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007715 index = i & IORING_FILE_TABLE_MASK;
7716 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007717 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007718 err = io_queue_file_removal(data, file);
7719 if (err)
7720 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007721 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007722 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007723 }
7724 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007725 file = fget(fd);
7726 if (!file) {
7727 err = -EBADF;
7728 break;
7729 }
7730 /*
7731 * Don't allow io_uring instances to be registered. If
7732 * UNIX isn't enabled, then this causes a reference
7733 * cycle and this instance can never get freed. If UNIX
7734 * is enabled we'll handle it just fine, but there's
7735 * still no point in allowing a ring fd as it doesn't
7736 * support regular read/write anyway.
7737 */
7738 if (file->f_op == &io_uring_fops) {
7739 fput(file);
7740 err = -EBADF;
7741 break;
7742 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007743 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007744 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007745 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007746 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007747 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007748 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007749 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007750 }
7751 nr_args--;
7752 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007753 up->offset++;
7754 }
7755
Xiaoguang Wang05589552020-03-31 14:05:18 +08007756 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007757 percpu_ref_kill(&data->node->refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007758 spin_lock(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007759 list_add_tail(&ref_node->node, &data->ref_list);
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007760 data->node = ref_node;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007761 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007762 percpu_ref_get(&ctx->file_data->refs);
7763 } else
7764 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007765
7766 return done ? done : err;
7767}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007768
Jens Axboe05f3fb32019-12-09 11:22:50 -07007769static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7770 unsigned nr_args)
7771{
7772 struct io_uring_files_update up;
7773
7774 if (!ctx->file_data)
7775 return -ENXIO;
7776 if (!nr_args)
7777 return -EINVAL;
7778 if (copy_from_user(&up, arg, sizeof(up)))
7779 return -EFAULT;
7780 if (up.resv)
7781 return -EINVAL;
7782
7783 return __io_sqe_files_update(ctx, &up, nr_args);
7784}
Jens Axboec3a31e62019-10-03 13:59:56 -06007785
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007786static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007787{
7788 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7789
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007790 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007791 io_put_req(req);
7792}
7793
Pavel Begunkov24369c22020-01-28 03:15:48 +03007794static int io_init_wq_offload(struct io_ring_ctx *ctx,
7795 struct io_uring_params *p)
7796{
7797 struct io_wq_data data;
7798 struct fd f;
7799 struct io_ring_ctx *ctx_attach;
7800 unsigned int concurrency;
7801 int ret = 0;
7802
7803 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007804 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007805 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007806
7807 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7808 /* Do QD, or 4 * CPUS, whatever is smallest */
7809 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7810
7811 ctx->io_wq = io_wq_create(concurrency, &data);
7812 if (IS_ERR(ctx->io_wq)) {
7813 ret = PTR_ERR(ctx->io_wq);
7814 ctx->io_wq = NULL;
7815 }
7816 return ret;
7817 }
7818
7819 f = fdget(p->wq_fd);
7820 if (!f.file)
7821 return -EBADF;
7822
7823 if (f.file->f_op != &io_uring_fops) {
7824 ret = -EINVAL;
7825 goto out_fput;
7826 }
7827
7828 ctx_attach = f.file->private_data;
7829 /* @io_wq is protected by holding the fd */
7830 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7831 ret = -EINVAL;
7832 goto out_fput;
7833 }
7834
7835 ctx->io_wq = ctx_attach->io_wq;
7836out_fput:
7837 fdput(f);
7838 return ret;
7839}
7840
Jens Axboe0f212202020-09-13 13:09:39 -06007841static int io_uring_alloc_task_context(struct task_struct *task)
7842{
7843 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007844 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007845
7846 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7847 if (unlikely(!tctx))
7848 return -ENOMEM;
7849
Jens Axboed8a6df12020-10-15 16:24:45 -06007850 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7851 if (unlikely(ret)) {
7852 kfree(tctx);
7853 return ret;
7854 }
7855
Jens Axboe0f212202020-09-13 13:09:39 -06007856 xa_init(&tctx->xa);
7857 init_waitqueue_head(&tctx->wait);
7858 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007859 atomic_set(&tctx->in_idle, 0);
7860 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06007861 io_init_identity(&tctx->__identity);
7862 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06007863 task->io_uring = tctx;
7864 return 0;
7865}
7866
7867void __io_uring_free(struct task_struct *tsk)
7868{
7869 struct io_uring_task *tctx = tsk->io_uring;
7870
7871 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06007872 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
7873 if (tctx->identity != &tctx->__identity)
7874 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06007875 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007876 kfree(tctx);
7877 tsk->io_uring = NULL;
7878}
7879
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007880static int io_sq_offload_create(struct io_ring_ctx *ctx,
7881 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007882{
7883 int ret;
7884
Jens Axboe6c271ce2019-01-10 11:22:30 -07007885 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007886 struct io_sq_data *sqd;
7887
Jens Axboe3ec482d2019-04-08 10:51:01 -06007888 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06007889 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06007890 goto err;
7891
Jens Axboe534ca6d2020-09-02 13:52:19 -06007892 sqd = io_get_sq_data(p);
7893 if (IS_ERR(sqd)) {
7894 ret = PTR_ERR(sqd);
7895 goto err;
7896 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007897
Jens Axboe534ca6d2020-09-02 13:52:19 -06007898 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007899 io_sq_thread_park(sqd);
7900 mutex_lock(&sqd->ctx_lock);
7901 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7902 mutex_unlock(&sqd->ctx_lock);
7903 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007904
Jens Axboe917257d2019-04-13 09:28:55 -06007905 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7906 if (!ctx->sq_thread_idle)
7907 ctx->sq_thread_idle = HZ;
7908
Jens Axboeaa061652020-09-02 14:50:27 -06007909 if (sqd->thread)
7910 goto done;
7911
Jens Axboe6c271ce2019-01-10 11:22:30 -07007912 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007913 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007914
Jens Axboe917257d2019-04-13 09:28:55 -06007915 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007916 if (cpu >= nr_cpu_ids)
7917 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007918 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007919 goto err;
7920
Jens Axboe69fb2132020-09-14 11:16:23 -06007921 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007922 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007923 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007924 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007925 "io_uring-sq");
7926 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007927 if (IS_ERR(sqd->thread)) {
7928 ret = PTR_ERR(sqd->thread);
7929 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007930 goto err;
7931 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007932 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007933 if (ret)
7934 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007935 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7936 /* Can't have SQ_AFF without SQPOLL */
7937 ret = -EINVAL;
7938 goto err;
7939 }
7940
Jens Axboeaa061652020-09-02 14:50:27 -06007941done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007942 ret = io_init_wq_offload(ctx, p);
7943 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007944 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007945
7946 return 0;
7947err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007948 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007949 return ret;
7950}
7951
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007952static void io_sq_offload_start(struct io_ring_ctx *ctx)
7953{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007954 struct io_sq_data *sqd = ctx->sq_data;
7955
7956 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7957 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007958}
7959
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007960static inline void __io_unaccount_mem(struct user_struct *user,
7961 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007962{
7963 atomic_long_sub(nr_pages, &user->locked_vm);
7964}
7965
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007966static inline int __io_account_mem(struct user_struct *user,
7967 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007968{
7969 unsigned long page_limit, cur_pages, new_pages;
7970
7971 /* Don't allow more pages than we can safely lock */
7972 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7973
7974 do {
7975 cur_pages = atomic_long_read(&user->locked_vm);
7976 new_pages = cur_pages + nr_pages;
7977 if (new_pages > page_limit)
7978 return -ENOMEM;
7979 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7980 new_pages) != cur_pages);
7981
7982 return 0;
7983}
7984
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007985static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7986 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007987{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007988 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007989 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007990
Jens Axboe2aede0e2020-09-14 10:45:53 -06007991 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007992 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007993 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007994 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007995 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007996 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007997}
7998
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007999static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8000 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008001{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008002 int ret;
8003
8004 if (ctx->limit_mem) {
8005 ret = __io_account_mem(ctx->user, nr_pages);
8006 if (ret)
8007 return ret;
8008 }
8009
Jens Axboe2aede0e2020-09-14 10:45:53 -06008010 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008011 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008012 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008013 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06008014 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008015 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008016
8017 return 0;
8018}
8019
Jens Axboe2b188cc2019-01-07 10:46:33 -07008020static void io_mem_free(void *ptr)
8021{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008022 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008023
Mark Rutland52e04ef2019-04-30 17:30:21 +01008024 if (!ptr)
8025 return;
8026
8027 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008028 if (put_page_testzero(page))
8029 free_compound_page(page);
8030}
8031
8032static void *io_mem_alloc(size_t size)
8033{
8034 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8035 __GFP_NORETRY;
8036
8037 return (void *) __get_free_pages(gfp_flags, get_order(size));
8038}
8039
Hristo Venev75b28af2019-08-26 17:23:46 +00008040static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8041 size_t *sq_offset)
8042{
8043 struct io_rings *rings;
8044 size_t off, sq_array_size;
8045
8046 off = struct_size(rings, cqes, cq_entries);
8047 if (off == SIZE_MAX)
8048 return SIZE_MAX;
8049
8050#ifdef CONFIG_SMP
8051 off = ALIGN(off, SMP_CACHE_BYTES);
8052 if (off == 0)
8053 return SIZE_MAX;
8054#endif
8055
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008056 if (sq_offset)
8057 *sq_offset = off;
8058
Hristo Venev75b28af2019-08-26 17:23:46 +00008059 sq_array_size = array_size(sizeof(u32), sq_entries);
8060 if (sq_array_size == SIZE_MAX)
8061 return SIZE_MAX;
8062
8063 if (check_add_overflow(off, sq_array_size, &off))
8064 return SIZE_MAX;
8065
Hristo Venev75b28af2019-08-26 17:23:46 +00008066 return off;
8067}
8068
Jens Axboe2b188cc2019-01-07 10:46:33 -07008069static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8070{
Hristo Venev75b28af2019-08-26 17:23:46 +00008071 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008072
Hristo Venev75b28af2019-08-26 17:23:46 +00008073 pages = (size_t)1 << get_order(
8074 rings_size(sq_entries, cq_entries, NULL));
8075 pages += (size_t)1 << get_order(
8076 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008077
Hristo Venev75b28af2019-08-26 17:23:46 +00008078 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008079}
8080
Jens Axboeedafcce2019-01-09 09:16:05 -07008081static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8082{
8083 int i, j;
8084
8085 if (!ctx->user_bufs)
8086 return -ENXIO;
8087
8088 for (i = 0; i < ctx->nr_user_bufs; i++) {
8089 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8090
8091 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008092 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008093
Jens Axboede293932020-09-17 16:19:16 -06008094 if (imu->acct_pages)
8095 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008096 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008097 imu->nr_bvecs = 0;
8098 }
8099
8100 kfree(ctx->user_bufs);
8101 ctx->user_bufs = NULL;
8102 ctx->nr_user_bufs = 0;
8103 return 0;
8104}
8105
8106static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8107 void __user *arg, unsigned index)
8108{
8109 struct iovec __user *src;
8110
8111#ifdef CONFIG_COMPAT
8112 if (ctx->compat) {
8113 struct compat_iovec __user *ciovs;
8114 struct compat_iovec ciov;
8115
8116 ciovs = (struct compat_iovec __user *) arg;
8117 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8118 return -EFAULT;
8119
Jens Axboed55e5f52019-12-11 16:12:15 -07008120 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008121 dst->iov_len = ciov.iov_len;
8122 return 0;
8123 }
8124#endif
8125 src = (struct iovec __user *) arg;
8126 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8127 return -EFAULT;
8128 return 0;
8129}
8130
Jens Axboede293932020-09-17 16:19:16 -06008131/*
8132 * Not super efficient, but this is just a registration time. And we do cache
8133 * the last compound head, so generally we'll only do a full search if we don't
8134 * match that one.
8135 *
8136 * We check if the given compound head page has already been accounted, to
8137 * avoid double accounting it. This allows us to account the full size of the
8138 * page, not just the constituent pages of a huge page.
8139 */
8140static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8141 int nr_pages, struct page *hpage)
8142{
8143 int i, j;
8144
8145 /* check current page array */
8146 for (i = 0; i < nr_pages; i++) {
8147 if (!PageCompound(pages[i]))
8148 continue;
8149 if (compound_head(pages[i]) == hpage)
8150 return true;
8151 }
8152
8153 /* check previously registered pages */
8154 for (i = 0; i < ctx->nr_user_bufs; i++) {
8155 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8156
8157 for (j = 0; j < imu->nr_bvecs; j++) {
8158 if (!PageCompound(imu->bvec[j].bv_page))
8159 continue;
8160 if (compound_head(imu->bvec[j].bv_page) == hpage)
8161 return true;
8162 }
8163 }
8164
8165 return false;
8166}
8167
8168static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8169 int nr_pages, struct io_mapped_ubuf *imu,
8170 struct page **last_hpage)
8171{
8172 int i, ret;
8173
8174 for (i = 0; i < nr_pages; i++) {
8175 if (!PageCompound(pages[i])) {
8176 imu->acct_pages++;
8177 } else {
8178 struct page *hpage;
8179
8180 hpage = compound_head(pages[i]);
8181 if (hpage == *last_hpage)
8182 continue;
8183 *last_hpage = hpage;
8184 if (headpage_already_acct(ctx, pages, i, hpage))
8185 continue;
8186 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8187 }
8188 }
8189
8190 if (!imu->acct_pages)
8191 return 0;
8192
8193 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8194 if (ret)
8195 imu->acct_pages = 0;
8196 return ret;
8197}
8198
Jens Axboeedafcce2019-01-09 09:16:05 -07008199static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8200 unsigned nr_args)
8201{
8202 struct vm_area_struct **vmas = NULL;
8203 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008204 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008205 int i, j, got_pages = 0;
8206 int ret = -EINVAL;
8207
8208 if (ctx->user_bufs)
8209 return -EBUSY;
8210 if (!nr_args || nr_args > UIO_MAXIOV)
8211 return -EINVAL;
8212
8213 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8214 GFP_KERNEL);
8215 if (!ctx->user_bufs)
8216 return -ENOMEM;
8217
8218 for (i = 0; i < nr_args; i++) {
8219 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8220 unsigned long off, start, end, ubuf;
8221 int pret, nr_pages;
8222 struct iovec iov;
8223 size_t size;
8224
8225 ret = io_copy_iov(ctx, &iov, arg, i);
8226 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008227 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008228
8229 /*
8230 * Don't impose further limits on the size and buffer
8231 * constraints here, we'll -EINVAL later when IO is
8232 * submitted if they are wrong.
8233 */
8234 ret = -EFAULT;
8235 if (!iov.iov_base || !iov.iov_len)
8236 goto err;
8237
8238 /* arbitrary limit, but we need something */
8239 if (iov.iov_len > SZ_1G)
8240 goto err;
8241
8242 ubuf = (unsigned long) iov.iov_base;
8243 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8244 start = ubuf >> PAGE_SHIFT;
8245 nr_pages = end - start;
8246
Jens Axboeedafcce2019-01-09 09:16:05 -07008247 ret = 0;
8248 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008249 kvfree(vmas);
8250 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008251 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008252 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008253 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008254 sizeof(struct vm_area_struct *),
8255 GFP_KERNEL);
8256 if (!pages || !vmas) {
8257 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008258 goto err;
8259 }
8260 got_pages = nr_pages;
8261 }
8262
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008263 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008264 GFP_KERNEL);
8265 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008266 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008267 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008268
8269 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008270 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008271 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008272 FOLL_WRITE | FOLL_LONGTERM,
8273 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008274 if (pret == nr_pages) {
8275 /* don't support file backed memory */
8276 for (j = 0; j < nr_pages; j++) {
8277 struct vm_area_struct *vma = vmas[j];
8278
8279 if (vma->vm_file &&
8280 !is_file_hugepages(vma->vm_file)) {
8281 ret = -EOPNOTSUPP;
8282 break;
8283 }
8284 }
8285 } else {
8286 ret = pret < 0 ? pret : -EFAULT;
8287 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008288 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008289 if (ret) {
8290 /*
8291 * if we did partial map, or found file backed vmas,
8292 * release any pages we did get
8293 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008294 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008295 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008296 kvfree(imu->bvec);
8297 goto err;
8298 }
8299
8300 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8301 if (ret) {
8302 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008303 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008304 goto err;
8305 }
8306
8307 off = ubuf & ~PAGE_MASK;
8308 size = iov.iov_len;
8309 for (j = 0; j < nr_pages; j++) {
8310 size_t vec_len;
8311
8312 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8313 imu->bvec[j].bv_page = pages[j];
8314 imu->bvec[j].bv_len = vec_len;
8315 imu->bvec[j].bv_offset = off;
8316 off = 0;
8317 size -= vec_len;
8318 }
8319 /* store original address for later verification */
8320 imu->ubuf = ubuf;
8321 imu->len = iov.iov_len;
8322 imu->nr_bvecs = nr_pages;
8323
8324 ctx->nr_user_bufs++;
8325 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008326 kvfree(pages);
8327 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008328 return 0;
8329err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008330 kvfree(pages);
8331 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008332 io_sqe_buffer_unregister(ctx);
8333 return ret;
8334}
8335
Jens Axboe9b402842019-04-11 11:45:41 -06008336static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8337{
8338 __s32 __user *fds = arg;
8339 int fd;
8340
8341 if (ctx->cq_ev_fd)
8342 return -EBUSY;
8343
8344 if (copy_from_user(&fd, fds, sizeof(*fds)))
8345 return -EFAULT;
8346
8347 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8348 if (IS_ERR(ctx->cq_ev_fd)) {
8349 int ret = PTR_ERR(ctx->cq_ev_fd);
8350 ctx->cq_ev_fd = NULL;
8351 return ret;
8352 }
8353
8354 return 0;
8355}
8356
8357static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8358{
8359 if (ctx->cq_ev_fd) {
8360 eventfd_ctx_put(ctx->cq_ev_fd);
8361 ctx->cq_ev_fd = NULL;
8362 return 0;
8363 }
8364
8365 return -ENXIO;
8366}
8367
Jens Axboe5a2e7452020-02-23 16:23:11 -07008368static int __io_destroy_buffers(int id, void *p, void *data)
8369{
8370 struct io_ring_ctx *ctx = data;
8371 struct io_buffer *buf = p;
8372
Jens Axboe067524e2020-03-02 16:32:28 -07008373 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008374 return 0;
8375}
8376
8377static void io_destroy_buffers(struct io_ring_ctx *ctx)
8378{
8379 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8380 idr_destroy(&ctx->io_buffer_idr);
8381}
8382
Jens Axboe2b188cc2019-01-07 10:46:33 -07008383static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8384{
Jens Axboe6b063142019-01-10 22:13:58 -07008385 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008386 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008387
8388 if (ctx->sqo_task) {
8389 put_task_struct(ctx->sqo_task);
8390 ctx->sqo_task = NULL;
8391 mmdrop(ctx->mm_account);
8392 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008393 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008394
Dennis Zhou91d8f512020-09-16 13:41:05 -07008395#ifdef CONFIG_BLK_CGROUP
8396 if (ctx->sqo_blkcg_css)
8397 css_put(ctx->sqo_blkcg_css);
8398#endif
8399
Jens Axboe6b063142019-01-10 22:13:58 -07008400 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008401 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008402 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008403 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008404
Jens Axboe2b188cc2019-01-07 10:46:33 -07008405#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008406 if (ctx->ring_sock) {
8407 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008408 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008409 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008410#endif
8411
Hristo Venev75b28af2019-08-26 17:23:46 +00008412 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008413 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008414
8415 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008416 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008417 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008418 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008419 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008420 kfree(ctx);
8421}
8422
8423static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8424{
8425 struct io_ring_ctx *ctx = file->private_data;
8426 __poll_t mask = 0;
8427
8428 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008429 /*
8430 * synchronizes with barrier from wq_has_sleeper call in
8431 * io_commit_cqring
8432 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008433 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008434 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008435 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008436 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008437 mask |= EPOLLIN | EPOLLRDNORM;
8438
8439 return mask;
8440}
8441
8442static int io_uring_fasync(int fd, struct file *file, int on)
8443{
8444 struct io_ring_ctx *ctx = file->private_data;
8445
8446 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8447}
8448
Jens Axboe071698e2020-01-28 10:04:42 -07008449static int io_remove_personalities(int id, void *p, void *data)
8450{
8451 struct io_ring_ctx *ctx = data;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008452 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008453
Jens Axboe1e6fa522020-10-15 08:46:24 -06008454 iod = idr_remove(&ctx->personality_idr, id);
8455 if (iod) {
8456 put_cred(iod->creds);
8457 if (refcount_dec_and_test(&iod->count))
8458 kfree(iod);
8459 }
Jens Axboe071698e2020-01-28 10:04:42 -07008460 return 0;
8461}
8462
Jens Axboe85faa7b2020-04-09 18:14:00 -06008463static void io_ring_exit_work(struct work_struct *work)
8464{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008465 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8466 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008467
Jens Axboe56952e92020-06-17 15:00:04 -06008468 /*
8469 * If we're doing polled IO and end up having requests being
8470 * submitted async (out-of-line), then completions can come in while
8471 * we're waiting for refs to drop. We need to reap these manually,
8472 * as nobody else will be looking for them.
8473 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008474 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008475 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008476 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008477 io_iopoll_try_reap_events(ctx);
8478 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008479 io_ring_ctx_free(ctx);
8480}
8481
Jens Axboe2b188cc2019-01-07 10:46:33 -07008482static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8483{
8484 mutex_lock(&ctx->uring_lock);
8485 percpu_ref_kill(&ctx->refs);
8486 mutex_unlock(&ctx->uring_lock);
8487
Jens Axboef3606e32020-09-22 08:18:24 -06008488 io_kill_timeouts(ctx, NULL);
8489 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008490
8491 if (ctx->io_wq)
8492 io_wq_cancel_all(ctx->io_wq);
8493
Jens Axboe15dff282019-11-13 09:09:23 -07008494 /* if we failed setting up the ctx, we might not have any rings */
8495 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008496 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008497 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008498 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008499
8500 /*
8501 * Do this upfront, so we won't have a grace period where the ring
8502 * is closed but resources aren't reaped yet. This can cause
8503 * spurious failure in setting up a new ring.
8504 */
Jens Axboe760618f2020-07-24 12:53:31 -06008505 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8506 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008507
Jens Axboe85faa7b2020-04-09 18:14:00 -06008508 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008509 /*
8510 * Use system_unbound_wq to avoid spawning tons of event kworkers
8511 * if we're exiting a ton of rings at the same time. It just adds
8512 * noise and overhead, there's no discernable change in runtime
8513 * over using system_wq.
8514 */
8515 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008516}
8517
8518static int io_uring_release(struct inode *inode, struct file *file)
8519{
8520 struct io_ring_ctx *ctx = file->private_data;
8521
8522 file->private_data = NULL;
8523 io_ring_ctx_wait_and_kill(ctx);
8524 return 0;
8525}
8526
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008527static bool io_wq_files_match(struct io_wq_work *work, void *data)
8528{
8529 struct files_struct *files = data;
8530
Jens Axboedfead8a2020-10-14 10:12:37 -06008531 return !files || ((work->flags & IO_WQ_WORK_FILES) &&
Jens Axboe98447d62020-10-14 10:48:51 -06008532 work->identity->files == files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008533}
8534
Jens Axboef254ac02020-08-12 17:33:30 -06008535/*
8536 * Returns true if 'preq' is the link parent of 'req'
8537 */
8538static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8539{
8540 struct io_kiocb *link;
8541
8542 if (!(preq->flags & REQ_F_LINK_HEAD))
8543 return false;
8544
8545 list_for_each_entry(link, &preq->link_list, link_list) {
8546 if (link == req)
8547 return true;
8548 }
8549
8550 return false;
8551}
8552
8553/*
8554 * We're looking to cancel 'req' because it's holding on to our files, but
8555 * 'req' could be a link to another request. See if it is, and cancel that
8556 * parent request if so.
8557 */
8558static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8559{
8560 struct hlist_node *tmp;
8561 struct io_kiocb *preq;
8562 bool found = false;
8563 int i;
8564
8565 spin_lock_irq(&ctx->completion_lock);
8566 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8567 struct hlist_head *list;
8568
8569 list = &ctx->cancel_hash[i];
8570 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8571 found = io_match_link(preq, req);
8572 if (found) {
8573 io_poll_remove_one(preq);
8574 break;
8575 }
8576 }
8577 }
8578 spin_unlock_irq(&ctx->completion_lock);
8579 return found;
8580}
8581
8582static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8583 struct io_kiocb *req)
8584{
8585 struct io_kiocb *preq;
8586 bool found = false;
8587
8588 spin_lock_irq(&ctx->completion_lock);
8589 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8590 found = io_match_link(preq, req);
8591 if (found) {
8592 __io_timeout_cancel(preq);
8593 break;
8594 }
8595 }
8596 spin_unlock_irq(&ctx->completion_lock);
8597 return found;
8598}
8599
Jens Axboeb711d4e2020-08-16 08:23:05 -07008600static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8601{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008602 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8603 bool ret;
8604
8605 if (req->flags & REQ_F_LINK_TIMEOUT) {
8606 unsigned long flags;
8607 struct io_ring_ctx *ctx = req->ctx;
8608
8609 /* protect against races with linked timeouts */
8610 spin_lock_irqsave(&ctx->completion_lock, flags);
8611 ret = io_match_link(req, data);
8612 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8613 } else {
8614 ret = io_match_link(req, data);
8615 }
8616 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008617}
8618
8619static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8620{
8621 enum io_wq_cancel cret;
8622
8623 /* cancel this particular work, if it's running */
8624 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8625 if (cret != IO_WQ_CANCEL_NOTFOUND)
8626 return;
8627
8628 /* find links that hold this pending, cancel those */
8629 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8630 if (cret != IO_WQ_CANCEL_NOTFOUND)
8631 return;
8632
8633 /* if we have a poll link holding this pending, cancel that */
8634 if (io_poll_remove_link(ctx, req))
8635 return;
8636
8637 /* final option, timeout link is holding this req pending */
8638 io_timeout_remove_link(ctx, req);
8639}
8640
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008641static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008642 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008643 struct files_struct *files)
8644{
8645 struct io_defer_entry *de = NULL;
8646 LIST_HEAD(list);
8647
8648 spin_lock_irq(&ctx->completion_lock);
8649 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008650 if (io_task_match(de->req, task) &&
8651 io_match_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008652 list_cut_position(&list, &ctx->defer_list, &de->list);
8653 break;
8654 }
8655 }
8656 spin_unlock_irq(&ctx->completion_lock);
8657
8658 while (!list_empty(&list)) {
8659 de = list_first_entry(&list, struct io_defer_entry, list);
8660 list_del_init(&de->list);
8661 req_set_fail_links(de->req);
8662 io_put_req(de->req);
8663 io_req_complete(de->req, -ECANCELED);
8664 kfree(de);
8665 }
8666}
8667
Jens Axboe76e1b642020-09-26 15:05:03 -06008668/*
8669 * Returns true if we found and killed one or more files pinning requests
8670 */
8671static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008672 struct files_struct *files)
8673{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008674 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008675 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008676
8677 /* cancel all at once, should be faster than doing it one by one*/
8678 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8679
Jens Axboefcb323c2019-10-24 12:39:47 -06008680 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008681 struct io_kiocb *cancel_req = NULL, *req;
8682 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008683
8684 spin_lock_irq(&ctx->inflight_lock);
8685 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboedfead8a2020-10-14 10:12:37 -06008686 if (files && (req->work.flags & IO_WQ_WORK_FILES) &&
Jens Axboe98447d62020-10-14 10:48:51 -06008687 req->work.identity->files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008688 continue;
8689 /* req is being completed, ignore */
8690 if (!refcount_inc_not_zero(&req->refs))
8691 continue;
8692 cancel_req = req;
8693 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008694 }
Jens Axboe768134d2019-11-10 20:30:53 -07008695 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008696 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008697 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008698 spin_unlock_irq(&ctx->inflight_lock);
8699
Jens Axboe768134d2019-11-10 20:30:53 -07008700 /* We need to keep going until we don't find a matching req */
8701 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008702 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008703 /* cancel this request, or head link requests */
8704 io_attempt_cancel(ctx, cancel_req);
8705 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008706 /* cancellations _may_ trigger task work */
8707 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008708 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008709 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008710 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008711
8712 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008713}
8714
Pavel Begunkov801dd572020-06-15 10:33:14 +03008715static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008716{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008717 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8718 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008719
Jens Axboef3606e32020-09-22 08:18:24 -06008720 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008721}
8722
Jens Axboe0f212202020-09-13 13:09:39 -06008723static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8724 struct task_struct *task,
8725 struct files_struct *files)
8726{
8727 bool ret;
8728
8729 ret = io_uring_cancel_files(ctx, files);
8730 if (!files) {
8731 enum io_wq_cancel cret;
8732
8733 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8734 if (cret != IO_WQ_CANCEL_NOTFOUND)
8735 ret = true;
8736
8737 /* SQPOLL thread does its own polling */
8738 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8739 while (!list_empty_careful(&ctx->iopoll_list)) {
8740 io_iopoll_try_reap_events(ctx);
8741 ret = true;
8742 }
8743 }
8744
8745 ret |= io_poll_remove_all(ctx, task);
8746 ret |= io_kill_timeouts(ctx, task);
8747 }
8748
8749 return ret;
8750}
8751
8752/*
8753 * We need to iteratively cancel requests, in case a request has dependent
8754 * hard links. These persist even for failure of cancelations, hence keep
8755 * looping until none are found.
8756 */
8757static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8758 struct files_struct *files)
8759{
8760 struct task_struct *task = current;
8761
Jens Axboefdaf0832020-10-30 09:37:30 -06008762 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008763 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008764 atomic_inc(&task->io_uring->in_idle);
8765 io_sq_thread_park(ctx->sq_data);
8766 }
Jens Axboe0f212202020-09-13 13:09:39 -06008767
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008768 if (files)
8769 io_cancel_defer_files(ctx, NULL, files);
8770 else
8771 io_cancel_defer_files(ctx, task, NULL);
8772
Jens Axboe0f212202020-09-13 13:09:39 -06008773 io_cqring_overflow_flush(ctx, true, task, files);
8774
8775 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8776 io_run_task_work();
8777 cond_resched();
8778 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008779
8780 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8781 atomic_dec(&task->io_uring->in_idle);
8782 /*
8783 * If the files that are going away are the ones in the thread
8784 * identity, clear them out.
8785 */
8786 if (task->io_uring->identity->files == files)
8787 task->io_uring->identity->files = NULL;
8788 io_sq_thread_unpark(ctx->sq_data);
8789 }
Jens Axboe0f212202020-09-13 13:09:39 -06008790}
8791
8792/*
8793 * Note that this task has used io_uring. We use it for cancelation purposes.
8794 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008795static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008796{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008797 struct io_uring_task *tctx = current->io_uring;
8798
8799 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008800 int ret;
8801
8802 ret = io_uring_alloc_task_context(current);
8803 if (unlikely(ret))
8804 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008805 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008806 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008807 if (tctx->last != file) {
8808 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008809
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008810 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008811 get_file(file);
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008812 xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008813 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008814 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008815 }
8816
Jens Axboefdaf0832020-10-30 09:37:30 -06008817 /*
8818 * This is race safe in that the task itself is doing this, hence it
8819 * cannot be going through the exit/cancel paths at the same time.
8820 * This cannot be modified while exit/cancel is running.
8821 */
8822 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8823 tctx->sqpoll = true;
8824
Jens Axboe0f212202020-09-13 13:09:39 -06008825 return 0;
8826}
8827
8828/*
8829 * Remove this io_uring_file -> task mapping.
8830 */
8831static void io_uring_del_task_file(struct file *file)
8832{
8833 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008834
8835 if (tctx->last == file)
8836 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008837 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008838 if (file)
8839 fput(file);
8840}
8841
Jens Axboe0f212202020-09-13 13:09:39 -06008842/*
8843 * Drop task note for this file if we're the only ones that hold it after
8844 * pending fput()
8845 */
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01008846static void io_uring_attempt_task_drop(struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008847{
8848 if (!current->io_uring)
8849 return;
8850 /*
8851 * fput() is pending, will be 2 if the only other ref is our potential
8852 * task file note. If the task is exiting, drop regardless of count.
8853 */
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01008854 if (fatal_signal_pending(current) || (current->flags & PF_EXITING) ||
8855 atomic_long_read(&file->f_count) == 2)
8856 io_uring_del_task_file(file);
Jens Axboe0f212202020-09-13 13:09:39 -06008857}
8858
8859void __io_uring_files_cancel(struct files_struct *files)
8860{
8861 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008862 struct file *file;
8863 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008864
8865 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008866 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008867
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008868 xa_for_each(&tctx->xa, index, file) {
8869 struct io_ring_ctx *ctx = file->private_data;
Jens Axboe0f212202020-09-13 13:09:39 -06008870
8871 io_uring_cancel_task_requests(ctx, files);
8872 if (files)
8873 io_uring_del_task_file(file);
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008874 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008875
8876 atomic_dec(&tctx->in_idle);
8877}
8878
8879static s64 tctx_inflight(struct io_uring_task *tctx)
8880{
8881 unsigned long index;
8882 struct file *file;
8883 s64 inflight;
8884
8885 inflight = percpu_counter_sum(&tctx->inflight);
8886 if (!tctx->sqpoll)
8887 return inflight;
8888
8889 /*
8890 * If we have SQPOLL rings, then we need to iterate and find them, and
8891 * add the pending count for those.
8892 */
8893 xa_for_each(&tctx->xa, index, file) {
8894 struct io_ring_ctx *ctx = file->private_data;
8895
8896 if (ctx->flags & IORING_SETUP_SQPOLL) {
8897 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
8898
8899 inflight += percpu_counter_sum(&__tctx->inflight);
8900 }
8901 }
8902
8903 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008904}
8905
Jens Axboe0f212202020-09-13 13:09:39 -06008906/*
8907 * Find any io_uring fd that this task has registered or done IO on, and cancel
8908 * requests.
8909 */
8910void __io_uring_task_cancel(void)
8911{
8912 struct io_uring_task *tctx = current->io_uring;
8913 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008914 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008915
8916 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008917 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008918
Jens Axboed8a6df12020-10-15 16:24:45 -06008919 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008920 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008921 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008922 if (!inflight)
8923 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008924 __io_uring_files_cancel(NULL);
8925
8926 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8927
8928 /*
8929 * If we've seen completions, retry. This avoids a race where
8930 * a completion comes in before we did prepare_to_wait().
8931 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008932 if (inflight != tctx_inflight(tctx))
Jens Axboe0f212202020-09-13 13:09:39 -06008933 continue;
Jens Axboe0f212202020-09-13 13:09:39 -06008934 schedule();
Jens Axboed8a6df12020-10-15 16:24:45 -06008935 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008936
8937 finish_wait(&tctx->wait, &wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008938 atomic_dec(&tctx->in_idle);
Jens Axboefcb323c2019-10-24 12:39:47 -06008939}
8940
8941static int io_uring_flush(struct file *file, void *data)
8942{
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01008943 io_uring_attempt_task_drop(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06008944 return 0;
8945}
8946
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008947static void *io_uring_validate_mmap_request(struct file *file,
8948 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008949{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008950 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008951 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008952 struct page *page;
8953 void *ptr;
8954
8955 switch (offset) {
8956 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008957 case IORING_OFF_CQ_RING:
8958 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008959 break;
8960 case IORING_OFF_SQES:
8961 ptr = ctx->sq_sqes;
8962 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008963 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008964 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008965 }
8966
8967 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008968 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008969 return ERR_PTR(-EINVAL);
8970
8971 return ptr;
8972}
8973
8974#ifdef CONFIG_MMU
8975
8976static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8977{
8978 size_t sz = vma->vm_end - vma->vm_start;
8979 unsigned long pfn;
8980 void *ptr;
8981
8982 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8983 if (IS_ERR(ptr))
8984 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008985
8986 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8987 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8988}
8989
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008990#else /* !CONFIG_MMU */
8991
8992static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8993{
8994 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8995}
8996
8997static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8998{
8999 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9000}
9001
9002static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9003 unsigned long addr, unsigned long len,
9004 unsigned long pgoff, unsigned long flags)
9005{
9006 void *ptr;
9007
9008 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9009 if (IS_ERR(ptr))
9010 return PTR_ERR(ptr);
9011
9012 return (unsigned long) ptr;
9013}
9014
9015#endif /* !CONFIG_MMU */
9016
Jens Axboe90554202020-09-03 12:12:41 -06009017static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
9018{
9019 DEFINE_WAIT(wait);
9020
9021 do {
9022 if (!io_sqring_full(ctx))
9023 break;
9024
9025 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9026
9027 if (!io_sqring_full(ctx))
9028 break;
9029
9030 schedule();
9031 } while (!signal_pending(current));
9032
9033 finish_wait(&ctx->sqo_sq_wait, &wait);
9034}
9035
Jens Axboe2b188cc2019-01-07 10:46:33 -07009036SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
9037 u32, min_complete, u32, flags, const sigset_t __user *, sig,
9038 size_t, sigsz)
9039{
9040 struct io_ring_ctx *ctx;
9041 long ret = -EBADF;
9042 int submitted = 0;
9043 struct fd f;
9044
Jens Axboe4c6e2772020-07-01 11:29:10 -06009045 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009046
Jens Axboe90554202020-09-03 12:12:41 -06009047 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9048 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009049 return -EINVAL;
9050
9051 f = fdget(fd);
9052 if (!f.file)
9053 return -EBADF;
9054
9055 ret = -EOPNOTSUPP;
9056 if (f.file->f_op != &io_uring_fops)
9057 goto out_fput;
9058
9059 ret = -ENXIO;
9060 ctx = f.file->private_data;
9061 if (!percpu_ref_tryget(&ctx->refs))
9062 goto out_fput;
9063
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009064 ret = -EBADFD;
9065 if (ctx->flags & IORING_SETUP_R_DISABLED)
9066 goto out;
9067
Jens Axboe6c271ce2019-01-10 11:22:30 -07009068 /*
9069 * For SQ polling, the thread will do all submissions and completions.
9070 * Just return the requested submit count, and wake the thread if
9071 * we were asked to.
9072 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009073 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009074 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07009075 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06009076 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07009077 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009078 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06009079 if (flags & IORING_ENTER_SQ_WAIT)
9080 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07009081 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009082 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009083 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009084 if (unlikely(ret))
9085 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009086 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009087 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009088 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009089
9090 if (submitted != to_submit)
9091 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009092 }
9093 if (flags & IORING_ENTER_GETEVENTS) {
9094 min_complete = min(min_complete, ctx->cq_entries);
9095
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009096 /*
9097 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9098 * space applications don't need to do io completion events
9099 * polling again, they can rely on io_sq_thread to do polling
9100 * work, which can reduce cpu usage and uring_lock contention.
9101 */
9102 if (ctx->flags & IORING_SETUP_IOPOLL &&
9103 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009104 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009105 } else {
9106 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
9107 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009108 }
9109
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009110out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009111 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009112out_fput:
9113 fdput(f);
9114 return submitted ? submitted : ret;
9115}
9116
Tobias Klauserbebdb652020-02-26 18:38:32 +01009117#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009118static int io_uring_show_cred(int id, void *p, void *data)
9119{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009120 struct io_identity *iod = p;
9121 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009122 struct seq_file *m = data;
9123 struct user_namespace *uns = seq_user_ns(m);
9124 struct group_info *gi;
9125 kernel_cap_t cap;
9126 unsigned __capi;
9127 int g;
9128
9129 seq_printf(m, "%5d\n", id);
9130 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9131 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9132 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9133 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9134 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9135 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9136 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9137 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9138 seq_puts(m, "\n\tGroups:\t");
9139 gi = cred->group_info;
9140 for (g = 0; g < gi->ngroups; g++) {
9141 seq_put_decimal_ull(m, g ? " " : "",
9142 from_kgid_munged(uns, gi->gid[g]));
9143 }
9144 seq_puts(m, "\n\tCapEff:\t");
9145 cap = cred->cap_effective;
9146 CAP_FOR_EACH_U32(__capi)
9147 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9148 seq_putc(m, '\n');
9149 return 0;
9150}
9151
9152static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9153{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009154 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009155 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009156 int i;
9157
Jens Axboefad8e0d2020-09-28 08:57:48 -06009158 /*
9159 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9160 * since fdinfo case grabs it in the opposite direction of normal use
9161 * cases. If we fail to get the lock, we just don't iterate any
9162 * structures that could be going away outside the io_uring mutex.
9163 */
9164 has_lock = mutex_trylock(&ctx->uring_lock);
9165
Joseph Qidbbe9c62020-09-29 09:01:22 -06009166 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9167 sq = ctx->sq_data;
9168
9169 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9170 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009171 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009172 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009173 struct fixed_file_table *table;
9174 struct file *f;
9175
9176 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9177 f = table->files[i & IORING_FILE_TABLE_MASK];
9178 if (f)
9179 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9180 else
9181 seq_printf(m, "%5u: <none>\n", i);
9182 }
9183 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009184 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009185 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9186
9187 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9188 (unsigned int) buf->len);
9189 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009190 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009191 seq_printf(m, "Personalities:\n");
9192 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9193 }
Jens Axboed7718a92020-02-14 22:23:12 -07009194 seq_printf(m, "PollList:\n");
9195 spin_lock_irq(&ctx->completion_lock);
9196 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9197 struct hlist_head *list = &ctx->cancel_hash[i];
9198 struct io_kiocb *req;
9199
9200 hlist_for_each_entry(req, list, hash_node)
9201 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9202 req->task->task_works != NULL);
9203 }
9204 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009205 if (has_lock)
9206 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009207}
9208
9209static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9210{
9211 struct io_ring_ctx *ctx = f->private_data;
9212
9213 if (percpu_ref_tryget(&ctx->refs)) {
9214 __io_uring_show_fdinfo(ctx, m);
9215 percpu_ref_put(&ctx->refs);
9216 }
9217}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009218#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009219
Jens Axboe2b188cc2019-01-07 10:46:33 -07009220static const struct file_operations io_uring_fops = {
9221 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009222 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009223 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009224#ifndef CONFIG_MMU
9225 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9226 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9227#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009228 .poll = io_uring_poll,
9229 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009230#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009231 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009232#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009233};
9234
9235static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9236 struct io_uring_params *p)
9237{
Hristo Venev75b28af2019-08-26 17:23:46 +00009238 struct io_rings *rings;
9239 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009240
Jens Axboebd740482020-08-05 12:58:23 -06009241 /* make sure these are sane, as we already accounted them */
9242 ctx->sq_entries = p->sq_entries;
9243 ctx->cq_entries = p->cq_entries;
9244
Hristo Venev75b28af2019-08-26 17:23:46 +00009245 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9246 if (size == SIZE_MAX)
9247 return -EOVERFLOW;
9248
9249 rings = io_mem_alloc(size);
9250 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009251 return -ENOMEM;
9252
Hristo Venev75b28af2019-08-26 17:23:46 +00009253 ctx->rings = rings;
9254 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9255 rings->sq_ring_mask = p->sq_entries - 1;
9256 rings->cq_ring_mask = p->cq_entries - 1;
9257 rings->sq_ring_entries = p->sq_entries;
9258 rings->cq_ring_entries = p->cq_entries;
9259 ctx->sq_mask = rings->sq_ring_mask;
9260 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009261
9262 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009263 if (size == SIZE_MAX) {
9264 io_mem_free(ctx->rings);
9265 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009266 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009267 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009268
9269 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009270 if (!ctx->sq_sqes) {
9271 io_mem_free(ctx->rings);
9272 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009273 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009274 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009275
Jens Axboe2b188cc2019-01-07 10:46:33 -07009276 return 0;
9277}
9278
9279/*
9280 * Allocate an anonymous fd, this is what constitutes the application
9281 * visible backing of an io_uring instance. The application mmaps this
9282 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9283 * we have to tie this fd to a socket for file garbage collection purposes.
9284 */
9285static int io_uring_get_fd(struct io_ring_ctx *ctx)
9286{
9287 struct file *file;
9288 int ret;
9289
9290#if defined(CONFIG_UNIX)
9291 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9292 &ctx->ring_sock);
9293 if (ret)
9294 return ret;
9295#endif
9296
9297 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9298 if (ret < 0)
9299 goto err;
9300
9301 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9302 O_RDWR | O_CLOEXEC);
9303 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009304err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009305 put_unused_fd(ret);
9306 ret = PTR_ERR(file);
9307 goto err;
9308 }
9309
9310#if defined(CONFIG_UNIX)
9311 ctx->ring_sock->file = file;
9312#endif
Jens Axboefdaf0832020-10-30 09:37:30 -06009313 if (unlikely(io_uring_add_task_file(ctx, file))) {
Jens Axboe0f212202020-09-13 13:09:39 -06009314 file = ERR_PTR(-ENOMEM);
9315 goto err_fd;
9316 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009317 fd_install(ret, file);
9318 return ret;
9319err:
9320#if defined(CONFIG_UNIX)
9321 sock_release(ctx->ring_sock);
9322 ctx->ring_sock = NULL;
9323#endif
9324 return ret;
9325}
9326
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009327static int io_uring_create(unsigned entries, struct io_uring_params *p,
9328 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009329{
9330 struct user_struct *user = NULL;
9331 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009332 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009333 int ret;
9334
Jens Axboe8110c1a2019-12-28 15:39:54 -07009335 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009336 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009337 if (entries > IORING_MAX_ENTRIES) {
9338 if (!(p->flags & IORING_SETUP_CLAMP))
9339 return -EINVAL;
9340 entries = IORING_MAX_ENTRIES;
9341 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009342
9343 /*
9344 * Use twice as many entries for the CQ ring. It's possible for the
9345 * application to drive a higher depth than the size of the SQ ring,
9346 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009347 * some flexibility in overcommitting a bit. If the application has
9348 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9349 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009350 */
9351 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009352 if (p->flags & IORING_SETUP_CQSIZE) {
9353 /*
9354 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9355 * to a power-of-two, if it isn't already. We do NOT impose
9356 * any cq vs sq ring sizing.
9357 */
Jens Axboe88ec3212020-11-11 10:38:53 -07009358 p->cq_entries = roundup_pow_of_two(p->cq_entries);
Jens Axboe8110c1a2019-12-28 15:39:54 -07009359 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009360 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009361 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9362 if (!(p->flags & IORING_SETUP_CLAMP))
9363 return -EINVAL;
9364 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9365 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009366 } else {
9367 p->cq_entries = 2 * p->sq_entries;
9368 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009369
9370 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009371 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009372
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009373 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009374 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009375 ring_pages(p->sq_entries, p->cq_entries));
9376 if (ret) {
9377 free_uid(user);
9378 return ret;
9379 }
9380 }
9381
9382 ctx = io_ring_ctx_alloc(p);
9383 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009384 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009385 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009386 p->cq_entries));
9387 free_uid(user);
9388 return -ENOMEM;
9389 }
9390 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009391 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009392 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009393#ifdef CONFIG_AUDIT
9394 ctx->loginuid = current->loginuid;
9395 ctx->sessionid = current->sessionid;
9396#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009397 ctx->sqo_task = get_task_struct(current);
9398
9399 /*
9400 * This is just grabbed for accounting purposes. When a process exits,
9401 * the mm is exited and dropped before the files, hence we need to hang
9402 * on to this mm purely for the purposes of being able to unaccount
9403 * memory (locked/pinned vm). It's not used for anything else.
9404 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009405 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009406 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009407
Dennis Zhou91d8f512020-09-16 13:41:05 -07009408#ifdef CONFIG_BLK_CGROUP
9409 /*
9410 * The sq thread will belong to the original cgroup it was inited in.
9411 * If the cgroup goes offline (e.g. disabling the io controller), then
9412 * issued bios will be associated with the closest cgroup later in the
9413 * block layer.
9414 */
9415 rcu_read_lock();
9416 ctx->sqo_blkcg_css = blkcg_css();
9417 ret = css_tryget_online(ctx->sqo_blkcg_css);
9418 rcu_read_unlock();
9419 if (!ret) {
9420 /* don't init against a dying cgroup, have the user try again */
9421 ctx->sqo_blkcg_css = NULL;
9422 ret = -ENODEV;
9423 goto err;
9424 }
9425#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009426
Jens Axboe2b188cc2019-01-07 10:46:33 -07009427 /*
9428 * Account memory _before_ installing the file descriptor. Once
9429 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009430 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009431 * will un-account as well.
9432 */
9433 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9434 ACCT_LOCKED);
9435 ctx->limit_mem = limit_mem;
9436
9437 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009438 if (ret)
9439 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009440
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009441 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009442 if (ret)
9443 goto err;
9444
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009445 if (!(p->flags & IORING_SETUP_R_DISABLED))
9446 io_sq_offload_start(ctx);
9447
Jens Axboe2b188cc2019-01-07 10:46:33 -07009448 memset(&p->sq_off, 0, sizeof(p->sq_off));
9449 p->sq_off.head = offsetof(struct io_rings, sq.head);
9450 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9451 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9452 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9453 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9454 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9455 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9456
9457 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009458 p->cq_off.head = offsetof(struct io_rings, cq.head);
9459 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9460 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9461 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9462 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9463 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009464 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009465
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009466 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9467 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009468 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Jens Axboe28cea78a2020-09-14 10:51:17 -06009469 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009470
9471 if (copy_to_user(params, p, sizeof(*p))) {
9472 ret = -EFAULT;
9473 goto err;
9474 }
Jens Axboed1719f72020-07-30 13:43:53 -06009475
9476 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009477 * Install ring fd as the very last thing, so we don't risk someone
9478 * having closed it before we finish setup
9479 */
9480 ret = io_uring_get_fd(ctx);
9481 if (ret < 0)
9482 goto err;
9483
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009484 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009485 return ret;
9486err:
9487 io_ring_ctx_wait_and_kill(ctx);
9488 return ret;
9489}
9490
9491/*
9492 * Sets up an aio uring context, and returns the fd. Applications asks for a
9493 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9494 * params structure passed in.
9495 */
9496static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9497{
9498 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009499 int i;
9500
9501 if (copy_from_user(&p, params, sizeof(p)))
9502 return -EFAULT;
9503 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9504 if (p.resv[i])
9505 return -EINVAL;
9506 }
9507
Jens Axboe6c271ce2019-01-10 11:22:30 -07009508 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009509 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009510 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9511 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009512 return -EINVAL;
9513
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009514 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009515}
9516
9517SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9518 struct io_uring_params __user *, params)
9519{
9520 return io_uring_setup(entries, params);
9521}
9522
Jens Axboe66f4af92020-01-16 15:36:52 -07009523static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9524{
9525 struct io_uring_probe *p;
9526 size_t size;
9527 int i, ret;
9528
9529 size = struct_size(p, ops, nr_args);
9530 if (size == SIZE_MAX)
9531 return -EOVERFLOW;
9532 p = kzalloc(size, GFP_KERNEL);
9533 if (!p)
9534 return -ENOMEM;
9535
9536 ret = -EFAULT;
9537 if (copy_from_user(p, arg, size))
9538 goto out;
9539 ret = -EINVAL;
9540 if (memchr_inv(p, 0, size))
9541 goto out;
9542
9543 p->last_op = IORING_OP_LAST - 1;
9544 if (nr_args > IORING_OP_LAST)
9545 nr_args = IORING_OP_LAST;
9546
9547 for (i = 0; i < nr_args; i++) {
9548 p->ops[i].op = i;
9549 if (!io_op_defs[i].not_supported)
9550 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9551 }
9552 p->ops_len = i;
9553
9554 ret = 0;
9555 if (copy_to_user(arg, p, size))
9556 ret = -EFAULT;
9557out:
9558 kfree(p);
9559 return ret;
9560}
9561
Jens Axboe071698e2020-01-28 10:04:42 -07009562static int io_register_personality(struct io_ring_ctx *ctx)
9563{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009564 struct io_identity *id;
9565 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009566
Jens Axboe1e6fa522020-10-15 08:46:24 -06009567 id = kmalloc(sizeof(*id), GFP_KERNEL);
9568 if (unlikely(!id))
9569 return -ENOMEM;
9570
9571 io_init_identity(id);
9572 id->creds = get_current_cred();
9573
9574 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9575 if (ret < 0) {
9576 put_cred(id->creds);
9577 kfree(id);
9578 }
9579 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009580}
9581
9582static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9583{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009584 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07009585
Jens Axboe1e6fa522020-10-15 08:46:24 -06009586 iod = idr_remove(&ctx->personality_idr, id);
9587 if (iod) {
9588 put_cred(iod->creds);
9589 if (refcount_dec_and_test(&iod->count))
9590 kfree(iod);
Jens Axboe071698e2020-01-28 10:04:42 -07009591 return 0;
9592 }
9593
9594 return -EINVAL;
9595}
9596
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009597static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9598 unsigned int nr_args)
9599{
9600 struct io_uring_restriction *res;
9601 size_t size;
9602 int i, ret;
9603
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009604 /* Restrictions allowed only if rings started disabled */
9605 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9606 return -EBADFD;
9607
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009608 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009609 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009610 return -EBUSY;
9611
9612 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9613 return -EINVAL;
9614
9615 size = array_size(nr_args, sizeof(*res));
9616 if (size == SIZE_MAX)
9617 return -EOVERFLOW;
9618
9619 res = memdup_user(arg, size);
9620 if (IS_ERR(res))
9621 return PTR_ERR(res);
9622
9623 ret = 0;
9624
9625 for (i = 0; i < nr_args; i++) {
9626 switch (res[i].opcode) {
9627 case IORING_RESTRICTION_REGISTER_OP:
9628 if (res[i].register_op >= IORING_REGISTER_LAST) {
9629 ret = -EINVAL;
9630 goto out;
9631 }
9632
9633 __set_bit(res[i].register_op,
9634 ctx->restrictions.register_op);
9635 break;
9636 case IORING_RESTRICTION_SQE_OP:
9637 if (res[i].sqe_op >= IORING_OP_LAST) {
9638 ret = -EINVAL;
9639 goto out;
9640 }
9641
9642 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9643 break;
9644 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9645 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9646 break;
9647 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9648 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9649 break;
9650 default:
9651 ret = -EINVAL;
9652 goto out;
9653 }
9654 }
9655
9656out:
9657 /* Reset all restrictions if an error happened */
9658 if (ret != 0)
9659 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9660 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009661 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009662
9663 kfree(res);
9664 return ret;
9665}
9666
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009667static int io_register_enable_rings(struct io_ring_ctx *ctx)
9668{
9669 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9670 return -EBADFD;
9671
9672 if (ctx->restrictions.registered)
9673 ctx->restricted = 1;
9674
9675 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9676
9677 io_sq_offload_start(ctx);
9678
9679 return 0;
9680}
9681
Jens Axboe071698e2020-01-28 10:04:42 -07009682static bool io_register_op_must_quiesce(int op)
9683{
9684 switch (op) {
9685 case IORING_UNREGISTER_FILES:
9686 case IORING_REGISTER_FILES_UPDATE:
9687 case IORING_REGISTER_PROBE:
9688 case IORING_REGISTER_PERSONALITY:
9689 case IORING_UNREGISTER_PERSONALITY:
9690 return false;
9691 default:
9692 return true;
9693 }
9694}
9695
Jens Axboeedafcce2019-01-09 09:16:05 -07009696static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9697 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009698 __releases(ctx->uring_lock)
9699 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009700{
9701 int ret;
9702
Jens Axboe35fa71a2019-04-22 10:23:23 -06009703 /*
9704 * We're inside the ring mutex, if the ref is already dying, then
9705 * someone else killed the ctx or is already going through
9706 * io_uring_register().
9707 */
9708 if (percpu_ref_is_dying(&ctx->refs))
9709 return -ENXIO;
9710
Jens Axboe071698e2020-01-28 10:04:42 -07009711 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009712 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009713
Jens Axboe05f3fb32019-12-09 11:22:50 -07009714 /*
9715 * Drop uring mutex before waiting for references to exit. If
9716 * another thread is currently inside io_uring_enter() it might
9717 * need to grab the uring_lock to make progress. If we hold it
9718 * here across the drain wait, then we can deadlock. It's safe
9719 * to drop the mutex here, since no new references will come in
9720 * after we've killed the percpu ref.
9721 */
9722 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009723 do {
9724 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9725 if (!ret)
9726 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009727 ret = io_run_task_work_sig();
9728 if (ret < 0)
9729 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009730 } while (1);
9731
Jens Axboe05f3fb32019-12-09 11:22:50 -07009732 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009733
Jens Axboec1503682020-01-08 08:26:07 -07009734 if (ret) {
9735 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009736 goto out_quiesce;
9737 }
9738 }
9739
9740 if (ctx->restricted) {
9741 if (opcode >= IORING_REGISTER_LAST) {
9742 ret = -EINVAL;
9743 goto out;
9744 }
9745
9746 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9747 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009748 goto out;
9749 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009750 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009751
9752 switch (opcode) {
9753 case IORING_REGISTER_BUFFERS:
9754 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9755 break;
9756 case IORING_UNREGISTER_BUFFERS:
9757 ret = -EINVAL;
9758 if (arg || nr_args)
9759 break;
9760 ret = io_sqe_buffer_unregister(ctx);
9761 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009762 case IORING_REGISTER_FILES:
9763 ret = io_sqe_files_register(ctx, arg, nr_args);
9764 break;
9765 case IORING_UNREGISTER_FILES:
9766 ret = -EINVAL;
9767 if (arg || nr_args)
9768 break;
9769 ret = io_sqe_files_unregister(ctx);
9770 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009771 case IORING_REGISTER_FILES_UPDATE:
9772 ret = io_sqe_files_update(ctx, arg, nr_args);
9773 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009774 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009775 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009776 ret = -EINVAL;
9777 if (nr_args != 1)
9778 break;
9779 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009780 if (ret)
9781 break;
9782 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9783 ctx->eventfd_async = 1;
9784 else
9785 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009786 break;
9787 case IORING_UNREGISTER_EVENTFD:
9788 ret = -EINVAL;
9789 if (arg || nr_args)
9790 break;
9791 ret = io_eventfd_unregister(ctx);
9792 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009793 case IORING_REGISTER_PROBE:
9794 ret = -EINVAL;
9795 if (!arg || nr_args > 256)
9796 break;
9797 ret = io_probe(ctx, arg, nr_args);
9798 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009799 case IORING_REGISTER_PERSONALITY:
9800 ret = -EINVAL;
9801 if (arg || nr_args)
9802 break;
9803 ret = io_register_personality(ctx);
9804 break;
9805 case IORING_UNREGISTER_PERSONALITY:
9806 ret = -EINVAL;
9807 if (arg)
9808 break;
9809 ret = io_unregister_personality(ctx, nr_args);
9810 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009811 case IORING_REGISTER_ENABLE_RINGS:
9812 ret = -EINVAL;
9813 if (arg || nr_args)
9814 break;
9815 ret = io_register_enable_rings(ctx);
9816 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009817 case IORING_REGISTER_RESTRICTIONS:
9818 ret = io_register_restrictions(ctx, arg, nr_args);
9819 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009820 default:
9821 ret = -EINVAL;
9822 break;
9823 }
9824
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009825out:
Jens Axboe071698e2020-01-28 10:04:42 -07009826 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009827 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009828 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009829out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009830 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009831 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009832 return ret;
9833}
9834
9835SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9836 void __user *, arg, unsigned int, nr_args)
9837{
9838 struct io_ring_ctx *ctx;
9839 long ret = -EBADF;
9840 struct fd f;
9841
9842 f = fdget(fd);
9843 if (!f.file)
9844 return -EBADF;
9845
9846 ret = -EOPNOTSUPP;
9847 if (f.file->f_op != &io_uring_fops)
9848 goto out_fput;
9849
9850 ctx = f.file->private_data;
9851
9852 mutex_lock(&ctx->uring_lock);
9853 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9854 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009855 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9856 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009857out_fput:
9858 fdput(f);
9859 return ret;
9860}
9861
Jens Axboe2b188cc2019-01-07 10:46:33 -07009862static int __init io_uring_init(void)
9863{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009864#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9865 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9866 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9867} while (0)
9868
9869#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9870 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9871 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9872 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9873 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9874 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9875 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9876 BUILD_BUG_SQE_ELEM(8, __u64, off);
9877 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9878 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009879 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009880 BUILD_BUG_SQE_ELEM(24, __u32, len);
9881 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9882 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9883 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9884 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009885 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9886 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009887 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9888 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9889 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9890 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9891 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9892 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9893 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9894 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009895 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009896 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9897 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9898 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009899 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009900
Jens Axboed3656342019-12-18 09:50:26 -07009901 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009902 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009903 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9904 return 0;
9905};
9906__initcall(io_uring_init);