blob: 695fe00bafdcd46b7206e95735c684d61c4ad612 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060084#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020086#define CREATE_TRACE_POINTS
87#include <trace/events/io_uring.h>
88
Jens Axboe2b188cc2019-01-07 10:46:33 -070089#include <uapi/linux/io_uring.h>
90
91#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060092#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070093
Daniel Xu5277dea2019-09-14 14:23:45 -070094#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060095#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060096
97/*
98 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
99 */
100#define IORING_FILE_TABLE_SHIFT 9
101#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
102#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
103#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200104#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
105 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700106
107struct io_uring {
108 u32 head ____cacheline_aligned_in_smp;
109 u32 tail ____cacheline_aligned_in_smp;
110};
111
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * This data is shared with the application through the mmap at offsets
114 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 *
116 * The offsets to the member fields are published through struct
117 * io_sqring_offsets when calling io_uring_setup.
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 /*
121 * Head and tail offsets into the ring; the offsets need to be
122 * masked to get valid indices.
123 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * The kernel controls head of the sq ring and the tail of the cq ring,
125 * and the application controls tail of the sq ring and the head of the
126 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000130 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 * ring_entries - 1)
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_ring_mask, cq_ring_mask;
134 /* Ring sizes (constant, power of 2) */
135 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 /*
137 * Number of invalid entries dropped by the kernel due to
138 * invalid index stored in array
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application (i.e. get number of "new events" by comparing to
142 * cached value).
143 *
144 * After a new SQ head value was read by the application this
145 * counter includes all submissions that were dropped reaching
146 * the new SQ head (and possibly more).
147 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000148 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200150 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 *
152 * Written by the kernel, shouldn't be modified by the
153 * application.
154 *
155 * The application needs a full memory barrier before checking
156 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200160 * Runtime CQ flags
161 *
162 * Written by the application, shouldn't be modified by the
163 * kernel.
164 */
165 u32 cq_flags;
166 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 * Number of completion events lost because the queue was full;
168 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800169 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200170 * the completion queue.
171 *
172 * Written by the kernel, shouldn't be modified by the
173 * application (i.e. get number of "new events" by comparing to
174 * cached value).
175 *
176 * As completion events come in out of order this counter is not
177 * ordered with any other data.
178 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000179 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200180 /*
181 * Ring buffer of completion events.
182 *
183 * The kernel writes completion events fresh every time they are
184 * produced, so the application is allowed to modify pending
185 * entries.
186 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000187 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700188};
189
Jens Axboeedafcce2019-01-09 09:16:05 -0700190struct io_mapped_ubuf {
191 u64 ubuf;
192 size_t len;
193 struct bio_vec *bvec;
194 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600195 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700196};
197
Jens Axboe65e19f52019-10-26 07:20:21 -0600198struct fixed_file_table {
199 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700200};
201
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202struct fixed_file_ref_node {
203 struct percpu_ref refs;
204 struct list_head node;
205 struct list_head file_list;
206 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600207 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000208 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800209};
210
Jens Axboe05f3fb32019-12-09 11:22:50 -0700211struct fixed_file_data {
212 struct fixed_file_table *table;
213 struct io_ring_ctx *ctx;
214
Pavel Begunkovb2e96852020-10-10 18:34:16 +0100215 struct fixed_file_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700217 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800218 struct list_head ref_list;
219 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700220};
221
Jens Axboe5a2e7452020-02-23 16:23:11 -0700222struct io_buffer {
223 struct list_head list;
224 __u64 addr;
225 __s32 len;
226 __u16 bid;
227};
228
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200229struct io_restriction {
230 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
231 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
232 u8 sqe_flags_allowed;
233 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200234 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200235};
236
Jens Axboe534ca6d2020-09-02 13:52:19 -0600237struct io_sq_data {
238 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600239 struct mutex lock;
240
241 /* ctx's that are using this sqd */
242 struct list_head ctx_list;
243 struct list_head ctx_new_list;
244 struct mutex ctx_lock;
245
Jens Axboe534ca6d2020-09-02 13:52:19 -0600246 struct task_struct *thread;
247 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800248
249 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600250};
251
Jens Axboe2b188cc2019-01-07 10:46:33 -0700252struct io_ring_ctx {
253 struct {
254 struct percpu_ref refs;
255 } ____cacheline_aligned_in_smp;
256
257 struct {
258 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800259 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700260 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800261 unsigned int cq_overflow_flushed: 1;
262 unsigned int drain_next: 1;
263 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200264 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000265 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700266
Hristo Venev75b28af2019-08-26 17:23:46 +0000267 /*
268 * Ring buffer of indices into array of io_uring_sqe, which is
269 * mmapped by the application using the IORING_OFF_SQES offset.
270 *
271 * This indirection could e.g. be used to assign fixed
272 * io_uring_sqe entries to operations and only submit them to
273 * the queue when needed.
274 *
275 * The kernel modifies neither the indices array nor the entries
276 * array.
277 */
278 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279 unsigned cached_sq_head;
280 unsigned sq_entries;
281 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700282 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600283 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100284 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700285 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600286
287 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600288 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700289 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290
Jens Axboead3eb2c2019-12-18 17:12:20 -0700291 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292 } ____cacheline_aligned_in_smp;
293
Hristo Venev75b28af2019-08-26 17:23:46 +0000294 struct io_rings *rings;
295
Jens Axboe2b188cc2019-01-07 10:46:33 -0700296 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600297 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600298
299 /*
300 * For SQPOLL usage - we hold a reference to the parent task, so we
301 * have access to the ->files
302 */
303 struct task_struct *sqo_task;
304
305 /* Only used for accounting purposes */
306 struct mm_struct *mm_account;
307
Dennis Zhou91d8f512020-09-16 13:41:05 -0700308#ifdef CONFIG_BLK_CGROUP
309 struct cgroup_subsys_state *sqo_blkcg_css;
310#endif
311
Jens Axboe534ca6d2020-09-02 13:52:19 -0600312 struct io_sq_data *sq_data; /* if using sq thread polling */
313
Jens Axboe90554202020-09-03 12:12:41 -0600314 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600315 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700316
Jens Axboe6b063142019-01-10 22:13:58 -0700317 /*
318 * If used, fixed file set. Writers must ensure that ->refs is dead,
319 * readers must ensure that ->refs is alive as long as the file* is
320 * used. Only updated through io_uring_register(2).
321 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700322 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700323 unsigned nr_user_files;
324
Jens Axboeedafcce2019-01-09 09:16:05 -0700325 /* if used, fixed mapped user buffers */
326 unsigned nr_user_bufs;
327 struct io_mapped_ubuf *user_bufs;
328
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329 struct user_struct *user;
330
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700331 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700332
Jens Axboe4ea33a92020-10-15 13:46:44 -0600333#ifdef CONFIG_AUDIT
334 kuid_t loginuid;
335 unsigned int sessionid;
336#endif
337
Jens Axboe0f158b42020-05-14 17:18:39 -0600338 struct completion ref_comp;
339 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700340
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700341 /* if all else fails... */
342 struct io_kiocb *fallback_req;
343
Jens Axboe206aefd2019-11-07 18:27:42 -0700344#if defined(CONFIG_UNIX)
345 struct socket *ring_sock;
346#endif
347
Jens Axboe5a2e7452020-02-23 16:23:11 -0700348 struct idr io_buffer_idr;
349
Jens Axboe071698e2020-01-28 10:04:42 -0700350 struct idr personality_idr;
351
Jens Axboe206aefd2019-11-07 18:27:42 -0700352 struct {
353 unsigned cached_cq_tail;
354 unsigned cq_entries;
355 unsigned cq_mask;
356 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500357 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700358 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700359 struct wait_queue_head cq_wait;
360 struct fasync_struct *cq_fasync;
361 struct eventfd_ctx *cq_ev_fd;
362 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363
364 struct {
365 struct mutex uring_lock;
366 wait_queue_head_t wait;
367 } ____cacheline_aligned_in_smp;
368
369 struct {
370 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700371
Jens Axboedef596e2019-01-09 08:59:42 -0700372 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300373 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700374 * io_uring instances that don't use IORING_SETUP_SQPOLL.
375 * For SQPOLL, only the single threaded io_sq_thread() will
376 * manipulate the list, hence no extra locking is needed there.
377 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300378 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700379 struct hlist_head *cancel_hash;
380 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700381 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600382
383 spinlock_t inflight_lock;
384 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700385 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600386
Jens Axboe4a38aed22020-05-14 17:21:15 -0600387 struct delayed_work file_put_work;
388 struct llist_head file_put_llist;
389
Jens Axboe85faa7b2020-04-09 18:14:00 -0600390 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200391 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700392};
393
Jens Axboe09bb8392019-03-13 12:39:28 -0600394/*
395 * First field must be the file pointer in all the
396 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
397 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700398struct io_poll_iocb {
399 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000400 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700401 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600402 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700403 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700404 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700405};
406
Pavel Begunkov018043b2020-10-27 23:17:18 +0000407struct io_poll_remove {
408 struct file *file;
409 u64 addr;
410};
411
Jens Axboeb5dba592019-12-11 14:02:38 -0700412struct io_close {
413 struct file *file;
414 struct file *put_file;
415 int fd;
416};
417
Jens Axboead8a48a2019-11-15 08:49:11 -0700418struct io_timeout_data {
419 struct io_kiocb *req;
420 struct hrtimer timer;
421 struct timespec64 ts;
422 enum hrtimer_mode mode;
423};
424
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700425struct io_accept {
426 struct file *file;
427 struct sockaddr __user *addr;
428 int __user *addr_len;
429 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600430 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700431};
432
433struct io_sync {
434 struct file *file;
435 loff_t len;
436 loff_t off;
437 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700438 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700439};
440
Jens Axboefbf23842019-12-17 18:45:56 -0700441struct io_cancel {
442 struct file *file;
443 u64 addr;
444};
445
Jens Axboeb29472e2019-12-17 18:50:29 -0700446struct io_timeout {
447 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300448 u32 off;
449 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300450 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000451 /* head of the link, used by linked timeouts only */
452 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700453};
454
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100455struct io_timeout_rem {
456 struct file *file;
457 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000458
459 /* timeout update */
460 struct timespec64 ts;
461 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100462};
463
Jens Axboe9adbd452019-12-20 08:45:55 -0700464struct io_rw {
465 /* NOTE: kiocb has the file as the first member, so don't do it here */
466 struct kiocb kiocb;
467 u64 addr;
468 u64 len;
469};
470
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700471struct io_connect {
472 struct file *file;
473 struct sockaddr __user *addr;
474 int addr_len;
475};
476
Jens Axboee47293f2019-12-20 08:58:21 -0700477struct io_sr_msg {
478 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700479 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300480 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700481 void __user *buf;
482 };
Jens Axboee47293f2019-12-20 08:58:21 -0700483 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700484 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700485 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700486 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700487};
488
Jens Axboe15b71ab2019-12-11 11:20:36 -0700489struct io_open {
490 struct file *file;
491 int dfd;
Jens Axboe944d1442020-11-13 16:48:44 -0700492 bool ignore_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700493 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700494 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600495 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700496};
497
Jens Axboe05f3fb32019-12-09 11:22:50 -0700498struct io_files_update {
499 struct file *file;
500 u64 arg;
501 u32 nr_args;
502 u32 offset;
503};
504
Jens Axboe4840e412019-12-25 22:03:45 -0700505struct io_fadvise {
506 struct file *file;
507 u64 offset;
508 u32 len;
509 u32 advice;
510};
511
Jens Axboec1ca7572019-12-25 22:18:28 -0700512struct io_madvise {
513 struct file *file;
514 u64 addr;
515 u32 len;
516 u32 advice;
517};
518
Jens Axboe3e4827b2020-01-08 15:18:09 -0700519struct io_epoll {
520 struct file *file;
521 int epfd;
522 int op;
523 int fd;
524 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700525};
526
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300527struct io_splice {
528 struct file *file_out;
529 struct file *file_in;
530 loff_t off_out;
531 loff_t off_in;
532 u64 len;
533 unsigned int flags;
534};
535
Jens Axboeddf0322d2020-02-23 16:41:33 -0700536struct io_provide_buf {
537 struct file *file;
538 __u64 addr;
539 __s32 len;
540 __u32 bgid;
541 __u16 nbufs;
542 __u16 bid;
543};
544
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700545struct io_statx {
546 struct file *file;
547 int dfd;
548 unsigned int mask;
549 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700550 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700551 struct statx __user *buffer;
552};
553
Jens Axboe36f4fa62020-09-05 11:14:22 -0600554struct io_shutdown {
555 struct file *file;
556 int how;
557};
558
Jens Axboe80a261f2020-09-28 14:23:58 -0600559struct io_rename {
560 struct file *file;
561 int old_dfd;
562 int new_dfd;
563 struct filename *oldpath;
564 struct filename *newpath;
565 int flags;
566};
567
Jens Axboe14a11432020-09-28 14:27:37 -0600568struct io_unlink {
569 struct file *file;
570 int dfd;
571 int flags;
572 struct filename *filename;
573};
574
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300575struct io_completion {
576 struct file *file;
577 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300578 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300579};
580
Jens Axboef499a022019-12-02 16:28:46 -0700581struct io_async_connect {
582 struct sockaddr_storage address;
583};
584
Jens Axboe03b12302019-12-02 18:50:25 -0700585struct io_async_msghdr {
586 struct iovec fast_iov[UIO_FASTIOV];
587 struct iovec *iov;
588 struct sockaddr __user *uaddr;
589 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700590 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700591};
592
Jens Axboef67676d2019-12-02 11:03:47 -0700593struct io_async_rw {
594 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600595 const struct iovec *free_iovec;
596 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600597 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600598 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700599};
600
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300601enum {
602 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
603 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
604 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
605 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
606 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700607 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300608
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609 REQ_F_FAIL_LINK_BIT,
610 REQ_F_INFLIGHT_BIT,
611 REQ_F_CUR_POS_BIT,
612 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300613 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300614 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300615 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700616 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700617 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600618 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800619 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100620 REQ_F_LTIMEOUT_ACTIVE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700621
622 /* not a real bit, just to check we're not overflowing the space */
623 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300624};
625
626enum {
627 /* ctx owns file */
628 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
629 /* drain existing IO first */
630 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
631 /* linked sqes */
632 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
633 /* doesn't sever on completion < 0 */
634 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
635 /* IOSQE_ASYNC */
636 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700637 /* IOSQE_BUFFER_SELECT */
638 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300639
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300640 /* fail rest of links */
641 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
642 /* on inflight list */
643 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
644 /* read/write uses file position */
645 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
646 /* must not punt to workers */
647 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100648 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300649 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300650 /* regular file */
651 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300652 /* needs cleanup */
653 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700654 /* already went through poll handler */
655 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700656 /* buffer already selected */
657 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600658 /* doesn't need file table for this request */
659 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800660 /* io_wq_work is initialized */
661 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100662 /* linked timeout is active, i.e. prepared by link's head */
663 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700664};
665
666struct async_poll {
667 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600668 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300669};
670
Jens Axboe09bb8392019-03-13 12:39:28 -0600671/*
672 * NOTE! Each of the iocb union members has the file pointer
673 * as the first entry in their struct definition. So you can
674 * access the file pointer through any of the sub-structs,
675 * or directly as just 'ki_filp' in this struct.
676 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700677struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700678 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600679 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700680 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700681 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000682 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700683 struct io_accept accept;
684 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700685 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700686 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100687 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700688 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700689 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700690 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700691 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700692 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700693 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700694 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700695 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300696 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700697 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700698 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600699 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600700 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600701 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300702 /* use only after cleaning per-op data, see io_clean_op() */
703 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700704 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700705
Jens Axboee8c2bc12020-08-15 18:44:09 -0700706 /* opcode allocated if it needs to store data for async defer */
707 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700708 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800709 /* polled IO has completed */
710 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700711
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700712 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300713 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700714
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300715 struct io_ring_ctx *ctx;
716 unsigned int flags;
717 refcount_t refs;
718 struct task_struct *task;
719 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700720
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000721 struct io_kiocb *link;
Pavel Begunkov04157672020-10-27 23:25:38 +0000722 struct percpu_ref *fixed_file_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700723
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300724 /*
725 * 1. used with ctx->iopoll_list with reads/writes
726 * 2. to track reqs with ->files (see io_op_def::file_table)
727 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300728 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300729 struct callback_head task_work;
730 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
731 struct hlist_node hash_node;
732 struct async_poll *apoll;
733 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700734};
735
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300736struct io_defer_entry {
737 struct list_head list;
738 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300739 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300740};
741
Jens Axboedef596e2019-01-09 08:59:42 -0700742#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700743
Jens Axboe013538b2020-06-22 09:29:15 -0600744struct io_comp_state {
745 unsigned int nr;
746 struct list_head list;
747 struct io_ring_ctx *ctx;
748};
749
Jens Axboe9a56a232019-01-09 09:06:50 -0700750struct io_submit_state {
751 struct blk_plug plug;
752
753 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700754 * io_kiocb alloc cache
755 */
756 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300757 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700758
Jens Axboe27926b62020-10-28 09:33:23 -0600759 bool plug_started;
760
Jens Axboe2579f912019-01-09 09:10:43 -0700761 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600762 * Batch completion logic
763 */
764 struct io_comp_state comp;
765
766 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700767 * File reference cache
768 */
769 struct file *file;
770 unsigned int fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +0000771 unsigned int file_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700772 unsigned int ios_left;
773};
774
Jens Axboed3656342019-12-18 09:50:26 -0700775struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700776 /* needs req->file assigned */
777 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600778 /* don't fail if file grab fails */
779 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700780 /* hash wq insertion if file is a regular file */
781 unsigned hash_reg_file : 1;
782 /* unbound wq insertion if file is a non-regular file */
783 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700784 /* opcode is not supported by this kernel */
785 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700786 /* set if opcode supports polled "wait" */
787 unsigned pollin : 1;
788 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700789 /* op supports buffer selection */
790 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700791 /* must always have async data allocated */
792 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600793 /* should block plug */
794 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700795 /* size of async data needed, if any */
796 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600797 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700798};
799
Jens Axboe09186822020-10-13 15:01:40 -0600800static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_NOP] = {},
802 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700803 .needs_file = 1,
804 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700805 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700806 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700807 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600808 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700809 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600810 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700811 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300812 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700813 .needs_file = 1,
814 .hash_reg_file = 1,
815 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700816 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700817 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600818 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700819 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600820 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
821 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600825 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700826 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300827 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700828 .needs_file = 1,
829 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700830 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600831 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700832 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600833 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .needs_file = 1,
837 .hash_reg_file = 1,
838 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700839 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600840 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700841 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600842 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
843 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700846 .needs_file = 1,
847 .unbound_nonreg_file = 1,
848 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300849 [IORING_OP_POLL_REMOVE] = {},
850 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700851 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600852 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700855 .needs_file = 1,
856 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700857 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700858 .needs_async_data = 1,
859 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000860 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700863 .needs_file = 1,
864 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700865 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700866 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700867 .needs_async_data = 1,
868 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000869 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700872 .needs_async_data = 1,
873 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600874 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700875 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000876 [IORING_OP_TIMEOUT_REMOVE] = {
877 /* used by timeout updates' prep() */
878 .work_flags = IO_WQ_WORK_MM,
879 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300880 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700881 .needs_file = 1,
882 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700883 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600884 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700885 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300886 [IORING_OP_ASYNC_CANCEL] = {},
887 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700888 .needs_async_data = 1,
889 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600890 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700891 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300892 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700893 .needs_file = 1,
894 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700895 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700896 .needs_async_data = 1,
897 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600898 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700899 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300900 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700901 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600902 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700903 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300904 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600905 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600906 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700907 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300908 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600909 .needs_file = 1,
910 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600911 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700912 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300913 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600914 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700915 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300916 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600917 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
918 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700919 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300920 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700921 .needs_file = 1,
922 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700923 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700924 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600925 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700926 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600927 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700928 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300929 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700930 .needs_file = 1,
931 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700932 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600933 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700934 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600935 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
936 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700937 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300938 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700939 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600940 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700941 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300942 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600943 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700944 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300945 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700946 .needs_file = 1,
947 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700948 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600949 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700950 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300951 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700952 .needs_file = 1,
953 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700954 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700955 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600956 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700957 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300958 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600959 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600960 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700961 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700962 [IORING_OP_EPOLL_CTL] = {
963 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600964 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700965 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300966 [IORING_OP_SPLICE] = {
967 .needs_file = 1,
968 .hash_reg_file = 1,
969 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600970 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700971 },
972 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700973 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300974 [IORING_OP_TEE] = {
975 .needs_file = 1,
976 .hash_reg_file = 1,
977 .unbound_nonreg_file = 1,
978 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600979 [IORING_OP_SHUTDOWN] = {
980 .needs_file = 1,
981 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600982 [IORING_OP_RENAMEAT] = {
983 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
984 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
985 },
Jens Axboe14a11432020-09-28 14:27:37 -0600986 [IORING_OP_UNLINKAT] = {
987 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
988 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
989 },
Jens Axboed3656342019-12-18 09:50:26 -0700990};
991
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700992enum io_mem_account {
993 ACCT_LOCKED,
994 ACCT_PINNED,
995};
996
Pavel Begunkov90df0852021-01-04 20:43:30 +0000997static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
998 struct task_struct *task);
999
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001000static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node);
1001static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
1002 struct io_ring_ctx *ctx);
1003
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001004static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
1005 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001006static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001007static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001008static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001009static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001010static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001011static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001012static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001013static int __io_sqe_files_update(struct io_ring_ctx *ctx,
1014 struct io_uring_files_update *ip,
1015 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001016static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001017static struct file *io_file_get(struct io_submit_state *state,
1018 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001019static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -06001020static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001021
Jens Axboeb63534c2020-06-04 11:28:00 -06001022static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1023 struct iovec **iovec, struct iov_iter *iter,
1024 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001025static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1026 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001027 struct iov_iter *iter, bool force);
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001028static void io_req_drop_files(struct io_kiocb *req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001029
1030static struct kmem_cache *req_cachep;
1031
Jens Axboe09186822020-10-13 15:01:40 -06001032static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001033
1034struct sock *io_uring_get_socket(struct file *file)
1035{
1036#if defined(CONFIG_UNIX)
1037 if (file->f_op == &io_uring_fops) {
1038 struct io_ring_ctx *ctx = file->private_data;
1039
1040 return ctx->ring_sock->sk;
1041 }
1042#endif
1043 return NULL;
1044}
1045EXPORT_SYMBOL(io_uring_get_socket);
1046
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001047#define io_for_each_link(pos, head) \
1048 for (pos = (head); pos; pos = pos->link)
1049
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001050static inline void io_clean_op(struct io_kiocb *req)
1051{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001052 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001053 __io_clean_op(req);
1054}
1055
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001056static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001057{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001058 struct io_ring_ctx *ctx = req->ctx;
1059
1060 if (!req->fixed_file_refs) {
1061 req->fixed_file_refs = &ctx->file_data->node->refs;
1062 percpu_ref_get(req->fixed_file_refs);
1063 }
1064}
1065
Pavel Begunkov08d23632020-11-06 13:00:22 +00001066static bool io_match_task(struct io_kiocb *head,
1067 struct task_struct *task,
1068 struct files_struct *files)
1069{
1070 struct io_kiocb *req;
1071
Jens Axboe84965ff2021-01-23 15:51:11 -07001072 if (task && head->task != task) {
1073 /* in terms of cancelation, always match if req task is dead */
1074 if (head->task->flags & PF_EXITING)
1075 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001076 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001077 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001078 if (!files)
1079 return true;
1080
1081 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001082 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1083 continue;
1084 if (req->file && req->file->f_op == &io_uring_fops)
1085 return true;
1086 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001087 req->work.identity->files == files)
1088 return true;
1089 }
1090 return false;
1091}
1092
Jens Axboe28cea78a2020-09-14 10:51:17 -06001093static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001094{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001095 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001096 struct mm_struct *mm = current->mm;
1097
1098 if (mm) {
1099 kthread_unuse_mm(mm);
1100 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001101 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001102 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001103 if (files) {
1104 struct nsproxy *nsproxy = current->nsproxy;
1105
1106 task_lock(current);
1107 current->files = NULL;
1108 current->nsproxy = NULL;
1109 task_unlock(current);
1110 put_files_struct(files);
1111 put_nsproxy(nsproxy);
1112 }
1113}
1114
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001115static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001116{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001117 if (current->flags & PF_EXITING)
1118 return -EFAULT;
1119
Jens Axboe28cea78a2020-09-14 10:51:17 -06001120 if (!current->files) {
1121 struct files_struct *files;
1122 struct nsproxy *nsproxy;
1123
1124 task_lock(ctx->sqo_task);
1125 files = ctx->sqo_task->files;
1126 if (!files) {
1127 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001128 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001129 }
1130 atomic_inc(&files->count);
1131 get_nsproxy(ctx->sqo_task->nsproxy);
1132 nsproxy = ctx->sqo_task->nsproxy;
1133 task_unlock(ctx->sqo_task);
1134
1135 task_lock(current);
1136 current->files = files;
1137 current->nsproxy = nsproxy;
1138 task_unlock(current);
1139 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001140 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001141}
1142
1143static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1144{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001145 struct mm_struct *mm;
1146
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001147 if (current->flags & PF_EXITING)
1148 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001149 if (current->mm)
1150 return 0;
1151
1152 /* Should never happen */
1153 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1154 return -EFAULT;
1155
1156 task_lock(ctx->sqo_task);
1157 mm = ctx->sqo_task->mm;
1158 if (unlikely(!mm || !mmget_not_zero(mm)))
1159 mm = NULL;
1160 task_unlock(ctx->sqo_task);
1161
1162 if (mm) {
1163 kthread_use_mm(mm);
1164 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001165 }
1166
Jens Axboe4b70cf92020-11-02 10:39:05 -07001167 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001168}
1169
Jens Axboe28cea78a2020-09-14 10:51:17 -06001170static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1171 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001172{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001173 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001174 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001175
1176 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001177 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001178 if (unlikely(ret))
1179 return ret;
1180 }
1181
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001182 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1183 ret = __io_sq_thread_acquire_files(ctx);
1184 if (unlikely(ret))
1185 return ret;
1186 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001187
1188 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001189}
1190
Dennis Zhou91d8f512020-09-16 13:41:05 -07001191static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1192 struct cgroup_subsys_state **cur_css)
1193
1194{
1195#ifdef CONFIG_BLK_CGROUP
1196 /* puts the old one when swapping */
1197 if (*cur_css != ctx->sqo_blkcg_css) {
1198 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1199 *cur_css = ctx->sqo_blkcg_css;
1200 }
1201#endif
1202}
1203
1204static void io_sq_thread_unassociate_blkcg(void)
1205{
1206#ifdef CONFIG_BLK_CGROUP
1207 kthread_associate_blkcg(NULL);
1208#endif
1209}
1210
Jens Axboec40f6372020-06-25 15:39:59 -06001211static inline void req_set_fail_links(struct io_kiocb *req)
1212{
1213 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1214 req->flags |= REQ_F_FAIL_LINK;
1215}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001216
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001217/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001218 * None of these are dereferenced, they are simply used to check if any of
1219 * them have changed. If we're under current and check they are still the
1220 * same, we're fine to grab references to them for actual out-of-line use.
1221 */
1222static void io_init_identity(struct io_identity *id)
1223{
1224 id->files = current->files;
1225 id->mm = current->mm;
1226#ifdef CONFIG_BLK_CGROUP
1227 rcu_read_lock();
1228 id->blkcg_css = blkcg_css();
1229 rcu_read_unlock();
1230#endif
1231 id->creds = current_cred();
1232 id->nsproxy = current->nsproxy;
1233 id->fs = current->fs;
1234 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001235#ifdef CONFIG_AUDIT
1236 id->loginuid = current->loginuid;
1237 id->sessionid = current->sessionid;
1238#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001239 refcount_set(&id->count, 1);
1240}
1241
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001242static inline void __io_req_init_async(struct io_kiocb *req)
1243{
1244 memset(&req->work, 0, sizeof(req->work));
1245 req->flags |= REQ_F_WORK_INITIALIZED;
1246}
1247
Jens Axboe1e6fa522020-10-15 08:46:24 -06001248/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001249 * Note: must call io_req_init_async() for the first time you
1250 * touch any members of io_wq_work.
1251 */
1252static inline void io_req_init_async(struct io_kiocb *req)
1253{
Jens Axboe500a3732020-10-15 17:38:03 -06001254 struct io_uring_task *tctx = current->io_uring;
1255
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001256 if (req->flags & REQ_F_WORK_INITIALIZED)
1257 return;
1258
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001259 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001260
1261 /* Grab a ref if this isn't our static identity */
1262 req->work.identity = tctx->identity;
1263 if (tctx->identity != &tctx->__identity)
1264 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001265}
1266
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001267static inline bool io_async_submit(struct io_ring_ctx *ctx)
1268{
1269 return ctx->flags & IORING_SETUP_SQPOLL;
1270}
1271
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1273{
1274 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1275
Jens Axboe0f158b42020-05-14 17:18:39 -06001276 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001277}
1278
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001279static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1280{
1281 return !req->timeout.off;
1282}
1283
Jens Axboe2b188cc2019-01-07 10:46:33 -07001284static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1285{
1286 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001287 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288
1289 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1290 if (!ctx)
1291 return NULL;
1292
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001293 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1294 if (!ctx->fallback_req)
1295 goto err;
1296
Jens Axboe78076bb2019-12-04 19:56:40 -07001297 /*
1298 * Use 5 bits less than the max cq entries, that should give us around
1299 * 32 entries per hash list if totally full and uniformly spread.
1300 */
1301 hash_bits = ilog2(p->cq_entries);
1302 hash_bits -= 5;
1303 if (hash_bits <= 0)
1304 hash_bits = 1;
1305 ctx->cancel_hash_bits = hash_bits;
1306 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1307 GFP_KERNEL);
1308 if (!ctx->cancel_hash)
1309 goto err;
1310 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1311
Roman Gushchin21482892019-05-07 10:01:48 -07001312 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001313 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1314 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315
1316 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001317 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001318 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001319 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001320 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001321 init_completion(&ctx->ref_comp);
1322 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001323 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001324 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325 mutex_init(&ctx->uring_lock);
1326 init_waitqueue_head(&ctx->wait);
1327 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001328 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001329 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001330 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001331 spin_lock_init(&ctx->inflight_lock);
1332 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001333 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1334 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001335 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001336err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001337 if (ctx->fallback_req)
1338 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001339 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001340 kfree(ctx);
1341 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342}
1343
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001344static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001345{
Jens Axboe2bc99302020-07-09 09:43:27 -06001346 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1347 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001348
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001349 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001350 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001351 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001352
Bob Liu9d858b22019-11-13 18:06:25 +08001353 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001354}
1355
Jens Axboede0617e2019-04-06 21:51:27 -06001356static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001357{
Hristo Venev75b28af2019-08-26 17:23:46 +00001358 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359
Pavel Begunkov07910152020-01-17 03:52:46 +03001360 /* order cqe stores with ring update */
1361 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001362}
1363
Jens Axboe5c3462c2020-10-15 09:02:33 -06001364static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001365{
Jens Axboe500a3732020-10-15 17:38:03 -06001366 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001367 return;
1368 if (refcount_dec_and_test(&req->work.identity->count))
1369 kfree(req->work.identity);
1370}
1371
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001372static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001373{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001374 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001375 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001376
1377 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001378
Jens Axboedfead8a2020-10-14 10:12:37 -06001379 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboe98447d62020-10-14 10:48:51 -06001380 mmdrop(req->work.identity->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001381 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001382 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001383#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001384 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Jens Axboe98447d62020-10-14 10:48:51 -06001385 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001386 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001387 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001388#endif
1389 if (req->work.flags & IO_WQ_WORK_CREDS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001390 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001391 req->work.flags &= ~IO_WQ_WORK_CREDS;
1392 }
1393 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001394 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001395
Jens Axboe98447d62020-10-14 10:48:51 -06001396 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001397 if (--fs->users)
1398 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001399 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001400 if (fs)
1401 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001402 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001403 }
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001404 if (req->flags & REQ_F_INFLIGHT)
1405 io_req_drop_files(req);
Jens Axboe51a4cc12020-08-10 10:55:56 -06001406
Jens Axboe5c3462c2020-10-15 09:02:33 -06001407 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001408}
1409
1410/*
1411 * Create a private copy of io_identity, since some fields don't match
1412 * the current context.
1413 */
1414static bool io_identity_cow(struct io_kiocb *req)
1415{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001416 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001417 const struct cred *creds = NULL;
1418 struct io_identity *id;
1419
1420 if (req->work.flags & IO_WQ_WORK_CREDS)
1421 creds = req->work.identity->creds;
1422
1423 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1424 if (unlikely(!id)) {
1425 req->work.flags |= IO_WQ_WORK_CANCEL;
1426 return false;
1427 }
1428
1429 /*
1430 * We can safely just re-init the creds we copied Either the field
1431 * matches the current one, or we haven't grabbed it yet. The only
1432 * exception is ->creds, through registered personalities, so handle
1433 * that one separately.
1434 */
1435 io_init_identity(id);
1436 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001437 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001438
1439 /* add one for this request */
1440 refcount_inc(&id->count);
1441
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001442 /* drop tctx and req identity references, if needed */
1443 if (tctx->identity != &tctx->__identity &&
1444 refcount_dec_and_test(&tctx->identity->count))
1445 kfree(tctx->identity);
1446 if (req->work.identity != &tctx->__identity &&
1447 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001448 kfree(req->work.identity);
1449
1450 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001451 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001452 return true;
1453}
1454
1455static bool io_grab_identity(struct io_kiocb *req)
1456{
1457 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001458 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001459 struct io_ring_ctx *ctx = req->ctx;
1460
Jens Axboe69228332020-10-20 14:28:41 -06001461 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1462 if (id->fsize != rlimit(RLIMIT_FSIZE))
1463 return false;
1464 req->work.flags |= IO_WQ_WORK_FSIZE;
1465 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001466#ifdef CONFIG_BLK_CGROUP
1467 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1468 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1469 rcu_read_lock();
1470 if (id->blkcg_css != blkcg_css()) {
1471 rcu_read_unlock();
1472 return false;
1473 }
1474 /*
1475 * This should be rare, either the cgroup is dying or the task
1476 * is moving cgroups. Just punt to root for the handful of ios.
1477 */
1478 if (css_tryget_online(id->blkcg_css))
1479 req->work.flags |= IO_WQ_WORK_BLKCG;
1480 rcu_read_unlock();
1481 }
1482#endif
1483 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1484 if (id->creds != current_cred())
1485 return false;
1486 get_cred(id->creds);
1487 req->work.flags |= IO_WQ_WORK_CREDS;
1488 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001489#ifdef CONFIG_AUDIT
1490 if (!uid_eq(current->loginuid, id->loginuid) ||
1491 current->sessionid != id->sessionid)
1492 return false;
1493#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001494 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1495 (def->work_flags & IO_WQ_WORK_FS)) {
1496 if (current->fs != id->fs)
1497 return false;
1498 spin_lock(&id->fs->lock);
1499 if (!id->fs->in_exec) {
1500 id->fs->users++;
1501 req->work.flags |= IO_WQ_WORK_FS;
1502 } else {
1503 req->work.flags |= IO_WQ_WORK_CANCEL;
1504 }
1505 spin_unlock(&current->fs->lock);
1506 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001507 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1508 (def->work_flags & IO_WQ_WORK_FILES) &&
1509 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1510 if (id->files != current->files ||
1511 id->nsproxy != current->nsproxy)
1512 return false;
1513 atomic_inc(&id->files->count);
1514 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001515
Jens Axboe02a13672021-01-23 15:49:31 -07001516 if (!(req->flags & REQ_F_INFLIGHT)) {
1517 req->flags |= REQ_F_INFLIGHT;
1518
1519 spin_lock_irq(&ctx->inflight_lock);
1520 list_add(&req->inflight_entry, &ctx->inflight_list);
1521 spin_unlock_irq(&ctx->inflight_lock);
1522 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001523 req->work.flags |= IO_WQ_WORK_FILES;
1524 }
Jens Axboe77788772020-12-29 10:50:46 -07001525 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1526 (def->work_flags & IO_WQ_WORK_MM)) {
1527 if (id->mm != current->mm)
1528 return false;
1529 mmgrab(id->mm);
1530 req->work.flags |= IO_WQ_WORK_MM;
1531 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001532
1533 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001534}
1535
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001536static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001537{
Jens Axboed3656342019-12-18 09:50:26 -07001538 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001539 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001540
Pavel Begunkov16d59802020-07-12 16:16:47 +03001541 io_req_init_async(req);
1542
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001543 if (req->flags & REQ_F_FORCE_ASYNC)
1544 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1545
Jens Axboed3656342019-12-18 09:50:26 -07001546 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001547 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001548 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001549 } else {
1550 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001551 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001552 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001553
Jens Axboe1e6fa522020-10-15 08:46:24 -06001554 /* if we fail grabbing identity, we must COW, regrab, and retry */
1555 if (io_grab_identity(req))
1556 return;
1557
1558 if (!io_identity_cow(req))
1559 return;
1560
1561 /* can't fail at this point */
1562 if (!io_grab_identity(req))
1563 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001564}
1565
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001566static void io_prep_async_link(struct io_kiocb *req)
1567{
1568 struct io_kiocb *cur;
1569
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001570 io_for_each_link(cur, req)
1571 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001572}
1573
Jens Axboe7271ef32020-08-10 09:55:22 -06001574static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001575{
Jackie Liua197f662019-11-08 08:09:12 -07001576 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001577 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001578
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001579 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1580 &req->work, req->flags);
1581 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001582 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001583}
1584
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001585static void io_queue_async_work(struct io_kiocb *req)
1586{
Jens Axboe7271ef32020-08-10 09:55:22 -06001587 struct io_kiocb *link;
1588
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001589 /* init ->work of the whole link before punting */
1590 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001591 link = __io_queue_async_work(req);
1592
1593 if (link)
1594 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001595}
1596
Jens Axboe5262f562019-09-17 12:26:57 -06001597static void io_kill_timeout(struct io_kiocb *req)
1598{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001599 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001600 int ret;
1601
Jens Axboee8c2bc12020-08-15 18:44:09 -07001602 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001603 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001604 atomic_set(&req->ctx->cq_timeouts,
1605 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001606 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001607 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001608 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001609 }
1610}
1611
Jens Axboe76e1b642020-09-26 15:05:03 -06001612/*
1613 * Returns true if we found and killed one or more timeouts
1614 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001615static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1616 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001617{
1618 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001619 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001620
1621 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001622 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001623 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001624 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001625 canceled++;
1626 }
Jens Axboef3606e32020-09-22 08:18:24 -06001627 }
Jens Axboe5262f562019-09-17 12:26:57 -06001628 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001629 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001630}
1631
Pavel Begunkov04518942020-05-26 20:34:05 +03001632static void __io_queue_deferred(struct io_ring_ctx *ctx)
1633{
1634 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001635 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1636 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001637 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001638
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001639 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001640 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001641 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001642 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001643 link = __io_queue_async_work(de->req);
1644 if (link) {
1645 __io_queue_linked_timeout(link);
1646 /* drop submission reference */
Pavel Begunkov216578e2020-10-13 09:44:00 +01001647 io_put_req_deferred(link, 1);
Jens Axboe7271ef32020-08-10 09:55:22 -06001648 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001649 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001650 } while (!list_empty(&ctx->defer_list));
1651}
1652
Pavel Begunkov360428f2020-05-30 14:54:17 +03001653static void io_flush_timeouts(struct io_ring_ctx *ctx)
1654{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001655 u32 seq;
1656
1657 if (list_empty(&ctx->timeout_list))
1658 return;
1659
1660 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1661
1662 do {
1663 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001664 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001665 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001666
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001667 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001668 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001669
1670 /*
1671 * Since seq can easily wrap around over time, subtract
1672 * the last seq at which timeouts were flushed before comparing.
1673 * Assuming not more than 2^31-1 events have happened since,
1674 * these subtractions won't have wrapped, so we can check if
1675 * target is in [last_seq, current_seq] by comparing the two.
1676 */
1677 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1678 events_got = seq - ctx->cq_last_tm_flush;
1679 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001680 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001681
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001682 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001683 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001684 } while (!list_empty(&ctx->timeout_list));
1685
1686 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001687}
1688
Jens Axboede0617e2019-04-06 21:51:27 -06001689static void io_commit_cqring(struct io_ring_ctx *ctx)
1690{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001691 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001692 __io_commit_cqring(ctx);
1693
Pavel Begunkov04518942020-05-26 20:34:05 +03001694 if (unlikely(!list_empty(&ctx->defer_list)))
1695 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001696}
1697
Jens Axboe90554202020-09-03 12:12:41 -06001698static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1699{
1700 struct io_rings *r = ctx->rings;
1701
1702 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1703}
1704
Jens Axboe2b188cc2019-01-07 10:46:33 -07001705static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1706{
Hristo Venev75b28af2019-08-26 17:23:46 +00001707 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 unsigned tail;
1709
1710 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001711 /*
1712 * writes to the cq entry need to come after reading head; the
1713 * control dependency is enough as we're using WRITE_ONCE to
1714 * fill the cq entry
1715 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001716 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717 return NULL;
1718
1719 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001720 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721}
1722
Jens Axboef2842ab2020-01-08 11:04:00 -07001723static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1724{
Jens Axboef0b493e2020-02-01 21:30:11 -07001725 if (!ctx->cq_ev_fd)
1726 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001727 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1728 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001729 if (!ctx->eventfd_async)
1730 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001731 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001732}
1733
Pavel Begunkove23de152020-12-17 00:24:37 +00001734static inline unsigned __io_cqring_events(struct io_ring_ctx *ctx)
1735{
1736 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1737}
1738
Jens Axboeb41e9852020-02-17 09:52:41 -07001739static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001740{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001741 /* see waitqueue_active() comment */
1742 smp_mb();
1743
Jens Axboe8c838782019-03-12 15:48:16 -06001744 if (waitqueue_active(&ctx->wait))
1745 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001746 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1747 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001748 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001749 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001750 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001751 wake_up_interruptible(&ctx->cq_wait);
1752 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1753 }
Jens Axboe8c838782019-03-12 15:48:16 -06001754}
1755
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001756static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1757{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001758 /* see waitqueue_active() comment */
1759 smp_mb();
1760
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001761 if (ctx->flags & IORING_SETUP_SQPOLL) {
1762 if (waitqueue_active(&ctx->wait))
1763 wake_up(&ctx->wait);
1764 }
1765 if (io_should_trigger_evfd(ctx))
1766 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001767 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001768 wake_up_interruptible(&ctx->cq_wait);
1769 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1770 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001771}
1772
Jens Axboec4a2ed72019-11-21 21:01:26 -07001773/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001774static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1775 struct task_struct *tsk,
1776 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001778 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001779 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001780 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001781 unsigned long flags;
Pavel Begunkov09e88402020-12-17 00:24:38 +00001782 bool all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001783 LIST_HEAD(list);
1784
Pavel Begunkove23de152020-12-17 00:24:37 +00001785 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1786 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001787
1788 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001789 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001790 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001791 continue;
1792
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001793 cqe = io_get_cqring(ctx);
1794 if (!cqe && !force)
1795 break;
1796
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001797 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001798 if (cqe) {
1799 WRITE_ONCE(cqe->user_data, req->user_data);
1800 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001801 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001802 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001803 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001804 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001805 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001806 }
1807 }
1808
Pavel Begunkov09e88402020-12-17 00:24:38 +00001809 all_flushed = list_empty(&ctx->cq_overflow_list);
1810 if (all_flushed) {
1811 clear_bit(0, &ctx->sq_check_overflow);
1812 clear_bit(0, &ctx->cq_check_overflow);
1813 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1814 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001815
Pavel Begunkov09e88402020-12-17 00:24:38 +00001816 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001817 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1818 io_cqring_ev_posted(ctx);
1819
1820 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001821 req = list_first_entry(&list, struct io_kiocb, compl.list);
1822 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001823 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001824 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001825
Pavel Begunkov09e88402020-12-17 00:24:38 +00001826 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001827}
1828
Pavel Begunkov6c503152021-01-04 20:36:36 +00001829static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1830 struct task_struct *tsk,
1831 struct files_struct *files)
1832{
1833 if (test_bit(0, &ctx->cq_check_overflow)) {
1834 /* iopoll syncs against uring_lock, not completion_lock */
1835 if (ctx->flags & IORING_SETUP_IOPOLL)
1836 mutex_lock(&ctx->uring_lock);
1837 __io_cqring_overflow_flush(ctx, force, tsk, files);
1838 if (ctx->flags & IORING_SETUP_IOPOLL)
1839 mutex_unlock(&ctx->uring_lock);
1840 }
1841}
1842
Jens Axboebcda7ba2020-02-23 16:42:51 -07001843static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001844{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001845 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001846 struct io_uring_cqe *cqe;
1847
Jens Axboe78e19bb2019-11-06 15:21:34 -07001848 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001849
Jens Axboe2b188cc2019-01-07 10:46:33 -07001850 /*
1851 * If we can't get a cq entry, userspace overflowed the
1852 * submission (by quite a lot). Increment the overflow count in
1853 * the ring.
1854 */
1855 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001856 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001857 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001858 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001859 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001860 } else if (ctx->cq_overflow_flushed ||
1861 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001862 /*
1863 * If we're in ring overflow flush mode, or in task cancel mode,
1864 * then we cannot store the request for later flushing, we need
1865 * to drop it on the floor.
1866 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001867 ctx->cached_cq_overflow++;
1868 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001869 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001870 if (list_empty(&ctx->cq_overflow_list)) {
1871 set_bit(0, &ctx->sq_check_overflow);
1872 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001873 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001874 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001875 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001876 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001877 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001878 refcount_inc(&req->refs);
1879 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001880 }
1881}
1882
Jens Axboebcda7ba2020-02-23 16:42:51 -07001883static void io_cqring_fill_event(struct io_kiocb *req, long res)
1884{
1885 __io_cqring_fill_event(req, res, 0);
1886}
1887
Jens Axboee1e16092020-06-22 09:17:17 -06001888static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001890 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891 unsigned long flags;
1892
1893 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001894 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001895 io_commit_cqring(ctx);
1896 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1897
Jens Axboe8c838782019-03-12 15:48:16 -06001898 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899}
1900
Jens Axboe229a7b62020-06-22 10:13:11 -06001901static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001902{
Jens Axboe229a7b62020-06-22 10:13:11 -06001903 struct io_ring_ctx *ctx = cs->ctx;
1904
1905 spin_lock_irq(&ctx->completion_lock);
1906 while (!list_empty(&cs->list)) {
1907 struct io_kiocb *req;
1908
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001909 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1910 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001911 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001912
1913 /*
1914 * io_free_req() doesn't care about completion_lock unless one
1915 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1916 * because of a potential deadlock with req->work.fs->lock
1917 */
1918 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1919 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001920 spin_unlock_irq(&ctx->completion_lock);
1921 io_put_req(req);
1922 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001923 } else {
1924 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001925 }
1926 }
1927 io_commit_cqring(ctx);
1928 spin_unlock_irq(&ctx->completion_lock);
1929
1930 io_cqring_ev_posted(ctx);
1931 cs->nr = 0;
1932}
1933
1934static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1935 struct io_comp_state *cs)
1936{
1937 if (!cs) {
1938 io_cqring_add_event(req, res, cflags);
1939 io_put_req(req);
1940 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001941 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001942 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001943 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001944 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001945 if (++cs->nr >= 32)
1946 io_submit_flush_completions(cs);
1947 }
Jens Axboee1e16092020-06-22 09:17:17 -06001948}
1949
1950static void io_req_complete(struct io_kiocb *req, long res)
1951{
Jens Axboe229a7b62020-06-22 10:13:11 -06001952 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001953}
1954
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001955static inline bool io_is_fallback_req(struct io_kiocb *req)
1956{
1957 return req == (struct io_kiocb *)
1958 ((unsigned long) req->ctx->fallback_req & ~1UL);
1959}
1960
1961static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1962{
1963 struct io_kiocb *req;
1964
1965 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001966 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001967 return req;
1968
1969 return NULL;
1970}
1971
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001972static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1973 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001974{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001975 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001976 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001977 size_t sz;
1978 int ret;
1979
1980 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001981 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1982
1983 /*
1984 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1985 * retry single alloc to be on the safe side.
1986 */
1987 if (unlikely(ret <= 0)) {
1988 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1989 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001990 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001991 ret = 1;
1992 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001993 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001994 }
1995
Pavel Begunkov291b2822020-09-30 22:57:01 +03001996 state->free_reqs--;
1997 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001998fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001999 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002000}
2001
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002002static inline void io_put_file(struct io_kiocb *req, struct file *file,
2003 bool fixed)
2004{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002005 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002006 fput(file);
2007}
2008
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002009static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002010{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002011 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002012
Jens Axboee8c2bc12020-08-15 18:44:09 -07002013 if (req->async_data)
2014 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002015 if (req->file)
2016 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002017 if (req->fixed_file_refs)
2018 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002019 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002020}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002021
Pavel Begunkov216578e2020-10-13 09:44:00 +01002022static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002023{
Jens Axboe0f212202020-09-13 13:09:39 -06002024 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06002025 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002026
Pavel Begunkov216578e2020-10-13 09:44:00 +01002027 io_dismantle_req(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002028
Jens Axboed8a6df12020-10-15 16:24:45 -06002029 percpu_counter_dec(&tctx->inflight);
Jens Axboefdaf0832020-10-30 09:37:30 -06002030 if (atomic_read(&tctx->in_idle))
Jens Axboe0f212202020-09-13 13:09:39 -06002031 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002032 put_task_struct(req->task);
2033
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03002034 if (likely(!io_is_fallback_req(req)))
2035 kmem_cache_free(req_cachep, req);
2036 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002037 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
2038 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002039}
2040
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002041static inline void io_remove_next_linked(struct io_kiocb *req)
2042{
2043 struct io_kiocb *nxt = req->link;
2044
2045 req->link = nxt->link;
2046 nxt->link = NULL;
2047}
2048
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002049static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002050{
Jackie Liua197f662019-11-08 08:09:12 -07002051 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002052 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002053 bool cancelled = false;
2054 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002055
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002056 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002057 link = req->link;
2058
Pavel Begunkov900fad42020-10-19 16:39:16 +01002059 /*
2060 * Can happen if a linked timeout fired and link had been like
2061 * req -> link t-out -> link t-out [-> ...]
2062 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002063 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2064 struct io_timeout_data *io = link->async_data;
2065 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002066
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002067 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002068 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002069 ret = hrtimer_try_to_cancel(&io->timer);
2070 if (ret != -1) {
2071 io_cqring_fill_event(link, -ECANCELED);
2072 io_commit_cqring(ctx);
2073 cancelled = true;
2074 }
2075 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002076 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002077 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002078
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002079 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002080 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002081 io_put_req(link);
2082 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002083}
2084
Jens Axboe4d7dd462019-11-20 13:03:52 -07002085
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002086static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002087{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002088 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002089 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002090 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002091
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002092 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002093 link = req->link;
2094 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002095
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002096 while (link) {
2097 nxt = link->link;
2098 link->link = NULL;
2099
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002100 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002101 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002102
2103 /*
2104 * It's ok to free under spinlock as they're not linked anymore,
2105 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2106 * work.fs->lock.
2107 */
2108 if (link->flags & REQ_F_WORK_INITIALIZED)
2109 io_put_req_deferred(link, 2);
2110 else
2111 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002112 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002113 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002114 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002115 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002116
Jens Axboe2665abf2019-11-05 12:40:47 -07002117 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002118}
2119
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002120static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002121{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002122 if (req->flags & REQ_F_LINK_TIMEOUT)
2123 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002124
Jens Axboe9e645e112019-05-10 16:07:28 -06002125 /*
2126 * If LINK is set, we have dependent requests in this chain. If we
2127 * didn't fail this request, queue the first one up, moving any other
2128 * dependencies to the next request. In case of failure, fail the rest
2129 * of the chain.
2130 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002131 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2132 struct io_kiocb *nxt = req->link;
2133
2134 req->link = NULL;
2135 return nxt;
2136 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002137 io_fail_links(req);
2138 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002139}
Jens Axboe2665abf2019-11-05 12:40:47 -07002140
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002141static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002142{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002143 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002144 return NULL;
2145 return __io_req_find_next(req);
2146}
2147
Jens Axboe355fb9e2020-10-22 20:19:35 -06002148static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002149{
2150 struct task_struct *tsk = req->task;
2151 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002152 enum task_work_notify_mode notify;
2153 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002154
Jens Axboe6200b0a2020-09-13 14:38:30 -06002155 if (tsk->flags & PF_EXITING)
2156 return -ESRCH;
2157
Jens Axboec2c4c832020-07-01 15:37:11 -06002158 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002159 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2160 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2161 * processing task_work. There's no reliable way to tell if TWA_RESUME
2162 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002163 */
Jens Axboe91989c72020-10-16 09:02:26 -06002164 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002165 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002166 notify = TWA_SIGNAL;
2167
Jens Axboe87c43112020-09-30 21:00:14 -06002168 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002169 if (!ret)
2170 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002171
Jens Axboec2c4c832020-07-01 15:37:11 -06002172 return ret;
2173}
2174
Jens Axboec40f6372020-06-25 15:39:59 -06002175static void __io_req_task_cancel(struct io_kiocb *req, int error)
2176{
2177 struct io_ring_ctx *ctx = req->ctx;
2178
2179 spin_lock_irq(&ctx->completion_lock);
2180 io_cqring_fill_event(req, error);
2181 io_commit_cqring(ctx);
2182 spin_unlock_irq(&ctx->completion_lock);
2183
2184 io_cqring_ev_posted(ctx);
2185 req_set_fail_links(req);
2186 io_double_put_req(req);
2187}
2188
2189static void io_req_task_cancel(struct callback_head *cb)
2190{
2191 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002192 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002193
2194 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002195 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002196}
2197
2198static void __io_req_task_submit(struct io_kiocb *req)
2199{
2200 struct io_ring_ctx *ctx = req->ctx;
2201
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002202 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002203 if (!ctx->sqo_dead &&
2204 !__io_sq_thread_acquire_mm(ctx) &&
2205 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002206 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002207 else
Jens Axboec40f6372020-06-25 15:39:59 -06002208 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002209 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002210}
2211
Jens Axboec40f6372020-06-25 15:39:59 -06002212static void io_req_task_submit(struct callback_head *cb)
2213{
2214 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002215 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002216
2217 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002218 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002219}
2220
2221static void io_req_task_queue(struct io_kiocb *req)
2222{
Jens Axboec40f6372020-06-25 15:39:59 -06002223 int ret;
2224
2225 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002226 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002227
Jens Axboe355fb9e2020-10-22 20:19:35 -06002228 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002229 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002230 struct task_struct *tsk;
2231
Jens Axboec40f6372020-06-25 15:39:59 -06002232 init_task_work(&req->task_work, io_req_task_cancel);
2233 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002234 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002235 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002236 }
Jens Axboec40f6372020-06-25 15:39:59 -06002237}
2238
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002239static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002240{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002241 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002242
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002243 if (nxt)
2244 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002245}
2246
Jens Axboe9e645e112019-05-10 16:07:28 -06002247static void io_free_req(struct io_kiocb *req)
2248{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002249 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002250 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002251}
2252
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002253struct req_batch {
2254 void *reqs[IO_IOPOLL_BATCH];
2255 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002256
2257 struct task_struct *task;
2258 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002259};
2260
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002261static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002262{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002263 rb->to_free = 0;
2264 rb->task_refs = 0;
2265 rb->task = NULL;
2266}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002267
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002268static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2269 struct req_batch *rb)
2270{
2271 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2272 percpu_ref_put_many(&ctx->refs, rb->to_free);
2273 rb->to_free = 0;
2274}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002275
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002276static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2277 struct req_batch *rb)
2278{
2279 if (rb->to_free)
2280 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002281 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002282 struct io_uring_task *tctx = rb->task->io_uring;
2283
2284 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboec93cc9e2021-01-16 11:52:11 -07002285 if (atomic_read(&tctx->in_idle))
2286 wake_up(&tctx->wait);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002287 put_task_struct_many(rb->task, rb->task_refs);
2288 rb->task = NULL;
2289 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002290}
2291
2292static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2293{
2294 if (unlikely(io_is_fallback_req(req))) {
2295 io_free_req(req);
2296 return;
2297 }
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002298 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002299
Jens Axboee3bc8e92020-09-24 08:45:57 -06002300 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002301 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002302 struct io_uring_task *tctx = rb->task->io_uring;
2303
2304 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboec93cc9e2021-01-16 11:52:11 -07002305 if (atomic_read(&tctx->in_idle))
2306 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002307 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002308 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002309 rb->task = req->task;
2310 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002311 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002312 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002313
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002314 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002315 rb->reqs[rb->to_free++] = req;
2316 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2317 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002318}
2319
Jens Axboeba816ad2019-09-28 11:36:45 -06002320/*
2321 * Drop reference to request, return next in chain (if there is one) if this
2322 * was the last reference to this request.
2323 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002324static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002325{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002326 struct io_kiocb *nxt = NULL;
2327
Jens Axboe2a44f462020-02-25 13:25:41 -07002328 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002329 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002330 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002331 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002332 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333}
2334
Jens Axboe2b188cc2019-01-07 10:46:33 -07002335static void io_put_req(struct io_kiocb *req)
2336{
Jens Axboedef596e2019-01-09 08:59:42 -07002337 if (refcount_dec_and_test(&req->refs))
2338 io_free_req(req);
2339}
2340
Pavel Begunkov216578e2020-10-13 09:44:00 +01002341static void io_put_req_deferred_cb(struct callback_head *cb)
2342{
2343 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2344
2345 io_free_req(req);
2346}
2347
2348static void io_free_req_deferred(struct io_kiocb *req)
2349{
2350 int ret;
2351
2352 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002353 ret = io_req_task_work_add(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002354 if (unlikely(ret)) {
2355 struct task_struct *tsk;
2356
2357 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002358 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002359 wake_up_process(tsk);
2360 }
2361}
2362
2363static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2364{
2365 if (refcount_sub_and_test(refs, &req->refs))
2366 io_free_req_deferred(req);
2367}
2368
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002369static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002370{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002371 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002372
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002373 /*
2374 * A ref is owned by io-wq in which context we're. So, if that's the
2375 * last one, it's safe to steal next work. False negatives are Ok,
2376 * it just will be re-punted async in io_put_work()
2377 */
2378 if (refcount_read(&req->refs) != 1)
2379 return NULL;
2380
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002381 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002382 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002383}
2384
Jens Axboe978db572019-11-14 22:39:04 -07002385static void io_double_put_req(struct io_kiocb *req)
2386{
2387 /* drop both submit and complete references */
2388 if (refcount_sub_and_test(2, &req->refs))
2389 io_free_req(req);
2390}
2391
Pavel Begunkov6c503152021-01-04 20:36:36 +00002392static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002393{
2394 /* See comment at the top of this file */
2395 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002396 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002397}
2398
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002399static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2400{
2401 struct io_rings *rings = ctx->rings;
2402
2403 /* make sure SQ entry isn't read before tail */
2404 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2405}
2406
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002407static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002408{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002409 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002410
Jens Axboebcda7ba2020-02-23 16:42:51 -07002411 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2412 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002413 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002414 kfree(kbuf);
2415 return cflags;
2416}
2417
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002418static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2419{
2420 struct io_buffer *kbuf;
2421
2422 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2423 return io_put_kbuf(req, kbuf);
2424}
2425
Jens Axboe4c6e2772020-07-01 11:29:10 -06002426static inline bool io_run_task_work(void)
2427{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002428 /*
2429 * Not safe to run on exiting task, and the task_work handling will
2430 * not add work to such a task.
2431 */
2432 if (unlikely(current->flags & PF_EXITING))
2433 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002434 if (current->task_works) {
2435 __set_current_state(TASK_RUNNING);
2436 task_work_run();
2437 return true;
2438 }
2439
2440 return false;
2441}
2442
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002443static void io_iopoll_queue(struct list_head *again)
2444{
2445 struct io_kiocb *req;
2446
2447 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002448 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2449 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002450 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002451 } while (!list_empty(again));
2452}
2453
Jens Axboedef596e2019-01-09 08:59:42 -07002454/*
2455 * Find and free completed poll iocbs
2456 */
2457static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2458 struct list_head *done)
2459{
Jens Axboe8237e042019-12-28 10:48:22 -07002460 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002461 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002462 LIST_HEAD(again);
2463
2464 /* order with ->result store in io_complete_rw_iopoll() */
2465 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002466
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002467 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002468 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002469 int cflags = 0;
2470
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002471 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002472 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002473 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002474 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002475 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002476 continue;
2477 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002478 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002479
Jens Axboebcda7ba2020-02-23 16:42:51 -07002480 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002481 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002482
2483 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002484 (*nr_events)++;
2485
Pavel Begunkovc3524382020-06-28 12:52:32 +03002486 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002487 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002488 }
Jens Axboedef596e2019-01-09 08:59:42 -07002489
Jens Axboe09bb8392019-03-13 12:39:28 -06002490 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002491 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002492 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002493
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002494 if (!list_empty(&again))
2495 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002496}
2497
Jens Axboedef596e2019-01-09 08:59:42 -07002498static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2499 long min)
2500{
2501 struct io_kiocb *req, *tmp;
2502 LIST_HEAD(done);
2503 bool spin;
2504 int ret;
2505
2506 /*
2507 * Only spin for completions if we don't have multiple devices hanging
2508 * off our complete list, and we're under the requested amount.
2509 */
2510 spin = !ctx->poll_multi_file && *nr_events < min;
2511
2512 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002513 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002514 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002515
2516 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002517 * Move completed and retryable entries to our local lists.
2518 * If we find a request that requires polling, break out
2519 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002520 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002521 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002522 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002523 continue;
2524 }
2525 if (!list_empty(&done))
2526 break;
2527
2528 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2529 if (ret < 0)
2530 break;
2531
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002532 /* iopoll may have completed current req */
2533 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002534 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002535
Jens Axboedef596e2019-01-09 08:59:42 -07002536 if (ret && spin)
2537 spin = false;
2538 ret = 0;
2539 }
2540
2541 if (!list_empty(&done))
2542 io_iopoll_complete(ctx, nr_events, &done);
2543
2544 return ret;
2545}
2546
2547/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002548 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002549 * non-spinning poll check - we'll still enter the driver poll loop, but only
2550 * as a non-spinning completion check.
2551 */
2552static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2553 long min)
2554{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002555 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002556 int ret;
2557
2558 ret = io_do_iopoll(ctx, nr_events, min);
2559 if (ret < 0)
2560 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002561 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002562 return 0;
2563 }
2564
2565 return 1;
2566}
2567
2568/*
2569 * We can't just wait for polled events to come to us, we have to actively
2570 * find and complete them.
2571 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002572static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002573{
2574 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2575 return;
2576
2577 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002578 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002579 unsigned int nr_events = 0;
2580
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002581 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002582
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002583 /* let it sleep and repeat later if can't complete a request */
2584 if (nr_events == 0)
2585 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002586 /*
2587 * Ensure we allow local-to-the-cpu processing to take place,
2588 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002589 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002590 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002591 if (need_resched()) {
2592 mutex_unlock(&ctx->uring_lock);
2593 cond_resched();
2594 mutex_lock(&ctx->uring_lock);
2595 }
Jens Axboedef596e2019-01-09 08:59:42 -07002596 }
2597 mutex_unlock(&ctx->uring_lock);
2598}
2599
Pavel Begunkov7668b922020-07-07 16:36:21 +03002600static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002601{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002602 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002603 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002604
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002605 /*
2606 * We disallow the app entering submit/complete with polling, but we
2607 * still need to lock the ring to prevent racing with polled issue
2608 * that got punted to a workqueue.
2609 */
2610 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002611 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002612 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002613 * Don't enter poll loop if we already have events pending.
2614 * If we do, we can potentially be spinning for commands that
2615 * already triggered a CQE (eg in error).
2616 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002617 if (test_bit(0, &ctx->cq_check_overflow))
2618 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2619 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002620 break;
2621
2622 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002623 * If a submit got punted to a workqueue, we can have the
2624 * application entering polling for a command before it gets
2625 * issued. That app will hold the uring_lock for the duration
2626 * of the poll right here, so we need to take a breather every
2627 * now and then to ensure that the issue has a chance to add
2628 * the poll to the issued list. Otherwise we can spin here
2629 * forever, while the workqueue is stuck trying to acquire the
2630 * very same mutex.
2631 */
2632 if (!(++iters & 7)) {
2633 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002634 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002635 mutex_lock(&ctx->uring_lock);
2636 }
2637
Pavel Begunkov7668b922020-07-07 16:36:21 +03002638 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002639 if (ret <= 0)
2640 break;
2641 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002642 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002643
Jens Axboe500f9fb2019-08-19 12:15:59 -06002644 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002645 return ret;
2646}
2647
Jens Axboe491381ce2019-10-17 09:20:46 -06002648static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002649{
Jens Axboe491381ce2019-10-17 09:20:46 -06002650 /*
2651 * Tell lockdep we inherited freeze protection from submission
2652 * thread.
2653 */
2654 if (req->flags & REQ_F_ISREG) {
2655 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002656
Jens Axboe491381ce2019-10-17 09:20:46 -06002657 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002658 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002659 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002660}
2661
Jens Axboea1d7c392020-06-22 11:09:46 -06002662static void io_complete_rw_common(struct kiocb *kiocb, long res,
2663 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002664{
Jens Axboe9adbd452019-12-20 08:45:55 -07002665 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002666 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002667
Jens Axboe491381ce2019-10-17 09:20:46 -06002668 if (kiocb->ki_flags & IOCB_WRITE)
2669 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002670
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002671 if (res != req->result)
2672 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002673 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002674 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002675 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002676}
2677
Jens Axboeb63534c2020-06-04 11:28:00 -06002678#ifdef CONFIG_BLOCK
2679static bool io_resubmit_prep(struct io_kiocb *req, int error)
2680{
2681 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2682 ssize_t ret = -ECANCELED;
2683 struct iov_iter iter;
2684 int rw;
2685
2686 if (error) {
2687 ret = error;
2688 goto end_req;
2689 }
2690
2691 switch (req->opcode) {
2692 case IORING_OP_READV:
2693 case IORING_OP_READ_FIXED:
2694 case IORING_OP_READ:
2695 rw = READ;
2696 break;
2697 case IORING_OP_WRITEV:
2698 case IORING_OP_WRITE_FIXED:
2699 case IORING_OP_WRITE:
2700 rw = WRITE;
2701 break;
2702 default:
2703 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2704 req->opcode);
2705 goto end_req;
2706 }
2707
Jens Axboee8c2bc12020-08-15 18:44:09 -07002708 if (!req->async_data) {
Jens Axboe8f3d7492020-09-14 09:28:14 -06002709 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2710 if (ret < 0)
2711 goto end_req;
2712 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2713 if (!ret)
2714 return true;
2715 kfree(iovec);
2716 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002717 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002718 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002719end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002720 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002721 return false;
2722}
Jens Axboeb63534c2020-06-04 11:28:00 -06002723#endif
2724
2725static bool io_rw_reissue(struct io_kiocb *req, long res)
2726{
2727#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002728 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002729 int ret;
2730
Jens Axboe355afae2020-09-02 09:30:31 -06002731 if (!S_ISBLK(mode) && !S_ISREG(mode))
2732 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002733 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2734 return false;
2735
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002736 lockdep_assert_held(&req->ctx->uring_lock);
2737
Jens Axboe28cea78a2020-09-14 10:51:17 -06002738 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002739
Jens Axboefdee9462020-08-27 16:46:24 -06002740 if (io_resubmit_prep(req, ret)) {
2741 refcount_inc(&req->refs);
2742 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002743 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002744 }
2745
Jens Axboeb63534c2020-06-04 11:28:00 -06002746#endif
2747 return false;
2748}
2749
Jens Axboea1d7c392020-06-22 11:09:46 -06002750static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2751 struct io_comp_state *cs)
2752{
2753 if (!io_rw_reissue(req, res))
2754 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002755}
2756
2757static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2758{
Jens Axboe9adbd452019-12-20 08:45:55 -07002759 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002760
Jens Axboea1d7c392020-06-22 11:09:46 -06002761 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002762}
2763
Jens Axboedef596e2019-01-09 08:59:42 -07002764static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2765{
Jens Axboe9adbd452019-12-20 08:45:55 -07002766 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002767
Jens Axboe491381ce2019-10-17 09:20:46 -06002768 if (kiocb->ki_flags & IOCB_WRITE)
2769 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002770
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002771 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002772 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002773
2774 WRITE_ONCE(req->result, res);
2775 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002776 smp_wmb();
2777 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002778}
2779
2780/*
2781 * After the iocb has been issued, it's safe to be found on the poll list.
2782 * Adding the kiocb to the list AFTER submission ensures that we don't
2783 * find it from a io_iopoll_getevents() thread before the issuer is done
2784 * accessing the kiocb cookie.
2785 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002786static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002787{
2788 struct io_ring_ctx *ctx = req->ctx;
2789
2790 /*
2791 * Track whether we have multiple files in our lists. This will impact
2792 * how we do polling eventually, not spinning if we're on potentially
2793 * different devices.
2794 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002795 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002796 ctx->poll_multi_file = false;
2797 } else if (!ctx->poll_multi_file) {
2798 struct io_kiocb *list_req;
2799
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002800 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002801 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002802 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002803 ctx->poll_multi_file = true;
2804 }
2805
2806 /*
2807 * For fast devices, IO may have already completed. If it has, add
2808 * it to the front so we find it first.
2809 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002810 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002811 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002812 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002813 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002814
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002815 /*
2816 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2817 * task context or in io worker task context. If current task context is
2818 * sq thread, we don't need to check whether should wake up sq thread.
2819 */
2820 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002821 wq_has_sleeper(&ctx->sq_data->wait))
2822 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002823}
2824
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002825static inline void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002826{
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002827 fput_many(state->file, state->file_refs);
2828 state->file_refs = 0;
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002829}
2830
2831static inline void io_state_file_put(struct io_submit_state *state)
2832{
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002833 if (state->file_refs)
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002834 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002835}
2836
2837/*
2838 * Get as many references to a file as we have IOs left in this submission,
2839 * assuming most submissions are for one file, or at least that each file
2840 * has more than one submission.
2841 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002842static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002843{
2844 if (!state)
2845 return fget(fd);
2846
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002847 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002848 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002849 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002850 return state->file;
2851 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002852 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002853 }
2854 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002855 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002856 return NULL;
2857
2858 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002859 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002860 return state->file;
2861}
2862
Jens Axboe4503b762020-06-01 10:00:27 -06002863static bool io_bdev_nowait(struct block_device *bdev)
2864{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002865 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002866}
2867
Jens Axboe2b188cc2019-01-07 10:46:33 -07002868/*
2869 * If we tracked the file through the SCM inflight mechanism, we could support
2870 * any file. For now, just ensure that anything potentially problematic is done
2871 * inline.
2872 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002873static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002874{
2875 umode_t mode = file_inode(file)->i_mode;
2876
Jens Axboe4503b762020-06-01 10:00:27 -06002877 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002878 if (IS_ENABLED(CONFIG_BLOCK) &&
2879 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002880 return true;
2881 return false;
2882 }
2883 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002884 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002885 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002886 if (IS_ENABLED(CONFIG_BLOCK) &&
2887 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002888 file->f_op != &io_uring_fops)
2889 return true;
2890 return false;
2891 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002892
Jens Axboec5b85622020-06-09 19:23:05 -06002893 /* any ->read/write should understand O_NONBLOCK */
2894 if (file->f_flags & O_NONBLOCK)
2895 return true;
2896
Jens Axboeaf197f52020-04-28 13:15:06 -06002897 if (!(file->f_mode & FMODE_NOWAIT))
2898 return false;
2899
2900 if (rw == READ)
2901 return file->f_op->read_iter != NULL;
2902
2903 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002904}
2905
Pavel Begunkova88fc402020-09-30 22:57:53 +03002906static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002907{
Jens Axboedef596e2019-01-09 08:59:42 -07002908 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002909 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002910 unsigned ioprio;
2911 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002912
Jens Axboe491381ce2019-10-17 09:20:46 -06002913 if (S_ISREG(file_inode(req->file)->i_mode))
2914 req->flags |= REQ_F_ISREG;
2915
Jens Axboe2b188cc2019-01-07 10:46:33 -07002916 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002917 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2918 req->flags |= REQ_F_CUR_POS;
2919 kiocb->ki_pos = req->file->f_pos;
2920 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002922 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2923 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2924 if (unlikely(ret))
2925 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926
2927 ioprio = READ_ONCE(sqe->ioprio);
2928 if (ioprio) {
2929 ret = ioprio_check_cap(ioprio);
2930 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002931 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002932
2933 kiocb->ki_ioprio = ioprio;
2934 } else
2935 kiocb->ki_ioprio = get_current_ioprio();
2936
Stefan Bühler8449eed2019-04-27 20:34:19 +02002937 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002938 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002939 req->flags |= REQ_F_NOWAIT;
2940
Jens Axboedef596e2019-01-09 08:59:42 -07002941 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002942 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2943 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002944 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002945
Jens Axboedef596e2019-01-09 08:59:42 -07002946 kiocb->ki_flags |= IOCB_HIPRI;
2947 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002948 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002949 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002950 if (kiocb->ki_flags & IOCB_HIPRI)
2951 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002952 kiocb->ki_complete = io_complete_rw;
2953 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002954
Jens Axboe3529d8c2019-12-19 18:24:38 -07002955 req->rw.addr = READ_ONCE(sqe->addr);
2956 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002957 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002958 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002959}
2960
2961static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2962{
2963 switch (ret) {
2964 case -EIOCBQUEUED:
2965 break;
2966 case -ERESTARTSYS:
2967 case -ERESTARTNOINTR:
2968 case -ERESTARTNOHAND:
2969 case -ERESTART_RESTARTBLOCK:
2970 /*
2971 * We can't just restart the syscall, since previously
2972 * submitted sqes may already be in progress. Just fail this
2973 * IO with EINTR.
2974 */
2975 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002976 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002977 default:
2978 kiocb->ki_complete(kiocb, ret, 0);
2979 }
2980}
2981
Jens Axboea1d7c392020-06-22 11:09:46 -06002982static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2983 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002984{
Jens Axboeba042912019-12-25 16:33:42 -07002985 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002986 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002987
Jens Axboe227c0c92020-08-13 11:51:40 -06002988 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002989 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002990 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002991 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002992 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002993 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002994 }
2995
Jens Axboeba042912019-12-25 16:33:42 -07002996 if (req->flags & REQ_F_CUR_POS)
2997 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002998 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002999 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06003000 else
3001 io_rw_done(kiocb, ret);
3002}
3003
Jens Axboe9adbd452019-12-20 08:45:55 -07003004static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03003005 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07003006{
Jens Axboe9adbd452019-12-20 08:45:55 -07003007 struct io_ring_ctx *ctx = req->ctx;
3008 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003009 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003010 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003011 size_t offset;
3012 u64 buf_addr;
3013
Jens Axboeedafcce2019-01-09 09:16:05 -07003014 if (unlikely(buf_index >= ctx->nr_user_bufs))
3015 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003016 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3017 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003018 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003019
3020 /* overflow */
3021 if (buf_addr + len < buf_addr)
3022 return -EFAULT;
3023 /* not inside the mapped region */
3024 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3025 return -EFAULT;
3026
3027 /*
3028 * May not be a start of buffer, set size appropriately
3029 * and advance us to the beginning.
3030 */
3031 offset = buf_addr - imu->ubuf;
3032 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003033
3034 if (offset) {
3035 /*
3036 * Don't use iov_iter_advance() here, as it's really slow for
3037 * using the latter parts of a big fixed buffer - it iterates
3038 * over each segment manually. We can cheat a bit here, because
3039 * we know that:
3040 *
3041 * 1) it's a BVEC iter, we set it up
3042 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3043 * first and last bvec
3044 *
3045 * So just find our index, and adjust the iterator afterwards.
3046 * If the offset is within the first bvec (or the whole first
3047 * bvec, just use iov_iter_advance(). This makes it easier
3048 * since we can just skip the first segment, which may not
3049 * be PAGE_SIZE aligned.
3050 */
3051 const struct bio_vec *bvec = imu->bvec;
3052
3053 if (offset <= bvec->bv_len) {
3054 iov_iter_advance(iter, offset);
3055 } else {
3056 unsigned long seg_skip;
3057
3058 /* skip first vec */
3059 offset -= bvec->bv_len;
3060 seg_skip = 1 + (offset >> PAGE_SHIFT);
3061
3062 iter->bvec = bvec + seg_skip;
3063 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003064 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003065 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003066 }
3067 }
3068
Jens Axboe5e559562019-11-13 16:12:46 -07003069 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003070}
3071
Jens Axboebcda7ba2020-02-23 16:42:51 -07003072static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3073{
3074 if (needs_lock)
3075 mutex_unlock(&ctx->uring_lock);
3076}
3077
3078static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3079{
3080 /*
3081 * "Normal" inline submissions always hold the uring_lock, since we
3082 * grab it from the system call. Same is true for the SQPOLL offload.
3083 * The only exception is when we've detached the request and issue it
3084 * from an async worker thread, grab the lock for that case.
3085 */
3086 if (needs_lock)
3087 mutex_lock(&ctx->uring_lock);
3088}
3089
3090static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3091 int bgid, struct io_buffer *kbuf,
3092 bool needs_lock)
3093{
3094 struct io_buffer *head;
3095
3096 if (req->flags & REQ_F_BUFFER_SELECTED)
3097 return kbuf;
3098
3099 io_ring_submit_lock(req->ctx, needs_lock);
3100
3101 lockdep_assert_held(&req->ctx->uring_lock);
3102
3103 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3104 if (head) {
3105 if (!list_empty(&head->list)) {
3106 kbuf = list_last_entry(&head->list, struct io_buffer,
3107 list);
3108 list_del(&kbuf->list);
3109 } else {
3110 kbuf = head;
3111 idr_remove(&req->ctx->io_buffer_idr, bgid);
3112 }
3113 if (*len > kbuf->len)
3114 *len = kbuf->len;
3115 } else {
3116 kbuf = ERR_PTR(-ENOBUFS);
3117 }
3118
3119 io_ring_submit_unlock(req->ctx, needs_lock);
3120
3121 return kbuf;
3122}
3123
Jens Axboe4d954c22020-02-27 07:31:19 -07003124static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3125 bool needs_lock)
3126{
3127 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003128 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003129
3130 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003131 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003132 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3133 if (IS_ERR(kbuf))
3134 return kbuf;
3135 req->rw.addr = (u64) (unsigned long) kbuf;
3136 req->flags |= REQ_F_BUFFER_SELECTED;
3137 return u64_to_user_ptr(kbuf->addr);
3138}
3139
3140#ifdef CONFIG_COMPAT
3141static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3142 bool needs_lock)
3143{
3144 struct compat_iovec __user *uiov;
3145 compat_ssize_t clen;
3146 void __user *buf;
3147 ssize_t len;
3148
3149 uiov = u64_to_user_ptr(req->rw.addr);
3150 if (!access_ok(uiov, sizeof(*uiov)))
3151 return -EFAULT;
3152 if (__get_user(clen, &uiov->iov_len))
3153 return -EFAULT;
3154 if (clen < 0)
3155 return -EINVAL;
3156
3157 len = clen;
3158 buf = io_rw_buffer_select(req, &len, needs_lock);
3159 if (IS_ERR(buf))
3160 return PTR_ERR(buf);
3161 iov[0].iov_base = buf;
3162 iov[0].iov_len = (compat_size_t) len;
3163 return 0;
3164}
3165#endif
3166
3167static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3168 bool needs_lock)
3169{
3170 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3171 void __user *buf;
3172 ssize_t len;
3173
3174 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3175 return -EFAULT;
3176
3177 len = iov[0].iov_len;
3178 if (len < 0)
3179 return -EINVAL;
3180 buf = io_rw_buffer_select(req, &len, needs_lock);
3181 if (IS_ERR(buf))
3182 return PTR_ERR(buf);
3183 iov[0].iov_base = buf;
3184 iov[0].iov_len = len;
3185 return 0;
3186}
3187
3188static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3189 bool needs_lock)
3190{
Jens Axboedddb3e22020-06-04 11:27:01 -06003191 if (req->flags & REQ_F_BUFFER_SELECTED) {
3192 struct io_buffer *kbuf;
3193
3194 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3195 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3196 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003197 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003198 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003199 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003200 return -EINVAL;
3201
3202#ifdef CONFIG_COMPAT
3203 if (req->ctx->compat)
3204 return io_compat_import(req, iov, needs_lock);
3205#endif
3206
3207 return __io_iov_buffer_select(req, iov, needs_lock);
3208}
3209
Pavel Begunkov2846c482020-11-07 13:16:27 +00003210static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboe8452fd02020-08-18 13:58:33 -07003211 struct iovec **iovec, struct iov_iter *iter,
3212 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003213{
Jens Axboe9adbd452019-12-20 08:45:55 -07003214 void __user *buf = u64_to_user_ptr(req->rw.addr);
3215 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003216 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003217 u8 opcode;
3218
Jens Axboed625c6e2019-12-17 19:53:05 -07003219 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003220 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003221 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003222 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003223 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003224
Jens Axboebcda7ba2020-02-23 16:42:51 -07003225 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003226 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003227 return -EINVAL;
3228
Jens Axboe3a6820f2019-12-22 15:19:35 -07003229 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003230 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003231 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003232 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003233 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003234 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003235 }
3236
Jens Axboe3a6820f2019-12-22 15:19:35 -07003237 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3238 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003239 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003240 }
3241
Jens Axboe4d954c22020-02-27 07:31:19 -07003242 if (req->flags & REQ_F_BUFFER_SELECT) {
3243 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003244 if (!ret) {
3245 ret = (*iovec)->iov_len;
3246 iov_iter_init(iter, rw, *iovec, 1, ret);
3247 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003248 *iovec = NULL;
3249 return ret;
3250 }
3251
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003252 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3253 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003254}
3255
Jens Axboe0fef9482020-08-26 10:36:20 -06003256static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3257{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003258 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003259}
3260
Jens Axboe32960612019-09-23 11:05:34 -06003261/*
3262 * For files that don't have ->read_iter() and ->write_iter(), handle them
3263 * by looping over ->read() or ->write() manually.
3264 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003265static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003266{
Jens Axboe4017eb92020-10-22 14:14:12 -06003267 struct kiocb *kiocb = &req->rw.kiocb;
3268 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003269 ssize_t ret = 0;
3270
3271 /*
3272 * Don't support polled IO through this interface, and we can't
3273 * support non-blocking either. For the latter, this just causes
3274 * the kiocb to be handled from an async context.
3275 */
3276 if (kiocb->ki_flags & IOCB_HIPRI)
3277 return -EOPNOTSUPP;
3278 if (kiocb->ki_flags & IOCB_NOWAIT)
3279 return -EAGAIN;
3280
3281 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003282 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003283 ssize_t nr;
3284
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003285 if (!iov_iter_is_bvec(iter)) {
3286 iovec = iov_iter_iovec(iter);
3287 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003288 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3289 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003290 }
3291
Jens Axboe32960612019-09-23 11:05:34 -06003292 if (rw == READ) {
3293 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003294 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003295 } else {
3296 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003297 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003298 }
3299
3300 if (nr < 0) {
3301 if (!ret)
3302 ret = nr;
3303 break;
3304 }
3305 ret += nr;
3306 if (nr != iovec.iov_len)
3307 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003308 req->rw.len -= nr;
3309 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003310 iov_iter_advance(iter, nr);
3311 }
3312
3313 return ret;
3314}
3315
Jens Axboeff6165b2020-08-13 09:47:43 -06003316static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3317 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003318{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003319 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003320
Jens Axboeff6165b2020-08-13 09:47:43 -06003321 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003322 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003323 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003324 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003325 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003326 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003327 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003328 unsigned iov_off = 0;
3329
3330 rw->iter.iov = rw->fast_iov;
3331 if (iter->iov != fast_iov) {
3332 iov_off = iter->iov - fast_iov;
3333 rw->iter.iov += iov_off;
3334 }
3335 if (rw->fast_iov != fast_iov)
3336 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003337 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003338 } else {
3339 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003340 }
3341}
3342
Jens Axboee8c2bc12020-08-15 18:44:09 -07003343static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003344{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003345 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3346 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3347 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003348}
3349
Jens Axboee8c2bc12020-08-15 18:44:09 -07003350static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003351{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003352 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003353 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003354
Jens Axboee8c2bc12020-08-15 18:44:09 -07003355 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003356}
3357
Jens Axboeff6165b2020-08-13 09:47:43 -06003358static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3359 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003360 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003361{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003362 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003363 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003364 if (!req->async_data) {
3365 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003366 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003367
Jens Axboeff6165b2020-08-13 09:47:43 -06003368 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003369 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003370 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003371}
3372
Pavel Begunkov73debe62020-09-30 22:57:54 +03003373static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003374{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003375 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003376 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003377 ssize_t ret;
3378
Pavel Begunkov2846c482020-11-07 13:16:27 +00003379 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003380 if (unlikely(ret < 0))
3381 return ret;
3382
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003383 iorw->bytes_done = 0;
3384 iorw->free_iovec = iov;
3385 if (iov)
3386 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003387 return 0;
3388}
3389
Pavel Begunkov73debe62020-09-30 22:57:54 +03003390static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003391{
3392 ssize_t ret;
3393
Pavel Begunkova88fc402020-09-30 22:57:53 +03003394 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003395 if (ret)
3396 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003397
Jens Axboe3529d8c2019-12-19 18:24:38 -07003398 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3399 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003400
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003401 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003402 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003403 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003404 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003405}
3406
Jens Axboec1dd91d2020-08-03 16:43:59 -06003407/*
3408 * This is our waitqueue callback handler, registered through lock_page_async()
3409 * when we initially tried to do the IO with the iocb armed our waitqueue.
3410 * This gets called when the page is unlocked, and we generally expect that to
3411 * happen when the page IO is completed and the page is now uptodate. This will
3412 * queue a task_work based retry of the operation, attempting to copy the data
3413 * again. If the latter fails because the page was NOT uptodate, then we will
3414 * do a thread based blocking retry of the operation. That's the unexpected
3415 * slow path.
3416 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003417static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3418 int sync, void *arg)
3419{
3420 struct wait_page_queue *wpq;
3421 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003422 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003423 int ret;
3424
3425 wpq = container_of(wait, struct wait_page_queue, wait);
3426
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003427 if (!wake_page_match(wpq, key))
3428 return 0;
3429
Hao Xuc8d317a2020-09-29 20:00:45 +08003430 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003431 list_del_init(&wait->entry);
3432
Pavel Begunkove7375122020-07-12 20:42:04 +03003433 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003434 percpu_ref_get(&req->ctx->refs);
3435
Jens Axboebcf5a062020-05-22 09:24:42 -06003436 /* submit ref gets dropped, acquire a new one */
3437 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003438 ret = io_req_task_work_add(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003439 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003440 struct task_struct *tsk;
3441
Jens Axboebcf5a062020-05-22 09:24:42 -06003442 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003443 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003444 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003445 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003446 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003447 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003448 return 1;
3449}
3450
Jens Axboec1dd91d2020-08-03 16:43:59 -06003451/*
3452 * This controls whether a given IO request should be armed for async page
3453 * based retry. If we return false here, the request is handed to the async
3454 * worker threads for retry. If we're doing buffered reads on a regular file,
3455 * we prepare a private wait_page_queue entry and retry the operation. This
3456 * will either succeed because the page is now uptodate and unlocked, or it
3457 * will register a callback when the page is unlocked at IO completion. Through
3458 * that callback, io_uring uses task_work to setup a retry of the operation.
3459 * That retry will attempt the buffered read again. The retry will generally
3460 * succeed, or in rare cases where it fails, we then fall back to using the
3461 * async worker threads for a blocking retry.
3462 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003463static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003464{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003465 struct io_async_rw *rw = req->async_data;
3466 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003467 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003468
3469 /* never retry for NOWAIT, we just complete with -EAGAIN */
3470 if (req->flags & REQ_F_NOWAIT)
3471 return false;
3472
Jens Axboe227c0c92020-08-13 11:51:40 -06003473 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003474 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003475 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003476
Jens Axboebcf5a062020-05-22 09:24:42 -06003477 /*
3478 * just use poll if we can, and don't attempt if the fs doesn't
3479 * support callback based unlocks
3480 */
3481 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3482 return false;
3483
Jens Axboe3b2a4432020-08-16 10:58:43 -07003484 wait->wait.func = io_async_buf_func;
3485 wait->wait.private = req;
3486 wait->wait.flags = 0;
3487 INIT_LIST_HEAD(&wait->wait.entry);
3488 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003489 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003490 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003491 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003492}
3493
3494static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3495{
3496 if (req->file->f_op->read_iter)
3497 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003498 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003499 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003500 else
3501 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003502}
3503
Jens Axboea1d7c392020-06-22 11:09:46 -06003504static int io_read(struct io_kiocb *req, bool force_nonblock,
3505 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003506{
3507 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003508 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003509 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003510 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003511 ssize_t io_size, ret, ret2;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003512 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003513
Pavel Begunkov2846c482020-11-07 13:16:27 +00003514 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003515 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003516 iovec = NULL;
3517 } else {
3518 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3519 if (ret < 0)
3520 return ret;
3521 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003522 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003523 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003524 ret = 0;
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)
Jens Axboe29de5f62020-02-20 09:56:08 -07003528 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003529 else
3530 kiocb->ki_flags |= IOCB_NOWAIT;
3531
Jens Axboefd6c2e42019-12-18 12:19:41 -07003532
Pavel Begunkov24c74672020-06-21 13:09:51 +03003533 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003534 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3535 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003536 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003537
Pavel Begunkov632546c2020-11-07 13:16:26 +00003538 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003539 if (unlikely(ret))
3540 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003541
Jens Axboe227c0c92020-08-13 11:51:40 -06003542 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003543
Jens Axboe227c0c92020-08-13 11:51:40 -06003544 if (!ret) {
3545 goto done;
3546 } else if (ret == -EIOCBQUEUED) {
3547 ret = 0;
3548 goto out_free;
3549 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003550 /* IOPOLL retry should happen for io-wq threads */
3551 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003552 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003553 /* no retry on NONBLOCK marked file */
3554 if (req->file->f_flags & O_NONBLOCK)
3555 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003556 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003557 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003558 ret = 0;
3559 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003560 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003561 /* make sure -ERESTARTSYS -> -EINTR is done */
3562 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003563 }
3564
3565 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003566 if (!iov_iter_count(iter) || !force_nonblock ||
Pavel Begunkov9a173342021-01-21 12:01:08 +00003567 (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG))
Jens Axboe227c0c92020-08-13 11:51:40 -06003568 goto done;
3569
3570 io_size -= ret;
3571copy_iov:
3572 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3573 if (ret2) {
3574 ret = ret2;
3575 goto out_free;
3576 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003577 if (no_async)
3578 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003579 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003580 /* it's copied and will be cleaned with ->io */
3581 iovec = NULL;
3582 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003583 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003584retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003585 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003586 /* if we can retry, do so with the callbacks armed */
3587 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003588 kiocb->ki_flags &= ~IOCB_WAITQ;
3589 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003590 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003591
3592 /*
3593 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3594 * get -EIOCBQUEUED, then we'll get a notification when the desired
3595 * page gets unlocked. We can also get a partial read here, and if we
3596 * do, then just retry at the new offset.
3597 */
3598 ret = io_iter_do_read(req, iter);
3599 if (ret == -EIOCBQUEUED) {
3600 ret = 0;
3601 goto out_free;
3602 } else if (ret > 0 && ret < io_size) {
3603 /* we got some bytes, but not all. retry. */
3604 goto retry;
3605 }
3606done:
3607 kiocb_done(kiocb, ret, cs);
3608 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003609out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003610 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003611 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003612 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003613 return ret;
3614}
3615
Pavel Begunkov73debe62020-09-30 22:57:54 +03003616static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003617{
3618 ssize_t ret;
3619
Pavel Begunkova88fc402020-09-30 22:57:53 +03003620 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003621 if (ret)
3622 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003623
Jens Axboe3529d8c2019-12-19 18:24:38 -07003624 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3625 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003626
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003627 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003628 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003629 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003630 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003631}
3632
Jens Axboea1d7c392020-06-22 11:09:46 -06003633static int io_write(struct io_kiocb *req, bool force_nonblock,
3634 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003635{
3636 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003637 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003638 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003639 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003640 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003641
Pavel Begunkov2846c482020-11-07 13:16:27 +00003642 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003643 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003644 iovec = NULL;
3645 } else {
3646 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3647 if (ret < 0)
3648 return ret;
3649 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003650 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003651 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003652
Jens Axboefd6c2e42019-12-18 12:19:41 -07003653 /* Ensure we clear previously set non-block flag */
3654 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003655 kiocb->ki_flags &= ~IOCB_NOWAIT;
3656 else
3657 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003658
Pavel Begunkov24c74672020-06-21 13:09:51 +03003659 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003660 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003661 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003662
Jens Axboe10d59342019-12-09 20:16:22 -07003663 /* file path doesn't support NOWAIT for non-direct_IO */
3664 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3665 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003666 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003667
Pavel Begunkov632546c2020-11-07 13:16:26 +00003668 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003669 if (unlikely(ret))
3670 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003671
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003672 /*
3673 * Open-code file_start_write here to grab freeze protection,
3674 * which will be released by another thread in
3675 * io_complete_rw(). Fool lockdep by telling it the lock got
3676 * released so that it doesn't complain about the held lock when
3677 * we return to userspace.
3678 */
3679 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003680 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003681 __sb_writers_release(file_inode(req->file)->i_sb,
3682 SB_FREEZE_WRITE);
3683 }
3684 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003685
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003686 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003687 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003688 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003689 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003690 else
3691 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003692
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003693 /*
3694 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3695 * retry them without IOCB_NOWAIT.
3696 */
3697 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3698 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003699 /* no retry on NONBLOCK marked file */
3700 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3701 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003702 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003703 /* IOPOLL retry should happen for io-wq threads */
3704 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3705 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003706done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003707 kiocb_done(kiocb, ret2, cs);
3708 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003709copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003710 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003711 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003712 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003713 if (!ret)
3714 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003715 }
Jens Axboe31b51512019-01-18 22:56:34 -07003716out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003717 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003718 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003719 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003720 return ret;
3721}
3722
Jens Axboe80a261f2020-09-28 14:23:58 -06003723static int io_renameat_prep(struct io_kiocb *req,
3724 const struct io_uring_sqe *sqe)
3725{
3726 struct io_rename *ren = &req->rename;
3727 const char __user *oldf, *newf;
3728
3729 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3730 return -EBADF;
3731
3732 ren->old_dfd = READ_ONCE(sqe->fd);
3733 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3734 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3735 ren->new_dfd = READ_ONCE(sqe->len);
3736 ren->flags = READ_ONCE(sqe->rename_flags);
3737
3738 ren->oldpath = getname(oldf);
3739 if (IS_ERR(ren->oldpath))
3740 return PTR_ERR(ren->oldpath);
3741
3742 ren->newpath = getname(newf);
3743 if (IS_ERR(ren->newpath)) {
3744 putname(ren->oldpath);
3745 return PTR_ERR(ren->newpath);
3746 }
3747
3748 req->flags |= REQ_F_NEED_CLEANUP;
3749 return 0;
3750}
3751
3752static int io_renameat(struct io_kiocb *req, bool force_nonblock)
3753{
3754 struct io_rename *ren = &req->rename;
3755 int ret;
3756
3757 if (force_nonblock)
3758 return -EAGAIN;
3759
3760 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3761 ren->newpath, ren->flags);
3762
3763 req->flags &= ~REQ_F_NEED_CLEANUP;
3764 if (ret < 0)
3765 req_set_fail_links(req);
3766 io_req_complete(req, ret);
3767 return 0;
3768}
3769
Jens Axboe14a11432020-09-28 14:27:37 -06003770static int io_unlinkat_prep(struct io_kiocb *req,
3771 const struct io_uring_sqe *sqe)
3772{
3773 struct io_unlink *un = &req->unlink;
3774 const char __user *fname;
3775
3776 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3777 return -EBADF;
3778
3779 un->dfd = READ_ONCE(sqe->fd);
3780
3781 un->flags = READ_ONCE(sqe->unlink_flags);
3782 if (un->flags & ~AT_REMOVEDIR)
3783 return -EINVAL;
3784
3785 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3786 un->filename = getname(fname);
3787 if (IS_ERR(un->filename))
3788 return PTR_ERR(un->filename);
3789
3790 req->flags |= REQ_F_NEED_CLEANUP;
3791 return 0;
3792}
3793
3794static int io_unlinkat(struct io_kiocb *req, bool force_nonblock)
3795{
3796 struct io_unlink *un = &req->unlink;
3797 int ret;
3798
3799 if (force_nonblock)
3800 return -EAGAIN;
3801
3802 if (un->flags & AT_REMOVEDIR)
3803 ret = do_rmdir(un->dfd, un->filename);
3804 else
3805 ret = do_unlinkat(un->dfd, un->filename);
3806
3807 req->flags &= ~REQ_F_NEED_CLEANUP;
3808 if (ret < 0)
3809 req_set_fail_links(req);
3810 io_req_complete(req, ret);
3811 return 0;
3812}
3813
Jens Axboe36f4fa62020-09-05 11:14:22 -06003814static int io_shutdown_prep(struct io_kiocb *req,
3815 const struct io_uring_sqe *sqe)
3816{
3817#if defined(CONFIG_NET)
3818 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3819 return -EINVAL;
3820 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3821 sqe->buf_index)
3822 return -EINVAL;
3823
3824 req->shutdown.how = READ_ONCE(sqe->len);
3825 return 0;
3826#else
3827 return -EOPNOTSUPP;
3828#endif
3829}
3830
3831static int io_shutdown(struct io_kiocb *req, bool force_nonblock)
3832{
3833#if defined(CONFIG_NET)
3834 struct socket *sock;
3835 int ret;
3836
3837 if (force_nonblock)
3838 return -EAGAIN;
3839
Linus Torvalds48aba792020-12-16 12:44:05 -08003840 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003841 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003842 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003843
3844 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003845 if (ret < 0)
3846 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003847 io_req_complete(req, ret);
3848 return 0;
3849#else
3850 return -EOPNOTSUPP;
3851#endif
3852}
3853
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003854static int __io_splice_prep(struct io_kiocb *req,
3855 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003856{
3857 struct io_splice* sp = &req->splice;
3858 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003859
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003860 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3861 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003862
3863 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003864 sp->len = READ_ONCE(sqe->len);
3865 sp->flags = READ_ONCE(sqe->splice_flags);
3866
3867 if (unlikely(sp->flags & ~valid_flags))
3868 return -EINVAL;
3869
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003870 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3871 (sp->flags & SPLICE_F_FD_IN_FIXED));
3872 if (!sp->file_in)
3873 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003874 req->flags |= REQ_F_NEED_CLEANUP;
3875
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003876 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3877 /*
3878 * Splice operation will be punted aync, and here need to
3879 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3880 */
3881 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003882 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003883 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003884
3885 return 0;
3886}
3887
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003888static int io_tee_prep(struct io_kiocb *req,
3889 const struct io_uring_sqe *sqe)
3890{
3891 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3892 return -EINVAL;
3893 return __io_splice_prep(req, sqe);
3894}
3895
3896static int io_tee(struct io_kiocb *req, bool force_nonblock)
3897{
3898 struct io_splice *sp = &req->splice;
3899 struct file *in = sp->file_in;
3900 struct file *out = sp->file_out;
3901 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3902 long ret = 0;
3903
3904 if (force_nonblock)
3905 return -EAGAIN;
3906 if (sp->len)
3907 ret = do_tee(in, out, sp->len, flags);
3908
3909 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3910 req->flags &= ~REQ_F_NEED_CLEANUP;
3911
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003912 if (ret != sp->len)
3913 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003914 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003915 return 0;
3916}
3917
3918static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3919{
3920 struct io_splice* sp = &req->splice;
3921
3922 sp->off_in = READ_ONCE(sqe->splice_off_in);
3923 sp->off_out = READ_ONCE(sqe->off);
3924 return __io_splice_prep(req, sqe);
3925}
3926
Pavel Begunkov014db002020-03-03 21:33:12 +03003927static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003928{
3929 struct io_splice *sp = &req->splice;
3930 struct file *in = sp->file_in;
3931 struct file *out = sp->file_out;
3932 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3933 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003934 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003935
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003936 if (force_nonblock)
3937 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003938
3939 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3940 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003941
Jens Axboe948a7742020-05-17 14:21:38 -06003942 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003943 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003944
3945 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3946 req->flags &= ~REQ_F_NEED_CLEANUP;
3947
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003948 if (ret != sp->len)
3949 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003950 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003951 return 0;
3952}
3953
Jens Axboe2b188cc2019-01-07 10:46:33 -07003954/*
3955 * IORING_OP_NOP just posts a completion event, nothing else.
3956 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003957static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003958{
3959 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003960
Jens Axboedef596e2019-01-09 08:59:42 -07003961 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3962 return -EINVAL;
3963
Jens Axboe229a7b62020-06-22 10:13:11 -06003964 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003965 return 0;
3966}
3967
Jens Axboe3529d8c2019-12-19 18:24:38 -07003968static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003969{
Jens Axboe6b063142019-01-10 22:13:58 -07003970 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003971
Jens Axboe09bb8392019-03-13 12:39:28 -06003972 if (!req->file)
3973 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003974
Jens Axboe6b063142019-01-10 22:13:58 -07003975 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003976 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003977 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003978 return -EINVAL;
3979
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003980 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3981 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3982 return -EINVAL;
3983
3984 req->sync.off = READ_ONCE(sqe->off);
3985 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003986 return 0;
3987}
3988
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003989static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003990{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003991 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003992 int ret;
3993
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003994 /* fsync always requires a blocking context */
3995 if (force_nonblock)
3996 return -EAGAIN;
3997
Jens Axboe9adbd452019-12-20 08:45:55 -07003998 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003999 end > 0 ? end : LLONG_MAX,
4000 req->sync.flags & IORING_FSYNC_DATASYNC);
4001 if (ret < 0)
4002 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004003 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004004 return 0;
4005}
4006
Jens Axboed63d1b52019-12-10 10:38:56 -07004007static int io_fallocate_prep(struct io_kiocb *req,
4008 const struct io_uring_sqe *sqe)
4009{
4010 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
4011 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004012 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4013 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004014
4015 req->sync.off = READ_ONCE(sqe->off);
4016 req->sync.len = READ_ONCE(sqe->addr);
4017 req->sync.mode = READ_ONCE(sqe->len);
4018 return 0;
4019}
4020
Pavel Begunkov014db002020-03-03 21:33:12 +03004021static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07004022{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004023 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004024
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004025 /* fallocate always requiring blocking context */
4026 if (force_nonblock)
4027 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004028 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4029 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004030 if (ret < 0)
4031 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004032 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004033 return 0;
4034}
4035
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004036static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004037{
Jens Axboef8748882020-01-08 17:47:02 -07004038 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004039 int ret;
4040
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004041 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004042 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004043 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004044 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004045
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004046 /* open.how should be already initialised */
4047 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004048 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004049
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004050 req->open.dfd = READ_ONCE(sqe->fd);
4051 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004052 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004053 if (IS_ERR(req->open.filename)) {
4054 ret = PTR_ERR(req->open.filename);
4055 req->open.filename = NULL;
4056 return ret;
4057 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004058 req->open.nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe944d1442020-11-13 16:48:44 -07004059 req->open.ignore_nonblock = false;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004060 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004061 return 0;
4062}
4063
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004064static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4065{
4066 u64 flags, mode;
4067
Jens Axboe14587a462020-09-05 11:36:08 -06004068 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004069 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004070 mode = READ_ONCE(sqe->len);
4071 flags = READ_ONCE(sqe->open_flags);
4072 req->open.how = build_open_how(flags, mode);
4073 return __io_openat_prep(req, sqe);
4074}
4075
Jens Axboecebdb982020-01-08 17:59:24 -07004076static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4077{
4078 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004079 size_t len;
4080 int ret;
4081
Jens Axboe14587a462020-09-05 11:36:08 -06004082 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004083 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004084 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4085 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004086 if (len < OPEN_HOW_SIZE_VER0)
4087 return -EINVAL;
4088
4089 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4090 len);
4091 if (ret)
4092 return ret;
4093
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004094 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004095}
4096
Pavel Begunkov014db002020-03-03 21:33:12 +03004097static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004098{
4099 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004100 struct file *file;
4101 int ret;
4102
Jens Axboe944d1442020-11-13 16:48:44 -07004103 if (force_nonblock && !req->open.ignore_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004104 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004105
Jens Axboecebdb982020-01-08 17:59:24 -07004106 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004107 if (ret)
4108 goto err;
4109
Jens Axboe4022e7a2020-03-19 19:23:18 -06004110 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004111 if (ret < 0)
4112 goto err;
4113
4114 file = do_filp_open(req->open.dfd, req->open.filename, &op);
4115 if (IS_ERR(file)) {
4116 put_unused_fd(ret);
4117 ret = PTR_ERR(file);
Jens Axboe944d1442020-11-13 16:48:44 -07004118 /*
4119 * A work-around to ensure that /proc/self works that way
4120 * that it should - if we get -EOPNOTSUPP back, then assume
4121 * that proc_self_get_link() failed us because we're in async
4122 * context. We should be safe to retry this from the task
4123 * itself with force_nonblock == false set, as it should not
4124 * block on lookup. Would be nice to know this upfront and
4125 * avoid the async dance, but doesn't seem feasible.
4126 */
4127 if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) {
4128 req->open.ignore_nonblock = true;
4129 refcount_inc(&req->refs);
4130 io_req_task_queue(req);
4131 return 0;
4132 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004133 } else {
4134 fsnotify_open(file);
4135 fd_install(ret, file);
4136 }
4137err:
4138 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004139 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004140 if (ret < 0)
4141 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004142 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004143 return 0;
4144}
4145
Pavel Begunkov014db002020-03-03 21:33:12 +03004146static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07004147{
Pavel Begunkov014db002020-03-03 21:33:12 +03004148 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07004149}
4150
Jens Axboe067524e2020-03-02 16:32:28 -07004151static int io_remove_buffers_prep(struct io_kiocb *req,
4152 const struct io_uring_sqe *sqe)
4153{
4154 struct io_provide_buf *p = &req->pbuf;
4155 u64 tmp;
4156
4157 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4158 return -EINVAL;
4159
4160 tmp = READ_ONCE(sqe->fd);
4161 if (!tmp || tmp > USHRT_MAX)
4162 return -EINVAL;
4163
4164 memset(p, 0, sizeof(*p));
4165 p->nbufs = tmp;
4166 p->bgid = READ_ONCE(sqe->buf_group);
4167 return 0;
4168}
4169
4170static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4171 int bgid, unsigned nbufs)
4172{
4173 unsigned i = 0;
4174
4175 /* shouldn't happen */
4176 if (!nbufs)
4177 return 0;
4178
4179 /* the head kbuf is the list itself */
4180 while (!list_empty(&buf->list)) {
4181 struct io_buffer *nxt;
4182
4183 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4184 list_del(&nxt->list);
4185 kfree(nxt);
4186 if (++i == nbufs)
4187 return i;
4188 }
4189 i++;
4190 kfree(buf);
4191 idr_remove(&ctx->io_buffer_idr, bgid);
4192
4193 return i;
4194}
4195
Jens Axboe229a7b62020-06-22 10:13:11 -06004196static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
4197 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07004198{
4199 struct io_provide_buf *p = &req->pbuf;
4200 struct io_ring_ctx *ctx = req->ctx;
4201 struct io_buffer *head;
4202 int ret = 0;
4203
4204 io_ring_submit_lock(ctx, !force_nonblock);
4205
4206 lockdep_assert_held(&ctx->uring_lock);
4207
4208 ret = -ENOENT;
4209 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4210 if (head)
4211 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004212 if (ret < 0)
4213 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004214
4215 /* need to hold the lock to complete IOPOLL requests */
4216 if (ctx->flags & IORING_SETUP_IOPOLL) {
4217 __io_req_complete(req, ret, 0, cs);
4218 io_ring_submit_unlock(ctx, !force_nonblock);
4219 } else {
4220 io_ring_submit_unlock(ctx, !force_nonblock);
4221 __io_req_complete(req, ret, 0, cs);
4222 }
Jens Axboe067524e2020-03-02 16:32:28 -07004223 return 0;
4224}
4225
Jens Axboeddf0322d2020-02-23 16:41:33 -07004226static int io_provide_buffers_prep(struct io_kiocb *req,
4227 const struct io_uring_sqe *sqe)
4228{
4229 struct io_provide_buf *p = &req->pbuf;
4230 u64 tmp;
4231
4232 if (sqe->ioprio || sqe->rw_flags)
4233 return -EINVAL;
4234
4235 tmp = READ_ONCE(sqe->fd);
4236 if (!tmp || tmp > USHRT_MAX)
4237 return -E2BIG;
4238 p->nbufs = tmp;
4239 p->addr = READ_ONCE(sqe->addr);
4240 p->len = READ_ONCE(sqe->len);
4241
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004242 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004243 return -EFAULT;
4244
4245 p->bgid = READ_ONCE(sqe->buf_group);
4246 tmp = READ_ONCE(sqe->off);
4247 if (tmp > USHRT_MAX)
4248 return -E2BIG;
4249 p->bid = tmp;
4250 return 0;
4251}
4252
4253static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4254{
4255 struct io_buffer *buf;
4256 u64 addr = pbuf->addr;
4257 int i, bid = pbuf->bid;
4258
4259 for (i = 0; i < pbuf->nbufs; i++) {
4260 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4261 if (!buf)
4262 break;
4263
4264 buf->addr = addr;
4265 buf->len = pbuf->len;
4266 buf->bid = bid;
4267 addr += pbuf->len;
4268 bid++;
4269 if (!*head) {
4270 INIT_LIST_HEAD(&buf->list);
4271 *head = buf;
4272 } else {
4273 list_add_tail(&buf->list, &(*head)->list);
4274 }
4275 }
4276
4277 return i ? i : -ENOMEM;
4278}
4279
Jens Axboe229a7b62020-06-22 10:13:11 -06004280static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4281 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004282{
4283 struct io_provide_buf *p = &req->pbuf;
4284 struct io_ring_ctx *ctx = req->ctx;
4285 struct io_buffer *head, *list;
4286 int ret = 0;
4287
4288 io_ring_submit_lock(ctx, !force_nonblock);
4289
4290 lockdep_assert_held(&ctx->uring_lock);
4291
4292 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4293
4294 ret = io_add_buffers(p, &head);
4295 if (ret < 0)
4296 goto out;
4297
4298 if (!list) {
4299 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4300 GFP_KERNEL);
4301 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004302 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004303 goto out;
4304 }
4305 }
4306out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004307 if (ret < 0)
4308 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004309
4310 /* need to hold the lock to complete IOPOLL requests */
4311 if (ctx->flags & IORING_SETUP_IOPOLL) {
4312 __io_req_complete(req, ret, 0, cs);
4313 io_ring_submit_unlock(ctx, !force_nonblock);
4314 } else {
4315 io_ring_submit_unlock(ctx, !force_nonblock);
4316 __io_req_complete(req, ret, 0, cs);
4317 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004318 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004319}
4320
Jens Axboe3e4827b2020-01-08 15:18:09 -07004321static int io_epoll_ctl_prep(struct io_kiocb *req,
4322 const struct io_uring_sqe *sqe)
4323{
4324#if defined(CONFIG_EPOLL)
4325 if (sqe->ioprio || sqe->buf_index)
4326 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004327 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004328 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004329
4330 req->epoll.epfd = READ_ONCE(sqe->fd);
4331 req->epoll.op = READ_ONCE(sqe->len);
4332 req->epoll.fd = READ_ONCE(sqe->off);
4333
4334 if (ep_op_has_event(req->epoll.op)) {
4335 struct epoll_event __user *ev;
4336
4337 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4338 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4339 return -EFAULT;
4340 }
4341
4342 return 0;
4343#else
4344 return -EOPNOTSUPP;
4345#endif
4346}
4347
Jens Axboe229a7b62020-06-22 10:13:11 -06004348static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4349 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004350{
4351#if defined(CONFIG_EPOLL)
4352 struct io_epoll *ie = &req->epoll;
4353 int ret;
4354
4355 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4356 if (force_nonblock && ret == -EAGAIN)
4357 return -EAGAIN;
4358
4359 if (ret < 0)
4360 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004361 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004362 return 0;
4363#else
4364 return -EOPNOTSUPP;
4365#endif
4366}
4367
Jens Axboec1ca7572019-12-25 22:18:28 -07004368static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4369{
4370#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4371 if (sqe->ioprio || sqe->buf_index || sqe->off)
4372 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004373 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4374 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004375
4376 req->madvise.addr = READ_ONCE(sqe->addr);
4377 req->madvise.len = READ_ONCE(sqe->len);
4378 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4379 return 0;
4380#else
4381 return -EOPNOTSUPP;
4382#endif
4383}
4384
Pavel Begunkov014db002020-03-03 21:33:12 +03004385static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004386{
4387#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4388 struct io_madvise *ma = &req->madvise;
4389 int ret;
4390
4391 if (force_nonblock)
4392 return -EAGAIN;
4393
Minchan Kim0726b012020-10-17 16:14:50 -07004394 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004395 if (ret < 0)
4396 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004397 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004398 return 0;
4399#else
4400 return -EOPNOTSUPP;
4401#endif
4402}
4403
Jens Axboe4840e412019-12-25 22:03:45 -07004404static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4405{
4406 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4407 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004408 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4409 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004410
4411 req->fadvise.offset = READ_ONCE(sqe->off);
4412 req->fadvise.len = READ_ONCE(sqe->len);
4413 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4414 return 0;
4415}
4416
Pavel Begunkov014db002020-03-03 21:33:12 +03004417static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004418{
4419 struct io_fadvise *fa = &req->fadvise;
4420 int ret;
4421
Jens Axboe3e694262020-02-01 09:22:49 -07004422 if (force_nonblock) {
4423 switch (fa->advice) {
4424 case POSIX_FADV_NORMAL:
4425 case POSIX_FADV_RANDOM:
4426 case POSIX_FADV_SEQUENTIAL:
4427 break;
4428 default:
4429 return -EAGAIN;
4430 }
4431 }
Jens Axboe4840e412019-12-25 22:03:45 -07004432
4433 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4434 if (ret < 0)
4435 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004436 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004437 return 0;
4438}
4439
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004440static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4441{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004442 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004443 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004444 if (sqe->ioprio || sqe->buf_index)
4445 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004446 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004447 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004448
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004449 req->statx.dfd = READ_ONCE(sqe->fd);
4450 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004451 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004452 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4453 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004454
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004455 return 0;
4456}
4457
Pavel Begunkov014db002020-03-03 21:33:12 +03004458static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004459{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004460 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004461 int ret;
4462
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004463 if (force_nonblock) {
4464 /* only need file table for an actual valid fd */
4465 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4466 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004467 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004468 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004469
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004470 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4471 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004472
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004473 if (ret < 0)
4474 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004475 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004476 return 0;
4477}
4478
Jens Axboeb5dba592019-12-11 14:02:38 -07004479static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4480{
4481 /*
4482 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004483 * leave the 'file' in an undeterminate state, and here need to modify
4484 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004485 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004486 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004487
Jens Axboe14587a462020-09-05 11:36:08 -06004488 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004489 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004490 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4491 sqe->rw_flags || sqe->buf_index)
4492 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004493 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004494 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004495
4496 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004497 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004498 return -EBADF;
4499
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004500 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004501 return 0;
4502}
4503
Jens Axboe229a7b62020-06-22 10:13:11 -06004504static int io_close(struct io_kiocb *req, bool force_nonblock,
4505 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004506{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004507 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004508 int ret;
4509
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004510 /* might be already done during nonblock submission */
4511 if (!close->put_file) {
Eric W. Biederman9fe83c42020-11-20 17:14:40 -06004512 ret = close_fd_get_file(close->fd, &close->put_file);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004513 if (ret < 0)
4514 return (ret == -ENOENT) ? -EBADF : ret;
4515 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004516
4517 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004518 if (close->put_file->f_op->flush && force_nonblock) {
Jens Axboe607ec892021-01-19 10:10:54 -07004519 /* not safe to cancel at this point */
4520 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004521 /* was never set, but play safe */
4522 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004523 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004524 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004525 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004526 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004527
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004528 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004529 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004530 if (ret < 0)
4531 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004532 fput(close->put_file);
4533 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004534 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004535 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004536}
4537
Jens Axboe3529d8c2019-12-19 18:24:38 -07004538static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004539{
4540 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004541
4542 if (!req->file)
4543 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004544
4545 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4546 return -EINVAL;
4547 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4548 return -EINVAL;
4549
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004550 req->sync.off = READ_ONCE(sqe->off);
4551 req->sync.len = READ_ONCE(sqe->len);
4552 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004553 return 0;
4554}
4555
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004556static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004557{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004558 int ret;
4559
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004560 /* sync_file_range always requires a blocking context */
4561 if (force_nonblock)
4562 return -EAGAIN;
4563
Jens Axboe9adbd452019-12-20 08:45:55 -07004564 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004565 req->sync.flags);
4566 if (ret < 0)
4567 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004568 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004569 return 0;
4570}
4571
YueHaibing469956e2020-03-04 15:53:52 +08004572#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004573static int io_setup_async_msg(struct io_kiocb *req,
4574 struct io_async_msghdr *kmsg)
4575{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004576 struct io_async_msghdr *async_msg = req->async_data;
4577
4578 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004579 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004580 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004581 if (kmsg->iov != kmsg->fast_iov)
4582 kfree(kmsg->iov);
4583 return -ENOMEM;
4584 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004585 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004586 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004587 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004588 return -EAGAIN;
4589}
4590
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004591static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4592 struct io_async_msghdr *iomsg)
4593{
4594 iomsg->iov = iomsg->fast_iov;
4595 iomsg->msg.msg_name = &iomsg->addr;
4596 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4597 req->sr_msg.msg_flags, &iomsg->iov);
4598}
4599
Jens Axboe3529d8c2019-12-19 18:24:38 -07004600static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004601{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004602 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004603 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004604 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004605
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004606 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4607 return -EINVAL;
4608
Jens Axboee47293f2019-12-20 08:58:21 -07004609 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004610 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004611 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004612
Jens Axboed8768362020-02-27 14:17:49 -07004613#ifdef CONFIG_COMPAT
4614 if (req->ctx->compat)
4615 sr->msg_flags |= MSG_CMSG_COMPAT;
4616#endif
4617
Jens Axboee8c2bc12020-08-15 18:44:09 -07004618 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004619 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004620 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004621 if (!ret)
4622 req->flags |= REQ_F_NEED_CLEANUP;
4623 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004624}
4625
Jens Axboe229a7b62020-06-22 10:13:11 -06004626static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4627 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004628{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004629 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004630 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004631 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004632 int ret;
4633
Florent Revestdba4a922020-12-04 12:36:04 +01004634 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004635 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004636 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004637
Jens Axboee8c2bc12020-08-15 18:44:09 -07004638 if (req->async_data) {
4639 kmsg = req->async_data;
4640 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004641 /* if iov is set, it's allocated already */
4642 if (!kmsg->iov)
4643 kmsg->iov = kmsg->fast_iov;
4644 kmsg->msg.msg_iter.iov = kmsg->iov;
4645 } else {
4646 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004647 if (ret)
4648 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004649 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004650 }
4651
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004652 flags = req->sr_msg.msg_flags;
4653 if (flags & MSG_DONTWAIT)
4654 req->flags |= REQ_F_NOWAIT;
4655 else if (force_nonblock)
4656 flags |= MSG_DONTWAIT;
4657
4658 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4659 if (force_nonblock && ret == -EAGAIN)
4660 return io_setup_async_msg(req, kmsg);
4661 if (ret == -ERESTARTSYS)
4662 ret = -EINTR;
4663
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004664 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004665 kfree(kmsg->iov);
4666 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004667 if (ret < 0)
4668 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004669 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004670 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004671}
4672
Jens Axboe229a7b62020-06-22 10:13:11 -06004673static int io_send(struct io_kiocb *req, bool force_nonblock,
4674 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004675{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004676 struct io_sr_msg *sr = &req->sr_msg;
4677 struct msghdr msg;
4678 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004679 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004680 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004681 int ret;
4682
Florent Revestdba4a922020-12-04 12:36:04 +01004683 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004684 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004685 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004686
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004687 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4688 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004689 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004690
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004691 msg.msg_name = NULL;
4692 msg.msg_control = NULL;
4693 msg.msg_controllen = 0;
4694 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004695
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004696 flags = req->sr_msg.msg_flags;
4697 if (flags & MSG_DONTWAIT)
4698 req->flags |= REQ_F_NOWAIT;
4699 else if (force_nonblock)
4700 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004701
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004702 msg.msg_flags = flags;
4703 ret = sock_sendmsg(sock, &msg);
4704 if (force_nonblock && ret == -EAGAIN)
4705 return -EAGAIN;
4706 if (ret == -ERESTARTSYS)
4707 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004708
Jens Axboe03b12302019-12-02 18:50:25 -07004709 if (ret < 0)
4710 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004711 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004712 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004713}
4714
Pavel Begunkov1400e692020-07-12 20:41:05 +03004715static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4716 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004717{
4718 struct io_sr_msg *sr = &req->sr_msg;
4719 struct iovec __user *uiov;
4720 size_t iov_len;
4721 int ret;
4722
Pavel Begunkov1400e692020-07-12 20:41:05 +03004723 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4724 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004725 if (ret)
4726 return ret;
4727
4728 if (req->flags & REQ_F_BUFFER_SELECT) {
4729 if (iov_len > 1)
4730 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004731 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004732 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004733 sr->len = iomsg->iov[0].iov_len;
4734 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004735 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004736 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004737 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004738 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4739 &iomsg->iov, &iomsg->msg.msg_iter,
4740 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004741 if (ret > 0)
4742 ret = 0;
4743 }
4744
4745 return ret;
4746}
4747
4748#ifdef CONFIG_COMPAT
4749static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004750 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004751{
4752 struct compat_msghdr __user *msg_compat;
4753 struct io_sr_msg *sr = &req->sr_msg;
4754 struct compat_iovec __user *uiov;
4755 compat_uptr_t ptr;
4756 compat_size_t len;
4757 int ret;
4758
Pavel Begunkov270a5942020-07-12 20:41:04 +03004759 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004760 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004761 &ptr, &len);
4762 if (ret)
4763 return ret;
4764
4765 uiov = compat_ptr(ptr);
4766 if (req->flags & REQ_F_BUFFER_SELECT) {
4767 compat_ssize_t clen;
4768
4769 if (len > 1)
4770 return -EINVAL;
4771 if (!access_ok(uiov, sizeof(*uiov)))
4772 return -EFAULT;
4773 if (__get_user(clen, &uiov->iov_len))
4774 return -EFAULT;
4775 if (clen < 0)
4776 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004777 sr->len = clen;
4778 iomsg->iov[0].iov_len = clen;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004779 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004780 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004781 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4782 UIO_FASTIOV, &iomsg->iov,
4783 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004784 if (ret < 0)
4785 return ret;
4786 }
4787
4788 return 0;
4789}
Jens Axboe03b12302019-12-02 18:50:25 -07004790#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004791
Pavel Begunkov1400e692020-07-12 20:41:05 +03004792static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4793 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004794{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004795 iomsg->msg.msg_name = &iomsg->addr;
4796 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004797
4798#ifdef CONFIG_COMPAT
4799 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004800 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004801#endif
4802
Pavel Begunkov1400e692020-07-12 20:41:05 +03004803 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004804}
4805
Jens Axboebcda7ba2020-02-23 16:42:51 -07004806static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004807 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004808{
4809 struct io_sr_msg *sr = &req->sr_msg;
4810 struct io_buffer *kbuf;
4811
Jens Axboebcda7ba2020-02-23 16:42:51 -07004812 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4813 if (IS_ERR(kbuf))
4814 return kbuf;
4815
4816 sr->kbuf = kbuf;
4817 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004818 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004819}
4820
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004821static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4822{
4823 return io_put_kbuf(req, req->sr_msg.kbuf);
4824}
4825
Jens Axboe3529d8c2019-12-19 18:24:38 -07004826static int io_recvmsg_prep(struct io_kiocb *req,
4827 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004828{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004829 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004830 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004831 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004832
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004833 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4834 return -EINVAL;
4835
Jens Axboe3529d8c2019-12-19 18:24:38 -07004836 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004837 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004838 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004839 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004840
Jens Axboed8768362020-02-27 14:17:49 -07004841#ifdef CONFIG_COMPAT
4842 if (req->ctx->compat)
4843 sr->msg_flags |= MSG_CMSG_COMPAT;
4844#endif
4845
Jens Axboee8c2bc12020-08-15 18:44:09 -07004846 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004847 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004848 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004849 if (!ret)
4850 req->flags |= REQ_F_NEED_CLEANUP;
4851 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004852}
4853
Jens Axboe229a7b62020-06-22 10:13:11 -06004854static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4855 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004856{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004857 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004858 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004859 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004860 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004861 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004862
Florent Revestdba4a922020-12-04 12:36:04 +01004863 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004864 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004865 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004866
Jens Axboee8c2bc12020-08-15 18:44:09 -07004867 if (req->async_data) {
4868 kmsg = req->async_data;
4869 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004870 /* if iov is set, it's allocated already */
4871 if (!kmsg->iov)
4872 kmsg->iov = kmsg->fast_iov;
4873 kmsg->msg.msg_iter.iov = kmsg->iov;
4874 } else {
4875 ret = io_recvmsg_copy_hdr(req, &iomsg);
4876 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004877 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004878 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004879 }
4880
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004881 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004882 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004883 if (IS_ERR(kbuf))
4884 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004885 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4886 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4887 1, req->sr_msg.len);
4888 }
4889
4890 flags = req->sr_msg.msg_flags;
4891 if (flags & MSG_DONTWAIT)
4892 req->flags |= REQ_F_NOWAIT;
4893 else if (force_nonblock)
4894 flags |= MSG_DONTWAIT;
4895
4896 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4897 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004898 if (force_nonblock && ret == -EAGAIN)
4899 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004900 if (ret == -ERESTARTSYS)
4901 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004902
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004903 if (req->flags & REQ_F_BUFFER_SELECTED)
4904 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004905 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004906 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004907 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004908 if (ret < 0)
4909 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004910 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004911 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004912}
4913
Jens Axboe229a7b62020-06-22 10:13:11 -06004914static int io_recv(struct io_kiocb *req, bool force_nonblock,
4915 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004916{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004917 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004918 struct io_sr_msg *sr = &req->sr_msg;
4919 struct msghdr msg;
4920 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004921 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004922 struct iovec iov;
4923 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004924 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004925
Florent Revestdba4a922020-12-04 12:36:04 +01004926 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004927 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004928 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004929
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004930 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004931 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004932 if (IS_ERR(kbuf))
4933 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004934 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004935 }
4936
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004937 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004938 if (unlikely(ret))
4939 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004940
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004941 msg.msg_name = NULL;
4942 msg.msg_control = NULL;
4943 msg.msg_controllen = 0;
4944 msg.msg_namelen = 0;
4945 msg.msg_iocb = NULL;
4946 msg.msg_flags = 0;
4947
4948 flags = req->sr_msg.msg_flags;
4949 if (flags & MSG_DONTWAIT)
4950 req->flags |= REQ_F_NOWAIT;
4951 else if (force_nonblock)
4952 flags |= MSG_DONTWAIT;
4953
4954 ret = sock_recvmsg(sock, &msg, flags);
4955 if (force_nonblock && ret == -EAGAIN)
4956 return -EAGAIN;
4957 if (ret == -ERESTARTSYS)
4958 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004959out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004960 if (req->flags & REQ_F_BUFFER_SELECTED)
4961 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004962 if (ret < 0)
4963 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004964 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004965 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004966}
4967
Jens Axboe3529d8c2019-12-19 18:24:38 -07004968static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004969{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004970 struct io_accept *accept = &req->accept;
4971
Jens Axboe14587a462020-09-05 11:36:08 -06004972 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004973 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004974 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004975 return -EINVAL;
4976
Jens Axboed55e5f52019-12-11 16:12:15 -07004977 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4978 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004979 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004980 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004981 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004982}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004983
Jens Axboe229a7b62020-06-22 10:13:11 -06004984static int io_accept(struct io_kiocb *req, bool force_nonblock,
4985 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004986{
4987 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004988 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004989 int ret;
4990
Jiufei Xuee697dee2020-06-10 13:41:59 +08004991 if (req->file->f_flags & O_NONBLOCK)
4992 req->flags |= REQ_F_NOWAIT;
4993
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004994 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004995 accept->addr_len, accept->flags,
4996 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004997 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004998 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004999 if (ret < 0) {
5000 if (ret == -ERESTARTSYS)
5001 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005002 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005003 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005004 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005005 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005006}
5007
Jens Axboe3529d8c2019-12-19 18:24:38 -07005008static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005009{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005010 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005011 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005012
Jens Axboe14587a462020-09-05 11:36:08 -06005013 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005014 return -EINVAL;
5015 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
5016 return -EINVAL;
5017
Jens Axboe3529d8c2019-12-19 18:24:38 -07005018 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5019 conn->addr_len = READ_ONCE(sqe->addr2);
5020
5021 if (!io)
5022 return 0;
5023
5024 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005025 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07005026}
5027
Jens Axboe229a7b62020-06-22 10:13:11 -06005028static int io_connect(struct io_kiocb *req, bool force_nonblock,
5029 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005030{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005031 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005032 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005033 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005034
Jens Axboee8c2bc12020-08-15 18:44:09 -07005035 if (req->async_data) {
5036 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005037 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005038 ret = move_addr_to_kernel(req->connect.addr,
5039 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005040 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005041 if (ret)
5042 goto out;
5043 io = &__io;
5044 }
5045
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005046 file_flags = force_nonblock ? O_NONBLOCK : 0;
5047
Jens Axboee8c2bc12020-08-15 18:44:09 -07005048 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005049 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005050 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005051 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005052 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005053 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005054 ret = -ENOMEM;
5055 goto out;
5056 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005057 io = req->async_data;
5058 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005059 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005060 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005061 if (ret == -ERESTARTSYS)
5062 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005063out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005064 if (ret < 0)
5065 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005066 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005067 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005068}
YueHaibing469956e2020-03-04 15:53:52 +08005069#else /* !CONFIG_NET */
5070static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5071{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005072 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005073}
5074
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005075static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
5076 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005077{
YueHaibing469956e2020-03-04 15:53:52 +08005078 return -EOPNOTSUPP;
5079}
5080
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005081static int io_send(struct io_kiocb *req, bool force_nonblock,
5082 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005083{
5084 return -EOPNOTSUPP;
5085}
5086
5087static int io_recvmsg_prep(struct io_kiocb *req,
5088 const struct io_uring_sqe *sqe)
5089{
5090 return -EOPNOTSUPP;
5091}
5092
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005093static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
5094 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005095{
5096 return -EOPNOTSUPP;
5097}
5098
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005099static int io_recv(struct io_kiocb *req, bool force_nonblock,
5100 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005101{
5102 return -EOPNOTSUPP;
5103}
5104
5105static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5106{
5107 return -EOPNOTSUPP;
5108}
5109
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005110static int io_accept(struct io_kiocb *req, bool force_nonblock,
5111 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005112{
5113 return -EOPNOTSUPP;
5114}
5115
5116static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5117{
5118 return -EOPNOTSUPP;
5119}
5120
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005121static int io_connect(struct io_kiocb *req, bool force_nonblock,
5122 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005123{
5124 return -EOPNOTSUPP;
5125}
5126#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005127
Jens Axboed7718a92020-02-14 22:23:12 -07005128struct io_poll_table {
5129 struct poll_table_struct pt;
5130 struct io_kiocb *req;
5131 int error;
5132};
5133
Jens Axboed7718a92020-02-14 22:23:12 -07005134static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5135 __poll_t mask, task_work_func_t func)
5136{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005137 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005138
5139 /* for instances that support it check for an event match first: */
5140 if (mask && !(mask & poll->events))
5141 return 0;
5142
5143 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5144
5145 list_del_init(&poll->wait.entry);
5146
Jens Axboed7718a92020-02-14 22:23:12 -07005147 req->result = mask;
5148 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005149 percpu_ref_get(&req->ctx->refs);
5150
Jens Axboed7718a92020-02-14 22:23:12 -07005151 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005152 * If this fails, then the task is exiting. When a task exits, the
5153 * work gets canceled, so just cancel this request as well instead
5154 * of executing it. We can't safely execute it anyway, as we may not
5155 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005156 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005157 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005158 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06005159 struct task_struct *tsk;
5160
Jens Axboee3aabf92020-05-18 11:04:17 -06005161 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005162 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06005163 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06005164 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005165 }
Jens Axboed7718a92020-02-14 22:23:12 -07005166 return 1;
5167}
5168
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005169static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5170 __acquires(&req->ctx->completion_lock)
5171{
5172 struct io_ring_ctx *ctx = req->ctx;
5173
5174 if (!req->result && !READ_ONCE(poll->canceled)) {
5175 struct poll_table_struct pt = { ._key = poll->events };
5176
5177 req->result = vfs_poll(req->file, &pt) & poll->events;
5178 }
5179
5180 spin_lock_irq(&ctx->completion_lock);
5181 if (!req->result && !READ_ONCE(poll->canceled)) {
5182 add_wait_queue(poll->head, &poll->wait);
5183 return true;
5184 }
5185
5186 return false;
5187}
5188
Jens Axboed4e7cd32020-08-15 11:44:50 -07005189static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005190{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005191 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005192 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005193 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005194 return req->apoll->double_poll;
5195}
5196
5197static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5198{
5199 if (req->opcode == IORING_OP_POLL_ADD)
5200 return &req->poll;
5201 return &req->apoll->poll;
5202}
5203
5204static void io_poll_remove_double(struct io_kiocb *req)
5205{
5206 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005207
5208 lockdep_assert_held(&req->ctx->completion_lock);
5209
5210 if (poll && poll->head) {
5211 struct wait_queue_head *head = poll->head;
5212
5213 spin_lock(&head->lock);
5214 list_del_init(&poll->wait.entry);
5215 if (poll->wait.private)
5216 refcount_dec(&req->refs);
5217 poll->head = NULL;
5218 spin_unlock(&head->lock);
5219 }
5220}
5221
5222static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5223{
5224 struct io_ring_ctx *ctx = req->ctx;
5225
Jens Axboed4e7cd32020-08-15 11:44:50 -07005226 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005227 req->poll.done = true;
5228 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5229 io_commit_cqring(ctx);
5230}
5231
Jens Axboe18bceab2020-05-15 11:56:54 -06005232static void io_poll_task_func(struct callback_head *cb)
5233{
5234 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005235 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005236 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005237
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005238 if (io_poll_rewait(req, &req->poll)) {
5239 spin_unlock_irq(&ctx->completion_lock);
5240 } else {
5241 hash_del(&req->hash_node);
5242 io_poll_complete(req, req->result, 0);
5243 spin_unlock_irq(&ctx->completion_lock);
5244
5245 nxt = io_put_req_find_next(req);
5246 io_cqring_ev_posted(ctx);
5247 if (nxt)
5248 __io_req_task_submit(nxt);
5249 }
5250
Jens Axboe6d816e02020-08-11 08:04:14 -06005251 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005252}
5253
5254static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5255 int sync, void *key)
5256{
5257 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005258 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005259 __poll_t mask = key_to_poll(key);
5260
5261 /* for instances that support it check for an event match first: */
5262 if (mask && !(mask & poll->events))
5263 return 0;
5264
Jens Axboe8706e042020-09-28 08:38:54 -06005265 list_del_init(&wait->entry);
5266
Jens Axboe807abcb2020-07-17 17:09:27 -06005267 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005268 bool done;
5269
Jens Axboe807abcb2020-07-17 17:09:27 -06005270 spin_lock(&poll->head->lock);
5271 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005272 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005273 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005274 /* make sure double remove sees this as being gone */
5275 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005276 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005277 if (!done) {
5278 /* use wait func handler, so it matches the rq type */
5279 poll->wait.func(&poll->wait, mode, sync, key);
5280 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005281 }
5282 refcount_dec(&req->refs);
5283 return 1;
5284}
5285
5286static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5287 wait_queue_func_t wake_func)
5288{
5289 poll->head = NULL;
5290 poll->done = false;
5291 poll->canceled = false;
5292 poll->events = events;
5293 INIT_LIST_HEAD(&poll->wait.entry);
5294 init_waitqueue_func_entry(&poll->wait, wake_func);
5295}
5296
5297static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005298 struct wait_queue_head *head,
5299 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005300{
5301 struct io_kiocb *req = pt->req;
5302
5303 /*
5304 * If poll->head is already set, it's because the file being polled
5305 * uses multiple waitqueues for poll handling (eg one for read, one
5306 * for write). Setup a separate io_poll_iocb if this happens.
5307 */
5308 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005309 struct io_poll_iocb *poll_one = poll;
5310
Jens Axboe18bceab2020-05-15 11:56:54 -06005311 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005312 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005313 pt->error = -EINVAL;
5314 return;
5315 }
5316 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5317 if (!poll) {
5318 pt->error = -ENOMEM;
5319 return;
5320 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005321 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005322 refcount_inc(&req->refs);
5323 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005324 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005325 }
5326
5327 pt->error = 0;
5328 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005329
5330 if (poll->events & EPOLLEXCLUSIVE)
5331 add_wait_queue_exclusive(head, &poll->wait);
5332 else
5333 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005334}
5335
5336static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5337 struct poll_table_struct *p)
5338{
5339 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005340 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005341
Jens Axboe807abcb2020-07-17 17:09:27 -06005342 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005343}
5344
Jens Axboed7718a92020-02-14 22:23:12 -07005345static void io_async_task_func(struct callback_head *cb)
5346{
5347 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5348 struct async_poll *apoll = req->apoll;
5349 struct io_ring_ctx *ctx = req->ctx;
5350
5351 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5352
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005353 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005354 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005355 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005356 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005357 }
5358
Jens Axboe31067252020-05-17 17:43:31 -06005359 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005360 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005361 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005362
Jens Axboed4e7cd32020-08-15 11:44:50 -07005363 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005364 spin_unlock_irq(&ctx->completion_lock);
5365
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005366 if (!READ_ONCE(apoll->poll.canceled))
5367 __io_req_task_submit(req);
5368 else
5369 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005370
Jens Axboe6d816e02020-08-11 08:04:14 -06005371 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005372 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005373 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005374}
5375
5376static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5377 void *key)
5378{
5379 struct io_kiocb *req = wait->private;
5380 struct io_poll_iocb *poll = &req->apoll->poll;
5381
5382 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5383 key_to_poll(key));
5384
5385 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5386}
5387
5388static void io_poll_req_insert(struct io_kiocb *req)
5389{
5390 struct io_ring_ctx *ctx = req->ctx;
5391 struct hlist_head *list;
5392
5393 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5394 hlist_add_head(&req->hash_node, list);
5395}
5396
5397static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5398 struct io_poll_iocb *poll,
5399 struct io_poll_table *ipt, __poll_t mask,
5400 wait_queue_func_t wake_func)
5401 __acquires(&ctx->completion_lock)
5402{
5403 struct io_ring_ctx *ctx = req->ctx;
5404 bool cancel = false;
5405
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005406 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005407 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005408 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005409 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005410
5411 ipt->pt._key = mask;
5412 ipt->req = req;
5413 ipt->error = -EINVAL;
5414
Jens Axboed7718a92020-02-14 22:23:12 -07005415 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5416
5417 spin_lock_irq(&ctx->completion_lock);
5418 if (likely(poll->head)) {
5419 spin_lock(&poll->head->lock);
5420 if (unlikely(list_empty(&poll->wait.entry))) {
5421 if (ipt->error)
5422 cancel = true;
5423 ipt->error = 0;
5424 mask = 0;
5425 }
5426 if (mask || ipt->error)
5427 list_del_init(&poll->wait.entry);
5428 else if (cancel)
5429 WRITE_ONCE(poll->canceled, true);
5430 else if (!poll->done) /* actually waiting for an event */
5431 io_poll_req_insert(req);
5432 spin_unlock(&poll->head->lock);
5433 }
5434
5435 return mask;
5436}
5437
5438static bool io_arm_poll_handler(struct io_kiocb *req)
5439{
5440 const struct io_op_def *def = &io_op_defs[req->opcode];
5441 struct io_ring_ctx *ctx = req->ctx;
5442 struct async_poll *apoll;
5443 struct io_poll_table ipt;
5444 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005445 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005446
5447 if (!req->file || !file_can_poll(req->file))
5448 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005449 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005450 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005451 if (def->pollin)
5452 rw = READ;
5453 else if (def->pollout)
5454 rw = WRITE;
5455 else
5456 return false;
5457 /* if we can't nonblock try, then no point in arming a poll handler */
5458 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005459 return false;
5460
5461 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5462 if (unlikely(!apoll))
5463 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005464 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005465
5466 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005467 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005468
Nathan Chancellor8755d972020-03-02 16:01:19 -07005469 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005470 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005471 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005472 if (def->pollout)
5473 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005474
5475 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5476 if ((req->opcode == IORING_OP_RECVMSG) &&
5477 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5478 mask &= ~POLLIN;
5479
Jens Axboed7718a92020-02-14 22:23:12 -07005480 mask |= POLLERR | POLLPRI;
5481
5482 ipt.pt._qproc = io_async_queue_proc;
5483
5484 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5485 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005486 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005487 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005488 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005489 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005490 kfree(apoll);
5491 return false;
5492 }
5493 spin_unlock_irq(&ctx->completion_lock);
5494 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5495 apoll->poll.events);
5496 return true;
5497}
5498
5499static bool __io_poll_remove_one(struct io_kiocb *req,
5500 struct io_poll_iocb *poll)
5501{
Jens Axboeb41e9852020-02-17 09:52:41 -07005502 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005503
5504 spin_lock(&poll->head->lock);
5505 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005506 if (!list_empty(&poll->wait.entry)) {
5507 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005508 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005509 }
5510 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005511 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005512 return do_complete;
5513}
5514
5515static bool io_poll_remove_one(struct io_kiocb *req)
5516{
5517 bool do_complete;
5518
Jens Axboed4e7cd32020-08-15 11:44:50 -07005519 io_poll_remove_double(req);
5520
Jens Axboed7718a92020-02-14 22:23:12 -07005521 if (req->opcode == IORING_OP_POLL_ADD) {
5522 do_complete = __io_poll_remove_one(req, &req->poll);
5523 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005524 struct async_poll *apoll = req->apoll;
5525
Jens Axboed7718a92020-02-14 22:23:12 -07005526 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005527 do_complete = __io_poll_remove_one(req, &apoll->poll);
5528 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005529 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005530 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005531 kfree(apoll);
5532 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005533 }
5534
Jens Axboeb41e9852020-02-17 09:52:41 -07005535 if (do_complete) {
5536 io_cqring_fill_event(req, -ECANCELED);
5537 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005538 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005539 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005540 }
5541
5542 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005543}
5544
Jens Axboe76e1b642020-09-26 15:05:03 -06005545/*
5546 * Returns true if we found and killed one or more poll requests
5547 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005548static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5549 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005550{
Jens Axboe78076bb2019-12-04 19:56:40 -07005551 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005552 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005553 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005554
5555 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005556 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5557 struct hlist_head *list;
5558
5559 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005560 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005561 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005562 posted += io_poll_remove_one(req);
5563 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005564 }
5565 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005566
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005567 if (posted)
5568 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005569
5570 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005571}
5572
Jens Axboe47f46762019-11-09 17:43:02 -07005573static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5574{
Jens Axboe78076bb2019-12-04 19:56:40 -07005575 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005576 struct io_kiocb *req;
5577
Jens Axboe78076bb2019-12-04 19:56:40 -07005578 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5579 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005580 if (sqe_addr != req->user_data)
5581 continue;
5582 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005583 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005584 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005585 }
5586
5587 return -ENOENT;
5588}
5589
Jens Axboe3529d8c2019-12-19 18:24:38 -07005590static int io_poll_remove_prep(struct io_kiocb *req,
5591 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005592{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005593 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5594 return -EINVAL;
5595 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5596 sqe->poll_events)
5597 return -EINVAL;
5598
Pavel Begunkov018043b2020-10-27 23:17:18 +00005599 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005600 return 0;
5601}
5602
5603/*
5604 * Find a running poll command that matches one specified in sqe->addr,
5605 * and remove it if found.
5606 */
5607static int io_poll_remove(struct io_kiocb *req)
5608{
5609 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005610 int ret;
5611
Jens Axboe221c5eb2019-01-17 09:41:58 -07005612 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005613 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005614 spin_unlock_irq(&ctx->completion_lock);
5615
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005616 if (ret < 0)
5617 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005618 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005619 return 0;
5620}
5621
Jens Axboe221c5eb2019-01-17 09:41:58 -07005622static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5623 void *key)
5624{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005625 struct io_kiocb *req = wait->private;
5626 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005627
Jens Axboed7718a92020-02-14 22:23:12 -07005628 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005629}
5630
Jens Axboe221c5eb2019-01-17 09:41:58 -07005631static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5632 struct poll_table_struct *p)
5633{
5634 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5635
Jens Axboee8c2bc12020-08-15 18:44:09 -07005636 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005637}
5638
Jens Axboe3529d8c2019-12-19 18:24:38 -07005639static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005640{
5641 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005642 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005643
5644 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5645 return -EINVAL;
5646 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5647 return -EINVAL;
5648
Jiufei Xue5769a352020-06-17 17:53:55 +08005649 events = READ_ONCE(sqe->poll32_events);
5650#ifdef __BIG_ENDIAN
5651 events = swahw32(events);
5652#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005653 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5654 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005655 return 0;
5656}
5657
Pavel Begunkov014db002020-03-03 21:33:12 +03005658static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005659{
5660 struct io_poll_iocb *poll = &req->poll;
5661 struct io_ring_ctx *ctx = req->ctx;
5662 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005663 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005664
Jens Axboed7718a92020-02-14 22:23:12 -07005665 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005666
Jens Axboed7718a92020-02-14 22:23:12 -07005667 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5668 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005669
Jens Axboe8c838782019-03-12 15:48:16 -06005670 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005671 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005672 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005673 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005674 spin_unlock_irq(&ctx->completion_lock);
5675
Jens Axboe8c838782019-03-12 15:48:16 -06005676 if (mask) {
5677 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005678 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005679 }
Jens Axboe8c838782019-03-12 15:48:16 -06005680 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005681}
5682
Jens Axboe5262f562019-09-17 12:26:57 -06005683static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5684{
Jens Axboead8a48a2019-11-15 08:49:11 -07005685 struct io_timeout_data *data = container_of(timer,
5686 struct io_timeout_data, timer);
5687 struct io_kiocb *req = data->req;
5688 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005689 unsigned long flags;
5690
Jens Axboe5262f562019-09-17 12:26:57 -06005691 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005692 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005693 atomic_set(&req->ctx->cq_timeouts,
5694 atomic_read(&req->ctx->cq_timeouts) + 1);
5695
Jens Axboe78e19bb2019-11-06 15:21:34 -07005696 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005697 io_commit_cqring(ctx);
5698 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5699
5700 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005701 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005702 io_put_req(req);
5703 return HRTIMER_NORESTART;
5704}
5705
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005706static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5707 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005708{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005709 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005710 struct io_kiocb *req;
5711 int ret = -ENOENT;
5712
5713 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5714 if (user_data == req->user_data) {
5715 ret = 0;
5716 break;
5717 }
5718 }
5719
5720 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005721 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005722
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005723 io = req->async_data;
5724 ret = hrtimer_try_to_cancel(&io->timer);
5725 if (ret == -1)
5726 return ERR_PTR(-EALREADY);
5727 list_del_init(&req->timeout.list);
5728 return req;
5729}
5730
5731static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5732{
5733 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5734
5735 if (IS_ERR(req))
5736 return PTR_ERR(req);
5737
5738 req_set_fail_links(req);
5739 io_cqring_fill_event(req, -ECANCELED);
5740 io_put_req_deferred(req, 1);
5741 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005742}
5743
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005744static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5745 struct timespec64 *ts, enum hrtimer_mode mode)
5746{
5747 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5748 struct io_timeout_data *data;
5749
5750 if (IS_ERR(req))
5751 return PTR_ERR(req);
5752
5753 req->timeout.off = 0; /* noseq */
5754 data = req->async_data;
5755 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5756 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5757 data->timer.function = io_timeout_fn;
5758 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5759 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005760}
5761
Jens Axboe3529d8c2019-12-19 18:24:38 -07005762static int io_timeout_remove_prep(struct io_kiocb *req,
5763 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005764{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005765 struct io_timeout_rem *tr = &req->timeout_rem;
5766
Jens Axboeb29472e2019-12-17 18:50:29 -07005767 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5768 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005769 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5770 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005771 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005772 return -EINVAL;
5773
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005774 tr->addr = READ_ONCE(sqe->addr);
5775 tr->flags = READ_ONCE(sqe->timeout_flags);
5776 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5777 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5778 return -EINVAL;
5779 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5780 return -EFAULT;
5781 } else if (tr->flags) {
5782 /* timeout removal doesn't support flags */
5783 return -EINVAL;
5784 }
5785
Jens Axboeb29472e2019-12-17 18:50:29 -07005786 return 0;
5787}
5788
Jens Axboe11365042019-10-16 09:08:32 -06005789/*
5790 * Remove or update an existing timeout command
5791 */
Jens Axboefc4df992019-12-10 14:38:45 -07005792static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005793{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005794 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005795 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005796 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005797
Jens Axboe11365042019-10-16 09:08:32 -06005798 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005799 if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) {
5800 enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS)
5801 ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
5802
5803 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
5804 } else {
5805 ret = io_timeout_cancel(ctx, tr->addr);
5806 }
Jens Axboe11365042019-10-16 09:08:32 -06005807
Jens Axboe47f46762019-11-09 17:43:02 -07005808 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005809 io_commit_cqring(ctx);
5810 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005811 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005812 if (ret < 0)
5813 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005814 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005815 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005816}
5817
Jens Axboe3529d8c2019-12-19 18:24:38 -07005818static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005819 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005820{
Jens Axboead8a48a2019-11-15 08:49:11 -07005821 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005822 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005823 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005824
Jens Axboead8a48a2019-11-15 08:49:11 -07005825 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005826 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005827 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005828 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005829 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005830 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005831 flags = READ_ONCE(sqe->timeout_flags);
5832 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005833 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005834
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005835 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005836
Jens Axboee8c2bc12020-08-15 18:44:09 -07005837 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005838 return -ENOMEM;
5839
Jens Axboee8c2bc12020-08-15 18:44:09 -07005840 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005841 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005842
5843 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005844 return -EFAULT;
5845
Jens Axboe11365042019-10-16 09:08:32 -06005846 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005847 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005848 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005849 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005850
Jens Axboead8a48a2019-11-15 08:49:11 -07005851 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5852 return 0;
5853}
5854
Jens Axboefc4df992019-12-10 14:38:45 -07005855static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005856{
Jens Axboead8a48a2019-11-15 08:49:11 -07005857 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005858 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005859 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005860 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005861
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005862 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005863
Jens Axboe5262f562019-09-17 12:26:57 -06005864 /*
5865 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005866 * timeout event to be satisfied. If it isn't set, then this is
5867 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005868 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005869 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005870 entry = ctx->timeout_list.prev;
5871 goto add;
5872 }
Jens Axboe5262f562019-09-17 12:26:57 -06005873
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005874 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5875 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005876
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005877 /* Update the last seq here in case io_flush_timeouts() hasn't.
5878 * This is safe because ->completion_lock is held, and submissions
5879 * and completions are never mixed in the same ->completion_lock section.
5880 */
5881 ctx->cq_last_tm_flush = tail;
5882
Jens Axboe5262f562019-09-17 12:26:57 -06005883 /*
5884 * Insertion sort, ensuring the first entry in the list is always
5885 * the one we need first.
5886 */
Jens Axboe5262f562019-09-17 12:26:57 -06005887 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005888 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5889 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005890
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005891 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005892 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005893 /* nxt.seq is behind @tail, otherwise would've been completed */
5894 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005895 break;
5896 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005897add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005898 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005899 data->timer.function = io_timeout_fn;
5900 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005901 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005902 return 0;
5903}
5904
Jens Axboe62755e32019-10-28 21:49:21 -06005905static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005906{
Jens Axboe62755e32019-10-28 21:49:21 -06005907 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005908
Jens Axboe62755e32019-10-28 21:49:21 -06005909 return req->user_data == (unsigned long) data;
5910}
5911
Jens Axboee977d6d2019-11-05 12:39:45 -07005912static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005913{
Jens Axboe62755e32019-10-28 21:49:21 -06005914 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005915 int ret = 0;
5916
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005917 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005918 switch (cancel_ret) {
5919 case IO_WQ_CANCEL_OK:
5920 ret = 0;
5921 break;
5922 case IO_WQ_CANCEL_RUNNING:
5923 ret = -EALREADY;
5924 break;
5925 case IO_WQ_CANCEL_NOTFOUND:
5926 ret = -ENOENT;
5927 break;
5928 }
5929
Jens Axboee977d6d2019-11-05 12:39:45 -07005930 return ret;
5931}
5932
Jens Axboe47f46762019-11-09 17:43:02 -07005933static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5934 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005935 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005936{
5937 unsigned long flags;
5938 int ret;
5939
5940 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5941 if (ret != -ENOENT) {
5942 spin_lock_irqsave(&ctx->completion_lock, flags);
5943 goto done;
5944 }
5945
5946 spin_lock_irqsave(&ctx->completion_lock, flags);
5947 ret = io_timeout_cancel(ctx, sqe_addr);
5948 if (ret != -ENOENT)
5949 goto done;
5950 ret = io_poll_cancel(ctx, sqe_addr);
5951done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005952 if (!ret)
5953 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005954 io_cqring_fill_event(req, ret);
5955 io_commit_cqring(ctx);
5956 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5957 io_cqring_ev_posted(ctx);
5958
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005959 if (ret < 0)
5960 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005961 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005962}
5963
Jens Axboe3529d8c2019-12-19 18:24:38 -07005964static int io_async_cancel_prep(struct io_kiocb *req,
5965 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005966{
Jens Axboefbf23842019-12-17 18:45:56 -07005967 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005968 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005969 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5970 return -EINVAL;
5971 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005972 return -EINVAL;
5973
Jens Axboefbf23842019-12-17 18:45:56 -07005974 req->cancel.addr = READ_ONCE(sqe->addr);
5975 return 0;
5976}
5977
Pavel Begunkov014db002020-03-03 21:33:12 +03005978static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005979{
5980 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005981
Pavel Begunkov014db002020-03-03 21:33:12 +03005982 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005983 return 0;
5984}
5985
Jens Axboe05f3fb32019-12-09 11:22:50 -07005986static int io_files_update_prep(struct io_kiocb *req,
5987 const struct io_uring_sqe *sqe)
5988{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005989 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5990 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005991 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5992 return -EINVAL;
5993 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005994 return -EINVAL;
5995
5996 req->files_update.offset = READ_ONCE(sqe->off);
5997 req->files_update.nr_args = READ_ONCE(sqe->len);
5998 if (!req->files_update.nr_args)
5999 return -EINVAL;
6000 req->files_update.arg = READ_ONCE(sqe->addr);
6001 return 0;
6002}
6003
Jens Axboe229a7b62020-06-22 10:13:11 -06006004static int io_files_update(struct io_kiocb *req, bool force_nonblock,
6005 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006006{
6007 struct io_ring_ctx *ctx = req->ctx;
6008 struct io_uring_files_update up;
6009 int ret;
6010
Jens Axboef86cd202020-01-29 13:46:44 -07006011 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006012 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006013
6014 up.offset = req->files_update.offset;
6015 up.fds = req->files_update.arg;
6016
6017 mutex_lock(&ctx->uring_lock);
6018 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
6019 mutex_unlock(&ctx->uring_lock);
6020
6021 if (ret < 0)
6022 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06006023 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006024 return 0;
6025}
6026
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006027static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006028{
Jens Axboed625c6e2019-12-17 19:53:05 -07006029 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006030 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006031 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006032 case IORING_OP_READV:
6033 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006034 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006035 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006036 case IORING_OP_WRITEV:
6037 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006038 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006039 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006040 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006041 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006042 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006043 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006044 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006045 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006046 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006047 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006048 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006049 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006050 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006051 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006052 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006053 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006054 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006055 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006056 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006057 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006058 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006059 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006060 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006061 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006062 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006063 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006064 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006065 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006066 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006067 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006068 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006069 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006070 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006071 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006072 case IORING_OP_FILES_UPDATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006073 return io_files_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006074 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006075 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006076 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006077 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006078 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006079 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006080 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006081 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006082 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006083 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006084 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006085 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006086 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006087 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006088 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006089 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006090 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006091 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006092 case IORING_OP_SHUTDOWN:
6093 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006094 case IORING_OP_RENAMEAT:
6095 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006096 case IORING_OP_UNLINKAT:
6097 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006098 }
6099
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006100 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6101 req->opcode);
6102 return-EINVAL;
6103}
6104
Jens Axboedef596e2019-01-09 08:59:42 -07006105static int io_req_defer_prep(struct io_kiocb *req,
6106 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006107{
Jens Axboedef596e2019-01-09 08:59:42 -07006108 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006109 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006110 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006111 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006112 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006113}
6114
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006115static u32 io_get_sequence(struct io_kiocb *req)
6116{
6117 struct io_kiocb *pos;
6118 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006119 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006120
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006121 io_for_each_link(pos, req)
6122 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006123
6124 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6125 return total_submitted - nr_reqs;
6126}
6127
Jens Axboe3529d8c2019-12-19 18:24:38 -07006128static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006129{
6130 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006131 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006132 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006133 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006134
6135 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006136 if (likely(list_empty_careful(&ctx->defer_list) &&
6137 !(req->flags & REQ_F_IO_DRAIN)))
6138 return 0;
6139
6140 seq = io_get_sequence(req);
6141 /* Still a chance to pass the sequence check */
6142 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006143 return 0;
6144
Jens Axboee8c2bc12020-08-15 18:44:09 -07006145 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006146 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006147 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006148 return ret;
6149 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006150 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006151 de = kmalloc(sizeof(*de), GFP_KERNEL);
6152 if (!de)
6153 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006154
6155 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006156 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006157 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006158 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006159 io_queue_async_work(req);
6160 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006161 }
6162
6163 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006164 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006165 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006166 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006167 spin_unlock_irq(&ctx->completion_lock);
6168 return -EIOCBQUEUED;
6169}
Jens Axboeedafcce2019-01-09 09:16:05 -07006170
Jens Axboef573d382020-09-22 10:19:24 -06006171static void io_req_drop_files(struct io_kiocb *req)
6172{
6173 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc98de082020-11-15 12:56:32 +00006174 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboef573d382020-09-22 10:19:24 -06006175 unsigned long flags;
6176
Jens Axboe02a13672021-01-23 15:49:31 -07006177 if (req->work.flags & IO_WQ_WORK_FILES) {
6178 put_files_struct(req->work.identity->files);
6179 put_nsproxy(req->work.identity->nsproxy);
6180 }
Pavel Begunkovdfea9fc2020-12-18 13:12:21 +00006181 spin_lock_irqsave(&ctx->inflight_lock, flags);
6182 list_del(&req->inflight_entry);
6183 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
6184 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboedfead8a2020-10-14 10:12:37 -06006185 req->work.flags &= ~IO_WQ_WORK_FILES;
Pavel Begunkovdfea9fc2020-12-18 13:12:21 +00006186 if (atomic_read(&tctx->in_idle))
6187 wake_up(&tctx->wait);
Jens Axboef573d382020-09-22 10:19:24 -06006188}
6189
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006190static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006191{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006192 if (req->flags & REQ_F_BUFFER_SELECTED) {
6193 switch (req->opcode) {
6194 case IORING_OP_READV:
6195 case IORING_OP_READ_FIXED:
6196 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006197 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006198 break;
6199 case IORING_OP_RECVMSG:
6200 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006201 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006202 break;
6203 }
6204 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006205 }
6206
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006207 if (req->flags & REQ_F_NEED_CLEANUP) {
6208 switch (req->opcode) {
6209 case IORING_OP_READV:
6210 case IORING_OP_READ_FIXED:
6211 case IORING_OP_READ:
6212 case IORING_OP_WRITEV:
6213 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006214 case IORING_OP_WRITE: {
6215 struct io_async_rw *io = req->async_data;
6216 if (io->free_iovec)
6217 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006218 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006219 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006220 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006221 case IORING_OP_SENDMSG: {
6222 struct io_async_msghdr *io = req->async_data;
6223 if (io->iov != io->fast_iov)
6224 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006225 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006226 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006227 case IORING_OP_SPLICE:
6228 case IORING_OP_TEE:
6229 io_put_file(req, req->splice.file_in,
6230 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6231 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006232 case IORING_OP_OPENAT:
6233 case IORING_OP_OPENAT2:
6234 if (req->open.filename)
6235 putname(req->open.filename);
6236 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006237 case IORING_OP_RENAMEAT:
6238 putname(req->rename.oldpath);
6239 putname(req->rename.newpath);
6240 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006241 case IORING_OP_UNLINKAT:
6242 putname(req->unlink.filename);
6243 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006244 }
6245 req->flags &= ~REQ_F_NEED_CLEANUP;
6246 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006247}
6248
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006249static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
6250 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006251{
Jens Axboeedafcce2019-01-09 09:16:05 -07006252 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006253 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006254
Jens Axboed625c6e2019-12-17 19:53:05 -07006255 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006256 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06006257 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006258 break;
6259 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006260 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006261 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06006262 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006263 break;
6264 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006266 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06006267 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006268 break;
6269 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03006270 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006271 break;
6272 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03006273 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006274 break;
6275 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07006276 ret = io_poll_remove(req);
6277 break;
6278 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006279 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07006280 break;
6281 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006282 ret = io_sendmsg(req, force_nonblock, cs);
6283 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006284 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006285 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006286 break;
6287 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006288 ret = io_recvmsg(req, force_nonblock, cs);
6289 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006290 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006291 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006292 break;
6293 case IORING_OP_TIMEOUT:
6294 ret = io_timeout(req);
6295 break;
6296 case IORING_OP_TIMEOUT_REMOVE:
6297 ret = io_timeout_remove(req);
6298 break;
6299 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006300 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301 break;
6302 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006303 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006304 break;
6305 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006306 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006307 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006308 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006309 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006310 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006311 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006312 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006313 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006314 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006315 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006316 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006317 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006318 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006319 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006320 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006321 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006322 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006323 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006324 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006325 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006326 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006327 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006328 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006329 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006330 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006331 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006332 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006333 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006334 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006335 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006336 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006337 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006338 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006339 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006340 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006341 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006342 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006343 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006344 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006345 ret = io_tee(req, force_nonblock);
6346 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006347 case IORING_OP_SHUTDOWN:
6348 ret = io_shutdown(req, force_nonblock);
6349 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006350 case IORING_OP_RENAMEAT:
6351 ret = io_renameat(req, force_nonblock);
6352 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006353 case IORING_OP_UNLINKAT:
6354 ret = io_unlinkat(req, force_nonblock);
6355 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356 default:
6357 ret = -EINVAL;
6358 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006359 }
6360
6361 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006362 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006363
Jens Axboeb5325762020-05-19 21:20:27 -06006364 /* If the op doesn't have a file, we're not polling for it */
6365 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006366 const bool in_async = io_wq_current_is_worker();
6367
Jens Axboe11ba8202020-01-15 21:51:17 -07006368 /* workqueue context doesn't hold uring_lock, grab it now */
6369 if (in_async)
6370 mutex_lock(&ctx->uring_lock);
6371
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006372 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006373
6374 if (in_async)
6375 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006376 }
6377
6378 return 0;
6379}
6380
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006381static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006382{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006383 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006384 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006385 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006386
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006387 timeout = io_prep_linked_timeout(req);
6388 if (timeout)
6389 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006390
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006391 /* if NO_CANCEL is set, we must still run the work */
6392 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6393 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006394 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006395 }
Jens Axboe31b51512019-01-18 22:56:34 -07006396
Jens Axboe561fb042019-10-24 07:25:42 -06006397 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006398 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006399 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006400 /*
6401 * We can get EAGAIN for polled IO even though we're
6402 * forcing a sync submission from here, since we can't
6403 * wait for request slots on the block side.
6404 */
6405 if (ret != -EAGAIN)
6406 break;
6407 cond_resched();
6408 } while (1);
6409 }
Jens Axboe31b51512019-01-18 22:56:34 -07006410
Jens Axboe561fb042019-10-24 07:25:42 -06006411 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006412 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006413
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006414 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6415 lock_ctx = req->ctx;
6416
6417 /*
6418 * io_iopoll_complete() does not hold completion_lock to
6419 * complete polled io, so here for polled io, we can not call
6420 * io_req_complete() directly, otherwise there maybe concurrent
6421 * access to cqring, defer_list, etc, which is not safe. Given
6422 * that io_iopoll_complete() is always called under uring_lock,
6423 * so here for polled io, we also get uring_lock to complete
6424 * it.
6425 */
6426 if (lock_ctx)
6427 mutex_lock(&lock_ctx->uring_lock);
6428
6429 req_set_fail_links(req);
6430 io_req_complete(req, ret);
6431
6432 if (lock_ctx)
6433 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006434 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006435
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006436 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006437}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006438
Jens Axboe65e19f52019-10-26 07:20:21 -06006439static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6440 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006441{
Jens Axboe65e19f52019-10-26 07:20:21 -06006442 struct fixed_file_table *table;
6443
Jens Axboe05f3fb32019-12-09 11:22:50 -07006444 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006445 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006446}
6447
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006448static struct file *io_file_get(struct io_submit_state *state,
6449 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006450{
6451 struct io_ring_ctx *ctx = req->ctx;
6452 struct file *file;
6453
6454 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006455 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006456 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006457 fd = array_index_nospec(fd, ctx->nr_user_files);
6458 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006459 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006460 } else {
6461 trace_io_uring_file_get(ctx, fd);
6462 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006463 }
6464
Jens Axboe02a13672021-01-23 15:49:31 -07006465 if (file && file->f_op == &io_uring_fops) {
6466 io_req_init_async(req);
6467 req->flags |= REQ_F_INFLIGHT;
6468
6469 spin_lock_irq(&ctx->inflight_lock);
6470 list_add(&req->inflight_entry, &ctx->inflight_list);
6471 spin_unlock_irq(&ctx->inflight_lock);
6472 }
6473
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006474 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006475}
6476
Jens Axboe2665abf2019-11-05 12:40:47 -07006477static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6478{
Jens Axboead8a48a2019-11-15 08:49:11 -07006479 struct io_timeout_data *data = container_of(timer,
6480 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006481 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006482 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006483 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006484
6485 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006486 prev = req->timeout.head;
6487 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006488
6489 /*
6490 * We don't expect the list to be empty, that will only happen if we
6491 * race with the completion of the linked work.
6492 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006493 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006494 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006495 else
6496 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006497 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6498
6499 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006500 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006501 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006502 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006503 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006504 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006505 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006506 return HRTIMER_NORESTART;
6507}
6508
Jens Axboe7271ef32020-08-10 09:55:22 -06006509static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006510{
Jens Axboe76a46e02019-11-10 23:34:16 -07006511 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006512 * If the back reference is NULL, then our linked request finished
6513 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006514 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006515 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006516 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006517
Jens Axboead8a48a2019-11-15 08:49:11 -07006518 data->timer.function = io_link_timeout_fn;
6519 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6520 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006521 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006522}
6523
6524static void io_queue_linked_timeout(struct io_kiocb *req)
6525{
6526 struct io_ring_ctx *ctx = req->ctx;
6527
6528 spin_lock_irq(&ctx->completion_lock);
6529 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006530 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006531
Jens Axboe2665abf2019-11-05 12:40:47 -07006532 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006533 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006534}
6535
Jens Axboead8a48a2019-11-15 08:49:11 -07006536static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006537{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006538 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006539
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006540 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6541 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006542 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006543
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006544 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006545 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006546 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006547 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006548}
6549
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006550static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006551{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006552 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006553 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006554 int ret;
6555
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006556again:
6557 linked_timeout = io_prep_linked_timeout(req);
6558
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006559 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6560 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006561 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006562 if (old_creds)
6563 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006564 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006565 old_creds = NULL; /* restored original creds */
6566 else
Jens Axboe98447d62020-10-14 10:48:51 -06006567 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006568 }
6569
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006570 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006571
6572 /*
6573 * We async punt it if the file wasn't marked NOWAIT, or if the file
6574 * doesn't support non-blocking read/write attempts
6575 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006576 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006577 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006578 /*
6579 * Queued up for async execution, worker will release
6580 * submit reference when the iocb is actually submitted.
6581 */
6582 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006583 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006584
Pavel Begunkovf063c542020-07-25 14:41:59 +03006585 if (linked_timeout)
6586 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006587 } else if (likely(!ret)) {
6588 /* drop submission reference */
6589 req = io_put_req_find_next(req);
6590 if (linked_timeout)
6591 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006592
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006593 if (req) {
6594 if (!(req->flags & REQ_F_FORCE_ASYNC))
6595 goto again;
6596 io_queue_async_work(req);
6597 }
6598 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006599 /* un-prep timeout, so it'll be killed as any other linked */
6600 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006601 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006602 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006603 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006604 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006605
Jens Axboe193155c2020-02-22 23:22:19 -07006606 if (old_creds)
6607 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006608}
6609
Jens Axboef13fad72020-06-22 09:34:30 -06006610static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6611 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006612{
6613 int ret;
6614
Jens Axboe3529d8c2019-12-19 18:24:38 -07006615 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006616 if (ret) {
6617 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006618fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006619 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006620 io_put_req(req);
6621 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006622 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006623 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006624 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006625 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006626 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006627 goto fail_req;
6628 }
Jens Axboece35a472019-12-17 08:04:44 -07006629 io_queue_async_work(req);
6630 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006631 if (sqe) {
6632 ret = io_req_prep(req, sqe);
6633 if (unlikely(ret))
6634 goto fail_req;
6635 }
6636 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006637 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006638}
6639
Jens Axboef13fad72020-06-22 09:34:30 -06006640static inline void io_queue_link_head(struct io_kiocb *req,
6641 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006642{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006643 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006644 io_put_req(req);
6645 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006646 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006647 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006648}
6649
Pavel Begunkov863e0562020-10-27 23:25:35 +00006650struct io_submit_link {
6651 struct io_kiocb *head;
6652 struct io_kiocb *last;
6653};
6654
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006655static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006656 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006657{
Jackie Liua197f662019-11-08 08:09:12 -07006658 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006659 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006660
Jens Axboe9e645e112019-05-10 16:07:28 -06006661 /*
6662 * If we already have a head request, queue this one for async
6663 * submittal once the head completes. If we don't have a head but
6664 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6665 * submitted sync once the chain is complete. If none of those
6666 * conditions are true (normal request), then just queue it.
6667 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006668 if (link->head) {
6669 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006670
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006671 /*
6672 * Taking sequential execution of a link, draining both sides
6673 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6674 * requests in the link. So, it drains the head and the
6675 * next after the link request. The last one is done via
6676 * drain_next flag to persist the effect across calls.
6677 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006678 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006679 head->flags |= REQ_F_IO_DRAIN;
6680 ctx->drain_next = 1;
6681 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006682 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006683 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006684 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006685 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006686 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006687 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006688 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006689 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006690 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006691
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006692 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006693 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006694 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006695 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006696 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006697 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006698 if (unlikely(ctx->drain_next)) {
6699 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006700 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006701 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006702 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006703 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006704 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006705 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006706 link->head = req;
6707 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006708 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006709 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006710 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006711 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006712
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006713 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006714}
6715
Jens Axboe9a56a232019-01-09 09:06:50 -07006716/*
6717 * Batched submission is done, ensure local IO is flushed out.
6718 */
6719static void io_submit_state_end(struct io_submit_state *state)
6720{
Jens Axboef13fad72020-06-22 09:34:30 -06006721 if (!list_empty(&state->comp.list))
6722 io_submit_flush_completions(&state->comp);
Jens Axboe27926b62020-10-28 09:33:23 -06006723 if (state->plug_started)
6724 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006725 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006726 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006727 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006728}
6729
6730/*
6731 * Start submission side cache.
6732 */
6733static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006734 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006735{
Jens Axboe27926b62020-10-28 09:33:23 -06006736 state->plug_started = false;
Jens Axboe013538b2020-06-22 09:29:15 -06006737 state->comp.nr = 0;
6738 INIT_LIST_HEAD(&state->comp.list);
6739 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006740 state->free_reqs = 0;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00006741 state->file_refs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006742 state->ios_left = max_ios;
6743}
6744
Jens Axboe2b188cc2019-01-07 10:46:33 -07006745static void io_commit_sqring(struct io_ring_ctx *ctx)
6746{
Hristo Venev75b28af2019-08-26 17:23:46 +00006747 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006748
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006749 /*
6750 * Ensure any loads from the SQEs are done at this point,
6751 * since once we write the new head, the application could
6752 * write new data to them.
6753 */
6754 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006755}
6756
6757/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006758 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006759 * that is mapped by userspace. This means that care needs to be taken to
6760 * ensure that reads are stable, as we cannot rely on userspace always
6761 * being a good citizen. If members of the sqe are validated and then later
6762 * used, it's important that those reads are done through READ_ONCE() to
6763 * prevent a re-load down the line.
6764 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006765static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006766{
Hristo Venev75b28af2019-08-26 17:23:46 +00006767 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006768 unsigned head;
6769
6770 /*
6771 * The cached sq head (or cq tail) serves two purposes:
6772 *
6773 * 1) allows us to batch the cost of updating the user visible
6774 * head updates.
6775 * 2) allows the kernel side to track the head on its own, even
6776 * though the application is the one updating it.
6777 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006778 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006779 if (likely(head < ctx->sq_entries))
6780 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006781
6782 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006783 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006784 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006785 return NULL;
6786}
6787
6788static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6789{
6790 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006791}
6792
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006793/*
6794 * Check SQE restrictions (opcode and flags).
6795 *
6796 * Returns 'true' if SQE is allowed, 'false' otherwise.
6797 */
6798static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6799 struct io_kiocb *req,
6800 unsigned int sqe_flags)
6801{
6802 if (!ctx->restricted)
6803 return true;
6804
6805 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6806 return false;
6807
6808 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6809 ctx->restrictions.sqe_flags_required)
6810 return false;
6811
6812 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6813 ctx->restrictions.sqe_flags_required))
6814 return false;
6815
6816 return true;
6817}
6818
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006819#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6820 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6821 IOSQE_BUFFER_SELECT)
6822
6823static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6824 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006825 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006826{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006827 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006828 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006829
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006830 req->opcode = READ_ONCE(sqe->opcode);
6831 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006832 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006833 req->file = NULL;
6834 req->ctx = ctx;
6835 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006836 req->link = NULL;
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006837 req->fixed_file_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006838 /* one is dropped after submission, the other at completion */
6839 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006840 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006841 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006842
6843 if (unlikely(req->opcode >= IORING_OP_LAST))
6844 return -EINVAL;
6845
Jens Axboe28cea78a2020-09-14 10:51:17 -06006846 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006847 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006848
6849 sqe_flags = READ_ONCE(sqe->flags);
6850 /* enforce forwards compatibility on users */
6851 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6852 return -EINVAL;
6853
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006854 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6855 return -EACCES;
6856
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006857 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6858 !io_op_defs[req->opcode].buffer_select)
6859 return -EOPNOTSUPP;
6860
6861 id = READ_ONCE(sqe->personality);
6862 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006863 struct io_identity *iod;
6864
Jens Axboe1e6fa522020-10-15 08:46:24 -06006865 iod = idr_find(&ctx->personality_idr, id);
6866 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006867 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006868 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006869
6870 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006871 get_cred(iod->creds);
6872 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006873 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006874 }
6875
6876 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006877 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006878
Jens Axboe27926b62020-10-28 09:33:23 -06006879 /*
6880 * Plug now if we have more than 1 IO left after this, and the target
6881 * is potentially a read/write to block based storage.
6882 */
6883 if (!state->plug_started && state->ios_left > 1 &&
6884 io_op_defs[req->opcode].plug) {
6885 blk_start_plug(&state->plug);
6886 state->plug_started = true;
6887 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006888
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006889 ret = 0;
6890 if (io_op_defs[req->opcode].needs_file) {
6891 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006892
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006893 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
6894 if (unlikely(!req->file &&
6895 !io_op_defs[req->opcode].needs_file_no_error))
6896 ret = -EBADF;
6897 }
6898
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006899 state->ios_left--;
6900 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006901}
6902
Jens Axboe0f212202020-09-13 13:09:39 -06006903static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006904{
Jens Axboeac8691c2020-06-01 08:30:41 -06006905 struct io_submit_state state;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006906 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006907 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006908
Jens Axboec4a2ed72019-11-21 21:01:26 -07006909 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006910 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006911 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006912 return -EBUSY;
6913 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006914
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006915 /* make sure SQ entry isn't read before tail */
6916 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006917
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006918 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6919 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006920
Jens Axboed8a6df12020-10-15 16:24:45 -06006921 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006922 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006923
Jens Axboe6c271ce2019-01-10 11:22:30 -07006924 io_submit_state_start(&state, ctx, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006925 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006926
Jens Axboe6c271ce2019-01-10 11:22:30 -07006927 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006928 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006929 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006930 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006931
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006932 sqe = io_get_sqe(ctx);
6933 if (unlikely(!sqe)) {
6934 io_consume_sqe(ctx);
6935 break;
6936 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006937 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006938 if (unlikely(!req)) {
6939 if (!submitted)
6940 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006941 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006942 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006943 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006944 /* will complete beyond this point, count as submitted */
6945 submitted++;
6946
Pavel Begunkov692d8362020-10-10 18:34:13 +01006947 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006948 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006949fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006950 io_put_req(req);
6951 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006952 break;
6953 }
6954
Jens Axboe354420f2020-01-08 18:55:15 -07006955 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006956 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006957 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006958 if (err)
6959 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006960 }
6961
Pavel Begunkov9466f432020-01-25 22:34:01 +03006962 if (unlikely(submitted != nr)) {
6963 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006964 struct io_uring_task *tctx = current->io_uring;
6965 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006966
Jens Axboed8a6df12020-10-15 16:24:45 -06006967 percpu_ref_put_many(&ctx->refs, unused);
6968 percpu_counter_sub(&tctx->inflight, unused);
6969 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006970 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006971 if (link.head)
6972 io_queue_link_head(link.head, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006973 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006974
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006975 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6976 io_commit_sqring(ctx);
6977
Jens Axboe6c271ce2019-01-10 11:22:30 -07006978 return submitted;
6979}
6980
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006981static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6982{
6983 /* Tell userspace we may need a wakeup call */
6984 spin_lock_irq(&ctx->completion_lock);
6985 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6986 spin_unlock_irq(&ctx->completion_lock);
6987}
6988
6989static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6990{
6991 spin_lock_irq(&ctx->completion_lock);
6992 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6993 spin_unlock_irq(&ctx->completion_lock);
6994}
6995
Xiaoguang Wang08369242020-11-03 14:15:59 +08006996static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006997{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006998 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006999 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007000
Jens Axboec8d1ba52020-09-14 11:07:26 -06007001 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007002 /* if we're handling multiple rings, cap submit size for fairness */
7003 if (cap_entries && to_submit > 8)
7004 to_submit = 8;
7005
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007006 if (!list_empty(&ctx->iopoll_list) || to_submit) {
7007 unsigned nr_events = 0;
7008
Xiaoguang Wang08369242020-11-03 14:15:59 +08007009 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007010 if (!list_empty(&ctx->iopoll_list))
7011 io_do_iopoll(ctx, &nr_events, 0);
7012
Pavel Begunkovd9d05212021-01-08 20:57:25 +00007013 if (to_submit && !ctx->sqo_dead &&
7014 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007015 ret = io_submit_sqes(ctx, to_submit);
7016 mutex_unlock(&ctx->uring_lock);
7017 }
Jens Axboe90554202020-09-03 12:12:41 -06007018
7019 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
7020 wake_up(&ctx->sqo_sq_wait);
7021
Xiaoguang Wang08369242020-11-03 14:15:59 +08007022 return ret;
7023}
7024
7025static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7026{
7027 struct io_ring_ctx *ctx;
7028 unsigned sq_thread_idle = 0;
7029
7030 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7031 if (sq_thread_idle < ctx->sq_thread_idle)
7032 sq_thread_idle = ctx->sq_thread_idle;
7033 }
7034
7035 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007036}
7037
Jens Axboe69fb2132020-09-14 11:16:23 -06007038static void io_sqd_init_new(struct io_sq_data *sqd)
7039{
7040 struct io_ring_ctx *ctx;
7041
7042 while (!list_empty(&sqd->ctx_new_list)) {
7043 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007044 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
7045 complete(&ctx->sq_thread_comp);
7046 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007047
7048 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007049}
7050
Jens Axboe6c271ce2019-01-10 11:22:30 -07007051static int io_sq_thread(void *data)
7052{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007053 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007054 struct files_struct *old_files = current->files;
7055 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007056 const struct cred *old_cred = NULL;
7057 struct io_sq_data *sqd = data;
7058 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007059 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007060 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007061
Jens Axboe28cea78a2020-09-14 10:51:17 -06007062 task_lock(current);
7063 current->files = NULL;
7064 current->nsproxy = NULL;
7065 task_unlock(current);
7066
Jens Axboe69fb2132020-09-14 11:16:23 -06007067 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007068 int ret;
7069 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007070
7071 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007072 * Any changes to the sqd lists are synchronized through the
7073 * kthread parking. This synchronizes the thread vs users,
7074 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007075 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007076 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007077 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007078 /*
7079 * When sq thread is unparked, in case the previous park operation
7080 * comes from io_put_sq_data(), which means that sq thread is going
7081 * to be stopped, so here needs to have a check.
7082 */
7083 if (kthread_should_stop())
7084 break;
7085 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007086
Xiaoguang Wang08369242020-11-03 14:15:59 +08007087 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007088 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007089 timeout = jiffies + sqd->sq_thread_idle;
7090 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007091
Xiaoguang Wang08369242020-11-03 14:15:59 +08007092 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007093 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007094 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7095 if (current->cred != ctx->creds) {
7096 if (old_cred)
7097 revert_creds(old_cred);
7098 old_cred = override_creds(ctx->creds);
7099 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007100 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007101#ifdef CONFIG_AUDIT
7102 current->loginuid = ctx->loginuid;
7103 current->sessionid = ctx->sessionid;
7104#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007105
Xiaoguang Wang08369242020-11-03 14:15:59 +08007106 ret = __io_sq_thread(ctx, cap_entries);
7107 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7108 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007109
Jens Axboe28cea78a2020-09-14 10:51:17 -06007110 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007111 }
7112
Xiaoguang Wang08369242020-11-03 14:15:59 +08007113 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007114 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007115 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007116 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007117 if (sqt_spin)
7118 timeout = jiffies + sqd->sq_thread_idle;
7119 continue;
7120 }
7121
7122 if (kthread_should_park())
7123 continue;
7124
7125 needs_sched = true;
7126 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7127 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7128 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7129 !list_empty_careful(&ctx->iopoll_list)) {
7130 needs_sched = false;
7131 break;
7132 }
7133 if (io_sqring_entries(ctx)) {
7134 needs_sched = false;
7135 break;
7136 }
7137 }
7138
7139 if (needs_sched) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007140 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7141 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007142
Jens Axboe69fb2132020-09-14 11:16:23 -06007143 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007144 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7145 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007146 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007147
7148 finish_wait(&sqd->wait, &wait);
7149 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007150 }
7151
Jens Axboe4c6e2772020-07-01 11:29:10 -06007152 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007153 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007154
Dennis Zhou91d8f512020-09-16 13:41:05 -07007155 if (cur_css)
7156 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007157 if (old_cred)
7158 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007159
Jens Axboe28cea78a2020-09-14 10:51:17 -06007160 task_lock(current);
7161 current->files = old_files;
7162 current->nsproxy = old_nsproxy;
7163 task_unlock(current);
7164
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007165 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007166
Jens Axboe6c271ce2019-01-10 11:22:30 -07007167 return 0;
7168}
7169
Jens Axboebda52162019-09-24 13:47:15 -06007170struct io_wait_queue {
7171 struct wait_queue_entry wq;
7172 struct io_ring_ctx *ctx;
7173 unsigned to_wait;
7174 unsigned nr_timeouts;
7175};
7176
Pavel Begunkov6c503152021-01-04 20:36:36 +00007177static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007178{
7179 struct io_ring_ctx *ctx = iowq->ctx;
7180
7181 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007182 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007183 * started waiting. For timeouts, we always want to return to userspace,
7184 * regardless of event count.
7185 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007186 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007187 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7188}
7189
7190static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7191 int wake_flags, void *key)
7192{
7193 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7194 wq);
7195
Pavel Begunkov6c503152021-01-04 20:36:36 +00007196 /*
7197 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7198 * the task, and the next invocation will do it.
7199 */
7200 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7201 return autoremove_wake_function(curr, mode, wake_flags, key);
7202 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007203}
7204
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007205static int io_run_task_work_sig(void)
7206{
7207 if (io_run_task_work())
7208 return 1;
7209 if (!signal_pending(current))
7210 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007211 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7212 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007213 return -EINTR;
7214}
7215
Jens Axboe2b188cc2019-01-07 10:46:33 -07007216/*
7217 * Wait until events become available, if we don't already have some. The
7218 * application must reap them itself, as they reside on the shared cq ring.
7219 */
7220static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007221 const sigset_t __user *sig, size_t sigsz,
7222 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007223{
Jens Axboebda52162019-09-24 13:47:15 -06007224 struct io_wait_queue iowq = {
7225 .wq = {
7226 .private = current,
7227 .func = io_wake_function,
7228 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7229 },
7230 .ctx = ctx,
7231 .to_wait = min_events,
7232 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007233 struct io_rings *rings = ctx->rings;
Hao Xuc73ebb62020-11-03 10:54:37 +08007234 struct timespec64 ts;
7235 signed long timeout = 0;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08007236 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007237
Jens Axboeb41e9852020-02-17 09:52:41 -07007238 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007239 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7240 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007241 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007242 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007243 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007244 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007245
7246 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007247#ifdef CONFIG_COMPAT
7248 if (in_compat_syscall())
7249 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007250 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007251 else
7252#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007253 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007254
Jens Axboe2b188cc2019-01-07 10:46:33 -07007255 if (ret)
7256 return ret;
7257 }
7258
Hao Xuc73ebb62020-11-03 10:54:37 +08007259 if (uts) {
7260 if (get_timespec64(&ts, uts))
7261 return -EFAULT;
7262 timeout = timespec64_to_jiffies(&ts);
7263 }
7264
Jens Axboebda52162019-09-24 13:47:15 -06007265 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007266 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007267 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007268 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007269 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7270 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007271 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007272 ret = io_run_task_work_sig();
7273 if (ret > 0)
Jens Axboe4c6e2772020-07-01 11:29:10 -06007274 continue;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007275 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007276 break;
Pavel Begunkov6c503152021-01-04 20:36:36 +00007277 if (io_should_wake(&iowq))
Jens Axboebda52162019-09-24 13:47:15 -06007278 break;
Pavel Begunkov6c503152021-01-04 20:36:36 +00007279 if (test_bit(0, &ctx->cq_check_overflow))
7280 continue;
Hao Xuc73ebb62020-11-03 10:54:37 +08007281 if (uts) {
7282 timeout = schedule_timeout(timeout);
7283 if (timeout == 0) {
7284 ret = -ETIME;
7285 break;
7286 }
7287 } else {
7288 schedule();
7289 }
Jens Axboebda52162019-09-24 13:47:15 -06007290 } while (1);
7291 finish_wait(&ctx->wait, &iowq.wq);
7292
Jens Axboeb7db41c2020-07-04 08:55:50 -06007293 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007294
Hristo Venev75b28af2019-08-26 17:23:46 +00007295 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007296}
7297
Jens Axboe6b063142019-01-10 22:13:58 -07007298static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7299{
7300#if defined(CONFIG_UNIX)
7301 if (ctx->ring_sock) {
7302 struct sock *sock = ctx->ring_sock->sk;
7303 struct sk_buff *skb;
7304
7305 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7306 kfree_skb(skb);
7307 }
7308#else
7309 int i;
7310
Jens Axboe65e19f52019-10-26 07:20:21 -06007311 for (i = 0; i < ctx->nr_user_files; i++) {
7312 struct file *file;
7313
7314 file = io_file_from_index(ctx, i);
7315 if (file)
7316 fput(file);
7317 }
Jens Axboe6b063142019-01-10 22:13:58 -07007318#endif
7319}
7320
Jens Axboe05f3fb32019-12-09 11:22:50 -07007321static void io_file_ref_kill(struct percpu_ref *ref)
7322{
7323 struct fixed_file_data *data;
7324
7325 data = container_of(ref, struct fixed_file_data, refs);
7326 complete(&data->done);
7327}
7328
Pavel Begunkov1642b442020-12-30 21:34:14 +00007329static void io_sqe_files_set_node(struct fixed_file_data *file_data,
7330 struct fixed_file_ref_node *ref_node)
7331{
7332 spin_lock_bh(&file_data->lock);
7333 file_data->node = ref_node;
7334 list_add_tail(&ref_node->node, &file_data->ref_list);
7335 spin_unlock_bh(&file_data->lock);
7336 percpu_ref_get(&file_data->refs);
7337}
7338
Jens Axboe6b063142019-01-10 22:13:58 -07007339static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7340{
Jens Axboe05f3fb32019-12-09 11:22:50 -07007341 struct fixed_file_data *data = ctx->file_data;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007342 struct fixed_file_ref_node *backup_node, *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007343 unsigned nr_tables, i;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007344 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007345
Jens Axboe05f3fb32019-12-09 11:22:50 -07007346 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07007347 return -ENXIO;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007348 backup_node = alloc_fixed_file_ref_node(ctx);
7349 if (!backup_node)
7350 return -ENOMEM;
Jens Axboe6b063142019-01-10 22:13:58 -07007351
Jens Axboeac0648a2020-11-23 09:37:51 -07007352 spin_lock_bh(&data->lock);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007353 ref_node = data->node;
Jens Axboeac0648a2020-11-23 09:37:51 -07007354 spin_unlock_bh(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007355 if (ref_node)
7356 percpu_ref_kill(&ref_node->refs);
7357
7358 percpu_ref_kill(&data->refs);
7359
7360 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06007361 flush_delayed_work(&ctx->file_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007362 do {
7363 ret = wait_for_completion_interruptible(&data->done);
7364 if (!ret)
7365 break;
7366 ret = io_run_task_work_sig();
7367 if (ret < 0) {
7368 percpu_ref_resurrect(&data->refs);
7369 reinit_completion(&data->done);
7370 io_sqe_files_set_node(data, backup_node);
7371 return ret;
7372 }
7373 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007374
Jens Axboe6b063142019-01-10 22:13:58 -07007375 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007376 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7377 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007378 kfree(data->table[i].files);
7379 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007380 percpu_ref_exit(&data->refs);
7381 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007382 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007383 ctx->nr_user_files = 0;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007384 destroy_fixed_file_ref_node(backup_node);
Jens Axboe6b063142019-01-10 22:13:58 -07007385 return 0;
7386}
7387
Jens Axboe534ca6d2020-09-02 13:52:19 -06007388static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007389{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007390 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007391 /*
7392 * The park is a bit of a work-around, without it we get
7393 * warning spews on shutdown with SQPOLL set and affinity
7394 * set to a single CPU.
7395 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007396 if (sqd->thread) {
7397 kthread_park(sqd->thread);
7398 kthread_stop(sqd->thread);
7399 }
7400
7401 kfree(sqd);
7402 }
7403}
7404
Jens Axboeaa061652020-09-02 14:50:27 -06007405static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7406{
7407 struct io_ring_ctx *ctx_attach;
7408 struct io_sq_data *sqd;
7409 struct fd f;
7410
7411 f = fdget(p->wq_fd);
7412 if (!f.file)
7413 return ERR_PTR(-ENXIO);
7414 if (f.file->f_op != &io_uring_fops) {
7415 fdput(f);
7416 return ERR_PTR(-EINVAL);
7417 }
7418
7419 ctx_attach = f.file->private_data;
7420 sqd = ctx_attach->sq_data;
7421 if (!sqd) {
7422 fdput(f);
7423 return ERR_PTR(-EINVAL);
7424 }
7425
7426 refcount_inc(&sqd->refs);
7427 fdput(f);
7428 return sqd;
7429}
7430
Jens Axboe534ca6d2020-09-02 13:52:19 -06007431static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7432{
7433 struct io_sq_data *sqd;
7434
Jens Axboeaa061652020-09-02 14:50:27 -06007435 if (p->flags & IORING_SETUP_ATTACH_WQ)
7436 return io_attach_sq_data(p);
7437
Jens Axboe534ca6d2020-09-02 13:52:19 -06007438 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7439 if (!sqd)
7440 return ERR_PTR(-ENOMEM);
7441
7442 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007443 INIT_LIST_HEAD(&sqd->ctx_list);
7444 INIT_LIST_HEAD(&sqd->ctx_new_list);
7445 mutex_init(&sqd->ctx_lock);
7446 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007447 init_waitqueue_head(&sqd->wait);
7448 return sqd;
7449}
7450
Jens Axboe69fb2132020-09-14 11:16:23 -06007451static void io_sq_thread_unpark(struct io_sq_data *sqd)
7452 __releases(&sqd->lock)
7453{
7454 if (!sqd->thread)
7455 return;
7456 kthread_unpark(sqd->thread);
7457 mutex_unlock(&sqd->lock);
7458}
7459
7460static void io_sq_thread_park(struct io_sq_data *sqd)
7461 __acquires(&sqd->lock)
7462{
7463 if (!sqd->thread)
7464 return;
7465 mutex_lock(&sqd->lock);
7466 kthread_park(sqd->thread);
7467}
7468
Jens Axboe534ca6d2020-09-02 13:52:19 -06007469static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7470{
7471 struct io_sq_data *sqd = ctx->sq_data;
7472
7473 if (sqd) {
7474 if (sqd->thread) {
7475 /*
7476 * We may arrive here from the error branch in
7477 * io_sq_offload_create() where the kthread is created
7478 * without being waked up, thus wake it up now to make
7479 * sure the wait will complete.
7480 */
7481 wake_up_process(sqd->thread);
7482 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007483
7484 io_sq_thread_park(sqd);
7485 }
7486
7487 mutex_lock(&sqd->ctx_lock);
7488 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007489 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007490 mutex_unlock(&sqd->ctx_lock);
7491
Xiaoguang Wang08369242020-11-03 14:15:59 +08007492 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007493 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007494
7495 io_put_sq_data(sqd);
7496 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007497 }
7498}
7499
Jens Axboe6b063142019-01-10 22:13:58 -07007500static void io_finish_async(struct io_ring_ctx *ctx)
7501{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007502 io_sq_thread_stop(ctx);
7503
Jens Axboe561fb042019-10-24 07:25:42 -06007504 if (ctx->io_wq) {
7505 io_wq_destroy(ctx->io_wq);
7506 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007507 }
7508}
7509
7510#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007511/*
7512 * Ensure the UNIX gc is aware of our file set, so we are certain that
7513 * the io_uring can be safely unregistered on process exit, even if we have
7514 * loops in the file referencing.
7515 */
7516static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7517{
7518 struct sock *sk = ctx->ring_sock->sk;
7519 struct scm_fp_list *fpl;
7520 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007521 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007522
Jens Axboe6b063142019-01-10 22:13:58 -07007523 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7524 if (!fpl)
7525 return -ENOMEM;
7526
7527 skb = alloc_skb(0, GFP_KERNEL);
7528 if (!skb) {
7529 kfree(fpl);
7530 return -ENOMEM;
7531 }
7532
7533 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007534
Jens Axboe08a45172019-10-03 08:11:03 -06007535 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007536 fpl->user = get_uid(ctx->user);
7537 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007538 struct file *file = io_file_from_index(ctx, i + offset);
7539
7540 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007541 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007542 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007543 unix_inflight(fpl->user, fpl->fp[nr_files]);
7544 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007545 }
7546
Jens Axboe08a45172019-10-03 08:11:03 -06007547 if (nr_files) {
7548 fpl->max = SCM_MAX_FD;
7549 fpl->count = nr_files;
7550 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007551 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007552 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7553 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007554
Jens Axboe08a45172019-10-03 08:11:03 -06007555 for (i = 0; i < nr_files; i++)
7556 fput(fpl->fp[i]);
7557 } else {
7558 kfree_skb(skb);
7559 kfree(fpl);
7560 }
Jens Axboe6b063142019-01-10 22:13:58 -07007561
7562 return 0;
7563}
7564
7565/*
7566 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7567 * causes regular reference counting to break down. We rely on the UNIX
7568 * garbage collection to take care of this problem for us.
7569 */
7570static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7571{
7572 unsigned left, total;
7573 int ret = 0;
7574
7575 total = 0;
7576 left = ctx->nr_user_files;
7577 while (left) {
7578 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007579
7580 ret = __io_sqe_files_scm(ctx, this_files, total);
7581 if (ret)
7582 break;
7583 left -= this_files;
7584 total += this_files;
7585 }
7586
7587 if (!ret)
7588 return 0;
7589
7590 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007591 struct file *file = io_file_from_index(ctx, total);
7592
7593 if (file)
7594 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007595 total++;
7596 }
7597
7598 return ret;
7599}
7600#else
7601static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7602{
7603 return 0;
7604}
7605#endif
7606
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007607static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
7608 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007609{
7610 int i;
7611
7612 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007613 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007614 unsigned this_files;
7615
7616 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7617 table->files = kcalloc(this_files, sizeof(struct file *),
7618 GFP_KERNEL);
7619 if (!table->files)
7620 break;
7621 nr_files -= this_files;
7622 }
7623
7624 if (i == nr_tables)
7625 return 0;
7626
7627 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007628 struct fixed_file_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007629 kfree(table->files);
7630 }
7631 return 1;
7632}
7633
Jens Axboe05f3fb32019-12-09 11:22:50 -07007634static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007635{
7636#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007637 struct sock *sock = ctx->ring_sock->sk;
7638 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7639 struct sk_buff *skb;
7640 int i;
7641
7642 __skb_queue_head_init(&list);
7643
7644 /*
7645 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7646 * remove this entry and rearrange the file array.
7647 */
7648 skb = skb_dequeue(head);
7649 while (skb) {
7650 struct scm_fp_list *fp;
7651
7652 fp = UNIXCB(skb).fp;
7653 for (i = 0; i < fp->count; i++) {
7654 int left;
7655
7656 if (fp->fp[i] != file)
7657 continue;
7658
7659 unix_notinflight(fp->user, fp->fp[i]);
7660 left = fp->count - 1 - i;
7661 if (left) {
7662 memmove(&fp->fp[i], &fp->fp[i + 1],
7663 left * sizeof(struct file *));
7664 }
7665 fp->count--;
7666 if (!fp->count) {
7667 kfree_skb(skb);
7668 skb = NULL;
7669 } else {
7670 __skb_queue_tail(&list, skb);
7671 }
7672 fput(file);
7673 file = NULL;
7674 break;
7675 }
7676
7677 if (!file)
7678 break;
7679
7680 __skb_queue_tail(&list, skb);
7681
7682 skb = skb_dequeue(head);
7683 }
7684
7685 if (skb_peek(&list)) {
7686 spin_lock_irq(&head->lock);
7687 while ((skb = __skb_dequeue(&list)) != NULL)
7688 __skb_queue_tail(head, skb);
7689 spin_unlock_irq(&head->lock);
7690 }
7691#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007692 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007693#endif
7694}
7695
Jens Axboe05f3fb32019-12-09 11:22:50 -07007696struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007697 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007698 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007699};
7700
Jens Axboe4a38aed22020-05-14 17:21:15 -06007701static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007702{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007703 struct fixed_file_data *file_data = ref_node->file_data;
7704 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007705 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007706
7707 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007708 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007709 io_ring_file_put(ctx, pfile->file);
7710 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007711 }
7712
Xiaoguang Wang05589552020-03-31 14:05:18 +08007713 percpu_ref_exit(&ref_node->refs);
7714 kfree(ref_node);
7715 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007716}
7717
Jens Axboe4a38aed22020-05-14 17:21:15 -06007718static void io_file_put_work(struct work_struct *work)
7719{
7720 struct io_ring_ctx *ctx;
7721 struct llist_node *node;
7722
7723 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7724 node = llist_del_all(&ctx->file_put_llist);
7725
7726 while (node) {
7727 struct fixed_file_ref_node *ref_node;
7728 struct llist_node *next = node->next;
7729
7730 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7731 __io_file_put_work(ref_node);
7732 node = next;
7733 }
7734}
7735
Jens Axboe05f3fb32019-12-09 11:22:50 -07007736static void io_file_data_ref_zero(struct percpu_ref *ref)
7737{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007738 struct fixed_file_ref_node *ref_node;
Pavel Begunkove2978222020-11-18 14:56:26 +00007739 struct fixed_file_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007740 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007741 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007742 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007743
Xiaoguang Wang05589552020-03-31 14:05:18 +08007744 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Pavel Begunkove2978222020-11-18 14:56:26 +00007745 data = ref_node->file_data;
7746 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007747
Jens Axboeac0648a2020-11-23 09:37:51 -07007748 spin_lock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007749 ref_node->done = true;
7750
7751 while (!list_empty(&data->ref_list)) {
7752 ref_node = list_first_entry(&data->ref_list,
7753 struct fixed_file_ref_node, node);
7754 /* recycle ref nodes in order */
7755 if (!ref_node->done)
7756 break;
7757 list_del(&ref_node->node);
7758 first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist);
7759 }
Jens Axboeac0648a2020-11-23 09:37:51 -07007760 spin_unlock_bh(&data->lock);
Pavel Begunkove2978222020-11-18 14:56:26 +00007761
7762 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007763 delay = 0;
7764
Jens Axboe4a38aed22020-05-14 17:21:15 -06007765 if (!delay)
7766 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7767 else if (first_add)
7768 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007769}
7770
7771static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7772 struct io_ring_ctx *ctx)
7773{
7774 struct fixed_file_ref_node *ref_node;
7775
7776 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7777 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007778 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007779
7780 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7781 0, GFP_KERNEL)) {
7782 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007783 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007784 }
7785 INIT_LIST_HEAD(&ref_node->node);
7786 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007787 ref_node->file_data = ctx->file_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007788 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007789 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007790}
7791
7792static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7793{
7794 percpu_ref_exit(&ref_node->refs);
7795 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007796}
7797
7798static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7799 unsigned nr_args)
7800{
7801 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007802 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007803 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007804 int fd, ret = -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007805 struct fixed_file_ref_node *ref_node;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007806 struct fixed_file_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007807
7808 if (ctx->file_data)
7809 return -EBUSY;
7810 if (!nr_args)
7811 return -EINVAL;
7812 if (nr_args > IORING_MAX_FIXED_FILES)
7813 return -EMFILE;
7814
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007815 file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7816 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007817 return -ENOMEM;
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007818 file_data->ctx = ctx;
7819 init_completion(&file_data->done);
7820 INIT_LIST_HEAD(&file_data->ref_list);
7821 spin_lock_init(&file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007822
7823 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007824 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007825 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007826 if (!file_data->table)
7827 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007828
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007829 if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007830 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
7831 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007832
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007833 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
7834 goto out_ref;
Jens Axboe55cbc252020-10-14 07:35:57 -06007835 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007836
7837 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7838 struct fixed_file_table *table;
7839 unsigned index;
7840
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007841 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7842 ret = -EFAULT;
7843 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007844 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007845 /* allow sparse sets */
7846 if (fd == -1)
7847 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007848
Jens Axboe05f3fb32019-12-09 11:22:50 -07007849 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007850 ret = -EBADF;
7851 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007852 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007853
7854 /*
7855 * Don't allow io_uring instances to be registered. If UNIX
7856 * isn't enabled, then this causes a reference cycle and this
7857 * instance can never get freed. If UNIX is enabled we'll
7858 * handle it just fine, but there's still no point in allowing
7859 * a ring fd as it doesn't support regular read/write anyway.
7860 */
7861 if (file->f_op == &io_uring_fops) {
7862 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007863 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007864 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007865 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7866 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007867 table->files[index] = file;
7868 }
7869
Jens Axboe05f3fb32019-12-09 11:22:50 -07007870 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007871 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007872 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007873 return ret;
7874 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007875
Xiaoguang Wang05589552020-03-31 14:05:18 +08007876 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007877 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007878 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007879 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007880 }
7881
Pavel Begunkov1642b442020-12-30 21:34:14 +00007882 io_sqe_files_set_node(file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007883 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007884out_fput:
7885 for (i = 0; i < ctx->nr_user_files; i++) {
7886 file = io_file_from_index(ctx, i);
7887 if (file)
7888 fput(file);
7889 }
7890 for (i = 0; i < nr_tables; i++)
7891 kfree(file_data->table[i].files);
7892 ctx->nr_user_files = 0;
7893out_ref:
7894 percpu_ref_exit(&file_data->refs);
7895out_free:
7896 kfree(file_data->table);
7897 kfree(file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007898 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007899 return ret;
7900}
7901
Jens Axboec3a31e62019-10-03 13:59:56 -06007902static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7903 int index)
7904{
7905#if defined(CONFIG_UNIX)
7906 struct sock *sock = ctx->ring_sock->sk;
7907 struct sk_buff_head *head = &sock->sk_receive_queue;
7908 struct sk_buff *skb;
7909
7910 /*
7911 * See if we can merge this file into an existing skb SCM_RIGHTS
7912 * file set. If there's no room, fall back to allocating a new skb
7913 * and filling it in.
7914 */
7915 spin_lock_irq(&head->lock);
7916 skb = skb_peek(head);
7917 if (skb) {
7918 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7919
7920 if (fpl->count < SCM_MAX_FD) {
7921 __skb_unlink(skb, head);
7922 spin_unlock_irq(&head->lock);
7923 fpl->fp[fpl->count] = get_file(file);
7924 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7925 fpl->count++;
7926 spin_lock_irq(&head->lock);
7927 __skb_queue_head(head, skb);
7928 } else {
7929 skb = NULL;
7930 }
7931 }
7932 spin_unlock_irq(&head->lock);
7933
7934 if (skb) {
7935 fput(file);
7936 return 0;
7937 }
7938
7939 return __io_sqe_files_scm(ctx, 1, index);
7940#else
7941 return 0;
7942#endif
7943}
7944
Hillf Dantona5318d32020-03-23 17:47:15 +08007945static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007946 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007947{
Hillf Dantona5318d32020-03-23 17:47:15 +08007948 struct io_file_put *pfile;
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007949 struct fixed_file_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007950
Jens Axboe05f3fb32019-12-09 11:22:50 -07007951 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007952 if (!pfile)
7953 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007954
7955 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007956 list_add(&pfile->list, &ref_node->file_list);
7957
Hillf Dantona5318d32020-03-23 17:47:15 +08007958 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007959}
7960
7961static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7962 struct io_uring_files_update *up,
7963 unsigned nr_args)
7964{
7965 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007966 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007967 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007968 __s32 __user *fds;
7969 int fd, i, err;
7970 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007971 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007972
Jens Axboe05f3fb32019-12-09 11:22:50 -07007973 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007974 return -EOVERFLOW;
7975 if (done > ctx->nr_user_files)
7976 return -EINVAL;
7977
Xiaoguang Wang05589552020-03-31 14:05:18 +08007978 ref_node = alloc_fixed_file_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007979 if (!ref_node)
7980 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007981
Jens Axboec3a31e62019-10-03 13:59:56 -06007982 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007983 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007984 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007985 struct fixed_file_table *table;
7986 unsigned index;
7987
Jens Axboec3a31e62019-10-03 13:59:56 -06007988 err = 0;
7989 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7990 err = -EFAULT;
7991 break;
7992 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007993 i = array_index_nospec(up->offset, ctx->nr_user_files);
7994 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007995 index = i & IORING_FILE_TABLE_MASK;
7996 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007997 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007998 err = io_queue_file_removal(data, file);
7999 if (err)
8000 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06008001 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008002 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008003 }
8004 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008005 file = fget(fd);
8006 if (!file) {
8007 err = -EBADF;
8008 break;
8009 }
8010 /*
8011 * Don't allow io_uring instances to be registered. If
8012 * UNIX isn't enabled, then this causes a reference
8013 * cycle and this instance can never get freed. If UNIX
8014 * is enabled we'll handle it just fine, but there's
8015 * still no point in allowing a ring fd as it doesn't
8016 * support regular read/write anyway.
8017 */
8018 if (file->f_op == &io_uring_fops) {
8019 fput(file);
8020 err = -EBADF;
8021 break;
8022 }
Jens Axboe65e19f52019-10-26 07:20:21 -06008023 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008024 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008025 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08008026 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008027 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008028 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008029 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008030 }
8031 nr_args--;
8032 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008033 up->offset++;
8034 }
8035
Xiaoguang Wang05589552020-03-31 14:05:18 +08008036 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008037 percpu_ref_kill(&data->node->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00008038 io_sqe_files_set_node(data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008039 } else
8040 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008041
8042 return done ? done : err;
8043}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008044
Jens Axboe05f3fb32019-12-09 11:22:50 -07008045static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8046 unsigned nr_args)
8047{
8048 struct io_uring_files_update up;
8049
8050 if (!ctx->file_data)
8051 return -ENXIO;
8052 if (!nr_args)
8053 return -EINVAL;
8054 if (copy_from_user(&up, arg, sizeof(up)))
8055 return -EFAULT;
8056 if (up.resv)
8057 return -EINVAL;
8058
8059 return __io_sqe_files_update(ctx, &up, nr_args);
8060}
Jens Axboec3a31e62019-10-03 13:59:56 -06008061
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008062static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008063{
8064 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8065
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008066 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07008067 io_put_req(req);
8068}
8069
Pavel Begunkov24369c22020-01-28 03:15:48 +03008070static int io_init_wq_offload(struct io_ring_ctx *ctx,
8071 struct io_uring_params *p)
8072{
8073 struct io_wq_data data;
8074 struct fd f;
8075 struct io_ring_ctx *ctx_attach;
8076 unsigned int concurrency;
8077 int ret = 0;
8078
8079 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008080 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008081 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008082
8083 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8084 /* Do QD, or 4 * CPUS, whatever is smallest */
8085 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8086
8087 ctx->io_wq = io_wq_create(concurrency, &data);
8088 if (IS_ERR(ctx->io_wq)) {
8089 ret = PTR_ERR(ctx->io_wq);
8090 ctx->io_wq = NULL;
8091 }
8092 return ret;
8093 }
8094
8095 f = fdget(p->wq_fd);
8096 if (!f.file)
8097 return -EBADF;
8098
8099 if (f.file->f_op != &io_uring_fops) {
8100 ret = -EINVAL;
8101 goto out_fput;
8102 }
8103
8104 ctx_attach = f.file->private_data;
8105 /* @io_wq is protected by holding the fd */
8106 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8107 ret = -EINVAL;
8108 goto out_fput;
8109 }
8110
8111 ctx->io_wq = ctx_attach->io_wq;
8112out_fput:
8113 fdput(f);
8114 return ret;
8115}
8116
Jens Axboe0f212202020-09-13 13:09:39 -06008117static int io_uring_alloc_task_context(struct task_struct *task)
8118{
8119 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008120 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008121
8122 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8123 if (unlikely(!tctx))
8124 return -ENOMEM;
8125
Jens Axboed8a6df12020-10-15 16:24:45 -06008126 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8127 if (unlikely(ret)) {
8128 kfree(tctx);
8129 return ret;
8130 }
8131
Jens Axboe0f212202020-09-13 13:09:39 -06008132 xa_init(&tctx->xa);
8133 init_waitqueue_head(&tctx->wait);
8134 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008135 atomic_set(&tctx->in_idle, 0);
8136 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008137 io_init_identity(&tctx->__identity);
8138 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008139 task->io_uring = tctx;
8140 return 0;
8141}
8142
8143void __io_uring_free(struct task_struct *tsk)
8144{
8145 struct io_uring_task *tctx = tsk->io_uring;
8146
8147 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008148 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8149 if (tctx->identity != &tctx->__identity)
8150 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008151 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008152 kfree(tctx);
8153 tsk->io_uring = NULL;
8154}
8155
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008156static int io_sq_offload_create(struct io_ring_ctx *ctx,
8157 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008158{
8159 int ret;
8160
Jens Axboe6c271ce2019-01-10 11:22:30 -07008161 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008162 struct io_sq_data *sqd;
8163
Jens Axboe3ec482d2019-04-08 10:51:01 -06008164 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008165 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008166 goto err;
8167
Jens Axboe534ca6d2020-09-02 13:52:19 -06008168 sqd = io_get_sq_data(p);
8169 if (IS_ERR(sqd)) {
8170 ret = PTR_ERR(sqd);
8171 goto err;
8172 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008173
Jens Axboe534ca6d2020-09-02 13:52:19 -06008174 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008175 io_sq_thread_park(sqd);
8176 mutex_lock(&sqd->ctx_lock);
8177 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8178 mutex_unlock(&sqd->ctx_lock);
8179 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008180
Jens Axboe917257d2019-04-13 09:28:55 -06008181 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8182 if (!ctx->sq_thread_idle)
8183 ctx->sq_thread_idle = HZ;
8184
Jens Axboeaa061652020-09-02 14:50:27 -06008185 if (sqd->thread)
8186 goto done;
8187
Jens Axboe6c271ce2019-01-10 11:22:30 -07008188 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008189 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008190
Jens Axboe917257d2019-04-13 09:28:55 -06008191 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008192 if (cpu >= nr_cpu_ids)
8193 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008194 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008195 goto err;
8196
Jens Axboe69fb2132020-09-14 11:16:23 -06008197 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008198 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008199 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008200 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008201 "io_uring-sq");
8202 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008203 if (IS_ERR(sqd->thread)) {
8204 ret = PTR_ERR(sqd->thread);
8205 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008206 goto err;
8207 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008208 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008209 if (ret)
8210 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008211 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8212 /* Can't have SQ_AFF without SQPOLL */
8213 ret = -EINVAL;
8214 goto err;
8215 }
8216
Jens Axboeaa061652020-09-02 14:50:27 -06008217done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008218 ret = io_init_wq_offload(ctx, p);
8219 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008220 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008221
8222 return 0;
8223err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008224 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008225 return ret;
8226}
8227
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008228static void io_sq_offload_start(struct io_ring_ctx *ctx)
8229{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008230 struct io_sq_data *sqd = ctx->sq_data;
8231
8232 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8233 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008234}
8235
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008236static inline void __io_unaccount_mem(struct user_struct *user,
8237 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008238{
8239 atomic_long_sub(nr_pages, &user->locked_vm);
8240}
8241
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008242static inline int __io_account_mem(struct user_struct *user,
8243 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008244{
8245 unsigned long page_limit, cur_pages, new_pages;
8246
8247 /* Don't allow more pages than we can safely lock */
8248 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8249
8250 do {
8251 cur_pages = atomic_long_read(&user->locked_vm);
8252 new_pages = cur_pages + nr_pages;
8253 if (new_pages > page_limit)
8254 return -ENOMEM;
8255 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8256 new_pages) != cur_pages);
8257
8258 return 0;
8259}
8260
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008261static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8262 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008263{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008264 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008265 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008266
Jens Axboe2aede0e2020-09-14 10:45:53 -06008267 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008268 if (acct == ACCT_LOCKED) {
8269 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008270 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008271 mmap_write_unlock(ctx->mm_account);
8272 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008273 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008274 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008275 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008276}
8277
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008278static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8279 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008280{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008281 int ret;
8282
8283 if (ctx->limit_mem) {
8284 ret = __io_account_mem(ctx->user, nr_pages);
8285 if (ret)
8286 return ret;
8287 }
8288
Jens Axboe2aede0e2020-09-14 10:45:53 -06008289 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008290 if (acct == ACCT_LOCKED) {
8291 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008292 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008293 mmap_write_unlock(ctx->mm_account);
8294 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008295 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008296 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008297 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008298
8299 return 0;
8300}
8301
Jens Axboe2b188cc2019-01-07 10:46:33 -07008302static void io_mem_free(void *ptr)
8303{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008304 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008305
Mark Rutland52e04ef2019-04-30 17:30:21 +01008306 if (!ptr)
8307 return;
8308
8309 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008310 if (put_page_testzero(page))
8311 free_compound_page(page);
8312}
8313
8314static void *io_mem_alloc(size_t size)
8315{
8316 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8317 __GFP_NORETRY;
8318
8319 return (void *) __get_free_pages(gfp_flags, get_order(size));
8320}
8321
Hristo Venev75b28af2019-08-26 17:23:46 +00008322static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8323 size_t *sq_offset)
8324{
8325 struct io_rings *rings;
8326 size_t off, sq_array_size;
8327
8328 off = struct_size(rings, cqes, cq_entries);
8329 if (off == SIZE_MAX)
8330 return SIZE_MAX;
8331
8332#ifdef CONFIG_SMP
8333 off = ALIGN(off, SMP_CACHE_BYTES);
8334 if (off == 0)
8335 return SIZE_MAX;
8336#endif
8337
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008338 if (sq_offset)
8339 *sq_offset = off;
8340
Hristo Venev75b28af2019-08-26 17:23:46 +00008341 sq_array_size = array_size(sizeof(u32), sq_entries);
8342 if (sq_array_size == SIZE_MAX)
8343 return SIZE_MAX;
8344
8345 if (check_add_overflow(off, sq_array_size, &off))
8346 return SIZE_MAX;
8347
Hristo Venev75b28af2019-08-26 17:23:46 +00008348 return off;
8349}
8350
Jens Axboe2b188cc2019-01-07 10:46:33 -07008351static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8352{
Hristo Venev75b28af2019-08-26 17:23:46 +00008353 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008354
Hristo Venev75b28af2019-08-26 17:23:46 +00008355 pages = (size_t)1 << get_order(
8356 rings_size(sq_entries, cq_entries, NULL));
8357 pages += (size_t)1 << get_order(
8358 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008359
Hristo Venev75b28af2019-08-26 17:23:46 +00008360 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008361}
8362
Jens Axboeedafcce2019-01-09 09:16:05 -07008363static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
8364{
8365 int i, j;
8366
8367 if (!ctx->user_bufs)
8368 return -ENXIO;
8369
8370 for (i = 0; i < ctx->nr_user_bufs; i++) {
8371 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8372
8373 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008374 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008375
Jens Axboede293932020-09-17 16:19:16 -06008376 if (imu->acct_pages)
8377 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008378 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008379 imu->nr_bvecs = 0;
8380 }
8381
8382 kfree(ctx->user_bufs);
8383 ctx->user_bufs = NULL;
8384 ctx->nr_user_bufs = 0;
8385 return 0;
8386}
8387
8388static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8389 void __user *arg, unsigned index)
8390{
8391 struct iovec __user *src;
8392
8393#ifdef CONFIG_COMPAT
8394 if (ctx->compat) {
8395 struct compat_iovec __user *ciovs;
8396 struct compat_iovec ciov;
8397
8398 ciovs = (struct compat_iovec __user *) arg;
8399 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8400 return -EFAULT;
8401
Jens Axboed55e5f52019-12-11 16:12:15 -07008402 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008403 dst->iov_len = ciov.iov_len;
8404 return 0;
8405 }
8406#endif
8407 src = (struct iovec __user *) arg;
8408 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8409 return -EFAULT;
8410 return 0;
8411}
8412
Jens Axboede293932020-09-17 16:19:16 -06008413/*
8414 * Not super efficient, but this is just a registration time. And we do cache
8415 * the last compound head, so generally we'll only do a full search if we don't
8416 * match that one.
8417 *
8418 * We check if the given compound head page has already been accounted, to
8419 * avoid double accounting it. This allows us to account the full size of the
8420 * page, not just the constituent pages of a huge page.
8421 */
8422static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8423 int nr_pages, struct page *hpage)
8424{
8425 int i, j;
8426
8427 /* check current page array */
8428 for (i = 0; i < nr_pages; i++) {
8429 if (!PageCompound(pages[i]))
8430 continue;
8431 if (compound_head(pages[i]) == hpage)
8432 return true;
8433 }
8434
8435 /* check previously registered pages */
8436 for (i = 0; i < ctx->nr_user_bufs; i++) {
8437 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8438
8439 for (j = 0; j < imu->nr_bvecs; j++) {
8440 if (!PageCompound(imu->bvec[j].bv_page))
8441 continue;
8442 if (compound_head(imu->bvec[j].bv_page) == hpage)
8443 return true;
8444 }
8445 }
8446
8447 return false;
8448}
8449
8450static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8451 int nr_pages, struct io_mapped_ubuf *imu,
8452 struct page **last_hpage)
8453{
8454 int i, ret;
8455
8456 for (i = 0; i < nr_pages; i++) {
8457 if (!PageCompound(pages[i])) {
8458 imu->acct_pages++;
8459 } else {
8460 struct page *hpage;
8461
8462 hpage = compound_head(pages[i]);
8463 if (hpage == *last_hpage)
8464 continue;
8465 *last_hpage = hpage;
8466 if (headpage_already_acct(ctx, pages, i, hpage))
8467 continue;
8468 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8469 }
8470 }
8471
8472 if (!imu->acct_pages)
8473 return 0;
8474
8475 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8476 if (ret)
8477 imu->acct_pages = 0;
8478 return ret;
8479}
8480
Jens Axboeedafcce2019-01-09 09:16:05 -07008481static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8482 unsigned nr_args)
8483{
8484 struct vm_area_struct **vmas = NULL;
8485 struct page **pages = NULL;
Jens Axboede293932020-09-17 16:19:16 -06008486 struct page *last_hpage = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008487 int i, j, got_pages = 0;
8488 int ret = -EINVAL;
8489
8490 if (ctx->user_bufs)
8491 return -EBUSY;
8492 if (!nr_args || nr_args > UIO_MAXIOV)
8493 return -EINVAL;
8494
8495 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8496 GFP_KERNEL);
8497 if (!ctx->user_bufs)
8498 return -ENOMEM;
8499
8500 for (i = 0; i < nr_args; i++) {
8501 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8502 unsigned long off, start, end, ubuf;
8503 int pret, nr_pages;
8504 struct iovec iov;
8505 size_t size;
8506
8507 ret = io_copy_iov(ctx, &iov, arg, i);
8508 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008509 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008510
8511 /*
8512 * Don't impose further limits on the size and buffer
8513 * constraints here, we'll -EINVAL later when IO is
8514 * submitted if they are wrong.
8515 */
8516 ret = -EFAULT;
8517 if (!iov.iov_base || !iov.iov_len)
8518 goto err;
8519
8520 /* arbitrary limit, but we need something */
8521 if (iov.iov_len > SZ_1G)
8522 goto err;
8523
8524 ubuf = (unsigned long) iov.iov_base;
8525 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8526 start = ubuf >> PAGE_SHIFT;
8527 nr_pages = end - start;
8528
Jens Axboeedafcce2019-01-09 09:16:05 -07008529 ret = 0;
8530 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008531 kvfree(vmas);
8532 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008533 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008534 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008535 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008536 sizeof(struct vm_area_struct *),
8537 GFP_KERNEL);
8538 if (!pages || !vmas) {
8539 ret = -ENOMEM;
Jens Axboeedafcce2019-01-09 09:16:05 -07008540 goto err;
8541 }
8542 got_pages = nr_pages;
8543 }
8544
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008545 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008546 GFP_KERNEL);
8547 ret = -ENOMEM;
Jens Axboede293932020-09-17 16:19:16 -06008548 if (!imu->bvec)
Jens Axboeedafcce2019-01-09 09:16:05 -07008549 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008550
8551 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008552 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008553 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008554 FOLL_WRITE | FOLL_LONGTERM,
8555 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008556 if (pret == nr_pages) {
8557 /* don't support file backed memory */
8558 for (j = 0; j < nr_pages; j++) {
8559 struct vm_area_struct *vma = vmas[j];
8560
8561 if (vma->vm_file &&
8562 !is_file_hugepages(vma->vm_file)) {
8563 ret = -EOPNOTSUPP;
8564 break;
8565 }
8566 }
8567 } else {
8568 ret = pret < 0 ? pret : -EFAULT;
8569 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008570 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008571 if (ret) {
8572 /*
8573 * if we did partial map, or found file backed vmas,
8574 * release any pages we did get
8575 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008576 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008577 unpin_user_pages(pages, pret);
Jens Axboede293932020-09-17 16:19:16 -06008578 kvfree(imu->bvec);
8579 goto err;
8580 }
8581
8582 ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
8583 if (ret) {
8584 unpin_user_pages(pages, pret);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008585 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008586 goto err;
8587 }
8588
8589 off = ubuf & ~PAGE_MASK;
8590 size = iov.iov_len;
8591 for (j = 0; j < nr_pages; j++) {
8592 size_t vec_len;
8593
8594 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8595 imu->bvec[j].bv_page = pages[j];
8596 imu->bvec[j].bv_len = vec_len;
8597 imu->bvec[j].bv_offset = off;
8598 off = 0;
8599 size -= vec_len;
8600 }
8601 /* store original address for later verification */
8602 imu->ubuf = ubuf;
8603 imu->len = iov.iov_len;
8604 imu->nr_bvecs = nr_pages;
8605
8606 ctx->nr_user_bufs++;
8607 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008608 kvfree(pages);
8609 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008610 return 0;
8611err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008612 kvfree(pages);
8613 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008614 io_sqe_buffer_unregister(ctx);
8615 return ret;
8616}
8617
Jens Axboe9b402842019-04-11 11:45:41 -06008618static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8619{
8620 __s32 __user *fds = arg;
8621 int fd;
8622
8623 if (ctx->cq_ev_fd)
8624 return -EBUSY;
8625
8626 if (copy_from_user(&fd, fds, sizeof(*fds)))
8627 return -EFAULT;
8628
8629 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8630 if (IS_ERR(ctx->cq_ev_fd)) {
8631 int ret = PTR_ERR(ctx->cq_ev_fd);
8632 ctx->cq_ev_fd = NULL;
8633 return ret;
8634 }
8635
8636 return 0;
8637}
8638
8639static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8640{
8641 if (ctx->cq_ev_fd) {
8642 eventfd_ctx_put(ctx->cq_ev_fd);
8643 ctx->cq_ev_fd = NULL;
8644 return 0;
8645 }
8646
8647 return -ENXIO;
8648}
8649
Jens Axboe5a2e7452020-02-23 16:23:11 -07008650static int __io_destroy_buffers(int id, void *p, void *data)
8651{
8652 struct io_ring_ctx *ctx = data;
8653 struct io_buffer *buf = p;
8654
Jens Axboe067524e2020-03-02 16:32:28 -07008655 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008656 return 0;
8657}
8658
8659static void io_destroy_buffers(struct io_ring_ctx *ctx)
8660{
8661 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8662 idr_destroy(&ctx->io_buffer_idr);
8663}
8664
Jens Axboe2b188cc2019-01-07 10:46:33 -07008665static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8666{
Jens Axboe6b063142019-01-10 22:13:58 -07008667 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008668 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008669
8670 if (ctx->sqo_task) {
8671 put_task_struct(ctx->sqo_task);
8672 ctx->sqo_task = NULL;
8673 mmdrop(ctx->mm_account);
8674 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008675 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008676
Dennis Zhou91d8f512020-09-16 13:41:05 -07008677#ifdef CONFIG_BLK_CGROUP
8678 if (ctx->sqo_blkcg_css)
8679 css_put(ctx->sqo_blkcg_css);
8680#endif
8681
Jens Axboe6b063142019-01-10 22:13:58 -07008682 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008683 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008684 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008685 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008686
Jens Axboe2b188cc2019-01-07 10:46:33 -07008687#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008688 if (ctx->ring_sock) {
8689 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008690 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008691 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008692#endif
8693
Hristo Venev75b28af2019-08-26 17:23:46 +00008694 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008695 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008696
8697 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008698 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008699 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008700 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008701 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008702 kfree(ctx);
8703}
8704
8705static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8706{
8707 struct io_ring_ctx *ctx = file->private_data;
8708 __poll_t mask = 0;
8709
8710 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008711 /*
8712 * synchronizes with barrier from wq_has_sleeper call in
8713 * io_commit_cqring
8714 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008715 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008716 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008717 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008718 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8719 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008720 mask |= EPOLLIN | EPOLLRDNORM;
8721
8722 return mask;
8723}
8724
8725static int io_uring_fasync(int fd, struct file *file, int on)
8726{
8727 struct io_ring_ctx *ctx = file->private_data;
8728
8729 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8730}
8731
Jens Axboe071698e2020-01-28 10:04:42 -07008732static int io_remove_personalities(int id, void *p, void *data)
8733{
8734 struct io_ring_ctx *ctx = data;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008735 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008736
Jens Axboe1e6fa522020-10-15 08:46:24 -06008737 iod = idr_remove(&ctx->personality_idr, id);
8738 if (iod) {
8739 put_cred(iod->creds);
8740 if (refcount_dec_and_test(&iod->count))
8741 kfree(iod);
8742 }
Jens Axboe071698e2020-01-28 10:04:42 -07008743 return 0;
8744}
8745
Jens Axboe85faa7b2020-04-09 18:14:00 -06008746static void io_ring_exit_work(struct work_struct *work)
8747{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008748 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8749 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008750
Jens Axboe56952e92020-06-17 15:00:04 -06008751 /*
8752 * If we're doing polled IO and end up having requests being
8753 * submitted async (out-of-line), then completions can come in while
8754 * we're waiting for refs to drop. We need to reap these manually,
8755 * as nobody else will be looking for them.
8756 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008757 do {
Pavel Begunkov90df0852021-01-04 20:43:30 +00008758 __io_uring_cancel_task_requests(ctx, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008759 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008760 io_ring_ctx_free(ctx);
8761}
8762
Jens Axboe00c18642020-12-20 10:45:02 -07008763static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8764{
8765 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8766
8767 return req->ctx == data;
8768}
8769
Jens Axboe2b188cc2019-01-07 10:46:33 -07008770static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8771{
8772 mutex_lock(&ctx->uring_lock);
8773 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008774
8775 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8776 ctx->sqo_dead = 1;
8777
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008778 /* if force is set, the ring is going away. always drop after that */
8779 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008780 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008781 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008782 mutex_unlock(&ctx->uring_lock);
8783
Pavel Begunkov6b819282020-11-06 13:00:25 +00008784 io_kill_timeouts(ctx, NULL, NULL);
8785 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008786
8787 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008788 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008789
Jens Axboe15dff282019-11-13 09:09:23 -07008790 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008791 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008792 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008793
8794 /*
8795 * Do this upfront, so we won't have a grace period where the ring
8796 * is closed but resources aren't reaped yet. This can cause
8797 * spurious failure in setting up a new ring.
8798 */
Jens Axboe760618f2020-07-24 12:53:31 -06008799 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8800 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008801
Jens Axboe85faa7b2020-04-09 18:14:00 -06008802 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008803 /*
8804 * Use system_unbound_wq to avoid spawning tons of event kworkers
8805 * if we're exiting a ton of rings at the same time. It just adds
8806 * noise and overhead, there's no discernable change in runtime
8807 * over using system_wq.
8808 */
8809 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008810}
8811
8812static int io_uring_release(struct inode *inode, struct file *file)
8813{
8814 struct io_ring_ctx *ctx = file->private_data;
8815
8816 file->private_data = NULL;
8817 io_ring_ctx_wait_and_kill(ctx);
8818 return 0;
8819}
8820
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008821struct io_task_cancel {
8822 struct task_struct *task;
8823 struct files_struct *files;
8824};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008825
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008826static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008827{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008828 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008829 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008830 bool ret;
8831
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008832 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008833 unsigned long flags;
8834 struct io_ring_ctx *ctx = req->ctx;
8835
8836 /* protect against races with linked timeouts */
8837 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008838 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008839 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8840 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008841 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008842 }
8843 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008844}
8845
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008846static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008847 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008848 struct files_struct *files)
8849{
8850 struct io_defer_entry *de = NULL;
8851 LIST_HEAD(list);
8852
8853 spin_lock_irq(&ctx->completion_lock);
8854 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008855 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008856 list_cut_position(&list, &ctx->defer_list, &de->list);
8857 break;
8858 }
8859 }
8860 spin_unlock_irq(&ctx->completion_lock);
8861
8862 while (!list_empty(&list)) {
8863 de = list_first_entry(&list, struct io_defer_entry, list);
8864 list_del_init(&de->list);
8865 req_set_fail_links(de->req);
8866 io_put_req(de->req);
8867 io_req_complete(de->req, -ECANCELED);
8868 kfree(de);
8869 }
8870}
8871
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008872static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008873 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008874 struct files_struct *files)
8875{
Jens Axboefcb323c2019-10-24 12:39:47 -06008876 while (!list_empty_careful(&ctx->inflight_list)) {
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008877 struct io_task_cancel cancel = { .task = task, .files = files };
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008878 struct io_kiocb *req;
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008879 DEFINE_WAIT(wait);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008880 bool found = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06008881
8882 spin_lock_irq(&ctx->inflight_lock);
8883 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe02a13672021-01-23 15:49:31 -07008884 if (!io_match_task(req, task, files))
Jens Axboe768134d2019-11-10 20:30:53 -07008885 continue;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008886 found = true;
Jens Axboe768134d2019-11-10 20:30:53 -07008887 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008888 }
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008889 if (found)
Pavel Begunkovc98de082020-11-15 12:56:32 +00008890 prepare_to_wait(&task->io_uring->wait, &wait,
8891 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008892 spin_unlock_irq(&ctx->inflight_lock);
8893
Jens Axboe768134d2019-11-10 20:30:53 -07008894 /* We need to keep going until we don't find a matching req */
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008895 if (!found)
Jens Axboefcb323c2019-10-24 12:39:47 -06008896 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008897
8898 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
8899 io_poll_remove_all(ctx, task, files);
8900 io_kill_timeouts(ctx, task, files);
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00008901 io_cqring_overflow_flush(ctx, true, task, files);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008902 /* cancellations _may_ trigger task work */
8903 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008904 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008905 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008906 }
8907}
8908
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008909static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8910 struct task_struct *task)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008911{
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008912 while (1) {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008913 struct io_task_cancel cancel = { .task = task, .files = NULL, };
Jens Axboe0f212202020-09-13 13:09:39 -06008914 enum io_wq_cancel cret;
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008915 bool ret = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008916
Pavel Begunkov90df0852021-01-04 20:43:30 +00008917 if (ctx->io_wq) {
8918 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8919 &cancel, true);
8920 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8921 }
Jens Axboe0f212202020-09-13 13:09:39 -06008922
8923 /* SQPOLL thread does its own polling */
8924 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8925 while (!list_empty_careful(&ctx->iopoll_list)) {
8926 io_iopoll_try_reap_events(ctx);
8927 ret = true;
8928 }
8929 }
8930
Pavel Begunkov6b819282020-11-06 13:00:25 +00008931 ret |= io_poll_remove_all(ctx, task, NULL);
8932 ret |= io_kill_timeouts(ctx, task, NULL);
Pavel Begunkov55583d72020-12-20 13:21:43 +00008933 ret |= io_run_task_work();
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008934 if (!ret)
8935 break;
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008936 cond_resched();
Jens Axboe0f212202020-09-13 13:09:39 -06008937 }
Jens Axboe0f212202020-09-13 13:09:39 -06008938}
8939
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008940static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8941{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008942 mutex_lock(&ctx->uring_lock);
8943 ctx->sqo_dead = 1;
8944 mutex_unlock(&ctx->uring_lock);
8945
8946 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008947 if (ctx->rings)
8948 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008949}
8950
Jens Axboe0f212202020-09-13 13:09:39 -06008951/*
8952 * We need to iteratively cancel requests, in case a request has dependent
8953 * hard links. These persist even for failure of cancelations, hence keep
8954 * looping until none are found.
8955 */
8956static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8957 struct files_struct *files)
8958{
8959 struct task_struct *task = current;
8960
Jens Axboefdaf0832020-10-30 09:37:30 -06008961 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008962 /* for SQPOLL only sqo_task has task notes */
Pavel Begunkov6b393a12021-01-16 05:32:29 +00008963 WARN_ON_ONCE(ctx->sqo_task != current);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008964 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008965 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008966 atomic_inc(&task->io_uring->in_idle);
8967 io_sq_thread_park(ctx->sq_data);
8968 }
Jens Axboe0f212202020-09-13 13:09:39 -06008969
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008970 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008971 io_cqring_overflow_flush(ctx, true, task, files);
8972
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008973 if (!files)
8974 __io_uring_cancel_task_requests(ctx, task);
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008975 else
8976 io_uring_cancel_files(ctx, task, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008977
8978 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8979 atomic_dec(&task->io_uring->in_idle);
8980 /*
8981 * If the files that are going away are the ones in the thread
8982 * identity, clear them out.
8983 */
8984 if (task->io_uring->identity->files == files)
8985 task->io_uring->identity->files = NULL;
8986 io_sq_thread_unpark(ctx->sq_data);
8987 }
Jens Axboe0f212202020-09-13 13:09:39 -06008988}
8989
8990/*
8991 * Note that this task has used io_uring. We use it for cancelation purposes.
8992 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008993static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008994{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008995 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008996 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008997
8998 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06008999 ret = io_uring_alloc_task_context(current);
9000 if (unlikely(ret))
9001 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009002 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009003 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009004 if (tctx->last != file) {
9005 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009006
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009007 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009008 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009009 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9010 file, GFP_KERNEL));
9011 if (ret) {
9012 fput(file);
9013 return ret;
9014 }
Jens Axboe0f212202020-09-13 13:09:39 -06009015 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009016 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009017 }
9018
Jens Axboefdaf0832020-10-30 09:37:30 -06009019 /*
9020 * This is race safe in that the task itself is doing this, hence it
9021 * cannot be going through the exit/cancel paths at the same time.
9022 * This cannot be modified while exit/cancel is running.
9023 */
9024 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9025 tctx->sqpoll = true;
9026
Jens Axboe0f212202020-09-13 13:09:39 -06009027 return 0;
9028}
9029
9030/*
9031 * Remove this io_uring_file -> task mapping.
9032 */
9033static void io_uring_del_task_file(struct file *file)
9034{
9035 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009036
9037 if (tctx->last == file)
9038 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009039 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009040 if (file)
9041 fput(file);
9042}
9043
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009044static void io_uring_remove_task_files(struct io_uring_task *tctx)
9045{
9046 struct file *file;
9047 unsigned long index;
9048
9049 xa_for_each(&tctx->xa, index, file)
9050 io_uring_del_task_file(file);
9051}
9052
Jens Axboe0f212202020-09-13 13:09:39 -06009053void __io_uring_files_cancel(struct files_struct *files)
9054{
9055 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009056 struct file *file;
9057 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009058
9059 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009060 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009061 xa_for_each(&tctx->xa, index, file)
9062 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009063 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009064
9065 if (files)
9066 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009067}
9068
9069static s64 tctx_inflight(struct io_uring_task *tctx)
9070{
9071 unsigned long index;
9072 struct file *file;
9073 s64 inflight;
9074
9075 inflight = percpu_counter_sum(&tctx->inflight);
9076 if (!tctx->sqpoll)
9077 return inflight;
9078
9079 /*
9080 * If we have SQPOLL rings, then we need to iterate and find them, and
9081 * add the pending count for those.
9082 */
9083 xa_for_each(&tctx->xa, index, file) {
9084 struct io_ring_ctx *ctx = file->private_data;
9085
9086 if (ctx->flags & IORING_SETUP_SQPOLL) {
9087 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
9088
9089 inflight += percpu_counter_sum(&__tctx->inflight);
9090 }
9091 }
9092
9093 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009094}
9095
Jens Axboe0f212202020-09-13 13:09:39 -06009096/*
9097 * Find any io_uring fd that this task has registered or done IO on, and cancel
9098 * requests.
9099 */
9100void __io_uring_task_cancel(void)
9101{
9102 struct io_uring_task *tctx = current->io_uring;
9103 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009104 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009105
9106 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009107 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009108
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009109 /* trigger io_disable_sqo_submit() */
9110 if (tctx->sqpoll)
9111 __io_uring_files_cancel(NULL);
9112
Jens Axboed8a6df12020-10-15 16:24:45 -06009113 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009114 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009115 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009116 if (!inflight)
9117 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009118 __io_uring_files_cancel(NULL);
9119
9120 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9121
9122 /*
9123 * If we've seen completions, retry. This avoids a race where
9124 * a completion comes in before we did prepare_to_wait().
9125 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009126 if (inflight != tctx_inflight(tctx))
Jens Axboe0f212202020-09-13 13:09:39 -06009127 continue;
Jens Axboe0f212202020-09-13 13:09:39 -06009128 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009129 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009130 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009131
Jens Axboea8d13db2021-01-15 16:04:23 -07009132 finish_wait(&tctx->wait, &wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009133 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009134
9135 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009136}
9137
Jens Axboefcb323c2019-10-24 12:39:47 -06009138static int io_uring_flush(struct file *file, void *data)
9139{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009140 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009141 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009142
Jens Axboe84965ff2021-01-23 15:51:11 -07009143 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9144 io_uring_cancel_task_requests(ctx, NULL);
9145
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009146 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009147 return 0;
9148
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009149 /* we should have cancelled and erased it before PF_EXITING */
9150 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9151 xa_load(&tctx->xa, (unsigned long)file));
9152
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009153 /*
9154 * fput() is pending, will be 2 if the only other ref is our potential
9155 * task file note. If the task is exiting, drop regardless of count.
9156 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009157 if (atomic_long_read(&file->f_count) != 2)
9158 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009159
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009160 if (ctx->flags & IORING_SETUP_SQPOLL) {
9161 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009162 WARN_ON_ONCE(ctx->sqo_task != current &&
9163 xa_load(&tctx->xa, (unsigned long)file));
9164 /* sqo_dead check is for when this happens after cancellation */
9165 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009166 !xa_load(&tctx->xa, (unsigned long)file));
9167
9168 io_disable_sqo_submit(ctx);
9169 }
9170
9171 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9172 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009173 return 0;
9174}
9175
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009176static void *io_uring_validate_mmap_request(struct file *file,
9177 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009178{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009179 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009180 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009181 struct page *page;
9182 void *ptr;
9183
9184 switch (offset) {
9185 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009186 case IORING_OFF_CQ_RING:
9187 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009188 break;
9189 case IORING_OFF_SQES:
9190 ptr = ctx->sq_sqes;
9191 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009192 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009193 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009194 }
9195
9196 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009197 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009198 return ERR_PTR(-EINVAL);
9199
9200 return ptr;
9201}
9202
9203#ifdef CONFIG_MMU
9204
9205static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9206{
9207 size_t sz = vma->vm_end - vma->vm_start;
9208 unsigned long pfn;
9209 void *ptr;
9210
9211 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9212 if (IS_ERR(ptr))
9213 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009214
9215 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9216 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9217}
9218
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009219#else /* !CONFIG_MMU */
9220
9221static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9222{
9223 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9224}
9225
9226static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9227{
9228 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9229}
9230
9231static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9232 unsigned long addr, unsigned long len,
9233 unsigned long pgoff, unsigned long flags)
9234{
9235 void *ptr;
9236
9237 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9238 if (IS_ERR(ptr))
9239 return PTR_ERR(ptr);
9240
9241 return (unsigned long) ptr;
9242}
9243
9244#endif /* !CONFIG_MMU */
9245
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009246static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009247{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009248 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009249 DEFINE_WAIT(wait);
9250
9251 do {
9252 if (!io_sqring_full(ctx))
9253 break;
9254
9255 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9256
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009257 if (unlikely(ctx->sqo_dead)) {
9258 ret = -EOWNERDEAD;
9259 goto out;
9260 }
9261
Jens Axboe90554202020-09-03 12:12:41 -06009262 if (!io_sqring_full(ctx))
9263 break;
9264
9265 schedule();
9266 } while (!signal_pending(current));
9267
9268 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009269out:
9270 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009271}
9272
Hao Xuc73ebb62020-11-03 10:54:37 +08009273static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9274 struct __kernel_timespec __user **ts,
9275 const sigset_t __user **sig)
9276{
9277 struct io_uring_getevents_arg arg;
9278
9279 /*
9280 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9281 * is just a pointer to the sigset_t.
9282 */
9283 if (!(flags & IORING_ENTER_EXT_ARG)) {
9284 *sig = (const sigset_t __user *) argp;
9285 *ts = NULL;
9286 return 0;
9287 }
9288
9289 /*
9290 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9291 * timespec and sigset_t pointers if good.
9292 */
9293 if (*argsz != sizeof(arg))
9294 return -EINVAL;
9295 if (copy_from_user(&arg, argp, sizeof(arg)))
9296 return -EFAULT;
9297 *sig = u64_to_user_ptr(arg.sigmask);
9298 *argsz = arg.sigmask_sz;
9299 *ts = u64_to_user_ptr(arg.ts);
9300 return 0;
9301}
9302
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009304 u32, min_complete, u32, flags, const void __user *, argp,
9305 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009306{
9307 struct io_ring_ctx *ctx;
9308 long ret = -EBADF;
9309 int submitted = 0;
9310 struct fd f;
9311
Jens Axboe4c6e2772020-07-01 11:29:10 -06009312 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009313
Jens Axboe90554202020-09-03 12:12:41 -06009314 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009315 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009316 return -EINVAL;
9317
9318 f = fdget(fd);
9319 if (!f.file)
9320 return -EBADF;
9321
9322 ret = -EOPNOTSUPP;
9323 if (f.file->f_op != &io_uring_fops)
9324 goto out_fput;
9325
9326 ret = -ENXIO;
9327 ctx = f.file->private_data;
9328 if (!percpu_ref_tryget(&ctx->refs))
9329 goto out_fput;
9330
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009331 ret = -EBADFD;
9332 if (ctx->flags & IORING_SETUP_R_DISABLED)
9333 goto out;
9334
Jens Axboe6c271ce2019-01-10 11:22:30 -07009335 /*
9336 * For SQ polling, the thread will do all submissions and completions.
9337 * Just return the requested submit count, and wake the thread if
9338 * we were asked to.
9339 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009340 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009341 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009342 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009343
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009344 ret = -EOWNERDEAD;
9345 if (unlikely(ctx->sqo_dead))
9346 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009347 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009348 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009349 if (flags & IORING_ENTER_SQ_WAIT) {
9350 ret = io_sqpoll_wait_sq(ctx);
9351 if (ret)
9352 goto out;
9353 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009354 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009355 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009356 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009357 if (unlikely(ret))
9358 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009359 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009360 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009361 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009362
9363 if (submitted != to_submit)
9364 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009365 }
9366 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009367 const sigset_t __user *sig;
9368 struct __kernel_timespec __user *ts;
9369
9370 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9371 if (unlikely(ret))
9372 goto out;
9373
Jens Axboe2b188cc2019-01-07 10:46:33 -07009374 min_complete = min(min_complete, ctx->cq_entries);
9375
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009376 /*
9377 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9378 * space applications don't need to do io completion events
9379 * polling again, they can rely on io_sq_thread to do polling
9380 * work, which can reduce cpu usage and uring_lock contention.
9381 */
9382 if (ctx->flags & IORING_SETUP_IOPOLL &&
9383 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009384 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009385 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009386 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009387 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009388 }
9389
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009390out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009391 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009392out_fput:
9393 fdput(f);
9394 return submitted ? submitted : ret;
9395}
9396
Tobias Klauserbebdb652020-02-26 18:38:32 +01009397#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009398static int io_uring_show_cred(int id, void *p, void *data)
9399{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009400 struct io_identity *iod = p;
9401 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009402 struct seq_file *m = data;
9403 struct user_namespace *uns = seq_user_ns(m);
9404 struct group_info *gi;
9405 kernel_cap_t cap;
9406 unsigned __capi;
9407 int g;
9408
9409 seq_printf(m, "%5d\n", id);
9410 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9411 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9412 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9413 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9414 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9415 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9416 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9417 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9418 seq_puts(m, "\n\tGroups:\t");
9419 gi = cred->group_info;
9420 for (g = 0; g < gi->ngroups; g++) {
9421 seq_put_decimal_ull(m, g ? " " : "",
9422 from_kgid_munged(uns, gi->gid[g]));
9423 }
9424 seq_puts(m, "\n\tCapEff:\t");
9425 cap = cred->cap_effective;
9426 CAP_FOR_EACH_U32(__capi)
9427 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9428 seq_putc(m, '\n');
9429 return 0;
9430}
9431
9432static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9433{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009434 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009435 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009436 int i;
9437
Jens Axboefad8e0d2020-09-28 08:57:48 -06009438 /*
9439 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9440 * since fdinfo case grabs it in the opposite direction of normal use
9441 * cases. If we fail to get the lock, we just don't iterate any
9442 * structures that could be going away outside the io_uring mutex.
9443 */
9444 has_lock = mutex_trylock(&ctx->uring_lock);
9445
Joseph Qidbbe9c62020-09-29 09:01:22 -06009446 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9447 sq = ctx->sq_data;
9448
9449 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9450 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009451 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009452 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009453 struct fixed_file_table *table;
9454 struct file *f;
9455
9456 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9457 f = table->files[i & IORING_FILE_TABLE_MASK];
9458 if (f)
9459 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9460 else
9461 seq_printf(m, "%5u: <none>\n", i);
9462 }
9463 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009464 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009465 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9466
9467 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9468 (unsigned int) buf->len);
9469 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009470 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009471 seq_printf(m, "Personalities:\n");
9472 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9473 }
Jens Axboed7718a92020-02-14 22:23:12 -07009474 seq_printf(m, "PollList:\n");
9475 spin_lock_irq(&ctx->completion_lock);
9476 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9477 struct hlist_head *list = &ctx->cancel_hash[i];
9478 struct io_kiocb *req;
9479
9480 hlist_for_each_entry(req, list, hash_node)
9481 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9482 req->task->task_works != NULL);
9483 }
9484 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009485 if (has_lock)
9486 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009487}
9488
9489static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9490{
9491 struct io_ring_ctx *ctx = f->private_data;
9492
9493 if (percpu_ref_tryget(&ctx->refs)) {
9494 __io_uring_show_fdinfo(ctx, m);
9495 percpu_ref_put(&ctx->refs);
9496 }
9497}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009498#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009499
Jens Axboe2b188cc2019-01-07 10:46:33 -07009500static const struct file_operations io_uring_fops = {
9501 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009502 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009503 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009504#ifndef CONFIG_MMU
9505 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9506 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9507#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009508 .poll = io_uring_poll,
9509 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009510#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009511 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009512#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009513};
9514
9515static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9516 struct io_uring_params *p)
9517{
Hristo Venev75b28af2019-08-26 17:23:46 +00009518 struct io_rings *rings;
9519 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009520
Jens Axboebd740482020-08-05 12:58:23 -06009521 /* make sure these are sane, as we already accounted them */
9522 ctx->sq_entries = p->sq_entries;
9523 ctx->cq_entries = p->cq_entries;
9524
Hristo Venev75b28af2019-08-26 17:23:46 +00009525 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9526 if (size == SIZE_MAX)
9527 return -EOVERFLOW;
9528
9529 rings = io_mem_alloc(size);
9530 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009531 return -ENOMEM;
9532
Hristo Venev75b28af2019-08-26 17:23:46 +00009533 ctx->rings = rings;
9534 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9535 rings->sq_ring_mask = p->sq_entries - 1;
9536 rings->cq_ring_mask = p->cq_entries - 1;
9537 rings->sq_ring_entries = p->sq_entries;
9538 rings->cq_ring_entries = p->cq_entries;
9539 ctx->sq_mask = rings->sq_ring_mask;
9540 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009541
9542 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009543 if (size == SIZE_MAX) {
9544 io_mem_free(ctx->rings);
9545 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009546 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009547 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009548
9549 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009550 if (!ctx->sq_sqes) {
9551 io_mem_free(ctx->rings);
9552 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009553 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009554 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009555
Jens Axboe2b188cc2019-01-07 10:46:33 -07009556 return 0;
9557}
9558
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009559static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9560{
9561 int ret, fd;
9562
9563 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9564 if (fd < 0)
9565 return fd;
9566
9567 ret = io_uring_add_task_file(ctx, file);
9568 if (ret) {
9569 put_unused_fd(fd);
9570 return ret;
9571 }
9572 fd_install(fd, file);
9573 return fd;
9574}
9575
Jens Axboe2b188cc2019-01-07 10:46:33 -07009576/*
9577 * Allocate an anonymous fd, this is what constitutes the application
9578 * visible backing of an io_uring instance. The application mmaps this
9579 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9580 * we have to tie this fd to a socket for file garbage collection purposes.
9581 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009582static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009583{
9584 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009585#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009586 int ret;
9587
Jens Axboe2b188cc2019-01-07 10:46:33 -07009588 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9589 &ctx->ring_sock);
9590 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009591 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009592#endif
9593
Jens Axboe2b188cc2019-01-07 10:46:33 -07009594 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9595 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009596#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009597 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009598 sock_release(ctx->ring_sock);
9599 ctx->ring_sock = NULL;
9600 } else {
9601 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009602 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009603#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009604 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009605}
9606
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009607static int io_uring_create(unsigned entries, struct io_uring_params *p,
9608 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009609{
9610 struct user_struct *user = NULL;
9611 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009612 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009613 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009614 int ret;
9615
Jens Axboe8110c1a2019-12-28 15:39:54 -07009616 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009617 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009618 if (entries > IORING_MAX_ENTRIES) {
9619 if (!(p->flags & IORING_SETUP_CLAMP))
9620 return -EINVAL;
9621 entries = IORING_MAX_ENTRIES;
9622 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009623
9624 /*
9625 * Use twice as many entries for the CQ ring. It's possible for the
9626 * application to drive a higher depth than the size of the SQ ring,
9627 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009628 * some flexibility in overcommitting a bit. If the application has
9629 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9630 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009631 */
9632 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009633 if (p->flags & IORING_SETUP_CQSIZE) {
9634 /*
9635 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9636 * to a power-of-two, if it isn't already. We do NOT impose
9637 * any cq vs sq ring sizing.
9638 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009639 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009640 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009641 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9642 if (!(p->flags & IORING_SETUP_CLAMP))
9643 return -EINVAL;
9644 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9645 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009646 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9647 if (p->cq_entries < p->sq_entries)
9648 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009649 } else {
9650 p->cq_entries = 2 * p->sq_entries;
9651 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009652
9653 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009654 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009655
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009656 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009657 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009658 ring_pages(p->sq_entries, p->cq_entries));
9659 if (ret) {
9660 free_uid(user);
9661 return ret;
9662 }
9663 }
9664
9665 ctx = io_ring_ctx_alloc(p);
9666 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009667 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009668 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009669 p->cq_entries));
9670 free_uid(user);
9671 return -ENOMEM;
9672 }
9673 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009674 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009675 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009676#ifdef CONFIG_AUDIT
9677 ctx->loginuid = current->loginuid;
9678 ctx->sessionid = current->sessionid;
9679#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009680 ctx->sqo_task = get_task_struct(current);
9681
9682 /*
9683 * This is just grabbed for accounting purposes. When a process exits,
9684 * the mm is exited and dropped before the files, hence we need to hang
9685 * on to this mm purely for the purposes of being able to unaccount
9686 * memory (locked/pinned vm). It's not used for anything else.
9687 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009688 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009689 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009690
Dennis Zhou91d8f512020-09-16 13:41:05 -07009691#ifdef CONFIG_BLK_CGROUP
9692 /*
9693 * The sq thread will belong to the original cgroup it was inited in.
9694 * If the cgroup goes offline (e.g. disabling the io controller), then
9695 * issued bios will be associated with the closest cgroup later in the
9696 * block layer.
9697 */
9698 rcu_read_lock();
9699 ctx->sqo_blkcg_css = blkcg_css();
9700 ret = css_tryget_online(ctx->sqo_blkcg_css);
9701 rcu_read_unlock();
9702 if (!ret) {
9703 /* don't init against a dying cgroup, have the user try again */
9704 ctx->sqo_blkcg_css = NULL;
9705 ret = -ENODEV;
9706 goto err;
9707 }
9708#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009709
Jens Axboe2b188cc2019-01-07 10:46:33 -07009710 /*
9711 * Account memory _before_ installing the file descriptor. Once
9712 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009713 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009714 * will un-account as well.
9715 */
9716 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9717 ACCT_LOCKED);
9718 ctx->limit_mem = limit_mem;
9719
9720 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009721 if (ret)
9722 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009723
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009724 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009725 if (ret)
9726 goto err;
9727
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009728 if (!(p->flags & IORING_SETUP_R_DISABLED))
9729 io_sq_offload_start(ctx);
9730
Jens Axboe2b188cc2019-01-07 10:46:33 -07009731 memset(&p->sq_off, 0, sizeof(p->sq_off));
9732 p->sq_off.head = offsetof(struct io_rings, sq.head);
9733 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9734 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9735 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9736 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9737 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9738 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9739
9740 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009741 p->cq_off.head = offsetof(struct io_rings, cq.head);
9742 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9743 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9744 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9745 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9746 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009747 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009748
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009749 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9750 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009751 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009752 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9753 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009754
9755 if (copy_to_user(params, p, sizeof(*p))) {
9756 ret = -EFAULT;
9757 goto err;
9758 }
Jens Axboed1719f72020-07-30 13:43:53 -06009759
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009760 file = io_uring_get_file(ctx);
9761 if (IS_ERR(file)) {
9762 ret = PTR_ERR(file);
9763 goto err;
9764 }
9765
Jens Axboed1719f72020-07-30 13:43:53 -06009766 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009767 * Install ring fd as the very last thing, so we don't risk someone
9768 * having closed it before we finish setup
9769 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009770 ret = io_uring_install_fd(ctx, file);
9771 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009772 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009773 /* fput will clean it up */
9774 fput(file);
9775 return ret;
9776 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009777
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009778 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009779 return ret;
9780err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009781 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009782 io_ring_ctx_wait_and_kill(ctx);
9783 return ret;
9784}
9785
9786/*
9787 * Sets up an aio uring context, and returns the fd. Applications asks for a
9788 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9789 * params structure passed in.
9790 */
9791static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9792{
9793 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009794 int i;
9795
9796 if (copy_from_user(&p, params, sizeof(p)))
9797 return -EFAULT;
9798 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9799 if (p.resv[i])
9800 return -EINVAL;
9801 }
9802
Jens Axboe6c271ce2019-01-10 11:22:30 -07009803 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009804 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009805 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9806 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009807 return -EINVAL;
9808
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009809 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009810}
9811
9812SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9813 struct io_uring_params __user *, params)
9814{
9815 return io_uring_setup(entries, params);
9816}
9817
Jens Axboe66f4af92020-01-16 15:36:52 -07009818static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9819{
9820 struct io_uring_probe *p;
9821 size_t size;
9822 int i, ret;
9823
9824 size = struct_size(p, ops, nr_args);
9825 if (size == SIZE_MAX)
9826 return -EOVERFLOW;
9827 p = kzalloc(size, GFP_KERNEL);
9828 if (!p)
9829 return -ENOMEM;
9830
9831 ret = -EFAULT;
9832 if (copy_from_user(p, arg, size))
9833 goto out;
9834 ret = -EINVAL;
9835 if (memchr_inv(p, 0, size))
9836 goto out;
9837
9838 p->last_op = IORING_OP_LAST - 1;
9839 if (nr_args > IORING_OP_LAST)
9840 nr_args = IORING_OP_LAST;
9841
9842 for (i = 0; i < nr_args; i++) {
9843 p->ops[i].op = i;
9844 if (!io_op_defs[i].not_supported)
9845 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9846 }
9847 p->ops_len = i;
9848
9849 ret = 0;
9850 if (copy_to_user(arg, p, size))
9851 ret = -EFAULT;
9852out:
9853 kfree(p);
9854 return ret;
9855}
9856
Jens Axboe071698e2020-01-28 10:04:42 -07009857static int io_register_personality(struct io_ring_ctx *ctx)
9858{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009859 struct io_identity *id;
9860 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009861
Jens Axboe1e6fa522020-10-15 08:46:24 -06009862 id = kmalloc(sizeof(*id), GFP_KERNEL);
9863 if (unlikely(!id))
9864 return -ENOMEM;
9865
9866 io_init_identity(id);
9867 id->creds = get_current_cred();
9868
9869 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9870 if (ret < 0) {
9871 put_cred(id->creds);
9872 kfree(id);
9873 }
9874 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009875}
9876
9877static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9878{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009879 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07009880
Jens Axboe1e6fa522020-10-15 08:46:24 -06009881 iod = idr_remove(&ctx->personality_idr, id);
9882 if (iod) {
9883 put_cred(iod->creds);
9884 if (refcount_dec_and_test(&iod->count))
9885 kfree(iod);
Jens Axboe071698e2020-01-28 10:04:42 -07009886 return 0;
9887 }
9888
9889 return -EINVAL;
9890}
9891
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009892static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9893 unsigned int nr_args)
9894{
9895 struct io_uring_restriction *res;
9896 size_t size;
9897 int i, ret;
9898
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009899 /* Restrictions allowed only if rings started disabled */
9900 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9901 return -EBADFD;
9902
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009903 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009904 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009905 return -EBUSY;
9906
9907 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9908 return -EINVAL;
9909
9910 size = array_size(nr_args, sizeof(*res));
9911 if (size == SIZE_MAX)
9912 return -EOVERFLOW;
9913
9914 res = memdup_user(arg, size);
9915 if (IS_ERR(res))
9916 return PTR_ERR(res);
9917
9918 ret = 0;
9919
9920 for (i = 0; i < nr_args; i++) {
9921 switch (res[i].opcode) {
9922 case IORING_RESTRICTION_REGISTER_OP:
9923 if (res[i].register_op >= IORING_REGISTER_LAST) {
9924 ret = -EINVAL;
9925 goto out;
9926 }
9927
9928 __set_bit(res[i].register_op,
9929 ctx->restrictions.register_op);
9930 break;
9931 case IORING_RESTRICTION_SQE_OP:
9932 if (res[i].sqe_op >= IORING_OP_LAST) {
9933 ret = -EINVAL;
9934 goto out;
9935 }
9936
9937 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9938 break;
9939 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9940 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9941 break;
9942 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9943 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9944 break;
9945 default:
9946 ret = -EINVAL;
9947 goto out;
9948 }
9949 }
9950
9951out:
9952 /* Reset all restrictions if an error happened */
9953 if (ret != 0)
9954 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9955 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009956 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009957
9958 kfree(res);
9959 return ret;
9960}
9961
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009962static int io_register_enable_rings(struct io_ring_ctx *ctx)
9963{
9964 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9965 return -EBADFD;
9966
9967 if (ctx->restrictions.registered)
9968 ctx->restricted = 1;
9969
9970 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9971
9972 io_sq_offload_start(ctx);
9973
9974 return 0;
9975}
9976
Jens Axboe071698e2020-01-28 10:04:42 -07009977static bool io_register_op_must_quiesce(int op)
9978{
9979 switch (op) {
9980 case IORING_UNREGISTER_FILES:
9981 case IORING_REGISTER_FILES_UPDATE:
9982 case IORING_REGISTER_PROBE:
9983 case IORING_REGISTER_PERSONALITY:
9984 case IORING_UNREGISTER_PERSONALITY:
9985 return false;
9986 default:
9987 return true;
9988 }
9989}
9990
Jens Axboeedafcce2019-01-09 09:16:05 -07009991static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9992 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009993 __releases(ctx->uring_lock)
9994 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009995{
9996 int ret;
9997
Jens Axboe35fa71a2019-04-22 10:23:23 -06009998 /*
9999 * We're inside the ring mutex, if the ref is already dying, then
10000 * someone else killed the ctx or is already going through
10001 * io_uring_register().
10002 */
10003 if (percpu_ref_is_dying(&ctx->refs))
10004 return -ENXIO;
10005
Jens Axboe071698e2020-01-28 10:04:42 -070010006 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010007 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010008
Jens Axboe05f3fb32019-12-09 11:22:50 -070010009 /*
10010 * Drop uring mutex before waiting for references to exit. If
10011 * another thread is currently inside io_uring_enter() it might
10012 * need to grab the uring_lock to make progress. If we hold it
10013 * here across the drain wait, then we can deadlock. It's safe
10014 * to drop the mutex here, since no new references will come in
10015 * after we've killed the percpu ref.
10016 */
10017 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010018 do {
10019 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10020 if (!ret)
10021 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010022 ret = io_run_task_work_sig();
10023 if (ret < 0)
10024 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010025 } while (1);
10026
Jens Axboe05f3fb32019-12-09 11:22:50 -070010027 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010028
Jens Axboec1503682020-01-08 08:26:07 -070010029 if (ret) {
10030 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010031 goto out_quiesce;
10032 }
10033 }
10034
10035 if (ctx->restricted) {
10036 if (opcode >= IORING_REGISTER_LAST) {
10037 ret = -EINVAL;
10038 goto out;
10039 }
10040
10041 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10042 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010043 goto out;
10044 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010045 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010046
10047 switch (opcode) {
10048 case IORING_REGISTER_BUFFERS:
10049 ret = io_sqe_buffer_register(ctx, arg, nr_args);
10050 break;
10051 case IORING_UNREGISTER_BUFFERS:
10052 ret = -EINVAL;
10053 if (arg || nr_args)
10054 break;
10055 ret = io_sqe_buffer_unregister(ctx);
10056 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010057 case IORING_REGISTER_FILES:
10058 ret = io_sqe_files_register(ctx, arg, nr_args);
10059 break;
10060 case IORING_UNREGISTER_FILES:
10061 ret = -EINVAL;
10062 if (arg || nr_args)
10063 break;
10064 ret = io_sqe_files_unregister(ctx);
10065 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010066 case IORING_REGISTER_FILES_UPDATE:
10067 ret = io_sqe_files_update(ctx, arg, nr_args);
10068 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010069 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010070 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010071 ret = -EINVAL;
10072 if (nr_args != 1)
10073 break;
10074 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010075 if (ret)
10076 break;
10077 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10078 ctx->eventfd_async = 1;
10079 else
10080 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010081 break;
10082 case IORING_UNREGISTER_EVENTFD:
10083 ret = -EINVAL;
10084 if (arg || nr_args)
10085 break;
10086 ret = io_eventfd_unregister(ctx);
10087 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010088 case IORING_REGISTER_PROBE:
10089 ret = -EINVAL;
10090 if (!arg || nr_args > 256)
10091 break;
10092 ret = io_probe(ctx, arg, nr_args);
10093 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010094 case IORING_REGISTER_PERSONALITY:
10095 ret = -EINVAL;
10096 if (arg || nr_args)
10097 break;
10098 ret = io_register_personality(ctx);
10099 break;
10100 case IORING_UNREGISTER_PERSONALITY:
10101 ret = -EINVAL;
10102 if (arg)
10103 break;
10104 ret = io_unregister_personality(ctx, nr_args);
10105 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010106 case IORING_REGISTER_ENABLE_RINGS:
10107 ret = -EINVAL;
10108 if (arg || nr_args)
10109 break;
10110 ret = io_register_enable_rings(ctx);
10111 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010112 case IORING_REGISTER_RESTRICTIONS:
10113 ret = io_register_restrictions(ctx, arg, nr_args);
10114 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010115 default:
10116 ret = -EINVAL;
10117 break;
10118 }
10119
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010120out:
Jens Axboe071698e2020-01-28 10:04:42 -070010121 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010122 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010123 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010124out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010125 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010126 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010127 return ret;
10128}
10129
10130SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10131 void __user *, arg, unsigned int, nr_args)
10132{
10133 struct io_ring_ctx *ctx;
10134 long ret = -EBADF;
10135 struct fd f;
10136
10137 f = fdget(fd);
10138 if (!f.file)
10139 return -EBADF;
10140
10141 ret = -EOPNOTSUPP;
10142 if (f.file->f_op != &io_uring_fops)
10143 goto out_fput;
10144
10145 ctx = f.file->private_data;
10146
10147 mutex_lock(&ctx->uring_lock);
10148 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10149 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010150 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10151 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010152out_fput:
10153 fdput(f);
10154 return ret;
10155}
10156
Jens Axboe2b188cc2019-01-07 10:46:33 -070010157static int __init io_uring_init(void)
10158{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010159#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10160 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10161 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10162} while (0)
10163
10164#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10165 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10166 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10167 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10168 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10169 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10170 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10171 BUILD_BUG_SQE_ELEM(8, __u64, off);
10172 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10173 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010174 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010175 BUILD_BUG_SQE_ELEM(24, __u32, len);
10176 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10177 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10178 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10179 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010180 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10181 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010182 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10183 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10184 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10185 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10186 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10187 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10188 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10189 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010190 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010191 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10192 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10193 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010194 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010195
Jens Axboed3656342019-12-18 09:50:26 -070010196 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010197 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010198 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10199 return 0;
10200};
10201__initcall(io_uring_init);