blob: 6c356b9e87b39126710c89bb6c341e2b00a68fb7 [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
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300544struct io_completion {
545 struct file *file;
546 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300547 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300548};
549
Jens Axboef499a022019-12-02 16:28:46 -0700550struct io_async_connect {
551 struct sockaddr_storage address;
552};
553
Jens Axboe03b12302019-12-02 18:50:25 -0700554struct io_async_msghdr {
555 struct iovec fast_iov[UIO_FASTIOV];
556 struct iovec *iov;
557 struct sockaddr __user *uaddr;
558 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700559 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700560};
561
Jens Axboef67676d2019-12-02 11:03:47 -0700562struct io_async_rw {
563 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600564 const struct iovec *free_iovec;
565 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600566 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600567 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700568};
569
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300570enum {
571 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
572 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
573 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
574 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
575 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700576 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300578 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300579 REQ_F_FAIL_LINK_BIT,
580 REQ_F_INFLIGHT_BIT,
581 REQ_F_CUR_POS_BIT,
582 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300583 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300584 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300585 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700586 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700587 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600588 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800589 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100590 REQ_F_LTIMEOUT_ACTIVE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700591
592 /* not a real bit, just to check we're not overflowing the space */
593 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300594};
595
596enum {
597 /* ctx owns file */
598 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
599 /* drain existing IO first */
600 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
601 /* linked sqes */
602 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
603 /* doesn't sever on completion < 0 */
604 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
605 /* IOSQE_ASYNC */
606 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700607 /* IOSQE_BUFFER_SELECT */
608 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300610 /* head of a link */
611 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300612 /* fail rest of links */
613 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
614 /* on inflight list */
615 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
616 /* read/write uses file position */
617 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
618 /* must not punt to workers */
619 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100620 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300621 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300622 /* regular file */
623 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300624 /* needs cleanup */
625 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700626 /* already went through poll handler */
627 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700628 /* buffer already selected */
629 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600630 /* doesn't need file table for this request */
631 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800632 /* io_wq_work is initialized */
633 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100634 /* linked timeout is active, i.e. prepared by link's head */
635 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700636};
637
638struct async_poll {
639 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600640 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300641};
642
Jens Axboe09bb8392019-03-13 12:39:28 -0600643/*
644 * NOTE! Each of the iocb union members has the file pointer
645 * as the first entry in their struct definition. So you can
646 * access the file pointer through any of the sub-structs,
647 * or directly as just 'ki_filp' in this struct.
648 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700650 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600651 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700652 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700653 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700654 struct io_accept accept;
655 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700656 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700657 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100658 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700659 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700660 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700661 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700662 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700663 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700664 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700665 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700666 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300667 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700668 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700669 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300670 /* use only after cleaning per-op data, see io_clean_op() */
671 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700672 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700673
Jens Axboee8c2bc12020-08-15 18:44:09 -0700674 /* opcode allocated if it needs to store data for async defer */
675 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700676 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800677 /* polled IO has completed */
678 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700679
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700680 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300681 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700682
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300683 struct io_ring_ctx *ctx;
684 unsigned int flags;
685 refcount_t refs;
686 struct task_struct *task;
687 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700688
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300689 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700690
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300691 /*
692 * 1. used with ctx->iopoll_list with reads/writes
693 * 2. to track reqs with ->files (see io_op_def::file_table)
694 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300695 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600696
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300697 struct percpu_ref *fixed_file_refs;
698 struct callback_head task_work;
699 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
700 struct hlist_node hash_node;
701 struct async_poll *apoll;
702 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700703};
704
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300705struct io_defer_entry {
706 struct list_head list;
707 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300708 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300709};
710
Jens Axboedef596e2019-01-09 08:59:42 -0700711#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700712
Jens Axboe013538b2020-06-22 09:29:15 -0600713struct io_comp_state {
714 unsigned int nr;
715 struct list_head list;
716 struct io_ring_ctx *ctx;
717};
718
Jens Axboe9a56a232019-01-09 09:06:50 -0700719struct io_submit_state {
720 struct blk_plug plug;
721
722 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700723 * io_kiocb alloc cache
724 */
725 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300726 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700727
728 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600729 * Batch completion logic
730 */
731 struct io_comp_state comp;
732
733 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700734 * File reference cache
735 */
736 struct file *file;
737 unsigned int fd;
738 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700739 unsigned int ios_left;
740};
741
Jens Axboed3656342019-12-18 09:50:26 -0700742struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700743 /* needs req->file assigned */
744 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600745 /* don't fail if file grab fails */
746 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700747 /* hash wq insertion if file is a regular file */
748 unsigned hash_reg_file : 1;
749 /* unbound wq insertion if file is a non-regular file */
750 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700751 /* opcode is not supported by this kernel */
752 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700753 /* set if opcode supports polled "wait" */
754 unsigned pollin : 1;
755 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700756 /* op supports buffer selection */
757 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700758 /* must always have async data allocated */
759 unsigned needs_async_data : 1;
760 /* size of async data needed, if any */
761 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600762 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700763};
764
Jens Axboe09186822020-10-13 15:01:40 -0600765static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300766 [IORING_OP_NOP] = {},
767 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700768 .needs_file = 1,
769 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700770 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700771 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700772 .needs_async_data = 1,
773 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600774 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700777 .needs_file = 1,
778 .hash_reg_file = 1,
779 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700780 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700781 .needs_async_data = 1,
782 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600783 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
784 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700785 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300786 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700787 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600788 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700789 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300790 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700791 .needs_file = 1,
792 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700793 .pollin = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700794 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600795 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700798 .needs_file = 1,
799 .hash_reg_file = 1,
800 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700801 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700802 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600803 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
804 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700805 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300806 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700807 .needs_file = 1,
808 .unbound_nonreg_file = 1,
809 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300810 [IORING_OP_POLL_REMOVE] = {},
811 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700812 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600813 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700814 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300815 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700816 .needs_file = 1,
817 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700818 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700819 .needs_async_data = 1,
820 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600821 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
822 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700823 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300824 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700825 .needs_file = 1,
826 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700827 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700828 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700829 .needs_async_data = 1,
830 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe0f203762020-10-14 09:23:55 -0600831 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
832 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700833 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700835 .needs_async_data = 1,
836 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600837 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700838 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300839 [IORING_OP_TIMEOUT_REMOVE] = {},
840 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700841 .needs_file = 1,
842 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700843 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600844 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_ASYNC_CANCEL] = {},
847 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700848 .needs_async_data = 1,
849 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600850 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700853 .needs_file = 1,
854 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700855 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700856 .needs_async_data = 1,
857 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600858 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700861 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600862 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600865 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
866 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700867 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300868 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600869 .needs_file = 1,
870 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600871 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700872 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300873 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600874 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700875 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300876 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600877 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
878 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700879 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300880 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700881 .needs_file = 1,
882 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700883 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700884 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700885 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600886 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700889 .needs_file = 1,
890 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700891 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700892 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600893 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
894 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700895 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700897 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600898 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700899 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300900 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600901 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700902 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300903 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700904 .needs_file = 1,
905 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700906 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600907 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700908 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300909 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700910 .needs_file = 1,
911 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700912 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700913 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600914 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700915 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300916 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600917 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
918 IO_WQ_WORK_BLKCG,
Jens Axboecebdb982020-01-08 17:59:24 -0700919 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700920 [IORING_OP_EPOLL_CTL] = {
921 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600922 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700923 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300924 [IORING_OP_SPLICE] = {
925 .needs_file = 1,
926 .hash_reg_file = 1,
927 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600928 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700929 },
930 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700931 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300932 [IORING_OP_TEE] = {
933 .needs_file = 1,
934 .hash_reg_file = 1,
935 .unbound_nonreg_file = 1,
936 },
Jens Axboed3656342019-12-18 09:50:26 -0700937};
938
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700939enum io_mem_account {
940 ACCT_LOCKED,
941 ACCT_PINNED,
942};
943
Pavel Begunkovce00a7d2020-12-30 21:34:15 +0000944static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node);
945static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
946 struct io_ring_ctx *ctx);
947
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300948static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
949 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700950static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800951static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +0100952static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -0600953static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700954static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600955static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700956static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700957static int __io_sqe_files_update(struct io_ring_ctx *ctx,
958 struct io_uring_files_update *ip,
959 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300960static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +0100961static struct file *io_file_get(struct io_submit_state *state,
962 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +0300963static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600964static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600965
Jens Axboeb63534c2020-06-04 11:28:00 -0600966static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
967 struct iovec **iovec, struct iov_iter *iter,
968 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600969static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
970 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600971 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700972
973static struct kmem_cache *req_cachep;
974
Jens Axboe09186822020-10-13 15:01:40 -0600975static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700976
977struct sock *io_uring_get_socket(struct file *file)
978{
979#if defined(CONFIG_UNIX)
980 if (file->f_op == &io_uring_fops) {
981 struct io_ring_ctx *ctx = file->private_data;
982
983 return ctx->ring_sock->sk;
984 }
985#endif
986 return NULL;
987}
988EXPORT_SYMBOL(io_uring_get_socket);
989
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300990static inline void io_clean_op(struct io_kiocb *req)
991{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300992 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
993 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300994 __io_clean_op(req);
995}
996
Jens Axboe4349f302020-07-09 15:07:01 -0600997static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600998{
999 struct mm_struct *mm = current->mm;
1000
1001 if (mm) {
1002 kthread_unuse_mm(mm);
1003 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001004 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001005 }
1006}
1007
1008static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1009{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001010 struct mm_struct *mm;
1011
Pavel Begunkova3647cd2021-01-11 04:00:31 +00001012 if (current->flags & PF_EXITING)
1013 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001014 if (current->mm)
1015 return 0;
1016
1017 /* Should never happen */
1018 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1019 return -EFAULT;
1020
1021 task_lock(ctx->sqo_task);
1022 mm = ctx->sqo_task->mm;
1023 if (unlikely(!mm || !mmget_not_zero(mm)))
1024 mm = NULL;
1025 task_unlock(ctx->sqo_task);
1026
1027 if (mm) {
1028 kthread_use_mm(mm);
1029 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001030 }
1031
Jens Axboe4b70cf92020-11-02 10:39:05 -07001032 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001033}
1034
1035static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1036 struct io_kiocb *req)
1037{
Jens Axboe0f203762020-10-14 09:23:55 -06001038 if (!(io_op_defs[req->opcode].work_flags & IO_WQ_WORK_MM))
Jens Axboec40f6372020-06-25 15:39:59 -06001039 return 0;
1040 return __io_sq_thread_acquire_mm(ctx);
1041}
1042
Dennis Zhou91d8f512020-09-16 13:41:05 -07001043static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1044 struct cgroup_subsys_state **cur_css)
1045
1046{
1047#ifdef CONFIG_BLK_CGROUP
1048 /* puts the old one when swapping */
1049 if (*cur_css != ctx->sqo_blkcg_css) {
1050 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1051 *cur_css = ctx->sqo_blkcg_css;
1052 }
1053#endif
1054}
1055
1056static void io_sq_thread_unassociate_blkcg(void)
1057{
1058#ifdef CONFIG_BLK_CGROUP
1059 kthread_associate_blkcg(NULL);
1060#endif
1061}
1062
Jens Axboec40f6372020-06-25 15:39:59 -06001063static inline void req_set_fail_links(struct io_kiocb *req)
1064{
1065 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1066 req->flags |= REQ_F_FAIL_LINK;
1067}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001068
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001069/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001070 * None of these are dereferenced, they are simply used to check if any of
1071 * them have changed. If we're under current and check they are still the
1072 * same, we're fine to grab references to them for actual out-of-line use.
1073 */
1074static void io_init_identity(struct io_identity *id)
1075{
1076 id->files = current->files;
1077 id->mm = current->mm;
1078#ifdef CONFIG_BLK_CGROUP
1079 rcu_read_lock();
1080 id->blkcg_css = blkcg_css();
1081 rcu_read_unlock();
1082#endif
1083 id->creds = current_cred();
1084 id->nsproxy = current->nsproxy;
1085 id->fs = current->fs;
1086 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001087#ifdef CONFIG_AUDIT
1088 id->loginuid = current->loginuid;
1089 id->sessionid = current->sessionid;
1090#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001091 refcount_set(&id->count, 1);
1092}
1093
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001094static inline void __io_req_init_async(struct io_kiocb *req)
1095{
1096 memset(&req->work, 0, sizeof(req->work));
1097 req->flags |= REQ_F_WORK_INITIALIZED;
1098}
1099
Jens Axboe1e6fa522020-10-15 08:46:24 -06001100/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001101 * Note: must call io_req_init_async() for the first time you
1102 * touch any members of io_wq_work.
1103 */
1104static inline void io_req_init_async(struct io_kiocb *req)
1105{
Jens Axboe500a3732020-10-15 17:38:03 -06001106 struct io_uring_task *tctx = current->io_uring;
1107
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001108 if (req->flags & REQ_F_WORK_INITIALIZED)
1109 return;
1110
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001111 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001112
1113 /* Grab a ref if this isn't our static identity */
1114 req->work.identity = tctx->identity;
1115 if (tctx->identity != &tctx->__identity)
1116 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001117}
1118
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001119static inline bool io_async_submit(struct io_ring_ctx *ctx)
1120{
1121 return ctx->flags & IORING_SETUP_SQPOLL;
1122}
1123
Jens Axboe2b188cc2019-01-07 10:46:33 -07001124static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1125{
1126 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1127
Jens Axboe0f158b42020-05-14 17:18:39 -06001128 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129}
1130
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001131static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1132{
1133 return !req->timeout.off;
1134}
1135
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1137{
1138 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001139 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001140
1141 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1142 if (!ctx)
1143 return NULL;
1144
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001145 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1146 if (!ctx->fallback_req)
1147 goto err;
1148
Jens Axboe78076bb2019-12-04 19:56:40 -07001149 /*
1150 * Use 5 bits less than the max cq entries, that should give us around
1151 * 32 entries per hash list if totally full and uniformly spread.
1152 */
1153 hash_bits = ilog2(p->cq_entries);
1154 hash_bits -= 5;
1155 if (hash_bits <= 0)
1156 hash_bits = 1;
1157 ctx->cancel_hash_bits = hash_bits;
1158 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1159 GFP_KERNEL);
1160 if (!ctx->cancel_hash)
1161 goto err;
1162 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1163
Roman Gushchin21482892019-05-07 10:01:48 -07001164 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001165 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1166 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167
1168 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001169 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001170 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001172 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001173 init_completion(&ctx->ref_comp);
1174 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001175 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001176 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177 mutex_init(&ctx->uring_lock);
1178 init_waitqueue_head(&ctx->wait);
1179 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001180 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001181 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001182 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001183 init_waitqueue_head(&ctx->inflight_wait);
1184 spin_lock_init(&ctx->inflight_lock);
1185 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001186 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1187 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001188 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001189err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001190 if (ctx->fallback_req)
1191 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001192 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001193 kfree(ctx);
1194 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001195}
1196
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001197static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001198{
Jens Axboe2bc99302020-07-09 09:43:27 -06001199 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1200 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001201
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001202 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001203 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001204 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001205
Bob Liu9d858b22019-11-13 18:06:25 +08001206 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001207}
1208
Jens Axboede0617e2019-04-06 21:51:27 -06001209static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001210{
Hristo Venev75b28af2019-08-26 17:23:46 +00001211 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001212
Pavel Begunkov07910152020-01-17 03:52:46 +03001213 /* order cqe stores with ring update */
1214 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215
Pavel Begunkov07910152020-01-17 03:52:46 +03001216 if (wq_has_sleeper(&ctx->cq_wait)) {
1217 wake_up_interruptible(&ctx->cq_wait);
1218 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219 }
1220}
1221
Jens Axboe5c3462c2020-10-15 09:02:33 -06001222static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001223{
Jens Axboe500a3732020-10-15 17:38:03 -06001224 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001225 return;
1226 if (refcount_dec_and_test(&req->work.identity->count))
1227 kfree(req->work.identity);
1228}
1229
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001230static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001231{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001232 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001233 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001234
1235 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001236
Jens Axboedfead8a2020-10-14 10:12:37 -06001237 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboe98447d62020-10-14 10:48:51 -06001238 mmdrop(req->work.identity->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001239 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001240 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001241#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001242 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Jens Axboe98447d62020-10-14 10:48:51 -06001243 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001244 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001245 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001246#endif
1247 if (req->work.flags & IO_WQ_WORK_CREDS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001248 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001249 req->work.flags &= ~IO_WQ_WORK_CREDS;
1250 }
1251 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001252 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001253
Jens Axboe98447d62020-10-14 10:48:51 -06001254 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001255 if (--fs->users)
1256 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001257 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001258 if (fs)
1259 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001260 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001261 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001262
Jens Axboe5c3462c2020-10-15 09:02:33 -06001263 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001264}
1265
1266/*
1267 * Create a private copy of io_identity, since some fields don't match
1268 * the current context.
1269 */
1270static bool io_identity_cow(struct io_kiocb *req)
1271{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001272 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001273 const struct cred *creds = NULL;
1274 struct io_identity *id;
1275
1276 if (req->work.flags & IO_WQ_WORK_CREDS)
1277 creds = req->work.identity->creds;
1278
1279 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1280 if (unlikely(!id)) {
1281 req->work.flags |= IO_WQ_WORK_CANCEL;
1282 return false;
1283 }
1284
1285 /*
1286 * We can safely just re-init the creds we copied Either the field
1287 * matches the current one, or we haven't grabbed it yet. The only
1288 * exception is ->creds, through registered personalities, so handle
1289 * that one separately.
1290 */
1291 io_init_identity(id);
1292 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001293 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001294
1295 /* add one for this request */
1296 refcount_inc(&id->count);
1297
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001298 /* drop tctx and req identity references, if needed */
1299 if (tctx->identity != &tctx->__identity &&
1300 refcount_dec_and_test(&tctx->identity->count))
1301 kfree(tctx->identity);
1302 if (req->work.identity != &tctx->__identity &&
1303 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001304 kfree(req->work.identity);
1305
1306 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001307 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001308 return true;
1309}
1310
1311static bool io_grab_identity(struct io_kiocb *req)
1312{
1313 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001314 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001315 struct io_ring_ctx *ctx = req->ctx;
1316
Jens Axboe69228332020-10-20 14:28:41 -06001317 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1318 if (id->fsize != rlimit(RLIMIT_FSIZE))
1319 return false;
1320 req->work.flags |= IO_WQ_WORK_FSIZE;
1321 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001322#ifdef CONFIG_BLK_CGROUP
1323 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1324 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1325 rcu_read_lock();
1326 if (id->blkcg_css != blkcg_css()) {
1327 rcu_read_unlock();
1328 return false;
1329 }
1330 /*
1331 * This should be rare, either the cgroup is dying or the task
1332 * is moving cgroups. Just punt to root for the handful of ios.
1333 */
1334 if (css_tryget_online(id->blkcg_css))
1335 req->work.flags |= IO_WQ_WORK_BLKCG;
1336 rcu_read_unlock();
1337 }
1338#endif
1339 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1340 if (id->creds != current_cred())
1341 return false;
1342 get_cred(id->creds);
1343 req->work.flags |= IO_WQ_WORK_CREDS;
1344 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001345#ifdef CONFIG_AUDIT
1346 if (!uid_eq(current->loginuid, id->loginuid) ||
1347 current->sessionid != id->sessionid)
1348 return false;
1349#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001350 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1351 (def->work_flags & IO_WQ_WORK_FS)) {
1352 if (current->fs != id->fs)
1353 return false;
1354 spin_lock(&id->fs->lock);
1355 if (!id->fs->in_exec) {
1356 id->fs->users++;
1357 req->work.flags |= IO_WQ_WORK_FS;
1358 } else {
1359 req->work.flags |= IO_WQ_WORK_CANCEL;
1360 }
1361 spin_unlock(&current->fs->lock);
1362 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001363 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1364 (def->work_flags & IO_WQ_WORK_FILES) &&
1365 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1366 if (id->files != current->files ||
1367 id->nsproxy != current->nsproxy)
1368 return false;
1369 atomic_inc(&id->files->count);
1370 get_nsproxy(id->nsproxy);
1371 req->flags |= REQ_F_INFLIGHT;
1372
1373 spin_lock_irq(&ctx->inflight_lock);
1374 list_add(&req->inflight_entry, &ctx->inflight_list);
1375 spin_unlock_irq(&ctx->inflight_lock);
1376 req->work.flags |= IO_WQ_WORK_FILES;
1377 }
Jens Axboe7247bc62020-12-29 10:50:46 -07001378 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1379 (def->work_flags & IO_WQ_WORK_MM)) {
1380 if (id->mm != current->mm)
1381 return false;
1382 mmgrab(id->mm);
1383 req->work.flags |= IO_WQ_WORK_MM;
1384 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001385
1386 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001387}
1388
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001389static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001390{
Jens Axboed3656342019-12-18 09:50:26 -07001391 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001392 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5c3462c2020-10-15 09:02:33 -06001393 struct io_identity *id;
Jens Axboe54a91f32019-09-10 09:15:04 -06001394
Pavel Begunkov16d59802020-07-12 16:16:47 +03001395 io_req_init_async(req);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001396 id = req->work.identity;
Pavel Begunkov16d59802020-07-12 16:16:47 +03001397
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001398 if (req->flags & REQ_F_FORCE_ASYNC)
1399 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1400
Jens Axboed3656342019-12-18 09:50:26 -07001401 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001402 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001403 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001404 } else {
1405 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001406 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001407 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001408
Jens Axboe1e6fa522020-10-15 08:46:24 -06001409 /* if we fail grabbing identity, we must COW, regrab, and retry */
1410 if (io_grab_identity(req))
1411 return;
1412
1413 if (!io_identity_cow(req))
1414 return;
1415
1416 /* can't fail at this point */
1417 if (!io_grab_identity(req))
1418 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001419}
1420
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001421static void io_prep_async_link(struct io_kiocb *req)
1422{
1423 struct io_kiocb *cur;
1424
1425 io_prep_async_work(req);
1426 if (req->flags & REQ_F_LINK_HEAD)
1427 list_for_each_entry(cur, &req->link_list, link_list)
1428 io_prep_async_work(cur);
1429}
1430
Jens Axboe7271ef32020-08-10 09:55:22 -06001431static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001432{
Jackie Liua197f662019-11-08 08:09:12 -07001433 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001434 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001435
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001436 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1437 &req->work, req->flags);
1438 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001439 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001440}
1441
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001442static void io_queue_async_work(struct io_kiocb *req)
1443{
Jens Axboe7271ef32020-08-10 09:55:22 -06001444 struct io_kiocb *link;
1445
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001446 /* init ->work of the whole link before punting */
1447 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001448 link = __io_queue_async_work(req);
1449
1450 if (link)
1451 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001452}
1453
Jens Axboe5262f562019-09-17 12:26:57 -06001454static void io_kill_timeout(struct io_kiocb *req)
1455{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001456 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001457 int ret;
1458
Jens Axboee8c2bc12020-08-15 18:44:09 -07001459 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001460 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001461 atomic_set(&req->ctx->cq_timeouts,
1462 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001463 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001464 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001465 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001466 }
1467}
1468
Jens Axboef3606e32020-09-22 08:18:24 -06001469static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1470{
1471 struct io_ring_ctx *ctx = req->ctx;
1472
1473 if (!tsk || req->task == tsk)
1474 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001475 if (ctx->flags & IORING_SETUP_SQPOLL) {
1476 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1477 return true;
1478 }
Jens Axboef3606e32020-09-22 08:18:24 -06001479 return false;
1480}
1481
Jens Axboe76e1b642020-09-26 15:05:03 -06001482/*
1483 * Returns true if we found and killed one or more timeouts
1484 */
1485static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001486{
1487 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001488 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001489
1490 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001491 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001492 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001493 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001494 canceled++;
1495 }
Jens Axboef3606e32020-09-22 08:18:24 -06001496 }
Jens Axboe5262f562019-09-17 12:26:57 -06001497 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001498 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001499}
1500
Pavel Begunkov04518942020-05-26 20:34:05 +03001501static void __io_queue_deferred(struct io_ring_ctx *ctx)
1502{
1503 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001504 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1505 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001506 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001507
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001508 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001509 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001510 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001511 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001512 link = __io_queue_async_work(de->req);
1513 if (link) {
1514 __io_queue_linked_timeout(link);
1515 /* drop submission reference */
Pavel Begunkov216578e2020-10-13 09:44:00 +01001516 io_put_req_deferred(link, 1);
Jens Axboe7271ef32020-08-10 09:55:22 -06001517 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001518 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001519 } while (!list_empty(&ctx->defer_list));
1520}
1521
Pavel Begunkov360428f2020-05-30 14:54:17 +03001522static void io_flush_timeouts(struct io_ring_ctx *ctx)
1523{
1524 while (!list_empty(&ctx->timeout_list)) {
1525 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001526 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001527
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001528 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001529 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001530 if (req->timeout.target_seq != ctx->cached_cq_tail
1531 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001532 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001533
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001534 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001535 io_kill_timeout(req);
1536 }
1537}
1538
Jens Axboede0617e2019-04-06 21:51:27 -06001539static void io_commit_cqring(struct io_ring_ctx *ctx)
1540{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001541 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001542 __io_commit_cqring(ctx);
1543
Pavel Begunkov04518942020-05-26 20:34:05 +03001544 if (unlikely(!list_empty(&ctx->defer_list)))
1545 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001546}
1547
Jens Axboe90554202020-09-03 12:12:41 -06001548static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1549{
1550 struct io_rings *r = ctx->rings;
1551
1552 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1553}
1554
Jens Axboe2b188cc2019-01-07 10:46:33 -07001555static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1556{
Hristo Venev75b28af2019-08-26 17:23:46 +00001557 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001558 unsigned tail;
1559
1560 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001561 /*
1562 * writes to the cq entry need to come after reading head; the
1563 * control dependency is enough as we're using WRITE_ONCE to
1564 * fill the cq entry
1565 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001566 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001567 return NULL;
1568
1569 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001570 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001571}
1572
Jens Axboef2842ab2020-01-08 11:04:00 -07001573static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1574{
Jens Axboef0b493e2020-02-01 21:30:11 -07001575 if (!ctx->cq_ev_fd)
1576 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001577 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1578 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001579 if (!ctx->eventfd_async)
1580 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001581 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001582}
1583
Jens Axboeb41e9852020-02-17 09:52:41 -07001584static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001585{
1586 if (waitqueue_active(&ctx->wait))
1587 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001588 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1589 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001590 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001591 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001592}
1593
Pavel Begunkov46930142020-07-30 18:43:49 +03001594static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1595{
1596 if (list_empty(&ctx->cq_overflow_list)) {
1597 clear_bit(0, &ctx->sq_check_overflow);
1598 clear_bit(0, &ctx->cq_check_overflow);
1599 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1600 }
1601}
1602
Pavel Begunkov99b32802020-11-04 13:39:31 +00001603static inline bool __io_match_files(struct io_kiocb *req,
1604 struct files_struct *files)
Jens Axboee6c8aa92020-09-28 13:10:13 -06001605{
Pavel Begunkov99b32802020-11-04 13:39:31 +00001606 return ((req->flags & REQ_F_WORK_INITIALIZED) &&
1607 (req->work.flags & IO_WQ_WORK_FILES)) &&
1608 req->work.identity->files == files;
1609}
1610
1611static bool io_match_files(struct io_kiocb *req,
1612 struct files_struct *files)
1613{
1614 struct io_kiocb *link;
1615
Jens Axboee6c8aa92020-09-28 13:10:13 -06001616 if (!files)
1617 return true;
Pavel Begunkov99b32802020-11-04 13:39:31 +00001618 if (__io_match_files(req, files))
1619 return true;
1620 if (req->flags & REQ_F_LINK_HEAD) {
1621 list_for_each_entry(link, &req->link_list, link_list) {
1622 if (__io_match_files(link, files))
1623 return true;
1624 }
1625 }
Jens Axboee6c8aa92020-09-28 13:10:13 -06001626 return false;
1627}
1628
Jens Axboec4a2ed72019-11-21 21:01:26 -07001629/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00001630static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1631 struct task_struct *tsk,
1632 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001633{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001634 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001635 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001636 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001637 unsigned long flags;
1638 LIST_HEAD(list);
1639
1640 if (!force) {
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001641 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1642 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001643 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001644 }
1645
1646 spin_lock_irqsave(&ctx->completion_lock, flags);
1647
Jens Axboec4a2ed72019-11-21 21:01:26 -07001648 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001649 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1650 if (tsk && req->task != tsk)
1651 continue;
1652 if (!io_match_files(req, files))
1653 continue;
1654
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001655 cqe = io_get_cqring(ctx);
1656 if (!cqe && !force)
1657 break;
1658
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001659 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001660 if (cqe) {
1661 WRITE_ONCE(cqe->user_data, req->user_data);
1662 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001663 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001664 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001665 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001666 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001667 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001668 }
1669 }
1670
1671 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001672 io_cqring_mark_overflow(ctx);
1673
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001674 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1675 io_cqring_ev_posted(ctx);
1676
1677 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001678 req = list_first_entry(&list, struct io_kiocb, compl.list);
1679 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001680 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001681 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001682
1683 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001684}
1685
Pavel Begunkov85e25e22021-01-12 21:17:26 +00001686static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1687 struct task_struct *tsk,
1688 struct files_struct *files)
1689{
1690 if (test_bit(0, &ctx->cq_check_overflow)) {
1691 /* iopoll syncs against uring_lock, not completion_lock */
1692 if (ctx->flags & IORING_SETUP_IOPOLL)
1693 mutex_lock(&ctx->uring_lock);
1694 __io_cqring_overflow_flush(ctx, force, tsk, files);
1695 if (ctx->flags & IORING_SETUP_IOPOLL)
1696 mutex_unlock(&ctx->uring_lock);
1697 }
1698}
1699
Jens Axboebcda7ba2020-02-23 16:42:51 -07001700static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001701{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001702 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001703 struct io_uring_cqe *cqe;
1704
Jens Axboe78e19bb2019-11-06 15:21:34 -07001705 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001706
Jens Axboe2b188cc2019-01-07 10:46:33 -07001707 /*
1708 * If we can't get a cq entry, userspace overflowed the
1709 * submission (by quite a lot). Increment the overflow count in
1710 * the ring.
1711 */
1712 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001713 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001714 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001716 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001717 } else if (ctx->cq_overflow_flushed ||
1718 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001719 /*
1720 * If we're in ring overflow flush mode, or in task cancel mode,
1721 * then we cannot store the request for later flushing, we need
1722 * to drop it on the floor.
1723 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001724 ctx->cached_cq_overflow++;
1725 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001726 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001727 if (list_empty(&ctx->cq_overflow_list)) {
1728 set_bit(0, &ctx->sq_check_overflow);
1729 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001730 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001731 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001732 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001733 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001734 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001735 refcount_inc(&req->refs);
1736 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737 }
1738}
1739
Jens Axboebcda7ba2020-02-23 16:42:51 -07001740static void io_cqring_fill_event(struct io_kiocb *req, long res)
1741{
1742 __io_cqring_fill_event(req, res, 0);
1743}
1744
Jens Axboee1e16092020-06-22 09:17:17 -06001745static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001747 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001748 unsigned long flags;
1749
1750 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001751 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001752 io_commit_cqring(ctx);
1753 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1754
Jens Axboe8c838782019-03-12 15:48:16 -06001755 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756}
1757
Jens Axboe229a7b62020-06-22 10:13:11 -06001758static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001759{
Jens Axboe229a7b62020-06-22 10:13:11 -06001760 struct io_ring_ctx *ctx = cs->ctx;
1761
1762 spin_lock_irq(&ctx->completion_lock);
1763 while (!list_empty(&cs->list)) {
1764 struct io_kiocb *req;
1765
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001766 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1767 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001768 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001769
1770 /*
1771 * io_free_req() doesn't care about completion_lock unless one
1772 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1773 * because of a potential deadlock with req->work.fs->lock
1774 */
1775 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1776 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001777 spin_unlock_irq(&ctx->completion_lock);
1778 io_put_req(req);
1779 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001780 } else {
1781 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001782 }
1783 }
1784 io_commit_cqring(ctx);
1785 spin_unlock_irq(&ctx->completion_lock);
1786
1787 io_cqring_ev_posted(ctx);
1788 cs->nr = 0;
1789}
1790
1791static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1792 struct io_comp_state *cs)
1793{
1794 if (!cs) {
1795 io_cqring_add_event(req, res, cflags);
1796 io_put_req(req);
1797 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001798 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001799 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001800 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001801 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001802 if (++cs->nr >= 32)
1803 io_submit_flush_completions(cs);
1804 }
Jens Axboee1e16092020-06-22 09:17:17 -06001805}
1806
1807static void io_req_complete(struct io_kiocb *req, long res)
1808{
Jens Axboe229a7b62020-06-22 10:13:11 -06001809 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001810}
1811
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001812static inline bool io_is_fallback_req(struct io_kiocb *req)
1813{
1814 return req == (struct io_kiocb *)
1815 ((unsigned long) req->ctx->fallback_req & ~1UL);
1816}
1817
1818static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1819{
1820 struct io_kiocb *req;
1821
1822 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001823 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001824 return req;
1825
1826 return NULL;
1827}
1828
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001829static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1830 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001831{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001832 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001833 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001834 size_t sz;
1835 int ret;
1836
1837 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001838 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1839
1840 /*
1841 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1842 * retry single alloc to be on the safe side.
1843 */
1844 if (unlikely(ret <= 0)) {
1845 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1846 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001847 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001848 ret = 1;
1849 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001850 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001851 }
1852
Pavel Begunkov291b2822020-09-30 22:57:01 +03001853 state->free_reqs--;
1854 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001855fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001856 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857}
1858
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001859static inline void io_put_file(struct io_kiocb *req, struct file *file,
1860 bool fixed)
1861{
1862 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001863 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001864 else
1865 fput(file);
1866}
1867
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001868static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001869{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001870 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001871
Jens Axboee8c2bc12020-08-15 18:44:09 -07001872 if (req->async_data)
1873 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001874 if (req->file)
1875 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001876
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001877 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001878}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001879
Pavel Begunkov216578e2020-10-13 09:44:00 +01001880static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001881{
Jens Axboe0f212202020-09-13 13:09:39 -06001882 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001883 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001884
Pavel Begunkov216578e2020-10-13 09:44:00 +01001885 io_dismantle_req(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001886
Jens Axboed8a6df12020-10-15 16:24:45 -06001887 percpu_counter_dec(&tctx->inflight);
Jens Axboefdaf0832020-10-30 09:37:30 -06001888 if (atomic_read(&tctx->in_idle))
Jens Axboe0f212202020-09-13 13:09:39 -06001889 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001890 put_task_struct(req->task);
1891
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001892 if (likely(!io_is_fallback_req(req)))
1893 kmem_cache_free(req_cachep, req);
1894 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001895 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1896 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001897}
1898
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001899static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001900{
Jackie Liua197f662019-11-08 08:09:12 -07001901 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001902 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001903 bool cancelled = false;
1904 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001905
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001906 spin_lock_irqsave(&ctx->completion_lock, flags);
1907 link = list_first_entry_or_null(&req->link_list, struct io_kiocb,
1908 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01001909 /*
1910 * Can happen if a linked timeout fired and link had been like
1911 * req -> link t-out -> link t-out [-> ...]
1912 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001913 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1914 struct io_timeout_data *io = link->async_data;
1915 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001916
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001917 list_del_init(&link->link_list);
1918 ret = hrtimer_try_to_cancel(&io->timer);
1919 if (ret != -1) {
1920 io_cqring_fill_event(link, -ECANCELED);
1921 io_commit_cqring(ctx);
1922 cancelled = true;
1923 }
1924 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001925 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001926 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001927
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001928 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001929 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001930 io_put_req(link);
1931 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001932}
1933
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001934static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001935{
1936 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001937
Jens Axboe9e645e112019-05-10 16:07:28 -06001938 /*
1939 * The list should never be empty when we are called here. But could
1940 * potentially happen if the chain is messed up, check to be on the
1941 * safe side.
1942 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001943 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001944 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001945
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001946 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1947 list_del_init(&req->link_list);
1948 if (!list_empty(&nxt->link_list))
1949 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001950 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001951}
1952
1953/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001954 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001955 */
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001956static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001957{
Jens Axboe2665abf2019-11-05 12:40:47 -07001958 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001959 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001960
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001961 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001962 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001963 struct io_kiocb *link = list_first_entry(&req->link_list,
1964 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001965
Pavel Begunkov44932332019-12-05 16:16:35 +03001966 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001967 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001968
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001969 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001970
1971 /*
1972 * It's ok to free under spinlock as they're not linked anymore,
1973 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
1974 * work.fs->lock.
1975 */
1976 if (link->flags & REQ_F_WORK_INITIALIZED)
1977 io_put_req_deferred(link, 2);
1978 else
1979 io_double_put_req(link);
Jens Axboe9e645e112019-05-10 16:07:28 -06001980 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001981
1982 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001983 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001984
Jens Axboe2665abf2019-11-05 12:40:47 -07001985 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001986}
1987
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001988static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001989{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001990 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001991 if (req->flags & REQ_F_LINK_TIMEOUT)
1992 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001993
Jens Axboe9e645e112019-05-10 16:07:28 -06001994 /*
1995 * If LINK is set, we have dependent requests in this chain. If we
1996 * didn't fail this request, queue the first one up, moving any other
1997 * dependencies to the next request. In case of failure, fail the rest
1998 * of the chain.
1999 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002000 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
2001 return io_req_link_next(req);
2002 io_fail_links(req);
2003 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002004}
Jens Axboe2665abf2019-11-05 12:40:47 -07002005
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002006static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
2007{
2008 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
2009 return NULL;
2010 return __io_req_find_next(req);
2011}
2012
Jens Axboe87c43112020-09-30 21:00:14 -06002013static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06002014{
2015 struct task_struct *tsk = req->task;
2016 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002017 enum task_work_notify_mode notify;
2018 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002019
Jens Axboe6200b0a2020-09-13 14:38:30 -06002020 if (tsk->flags & PF_EXITING)
2021 return -ESRCH;
2022
Jens Axboec2c4c832020-07-01 15:37:11 -06002023 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002024 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2025 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2026 * processing task_work. There's no reliable way to tell if TWA_RESUME
2027 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002028 */
Jens Axboe91989c72020-10-16 09:02:26 -06002029 notify = TWA_NONE;
Jens Axboefd7d6de2020-08-23 11:00:37 -06002030 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06002031 notify = TWA_SIGNAL;
2032
Jens Axboe87c43112020-09-30 21:00:14 -06002033 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002034 if (!ret)
2035 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002036
Jens Axboec2c4c832020-07-01 15:37:11 -06002037 return ret;
2038}
2039
Jens Axboec40f6372020-06-25 15:39:59 -06002040static void __io_req_task_cancel(struct io_kiocb *req, int error)
2041{
2042 struct io_ring_ctx *ctx = req->ctx;
2043
2044 spin_lock_irq(&ctx->completion_lock);
2045 io_cqring_fill_event(req, error);
2046 io_commit_cqring(ctx);
2047 spin_unlock_irq(&ctx->completion_lock);
2048
2049 io_cqring_ev_posted(ctx);
2050 req_set_fail_links(req);
2051 io_double_put_req(req);
2052}
2053
2054static void io_req_task_cancel(struct callback_head *cb)
2055{
2056 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002057 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002058
2059 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002060 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002061}
2062
2063static void __io_req_task_submit(struct io_kiocb *req)
2064{
2065 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002066 bool fail;
Jens Axboec40f6372020-06-25 15:39:59 -06002067
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002068 fail = __io_sq_thread_acquire_mm(ctx);
2069 mutex_lock(&ctx->uring_lock);
2070 if (!fail)
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002071 __io_queue_sqe(req, NULL);
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002072 else
Jens Axboec40f6372020-06-25 15:39:59 -06002073 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov1d5e50d2021-01-12 21:17:24 +00002074 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002075}
2076
Jens Axboec40f6372020-06-25 15:39:59 -06002077static void io_req_task_submit(struct callback_head *cb)
2078{
2079 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002080 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002081
2082 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002083 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002084}
2085
2086static void io_req_task_queue(struct io_kiocb *req)
2087{
Jens Axboec40f6372020-06-25 15:39:59 -06002088 int ret;
2089
2090 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002091 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002092
Jens Axboe87c43112020-09-30 21:00:14 -06002093 ret = io_req_task_work_add(req, true);
Jens Axboec40f6372020-06-25 15:39:59 -06002094 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002095 struct task_struct *tsk;
2096
Jens Axboec40f6372020-06-25 15:39:59 -06002097 init_task_work(&req->task_work, io_req_task_cancel);
2098 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002099 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002100 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002101 }
Jens Axboec40f6372020-06-25 15:39:59 -06002102}
2103
Pavel Begunkovc3524382020-06-28 12:52:32 +03002104static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002105{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002106 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002107
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002108 if (nxt)
2109 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002110}
2111
Jens Axboe9e645e112019-05-10 16:07:28 -06002112static void io_free_req(struct io_kiocb *req)
2113{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002114 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002115 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002116}
2117
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002118struct req_batch {
2119 void *reqs[IO_IOPOLL_BATCH];
2120 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002121
2122 struct task_struct *task;
2123 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002124};
2125
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002126static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002127{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002128 rb->to_free = 0;
2129 rb->task_refs = 0;
2130 rb->task = NULL;
2131}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002132
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002133static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2134 struct req_batch *rb)
2135{
2136 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2137 percpu_ref_put_many(&ctx->refs, rb->to_free);
2138 rb->to_free = 0;
2139}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002140
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002141static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2142 struct req_batch *rb)
2143{
2144 if (rb->to_free)
2145 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002146 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002147 struct io_uring_task *tctx = rb->task->io_uring;
2148
2149 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002150 put_task_struct_many(rb->task, rb->task_refs);
2151 rb->task = NULL;
2152 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002153}
2154
2155static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2156{
2157 if (unlikely(io_is_fallback_req(req))) {
2158 io_free_req(req);
2159 return;
2160 }
2161 if (req->flags & REQ_F_LINK_HEAD)
2162 io_queue_next(req);
2163
Jens Axboee3bc8e92020-09-24 08:45:57 -06002164 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002165 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002166 struct io_uring_task *tctx = rb->task->io_uring;
2167
2168 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002169 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002170 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002171 rb->task = req->task;
2172 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002173 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002174 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002175
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002176 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002177 rb->reqs[rb->to_free++] = req;
2178 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2179 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002180}
2181
Jens Axboeba816ad2019-09-28 11:36:45 -06002182/*
2183 * Drop reference to request, return next in chain (if there is one) if this
2184 * was the last reference to this request.
2185 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002186static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002187{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002188 struct io_kiocb *nxt = NULL;
2189
Jens Axboe2a44f462020-02-25 13:25:41 -07002190 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002191 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002192 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002193 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002194 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002195}
2196
Jens Axboe2b188cc2019-01-07 10:46:33 -07002197static void io_put_req(struct io_kiocb *req)
2198{
Jens Axboedef596e2019-01-09 08:59:42 -07002199 if (refcount_dec_and_test(&req->refs))
2200 io_free_req(req);
2201}
2202
Pavel Begunkov216578e2020-10-13 09:44:00 +01002203static void io_put_req_deferred_cb(struct callback_head *cb)
2204{
2205 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2206
2207 io_free_req(req);
2208}
2209
2210static void io_free_req_deferred(struct io_kiocb *req)
2211{
2212 int ret;
2213
2214 init_task_work(&req->task_work, io_put_req_deferred_cb);
2215 ret = io_req_task_work_add(req, true);
2216 if (unlikely(ret)) {
2217 struct task_struct *tsk;
2218
2219 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002220 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002221 wake_up_process(tsk);
2222 }
2223}
2224
2225static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2226{
2227 if (refcount_sub_and_test(refs, &req->refs))
2228 io_free_req_deferred(req);
2229}
2230
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002231static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002232{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002233 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002234
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002235 /*
2236 * A ref is owned by io-wq in which context we're. So, if that's the
2237 * last one, it's safe to steal next work. False negatives are Ok,
2238 * it just will be re-punted async in io_put_work()
2239 */
2240 if (refcount_read(&req->refs) != 1)
2241 return NULL;
2242
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002243 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002244 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002245}
2246
Jens Axboe978db572019-11-14 22:39:04 -07002247static void io_double_put_req(struct io_kiocb *req)
2248{
2249 /* drop both submit and complete references */
2250 if (refcount_sub_and_test(2, &req->refs))
2251 io_free_req(req);
2252}
2253
Pavel Begunkov85e25e22021-01-12 21:17:26 +00002254static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002255{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002256 struct io_rings *rings = ctx->rings;
2257
Jens Axboea3a0e432019-08-20 11:03:11 -06002258 /* See comment at the top of this file */
2259 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002260 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002261}
2262
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002263static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2264{
2265 struct io_rings *rings = ctx->rings;
2266
2267 /* make sure SQ entry isn't read before tail */
2268 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2269}
2270
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002271static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002272{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002273 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002274
Jens Axboebcda7ba2020-02-23 16:42:51 -07002275 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2276 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002277 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002278 kfree(kbuf);
2279 return cflags;
2280}
2281
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002282static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2283{
2284 struct io_buffer *kbuf;
2285
2286 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2287 return io_put_kbuf(req, kbuf);
2288}
2289
Jens Axboe4c6e2772020-07-01 11:29:10 -06002290static inline bool io_run_task_work(void)
2291{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002292 /*
2293 * Not safe to run on exiting task, and the task_work handling will
2294 * not add work to such a task.
2295 */
2296 if (unlikely(current->flags & PF_EXITING))
2297 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002298 if (current->task_works) {
2299 __set_current_state(TASK_RUNNING);
2300 task_work_run();
2301 return true;
2302 }
2303
2304 return false;
2305}
2306
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002307static void io_iopoll_queue(struct list_head *again)
2308{
2309 struct io_kiocb *req;
2310
2311 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002312 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2313 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002314 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002315 } while (!list_empty(again));
2316}
2317
Jens Axboedef596e2019-01-09 08:59:42 -07002318/*
2319 * Find and free completed poll iocbs
2320 */
2321static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2322 struct list_head *done)
2323{
Jens Axboe8237e042019-12-28 10:48:22 -07002324 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002325 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002326 LIST_HEAD(again);
2327
2328 /* order with ->result store in io_complete_rw_iopoll() */
2329 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002330
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002331 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002332 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002333 int cflags = 0;
2334
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002335 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002336 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002337 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002338 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002339 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002340 continue;
2341 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002342 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002343
Jens Axboebcda7ba2020-02-23 16:42:51 -07002344 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002345 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002346
2347 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002348 (*nr_events)++;
2349
Pavel Begunkovc3524382020-06-28 12:52:32 +03002350 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002351 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002352 }
Jens Axboedef596e2019-01-09 08:59:42 -07002353
Jens Axboe09bb8392019-03-13 12:39:28 -06002354 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002355 if (ctx->flags & IORING_SETUP_SQPOLL)
2356 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002357 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002358
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002359 if (!list_empty(&again))
2360 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002361}
2362
Jens Axboedef596e2019-01-09 08:59:42 -07002363static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2364 long min)
2365{
2366 struct io_kiocb *req, *tmp;
2367 LIST_HEAD(done);
2368 bool spin;
2369 int ret;
2370
2371 /*
2372 * Only spin for completions if we don't have multiple devices hanging
2373 * off our complete list, and we're under the requested amount.
2374 */
2375 spin = !ctx->poll_multi_file && *nr_events < min;
2376
2377 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002378 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002379 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002380
2381 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002382 * Move completed and retryable entries to our local lists.
2383 * If we find a request that requires polling, break out
2384 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002385 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002386 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002387 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002388 continue;
2389 }
2390 if (!list_empty(&done))
2391 break;
2392
2393 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2394 if (ret < 0)
2395 break;
2396
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002397 /* iopoll may have completed current req */
2398 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002399 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002400
Jens Axboedef596e2019-01-09 08:59:42 -07002401 if (ret && spin)
2402 spin = false;
2403 ret = 0;
2404 }
2405
2406 if (!list_empty(&done))
2407 io_iopoll_complete(ctx, nr_events, &done);
2408
2409 return ret;
2410}
2411
2412/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002413 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002414 * non-spinning poll check - we'll still enter the driver poll loop, but only
2415 * as a non-spinning completion check.
2416 */
2417static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2418 long min)
2419{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002420 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002421 int ret;
2422
2423 ret = io_do_iopoll(ctx, nr_events, min);
2424 if (ret < 0)
2425 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002426 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002427 return 0;
2428 }
2429
2430 return 1;
2431}
2432
2433/*
2434 * We can't just wait for polled events to come to us, we have to actively
2435 * find and complete them.
2436 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002437static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002438{
2439 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2440 return;
2441
2442 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002443 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002444 unsigned int nr_events = 0;
2445
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002446 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002447
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002448 /* let it sleep and repeat later if can't complete a request */
2449 if (nr_events == 0)
2450 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002451 /*
2452 * Ensure we allow local-to-the-cpu processing to take place,
2453 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002454 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002455 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002456 if (need_resched()) {
2457 mutex_unlock(&ctx->uring_lock);
2458 cond_resched();
2459 mutex_lock(&ctx->uring_lock);
2460 }
Jens Axboedef596e2019-01-09 08:59:42 -07002461 }
2462 mutex_unlock(&ctx->uring_lock);
2463}
2464
Pavel Begunkov7668b922020-07-07 16:36:21 +03002465static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002466{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002467 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002468 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002469
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002470 /*
2471 * We disallow the app entering submit/complete with polling, but we
2472 * still need to lock the ring to prevent racing with polled issue
2473 * that got punted to a workqueue.
2474 */
2475 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002476 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002477 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002478 * Don't enter poll loop if we already have events pending.
2479 * If we do, we can potentially be spinning for commands that
2480 * already triggered a CQE (eg in error).
2481 */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00002482 if (test_bit(0, &ctx->cq_check_overflow))
2483 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2484 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002485 break;
2486
2487 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002488 * If a submit got punted to a workqueue, we can have the
2489 * application entering polling for a command before it gets
2490 * issued. That app will hold the uring_lock for the duration
2491 * of the poll right here, so we need to take a breather every
2492 * now and then to ensure that the issue has a chance to add
2493 * the poll to the issued list. Otherwise we can spin here
2494 * forever, while the workqueue is stuck trying to acquire the
2495 * very same mutex.
2496 */
2497 if (!(++iters & 7)) {
2498 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002499 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002500 mutex_lock(&ctx->uring_lock);
2501 }
2502
Pavel Begunkov7668b922020-07-07 16:36:21 +03002503 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002504 if (ret <= 0)
2505 break;
2506 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002507 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002508
Jens Axboe500f9fb2019-08-19 12:15:59 -06002509 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002510 return ret;
2511}
2512
Jens Axboe491381ce2019-10-17 09:20:46 -06002513static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002514{
Jens Axboe491381ce2019-10-17 09:20:46 -06002515 /*
2516 * Tell lockdep we inherited freeze protection from submission
2517 * thread.
2518 */
2519 if (req->flags & REQ_F_ISREG) {
2520 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002521
Jens Axboe491381ce2019-10-17 09:20:46 -06002522 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002524 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002525}
2526
Jens Axboea1d7c392020-06-22 11:09:46 -06002527static void io_complete_rw_common(struct kiocb *kiocb, long res,
2528 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002529{
Jens Axboe9adbd452019-12-20 08:45:55 -07002530 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002531 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532
Jens Axboe491381ce2019-10-17 09:20:46 -06002533 if (kiocb->ki_flags & IOCB_WRITE)
2534 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002535
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002536 if (res != req->result)
2537 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002538 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002539 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002540 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002541}
2542
Jens Axboeb63534c2020-06-04 11:28:00 -06002543#ifdef CONFIG_BLOCK
2544static bool io_resubmit_prep(struct io_kiocb *req, int error)
2545{
2546 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2547 ssize_t ret = -ECANCELED;
2548 struct iov_iter iter;
2549 int rw;
2550
2551 if (error) {
2552 ret = error;
2553 goto end_req;
2554 }
2555
2556 switch (req->opcode) {
2557 case IORING_OP_READV:
2558 case IORING_OP_READ_FIXED:
2559 case IORING_OP_READ:
2560 rw = READ;
2561 break;
2562 case IORING_OP_WRITEV:
2563 case IORING_OP_WRITE_FIXED:
2564 case IORING_OP_WRITE:
2565 rw = WRITE;
2566 break;
2567 default:
2568 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2569 req->opcode);
2570 goto end_req;
2571 }
2572
Jens Axboee8c2bc12020-08-15 18:44:09 -07002573 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002574 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2575 if (ret < 0)
2576 goto end_req;
2577 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2578 if (!ret)
2579 return true;
2580 kfree(iovec);
2581 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002582 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002583 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002584end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002585 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002586 return false;
2587}
Jens Axboeb63534c2020-06-04 11:28:00 -06002588#endif
2589
2590static bool io_rw_reissue(struct io_kiocb *req, long res)
2591{
2592#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002593 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002594 int ret;
2595
Jens Axboe355afae2020-09-02 09:30:31 -06002596 if (!S_ISBLK(mode) && !S_ISREG(mode))
2597 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002598 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2599 return false;
2600
Jens Axboefdee9462020-08-27 16:46:24 -06002601 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002602
Jens Axboefdee9462020-08-27 16:46:24 -06002603 if (io_resubmit_prep(req, ret)) {
2604 refcount_inc(&req->refs);
2605 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002606 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002607 }
2608
Jens Axboeb63534c2020-06-04 11:28:00 -06002609#endif
2610 return false;
2611}
2612
Jens Axboea1d7c392020-06-22 11:09:46 -06002613static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2614 struct io_comp_state *cs)
2615{
2616 if (!io_rw_reissue(req, res))
2617 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002618}
2619
2620static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2621{
Jens Axboe9adbd452019-12-20 08:45:55 -07002622 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002623
Jens Axboea1d7c392020-06-22 11:09:46 -06002624 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625}
2626
Jens Axboedef596e2019-01-09 08:59:42 -07002627static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2628{
Jens Axboe9adbd452019-12-20 08:45:55 -07002629 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002630
Jens Axboe491381ce2019-10-17 09:20:46 -06002631 if (kiocb->ki_flags & IOCB_WRITE)
2632 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002633
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002634 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002635 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002636
2637 WRITE_ONCE(req->result, res);
2638 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002639 smp_wmb();
2640 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002641}
2642
2643/*
2644 * After the iocb has been issued, it's safe to be found on the poll list.
2645 * Adding the kiocb to the list AFTER submission ensures that we don't
2646 * find it from a io_iopoll_getevents() thread before the issuer is done
2647 * accessing the kiocb cookie.
2648 */
2649static void io_iopoll_req_issued(struct io_kiocb *req)
2650{
2651 struct io_ring_ctx *ctx = req->ctx;
2652
2653 /*
2654 * Track whether we have multiple files in our lists. This will impact
2655 * how we do polling eventually, not spinning if we're on potentially
2656 * different devices.
2657 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002658 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002659 ctx->poll_multi_file = false;
2660 } else if (!ctx->poll_multi_file) {
2661 struct io_kiocb *list_req;
2662
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002663 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002664 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002665 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002666 ctx->poll_multi_file = true;
2667 }
2668
2669 /*
2670 * For fast devices, IO may have already completed. If it has, add
2671 * it to the front so we find it first.
2672 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002673 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002674 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002675 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002676 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002677
2678 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002679 wq_has_sleeper(&ctx->sq_data->wait))
2680 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002681}
2682
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002683static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002684{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002685 if (state->has_refs)
2686 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002687 state->file = NULL;
2688}
2689
2690static inline void io_state_file_put(struct io_submit_state *state)
2691{
2692 if (state->file)
2693 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002694}
2695
2696/*
2697 * Get as many references to a file as we have IOs left in this submission,
2698 * assuming most submissions are for one file, or at least that each file
2699 * has more than one submission.
2700 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002701static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002702{
2703 if (!state)
2704 return fget(fd);
2705
2706 if (state->file) {
2707 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002708 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002709 return state->file;
2710 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002711 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002712 }
2713 state->file = fget_many(fd, state->ios_left);
2714 if (!state->file)
2715 return NULL;
2716
2717 state->fd = fd;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01002718 state->has_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002719 return state->file;
2720}
2721
Jens Axboe4503b762020-06-01 10:00:27 -06002722static bool io_bdev_nowait(struct block_device *bdev)
2723{
2724#ifdef CONFIG_BLOCK
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002725 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002726#else
2727 return true;
2728#endif
2729}
2730
Jens Axboe2b188cc2019-01-07 10:46:33 -07002731/*
2732 * If we tracked the file through the SCM inflight mechanism, we could support
2733 * any file. For now, just ensure that anything potentially problematic is done
2734 * inline.
2735 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002736static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002737{
2738 umode_t mode = file_inode(file)->i_mode;
2739
Jens Axboe4503b762020-06-01 10:00:27 -06002740 if (S_ISBLK(mode)) {
2741 if (io_bdev_nowait(file->f_inode->i_bdev))
2742 return true;
2743 return false;
2744 }
2745 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002746 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002747 if (S_ISREG(mode)) {
2748 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2749 file->f_op != &io_uring_fops)
2750 return true;
2751 return false;
2752 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002753
Jens Axboec5b85622020-06-09 19:23:05 -06002754 /* any ->read/write should understand O_NONBLOCK */
2755 if (file->f_flags & O_NONBLOCK)
2756 return true;
2757
Jens Axboeaf197f52020-04-28 13:15:06 -06002758 if (!(file->f_mode & FMODE_NOWAIT))
2759 return false;
2760
2761 if (rw == READ)
2762 return file->f_op->read_iter != NULL;
2763
2764 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002765}
2766
Pavel Begunkova88fc402020-09-30 22:57:53 +03002767static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002768{
Jens Axboedef596e2019-01-09 08:59:42 -07002769 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002770 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002771 unsigned ioprio;
2772 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002773
Jens Axboe491381ce2019-10-17 09:20:46 -06002774 if (S_ISREG(file_inode(req->file)->i_mode))
2775 req->flags |= REQ_F_ISREG;
2776
Jens Axboe2b188cc2019-01-07 10:46:33 -07002777 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002778 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2779 req->flags |= REQ_F_CUR_POS;
2780 kiocb->ki_pos = req->file->f_pos;
2781 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002782 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002783 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2784 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2785 if (unlikely(ret))
2786 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002787
2788 ioprio = READ_ONCE(sqe->ioprio);
2789 if (ioprio) {
2790 ret = ioprio_check_cap(ioprio);
2791 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002792 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002793
2794 kiocb->ki_ioprio = ioprio;
2795 } else
2796 kiocb->ki_ioprio = get_current_ioprio();
2797
Stefan Bühler8449eed2019-04-27 20:34:19 +02002798 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002799 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002800 req->flags |= REQ_F_NOWAIT;
2801
Jens Axboedef596e2019-01-09 08:59:42 -07002802 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002803 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2804 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002805 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806
Jens Axboedef596e2019-01-09 08:59:42 -07002807 kiocb->ki_flags |= IOCB_HIPRI;
2808 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002809 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002810 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002811 if (kiocb->ki_flags & IOCB_HIPRI)
2812 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002813 kiocb->ki_complete = io_complete_rw;
2814 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002815
Jens Axboe3529d8c2019-12-19 18:24:38 -07002816 req->rw.addr = READ_ONCE(sqe->addr);
2817 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002818 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002819 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002820}
2821
2822static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2823{
2824 switch (ret) {
2825 case -EIOCBQUEUED:
2826 break;
2827 case -ERESTARTSYS:
2828 case -ERESTARTNOINTR:
2829 case -ERESTARTNOHAND:
2830 case -ERESTART_RESTARTBLOCK:
2831 /*
2832 * We can't just restart the syscall, since previously
2833 * submitted sqes may already be in progress. Just fail this
2834 * IO with EINTR.
2835 */
2836 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002837 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002838 default:
2839 kiocb->ki_complete(kiocb, ret, 0);
2840 }
2841}
2842
Jens Axboea1d7c392020-06-22 11:09:46 -06002843static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2844 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002845{
Jens Axboeba042912019-12-25 16:33:42 -07002846 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002847 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002848
Jens Axboe227c0c92020-08-13 11:51:40 -06002849 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002850 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002851 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002852 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002853 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002854 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002855 }
2856
Jens Axboeba042912019-12-25 16:33:42 -07002857 if (req->flags & REQ_F_CUR_POS)
2858 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002859 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002860 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002861 else
2862 io_rw_done(kiocb, ret);
2863}
2864
Jens Axboe9adbd452019-12-20 08:45:55 -07002865static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002866 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002867{
Jens Axboe9adbd452019-12-20 08:45:55 -07002868 struct io_ring_ctx *ctx = req->ctx;
2869 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002870 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002871 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002872 size_t offset;
2873 u64 buf_addr;
2874
Jens Axboeedafcce2019-01-09 09:16:05 -07002875 if (unlikely(buf_index >= ctx->nr_user_bufs))
2876 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002877 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2878 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002879 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002880
2881 /* overflow */
2882 if (buf_addr + len < buf_addr)
2883 return -EFAULT;
2884 /* not inside the mapped region */
2885 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2886 return -EFAULT;
2887
2888 /*
2889 * May not be a start of buffer, set size appropriately
2890 * and advance us to the beginning.
2891 */
2892 offset = buf_addr - imu->ubuf;
2893 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002894
2895 if (offset) {
2896 /*
2897 * Don't use iov_iter_advance() here, as it's really slow for
2898 * using the latter parts of a big fixed buffer - it iterates
2899 * over each segment manually. We can cheat a bit here, because
2900 * we know that:
2901 *
2902 * 1) it's a BVEC iter, we set it up
2903 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2904 * first and last bvec
2905 *
2906 * So just find our index, and adjust the iterator afterwards.
2907 * If the offset is within the first bvec (or the whole first
2908 * bvec, just use iov_iter_advance(). This makes it easier
2909 * since we can just skip the first segment, which may not
2910 * be PAGE_SIZE aligned.
2911 */
2912 const struct bio_vec *bvec = imu->bvec;
2913
2914 if (offset <= bvec->bv_len) {
2915 iov_iter_advance(iter, offset);
2916 } else {
2917 unsigned long seg_skip;
2918
2919 /* skip first vec */
2920 offset -= bvec->bv_len;
2921 seg_skip = 1 + (offset >> PAGE_SHIFT);
2922
2923 iter->bvec = bvec + seg_skip;
2924 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002925 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002926 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002927 }
2928 }
2929
Jens Axboe5e559562019-11-13 16:12:46 -07002930 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002931}
2932
Jens Axboebcda7ba2020-02-23 16:42:51 -07002933static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2934{
2935 if (needs_lock)
2936 mutex_unlock(&ctx->uring_lock);
2937}
2938
2939static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2940{
2941 /*
2942 * "Normal" inline submissions always hold the uring_lock, since we
2943 * grab it from the system call. Same is true for the SQPOLL offload.
2944 * The only exception is when we've detached the request and issue it
2945 * from an async worker thread, grab the lock for that case.
2946 */
2947 if (needs_lock)
2948 mutex_lock(&ctx->uring_lock);
2949}
2950
2951static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2952 int bgid, struct io_buffer *kbuf,
2953 bool needs_lock)
2954{
2955 struct io_buffer *head;
2956
2957 if (req->flags & REQ_F_BUFFER_SELECTED)
2958 return kbuf;
2959
2960 io_ring_submit_lock(req->ctx, needs_lock);
2961
2962 lockdep_assert_held(&req->ctx->uring_lock);
2963
2964 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2965 if (head) {
2966 if (!list_empty(&head->list)) {
2967 kbuf = list_last_entry(&head->list, struct io_buffer,
2968 list);
2969 list_del(&kbuf->list);
2970 } else {
2971 kbuf = head;
2972 idr_remove(&req->ctx->io_buffer_idr, bgid);
2973 }
2974 if (*len > kbuf->len)
2975 *len = kbuf->len;
2976 } else {
2977 kbuf = ERR_PTR(-ENOBUFS);
2978 }
2979
2980 io_ring_submit_unlock(req->ctx, needs_lock);
2981
2982 return kbuf;
2983}
2984
Jens Axboe4d954c22020-02-27 07:31:19 -07002985static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2986 bool needs_lock)
2987{
2988 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002989 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002990
2991 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002992 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002993 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2994 if (IS_ERR(kbuf))
2995 return kbuf;
2996 req->rw.addr = (u64) (unsigned long) kbuf;
2997 req->flags |= REQ_F_BUFFER_SELECTED;
2998 return u64_to_user_ptr(kbuf->addr);
2999}
3000
3001#ifdef CONFIG_COMPAT
3002static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3003 bool needs_lock)
3004{
3005 struct compat_iovec __user *uiov;
3006 compat_ssize_t clen;
3007 void __user *buf;
3008 ssize_t len;
3009
3010 uiov = u64_to_user_ptr(req->rw.addr);
3011 if (!access_ok(uiov, sizeof(*uiov)))
3012 return -EFAULT;
3013 if (__get_user(clen, &uiov->iov_len))
3014 return -EFAULT;
3015 if (clen < 0)
3016 return -EINVAL;
3017
3018 len = clen;
3019 buf = io_rw_buffer_select(req, &len, needs_lock);
3020 if (IS_ERR(buf))
3021 return PTR_ERR(buf);
3022 iov[0].iov_base = buf;
3023 iov[0].iov_len = (compat_size_t) len;
3024 return 0;
3025}
3026#endif
3027
3028static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3029 bool needs_lock)
3030{
3031 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3032 void __user *buf;
3033 ssize_t len;
3034
3035 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3036 return -EFAULT;
3037
3038 len = iov[0].iov_len;
3039 if (len < 0)
3040 return -EINVAL;
3041 buf = io_rw_buffer_select(req, &len, needs_lock);
3042 if (IS_ERR(buf))
3043 return PTR_ERR(buf);
3044 iov[0].iov_base = buf;
3045 iov[0].iov_len = len;
3046 return 0;
3047}
3048
3049static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3050 bool needs_lock)
3051{
Jens Axboedddb3e22020-06-04 11:27:01 -06003052 if (req->flags & REQ_F_BUFFER_SELECTED) {
3053 struct io_buffer *kbuf;
3054
3055 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3056 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3057 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003058 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003059 }
Pavel Begunkov72a016d2020-12-19 03:15:43 +00003060 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003061 return -EINVAL;
3062
3063#ifdef CONFIG_COMPAT
3064 if (req->ctx->compat)
3065 return io_compat_import(req, iov, needs_lock);
3066#endif
3067
3068 return __io_iov_buffer_select(req, iov, needs_lock);
3069}
3070
Jens Axboe8452fd02020-08-18 13:58:33 -07003071static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
3072 struct iovec **iovec, struct iov_iter *iter,
3073 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003074{
Jens Axboe9adbd452019-12-20 08:45:55 -07003075 void __user *buf = u64_to_user_ptr(req->rw.addr);
3076 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003077 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003078 u8 opcode;
3079
Jens Axboed625c6e2019-12-17 19:53:05 -07003080 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003081 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003082 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003083 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003084 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003085
Jens Axboebcda7ba2020-02-23 16:42:51 -07003086 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003087 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003088 return -EINVAL;
3089
Jens Axboe3a6820f2019-12-22 15:19:35 -07003090 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003091 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003092 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003093 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003094 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003095 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003096 }
3097
Jens Axboe3a6820f2019-12-22 15:19:35 -07003098 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3099 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07003100 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003101 }
3102
Jens Axboe4d954c22020-02-27 07:31:19 -07003103 if (req->flags & REQ_F_BUFFER_SELECT) {
3104 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003105 if (!ret) {
3106 ret = (*iovec)->iov_len;
3107 iov_iter_init(iter, rw, *iovec, 1, ret);
3108 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003109 *iovec = NULL;
3110 return ret;
3111 }
3112
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003113 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3114 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003115}
3116
Jens Axboe8452fd02020-08-18 13:58:33 -07003117static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
3118 struct iovec **iovec, struct iov_iter *iter,
3119 bool needs_lock)
3120{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003121 struct io_async_rw *iorw = req->async_data;
3122
3123 if (!iorw)
Jens Axboe8452fd02020-08-18 13:58:33 -07003124 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
3125 *iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003126 return iov_iter_count(&iorw->iter);
Jens Axboe8452fd02020-08-18 13:58:33 -07003127}
3128
Jens Axboe0fef9482020-08-26 10:36:20 -06003129static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3130{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003131 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003132}
3133
Jens Axboe32960612019-09-23 11:05:34 -06003134/*
3135 * For files that don't have ->read_iter() and ->write_iter(), handle them
3136 * by looping over ->read() or ->write() manually.
3137 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003138static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003139{
Jens Axboe4017eb92020-10-22 14:14:12 -06003140 struct kiocb *kiocb = &req->rw.kiocb;
3141 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003142 ssize_t ret = 0;
3143
3144 /*
3145 * Don't support polled IO through this interface, and we can't
3146 * support non-blocking either. For the latter, this just causes
3147 * the kiocb to be handled from an async context.
3148 */
3149 if (kiocb->ki_flags & IOCB_HIPRI)
3150 return -EOPNOTSUPP;
3151 if (kiocb->ki_flags & IOCB_NOWAIT)
3152 return -EAGAIN;
3153
3154 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003155 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003156 ssize_t nr;
3157
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003158 if (!iov_iter_is_bvec(iter)) {
3159 iovec = iov_iter_iovec(iter);
3160 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003161 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3162 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003163 }
3164
Jens Axboe32960612019-09-23 11:05:34 -06003165 if (rw == READ) {
3166 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003167 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003168 } else {
3169 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003170 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003171 }
3172
3173 if (nr < 0) {
3174 if (!ret)
3175 ret = nr;
3176 break;
3177 }
3178 ret += nr;
3179 if (nr != iovec.iov_len)
3180 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003181 req->rw.len -= nr;
3182 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003183 iov_iter_advance(iter, nr);
3184 }
3185
3186 return ret;
3187}
3188
Jens Axboeff6165b2020-08-13 09:47:43 -06003189static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3190 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003191{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003192 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003193
Jens Axboeff6165b2020-08-13 09:47:43 -06003194 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003195 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003196 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003197 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003198 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003199 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003200 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003201 unsigned iov_off = 0;
3202
3203 rw->iter.iov = rw->fast_iov;
3204 if (iter->iov != fast_iov) {
3205 iov_off = iter->iov - fast_iov;
3206 rw->iter.iov += iov_off;
3207 }
3208 if (rw->fast_iov != fast_iov)
3209 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003210 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003211 } else {
3212 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003213 }
3214}
3215
Jens Axboee8c2bc12020-08-15 18:44:09 -07003216static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003217{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003218 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3219 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3220 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003221}
3222
Jens Axboee8c2bc12020-08-15 18:44:09 -07003223static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003224{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003225 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003226 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003227
Jens Axboee8c2bc12020-08-15 18:44:09 -07003228 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003229}
3230
Jens Axboeff6165b2020-08-13 09:47:43 -06003231static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3232 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003233 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003234{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003235 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003236 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003237 if (!req->async_data) {
3238 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003239 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003240
Jens Axboeff6165b2020-08-13 09:47:43 -06003241 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003242 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003243 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003244}
3245
Pavel Begunkov73debe62020-09-30 22:57:54 +03003246static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003247{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003248 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003249 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003250 ssize_t ret;
3251
Pavel Begunkov73debe62020-09-30 22:57:54 +03003252 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003253 if (unlikely(ret < 0))
3254 return ret;
3255
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003256 iorw->bytes_done = 0;
3257 iorw->free_iovec = iov;
3258 if (iov)
3259 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003260 return 0;
3261}
3262
Pavel Begunkov73debe62020-09-30 22:57:54 +03003263static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003264{
3265 ssize_t ret;
3266
Pavel Begunkova88fc402020-09-30 22:57:53 +03003267 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003268 if (ret)
3269 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003270
Jens Axboe3529d8c2019-12-19 18:24:38 -07003271 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3272 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003273
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003274 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003275 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003276 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003277 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003278}
3279
Jens Axboec1dd91d2020-08-03 16:43:59 -06003280/*
3281 * This is our waitqueue callback handler, registered through lock_page_async()
3282 * when we initially tried to do the IO with the iocb armed our waitqueue.
3283 * This gets called when the page is unlocked, and we generally expect that to
3284 * happen when the page IO is completed and the page is now uptodate. This will
3285 * queue a task_work based retry of the operation, attempting to copy the data
3286 * again. If the latter fails because the page was NOT uptodate, then we will
3287 * do a thread based blocking retry of the operation. That's the unexpected
3288 * slow path.
3289 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003290static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3291 int sync, void *arg)
3292{
3293 struct wait_page_queue *wpq;
3294 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003295 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003296 int ret;
3297
3298 wpq = container_of(wait, struct wait_page_queue, wait);
3299
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003300 if (!wake_page_match(wpq, key))
3301 return 0;
3302
Hao Xuc8d317a2020-09-29 20:00:45 +08003303 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003304 list_del_init(&wait->entry);
3305
Pavel Begunkove7375122020-07-12 20:42:04 +03003306 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003307 percpu_ref_get(&req->ctx->refs);
3308
Jens Axboebcf5a062020-05-22 09:24:42 -06003309 /* submit ref gets dropped, acquire a new one */
3310 refcount_inc(&req->refs);
Jens Axboe87c43112020-09-30 21:00:14 -06003311 ret = io_req_task_work_add(req, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003312 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003313 struct task_struct *tsk;
3314
Jens Axboebcf5a062020-05-22 09:24:42 -06003315 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003316 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003317 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003318 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003319 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003320 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003321 return 1;
3322}
3323
Jens Axboec1dd91d2020-08-03 16:43:59 -06003324/*
3325 * This controls whether a given IO request should be armed for async page
3326 * based retry. If we return false here, the request is handed to the async
3327 * worker threads for retry. If we're doing buffered reads on a regular file,
3328 * we prepare a private wait_page_queue entry and retry the operation. This
3329 * will either succeed because the page is now uptodate and unlocked, or it
3330 * will register a callback when the page is unlocked at IO completion. Through
3331 * that callback, io_uring uses task_work to setup a retry of the operation.
3332 * That retry will attempt the buffered read again. The retry will generally
3333 * succeed, or in rare cases where it fails, we then fall back to using the
3334 * async worker threads for a blocking retry.
3335 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003336static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003337{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003338 struct io_async_rw *rw = req->async_data;
3339 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003340 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003341
3342 /* never retry for NOWAIT, we just complete with -EAGAIN */
3343 if (req->flags & REQ_F_NOWAIT)
3344 return false;
3345
Jens Axboe227c0c92020-08-13 11:51:40 -06003346 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003347 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003348 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003349
Jens Axboebcf5a062020-05-22 09:24:42 -06003350 /*
3351 * just use poll if we can, and don't attempt if the fs doesn't
3352 * support callback based unlocks
3353 */
3354 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3355 return false;
3356
Jens Axboe3b2a4432020-08-16 10:58:43 -07003357 wait->wait.func = io_async_buf_func;
3358 wait->wait.private = req;
3359 wait->wait.flags = 0;
3360 INIT_LIST_HEAD(&wait->wait.entry);
3361 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003362 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003363 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003364 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003365}
3366
3367static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3368{
3369 if (req->file->f_op->read_iter)
3370 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003371 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003372 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003373 else
3374 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003375}
3376
Jens Axboea1d7c392020-06-22 11:09:46 -06003377static int io_read(struct io_kiocb *req, bool force_nonblock,
3378 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003379{
3380 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003381 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003382 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003383 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003384 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003385 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003386 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003387
Jens Axboee8c2bc12020-08-15 18:44:09 -07003388 if (rw)
3389 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003390
3391 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003392 if (ret < 0)
3393 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003394 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003395 io_size = ret;
3396 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003397 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003398
Jens Axboefd6c2e42019-12-18 12:19:41 -07003399 /* Ensure we clear previously set non-block flag */
3400 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003401 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003402 else
3403 kiocb->ki_flags |= IOCB_NOWAIT;
3404
Jens Axboefd6c2e42019-12-18 12:19:41 -07003405
Pavel Begunkov24c74672020-06-21 13:09:51 +03003406 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003407 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3408 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003409 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003410
Jens Axboe0fef9482020-08-26 10:36:20 -06003411 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003412 if (unlikely(ret))
3413 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003414
Jens Axboe227c0c92020-08-13 11:51:40 -06003415 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003416
Jens Axboe227c0c92020-08-13 11:51:40 -06003417 if (!ret) {
3418 goto done;
3419 } else if (ret == -EIOCBQUEUED) {
3420 ret = 0;
3421 goto out_free;
3422 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003423 /* IOPOLL retry should happen for io-wq threads */
3424 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003425 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003426 /* no retry on NONBLOCK marked file */
3427 if (req->file->f_flags & O_NONBLOCK)
3428 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003429 /* some cases will consume bytes even on error returns */
3430 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003431 ret = 0;
3432 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003433 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003434 /* make sure -ERESTARTSYS -> -EINTR is done */
3435 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003436 }
3437
3438 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003439 if (!iov_iter_count(iter) || !force_nonblock ||
3440 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003441 goto done;
3442
3443 io_size -= ret;
3444copy_iov:
3445 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3446 if (ret2) {
3447 ret = ret2;
3448 goto out_free;
3449 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003450 if (no_async)
3451 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003452 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003453 /* it's copied and will be cleaned with ->io */
3454 iovec = NULL;
3455 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003456 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003457retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003458 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003459 /* if we can retry, do so with the callbacks armed */
3460 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003461 kiocb->ki_flags &= ~IOCB_WAITQ;
3462 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003463 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003464
3465 /*
3466 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3467 * get -EIOCBQUEUED, then we'll get a notification when the desired
3468 * page gets unlocked. We can also get a partial read here, and if we
3469 * do, then just retry at the new offset.
3470 */
3471 ret = io_iter_do_read(req, iter);
3472 if (ret == -EIOCBQUEUED) {
3473 ret = 0;
3474 goto out_free;
3475 } else if (ret > 0 && ret < io_size) {
3476 /* we got some bytes, but not all. retry. */
3477 goto retry;
3478 }
3479done:
3480 kiocb_done(kiocb, ret, cs);
3481 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003482out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003483 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003484 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003485 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003486 return ret;
3487}
3488
Pavel Begunkov73debe62020-09-30 22:57:54 +03003489static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003490{
3491 ssize_t ret;
3492
Pavel Begunkova88fc402020-09-30 22:57:53 +03003493 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003494 if (ret)
3495 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003496
Jens Axboe3529d8c2019-12-19 18:24:38 -07003497 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3498 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003499
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003500 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003501 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003502 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003503 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003504}
3505
Jens Axboea1d7c392020-06-22 11:09:46 -06003506static int io_write(struct io_kiocb *req, bool force_nonblock,
3507 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003508{
3509 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003510 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003511 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003512 struct io_async_rw *rw = req->async_data;
Jens Axboe31b51512019-01-18 22:56:34 -07003513 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003514 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003515
Jens Axboee8c2bc12020-08-15 18:44:09 -07003516 if (rw)
3517 iter = &rw->iter;
Jens Axboeff6165b2020-08-13 09:47:43 -06003518
3519 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003520 if (ret < 0)
3521 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003522 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003523 io_size = ret;
3524 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003525
Jens Axboefd6c2e42019-12-18 12:19:41 -07003526 /* Ensure we clear previously set non-block flag */
3527 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003528 kiocb->ki_flags &= ~IOCB_NOWAIT;
3529 else
3530 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003531
Pavel Begunkov24c74672020-06-21 13:09:51 +03003532 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003533 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003534 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003535
Jens Axboe10d59342019-12-09 20:16:22 -07003536 /* file path doesn't support NOWAIT for non-direct_IO */
3537 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3538 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003539 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003540
Jens Axboe0fef9482020-08-26 10:36:20 -06003541 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003542 if (unlikely(ret))
3543 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003544
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003545 /*
3546 * Open-code file_start_write here to grab freeze protection,
3547 * which will be released by another thread in
3548 * io_complete_rw(). Fool lockdep by telling it the lock got
3549 * released so that it doesn't complain about the held lock when
3550 * we return to userspace.
3551 */
3552 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003553 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003554 __sb_writers_release(file_inode(req->file)->i_sb,
3555 SB_FREEZE_WRITE);
3556 }
3557 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003558
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003559 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003560 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003561 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003562 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003563 else
3564 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003565
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003566 /*
3567 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3568 * retry them without IOCB_NOWAIT.
3569 */
3570 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3571 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003572 /* no retry on NONBLOCK marked file */
3573 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3574 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003575 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003576 /* IOPOLL retry should happen for io-wq threads */
3577 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3578 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003579done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003580 kiocb_done(kiocb, ret2, cs);
3581 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003582copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003583 /* some cases will consume bytes even on error returns */
3584 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003585 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003586 if (!ret)
3587 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003588 }
Jens Axboe31b51512019-01-18 22:56:34 -07003589out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003590 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003591 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003592 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003593 return ret;
3594}
3595
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003596static int __io_splice_prep(struct io_kiocb *req,
3597 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003598{
3599 struct io_splice* sp = &req->splice;
3600 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003601
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003602 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3603 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003604
3605 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003606 sp->len = READ_ONCE(sqe->len);
3607 sp->flags = READ_ONCE(sqe->splice_flags);
3608
3609 if (unlikely(sp->flags & ~valid_flags))
3610 return -EINVAL;
3611
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003612 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3613 (sp->flags & SPLICE_F_FD_IN_FIXED));
3614 if (!sp->file_in)
3615 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003616 req->flags |= REQ_F_NEED_CLEANUP;
3617
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003618 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3619 /*
3620 * Splice operation will be punted aync, and here need to
3621 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3622 */
3623 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003624 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003625 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003626
3627 return 0;
3628}
3629
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003630static int io_tee_prep(struct io_kiocb *req,
3631 const struct io_uring_sqe *sqe)
3632{
3633 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3634 return -EINVAL;
3635 return __io_splice_prep(req, sqe);
3636}
3637
3638static int io_tee(struct io_kiocb *req, bool force_nonblock)
3639{
3640 struct io_splice *sp = &req->splice;
3641 struct file *in = sp->file_in;
3642 struct file *out = sp->file_out;
3643 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3644 long ret = 0;
3645
3646 if (force_nonblock)
3647 return -EAGAIN;
3648 if (sp->len)
3649 ret = do_tee(in, out, sp->len, flags);
3650
3651 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3652 req->flags &= ~REQ_F_NEED_CLEANUP;
3653
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003654 if (ret != sp->len)
3655 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003656 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003657 return 0;
3658}
3659
3660static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3661{
3662 struct io_splice* sp = &req->splice;
3663
3664 sp->off_in = READ_ONCE(sqe->splice_off_in);
3665 sp->off_out = READ_ONCE(sqe->off);
3666 return __io_splice_prep(req, sqe);
3667}
3668
Pavel Begunkov014db002020-03-03 21:33:12 +03003669static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003670{
3671 struct io_splice *sp = &req->splice;
3672 struct file *in = sp->file_in;
3673 struct file *out = sp->file_out;
3674 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3675 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003676 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003677
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003678 if (force_nonblock)
3679 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003680
3681 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3682 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003683
Jens Axboe948a7742020-05-17 14:21:38 -06003684 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003685 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003686
3687 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3688 req->flags &= ~REQ_F_NEED_CLEANUP;
3689
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003690 if (ret != sp->len)
3691 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003692 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003693 return 0;
3694}
3695
Jens Axboe2b188cc2019-01-07 10:46:33 -07003696/*
3697 * IORING_OP_NOP just posts a completion event, nothing else.
3698 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003699static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003700{
3701 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003702
Jens Axboedef596e2019-01-09 08:59:42 -07003703 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3704 return -EINVAL;
3705
Jens Axboe229a7b62020-06-22 10:13:11 -06003706 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003707 return 0;
3708}
3709
Jens Axboe3529d8c2019-12-19 18:24:38 -07003710static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003711{
Jens Axboe6b063142019-01-10 22:13:58 -07003712 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003713
Jens Axboe09bb8392019-03-13 12:39:28 -06003714 if (!req->file)
3715 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003716
Jens Axboe6b063142019-01-10 22:13:58 -07003717 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003718 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003719 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003720 return -EINVAL;
3721
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003722 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3723 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3724 return -EINVAL;
3725
3726 req->sync.off = READ_ONCE(sqe->off);
3727 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003728 return 0;
3729}
3730
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003731static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003732{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003733 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003734 int ret;
3735
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003736 /* fsync always requires a blocking context */
3737 if (force_nonblock)
3738 return -EAGAIN;
3739
Jens Axboe9adbd452019-12-20 08:45:55 -07003740 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003741 end > 0 ? end : LLONG_MAX,
3742 req->sync.flags & IORING_FSYNC_DATASYNC);
3743 if (ret < 0)
3744 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003745 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003746 return 0;
3747}
3748
Jens Axboed63d1b52019-12-10 10:38:56 -07003749static int io_fallocate_prep(struct io_kiocb *req,
3750 const struct io_uring_sqe *sqe)
3751{
3752 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3753 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003754 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3755 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003756
3757 req->sync.off = READ_ONCE(sqe->off);
3758 req->sync.len = READ_ONCE(sqe->addr);
3759 req->sync.mode = READ_ONCE(sqe->len);
3760 return 0;
3761}
3762
Pavel Begunkov014db002020-03-03 21:33:12 +03003763static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003764{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003765 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003766
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003767 /* fallocate always requiring blocking context */
3768 if (force_nonblock)
3769 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003770 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3771 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003772 if (ret < 0)
3773 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003774 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003775 return 0;
3776}
3777
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003778static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003779{
Jens Axboef8748882020-01-08 17:47:02 -07003780 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003781 int ret;
3782
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003783 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003784 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003785 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003786 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003787
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003788 /* open.how should be already initialised */
3789 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003790 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003791
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003792 req->open.dfd = READ_ONCE(sqe->fd);
3793 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003794 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003795 if (IS_ERR(req->open.filename)) {
3796 ret = PTR_ERR(req->open.filename);
3797 req->open.filename = NULL;
3798 return ret;
3799 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003800 req->open.nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe944d1442020-11-13 16:48:44 -07003801 req->open.ignore_nonblock = false;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003802 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003803 return 0;
3804}
3805
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003806static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3807{
3808 u64 flags, mode;
3809
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003810 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3811 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003812 mode = READ_ONCE(sqe->len);
3813 flags = READ_ONCE(sqe->open_flags);
3814 req->open.how = build_open_how(flags, mode);
3815 return __io_openat_prep(req, sqe);
3816}
3817
Jens Axboecebdb982020-01-08 17:59:24 -07003818static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3819{
3820 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003821 size_t len;
3822 int ret;
3823
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003824 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3825 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003826 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3827 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003828 if (len < OPEN_HOW_SIZE_VER0)
3829 return -EINVAL;
3830
3831 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3832 len);
3833 if (ret)
3834 return ret;
3835
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003836 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003837}
3838
Pavel Begunkov014db002020-03-03 21:33:12 +03003839static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003840{
3841 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003842 struct file *file;
3843 int ret;
3844
Jens Axboe944d1442020-11-13 16:48:44 -07003845 if (force_nonblock && !req->open.ignore_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003846 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003847
Jens Axboecebdb982020-01-08 17:59:24 -07003848 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003849 if (ret)
3850 goto err;
3851
Jens Axboe4022e7a2020-03-19 19:23:18 -06003852 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003853 if (ret < 0)
3854 goto err;
3855
3856 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3857 if (IS_ERR(file)) {
3858 put_unused_fd(ret);
3859 ret = PTR_ERR(file);
Jens Axboe944d1442020-11-13 16:48:44 -07003860 /*
3861 * A work-around to ensure that /proc/self works that way
3862 * that it should - if we get -EOPNOTSUPP back, then assume
3863 * that proc_self_get_link() failed us because we're in async
3864 * context. We should be safe to retry this from the task
3865 * itself with force_nonblock == false set, as it should not
3866 * block on lookup. Would be nice to know this upfront and
3867 * avoid the async dance, but doesn't seem feasible.
3868 */
3869 if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) {
3870 req->open.ignore_nonblock = true;
3871 refcount_inc(&req->refs);
3872 io_req_task_queue(req);
3873 return 0;
3874 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003875 } else {
3876 fsnotify_open(file);
3877 fd_install(ret, file);
3878 }
3879err:
3880 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003881 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003882 if (ret < 0)
3883 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003884 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003885 return 0;
3886}
3887
Pavel Begunkov014db002020-03-03 21:33:12 +03003888static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003889{
Pavel Begunkov014db002020-03-03 21:33:12 +03003890 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003891}
3892
Jens Axboe067524e2020-03-02 16:32:28 -07003893static int io_remove_buffers_prep(struct io_kiocb *req,
3894 const struct io_uring_sqe *sqe)
3895{
3896 struct io_provide_buf *p = &req->pbuf;
3897 u64 tmp;
3898
3899 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3900 return -EINVAL;
3901
3902 tmp = READ_ONCE(sqe->fd);
3903 if (!tmp || tmp > USHRT_MAX)
3904 return -EINVAL;
3905
3906 memset(p, 0, sizeof(*p));
3907 p->nbufs = tmp;
3908 p->bgid = READ_ONCE(sqe->buf_group);
3909 return 0;
3910}
3911
3912static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3913 int bgid, unsigned nbufs)
3914{
3915 unsigned i = 0;
3916
3917 /* shouldn't happen */
3918 if (!nbufs)
3919 return 0;
3920
3921 /* the head kbuf is the list itself */
3922 while (!list_empty(&buf->list)) {
3923 struct io_buffer *nxt;
3924
3925 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3926 list_del(&nxt->list);
3927 kfree(nxt);
3928 if (++i == nbufs)
3929 return i;
3930 }
3931 i++;
3932 kfree(buf);
3933 idr_remove(&ctx->io_buffer_idr, bgid);
3934
3935 return i;
3936}
3937
Jens Axboe229a7b62020-06-22 10:13:11 -06003938static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3939 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003940{
3941 struct io_provide_buf *p = &req->pbuf;
3942 struct io_ring_ctx *ctx = req->ctx;
3943 struct io_buffer *head;
3944 int ret = 0;
3945
3946 io_ring_submit_lock(ctx, !force_nonblock);
3947
3948 lockdep_assert_held(&ctx->uring_lock);
3949
3950 ret = -ENOENT;
3951 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3952 if (head)
3953 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003954 if (ret < 0)
3955 req_set_fail_links(req);
Pavel Begunkovf961c2b2020-12-06 22:22:43 +00003956
3957 /* need to hold the lock to complete IOPOLL requests */
3958 if (ctx->flags & IORING_SETUP_IOPOLL) {
3959 __io_req_complete(req, ret, 0, cs);
3960 io_ring_submit_unlock(ctx, !force_nonblock);
3961 } else {
3962 io_ring_submit_unlock(ctx, !force_nonblock);
3963 __io_req_complete(req, ret, 0, cs);
3964 }
Jens Axboe067524e2020-03-02 16:32:28 -07003965 return 0;
3966}
3967
Jens Axboeddf0322d2020-02-23 16:41:33 -07003968static int io_provide_buffers_prep(struct io_kiocb *req,
3969 const struct io_uring_sqe *sqe)
3970{
3971 struct io_provide_buf *p = &req->pbuf;
3972 u64 tmp;
3973
3974 if (sqe->ioprio || sqe->rw_flags)
3975 return -EINVAL;
3976
3977 tmp = READ_ONCE(sqe->fd);
3978 if (!tmp || tmp > USHRT_MAX)
3979 return -E2BIG;
3980 p->nbufs = tmp;
3981 p->addr = READ_ONCE(sqe->addr);
3982 p->len = READ_ONCE(sqe->len);
3983
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003984 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003985 return -EFAULT;
3986
3987 p->bgid = READ_ONCE(sqe->buf_group);
3988 tmp = READ_ONCE(sqe->off);
3989 if (tmp > USHRT_MAX)
3990 return -E2BIG;
3991 p->bid = tmp;
3992 return 0;
3993}
3994
3995static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3996{
3997 struct io_buffer *buf;
3998 u64 addr = pbuf->addr;
3999 int i, bid = pbuf->bid;
4000
4001 for (i = 0; i < pbuf->nbufs; i++) {
4002 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4003 if (!buf)
4004 break;
4005
4006 buf->addr = addr;
4007 buf->len = pbuf->len;
4008 buf->bid = bid;
4009 addr += pbuf->len;
4010 bid++;
4011 if (!*head) {
4012 INIT_LIST_HEAD(&buf->list);
4013 *head = buf;
4014 } else {
4015 list_add_tail(&buf->list, &(*head)->list);
4016 }
4017 }
4018
4019 return i ? i : -ENOMEM;
4020}
4021
Jens Axboe229a7b62020-06-22 10:13:11 -06004022static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4023 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004024{
4025 struct io_provide_buf *p = &req->pbuf;
4026 struct io_ring_ctx *ctx = req->ctx;
4027 struct io_buffer *head, *list;
4028 int ret = 0;
4029
4030 io_ring_submit_lock(ctx, !force_nonblock);
4031
4032 lockdep_assert_held(&ctx->uring_lock);
4033
4034 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4035
4036 ret = io_add_buffers(p, &head);
4037 if (ret < 0)
4038 goto out;
4039
4040 if (!list) {
4041 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4042 GFP_KERNEL);
4043 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004044 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004045 goto out;
4046 }
4047 }
4048out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004049 if (ret < 0)
4050 req_set_fail_links(req);
Pavel Begunkovf961c2b2020-12-06 22:22:43 +00004051
4052 /* need to hold the lock to complete IOPOLL requests */
4053 if (ctx->flags & IORING_SETUP_IOPOLL) {
4054 __io_req_complete(req, ret, 0, cs);
4055 io_ring_submit_unlock(ctx, !force_nonblock);
4056 } else {
4057 io_ring_submit_unlock(ctx, !force_nonblock);
4058 __io_req_complete(req, ret, 0, cs);
4059 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004060 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004061}
4062
Jens Axboe3e4827b2020-01-08 15:18:09 -07004063static int io_epoll_ctl_prep(struct io_kiocb *req,
4064 const struct io_uring_sqe *sqe)
4065{
4066#if defined(CONFIG_EPOLL)
4067 if (sqe->ioprio || sqe->buf_index)
4068 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004069 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004070 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004071
4072 req->epoll.epfd = READ_ONCE(sqe->fd);
4073 req->epoll.op = READ_ONCE(sqe->len);
4074 req->epoll.fd = READ_ONCE(sqe->off);
4075
4076 if (ep_op_has_event(req->epoll.op)) {
4077 struct epoll_event __user *ev;
4078
4079 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4080 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4081 return -EFAULT;
4082 }
4083
4084 return 0;
4085#else
4086 return -EOPNOTSUPP;
4087#endif
4088}
4089
Jens Axboe229a7b62020-06-22 10:13:11 -06004090static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4091 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004092{
4093#if defined(CONFIG_EPOLL)
4094 struct io_epoll *ie = &req->epoll;
4095 int ret;
4096
4097 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4098 if (force_nonblock && ret == -EAGAIN)
4099 return -EAGAIN;
4100
4101 if (ret < 0)
4102 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004103 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004104 return 0;
4105#else
4106 return -EOPNOTSUPP;
4107#endif
4108}
4109
Jens Axboec1ca7572019-12-25 22:18:28 -07004110static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4111{
4112#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4113 if (sqe->ioprio || sqe->buf_index || sqe->off)
4114 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004115 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4116 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004117
4118 req->madvise.addr = READ_ONCE(sqe->addr);
4119 req->madvise.len = READ_ONCE(sqe->len);
4120 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4121 return 0;
4122#else
4123 return -EOPNOTSUPP;
4124#endif
4125}
4126
Pavel Begunkov014db002020-03-03 21:33:12 +03004127static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004128{
4129#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4130 struct io_madvise *ma = &req->madvise;
4131 int ret;
4132
4133 if (force_nonblock)
4134 return -EAGAIN;
4135
Minchan Kim0726b012020-10-17 16:14:50 -07004136 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004137 if (ret < 0)
4138 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004139 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004140 return 0;
4141#else
4142 return -EOPNOTSUPP;
4143#endif
4144}
4145
Jens Axboe4840e412019-12-25 22:03:45 -07004146static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4147{
4148 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4149 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004150 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4151 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004152
4153 req->fadvise.offset = READ_ONCE(sqe->off);
4154 req->fadvise.len = READ_ONCE(sqe->len);
4155 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4156 return 0;
4157}
4158
Pavel Begunkov014db002020-03-03 21:33:12 +03004159static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004160{
4161 struct io_fadvise *fa = &req->fadvise;
4162 int ret;
4163
Jens Axboe3e694262020-02-01 09:22:49 -07004164 if (force_nonblock) {
4165 switch (fa->advice) {
4166 case POSIX_FADV_NORMAL:
4167 case POSIX_FADV_RANDOM:
4168 case POSIX_FADV_SEQUENTIAL:
4169 break;
4170 default:
4171 return -EAGAIN;
4172 }
4173 }
Jens Axboe4840e412019-12-25 22:03:45 -07004174
4175 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4176 if (ret < 0)
4177 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004178 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004179 return 0;
4180}
4181
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004182static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4183{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004184 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004185 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004186 if (sqe->ioprio || sqe->buf_index)
4187 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004188 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004189 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004190
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004191 req->statx.dfd = READ_ONCE(sqe->fd);
4192 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004193 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004194 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4195 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004196
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004197 return 0;
4198}
4199
Pavel Begunkov014db002020-03-03 21:33:12 +03004200static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004201{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004202 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004203 int ret;
4204
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004205 if (force_nonblock) {
4206 /* only need file table for an actual valid fd */
4207 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4208 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004209 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004210 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004211
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004212 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4213 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004214
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004215 if (ret < 0)
4216 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004217 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004218 return 0;
4219}
4220
Jens Axboeb5dba592019-12-11 14:02:38 -07004221static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4222{
4223 /*
4224 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004225 * leave the 'file' in an undeterminate state, and here need to modify
4226 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004227 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004228 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004229 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4230
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004231 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4232 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004233 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4234 sqe->rw_flags || sqe->buf_index)
4235 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004236 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004237 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004238
4239 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004240 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004241 return -EBADF;
4242
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004243 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004244 return 0;
4245}
4246
Jens Axboe229a7b62020-06-22 10:13:11 -06004247static int io_close(struct io_kiocb *req, bool force_nonblock,
4248 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004249{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004250 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004251 int ret;
4252
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004253 /* might be already done during nonblock submission */
4254 if (!close->put_file) {
4255 ret = __close_fd_get_file(close->fd, &close->put_file);
4256 if (ret < 0)
4257 return (ret == -ENOENT) ? -EBADF : ret;
4258 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004259
4260 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004261 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004262 /* was never set, but play safe */
4263 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004264 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004265 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004266 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004267 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004268
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004269 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004270 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004271 if (ret < 0)
4272 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004273 fput(close->put_file);
4274 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004275 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004276 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004277}
4278
Jens Axboe3529d8c2019-12-19 18:24:38 -07004279static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004280{
4281 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004282
4283 if (!req->file)
4284 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004285
4286 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4287 return -EINVAL;
4288 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4289 return -EINVAL;
4290
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004291 req->sync.off = READ_ONCE(sqe->off);
4292 req->sync.len = READ_ONCE(sqe->len);
4293 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004294 return 0;
4295}
4296
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004297static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004298{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004299 int ret;
4300
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004301 /* sync_file_range always requires a blocking context */
4302 if (force_nonblock)
4303 return -EAGAIN;
4304
Jens Axboe9adbd452019-12-20 08:45:55 -07004305 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004306 req->sync.flags);
4307 if (ret < 0)
4308 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004309 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004310 return 0;
4311}
4312
YueHaibing469956e2020-03-04 15:53:52 +08004313#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004314static int io_setup_async_msg(struct io_kiocb *req,
4315 struct io_async_msghdr *kmsg)
4316{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004317 struct io_async_msghdr *async_msg = req->async_data;
4318
4319 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004320 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004321 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004322 if (kmsg->iov != kmsg->fast_iov)
4323 kfree(kmsg->iov);
4324 return -ENOMEM;
4325 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004326 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004327 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004328 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004329 return -EAGAIN;
4330}
4331
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004332static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4333 struct io_async_msghdr *iomsg)
4334{
4335 iomsg->iov = iomsg->fast_iov;
4336 iomsg->msg.msg_name = &iomsg->addr;
4337 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4338 req->sr_msg.msg_flags, &iomsg->iov);
4339}
4340
Jens Axboe3529d8c2019-12-19 18:24:38 -07004341static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004342{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004343 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004344 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004345 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004346
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004347 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4348 return -EINVAL;
4349
Jens Axboee47293f2019-12-20 08:58:21 -07004350 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004351 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004352 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004353
Jens Axboed8768362020-02-27 14:17:49 -07004354#ifdef CONFIG_COMPAT
4355 if (req->ctx->compat)
4356 sr->msg_flags |= MSG_CMSG_COMPAT;
4357#endif
4358
Jens Axboee8c2bc12020-08-15 18:44:09 -07004359 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004360 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004361 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004362 if (!ret)
4363 req->flags |= REQ_F_NEED_CLEANUP;
4364 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004365}
4366
Jens Axboe229a7b62020-06-22 10:13:11 -06004367static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4368 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004369{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004370 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004371 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004372 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004373 int ret;
4374
Jens Axboe03b12302019-12-02 18:50:25 -07004375 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004376 if (unlikely(!sock))
4377 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004378
Jens Axboee8c2bc12020-08-15 18:44:09 -07004379 if (req->async_data) {
4380 kmsg = req->async_data;
4381 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004382 /* if iov is set, it's allocated already */
4383 if (!kmsg->iov)
4384 kmsg->iov = kmsg->fast_iov;
4385 kmsg->msg.msg_iter.iov = kmsg->iov;
4386 } else {
4387 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004388 if (ret)
4389 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004390 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004391 }
4392
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004393 flags = req->sr_msg.msg_flags;
4394 if (flags & MSG_DONTWAIT)
4395 req->flags |= REQ_F_NOWAIT;
4396 else if (force_nonblock)
4397 flags |= MSG_DONTWAIT;
4398
4399 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4400 if (force_nonblock && ret == -EAGAIN)
4401 return io_setup_async_msg(req, kmsg);
4402 if (ret == -ERESTARTSYS)
4403 ret = -EINTR;
4404
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004405 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004406 kfree(kmsg->iov);
4407 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004408 if (ret < 0)
4409 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004410 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004411 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004412}
4413
Jens Axboe229a7b62020-06-22 10:13:11 -06004414static int io_send(struct io_kiocb *req, bool force_nonblock,
4415 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004416{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004417 struct io_sr_msg *sr = &req->sr_msg;
4418 struct msghdr msg;
4419 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004420 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004421 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004422 int ret;
4423
4424 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004425 if (unlikely(!sock))
4426 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004427
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004428 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4429 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004430 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004431
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004432 msg.msg_name = NULL;
4433 msg.msg_control = NULL;
4434 msg.msg_controllen = 0;
4435 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004436
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004437 flags = req->sr_msg.msg_flags;
4438 if (flags & MSG_DONTWAIT)
4439 req->flags |= REQ_F_NOWAIT;
4440 else if (force_nonblock)
4441 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004442
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004443 msg.msg_flags = flags;
4444 ret = sock_sendmsg(sock, &msg);
4445 if (force_nonblock && ret == -EAGAIN)
4446 return -EAGAIN;
4447 if (ret == -ERESTARTSYS)
4448 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004449
Jens Axboe03b12302019-12-02 18:50:25 -07004450 if (ret < 0)
4451 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004452 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004453 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004454}
4455
Pavel Begunkov1400e692020-07-12 20:41:05 +03004456static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4457 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004458{
4459 struct io_sr_msg *sr = &req->sr_msg;
4460 struct iovec __user *uiov;
4461 size_t iov_len;
4462 int ret;
4463
Pavel Begunkov1400e692020-07-12 20:41:05 +03004464 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4465 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004466 if (ret)
4467 return ret;
4468
4469 if (req->flags & REQ_F_BUFFER_SELECT) {
4470 if (iov_len > 1)
4471 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004472 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004473 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004474 sr->len = iomsg->iov[0].iov_len;
4475 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004476 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004477 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004478 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004479 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4480 &iomsg->iov, &iomsg->msg.msg_iter,
4481 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004482 if (ret > 0)
4483 ret = 0;
4484 }
4485
4486 return ret;
4487}
4488
4489#ifdef CONFIG_COMPAT
4490static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004491 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004492{
4493 struct compat_msghdr __user *msg_compat;
4494 struct io_sr_msg *sr = &req->sr_msg;
4495 struct compat_iovec __user *uiov;
4496 compat_uptr_t ptr;
4497 compat_size_t len;
4498 int ret;
4499
Pavel Begunkov270a5942020-07-12 20:41:04 +03004500 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004501 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004502 &ptr, &len);
4503 if (ret)
4504 return ret;
4505
4506 uiov = compat_ptr(ptr);
4507 if (req->flags & REQ_F_BUFFER_SELECT) {
4508 compat_ssize_t clen;
4509
4510 if (len > 1)
4511 return -EINVAL;
4512 if (!access_ok(uiov, sizeof(*uiov)))
4513 return -EFAULT;
4514 if (__get_user(clen, &uiov->iov_len))
4515 return -EFAULT;
4516 if (clen < 0)
4517 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004518 sr->len = clen;
4519 iomsg->iov[0].iov_len = clen;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004520 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004521 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004522 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4523 UIO_FASTIOV, &iomsg->iov,
4524 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004525 if (ret < 0)
4526 return ret;
4527 }
4528
4529 return 0;
4530}
Jens Axboe03b12302019-12-02 18:50:25 -07004531#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004532
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{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004536 iomsg->msg.msg_name = &iomsg->addr;
4537 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004538
4539#ifdef CONFIG_COMPAT
4540 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004541 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004542#endif
4543
Pavel Begunkov1400e692020-07-12 20:41:05 +03004544 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004545}
4546
Jens Axboebcda7ba2020-02-23 16:42:51 -07004547static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004548 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004549{
4550 struct io_sr_msg *sr = &req->sr_msg;
4551 struct io_buffer *kbuf;
4552
Jens Axboebcda7ba2020-02-23 16:42:51 -07004553 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4554 if (IS_ERR(kbuf))
4555 return kbuf;
4556
4557 sr->kbuf = kbuf;
4558 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004559 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004560}
4561
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004562static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4563{
4564 return io_put_kbuf(req, req->sr_msg.kbuf);
4565}
4566
Jens Axboe3529d8c2019-12-19 18:24:38 -07004567static int io_recvmsg_prep(struct io_kiocb *req,
4568 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004569{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004570 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004571 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004572 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004573
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004574 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4575 return -EINVAL;
4576
Jens Axboe3529d8c2019-12-19 18:24:38 -07004577 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004578 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004579 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004580 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004581
Jens Axboed8768362020-02-27 14:17:49 -07004582#ifdef CONFIG_COMPAT
4583 if (req->ctx->compat)
4584 sr->msg_flags |= MSG_CMSG_COMPAT;
4585#endif
4586
Jens Axboee8c2bc12020-08-15 18:44:09 -07004587 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004588 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004589 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004590 if (!ret)
4591 req->flags |= REQ_F_NEED_CLEANUP;
4592 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004593}
4594
Jens Axboe229a7b62020-06-22 10:13:11 -06004595static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4596 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004597{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004598 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004599 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004600 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004601 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004602 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004603
Jens Axboe0fa03c62019-04-19 13:34:07 -06004604 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004605 if (unlikely(!sock))
4606 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004607
Jens Axboee8c2bc12020-08-15 18:44:09 -07004608 if (req->async_data) {
4609 kmsg = req->async_data;
4610 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004611 /* if iov is set, it's allocated already */
4612 if (!kmsg->iov)
4613 kmsg->iov = kmsg->fast_iov;
4614 kmsg->msg.msg_iter.iov = kmsg->iov;
4615 } else {
4616 ret = io_recvmsg_copy_hdr(req, &iomsg);
4617 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004618 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004619 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004620 }
4621
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004622 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004623 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004624 if (IS_ERR(kbuf))
4625 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004626 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4627 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4628 1, req->sr_msg.len);
4629 }
4630
4631 flags = req->sr_msg.msg_flags;
4632 if (flags & MSG_DONTWAIT)
4633 req->flags |= REQ_F_NOWAIT;
4634 else if (force_nonblock)
4635 flags |= MSG_DONTWAIT;
4636
4637 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4638 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004639 if (force_nonblock && ret == -EAGAIN)
4640 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004641 if (ret == -ERESTARTSYS)
4642 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004643
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004644 if (req->flags & REQ_F_BUFFER_SELECTED)
4645 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004646 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004647 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004648 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004649 if (ret < 0)
4650 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004651 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004652 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004653}
4654
Jens Axboe229a7b62020-06-22 10:13:11 -06004655static int io_recv(struct io_kiocb *req, bool force_nonblock,
4656 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004657{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004658 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004659 struct io_sr_msg *sr = &req->sr_msg;
4660 struct msghdr msg;
4661 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004662 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004663 struct iovec iov;
4664 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004665 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004666
Jens Axboefddafac2020-01-04 20:19:44 -07004667 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004668 if (unlikely(!sock))
4669 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004670
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004671 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004672 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004673 if (IS_ERR(kbuf))
4674 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004675 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004676 }
4677
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004678 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004679 if (unlikely(ret))
4680 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004681
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004682 msg.msg_name = NULL;
4683 msg.msg_control = NULL;
4684 msg.msg_controllen = 0;
4685 msg.msg_namelen = 0;
4686 msg.msg_iocb = NULL;
4687 msg.msg_flags = 0;
4688
4689 flags = req->sr_msg.msg_flags;
4690 if (flags & MSG_DONTWAIT)
4691 req->flags |= REQ_F_NOWAIT;
4692 else if (force_nonblock)
4693 flags |= MSG_DONTWAIT;
4694
4695 ret = sock_recvmsg(sock, &msg, flags);
4696 if (force_nonblock && ret == -EAGAIN)
4697 return -EAGAIN;
4698 if (ret == -ERESTARTSYS)
4699 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004700out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004701 if (req->flags & REQ_F_BUFFER_SELECTED)
4702 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004703 if (ret < 0)
4704 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004705 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004706 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004707}
4708
Jens Axboe3529d8c2019-12-19 18:24:38 -07004709static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004710{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004711 struct io_accept *accept = &req->accept;
4712
Jens Axboe17f2fe32019-10-17 14:42:58 -06004713 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4714 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004715 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004716 return -EINVAL;
4717
Jens Axboed55e5f52019-12-11 16:12:15 -07004718 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4719 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004720 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004721 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004722 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004723}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004724
Jens Axboe229a7b62020-06-22 10:13:11 -06004725static int io_accept(struct io_kiocb *req, bool force_nonblock,
4726 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004727{
4728 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004729 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004730 int ret;
4731
Jiufei Xuee697dee2020-06-10 13:41:59 +08004732 if (req->file->f_flags & O_NONBLOCK)
4733 req->flags |= REQ_F_NOWAIT;
4734
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004735 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004736 accept->addr_len, accept->flags,
4737 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004738 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004739 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004740 if (ret < 0) {
4741 if (ret == -ERESTARTSYS)
4742 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004743 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004744 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004745 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004746 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004747}
4748
Jens Axboe3529d8c2019-12-19 18:24:38 -07004749static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004750{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004751 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004752 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004753
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004754 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4755 return -EINVAL;
4756 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4757 return -EINVAL;
4758
Jens Axboe3529d8c2019-12-19 18:24:38 -07004759 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4760 conn->addr_len = READ_ONCE(sqe->addr2);
4761
4762 if (!io)
4763 return 0;
4764
4765 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004766 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07004767}
4768
Jens Axboe229a7b62020-06-22 10:13:11 -06004769static int io_connect(struct io_kiocb *req, bool force_nonblock,
4770 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004771{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004772 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004773 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004774 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004775
Jens Axboee8c2bc12020-08-15 18:44:09 -07004776 if (req->async_data) {
4777 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004778 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004779 ret = move_addr_to_kernel(req->connect.addr,
4780 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004781 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004782 if (ret)
4783 goto out;
4784 io = &__io;
4785 }
4786
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004787 file_flags = force_nonblock ? O_NONBLOCK : 0;
4788
Jens Axboee8c2bc12020-08-15 18:44:09 -07004789 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004790 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004791 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004792 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004793 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004794 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004795 ret = -ENOMEM;
4796 goto out;
4797 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004798 io = req->async_data;
4799 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004800 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004801 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004802 if (ret == -ERESTARTSYS)
4803 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004804out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004805 if (ret < 0)
4806 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004807 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004808 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004809}
YueHaibing469956e2020-03-04 15:53:52 +08004810#else /* !CONFIG_NET */
4811static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4812{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004813 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004814}
4815
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004816static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4817 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004818{
YueHaibing469956e2020-03-04 15:53:52 +08004819 return -EOPNOTSUPP;
4820}
4821
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004822static int io_send(struct io_kiocb *req, bool force_nonblock,
4823 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004824{
4825 return -EOPNOTSUPP;
4826}
4827
4828static int io_recvmsg_prep(struct io_kiocb *req,
4829 const struct io_uring_sqe *sqe)
4830{
4831 return -EOPNOTSUPP;
4832}
4833
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004834static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4835 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004836{
4837 return -EOPNOTSUPP;
4838}
4839
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004840static int io_recv(struct io_kiocb *req, bool force_nonblock,
4841 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004842{
4843 return -EOPNOTSUPP;
4844}
4845
4846static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4847{
4848 return -EOPNOTSUPP;
4849}
4850
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004851static int io_accept(struct io_kiocb *req, bool force_nonblock,
4852 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004853{
4854 return -EOPNOTSUPP;
4855}
4856
4857static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4858{
4859 return -EOPNOTSUPP;
4860}
4861
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004862static int io_connect(struct io_kiocb *req, bool force_nonblock,
4863 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004864{
4865 return -EOPNOTSUPP;
4866}
4867#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004868
Jens Axboed7718a92020-02-14 22:23:12 -07004869struct io_poll_table {
4870 struct poll_table_struct pt;
4871 struct io_kiocb *req;
4872 int error;
4873};
4874
Jens Axboed7718a92020-02-14 22:23:12 -07004875static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4876 __poll_t mask, task_work_func_t func)
4877{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004878 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004879 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004880
4881 /* for instances that support it check for an event match first: */
4882 if (mask && !(mask & poll->events))
4883 return 0;
4884
4885 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4886
4887 list_del_init(&poll->wait.entry);
4888
Jens Axboed7718a92020-02-14 22:23:12 -07004889 req->result = mask;
4890 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004891 percpu_ref_get(&req->ctx->refs);
4892
Jens Axboed7718a92020-02-14 22:23:12 -07004893 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004894 * If we using the signalfd wait_queue_head for this wakeup, then
4895 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4896 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4897 * either, as the normal wakeup will suffice.
4898 */
4899 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4900
4901 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004902 * If this fails, then the task is exiting. When a task exits, the
4903 * work gets canceled, so just cancel this request as well instead
4904 * of executing it. We can't safely execute it anyway, as we may not
4905 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004906 */
Jens Axboe87c43112020-09-30 21:00:14 -06004907 ret = io_req_task_work_add(req, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004908 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004909 struct task_struct *tsk;
4910
Jens Axboee3aabf92020-05-18 11:04:17 -06004911 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004912 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06004913 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06004914 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004915 }
Jens Axboed7718a92020-02-14 22:23:12 -07004916 return 1;
4917}
4918
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004919static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4920 __acquires(&req->ctx->completion_lock)
4921{
4922 struct io_ring_ctx *ctx = req->ctx;
4923
4924 if (!req->result && !READ_ONCE(poll->canceled)) {
4925 struct poll_table_struct pt = { ._key = poll->events };
4926
4927 req->result = vfs_poll(req->file, &pt) & poll->events;
4928 }
4929
4930 spin_lock_irq(&ctx->completion_lock);
4931 if (!req->result && !READ_ONCE(poll->canceled)) {
4932 add_wait_queue(poll->head, &poll->wait);
4933 return true;
4934 }
4935
4936 return false;
4937}
4938
Jens Axboed4e7cd32020-08-15 11:44:50 -07004939static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004940{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004941 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004942 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004943 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004944 return req->apoll->double_poll;
4945}
4946
4947static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4948{
4949 if (req->opcode == IORING_OP_POLL_ADD)
4950 return &req->poll;
4951 return &req->apoll->poll;
4952}
4953
4954static void io_poll_remove_double(struct io_kiocb *req)
4955{
4956 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004957
4958 lockdep_assert_held(&req->ctx->completion_lock);
4959
4960 if (poll && poll->head) {
4961 struct wait_queue_head *head = poll->head;
4962
4963 spin_lock(&head->lock);
4964 list_del_init(&poll->wait.entry);
4965 if (poll->wait.private)
4966 refcount_dec(&req->refs);
4967 poll->head = NULL;
4968 spin_unlock(&head->lock);
4969 }
4970}
4971
4972static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4973{
4974 struct io_ring_ctx *ctx = req->ctx;
4975
Jens Axboed4e7cd32020-08-15 11:44:50 -07004976 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004977 req->poll.done = true;
4978 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4979 io_commit_cqring(ctx);
4980}
4981
Jens Axboe18bceab2020-05-15 11:56:54 -06004982static void io_poll_task_func(struct callback_head *cb)
4983{
4984 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004985 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004986 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06004987
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004988 if (io_poll_rewait(req, &req->poll)) {
4989 spin_unlock_irq(&ctx->completion_lock);
4990 } else {
4991 hash_del(&req->hash_node);
4992 io_poll_complete(req, req->result, 0);
4993 spin_unlock_irq(&ctx->completion_lock);
4994
4995 nxt = io_put_req_find_next(req);
4996 io_cqring_ev_posted(ctx);
4997 if (nxt)
4998 __io_req_task_submit(nxt);
4999 }
5000
Jens Axboe6d816e02020-08-11 08:04:14 -06005001 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005002}
5003
5004static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5005 int sync, void *key)
5006{
5007 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005008 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005009 __poll_t mask = key_to_poll(key);
5010
5011 /* for instances that support it check for an event match first: */
5012 if (mask && !(mask & poll->events))
5013 return 0;
5014
Jens Axboe8706e042020-09-28 08:38:54 -06005015 list_del_init(&wait->entry);
5016
Jens Axboe807abcb2020-07-17 17:09:27 -06005017 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005018 bool done;
5019
Jens Axboe807abcb2020-07-17 17:09:27 -06005020 spin_lock(&poll->head->lock);
5021 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005022 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005023 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005024 /* make sure double remove sees this as being gone */
5025 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005026 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005027 if (!done) {
5028 /* use wait func handler, so it matches the rq type */
5029 poll->wait.func(&poll->wait, mode, sync, key);
5030 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005031 }
5032 refcount_dec(&req->refs);
5033 return 1;
5034}
5035
5036static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5037 wait_queue_func_t wake_func)
5038{
5039 poll->head = NULL;
5040 poll->done = false;
5041 poll->canceled = false;
5042 poll->events = events;
5043 INIT_LIST_HEAD(&poll->wait.entry);
5044 init_waitqueue_func_entry(&poll->wait, wake_func);
5045}
5046
5047static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005048 struct wait_queue_head *head,
5049 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005050{
5051 struct io_kiocb *req = pt->req;
5052
5053 /*
5054 * If poll->head is already set, it's because the file being polled
5055 * uses multiple waitqueues for poll handling (eg one for read, one
5056 * for write). Setup a separate io_poll_iocb if this happens.
5057 */
5058 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005059 struct io_poll_iocb *poll_one = poll;
5060
Jens Axboe18bceab2020-05-15 11:56:54 -06005061 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005062 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005063 pt->error = -EINVAL;
5064 return;
5065 }
5066 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5067 if (!poll) {
5068 pt->error = -ENOMEM;
5069 return;
5070 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005071 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005072 refcount_inc(&req->refs);
5073 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005074 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005075 }
5076
5077 pt->error = 0;
5078 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005079
5080 if (poll->events & EPOLLEXCLUSIVE)
5081 add_wait_queue_exclusive(head, &poll->wait);
5082 else
5083 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005084}
5085
5086static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5087 struct poll_table_struct *p)
5088{
5089 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005090 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005091
Jens Axboe807abcb2020-07-17 17:09:27 -06005092 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005093}
5094
Jens Axboed7718a92020-02-14 22:23:12 -07005095static void io_async_task_func(struct callback_head *cb)
5096{
5097 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5098 struct async_poll *apoll = req->apoll;
5099 struct io_ring_ctx *ctx = req->ctx;
5100
5101 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5102
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005103 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005104 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005105 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005106 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005107 }
5108
Jens Axboe31067252020-05-17 17:43:31 -06005109 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005110 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005111 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005112
Jens Axboed4e7cd32020-08-15 11:44:50 -07005113 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005114 spin_unlock_irq(&ctx->completion_lock);
5115
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005116 if (!READ_ONCE(apoll->poll.canceled))
5117 __io_req_task_submit(req);
5118 else
5119 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005120
Jens Axboe6d816e02020-08-11 08:04:14 -06005121 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005122 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005123 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005124}
5125
5126static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5127 void *key)
5128{
5129 struct io_kiocb *req = wait->private;
5130 struct io_poll_iocb *poll = &req->apoll->poll;
5131
5132 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5133 key_to_poll(key));
5134
5135 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5136}
5137
5138static void io_poll_req_insert(struct io_kiocb *req)
5139{
5140 struct io_ring_ctx *ctx = req->ctx;
5141 struct hlist_head *list;
5142
5143 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5144 hlist_add_head(&req->hash_node, list);
5145}
5146
5147static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5148 struct io_poll_iocb *poll,
5149 struct io_poll_table *ipt, __poll_t mask,
5150 wait_queue_func_t wake_func)
5151 __acquires(&ctx->completion_lock)
5152{
5153 struct io_ring_ctx *ctx = req->ctx;
5154 bool cancel = false;
5155
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005156 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005157 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005158 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005159 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005160
5161 ipt->pt._key = mask;
5162 ipt->req = req;
5163 ipt->error = -EINVAL;
5164
Jens Axboed7718a92020-02-14 22:23:12 -07005165 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5166
5167 spin_lock_irq(&ctx->completion_lock);
5168 if (likely(poll->head)) {
5169 spin_lock(&poll->head->lock);
5170 if (unlikely(list_empty(&poll->wait.entry))) {
5171 if (ipt->error)
5172 cancel = true;
5173 ipt->error = 0;
5174 mask = 0;
5175 }
5176 if (mask || ipt->error)
5177 list_del_init(&poll->wait.entry);
5178 else if (cancel)
5179 WRITE_ONCE(poll->canceled, true);
5180 else if (!poll->done) /* actually waiting for an event */
5181 io_poll_req_insert(req);
5182 spin_unlock(&poll->head->lock);
5183 }
5184
5185 return mask;
5186}
5187
5188static bool io_arm_poll_handler(struct io_kiocb *req)
5189{
5190 const struct io_op_def *def = &io_op_defs[req->opcode];
5191 struct io_ring_ctx *ctx = req->ctx;
5192 struct async_poll *apoll;
5193 struct io_poll_table ipt;
5194 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005195 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005196
5197 if (!req->file || !file_can_poll(req->file))
5198 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005199 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005200 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005201 if (def->pollin)
5202 rw = READ;
5203 else if (def->pollout)
5204 rw = WRITE;
5205 else
5206 return false;
5207 /* if we can't nonblock try, then no point in arming a poll handler */
5208 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005209 return false;
5210
5211 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5212 if (unlikely(!apoll))
5213 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005214 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005215
5216 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005217 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005218
Nathan Chancellor8755d972020-03-02 16:01:19 -07005219 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005220 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005221 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005222 if (def->pollout)
5223 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005224
5225 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5226 if ((req->opcode == IORING_OP_RECVMSG) &&
5227 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5228 mask &= ~POLLIN;
5229
Jens Axboed7718a92020-02-14 22:23:12 -07005230 mask |= POLLERR | POLLPRI;
5231
5232 ipt.pt._qproc = io_async_queue_proc;
5233
5234 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5235 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005236 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005237 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005238 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005239 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005240 kfree(apoll);
5241 return false;
5242 }
5243 spin_unlock_irq(&ctx->completion_lock);
5244 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5245 apoll->poll.events);
5246 return true;
5247}
5248
5249static bool __io_poll_remove_one(struct io_kiocb *req,
5250 struct io_poll_iocb *poll)
5251{
Jens Axboeb41e9852020-02-17 09:52:41 -07005252 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005253
5254 spin_lock(&poll->head->lock);
5255 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005256 if (!list_empty(&poll->wait.entry)) {
5257 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005258 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005259 }
5260 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005261 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005262 return do_complete;
5263}
5264
5265static bool io_poll_remove_one(struct io_kiocb *req)
5266{
5267 bool do_complete;
5268
Jens Axboed4e7cd32020-08-15 11:44:50 -07005269 io_poll_remove_double(req);
5270
Jens Axboed7718a92020-02-14 22:23:12 -07005271 if (req->opcode == IORING_OP_POLL_ADD) {
5272 do_complete = __io_poll_remove_one(req, &req->poll);
5273 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005274 struct async_poll *apoll = req->apoll;
5275
Jens Axboed7718a92020-02-14 22:23:12 -07005276 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005277 do_complete = __io_poll_remove_one(req, &apoll->poll);
5278 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005279 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005280 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005281 kfree(apoll);
5282 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005283 }
5284
Jens Axboeb41e9852020-02-17 09:52:41 -07005285 if (do_complete) {
5286 io_cqring_fill_event(req, -ECANCELED);
5287 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005288 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005289 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005290 }
5291
5292 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005293}
5294
Jens Axboe76e1b642020-09-26 15:05:03 -06005295/*
5296 * Returns true if we found and killed one or more poll requests
5297 */
5298static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005299{
Jens Axboe78076bb2019-12-04 19:56:40 -07005300 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005301 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005302 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005303
5304 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005305 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5306 struct hlist_head *list;
5307
5308 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005309 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5310 if (io_task_match(req, tsk))
5311 posted += io_poll_remove_one(req);
5312 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005313 }
5314 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005315
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005316 if (posted)
5317 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005318
5319 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005320}
5321
Jens Axboe47f46762019-11-09 17:43:02 -07005322static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5323{
Jens Axboe78076bb2019-12-04 19:56:40 -07005324 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005325 struct io_kiocb *req;
5326
Jens Axboe78076bb2019-12-04 19:56:40 -07005327 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5328 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005329 if (sqe_addr != req->user_data)
5330 continue;
5331 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005332 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005333 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005334 }
5335
5336 return -ENOENT;
5337}
5338
Jens Axboe3529d8c2019-12-19 18:24:38 -07005339static int io_poll_remove_prep(struct io_kiocb *req,
5340 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005341{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005342 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5343 return -EINVAL;
5344 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5345 sqe->poll_events)
5346 return -EINVAL;
5347
Jens Axboe0969e782019-12-17 18:40:57 -07005348 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005349 return 0;
5350}
5351
5352/*
5353 * Find a running poll command that matches one specified in sqe->addr,
5354 * and remove it if found.
5355 */
5356static int io_poll_remove(struct io_kiocb *req)
5357{
5358 struct io_ring_ctx *ctx = req->ctx;
5359 u64 addr;
5360 int ret;
5361
Jens Axboe0969e782019-12-17 18:40:57 -07005362 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005363 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005364 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005365 spin_unlock_irq(&ctx->completion_lock);
5366
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005367 if (ret < 0)
5368 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005369 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005370 return 0;
5371}
5372
Jens Axboe221c5eb2019-01-17 09:41:58 -07005373static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5374 void *key)
5375{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005376 struct io_kiocb *req = wait->private;
5377 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005378
Jens Axboed7718a92020-02-14 22:23:12 -07005379 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005380}
5381
Jens Axboe221c5eb2019-01-17 09:41:58 -07005382static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5383 struct poll_table_struct *p)
5384{
5385 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5386
Jens Axboee8c2bc12020-08-15 18:44:09 -07005387 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005388}
5389
Jens Axboe3529d8c2019-12-19 18:24:38 -07005390static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005391{
5392 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005393 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005394
5395 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5396 return -EINVAL;
5397 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5398 return -EINVAL;
5399
Jiufei Xue5769a352020-06-17 17:53:55 +08005400 events = READ_ONCE(sqe->poll32_events);
5401#ifdef __BIG_ENDIAN
5402 events = swahw32(events);
5403#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005404 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5405 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005406 return 0;
5407}
5408
Pavel Begunkov014db002020-03-03 21:33:12 +03005409static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005410{
5411 struct io_poll_iocb *poll = &req->poll;
5412 struct io_ring_ctx *ctx = req->ctx;
5413 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005414 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005415
Jens Axboed7718a92020-02-14 22:23:12 -07005416 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005417
Jens Axboed7718a92020-02-14 22:23:12 -07005418 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5419 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005420
Jens Axboe8c838782019-03-12 15:48:16 -06005421 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005422 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005423 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005424 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005425 spin_unlock_irq(&ctx->completion_lock);
5426
Jens Axboe8c838782019-03-12 15:48:16 -06005427 if (mask) {
5428 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005429 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005430 }
Jens Axboe8c838782019-03-12 15:48:16 -06005431 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005432}
5433
Jens Axboe5262f562019-09-17 12:26:57 -06005434static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5435{
Jens Axboead8a48a2019-11-15 08:49:11 -07005436 struct io_timeout_data *data = container_of(timer,
5437 struct io_timeout_data, timer);
5438 struct io_kiocb *req = data->req;
5439 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005440 unsigned long flags;
5441
Jens Axboe5262f562019-09-17 12:26:57 -06005442 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005443 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005444 atomic_set(&req->ctx->cq_timeouts,
5445 atomic_read(&req->ctx->cq_timeouts) + 1);
5446
Jens Axboe78e19bb2019-11-06 15:21:34 -07005447 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005448 io_commit_cqring(ctx);
5449 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5450
5451 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005452 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005453 io_put_req(req);
5454 return HRTIMER_NORESTART;
5455}
5456
Jens Axboef254ac02020-08-12 17:33:30 -06005457static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005458{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005459 struct io_timeout_data *io = req->async_data;
Jens Axboef254ac02020-08-12 17:33:30 -06005460 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005461
Jens Axboee8c2bc12020-08-15 18:44:09 -07005462 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005463 if (ret == -1)
5464 return -EALREADY;
Pavel Begunkova71976f2020-10-10 18:34:11 +01005465 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005466
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005467 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005468 io_cqring_fill_event(req, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005469 io_put_req_deferred(req, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07005470 return 0;
5471}
5472
Jens Axboef254ac02020-08-12 17:33:30 -06005473static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5474{
5475 struct io_kiocb *req;
5476 int ret = -ENOENT;
5477
5478 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5479 if (user_data == req->user_data) {
5480 ret = 0;
5481 break;
5482 }
5483 }
5484
5485 if (ret == -ENOENT)
5486 return ret;
5487
5488 return __io_timeout_cancel(req);
5489}
5490
Jens Axboe3529d8c2019-12-19 18:24:38 -07005491static int io_timeout_remove_prep(struct io_kiocb *req,
5492 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005493{
Jens Axboeb29472e2019-12-17 18:50:29 -07005494 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5495 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005496 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5497 return -EINVAL;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005498 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags)
Jens Axboeb29472e2019-12-17 18:50:29 -07005499 return -EINVAL;
5500
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005501 req->timeout_rem.addr = READ_ONCE(sqe->addr);
Jens Axboeb29472e2019-12-17 18:50:29 -07005502 return 0;
5503}
5504
Jens Axboe11365042019-10-16 09:08:32 -06005505/*
5506 * Remove or update an existing timeout command
5507 */
Jens Axboefc4df992019-12-10 14:38:45 -07005508static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005509{
5510 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005511 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005512
Jens Axboe11365042019-10-16 09:08:32 -06005513 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +01005514 ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005515
Jens Axboe47f46762019-11-09 17:43:02 -07005516 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005517 io_commit_cqring(ctx);
5518 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005519 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005520 if (ret < 0)
5521 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005522 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005523 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005524}
5525
Jens Axboe3529d8c2019-12-19 18:24:38 -07005526static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005527 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005528{
Jens Axboead8a48a2019-11-15 08:49:11 -07005529 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005530 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005531 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005532
Jens Axboead8a48a2019-11-15 08:49:11 -07005533 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005534 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005535 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005536 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005537 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005538 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005539 flags = READ_ONCE(sqe->timeout_flags);
5540 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005541 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005542
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005543 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005544
Jens Axboee8c2bc12020-08-15 18:44:09 -07005545 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005546 return -ENOMEM;
5547
Jens Axboee8c2bc12020-08-15 18:44:09 -07005548 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005549 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005550
5551 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005552 return -EFAULT;
5553
Jens Axboe11365042019-10-16 09:08:32 -06005554 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005555 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005556 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005557 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005558
Jens Axboead8a48a2019-11-15 08:49:11 -07005559 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5560 return 0;
5561}
5562
Jens Axboefc4df992019-12-10 14:38:45 -07005563static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005564{
Jens Axboead8a48a2019-11-15 08:49:11 -07005565 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005566 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005567 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005568 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005569
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005570 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005571
Jens Axboe5262f562019-09-17 12:26:57 -06005572 /*
5573 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005574 * timeout event to be satisfied. If it isn't set, then this is
5575 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005576 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005577 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005578 entry = ctx->timeout_list.prev;
5579 goto add;
5580 }
Jens Axboe5262f562019-09-17 12:26:57 -06005581
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005582 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5583 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005584
5585 /*
5586 * Insertion sort, ensuring the first entry in the list is always
5587 * the one we need first.
5588 */
Jens Axboe5262f562019-09-17 12:26:57 -06005589 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005590 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5591 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005592
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005593 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005594 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005595 /* nxt.seq is behind @tail, otherwise would've been completed */
5596 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005597 break;
5598 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005599add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005600 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005601 data->timer.function = io_timeout_fn;
5602 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005603 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005604 return 0;
5605}
5606
Jens Axboe62755e32019-10-28 21:49:21 -06005607static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005608{
Jens Axboe62755e32019-10-28 21:49:21 -06005609 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005610
Jens Axboe62755e32019-10-28 21:49:21 -06005611 return req->user_data == (unsigned long) data;
5612}
5613
Jens Axboee977d6d2019-11-05 12:39:45 -07005614static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005615{
Jens Axboe62755e32019-10-28 21:49:21 -06005616 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005617 int ret = 0;
5618
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005619 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005620 switch (cancel_ret) {
5621 case IO_WQ_CANCEL_OK:
5622 ret = 0;
5623 break;
5624 case IO_WQ_CANCEL_RUNNING:
5625 ret = -EALREADY;
5626 break;
5627 case IO_WQ_CANCEL_NOTFOUND:
5628 ret = -ENOENT;
5629 break;
5630 }
5631
Jens Axboee977d6d2019-11-05 12:39:45 -07005632 return ret;
5633}
5634
Jens Axboe47f46762019-11-09 17:43:02 -07005635static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5636 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005637 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005638{
5639 unsigned long flags;
5640 int ret;
5641
5642 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5643 if (ret != -ENOENT) {
5644 spin_lock_irqsave(&ctx->completion_lock, flags);
5645 goto done;
5646 }
5647
5648 spin_lock_irqsave(&ctx->completion_lock, flags);
5649 ret = io_timeout_cancel(ctx, sqe_addr);
5650 if (ret != -ENOENT)
5651 goto done;
5652 ret = io_poll_cancel(ctx, sqe_addr);
5653done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005654 if (!ret)
5655 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005656 io_cqring_fill_event(req, ret);
5657 io_commit_cqring(ctx);
5658 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5659 io_cqring_ev_posted(ctx);
5660
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005661 if (ret < 0)
5662 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005663 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005664}
5665
Jens Axboe3529d8c2019-12-19 18:24:38 -07005666static int io_async_cancel_prep(struct io_kiocb *req,
5667 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005668{
Jens Axboefbf23842019-12-17 18:45:56 -07005669 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005670 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005671 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5672 return -EINVAL;
5673 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005674 return -EINVAL;
5675
Jens Axboefbf23842019-12-17 18:45:56 -07005676 req->cancel.addr = READ_ONCE(sqe->addr);
5677 return 0;
5678}
5679
Pavel Begunkov014db002020-03-03 21:33:12 +03005680static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005681{
5682 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005683
Pavel Begunkov014db002020-03-03 21:33:12 +03005684 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005685 return 0;
5686}
5687
Jens Axboe05f3fb32019-12-09 11:22:50 -07005688static int io_files_update_prep(struct io_kiocb *req,
5689 const struct io_uring_sqe *sqe)
5690{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005691 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5692 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005693 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5694 return -EINVAL;
5695 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005696 return -EINVAL;
5697
5698 req->files_update.offset = READ_ONCE(sqe->off);
5699 req->files_update.nr_args = READ_ONCE(sqe->len);
5700 if (!req->files_update.nr_args)
5701 return -EINVAL;
5702 req->files_update.arg = READ_ONCE(sqe->addr);
5703 return 0;
5704}
5705
Jens Axboe229a7b62020-06-22 10:13:11 -06005706static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5707 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005708{
5709 struct io_ring_ctx *ctx = req->ctx;
5710 struct io_uring_files_update up;
5711 int ret;
5712
Jens Axboef86cd202020-01-29 13:46:44 -07005713 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005714 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005715
5716 up.offset = req->files_update.offset;
5717 up.fds = req->files_update.arg;
5718
5719 mutex_lock(&ctx->uring_lock);
5720 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5721 mutex_unlock(&ctx->uring_lock);
5722
5723 if (ret < 0)
5724 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005725 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005726 return 0;
5727}
5728
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005729static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005730{
Jens Axboed625c6e2019-12-17 19:53:05 -07005731 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005732 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005733 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005734 case IORING_OP_READV:
5735 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005736 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005737 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005738 case IORING_OP_WRITEV:
5739 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005740 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005741 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005742 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005743 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005744 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005745 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005746 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005747 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005748 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005749 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005750 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005751 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005752 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005753 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005754 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005755 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005756 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005757 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005758 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005759 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005760 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005761 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005762 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005763 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005764 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005765 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005766 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005767 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005768 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005769 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005770 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005771 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005772 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005773 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005774 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005775 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005776 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005777 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005778 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005779 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005780 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005781 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005782 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005783 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005784 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005785 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005786 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005787 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005788 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005789 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005790 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005791 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005792 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005793 return io_tee_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005794 }
5795
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005796 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5797 req->opcode);
5798 return-EINVAL;
5799}
5800
Jens Axboedef596e2019-01-09 08:59:42 -07005801static int io_req_defer_prep(struct io_kiocb *req,
5802 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07005803{
Jens Axboedef596e2019-01-09 08:59:42 -07005804 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005805 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005806 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005807 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005808 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005809}
5810
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005811static u32 io_get_sequence(struct io_kiocb *req)
5812{
5813 struct io_kiocb *pos;
5814 struct io_ring_ctx *ctx = req->ctx;
5815 u32 total_submitted, nr_reqs = 1;
5816
5817 if (req->flags & REQ_F_LINK_HEAD)
5818 list_for_each_entry(pos, &req->link_list, link_list)
5819 nr_reqs++;
5820
5821 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5822 return total_submitted - nr_reqs;
5823}
5824
Jens Axboe3529d8c2019-12-19 18:24:38 -07005825static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005826{
5827 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005828 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005829 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005830 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005831
5832 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005833 if (likely(list_empty_careful(&ctx->defer_list) &&
5834 !(req->flags & REQ_F_IO_DRAIN)))
5835 return 0;
5836
5837 seq = io_get_sequence(req);
5838 /* Still a chance to pass the sequence check */
5839 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005840 return 0;
5841
Jens Axboee8c2bc12020-08-15 18:44:09 -07005842 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005843 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005844 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005845 return ret;
5846 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005847 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005848 de = kmalloc(sizeof(*de), GFP_KERNEL);
5849 if (!de)
5850 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005851
5852 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005853 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005854 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005855 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005856 io_queue_async_work(req);
5857 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005858 }
5859
5860 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005861 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005862 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005863 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005864 spin_unlock_irq(&ctx->completion_lock);
5865 return -EIOCBQUEUED;
5866}
Jens Axboeedafcce2019-01-09 09:16:05 -07005867
Jens Axboef573d382020-09-22 10:19:24 -06005868static void io_req_drop_files(struct io_kiocb *req)
5869{
5870 struct io_ring_ctx *ctx = req->ctx;
5871 unsigned long flags;
5872
Jens Axboe98447d62020-10-14 10:48:51 -06005873 put_files_struct(req->work.identity->files);
5874 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov52504a62020-12-18 13:12:21 +00005875 spin_lock_irqsave(&ctx->inflight_lock, flags);
5876 list_del(&req->inflight_entry);
5877 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5878 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboedfead8a2020-10-14 10:12:37 -06005879 req->work.flags &= ~IO_WQ_WORK_FILES;
Pavel Begunkov52504a62020-12-18 13:12:21 +00005880 if (waitqueue_active(&ctx->inflight_wait))
5881 wake_up(&ctx->inflight_wait);
Jens Axboef573d382020-09-22 10:19:24 -06005882}
5883
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005884static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005885{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005886 if (req->flags & REQ_F_BUFFER_SELECTED) {
5887 switch (req->opcode) {
5888 case IORING_OP_READV:
5889 case IORING_OP_READ_FIXED:
5890 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005891 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005892 break;
5893 case IORING_OP_RECVMSG:
5894 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005895 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005896 break;
5897 }
5898 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005899 }
5900
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005901 if (req->flags & REQ_F_NEED_CLEANUP) {
5902 switch (req->opcode) {
5903 case IORING_OP_READV:
5904 case IORING_OP_READ_FIXED:
5905 case IORING_OP_READ:
5906 case IORING_OP_WRITEV:
5907 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005908 case IORING_OP_WRITE: {
5909 struct io_async_rw *io = req->async_data;
5910 if (io->free_iovec)
5911 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005912 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005913 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005914 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005915 case IORING_OP_SENDMSG: {
5916 struct io_async_msghdr *io = req->async_data;
5917 if (io->iov != io->fast_iov)
5918 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005919 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005920 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005921 case IORING_OP_SPLICE:
5922 case IORING_OP_TEE:
5923 io_put_file(req, req->splice.file_in,
5924 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5925 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005926 case IORING_OP_OPENAT:
5927 case IORING_OP_OPENAT2:
5928 if (req->open.filename)
5929 putname(req->open.filename);
5930 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005931 }
5932 req->flags &= ~REQ_F_NEED_CLEANUP;
5933 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005934
Jens Axboef573d382020-09-22 10:19:24 -06005935 if (req->flags & REQ_F_INFLIGHT)
5936 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005937}
5938
Pavel Begunkovc1379e22020-09-30 22:57:56 +03005939static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
5940 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07005941{
Jens Axboeedafcce2019-01-09 09:16:05 -07005942 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005943 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005944
Jens Axboed625c6e2019-12-17 19:53:05 -07005945 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005946 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005947 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005948 break;
5949 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005950 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005951 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06005952 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005953 break;
5954 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005955 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005956 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06005957 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005958 break;
5959 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03005960 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005961 break;
5962 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03005963 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005964 break;
5965 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07005966 ret = io_poll_remove(req);
5967 break;
5968 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005969 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07005970 break;
5971 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005972 ret = io_sendmsg(req, force_nonblock, cs);
5973 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005974 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005975 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07005976 break;
5977 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005978 ret = io_recvmsg(req, force_nonblock, cs);
5979 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005980 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005981 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005982 break;
5983 case IORING_OP_TIMEOUT:
5984 ret = io_timeout(req);
5985 break;
5986 case IORING_OP_TIMEOUT_REMOVE:
5987 ret = io_timeout_remove(req);
5988 break;
5989 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005990 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005991 break;
5992 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06005993 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005994 break;
5995 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03005996 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005997 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005998 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03005999 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006000 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006001 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006002 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006003 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006004 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006005 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006006 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006007 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006008 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006009 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006010 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006011 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006012 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006013 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006014 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006015 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006016 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006017 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006018 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006019 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006020 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006021 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006022 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006023 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006024 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006025 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006026 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006027 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006028 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006029 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006030 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006031 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006032 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006033 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006034 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006035 ret = io_tee(req, force_nonblock);
6036 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006037 default:
6038 ret = -EINVAL;
6039 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006040 }
6041
6042 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006043 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006044
Jens Axboeb5325762020-05-19 21:20:27 -06006045 /* If the op doesn't have a file, we're not polling for it */
6046 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006047 const bool in_async = io_wq_current_is_worker();
6048
Jens Axboe11ba8202020-01-15 21:51:17 -07006049 /* workqueue context doesn't hold uring_lock, grab it now */
6050 if (in_async)
6051 mutex_lock(&ctx->uring_lock);
6052
Jens Axboe2b188cc2019-01-07 10:46:33 -07006053 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006054
6055 if (in_async)
6056 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006057 }
6058
6059 return 0;
6060}
6061
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006062static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006063{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006064 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006065 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006066 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006067
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006068 timeout = io_prep_linked_timeout(req);
6069 if (timeout)
6070 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006071
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006072 /* if NO_CANCEL is set, we must still run the work */
6073 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6074 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006075 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006076 }
Jens Axboe31b51512019-01-18 22:56:34 -07006077
Jens Axboe561fb042019-10-24 07:25:42 -06006078 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006079 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006080 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006081 /*
6082 * We can get EAGAIN for polled IO even though we're
6083 * forcing a sync submission from here, since we can't
6084 * wait for request slots on the block side.
6085 */
6086 if (ret != -EAGAIN)
6087 break;
6088 cond_resched();
6089 } while (1);
6090 }
Jens Axboe31b51512019-01-18 22:56:34 -07006091
Jens Axboe561fb042019-10-24 07:25:42 -06006092 if (ret) {
Xiaoguang Wang10e5fb02020-12-14 23:49:41 +08006093 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangcd13f1d2020-12-06 22:22:42 +00006094
Xiaoguang Wang10e5fb02020-12-14 23:49:41 +08006095 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6096 lock_ctx = req->ctx;
6097
6098 /*
6099 * io_iopoll_complete() does not hold completion_lock to
6100 * complete polled io, so here for polled io, we can not call
6101 * io_req_complete() directly, otherwise there maybe concurrent
6102 * access to cqring, defer_list, etc, which is not safe. Given
6103 * that io_iopoll_complete() is always called under uring_lock,
6104 * so here for polled io, we also get uring_lock to complete
6105 * it.
6106 */
6107 if (lock_ctx)
6108 mutex_lock(&lock_ctx->uring_lock);
6109
6110 req_set_fail_links(req);
6111 io_req_complete(req, ret);
6112
6113 if (lock_ctx)
6114 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006115 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006116
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006117 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006118}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006119
Jens Axboe65e19f52019-10-26 07:20:21 -06006120static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6121 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006122{
Jens Axboe65e19f52019-10-26 07:20:21 -06006123 struct fixed_file_table *table;
6124
Jens Axboe05f3fb32019-12-09 11:22:50 -07006125 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006126 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006127}
6128
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006129static struct file *io_file_get(struct io_submit_state *state,
6130 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006131{
6132 struct io_ring_ctx *ctx = req->ctx;
6133 struct file *file;
6134
6135 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006136 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006137 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006138 fd = array_index_nospec(fd, ctx->nr_user_files);
6139 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006140 if (file) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01006141 req->fixed_file_refs = &ctx->file_data->node->refs;
Jens Axboefd2206e2020-06-02 16:40:47 -06006142 percpu_ref_get(req->fixed_file_refs);
6143 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006144 } else {
6145 trace_io_uring_file_get(ctx, fd);
6146 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006147 }
6148
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006149 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006150}
6151
Jens Axboe3529d8c2019-12-19 18:24:38 -07006152static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006153 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006154{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006155 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006156
Jens Axboe63ff8222020-05-07 14:56:15 -06006157 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006158 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006159 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006160
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006161 req->file = io_file_get(state, req, fd, fixed);
6162 if (req->file || io_op_defs[req->opcode].needs_file_no_error)
Jens Axboef86cd202020-01-29 13:46:44 -07006163 return 0;
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006164 return -EBADF;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006165}
6166
Jens Axboe2665abf2019-11-05 12:40:47 -07006167static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6168{
Jens Axboead8a48a2019-11-15 08:49:11 -07006169 struct io_timeout_data *data = container_of(timer,
6170 struct io_timeout_data, timer);
6171 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006172 struct io_ring_ctx *ctx = req->ctx;
6173 struct io_kiocb *prev = NULL;
6174 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006175
6176 spin_lock_irqsave(&ctx->completion_lock, flags);
6177
6178 /*
6179 * We don't expect the list to be empty, that will only happen if we
6180 * race with the completion of the linked work.
6181 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006182 if (!list_empty(&req->link_list)) {
6183 prev = list_entry(req->link_list.prev, struct io_kiocb,
6184 link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006185 if (refcount_inc_not_zero(&prev->refs))
Pavel Begunkov44932332019-12-05 16:16:35 +03006186 list_del_init(&req->link_list);
Pavel Begunkov900fad42020-10-19 16:39:16 +01006187 else
Jens Axboe76a46e02019-11-10 23:34:16 -07006188 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006189 }
6190
6191 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6192
6193 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006194 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006195 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006196 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006197 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006198 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006199 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006200 return HRTIMER_NORESTART;
6201}
6202
Jens Axboe7271ef32020-08-10 09:55:22 -06006203static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006204{
Jens Axboe76a46e02019-11-10 23:34:16 -07006205 /*
6206 * If the list is now empty, then our linked request finished before
6207 * we got a chance to setup the timer
6208 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006209 if (!list_empty(&req->link_list)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006210 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006211
Jens Axboead8a48a2019-11-15 08:49:11 -07006212 data->timer.function = io_link_timeout_fn;
6213 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6214 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006215 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006216}
6217
6218static void io_queue_linked_timeout(struct io_kiocb *req)
6219{
6220 struct io_ring_ctx *ctx = req->ctx;
6221
6222 spin_lock_irq(&ctx->completion_lock);
6223 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006224 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006225
Jens Axboe2665abf2019-11-05 12:40:47 -07006226 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006227 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006228}
6229
Jens Axboead8a48a2019-11-15 08:49:11 -07006230static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006231{
6232 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006233
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006234 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006235 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006236 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006237 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006238
Pavel Begunkov44932332019-12-05 16:16:35 +03006239 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6240 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006241 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006242 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006243
Pavel Begunkov900fad42020-10-19 16:39:16 +01006244 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006245 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006246 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006247}
6248
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006249static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006250{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006251 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006252 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006253 int ret;
6254
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006255again:
6256 linked_timeout = io_prep_linked_timeout(req);
6257
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006258 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6259 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006260 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006261 if (old_creds)
6262 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006263 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006264 old_creds = NULL; /* restored original creds */
6265 else
Jens Axboe98447d62020-10-14 10:48:51 -06006266 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006267 }
6268
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006269 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006270
6271 /*
6272 * We async punt it if the file wasn't marked NOWAIT, or if the file
6273 * doesn't support non-blocking read/write attempts
6274 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006275 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006276 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006277 /*
6278 * Queued up for async execution, worker will release
6279 * submit reference when the iocb is actually submitted.
6280 */
6281 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006282 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006283
Pavel Begunkovf063c542020-07-25 14:41:59 +03006284 if (linked_timeout)
6285 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006286 } else if (likely(!ret)) {
6287 /* drop submission reference */
6288 req = io_put_req_find_next(req);
6289 if (linked_timeout)
6290 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006291
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006292 if (req) {
6293 if (!(req->flags & REQ_F_FORCE_ASYNC))
6294 goto again;
6295 io_queue_async_work(req);
6296 }
6297 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006298 /* un-prep timeout, so it'll be killed as any other linked */
6299 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006300 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006301 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006302 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006303 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006304
Jens Axboe193155c2020-02-22 23:22:19 -07006305 if (old_creds)
6306 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006307}
6308
Jens Axboef13fad72020-06-22 09:34:30 -06006309static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6310 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006311{
6312 int ret;
6313
Jens Axboe3529d8c2019-12-19 18:24:38 -07006314 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006315 if (ret) {
6316 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006317fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006318 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006319 io_put_req(req);
6320 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006321 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006322 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006323 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006324 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006325 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006326 goto fail_req;
6327 }
Jens Axboece35a472019-12-17 08:04:44 -07006328 io_queue_async_work(req);
6329 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006330 if (sqe) {
6331 ret = io_req_prep(req, sqe);
6332 if (unlikely(ret))
6333 goto fail_req;
6334 }
6335 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006336 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006337}
6338
Jens Axboef13fad72020-06-22 09:34:30 -06006339static inline void io_queue_link_head(struct io_kiocb *req,
6340 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006341{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006342 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006343 io_put_req(req);
6344 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006345 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006346 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006347}
6348
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006349static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006350 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006351{
Jackie Liua197f662019-11-08 08:09:12 -07006352 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006353 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006354
Jens Axboe9e645e112019-05-10 16:07:28 -06006355 /*
6356 * If we already have a head request, queue this one for async
6357 * submittal once the head completes. If we don't have a head but
6358 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6359 * submitted sync once the chain is complete. If none of those
6360 * conditions are true (normal request), then just queue it.
6361 */
6362 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006363 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006364
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006365 /*
6366 * Taking sequential execution of a link, draining both sides
6367 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6368 * requests in the link. So, it drains the head and the
6369 * next after the link request. The last one is done via
6370 * drain_next flag to persist the effect across calls.
6371 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006372 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006373 head->flags |= REQ_F_IO_DRAIN;
6374 ctx->drain_next = 1;
6375 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006376 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006377 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006378 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006379 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006380 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006381 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006382 trace_io_uring_link(ctx, req, head);
6383 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006384
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006385 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006386 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006387 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006388 *link = NULL;
6389 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006390 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006391 if (unlikely(ctx->drain_next)) {
6392 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006393 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006394 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006395 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006396 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006397 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006398
Pavel Begunkov711be032020-01-17 03:57:59 +03006399 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006400 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006401 req->flags |= REQ_F_FAIL_LINK;
6402 *link = req;
6403 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006404 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006405 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006406 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006407
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006408 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006409}
6410
Jens Axboe9a56a232019-01-09 09:06:50 -07006411/*
6412 * Batched submission is done, ensure local IO is flushed out.
6413 */
6414static void io_submit_state_end(struct io_submit_state *state)
6415{
Jens Axboef13fad72020-06-22 09:34:30 -06006416 if (!list_empty(&state->comp.list))
6417 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006418 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006419 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006420 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006421 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006422}
6423
6424/*
6425 * Start submission side cache.
6426 */
6427static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006428 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006429{
6430 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006431 state->comp.nr = 0;
6432 INIT_LIST_HEAD(&state->comp.list);
6433 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006434 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006435 state->file = NULL;
6436 state->ios_left = max_ios;
6437}
6438
Jens Axboe2b188cc2019-01-07 10:46:33 -07006439static void io_commit_sqring(struct io_ring_ctx *ctx)
6440{
Hristo Venev75b28af2019-08-26 17:23:46 +00006441 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006442
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006443 /*
6444 * Ensure any loads from the SQEs are done at this point,
6445 * since once we write the new head, the application could
6446 * write new data to them.
6447 */
6448 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006449}
6450
6451/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006452 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006453 * that is mapped by userspace. This means that care needs to be taken to
6454 * ensure that reads are stable, as we cannot rely on userspace always
6455 * being a good citizen. If members of the sqe are validated and then later
6456 * used, it's important that those reads are done through READ_ONCE() to
6457 * prevent a re-load down the line.
6458 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006459static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006460{
Hristo Venev75b28af2019-08-26 17:23:46 +00006461 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006462 unsigned head;
6463
6464 /*
6465 * The cached sq head (or cq tail) serves two purposes:
6466 *
6467 * 1) allows us to batch the cost of updating the user visible
6468 * head updates.
6469 * 2) allows the kernel side to track the head on its own, even
6470 * though the application is the one updating it.
6471 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006472 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006473 if (likely(head < ctx->sq_entries))
6474 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006475
6476 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006477 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006478 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006479 return NULL;
6480}
6481
6482static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6483{
6484 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006485}
6486
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006487/*
6488 * Check SQE restrictions (opcode and flags).
6489 *
6490 * Returns 'true' if SQE is allowed, 'false' otherwise.
6491 */
6492static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6493 struct io_kiocb *req,
6494 unsigned int sqe_flags)
6495{
6496 if (!ctx->restricted)
6497 return true;
6498
6499 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6500 return false;
6501
6502 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6503 ctx->restrictions.sqe_flags_required)
6504 return false;
6505
6506 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6507 ctx->restrictions.sqe_flags_required))
6508 return false;
6509
6510 return true;
6511}
6512
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006513#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6514 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6515 IOSQE_BUFFER_SELECT)
6516
6517static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6518 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006519 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006520{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006521 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006522 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006523
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006524 req->opcode = READ_ONCE(sqe->opcode);
6525 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006526 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006527 req->file = NULL;
6528 req->ctx = ctx;
6529 req->flags = 0;
6530 /* one is dropped after submission, the other at completion */
6531 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006532 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006533 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006534
6535 if (unlikely(req->opcode >= IORING_OP_LAST))
6536 return -EINVAL;
6537
Jens Axboe9d8426a2020-06-16 18:42:49 -06006538 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6539 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006540
6541 sqe_flags = READ_ONCE(sqe->flags);
6542 /* enforce forwards compatibility on users */
6543 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6544 return -EINVAL;
6545
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006546 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6547 return -EACCES;
6548
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006549 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6550 !io_op_defs[req->opcode].buffer_select)
6551 return -EOPNOTSUPP;
6552
6553 id = READ_ONCE(sqe->personality);
6554 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006555 struct io_identity *iod;
6556
Jens Axboe1e6fa522020-10-15 08:46:24 -06006557 iod = idr_find(&ctx->personality_idr, id);
6558 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006559 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006560 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006561
6562 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006563 get_cred(iod->creds);
6564 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006565 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006566 }
6567
6568 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006569 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006570
Jens Axboe63ff8222020-05-07 14:56:15 -06006571 if (!io_op_defs[req->opcode].needs_file)
6572 return 0;
6573
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006574 ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
6575 state->ios_left--;
6576 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006577}
6578
Jens Axboe0f212202020-09-13 13:09:39 -06006579static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006580{
Jens Axboeac8691c2020-06-01 08:30:41 -06006581 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006582 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006583 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006584
Jens Axboec4a2ed72019-11-21 21:01:26 -07006585 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006586 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006587 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006588 return -EBUSY;
6589 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006590
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006591 /* make sure SQ entry isn't read before tail */
6592 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006593
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006594 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6595 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006596
Jens Axboed8a6df12020-10-15 16:24:45 -06006597 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006598 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006599
Jens Axboe6c271ce2019-01-10 11:22:30 -07006600 io_submit_state_start(&state, ctx, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006601
Jens Axboe6c271ce2019-01-10 11:22:30 -07006602 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006603 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006604 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006605 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006606
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006607 sqe = io_get_sqe(ctx);
6608 if (unlikely(!sqe)) {
6609 io_consume_sqe(ctx);
6610 break;
6611 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006612 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006613 if (unlikely(!req)) {
6614 if (!submitted)
6615 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006616 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006617 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006618 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006619 /* will complete beyond this point, count as submitted */
6620 submitted++;
6621
Pavel Begunkov692d8362020-10-10 18:34:13 +01006622 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006623 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006624fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006625 io_put_req(req);
6626 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006627 break;
6628 }
6629
Jens Axboe354420f2020-01-08 18:55:15 -07006630 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006631 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006632 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006633 if (err)
6634 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006635 }
6636
Pavel Begunkov9466f432020-01-25 22:34:01 +03006637 if (unlikely(submitted != nr)) {
6638 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006639 struct io_uring_task *tctx = current->io_uring;
6640 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006641
Jens Axboed8a6df12020-10-15 16:24:45 -06006642 percpu_ref_put_many(&ctx->refs, unused);
6643 percpu_counter_sub(&tctx->inflight, unused);
6644 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006645 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006646 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006647 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006648 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006649
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006650 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6651 io_commit_sqring(ctx);
6652
Jens Axboe6c271ce2019-01-10 11:22:30 -07006653 return submitted;
6654}
6655
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006656static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6657{
6658 /* Tell userspace we may need a wakeup call */
6659 spin_lock_irq(&ctx->completion_lock);
6660 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6661 spin_unlock_irq(&ctx->completion_lock);
6662}
6663
6664static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6665{
6666 spin_lock_irq(&ctx->completion_lock);
6667 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6668 spin_unlock_irq(&ctx->completion_lock);
6669}
6670
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006671static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6672 int sync, void *key)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006673{
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006674 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6675 int ret;
6676
6677 ret = autoremove_wake_function(wqe, mode, sync, key);
6678 if (ret) {
6679 unsigned long flags;
6680
6681 spin_lock_irqsave(&ctx->completion_lock, flags);
6682 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6683 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6684 }
6685 return ret;
6686}
6687
Jens Axboec8d1ba52020-09-14 11:07:26 -06006688enum sq_ret {
6689 SQT_IDLE = 1,
6690 SQT_SPIN = 2,
6691 SQT_DID_WORK = 4,
6692};
6693
6694static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
Jens Axboee95eee22020-09-08 09:11:32 -06006695 unsigned long start_jiffies, bool cap_entries)
Jens Axboec8d1ba52020-09-14 11:07:26 -06006696{
6697 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006698 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006699 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006700 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006701
Jens Axboec8d1ba52020-09-14 11:07:26 -06006702again:
6703 if (!list_empty(&ctx->iopoll_list)) {
6704 unsigned nr_events = 0;
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006705
Jens Axboec8d1ba52020-09-14 11:07:26 -06006706 mutex_lock(&ctx->uring_lock);
6707 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6708 io_do_iopoll(ctx, &nr_events, 0);
6709 mutex_unlock(&ctx->uring_lock);
6710 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006711
Jens Axboec8d1ba52020-09-14 11:07:26 -06006712 to_submit = io_sqring_entries(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006713
Jens Axboec8d1ba52020-09-14 11:07:26 -06006714 /*
6715 * If submit got -EBUSY, flag us as needing the application
6716 * to enter the kernel to reap and flush events.
6717 */
6718 if (!to_submit || ret == -EBUSY || need_resched()) {
6719 /*
6720 * Drop cur_mm before scheduling, we can't hold it for
6721 * long periods (or over schedule()). Do this before
6722 * adding ourselves to the waitqueue, as the unuse/drop
6723 * may sleep.
6724 */
6725 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006726
Jens Axboec8d1ba52020-09-14 11:07:26 -06006727 /*
6728 * We're polling. If we're within the defined idle
6729 * period, then let us spin without work before going
6730 * to sleep. The exception is if we got EBUSY doing
6731 * more IO, we should wait for the application to
6732 * reap events and wake us up.
6733 */
6734 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6735 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6736 !percpu_ref_is_dying(&ctx->refs)))
6737 return SQT_SPIN;
6738
Jens Axboe534ca6d2020-09-02 13:52:19 -06006739 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006740 TASK_INTERRUPTIBLE);
6741
6742 /*
6743 * While doing polled IO, before going to sleep, we need
6744 * to check if there are new reqs added to iopoll_list,
6745 * it is because reqs may have been punted to io worker
6746 * and will be added to iopoll_list later, hence check
6747 * the iopoll_list again.
6748 */
6749 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6750 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006751 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006752 goto again;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006753 }
6754
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006755 to_submit = io_sqring_entries(ctx);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006756 if (!to_submit || ret == -EBUSY)
6757 return SQT_IDLE;
6758 }
6759
Jens Axboe534ca6d2020-09-02 13:52:19 -06006760 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006761 io_ring_clear_wakeup_flag(ctx);
6762
Jens Axboee95eee22020-09-08 09:11:32 -06006763 /* if we're handling multiple rings, cap submit size for fairness */
6764 if (cap_entries && to_submit > 8)
6765 to_submit = 8;
6766
Jens Axboec8d1ba52020-09-14 11:07:26 -06006767 mutex_lock(&ctx->uring_lock);
6768 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6769 ret = io_submit_sqes(ctx, to_submit);
6770 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006771
6772 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6773 wake_up(&ctx->sqo_sq_wait);
6774
Jens Axboec8d1ba52020-09-14 11:07:26 -06006775 return SQT_DID_WORK;
6776}
6777
Jens Axboe69fb2132020-09-14 11:16:23 -06006778static void io_sqd_init_new(struct io_sq_data *sqd)
6779{
6780 struct io_ring_ctx *ctx;
6781
6782 while (!list_empty(&sqd->ctx_new_list)) {
6783 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6784 init_wait(&ctx->sqo_wait_entry);
6785 ctx->sqo_wait_entry.func = io_sq_wake_function;
6786 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6787 complete(&ctx->sq_thread_comp);
6788 }
6789}
6790
Jens Axboe6c271ce2019-01-10 11:22:30 -07006791static int io_sq_thread(void *data)
6792{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006793 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe69fb2132020-09-14 11:16:23 -06006794 const struct cred *old_cred = NULL;
6795 struct io_sq_data *sqd = data;
6796 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006797 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006798
Jens Axboec8d1ba52020-09-14 11:07:26 -06006799 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006800 while (!kthread_should_stop()) {
6801 enum sq_ret ret = 0;
Jens Axboee95eee22020-09-08 09:11:32 -06006802 bool cap_entries;
Jens Axboec1edbf52019-11-10 16:56:04 -07006803
6804 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006805 * Any changes to the sqd lists are synchronized through the
6806 * kthread parking. This synchronizes the thread vs users,
6807 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006808 */
Xiaoguang Wangb5a2f092020-11-19 17:44:46 +08006809 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006810 kthread_parkme();
Xiaoguang Wangb5a2f092020-11-19 17:44:46 +08006811 /*
6812 * When sq thread is unparked, in case the previous park operation
6813 * comes from io_put_sq_data(), which means that sq thread is going
6814 * to be stopped, so here needs to have a check.
6815 */
6816 if (kthread_should_stop())
6817 break;
6818 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006819
6820 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6821 io_sqd_init_new(sqd);
6822
Jens Axboee95eee22020-09-08 09:11:32 -06006823 cap_entries = !list_is_singular(&sqd->ctx_list);
6824
Jens Axboe69fb2132020-09-14 11:16:23 -06006825 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6826 if (current->cred != ctx->creds) {
6827 if (old_cred)
6828 revert_creds(old_cred);
6829 old_cred = override_creds(ctx->creds);
6830 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006831 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06006832#ifdef CONFIG_AUDIT
6833 current->loginuid = ctx->loginuid;
6834 current->sessionid = ctx->sessionid;
6835#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06006836
Jens Axboee95eee22020-09-08 09:11:32 -06006837 ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
Jens Axboe69fb2132020-09-14 11:16:23 -06006838
Jens Axboe4349f302020-07-09 15:07:01 -06006839 io_sq_thread_drop_mm();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006840 }
6841
Jens Axboe69fb2132020-09-14 11:16:23 -06006842 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006843 io_run_task_work();
6844 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006845 } else if (ret == SQT_IDLE) {
6846 if (kthread_should_park())
6847 continue;
6848 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6849 io_ring_set_wakeup_flag(ctx);
6850 schedule();
6851 start_jiffies = jiffies;
6852 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6853 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006854 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006855 }
6856
Jens Axboe4c6e2772020-07-01 11:29:10 -06006857 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006858
Dennis Zhou91d8f512020-09-16 13:41:05 -07006859 if (cur_css)
6860 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006861 if (old_cred)
6862 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006863
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006864 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006865
Jens Axboe6c271ce2019-01-10 11:22:30 -07006866 return 0;
6867}
6868
Jens Axboebda52162019-09-24 13:47:15 -06006869struct io_wait_queue {
6870 struct wait_queue_entry wq;
6871 struct io_ring_ctx *ctx;
6872 unsigned to_wait;
6873 unsigned nr_timeouts;
6874};
6875
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006876static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006877{
6878 struct io_ring_ctx *ctx = iowq->ctx;
6879
6880 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006881 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006882 * started waiting. For timeouts, we always want to return to userspace,
6883 * regardless of event count.
6884 */
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006885 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006886 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6887}
6888
6889static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6890 int wake_flags, void *key)
6891{
6892 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6893 wq);
6894
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006895 /*
6896 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6897 * the task, and the next invocation will do it.
6898 */
6899 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6900 return autoremove_wake_function(curr, mode, wake_flags, key);
6901 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006902}
6903
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006904static int io_run_task_work_sig(void)
6905{
6906 if (io_run_task_work())
6907 return 1;
6908 if (!signal_pending(current))
6909 return 0;
6910 if (current->jobctl & JOBCTL_TASK_WORK) {
6911 spin_lock_irq(&current->sighand->siglock);
6912 current->jobctl &= ~JOBCTL_TASK_WORK;
6913 recalc_sigpending();
6914 spin_unlock_irq(&current->sighand->siglock);
6915 return 1;
6916 }
6917 return -EINTR;
6918}
6919
Jens Axboe2b188cc2019-01-07 10:46:33 -07006920/*
6921 * Wait until events become available, if we don't already have some. The
6922 * application must reap them itself, as they reside on the shared cq ring.
6923 */
6924static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6925 const sigset_t __user *sig, size_t sigsz)
6926{
Jens Axboebda52162019-09-24 13:47:15 -06006927 struct io_wait_queue iowq = {
6928 .wq = {
6929 .private = current,
6930 .func = io_wake_function,
6931 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6932 },
6933 .ctx = ctx,
6934 .to_wait = min_events,
6935 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006936 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006937 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006938
Jens Axboeb41e9852020-02-17 09:52:41 -07006939 do {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006940 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6941 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07006942 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006943 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006944 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006945 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006946
6947 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006948#ifdef CONFIG_COMPAT
6949 if (in_compat_syscall())
6950 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006951 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006952 else
6953#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006954 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006955
Jens Axboe2b188cc2019-01-07 10:46:33 -07006956 if (ret)
6957 return ret;
6958 }
6959
Jens Axboebda52162019-09-24 13:47:15 -06006960 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006961 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006962 do {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006963 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06006964 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6965 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006966 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006967 ret = io_run_task_work_sig();
6968 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06006969 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006970 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06006971 break;
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006972 if (io_should_wake(&iowq))
Jens Axboebda52162019-09-24 13:47:15 -06006973 break;
Pavel Begunkov85e25e22021-01-12 21:17:26 +00006974 if (test_bit(0, &ctx->cq_check_overflow))
6975 continue;
Jens Axboebda52162019-09-24 13:47:15 -06006976 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006977 } while (1);
6978 finish_wait(&ctx->wait, &iowq.wq);
6979
Jens Axboeb7db41c2020-07-04 08:55:50 -06006980 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006981
Hristo Venev75b28af2019-08-26 17:23:46 +00006982 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006983}
6984
Jens Axboe6b063142019-01-10 22:13:58 -07006985static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6986{
6987#if defined(CONFIG_UNIX)
6988 if (ctx->ring_sock) {
6989 struct sock *sock = ctx->ring_sock->sk;
6990 struct sk_buff *skb;
6991
6992 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6993 kfree_skb(skb);
6994 }
6995#else
6996 int i;
6997
Jens Axboe65e19f52019-10-26 07:20:21 -06006998 for (i = 0; i < ctx->nr_user_files; i++) {
6999 struct file *file;
7000
7001 file = io_file_from_index(ctx, i);
7002 if (file)
7003 fput(file);
7004 }
Jens Axboe6b063142019-01-10 22:13:58 -07007005#endif
7006}
7007
Jens Axboe05f3fb32019-12-09 11:22:50 -07007008static void io_file_ref_kill(struct percpu_ref *ref)
7009{
7010 struct fixed_file_data *data;
7011
7012 data = container_of(ref, struct fixed_file_data, refs);
7013 complete(&data->done);
7014}
7015
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007016static void io_sqe_files_set_node(struct fixed_file_data *file_data,
7017 struct fixed_file_ref_node *ref_node)
7018{
7019 spin_lock_bh(&file_data->lock);
7020 file_data->node = ref_node;
7021 list_add_tail(&ref_node->node, &file_data->ref_list);
7022 spin_unlock_bh(&file_data->lock);
7023 percpu_ref_get(&file_data->refs);
7024}
7025
Jens Axboe6b063142019-01-10 22:13:58 -07007026static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7027{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007028 struct fixed_file_data *data = ctx->file_data;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007029 struct fixed_file_ref_node *backup_node, *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007030 unsigned nr_tables, i;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007031 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007032
Jens Axboe05f3fb32019-12-09 11:22:50 -07007033 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007034 return -ENXIO;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007035 backup_node = alloc_fixed_file_ref_node(ctx);
7036 if (!backup_node)
7037 return -ENOMEM;
Jens Axboe6b063142019-01-10 22:13:58 -07007038
Jens Axboe25a2de62020-11-23 09:37:51 -07007039 spin_lock_bh(&data->lock);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007040 ref_node = data->node;
Jens Axboe25a2de62020-11-23 09:37:51 -07007041 spin_unlock_bh(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007042 if (ref_node)
7043 percpu_ref_kill(&ref_node->refs);
7044
7045 percpu_ref_kill(&data->refs);
7046
7047 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007048 flush_delayed_work(&ctx->file_put_work);
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007049 do {
7050 ret = wait_for_completion_interruptible(&data->done);
7051 if (!ret)
7052 break;
7053 ret = io_run_task_work_sig();
7054 if (ret < 0) {
7055 percpu_ref_resurrect(&data->refs);
7056 reinit_completion(&data->done);
7057 io_sqe_files_set_node(data, backup_node);
7058 return ret;
7059 }
7060 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007061
Jens Axboe6b063142019-01-10 22:13:58 -07007062 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007063 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7064 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007065 kfree(data->table[i].files);
7066 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007067 percpu_ref_exit(&data->refs);
7068 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007069 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007070 ctx->nr_user_files = 0;
Pavel Begunkovce00a7d2020-12-30 21:34:15 +00007071 destroy_fixed_file_ref_node(backup_node);
Jens Axboe6b063142019-01-10 22:13:58 -07007072 return 0;
7073}
7074
Jens Axboe534ca6d2020-09-02 13:52:19 -06007075static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007076{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007077 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007078 /*
7079 * The park is a bit of a work-around, without it we get
7080 * warning spews on shutdown with SQPOLL set and affinity
7081 * set to a single CPU.
7082 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007083 if (sqd->thread) {
7084 kthread_park(sqd->thread);
7085 kthread_stop(sqd->thread);
7086 }
7087
7088 kfree(sqd);
7089 }
7090}
7091
Jens Axboeaa061652020-09-02 14:50:27 -06007092static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7093{
7094 struct io_ring_ctx *ctx_attach;
7095 struct io_sq_data *sqd;
7096 struct fd f;
7097
7098 f = fdget(p->wq_fd);
7099 if (!f.file)
7100 return ERR_PTR(-ENXIO);
7101 if (f.file->f_op != &io_uring_fops) {
7102 fdput(f);
7103 return ERR_PTR(-EINVAL);
7104 }
7105
7106 ctx_attach = f.file->private_data;
7107 sqd = ctx_attach->sq_data;
7108 if (!sqd) {
7109 fdput(f);
7110 return ERR_PTR(-EINVAL);
7111 }
7112
7113 refcount_inc(&sqd->refs);
7114 fdput(f);
7115 return sqd;
7116}
7117
Jens Axboe534ca6d2020-09-02 13:52:19 -06007118static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7119{
7120 struct io_sq_data *sqd;
7121
Jens Axboeaa061652020-09-02 14:50:27 -06007122 if (p->flags & IORING_SETUP_ATTACH_WQ)
7123 return io_attach_sq_data(p);
7124
Jens Axboe534ca6d2020-09-02 13:52:19 -06007125 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7126 if (!sqd)
7127 return ERR_PTR(-ENOMEM);
7128
7129 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007130 INIT_LIST_HEAD(&sqd->ctx_list);
7131 INIT_LIST_HEAD(&sqd->ctx_new_list);
7132 mutex_init(&sqd->ctx_lock);
7133 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007134 init_waitqueue_head(&sqd->wait);
7135 return sqd;
7136}
7137
Jens Axboe69fb2132020-09-14 11:16:23 -06007138static void io_sq_thread_unpark(struct io_sq_data *sqd)
7139 __releases(&sqd->lock)
7140{
7141 if (!sqd->thread)
7142 return;
7143 kthread_unpark(sqd->thread);
7144 mutex_unlock(&sqd->lock);
7145}
7146
7147static void io_sq_thread_park(struct io_sq_data *sqd)
7148 __acquires(&sqd->lock)
7149{
7150 if (!sqd->thread)
7151 return;
7152 mutex_lock(&sqd->lock);
7153 kthread_park(sqd->thread);
7154}
7155
Jens Axboe534ca6d2020-09-02 13:52:19 -06007156static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7157{
7158 struct io_sq_data *sqd = ctx->sq_data;
7159
7160 if (sqd) {
7161 if (sqd->thread) {
7162 /*
7163 * We may arrive here from the error branch in
7164 * io_sq_offload_create() where the kthread is created
7165 * without being waked up, thus wake it up now to make
7166 * sure the wait will complete.
7167 */
7168 wake_up_process(sqd->thread);
7169 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007170
7171 io_sq_thread_park(sqd);
7172 }
7173
7174 mutex_lock(&sqd->ctx_lock);
7175 list_del(&ctx->sqd_list);
7176 mutex_unlock(&sqd->ctx_lock);
7177
7178 if (sqd->thread) {
7179 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7180 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007181 }
7182
7183 io_put_sq_data(sqd);
7184 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007185 }
7186}
7187
Jens Axboe6b063142019-01-10 22:13:58 -07007188static void io_finish_async(struct io_ring_ctx *ctx)
7189{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007190 io_sq_thread_stop(ctx);
7191
Jens Axboe561fb042019-10-24 07:25:42 -06007192 if (ctx->io_wq) {
7193 io_wq_destroy(ctx->io_wq);
7194 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007195 }
7196}
7197
7198#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007199/*
7200 * Ensure the UNIX gc is aware of our file set, so we are certain that
7201 * the io_uring can be safely unregistered on process exit, even if we have
7202 * loops in the file referencing.
7203 */
7204static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7205{
7206 struct sock *sk = ctx->ring_sock->sk;
7207 struct scm_fp_list *fpl;
7208 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007209 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007210
Jens Axboe6b063142019-01-10 22:13:58 -07007211 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7212 if (!fpl)
7213 return -ENOMEM;
7214
7215 skb = alloc_skb(0, GFP_KERNEL);
7216 if (!skb) {
7217 kfree(fpl);
7218 return -ENOMEM;
7219 }
7220
7221 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007222
Jens Axboe08a45172019-10-03 08:11:03 -06007223 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007224 fpl->user = get_uid(ctx->user);
7225 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007226 struct file *file = io_file_from_index(ctx, i + offset);
7227
7228 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007229 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007230 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007231 unix_inflight(fpl->user, fpl->fp[nr_files]);
7232 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007233 }
7234
Jens Axboe08a45172019-10-03 08:11:03 -06007235 if (nr_files) {
7236 fpl->max = SCM_MAX_FD;
7237 fpl->count = nr_files;
7238 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007239 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007240 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7241 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007242
Jens Axboe08a45172019-10-03 08:11:03 -06007243 for (i = 0; i < nr_files; i++)
7244 fput(fpl->fp[i]);
7245 } else {
7246 kfree_skb(skb);
7247 kfree(fpl);
7248 }
Jens Axboe6b063142019-01-10 22:13:58 -07007249
7250 return 0;
7251}
7252
7253/*
7254 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7255 * causes regular reference counting to break down. We rely on the UNIX
7256 * garbage collection to take care of this problem for us.
7257 */
7258static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7259{
7260 unsigned left, total;
7261 int ret = 0;
7262
7263 total = 0;
7264 left = ctx->nr_user_files;
7265 while (left) {
7266 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007267
7268 ret = __io_sqe_files_scm(ctx, this_files, total);
7269 if (ret)
7270 break;
7271 left -= this_files;
7272 total += this_files;
7273 }
7274
7275 if (!ret)
7276 return 0;
7277
7278 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007279 struct file *file = io_file_from_index(ctx, total);
7280
7281 if (file)
7282 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007283 total++;
7284 }
7285
7286 return ret;
7287}
7288#else
7289static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7290{
7291 return 0;
7292}
7293#endif
7294
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007295static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7296 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007297{
7298 int i;
7299
7300 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007301 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007302 unsigned this_files;
7303
7304 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7305 table->files = kcalloc(this_files, sizeof(struct file *),
7306 GFP_KERNEL);
7307 if (!table->files)
7308 break;
7309 nr_files -= this_files;
7310 }
7311
7312 if (i == nr_tables)
7313 return 0;
7314
7315 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007316 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007317 kfree(table->files);
7318 }
7319 return 1;
7320}
7321
Jens Axboe05f3fb32019-12-09 11:22:50 -07007322static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007323{
7324#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007325 struct sock *sock = ctx->ring_sock->sk;
7326 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7327 struct sk_buff *skb;
7328 int i;
7329
7330 __skb_queue_head_init(&list);
7331
7332 /*
7333 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7334 * remove this entry and rearrange the file array.
7335 */
7336 skb = skb_dequeue(head);
7337 while (skb) {
7338 struct scm_fp_list *fp;
7339
7340 fp = UNIXCB(skb).fp;
7341 for (i = 0; i < fp->count; i++) {
7342 int left;
7343
7344 if (fp->fp[i] != file)
7345 continue;
7346
7347 unix_notinflight(fp->user, fp->fp[i]);
7348 left = fp->count - 1 - i;
7349 if (left) {
7350 memmove(&fp->fp[i], &fp->fp[i + 1],
7351 left * sizeof(struct file *));
7352 }
7353 fp->count--;
7354 if (!fp->count) {
7355 kfree_skb(skb);
7356 skb = NULL;
7357 } else {
7358 __skb_queue_tail(&list, skb);
7359 }
7360 fput(file);
7361 file = NULL;
7362 break;
7363 }
7364
7365 if (!file)
7366 break;
7367
7368 __skb_queue_tail(&list, skb);
7369
7370 skb = skb_dequeue(head);
7371 }
7372
7373 if (skb_peek(&list)) {
7374 spin_lock_irq(&head->lock);
7375 while ((skb = __skb_dequeue(&list)) != NULL)
7376 __skb_queue_tail(head, skb);
7377 spin_unlock_irq(&head->lock);
7378 }
7379#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007380 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007381#endif
7382}
7383
Jens Axboe05f3fb32019-12-09 11:22:50 -07007384struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007385 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007386 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007387};
7388
Jens Axboe4a38aed22020-05-14 17:21:15 -06007389static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007390{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007391 struct fixed_file_data *file_data = ref_node->file_data;
7392 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007393 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007394
7395 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007396 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007397 io_ring_file_put(ctx, pfile->file);
7398 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007399 }
7400
Xiaoguang Wang05589552020-03-31 14:05:18 +08007401 percpu_ref_exit(&ref_node->refs);
7402 kfree(ref_node);
7403 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007404}
7405
Jens Axboe4a38aed22020-05-14 17:21:15 -06007406static void io_file_put_work(struct work_struct *work)
7407{
7408 struct io_ring_ctx *ctx;
7409 struct llist_node *node;
7410
7411 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7412 node = llist_del_all(&ctx->file_put_llist);
7413
7414 while (node) {
7415 struct fixed_file_ref_node *ref_node;
7416 struct llist_node *next = node->next;
7417
7418 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7419 __io_file_put_work(ref_node);
7420 node = next;
7421 }
7422}
7423
Jens Axboe05f3fb32019-12-09 11:22:50 -07007424static void io_file_data_ref_zero(struct percpu_ref *ref)
7425{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007426 struct fixed_file_ref_node *ref_node;
Pavel Begunkove2978222020-11-18 14:56:26 +00007427 struct fixed_file_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007428 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007429 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007430 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007431
Xiaoguang Wang05589552020-03-31 14:05:18 +08007432 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Pavel Begunkove2978222020-11-18 14:56:26 +00007433 data = ref_node->file_data;
7434 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007435
Jens Axboe25a2de62020-11-23 09:37:51 -07007436 spin_lock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007437 ref_node->done = true;
7438
7439 while (!list_empty(&data->ref_list)) {
7440 ref_node = list_first_entry(&data->ref_list,
7441 struct fixed_file_ref_node, node);
7442 /* recycle ref nodes in order */
7443 if (!ref_node->done)
7444 break;
7445 list_del(&ref_node->node);
7446 first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist);
7447 }
Jens Axboe25a2de62020-11-23 09:37:51 -07007448 spin_unlock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007449
7450 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007451 delay = 0;
7452
Jens Axboe4a38aed22020-05-14 17:21:15 -06007453 if (!delay)
7454 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7455 else if (first_add)
7456 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007457}
7458
7459static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7460 struct io_ring_ctx *ctx)
7461{
7462 struct fixed_file_ref_node *ref_node;
7463
7464 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7465 if (!ref_node)
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007466 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007467
7468 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7469 0, GFP_KERNEL)) {
7470 kfree(ref_node);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007471 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007472 }
7473 INIT_LIST_HEAD(&ref_node->node);
7474 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007475 ref_node->file_data = ctx->file_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007476 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007477 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007478}
7479
7480static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7481{
7482 percpu_ref_exit(&ref_node->refs);
7483 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007484}
7485
7486static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7487 unsigned nr_args)
7488{
7489 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007490 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007491 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007492 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007493 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007494 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007495
7496 if (ctx->file_data)
7497 return -EBUSY;
7498 if (!nr_args)
7499 return -EINVAL;
7500 if (nr_args > IORING_MAX_FIXED_FILES)
7501 return -EMFILE;
7502
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007503 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7504 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007505 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007506 file_data->ctx = ctx;
7507 init_completion(&file_data->done);
7508 INIT_LIST_HEAD(&file_data->ref_list);
7509 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007510
7511 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007512 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007513 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007514 if (!file_data->table)
7515 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007516
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007517 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007518 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7519 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007520
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007521 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7522 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007523 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007524
7525 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7526 struct fixed_file_table *table;
7527 unsigned index;
7528
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007529 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7530 ret = -EFAULT;
7531 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007532 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007533 /* allow sparse sets */
7534 if (fd == -1)
7535 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007536
Jens Axboe05f3fb32019-12-09 11:22:50 -07007537 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007538 ret = -EBADF;
7539 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007540 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007541
7542 /*
7543 * Don't allow io_uring instances to be registered. If UNIX
7544 * isn't enabled, then this causes a reference cycle and this
7545 * instance can never get freed. If UNIX is enabled we'll
7546 * handle it just fine, but there's still no point in allowing
7547 * a ring fd as it doesn't support regular read/write anyway.
7548 */
7549 if (file->f_op == &io_uring_fops) {
7550 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007551 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007552 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007553 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7554 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007555 table->files[index] = file;
7556 }
7557
Jens Axboe05f3fb32019-12-09 11:22:50 -07007558 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007559 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007560 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007561 return ret;
7562 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007563
Xiaoguang Wang05589552020-03-31 14:05:18 +08007564 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007565 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007566 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007567 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007568 }
7569
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007570 io_sqe_files_set_node(file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007571 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007572out_fput:
7573 for (i = 0; i < ctx->nr_user_files; i++) {
7574 file = io_file_from_index(ctx, i);
7575 if (file)
7576 fput(file);
7577 }
7578 for (i = 0; i < nr_tables; i++)
7579 kfree(file_data->table[i].files);
7580 ctx->nr_user_files = 0;
7581out_ref:
7582 percpu_ref_exit(&file_data->refs);
7583out_free:
7584 kfree(file_data->table);
7585 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007586 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007587 return ret;
7588}
7589
Jens Axboec3a31e62019-10-03 13:59:56 -06007590static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7591 int index)
7592{
7593#if defined(CONFIG_UNIX)
7594 struct sock *sock = ctx->ring_sock->sk;
7595 struct sk_buff_head *head = &sock->sk_receive_queue;
7596 struct sk_buff *skb;
7597
7598 /*
7599 * See if we can merge this file into an existing skb SCM_RIGHTS
7600 * file set. If there's no room, fall back to allocating a new skb
7601 * and filling it in.
7602 */
7603 spin_lock_irq(&head->lock);
7604 skb = skb_peek(head);
7605 if (skb) {
7606 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7607
7608 if (fpl->count < SCM_MAX_FD) {
7609 __skb_unlink(skb, head);
7610 spin_unlock_irq(&head->lock);
7611 fpl->fp[fpl->count] = get_file(file);
7612 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7613 fpl->count++;
7614 spin_lock_irq(&head->lock);
7615 __skb_queue_head(head, skb);
7616 } else {
7617 skb = NULL;
7618 }
7619 }
7620 spin_unlock_irq(&head->lock);
7621
7622 if (skb) {
7623 fput(file);
7624 return 0;
7625 }
7626
7627 return __io_sqe_files_scm(ctx, 1, index);
7628#else
7629 return 0;
7630#endif
7631}
7632
Hillf Dantona5318d32020-03-23 17:47:15 +08007633static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007634 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007635{
Hillf Dantona5318d32020-03-23 17:47:15 +08007636 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007637 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007638
Jens Axboe05f3fb32019-12-09 11:22:50 -07007639 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007640 if (!pfile)
7641 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007642
7643 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007644 list_add(&pfile->list, &ref_node->file_list);
7645
Hillf Dantona5318d32020-03-23 17:47:15 +08007646 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007647}
7648
7649static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7650 struct io_uring_files_update *up,
7651 unsigned nr_args)
7652{
7653 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007654 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007655 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007656 __s32 __user *fds;
7657 int fd, i, err;
7658 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007659 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007660
Jens Axboe05f3fb32019-12-09 11:22:50 -07007661 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007662 return -EOVERFLOW;
7663 if (done > ctx->nr_user_files)
7664 return -EINVAL;
7665
Xiaoguang Wang05589552020-03-31 14:05:18 +08007666 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)458b4052021-01-06 16:09:26 +00007667 if (!ref_node)
7668 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007669
Jens Axboec3a31e62019-10-03 13:59:56 -06007670 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007671 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007672 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007673 struct fixed_file_table *table;
7674 unsigned index;
7675
Jens Axboec3a31e62019-10-03 13:59:56 -06007676 err = 0;
7677 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7678 err = -EFAULT;
7679 break;
7680 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007681 i = array_index_nospec(up->offset, ctx->nr_user_files);
7682 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007683 index = i & IORING_FILE_TABLE_MASK;
7684 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007685 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007686 err = io_queue_file_removal(data, file);
7687 if (err)
7688 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007689 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007690 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007691 }
7692 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007693 file = fget(fd);
7694 if (!file) {
7695 err = -EBADF;
7696 break;
7697 }
7698 /*
7699 * Don't allow io_uring instances to be registered. If
7700 * UNIX isn't enabled, then this causes a reference
7701 * cycle and this instance can never get freed. If UNIX
7702 * is enabled we'll handle it just fine, but there's
7703 * still no point in allowing a ring fd as it doesn't
7704 * support regular read/write anyway.
7705 */
7706 if (file->f_op == &io_uring_fops) {
7707 fput(file);
7708 err = -EBADF;
7709 break;
7710 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007711 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007712 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007713 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007714 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007715 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007716 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007717 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007718 }
7719 nr_args--;
7720 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007721 up->offset++;
7722 }
7723
Xiaoguang Wang05589552020-03-31 14:05:18 +08007724 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007725 percpu_ref_kill(&data->node->refs);
Pavel Begunkovb25b8692020-12-30 21:34:14 +00007726 io_sqe_files_set_node(data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007727 } else
7728 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007729
7730 return done ? done : err;
7731}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007732
Jens Axboe05f3fb32019-12-09 11:22:50 -07007733static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7734 unsigned nr_args)
7735{
7736 struct io_uring_files_update up;
7737
7738 if (!ctx->file_data)
7739 return -ENXIO;
7740 if (!nr_args)
7741 return -EINVAL;
7742 if (copy_from_user(&up, arg, sizeof(up)))
7743 return -EFAULT;
7744 if (up.resv)
7745 return -EINVAL;
7746
7747 return __io_sqe_files_update(ctx, &up, nr_args);
7748}
Jens Axboec3a31e62019-10-03 13:59:56 -06007749
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007750static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007751{
7752 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7753
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007754 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007755 io_put_req(req);
7756}
7757
Pavel Begunkov24369c22020-01-28 03:15:48 +03007758static int io_init_wq_offload(struct io_ring_ctx *ctx,
7759 struct io_uring_params *p)
7760{
7761 struct io_wq_data data;
7762 struct fd f;
7763 struct io_ring_ctx *ctx_attach;
7764 unsigned int concurrency;
7765 int ret = 0;
7766
7767 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007768 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007769 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007770
7771 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7772 /* Do QD, or 4 * CPUS, whatever is smallest */
7773 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7774
7775 ctx->io_wq = io_wq_create(concurrency, &data);
7776 if (IS_ERR(ctx->io_wq)) {
7777 ret = PTR_ERR(ctx->io_wq);
7778 ctx->io_wq = NULL;
7779 }
7780 return ret;
7781 }
7782
7783 f = fdget(p->wq_fd);
7784 if (!f.file)
7785 return -EBADF;
7786
7787 if (f.file->f_op != &io_uring_fops) {
7788 ret = -EINVAL;
7789 goto out_fput;
7790 }
7791
7792 ctx_attach = f.file->private_data;
7793 /* @io_wq is protected by holding the fd */
7794 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7795 ret = -EINVAL;
7796 goto out_fput;
7797 }
7798
7799 ctx->io_wq = ctx_attach->io_wq;
7800out_fput:
7801 fdput(f);
7802 return ret;
7803}
7804
Jens Axboe0f212202020-09-13 13:09:39 -06007805static int io_uring_alloc_task_context(struct task_struct *task)
7806{
7807 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007808 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007809
7810 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7811 if (unlikely(!tctx))
7812 return -ENOMEM;
7813
Jens Axboed8a6df12020-10-15 16:24:45 -06007814 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7815 if (unlikely(ret)) {
7816 kfree(tctx);
7817 return ret;
7818 }
7819
Jens Axboe0f212202020-09-13 13:09:39 -06007820 xa_init(&tctx->xa);
7821 init_waitqueue_head(&tctx->wait);
7822 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007823 atomic_set(&tctx->in_idle, 0);
7824 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06007825 io_init_identity(&tctx->__identity);
7826 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06007827 task->io_uring = tctx;
7828 return 0;
7829}
7830
7831void __io_uring_free(struct task_struct *tsk)
7832{
7833 struct io_uring_task *tctx = tsk->io_uring;
7834
7835 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06007836 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
7837 if (tctx->identity != &tctx->__identity)
7838 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06007839 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007840 kfree(tctx);
7841 tsk->io_uring = NULL;
7842}
7843
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007844static int io_sq_offload_create(struct io_ring_ctx *ctx,
7845 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007846{
7847 int ret;
7848
Jens Axboe6c271ce2019-01-10 11:22:30 -07007849 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007850 struct io_sq_data *sqd;
7851
Jens Axboe3ec482d2019-04-08 10:51:01 -06007852 ret = -EPERM;
7853 if (!capable(CAP_SYS_ADMIN))
7854 goto err;
7855
Jens Axboe534ca6d2020-09-02 13:52:19 -06007856 sqd = io_get_sq_data(p);
7857 if (IS_ERR(sqd)) {
7858 ret = PTR_ERR(sqd);
7859 goto err;
7860 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007861
Jens Axboe534ca6d2020-09-02 13:52:19 -06007862 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007863 io_sq_thread_park(sqd);
7864 mutex_lock(&sqd->ctx_lock);
7865 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7866 mutex_unlock(&sqd->ctx_lock);
7867 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007868
Jens Axboe917257d2019-04-13 09:28:55 -06007869 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7870 if (!ctx->sq_thread_idle)
7871 ctx->sq_thread_idle = HZ;
7872
Jens Axboeaa061652020-09-02 14:50:27 -06007873 if (sqd->thread)
7874 goto done;
7875
Jens Axboe6c271ce2019-01-10 11:22:30 -07007876 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007877 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007878
Jens Axboe917257d2019-04-13 09:28:55 -06007879 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007880 if (cpu >= nr_cpu_ids)
7881 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007882 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007883 goto err;
7884
Jens Axboe69fb2132020-09-14 11:16:23 -06007885 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007886 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007887 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007888 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007889 "io_uring-sq");
7890 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007891 if (IS_ERR(sqd->thread)) {
7892 ret = PTR_ERR(sqd->thread);
7893 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007894 goto err;
7895 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007896 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007897 if (ret)
7898 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007899 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7900 /* Can't have SQ_AFF without SQPOLL */
7901 ret = -EINVAL;
7902 goto err;
7903 }
7904
Jens Axboeaa061652020-09-02 14:50:27 -06007905done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007906 ret = io_init_wq_offload(ctx, p);
7907 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007908 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007909
7910 return 0;
7911err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007912 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007913 return ret;
7914}
7915
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007916static void io_sq_offload_start(struct io_ring_ctx *ctx)
7917{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007918 struct io_sq_data *sqd = ctx->sq_data;
7919
7920 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7921 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007922}
7923
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007924static inline void __io_unaccount_mem(struct user_struct *user,
7925 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007926{
7927 atomic_long_sub(nr_pages, &user->locked_vm);
7928}
7929
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007930static inline int __io_account_mem(struct user_struct *user,
7931 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007932{
7933 unsigned long page_limit, cur_pages, new_pages;
7934
7935 /* Don't allow more pages than we can safely lock */
7936 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7937
7938 do {
7939 cur_pages = atomic_long_read(&user->locked_vm);
7940 new_pages = cur_pages + nr_pages;
7941 if (new_pages > page_limit)
7942 return -ENOMEM;
7943 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7944 new_pages) != cur_pages);
7945
7946 return 0;
7947}
7948
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007949static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7950 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007951{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007952 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007953 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007954
Jens Axboe2aede0e2020-09-14 10:45:53 -06007955 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007956 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007957 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007958 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007959 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007960 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007961}
7962
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007963static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7964 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007965{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007966 int ret;
7967
7968 if (ctx->limit_mem) {
7969 ret = __io_account_mem(ctx->user, nr_pages);
7970 if (ret)
7971 return ret;
7972 }
7973
Jens Axboe2aede0e2020-09-14 10:45:53 -06007974 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007975 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007976 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007977 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007978 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007979 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007980
7981 return 0;
7982}
7983
Jens Axboe2b188cc2019-01-07 10:46:33 -07007984static void io_mem_free(void *ptr)
7985{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007986 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007987
Mark Rutland52e04ef2019-04-30 17:30:21 +01007988 if (!ptr)
7989 return;
7990
7991 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007992 if (put_page_testzero(page))
7993 free_compound_page(page);
7994}
7995
7996static void *io_mem_alloc(size_t size)
7997{
7998 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7999 __GFP_NORETRY;
8000
8001 return (void *) __get_free_pages(gfp_flags, get_order(size));
8002}
8003
Hristo Venev75b28af2019-08-26 17:23:46 +00008004static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8005 size_t *sq_offset)
8006{
8007 struct io_rings *rings;
8008 size_t off, sq_array_size;
8009
8010 off = struct_size(rings, cqes, cq_entries);
8011 if (off == SIZE_MAX)
8012 return SIZE_MAX;
8013
8014#ifdef CONFIG_SMP
8015 off = ALIGN(off, SMP_CACHE_BYTES);
8016 if (off == 0)
8017 return SIZE_MAX;
8018#endif
8019
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008020 if (sq_offset)
8021 *sq_offset = off;
8022
Hristo Venev75b28af2019-08-26 17:23:46 +00008023 sq_array_size = array_size(sizeof(u32), sq_entries);
8024 if (sq_array_size == SIZE_MAX)
8025 return SIZE_MAX;
8026
8027 if (check_add_overflow(off, sq_array_size, &off))
8028 return SIZE_MAX;
8029
Hristo Venev75b28af2019-08-26 17:23:46 +00008030 return off;
8031}
8032
Jens Axboe2b188cc2019-01-07 10:46:33 -07008033static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8034{
Hristo Venev75b28af2019-08-26 17:23:46 +00008035 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008036
Hristo Venev75b28af2019-08-26 17:23:46 +00008037 pages = (size_t)1 << get_order(
8038 rings_size(sq_entries, cq_entries, NULL));
8039 pages += (size_t)1 << get_order(
8040 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008041
Hristo Venev75b28af2019-08-26 17:23:46 +00008042 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008043}
8044
Jens Axboeedafcce2019-01-09 09:16:05 -07008045static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8046{
8047 int i, j;
8048
8049 if (!ctx->user_bufs)
8050 return -ENXIO;
8051
8052 for (i = 0; i < ctx->nr_user_bufs; i++) {
8053 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8054
8055 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008056 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008057
Jens Axboede293932020-09-17 16:19:16 -06008058 if (imu->acct_pages)
8059 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008060 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008061 imu->nr_bvecs = 0;
8062 }
8063
8064 kfree(ctx->user_bufs);
8065 ctx->user_bufs = NULL;
8066 ctx->nr_user_bufs = 0;
8067 return 0;
8068}
8069
8070static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8071 void __user *arg, unsigned index)
8072{
8073 struct iovec __user *src;
8074
8075#ifdef CONFIG_COMPAT
8076 if (ctx->compat) {
8077 struct compat_iovec __user *ciovs;
8078 struct compat_iovec ciov;
8079
8080 ciovs = (struct compat_iovec __user *) arg;
8081 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8082 return -EFAULT;
8083
Jens Axboed55e5f52019-12-11 16:12:15 -07008084 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008085 dst->iov_len = ciov.iov_len;
8086 return 0;
8087 }
8088#endif
8089 src = (struct iovec __user *) arg;
8090 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8091 return -EFAULT;
8092 return 0;
8093}
8094
Jens Axboede293932020-09-17 16:19:16 -06008095/*
8096 * Not super efficient, but this is just a registration time. And we do cache
8097 * the last compound head, so generally we'll only do a full search if we don't
8098 * match that one.
8099 *
8100 * We check if the given compound head page has already been accounted, to
8101 * avoid double accounting it. This allows us to account the full size of the
8102 * page, not just the constituent pages of a huge page.
8103 */
8104static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8105 int nr_pages, struct page *hpage)
8106{
8107 int i, j;
8108
8109 /* check current page array */
8110 for (i = 0; i < nr_pages; i++) {
8111 if (!PageCompound(pages[i]))
8112 continue;
8113 if (compound_head(pages[i]) == hpage)
8114 return true;
8115 }
8116
8117 /* check previously registered pages */
8118 for (i = 0; i < ctx->nr_user_bufs; i++) {
8119 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8120
8121 for (j = 0; j < imu->nr_bvecs; j++) {
8122 if (!PageCompound(imu->bvec[j].bv_page))
8123 continue;
8124 if (compound_head(imu->bvec[j].bv_page) == hpage)
8125 return true;
8126 }
8127 }
8128
8129 return false;
8130}
8131
8132static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8133 int nr_pages, struct io_mapped_ubuf *imu,
8134 struct page **last_hpage)
8135{
8136 int i, ret;
8137
8138 for (i = 0; i < nr_pages; i++) {
8139 if (!PageCompound(pages[i])) {
8140 imu->acct_pages++;
8141 } else {
8142 struct page *hpage;
8143
8144 hpage = compound_head(pages[i]);
8145 if (hpage == *last_hpage)
8146 continue;
8147 *last_hpage = hpage;
8148 if (headpage_already_acct(ctx, pages, i, hpage))
8149 continue;
8150 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8151 }
8152 }
8153
8154 if (!imu->acct_pages)
8155 return 0;
8156
8157 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8158 if (ret)
8159 imu->acct_pages = 0;
8160 return ret;
8161}
8162
Jens Axboeedafcce2019-01-09 09:16:05 -07008163static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8164 unsigned nr_args)
8165{
8166 struct vm_area_struct **vmas = NULL;
8167 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008168 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008169 int i, j, got_pages = 0;
8170 int ret = -EINVAL;
8171
8172 if (ctx->user_bufs)
8173 return -EBUSY;
8174 if (!nr_args || nr_args > UIO_MAXIOV)
8175 return -EINVAL;
8176
8177 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8178 GFP_KERNEL);
8179 if (!ctx->user_bufs)
8180 return -ENOMEM;
8181
8182 for (i = 0; i < nr_args; i++) {
8183 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8184 unsigned long off, start, end, ubuf;
8185 int pret, nr_pages;
8186 struct iovec iov;
8187 size_t size;
8188
8189 ret = io_copy_iov(ctx, &iov, arg, i);
8190 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008191 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008192
8193 /*
8194 * Don't impose further limits on the size and buffer
8195 * constraints here, we'll -EINVAL later when IO is
8196 * submitted if they are wrong.
8197 */
8198 ret = -EFAULT;
8199 if (!iov.iov_base || !iov.iov_len)
8200 goto err;
8201
8202 /* arbitrary limit, but we need something */
8203 if (iov.iov_len > SZ_1G)
8204 goto err;
8205
8206 ubuf = (unsigned long) iov.iov_base;
8207 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8208 start = ubuf >> PAGE_SHIFT;
8209 nr_pages = end - start;
8210
Jens Axboeedafcce2019-01-09 09:16:05 -07008211 ret = 0;
8212 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008213 kvfree(vmas);
8214 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008215 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008216 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008217 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008218 sizeof(struct vm_area_struct *),
8219 GFP_KERNEL);
8220 if (!pages || !vmas) {
8221 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008222 goto err;
8223 }
8224 got_pages = nr_pages;
8225 }
8226
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008227 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008228 GFP_KERNEL);
8229 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008230 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008231 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008232
8233 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008234 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008235 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008236 FOLL_WRITE | FOLL_LONGTERM,
8237 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008238 if (pret == nr_pages) {
8239 /* don't support file backed memory */
8240 for (j = 0; j < nr_pages; j++) {
8241 struct vm_area_struct *vma = vmas[j];
8242
8243 if (vma->vm_file &&
8244 !is_file_hugepages(vma->vm_file)) {
8245 ret = -EOPNOTSUPP;
8246 break;
8247 }
8248 }
8249 } else {
8250 ret = pret < 0 ? pret : -EFAULT;
8251 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008252 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008253 if (ret) {
8254 /*
8255 * if we did partial map, or found file backed vmas,
8256 * release any pages we did get
8257 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008258 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008259 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008260 kvfree(imu->bvec);
8261 goto err;
8262 }
8263
8264 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8265 if (ret) {
8266 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008267 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008268 goto err;
8269 }
8270
8271 off = ubuf & ~PAGE_MASK;
8272 size = iov.iov_len;
8273 for (j = 0; j < nr_pages; j++) {
8274 size_t vec_len;
8275
8276 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8277 imu->bvec[j].bv_page = pages[j];
8278 imu->bvec[j].bv_len = vec_len;
8279 imu->bvec[j].bv_offset = off;
8280 off = 0;
8281 size -= vec_len;
8282 }
8283 /* store original address for later verification */
8284 imu->ubuf = ubuf;
8285 imu->len = iov.iov_len;
8286 imu->nr_bvecs = nr_pages;
8287
8288 ctx->nr_user_bufs++;
8289 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008290 kvfree(pages);
8291 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008292 return 0;
8293err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008294 kvfree(pages);
8295 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008296 io_sqe_buffer_unregister(ctx);
8297 return ret;
8298}
8299
Jens Axboe9b402842019-04-11 11:45:41 -06008300static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8301{
8302 __s32 __user *fds = arg;
8303 int fd;
8304
8305 if (ctx->cq_ev_fd)
8306 return -EBUSY;
8307
8308 if (copy_from_user(&fd, fds, sizeof(*fds)))
8309 return -EFAULT;
8310
8311 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8312 if (IS_ERR(ctx->cq_ev_fd)) {
8313 int ret = PTR_ERR(ctx->cq_ev_fd);
8314 ctx->cq_ev_fd = NULL;
8315 return ret;
8316 }
8317
8318 return 0;
8319}
8320
8321static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8322{
8323 if (ctx->cq_ev_fd) {
8324 eventfd_ctx_put(ctx->cq_ev_fd);
8325 ctx->cq_ev_fd = NULL;
8326 return 0;
8327 }
8328
8329 return -ENXIO;
8330}
8331
Jens Axboe5a2e7452020-02-23 16:23:11 -07008332static int __io_destroy_buffers(int id, void *p, void *data)
8333{
8334 struct io_ring_ctx *ctx = data;
8335 struct io_buffer *buf = p;
8336
Jens Axboe067524e2020-03-02 16:32:28 -07008337 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008338 return 0;
8339}
8340
8341static void io_destroy_buffers(struct io_ring_ctx *ctx)
8342{
8343 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8344 idr_destroy(&ctx->io_buffer_idr);
8345}
8346
Jens Axboe2b188cc2019-01-07 10:46:33 -07008347static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8348{
Jens Axboe6b063142019-01-10 22:13:58 -07008349 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008350 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008351
8352 if (ctx->sqo_task) {
8353 put_task_struct(ctx->sqo_task);
8354 ctx->sqo_task = NULL;
8355 mmdrop(ctx->mm_account);
8356 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008357 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008358
Dennis Zhou91d8f512020-09-16 13:41:05 -07008359#ifdef CONFIG_BLK_CGROUP
8360 if (ctx->sqo_blkcg_css)
8361 css_put(ctx->sqo_blkcg_css);
8362#endif
8363
Jens Axboe6b063142019-01-10 22:13:58 -07008364 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008365 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008366 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008367 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008368
Jens Axboe2b188cc2019-01-07 10:46:33 -07008369#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008370 if (ctx->ring_sock) {
8371 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008372 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008373 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008374#endif
8375
Hristo Venev75b28af2019-08-26 17:23:46 +00008376 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008377 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008378
8379 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008380 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008381 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008382 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008383 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008384 kfree(ctx);
8385}
8386
8387static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8388{
8389 struct io_ring_ctx *ctx = file->private_data;
8390 __poll_t mask = 0;
8391
8392 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008393 /*
8394 * synchronizes with barrier from wq_has_sleeper call in
8395 * io_commit_cqring
8396 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008397 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008398 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008399 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov85e25e22021-01-12 21:17:26 +00008400 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8401 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008402 mask |= EPOLLIN | EPOLLRDNORM;
8403
8404 return mask;
8405}
8406
8407static int io_uring_fasync(int fd, struct file *file, int on)
8408{
8409 struct io_ring_ctx *ctx = file->private_data;
8410
8411 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8412}
8413
Jens Axboe071698e2020-01-28 10:04:42 -07008414static int io_remove_personalities(int id, void *p, void *data)
8415{
8416 struct io_ring_ctx *ctx = data;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008417 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008418
Jens Axboe1e6fa522020-10-15 08:46:24 -06008419 iod = idr_remove(&ctx->personality_idr, id);
8420 if (iod) {
8421 put_cred(iod->creds);
8422 if (refcount_dec_and_test(&iod->count))
8423 kfree(iod);
8424 }
Jens Axboe071698e2020-01-28 10:04:42 -07008425 return 0;
8426}
8427
Jens Axboe85faa7b2020-04-09 18:14:00 -06008428static void io_ring_exit_work(struct work_struct *work)
8429{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008430 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8431 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008432
Jens Axboe56952e92020-06-17 15:00:04 -06008433 /*
8434 * If we're doing polled IO and end up having requests being
8435 * submitted async (out-of-line), then completions can come in while
8436 * we're waiting for refs to drop. We need to reap these manually,
8437 * as nobody else will be looking for them.
8438 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008439 do {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008440 io_iopoll_try_reap_events(ctx);
8441 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008442 io_ring_ctx_free(ctx);
8443}
8444
Jens Axboe7b81e2a2020-12-20 10:45:02 -07008445static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8446{
8447 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8448
8449 return req->ctx == data;
8450}
8451
Jens Axboe2b188cc2019-01-07 10:46:33 -07008452static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8453{
8454 mutex_lock(&ctx->uring_lock);
8455 percpu_ref_kill(&ctx->refs);
Pavel Begunkovb2ec2b12020-12-17 00:24:35 +00008456 /* if force is set, the ring is going away. always drop after that */
8457 ctx->cq_overflow_flushed = 1;
Pavel Begunkovc0fd45a2020-12-06 22:22:44 +00008458 if (ctx->rings)
Pavel Begunkov85e25e22021-01-12 21:17:26 +00008459 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008460 mutex_unlock(&ctx->uring_lock);
8461
Jens Axboef3606e32020-09-22 08:18:24 -06008462 io_kill_timeouts(ctx, NULL);
8463 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008464
8465 if (ctx->io_wq)
Jens Axboe7b81e2a2020-12-20 10:45:02 -07008466 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008467
Jens Axboe15dff282019-11-13 09:09:23 -07008468 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008469 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008470 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008471
8472 /*
8473 * Do this upfront, so we won't have a grace period where the ring
8474 * is closed but resources aren't reaped yet. This can cause
8475 * spurious failure in setting up a new ring.
8476 */
Jens Axboe760618f2020-07-24 12:53:31 -06008477 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8478 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008479
Jens Axboe85faa7b2020-04-09 18:14:00 -06008480 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008481 /*
8482 * Use system_unbound_wq to avoid spawning tons of event kworkers
8483 * if we're exiting a ton of rings at the same time. It just adds
8484 * noise and overhead, there's no discernable change in runtime
8485 * over using system_wq.
8486 */
8487 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008488}
8489
8490static int io_uring_release(struct inode *inode, struct file *file)
8491{
8492 struct io_ring_ctx *ctx = file->private_data;
8493
8494 file->private_data = NULL;
8495 io_ring_ctx_wait_and_kill(ctx);
8496 return 0;
8497}
8498
Jens Axboef254ac02020-08-12 17:33:30 -06008499/*
8500 * Returns true if 'preq' is the link parent of 'req'
8501 */
8502static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8503{
8504 struct io_kiocb *link;
8505
8506 if (!(preq->flags & REQ_F_LINK_HEAD))
8507 return false;
8508
8509 list_for_each_entry(link, &preq->link_list, link_list) {
8510 if (link == req)
8511 return true;
8512 }
8513
8514 return false;
8515}
8516
8517/*
8518 * We're looking to cancel 'req' because it's holding on to our files, but
8519 * 'req' could be a link to another request. See if it is, and cancel that
8520 * parent request if so.
8521 */
8522static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8523{
8524 struct hlist_node *tmp;
8525 struct io_kiocb *preq;
8526 bool found = false;
8527 int i;
8528
8529 spin_lock_irq(&ctx->completion_lock);
8530 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8531 struct hlist_head *list;
8532
8533 list = &ctx->cancel_hash[i];
8534 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8535 found = io_match_link(preq, req);
8536 if (found) {
8537 io_poll_remove_one(preq);
8538 break;
8539 }
8540 }
8541 }
8542 spin_unlock_irq(&ctx->completion_lock);
8543 return found;
8544}
8545
8546static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8547 struct io_kiocb *req)
8548{
8549 struct io_kiocb *preq;
8550 bool found = false;
8551
8552 spin_lock_irq(&ctx->completion_lock);
8553 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8554 found = io_match_link(preq, req);
8555 if (found) {
8556 __io_timeout_cancel(preq);
8557 break;
8558 }
8559 }
8560 spin_unlock_irq(&ctx->completion_lock);
8561 return found;
8562}
8563
Jens Axboeb711d4e2020-08-16 08:23:05 -07008564static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8565{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008566 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8567 bool ret;
8568
8569 if (req->flags & REQ_F_LINK_TIMEOUT) {
8570 unsigned long flags;
8571 struct io_ring_ctx *ctx = req->ctx;
8572
8573 /* protect against races with linked timeouts */
8574 spin_lock_irqsave(&ctx->completion_lock, flags);
8575 ret = io_match_link(req, data);
8576 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8577 } else {
8578 ret = io_match_link(req, data);
8579 }
8580 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008581}
8582
8583static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8584{
8585 enum io_wq_cancel cret;
8586
8587 /* cancel this particular work, if it's running */
8588 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8589 if (cret != IO_WQ_CANCEL_NOTFOUND)
8590 return;
8591
8592 /* find links that hold this pending, cancel those */
8593 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8594 if (cret != IO_WQ_CANCEL_NOTFOUND)
8595 return;
8596
8597 /* if we have a poll link holding this pending, cancel that */
8598 if (io_poll_remove_link(ctx, req))
8599 return;
8600
8601 /* final option, timeout link is holding this req pending */
8602 io_timeout_remove_link(ctx, req);
8603}
8604
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008605static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008606 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008607 struct files_struct *files)
8608{
8609 struct io_defer_entry *de = NULL;
8610 LIST_HEAD(list);
8611
8612 spin_lock_irq(&ctx->completion_lock);
8613 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008614 if (io_task_match(de->req, task) &&
8615 io_match_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008616 list_cut_position(&list, &ctx->defer_list, &de->list);
8617 break;
8618 }
8619 }
8620 spin_unlock_irq(&ctx->completion_lock);
8621
8622 while (!list_empty(&list)) {
8623 de = list_first_entry(&list, struct io_defer_entry, list);
8624 list_del_init(&de->list);
8625 req_set_fail_links(de->req);
8626 io_put_req(de->req);
8627 io_req_complete(de->req, -ECANCELED);
8628 kfree(de);
8629 }
8630}
8631
Jens Axboe76e1b642020-09-26 15:05:03 -06008632/*
8633 * Returns true if we found and killed one or more files pinning requests
8634 */
8635static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkova773dea2020-11-06 13:00:23 +00008636 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008637 struct files_struct *files)
8638{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008639 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008640 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008641
Jens Axboefcb323c2019-10-24 12:39:47 -06008642 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008643 struct io_kiocb *cancel_req = NULL, *req;
8644 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008645
8646 spin_lock_irq(&ctx->inflight_lock);
8647 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Pavel Begunkova773dea2020-11-06 13:00:23 +00008648 if (req->task == task &&
8649 (req->work.flags & IO_WQ_WORK_FILES) &&
Jens Axboe98447d62020-10-14 10:48:51 -06008650 req->work.identity->files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008651 continue;
8652 /* req is being completed, ignore */
8653 if (!refcount_inc_not_zero(&req->refs))
8654 continue;
8655 cancel_req = req;
8656 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008657 }
Jens Axboe768134d2019-11-10 20:30:53 -07008658 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008659 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008660 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008661 spin_unlock_irq(&ctx->inflight_lock);
8662
Jens Axboe768134d2019-11-10 20:30:53 -07008663 /* We need to keep going until we don't find a matching req */
8664 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008665 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008666 /* cancel this request, or head link requests */
8667 io_attempt_cancel(ctx, cancel_req);
8668 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008669 /* cancellations _may_ trigger task work */
8670 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008671 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008672 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008673 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008674
8675 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008676}
8677
Pavel Begunkov801dd572020-06-15 10:33:14 +03008678static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008679{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008680 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8681 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008682
Jens Axboef3606e32020-09-22 08:18:24 -06008683 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008684}
8685
Jens Axboe0f212202020-09-13 13:09:39 -06008686static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8687 struct task_struct *task,
8688 struct files_struct *files)
8689{
8690 bool ret;
8691
Pavel Begunkova773dea2020-11-06 13:00:23 +00008692 ret = io_uring_cancel_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008693 if (!files) {
8694 enum io_wq_cancel cret;
8695
8696 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8697 if (cret != IO_WQ_CANCEL_NOTFOUND)
8698 ret = true;
8699
8700 /* SQPOLL thread does its own polling */
8701 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8702 while (!list_empty_careful(&ctx->iopoll_list)) {
8703 io_iopoll_try_reap_events(ctx);
8704 ret = true;
8705 }
8706 }
8707
8708 ret |= io_poll_remove_all(ctx, task);
8709 ret |= io_kill_timeouts(ctx, task);
8710 }
8711
8712 return ret;
8713}
8714
8715/*
8716 * We need to iteratively cancel requests, in case a request has dependent
8717 * hard links. These persist even for failure of cancelations, hence keep
8718 * looping until none are found.
8719 */
8720static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8721 struct files_struct *files)
8722{
8723 struct task_struct *task = current;
8724
Jens Axboefdaf0832020-10-30 09:37:30 -06008725 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008726 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008727 atomic_inc(&task->io_uring->in_idle);
8728 io_sq_thread_park(ctx->sq_data);
8729 }
Jens Axboe0f212202020-09-13 13:09:39 -06008730
Pavel Begunkova773dea2020-11-06 13:00:23 +00008731 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008732 io_cqring_overflow_flush(ctx, true, task, files);
8733
8734 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8735 io_run_task_work();
8736 cond_resched();
8737 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008738
8739 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8740 atomic_dec(&task->io_uring->in_idle);
8741 /*
8742 * If the files that are going away are the ones in the thread
8743 * identity, clear them out.
8744 */
8745 if (task->io_uring->identity->files == files)
8746 task->io_uring->identity->files = NULL;
8747 io_sq_thread_unpark(ctx->sq_data);
8748 }
Jens Axboe0f212202020-09-13 13:09:39 -06008749}
8750
8751/*
8752 * Note that this task has used io_uring. We use it for cancelation purposes.
8753 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008754static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008755{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008756 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9f8ebec2020-12-21 18:34:04 +00008757 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008758
8759 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008760 ret = io_uring_alloc_task_context(current);
8761 if (unlikely(ret))
8762 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008763 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008764 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008765 if (tctx->last != file) {
8766 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008767
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008768 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008769 get_file(file);
Pavel Begunkov9f8ebec2020-12-21 18:34:04 +00008770 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8771 file, GFP_KERNEL));
8772 if (ret) {
8773 fput(file);
8774 return ret;
8775 }
Jens Axboe0f212202020-09-13 13:09:39 -06008776 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008777 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008778 }
8779
Jens Axboefdaf0832020-10-30 09:37:30 -06008780 /*
8781 * This is race safe in that the task itself is doing this, hence it
8782 * cannot be going through the exit/cancel paths at the same time.
8783 * This cannot be modified while exit/cancel is running.
8784 */
8785 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8786 tctx->sqpoll = true;
8787
Jens Axboe0f212202020-09-13 13:09:39 -06008788 return 0;
8789}
8790
8791/*
8792 * Remove this io_uring_file -> task mapping.
8793 */
8794static void io_uring_del_task_file(struct file *file)
8795{
8796 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008797
8798 if (tctx->last == file)
8799 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008800 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008801 if (file)
8802 fput(file);
8803}
8804
Jens Axboe0f212202020-09-13 13:09:39 -06008805/*
8806 * Drop task note for this file if we're the only ones that hold it after
8807 * pending fput()
8808 */
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01008809static void io_uring_attempt_task_drop(struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008810{
8811 if (!current->io_uring)
8812 return;
8813 /*
8814 * fput() is pending, will be 2 if the only other ref is our potential
8815 * task file note. If the task is exiting, drop regardless of count.
8816 */
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01008817 if (fatal_signal_pending(current) || (current->flags & PF_EXITING) ||
8818 atomic_long_read(&file->f_count) == 2)
8819 io_uring_del_task_file(file);
Jens Axboe0f212202020-09-13 13:09:39 -06008820}
8821
8822void __io_uring_files_cancel(struct files_struct *files)
8823{
8824 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008825 struct file *file;
8826 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008827
8828 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008829 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008830
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008831 xa_for_each(&tctx->xa, index, file) {
8832 struct io_ring_ctx *ctx = file->private_data;
Jens Axboe0f212202020-09-13 13:09:39 -06008833
8834 io_uring_cancel_task_requests(ctx, files);
8835 if (files)
8836 io_uring_del_task_file(file);
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008837 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008838
8839 atomic_dec(&tctx->in_idle);
8840}
8841
8842static s64 tctx_inflight(struct io_uring_task *tctx)
8843{
8844 unsigned long index;
8845 struct file *file;
8846 s64 inflight;
8847
8848 inflight = percpu_counter_sum(&tctx->inflight);
8849 if (!tctx->sqpoll)
8850 return inflight;
8851
8852 /*
8853 * If we have SQPOLL rings, then we need to iterate and find them, and
8854 * add the pending count for those.
8855 */
8856 xa_for_each(&tctx->xa, index, file) {
8857 struct io_ring_ctx *ctx = file->private_data;
8858
8859 if (ctx->flags & IORING_SETUP_SQPOLL) {
8860 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
8861
8862 inflight += percpu_counter_sum(&__tctx->inflight);
8863 }
8864 }
8865
8866 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008867}
8868
Jens Axboe0f212202020-09-13 13:09:39 -06008869/*
8870 * Find any io_uring fd that this task has registered or done IO on, and cancel
8871 * requests.
8872 */
8873void __io_uring_task_cancel(void)
8874{
8875 struct io_uring_task *tctx = current->io_uring;
8876 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008877 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008878
8879 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008880 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008881
Jens Axboed8a6df12020-10-15 16:24:45 -06008882 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008883 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008884 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008885 if (!inflight)
8886 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008887 __io_uring_files_cancel(NULL);
8888
8889 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8890
8891 /*
8892 * If we've seen completions, retry. This avoids a race where
8893 * a completion comes in before we did prepare_to_wait().
8894 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008895 if (inflight != tctx_inflight(tctx))
Jens Axboe0f212202020-09-13 13:09:39 -06008896 continue;
Jens Axboe0f212202020-09-13 13:09:39 -06008897 schedule();
Jens Axboed8a6df12020-10-15 16:24:45 -06008898 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008899
8900 finish_wait(&tctx->wait, &wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008901 atomic_dec(&tctx->in_idle);
Jens Axboefcb323c2019-10-24 12:39:47 -06008902}
8903
8904static int io_uring_flush(struct file *file, void *data)
8905{
Pavel Begunkovc8fb20b2020-10-22 16:38:27 +01008906 io_uring_attempt_task_drop(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06008907 return 0;
8908}
8909
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008910static void *io_uring_validate_mmap_request(struct file *file,
8911 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008912{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008913 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008914 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008915 struct page *page;
8916 void *ptr;
8917
8918 switch (offset) {
8919 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008920 case IORING_OFF_CQ_RING:
8921 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008922 break;
8923 case IORING_OFF_SQES:
8924 ptr = ctx->sq_sqes;
8925 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008926 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008927 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008928 }
8929
8930 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008931 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008932 return ERR_PTR(-EINVAL);
8933
8934 return ptr;
8935}
8936
8937#ifdef CONFIG_MMU
8938
8939static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8940{
8941 size_t sz = vma->vm_end - vma->vm_start;
8942 unsigned long pfn;
8943 void *ptr;
8944
8945 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8946 if (IS_ERR(ptr))
8947 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008948
8949 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8950 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8951}
8952
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008953#else /* !CONFIG_MMU */
8954
8955static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8956{
8957 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8958}
8959
8960static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8961{
8962 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8963}
8964
8965static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8966 unsigned long addr, unsigned long len,
8967 unsigned long pgoff, unsigned long flags)
8968{
8969 void *ptr;
8970
8971 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8972 if (IS_ERR(ptr))
8973 return PTR_ERR(ptr);
8974
8975 return (unsigned long) ptr;
8976}
8977
8978#endif /* !CONFIG_MMU */
8979
Jens Axboe90554202020-09-03 12:12:41 -06008980static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8981{
8982 DEFINE_WAIT(wait);
8983
8984 do {
8985 if (!io_sqring_full(ctx))
8986 break;
8987
8988 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8989
8990 if (!io_sqring_full(ctx))
8991 break;
8992
8993 schedule();
8994 } while (!signal_pending(current));
8995
8996 finish_wait(&ctx->sqo_sq_wait, &wait);
8997}
8998
Jens Axboe2b188cc2019-01-07 10:46:33 -07008999SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
9000 u32, min_complete, u32, flags, const sigset_t __user *, sig,
9001 size_t, sigsz)
9002{
9003 struct io_ring_ctx *ctx;
9004 long ret = -EBADF;
9005 int submitted = 0;
9006 struct fd f;
9007
Jens Axboe4c6e2772020-07-01 11:29:10 -06009008 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009009
Jens Axboe90554202020-09-03 12:12:41 -06009010 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9011 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009012 return -EINVAL;
9013
9014 f = fdget(fd);
9015 if (!f.file)
9016 return -EBADF;
9017
9018 ret = -EOPNOTSUPP;
9019 if (f.file->f_op != &io_uring_fops)
9020 goto out_fput;
9021
9022 ret = -ENXIO;
9023 ctx = f.file->private_data;
9024 if (!percpu_ref_tryget(&ctx->refs))
9025 goto out_fput;
9026
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009027 ret = -EBADFD;
9028 if (ctx->flags & IORING_SETUP_R_DISABLED)
9029 goto out;
9030
Jens Axboe6c271ce2019-01-10 11:22:30 -07009031 /*
9032 * For SQ polling, the thread will do all submissions and completions.
9033 * Just return the requested submit count, and wake the thread if
9034 * we were asked to.
9035 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009036 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009037 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov85e25e22021-01-12 21:17:26 +00009038 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkovbc924dd2021-01-12 21:17:25 +00009039
Jens Axboe6c271ce2019-01-10 11:22:30 -07009040 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009041 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06009042 if (flags & IORING_ENTER_SQ_WAIT)
9043 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07009044 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009045 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009046 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009047 if (unlikely(ret))
9048 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009049 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009050 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009051 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009052
9053 if (submitted != to_submit)
9054 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009055 }
9056 if (flags & IORING_ENTER_GETEVENTS) {
9057 min_complete = min(min_complete, ctx->cq_entries);
9058
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009059 /*
9060 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9061 * space applications don't need to do io completion events
9062 * polling again, they can rely on io_sq_thread to do polling
9063 * work, which can reduce cpu usage and uring_lock contention.
9064 */
9065 if (ctx->flags & IORING_SETUP_IOPOLL &&
9066 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009067 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009068 } else {
9069 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
9070 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009071 }
9072
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009073out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009074 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009075out_fput:
9076 fdput(f);
9077 return submitted ? submitted : ret;
9078}
9079
Tobias Klauserbebdb652020-02-26 18:38:32 +01009080#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009081static int io_uring_show_cred(int id, void *p, void *data)
9082{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009083 struct io_identity *iod = p;
9084 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009085 struct seq_file *m = data;
9086 struct user_namespace *uns = seq_user_ns(m);
9087 struct group_info *gi;
9088 kernel_cap_t cap;
9089 unsigned __capi;
9090 int g;
9091
9092 seq_printf(m, "%5d\n", id);
9093 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9094 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9095 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9096 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9097 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9098 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9099 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9100 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9101 seq_puts(m, "\n\tGroups:\t");
9102 gi = cred->group_info;
9103 for (g = 0; g < gi->ngroups; g++) {
9104 seq_put_decimal_ull(m, g ? " " : "",
9105 from_kgid_munged(uns, gi->gid[g]));
9106 }
9107 seq_puts(m, "\n\tCapEff:\t");
9108 cap = cred->cap_effective;
9109 CAP_FOR_EACH_U32(__capi)
9110 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9111 seq_putc(m, '\n');
9112 return 0;
9113}
9114
9115static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9116{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009117 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009118 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009119 int i;
9120
Jens Axboefad8e0d2020-09-28 08:57:48 -06009121 /*
9122 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9123 * since fdinfo case grabs it in the opposite direction of normal use
9124 * cases. If we fail to get the lock, we just don't iterate any
9125 * structures that could be going away outside the io_uring mutex.
9126 */
9127 has_lock = mutex_trylock(&ctx->uring_lock);
9128
Joseph Qidbbe9c62020-09-29 09:01:22 -06009129 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9130 sq = ctx->sq_data;
9131
9132 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9133 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009134 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009135 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009136 struct fixed_file_table *table;
9137 struct file *f;
9138
9139 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9140 f = table->files[i & IORING_FILE_TABLE_MASK];
9141 if (f)
9142 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9143 else
9144 seq_printf(m, "%5u: <none>\n", i);
9145 }
9146 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009147 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009148 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9149
9150 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9151 (unsigned int) buf->len);
9152 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009153 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009154 seq_printf(m, "Personalities:\n");
9155 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9156 }
Jens Axboed7718a92020-02-14 22:23:12 -07009157 seq_printf(m, "PollList:\n");
9158 spin_lock_irq(&ctx->completion_lock);
9159 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9160 struct hlist_head *list = &ctx->cancel_hash[i];
9161 struct io_kiocb *req;
9162
9163 hlist_for_each_entry(req, list, hash_node)
9164 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9165 req->task->task_works != NULL);
9166 }
9167 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009168 if (has_lock)
9169 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009170}
9171
9172static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9173{
9174 struct io_ring_ctx *ctx = f->private_data;
9175
9176 if (percpu_ref_tryget(&ctx->refs)) {
9177 __io_uring_show_fdinfo(ctx, m);
9178 percpu_ref_put(&ctx->refs);
9179 }
9180}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009181#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009182
Jens Axboe2b188cc2019-01-07 10:46:33 -07009183static const struct file_operations io_uring_fops = {
9184 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009185 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009186 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009187#ifndef CONFIG_MMU
9188 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9189 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9190#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009191 .poll = io_uring_poll,
9192 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009193#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009194 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009195#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009196};
9197
9198static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9199 struct io_uring_params *p)
9200{
Hristo Venev75b28af2019-08-26 17:23:46 +00009201 struct io_rings *rings;
9202 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009203
Jens Axboebd740482020-08-05 12:58:23 -06009204 /* make sure these are sane, as we already accounted them */
9205 ctx->sq_entries = p->sq_entries;
9206 ctx->cq_entries = p->cq_entries;
9207
Hristo Venev75b28af2019-08-26 17:23:46 +00009208 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9209 if (size == SIZE_MAX)
9210 return -EOVERFLOW;
9211
9212 rings = io_mem_alloc(size);
9213 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009214 return -ENOMEM;
9215
Hristo Venev75b28af2019-08-26 17:23:46 +00009216 ctx->rings = rings;
9217 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9218 rings->sq_ring_mask = p->sq_entries - 1;
9219 rings->cq_ring_mask = p->cq_entries - 1;
9220 rings->sq_ring_entries = p->sq_entries;
9221 rings->cq_ring_entries = p->cq_entries;
9222 ctx->sq_mask = rings->sq_ring_mask;
9223 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009224
9225 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009226 if (size == SIZE_MAX) {
9227 io_mem_free(ctx->rings);
9228 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009229 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009230 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009231
9232 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009233 if (!ctx->sq_sqes) {
9234 io_mem_free(ctx->rings);
9235 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009236 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009237 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009238
Jens Axboe2b188cc2019-01-07 10:46:33 -07009239 return 0;
9240}
9241
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009242static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9243{
9244 int ret, fd;
9245
9246 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9247 if (fd < 0)
9248 return fd;
9249
9250 ret = io_uring_add_task_file(ctx, file);
9251 if (ret) {
9252 put_unused_fd(fd);
9253 return ret;
9254 }
9255 fd_install(fd, file);
9256 return fd;
9257}
9258
Jens Axboe2b188cc2019-01-07 10:46:33 -07009259/*
9260 * Allocate an anonymous fd, this is what constitutes the application
9261 * visible backing of an io_uring instance. The application mmaps this
9262 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9263 * we have to tie this fd to a socket for file garbage collection purposes.
9264 */
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009265static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009266{
9267 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009268#if defined(CONFIG_UNIX)
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009269 int ret;
9270
Jens Axboe2b188cc2019-01-07 10:46:33 -07009271 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9272 &ctx->ring_sock);
9273 if (ret)
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009274 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009275#endif
9276
Jens Axboe2b188cc2019-01-07 10:46:33 -07009277 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9278 O_RDWR | O_CLOEXEC);
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009279#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009280 if (IS_ERR(file)) {
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009281 sock_release(ctx->ring_sock);
9282 ctx->ring_sock = NULL;
9283 } else {
9284 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009285 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009286#endif
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009287 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009288}
9289
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009290static int io_uring_create(unsigned entries, struct io_uring_params *p,
9291 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009292{
9293 struct user_struct *user = NULL;
9294 struct io_ring_ctx *ctx;
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009295 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009296 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009297 int ret;
9298
Jens Axboe8110c1a2019-12-28 15:39:54 -07009299 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009300 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009301 if (entries > IORING_MAX_ENTRIES) {
9302 if (!(p->flags & IORING_SETUP_CLAMP))
9303 return -EINVAL;
9304 entries = IORING_MAX_ENTRIES;
9305 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009306
9307 /*
9308 * Use twice as many entries for the CQ ring. It's possible for the
9309 * application to drive a higher depth than the size of the SQ ring,
9310 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009311 * some flexibility in overcommitting a bit. If the application has
9312 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9313 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009314 */
9315 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009316 if (p->flags & IORING_SETUP_CQSIZE) {
9317 /*
9318 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9319 * to a power-of-two, if it isn't already. We do NOT impose
9320 * any cq vs sq ring sizing.
9321 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009322 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009323 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009324 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9325 if (!(p->flags & IORING_SETUP_CLAMP))
9326 return -EINVAL;
9327 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9328 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009329 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9330 if (p->cq_entries < p->sq_entries)
9331 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009332 } else {
9333 p->cq_entries = 2 * p->sq_entries;
9334 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009335
9336 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009337 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009338
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009339 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009340 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009341 ring_pages(p->sq_entries, p->cq_entries));
9342 if (ret) {
9343 free_uid(user);
9344 return ret;
9345 }
9346 }
9347
9348 ctx = io_ring_ctx_alloc(p);
9349 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009350 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009351 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009352 p->cq_entries));
9353 free_uid(user);
9354 return -ENOMEM;
9355 }
9356 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009357 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009358 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009359#ifdef CONFIG_AUDIT
9360 ctx->loginuid = current->loginuid;
9361 ctx->sessionid = current->sessionid;
9362#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009363 ctx->sqo_task = get_task_struct(current);
9364
9365 /*
9366 * This is just grabbed for accounting purposes. When a process exits,
9367 * the mm is exited and dropped before the files, hence we need to hang
9368 * on to this mm purely for the purposes of being able to unaccount
9369 * memory (locked/pinned vm). It's not used for anything else.
9370 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009371 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009372 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009373
Dennis Zhou91d8f512020-09-16 13:41:05 -07009374#ifdef CONFIG_BLK_CGROUP
9375 /*
9376 * The sq thread will belong to the original cgroup it was inited in.
9377 * If the cgroup goes offline (e.g. disabling the io controller), then
9378 * issued bios will be associated with the closest cgroup later in the
9379 * block layer.
9380 */
9381 rcu_read_lock();
9382 ctx->sqo_blkcg_css = blkcg_css();
9383 ret = css_tryget_online(ctx->sqo_blkcg_css);
9384 rcu_read_unlock();
9385 if (!ret) {
9386 /* don't init against a dying cgroup, have the user try again */
9387 ctx->sqo_blkcg_css = NULL;
9388 ret = -ENODEV;
9389 goto err;
9390 }
9391#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009392
Jens Axboe2b188cc2019-01-07 10:46:33 -07009393 /*
9394 * Account memory _before_ installing the file descriptor. Once
9395 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009396 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009397 * will un-account as well.
9398 */
9399 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9400 ACCT_LOCKED);
9401 ctx->limit_mem = limit_mem;
9402
9403 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009404 if (ret)
9405 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009406
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009407 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009408 if (ret)
9409 goto err;
9410
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009411 if (!(p->flags & IORING_SETUP_R_DISABLED))
9412 io_sq_offload_start(ctx);
9413
Jens Axboe2b188cc2019-01-07 10:46:33 -07009414 memset(&p->sq_off, 0, sizeof(p->sq_off));
9415 p->sq_off.head = offsetof(struct io_rings, sq.head);
9416 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9417 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9418 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9419 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9420 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9421 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9422
9423 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009424 p->cq_off.head = offsetof(struct io_rings, cq.head);
9425 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9426 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9427 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9428 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9429 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009430 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009431
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009432 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9433 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009434 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9435 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009436
9437 if (copy_to_user(params, p, sizeof(*p))) {
9438 ret = -EFAULT;
9439 goto err;
9440 }
Jens Axboed1719f72020-07-30 13:43:53 -06009441
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009442 file = io_uring_get_file(ctx);
9443 if (IS_ERR(file)) {
9444 ret = PTR_ERR(file);
9445 goto err;
9446 }
9447
Jens Axboed1719f72020-07-30 13:43:53 -06009448 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009449 * Install ring fd as the very last thing, so we don't risk someone
9450 * having closed it before we finish setup
9451 */
Pavel Begunkov5998fe52020-12-21 18:34:05 +00009452 ret = io_uring_install_fd(ctx, file);
9453 if (ret < 0) {
9454 /* fput will clean it up */
9455 fput(file);
9456 return ret;
9457 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009458
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009459 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009460 return ret;
9461err:
9462 io_ring_ctx_wait_and_kill(ctx);
9463 return ret;
9464}
9465
9466/*
9467 * Sets up an aio uring context, and returns the fd. Applications asks for a
9468 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9469 * params structure passed in.
9470 */
9471static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9472{
9473 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009474 int i;
9475
9476 if (copy_from_user(&p, params, sizeof(p)))
9477 return -EFAULT;
9478 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9479 if (p.resv[i])
9480 return -EINVAL;
9481 }
9482
Jens Axboe6c271ce2019-01-10 11:22:30 -07009483 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009484 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009485 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9486 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009487 return -EINVAL;
9488
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009489 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009490}
9491
9492SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9493 struct io_uring_params __user *, params)
9494{
9495 return io_uring_setup(entries, params);
9496}
9497
Jens Axboe66f4af92020-01-16 15:36:52 -07009498static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9499{
9500 struct io_uring_probe *p;
9501 size_t size;
9502 int i, ret;
9503
9504 size = struct_size(p, ops, nr_args);
9505 if (size == SIZE_MAX)
9506 return -EOVERFLOW;
9507 p = kzalloc(size, GFP_KERNEL);
9508 if (!p)
9509 return -ENOMEM;
9510
9511 ret = -EFAULT;
9512 if (copy_from_user(p, arg, size))
9513 goto out;
9514 ret = -EINVAL;
9515 if (memchr_inv(p, 0, size))
9516 goto out;
9517
9518 p->last_op = IORING_OP_LAST - 1;
9519 if (nr_args > IORING_OP_LAST)
9520 nr_args = IORING_OP_LAST;
9521
9522 for (i = 0; i < nr_args; i++) {
9523 p->ops[i].op = i;
9524 if (!io_op_defs[i].not_supported)
9525 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9526 }
9527 p->ops_len = i;
9528
9529 ret = 0;
9530 if (copy_to_user(arg, p, size))
9531 ret = -EFAULT;
9532out:
9533 kfree(p);
9534 return ret;
9535}
9536
Jens Axboe071698e2020-01-28 10:04:42 -07009537static int io_register_personality(struct io_ring_ctx *ctx)
9538{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009539 struct io_identity *id;
9540 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009541
Jens Axboe1e6fa522020-10-15 08:46:24 -06009542 id = kmalloc(sizeof(*id), GFP_KERNEL);
9543 if (unlikely(!id))
9544 return -ENOMEM;
9545
9546 io_init_identity(id);
9547 id->creds = get_current_cred();
9548
9549 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9550 if (ret < 0) {
9551 put_cred(id->creds);
9552 kfree(id);
9553 }
9554 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009555}
9556
9557static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9558{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009559 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07009560
Jens Axboe1e6fa522020-10-15 08:46:24 -06009561 iod = idr_remove(&ctx->personality_idr, id);
9562 if (iod) {
9563 put_cred(iod->creds);
9564 if (refcount_dec_and_test(&iod->count))
9565 kfree(iod);
Jens Axboe071698e2020-01-28 10:04:42 -07009566 return 0;
9567 }
9568
9569 return -EINVAL;
9570}
9571
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009572static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9573 unsigned int nr_args)
9574{
9575 struct io_uring_restriction *res;
9576 size_t size;
9577 int i, ret;
9578
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009579 /* Restrictions allowed only if rings started disabled */
9580 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9581 return -EBADFD;
9582
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009583 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009584 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009585 return -EBUSY;
9586
9587 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9588 return -EINVAL;
9589
9590 size = array_size(nr_args, sizeof(*res));
9591 if (size == SIZE_MAX)
9592 return -EOVERFLOW;
9593
9594 res = memdup_user(arg, size);
9595 if (IS_ERR(res))
9596 return PTR_ERR(res);
9597
9598 ret = 0;
9599
9600 for (i = 0; i < nr_args; i++) {
9601 switch (res[i].opcode) {
9602 case IORING_RESTRICTION_REGISTER_OP:
9603 if (res[i].register_op >= IORING_REGISTER_LAST) {
9604 ret = -EINVAL;
9605 goto out;
9606 }
9607
9608 __set_bit(res[i].register_op,
9609 ctx->restrictions.register_op);
9610 break;
9611 case IORING_RESTRICTION_SQE_OP:
9612 if (res[i].sqe_op >= IORING_OP_LAST) {
9613 ret = -EINVAL;
9614 goto out;
9615 }
9616
9617 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9618 break;
9619 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9620 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9621 break;
9622 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9623 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9624 break;
9625 default:
9626 ret = -EINVAL;
9627 goto out;
9628 }
9629 }
9630
9631out:
9632 /* Reset all restrictions if an error happened */
9633 if (ret != 0)
9634 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9635 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009636 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009637
9638 kfree(res);
9639 return ret;
9640}
9641
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009642static int io_register_enable_rings(struct io_ring_ctx *ctx)
9643{
9644 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9645 return -EBADFD;
9646
9647 if (ctx->restrictions.registered)
9648 ctx->restricted = 1;
9649
9650 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9651
9652 io_sq_offload_start(ctx);
9653
9654 return 0;
9655}
9656
Jens Axboe071698e2020-01-28 10:04:42 -07009657static bool io_register_op_must_quiesce(int op)
9658{
9659 switch (op) {
9660 case IORING_UNREGISTER_FILES:
9661 case IORING_REGISTER_FILES_UPDATE:
9662 case IORING_REGISTER_PROBE:
9663 case IORING_REGISTER_PERSONALITY:
9664 case IORING_UNREGISTER_PERSONALITY:
9665 return false;
9666 default:
9667 return true;
9668 }
9669}
9670
Jens Axboeedafcce2019-01-09 09:16:05 -07009671static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9672 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009673 __releases(ctx->uring_lock)
9674 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009675{
9676 int ret;
9677
Jens Axboe35fa71a2019-04-22 10:23:23 -06009678 /*
9679 * We're inside the ring mutex, if the ref is already dying, then
9680 * someone else killed the ctx or is already going through
9681 * io_uring_register().
9682 */
9683 if (percpu_ref_is_dying(&ctx->refs))
9684 return -ENXIO;
9685
Jens Axboe071698e2020-01-28 10:04:42 -07009686 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009687 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009688
Jens Axboe05f3fb32019-12-09 11:22:50 -07009689 /*
9690 * Drop uring mutex before waiting for references to exit. If
9691 * another thread is currently inside io_uring_enter() it might
9692 * need to grab the uring_lock to make progress. If we hold it
9693 * here across the drain wait, then we can deadlock. It's safe
9694 * to drop the mutex here, since no new references will come in
9695 * after we've killed the percpu ref.
9696 */
9697 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009698 do {
9699 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9700 if (!ret)
9701 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009702 ret = io_run_task_work_sig();
9703 if (ret < 0)
9704 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009705 } while (1);
9706
Jens Axboe05f3fb32019-12-09 11:22:50 -07009707 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009708
Jens Axboec1503682020-01-08 08:26:07 -07009709 if (ret) {
9710 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009711 goto out_quiesce;
9712 }
9713 }
9714
9715 if (ctx->restricted) {
9716 if (opcode >= IORING_REGISTER_LAST) {
9717 ret = -EINVAL;
9718 goto out;
9719 }
9720
9721 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9722 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009723 goto out;
9724 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009725 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009726
9727 switch (opcode) {
9728 case IORING_REGISTER_BUFFERS:
9729 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9730 break;
9731 case IORING_UNREGISTER_BUFFERS:
9732 ret = -EINVAL;
9733 if (arg || nr_args)
9734 break;
9735 ret = io_sqe_buffer_unregister(ctx);
9736 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009737 case IORING_REGISTER_FILES:
9738 ret = io_sqe_files_register(ctx, arg, nr_args);
9739 break;
9740 case IORING_UNREGISTER_FILES:
9741 ret = -EINVAL;
9742 if (arg || nr_args)
9743 break;
9744 ret = io_sqe_files_unregister(ctx);
9745 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009746 case IORING_REGISTER_FILES_UPDATE:
9747 ret = io_sqe_files_update(ctx, arg, nr_args);
9748 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009749 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009750 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009751 ret = -EINVAL;
9752 if (nr_args != 1)
9753 break;
9754 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009755 if (ret)
9756 break;
9757 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9758 ctx->eventfd_async = 1;
9759 else
9760 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009761 break;
9762 case IORING_UNREGISTER_EVENTFD:
9763 ret = -EINVAL;
9764 if (arg || nr_args)
9765 break;
9766 ret = io_eventfd_unregister(ctx);
9767 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009768 case IORING_REGISTER_PROBE:
9769 ret = -EINVAL;
9770 if (!arg || nr_args > 256)
9771 break;
9772 ret = io_probe(ctx, arg, nr_args);
9773 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009774 case IORING_REGISTER_PERSONALITY:
9775 ret = -EINVAL;
9776 if (arg || nr_args)
9777 break;
9778 ret = io_register_personality(ctx);
9779 break;
9780 case IORING_UNREGISTER_PERSONALITY:
9781 ret = -EINVAL;
9782 if (arg)
9783 break;
9784 ret = io_unregister_personality(ctx, nr_args);
9785 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009786 case IORING_REGISTER_ENABLE_RINGS:
9787 ret = -EINVAL;
9788 if (arg || nr_args)
9789 break;
9790 ret = io_register_enable_rings(ctx);
9791 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009792 case IORING_REGISTER_RESTRICTIONS:
9793 ret = io_register_restrictions(ctx, arg, nr_args);
9794 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009795 default:
9796 ret = -EINVAL;
9797 break;
9798 }
9799
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009800out:
Jens Axboe071698e2020-01-28 10:04:42 -07009801 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009802 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009803 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009804out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009805 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009806 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009807 return ret;
9808}
9809
9810SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9811 void __user *, arg, unsigned int, nr_args)
9812{
9813 struct io_ring_ctx *ctx;
9814 long ret = -EBADF;
9815 struct fd f;
9816
9817 f = fdget(fd);
9818 if (!f.file)
9819 return -EBADF;
9820
9821 ret = -EOPNOTSUPP;
9822 if (f.file->f_op != &io_uring_fops)
9823 goto out_fput;
9824
9825 ctx = f.file->private_data;
9826
9827 mutex_lock(&ctx->uring_lock);
9828 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9829 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009830 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9831 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009832out_fput:
9833 fdput(f);
9834 return ret;
9835}
9836
Jens Axboe2b188cc2019-01-07 10:46:33 -07009837static int __init io_uring_init(void)
9838{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009839#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9840 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9841 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9842} while (0)
9843
9844#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9845 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9846 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9847 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9848 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9849 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9850 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9851 BUILD_BUG_SQE_ELEM(8, __u64, off);
9852 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9853 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009854 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009855 BUILD_BUG_SQE_ELEM(24, __u32, len);
9856 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9857 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9858 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9859 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009860 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9861 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009862 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9863 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9864 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9865 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9866 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9867 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9868 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9869 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009870 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009871 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9872 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9873 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009874 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009875
Jens Axboed3656342019-12-18 09:50:26 -07009876 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009877 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009878 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9879 return 0;
9880};
9881__initcall(io_uring_init);