blob: 5e576878efd91de2b05cc3c21ce4f74ec92c7da6 [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
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000198struct io_ring_ctx;
199
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000200struct io_rsrc_put {
201 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000202 union {
203 void *rsrc;
204 struct file *file;
205 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000206};
207
208struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600209 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700210};
211
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000212struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800213 struct percpu_ref refs;
214 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000215 struct list_head rsrc_list;
216 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000217 void (*rsrc_put)(struct io_ring_ctx *ctx,
218 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600219 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000220 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800221};
222
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000223struct fixed_rsrc_data {
224 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700225 struct io_ring_ctx *ctx;
226
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000227 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700228 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700229 struct completion done;
230};
231
Jens Axboe5a2e7452020-02-23 16:23:11 -0700232struct io_buffer {
233 struct list_head list;
234 __u64 addr;
235 __s32 len;
236 __u16 bid;
237};
238
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200239struct io_restriction {
240 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
241 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
242 u8 sqe_flags_allowed;
243 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200244 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200245};
246
Jens Axboe534ca6d2020-09-02 13:52:19 -0600247struct io_sq_data {
248 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600249 struct mutex lock;
250
251 /* ctx's that are using this sqd */
252 struct list_head ctx_list;
253 struct list_head ctx_new_list;
254 struct mutex ctx_lock;
255
Jens Axboe534ca6d2020-09-02 13:52:19 -0600256 struct task_struct *thread;
257 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800258
259 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600260};
261
Jens Axboe2b188cc2019-01-07 10:46:33 -0700262struct io_ring_ctx {
263 struct {
264 struct percpu_ref refs;
265 } ____cacheline_aligned_in_smp;
266
267 struct {
268 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800269 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700270 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800271 unsigned int cq_overflow_flushed: 1;
272 unsigned int drain_next: 1;
273 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200274 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000275 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276
Hristo Venev75b28af2019-08-26 17:23:46 +0000277 /*
278 * Ring buffer of indices into array of io_uring_sqe, which is
279 * mmapped by the application using the IORING_OFF_SQES offset.
280 *
281 * This indirection could e.g. be used to assign fixed
282 * io_uring_sqe entries to operations and only submit them to
283 * the queue when needed.
284 *
285 * The kernel modifies neither the indices array nor the entries
286 * array.
287 */
288 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289 unsigned cached_sq_head;
290 unsigned sq_entries;
291 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700292 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600293 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100294 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700295 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600296
297 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600298 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700299 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700300
Jens Axboead3eb2c2019-12-18 17:12:20 -0700301 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700302 } ____cacheline_aligned_in_smp;
303
Hristo Venev75b28af2019-08-26 17:23:46 +0000304 struct io_rings *rings;
305
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600307 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600308
309 /*
310 * For SQPOLL usage - we hold a reference to the parent task, so we
311 * have access to the ->files
312 */
313 struct task_struct *sqo_task;
314
315 /* Only used for accounting purposes */
316 struct mm_struct *mm_account;
317
Dennis Zhou91d8f512020-09-16 13:41:05 -0700318#ifdef CONFIG_BLK_CGROUP
319 struct cgroup_subsys_state *sqo_blkcg_css;
320#endif
321
Jens Axboe534ca6d2020-09-02 13:52:19 -0600322 struct io_sq_data *sq_data; /* if using sq thread polling */
323
Jens Axboe90554202020-09-03 12:12:41 -0600324 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600325 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700326
Jens Axboe6b063142019-01-10 22:13:58 -0700327 /*
328 * If used, fixed file set. Writers must ensure that ->refs is dead,
329 * readers must ensure that ->refs is alive as long as the file* is
330 * used. Only updated through io_uring_register(2).
331 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000332 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700333 unsigned nr_user_files;
334
Jens Axboeedafcce2019-01-09 09:16:05 -0700335 /* if used, fixed mapped user buffers */
336 unsigned nr_user_bufs;
337 struct io_mapped_ubuf *user_bufs;
338
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339 struct user_struct *user;
340
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700341 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700342
Jens Axboe4ea33a92020-10-15 13:46:44 -0600343#ifdef CONFIG_AUDIT
344 kuid_t loginuid;
345 unsigned int sessionid;
346#endif
347
Jens Axboe0f158b42020-05-14 17:18:39 -0600348 struct completion ref_comp;
349 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700350
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700351 /* if all else fails... */
352 struct io_kiocb *fallback_req;
353
Jens Axboe206aefd2019-11-07 18:27:42 -0700354#if defined(CONFIG_UNIX)
355 struct socket *ring_sock;
356#endif
357
Jens Axboe5a2e7452020-02-23 16:23:11 -0700358 struct idr io_buffer_idr;
359
Jens Axboe071698e2020-01-28 10:04:42 -0700360 struct idr personality_idr;
361
Jens Axboe206aefd2019-11-07 18:27:42 -0700362 struct {
363 unsigned cached_cq_tail;
364 unsigned cq_entries;
365 unsigned cq_mask;
366 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500367 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700368 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700369 struct wait_queue_head cq_wait;
370 struct fasync_struct *cq_fasync;
371 struct eventfd_ctx *cq_ev_fd;
372 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700373
374 struct {
375 struct mutex uring_lock;
376 wait_queue_head_t wait;
377 } ____cacheline_aligned_in_smp;
378
379 struct {
380 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700381
Jens Axboedef596e2019-01-09 08:59:42 -0700382 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300383 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700384 * io_uring instances that don't use IORING_SETUP_SQPOLL.
385 * For SQPOLL, only the single threaded io_sq_thread() will
386 * manipulate the list, hence no extra locking is needed there.
387 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300388 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700389 struct hlist_head *cancel_hash;
390 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700391 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600392
393 spinlock_t inflight_lock;
394 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700395 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600396
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000397 struct delayed_work rsrc_put_work;
398 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000399 struct list_head rsrc_ref_list;
400 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600401
Jens Axboe85faa7b2020-04-09 18:14:00 -0600402 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200403 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700404};
405
Jens Axboe09bb8392019-03-13 12:39:28 -0600406/*
407 * First field must be the file pointer in all the
408 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
409 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700410struct io_poll_iocb {
411 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000412 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700413 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600414 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700415 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700416 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700417};
418
Pavel Begunkov018043b2020-10-27 23:17:18 +0000419struct io_poll_remove {
420 struct file *file;
421 u64 addr;
422};
423
Jens Axboeb5dba592019-12-11 14:02:38 -0700424struct io_close {
425 struct file *file;
426 struct file *put_file;
427 int fd;
428};
429
Jens Axboead8a48a2019-11-15 08:49:11 -0700430struct io_timeout_data {
431 struct io_kiocb *req;
432 struct hrtimer timer;
433 struct timespec64 ts;
434 enum hrtimer_mode mode;
435};
436
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700437struct io_accept {
438 struct file *file;
439 struct sockaddr __user *addr;
440 int __user *addr_len;
441 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600442 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700443};
444
445struct io_sync {
446 struct file *file;
447 loff_t len;
448 loff_t off;
449 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700450 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700451};
452
Jens Axboefbf23842019-12-17 18:45:56 -0700453struct io_cancel {
454 struct file *file;
455 u64 addr;
456};
457
Jens Axboeb29472e2019-12-17 18:50:29 -0700458struct io_timeout {
459 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300460 u32 off;
461 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300462 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000463 /* head of the link, used by linked timeouts only */
464 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700465};
466
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100467struct io_timeout_rem {
468 struct file *file;
469 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000470
471 /* timeout update */
472 struct timespec64 ts;
473 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100474};
475
Jens Axboe9adbd452019-12-20 08:45:55 -0700476struct io_rw {
477 /* NOTE: kiocb has the file as the first member, so don't do it here */
478 struct kiocb kiocb;
479 u64 addr;
480 u64 len;
481};
482
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700483struct io_connect {
484 struct file *file;
485 struct sockaddr __user *addr;
486 int addr_len;
487};
488
Jens Axboee47293f2019-12-20 08:58:21 -0700489struct io_sr_msg {
490 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700491 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300492 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700493 void __user *buf;
494 };
Jens Axboee47293f2019-12-20 08:58:21 -0700495 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700496 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700497 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700498 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700499};
500
Jens Axboe15b71ab2019-12-11 11:20:36 -0700501struct io_open {
502 struct file *file;
503 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700504 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700505 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600506 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700507};
508
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000509struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700510 struct file *file;
511 u64 arg;
512 u32 nr_args;
513 u32 offset;
514};
515
Jens Axboe4840e412019-12-25 22:03:45 -0700516struct io_fadvise {
517 struct file *file;
518 u64 offset;
519 u32 len;
520 u32 advice;
521};
522
Jens Axboec1ca7572019-12-25 22:18:28 -0700523struct io_madvise {
524 struct file *file;
525 u64 addr;
526 u32 len;
527 u32 advice;
528};
529
Jens Axboe3e4827b2020-01-08 15:18:09 -0700530struct io_epoll {
531 struct file *file;
532 int epfd;
533 int op;
534 int fd;
535 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700536};
537
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300538struct io_splice {
539 struct file *file_out;
540 struct file *file_in;
541 loff_t off_out;
542 loff_t off_in;
543 u64 len;
544 unsigned int flags;
545};
546
Jens Axboeddf0322d2020-02-23 16:41:33 -0700547struct io_provide_buf {
548 struct file *file;
549 __u64 addr;
550 __s32 len;
551 __u32 bgid;
552 __u16 nbufs;
553 __u16 bid;
554};
555
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700556struct io_statx {
557 struct file *file;
558 int dfd;
559 unsigned int mask;
560 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700561 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700562 struct statx __user *buffer;
563};
564
Jens Axboe36f4fa62020-09-05 11:14:22 -0600565struct io_shutdown {
566 struct file *file;
567 int how;
568};
569
Jens Axboe80a261f2020-09-28 14:23:58 -0600570struct io_rename {
571 struct file *file;
572 int old_dfd;
573 int new_dfd;
574 struct filename *oldpath;
575 struct filename *newpath;
576 int flags;
577};
578
Jens Axboe14a11432020-09-28 14:27:37 -0600579struct io_unlink {
580 struct file *file;
581 int dfd;
582 int flags;
583 struct filename *filename;
584};
585
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300586struct io_completion {
587 struct file *file;
588 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300589 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300590};
591
Jens Axboef499a022019-12-02 16:28:46 -0700592struct io_async_connect {
593 struct sockaddr_storage address;
594};
595
Jens Axboe03b12302019-12-02 18:50:25 -0700596struct io_async_msghdr {
597 struct iovec fast_iov[UIO_FASTIOV];
598 struct iovec *iov;
599 struct sockaddr __user *uaddr;
600 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700601 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700602};
603
Jens Axboef67676d2019-12-02 11:03:47 -0700604struct io_async_rw {
605 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600606 const struct iovec *free_iovec;
607 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600608 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600609 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700610};
611
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300612enum {
613 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
614 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
615 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
616 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
617 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700618 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300619
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300620 REQ_F_FAIL_LINK_BIT,
621 REQ_F_INFLIGHT_BIT,
622 REQ_F_CUR_POS_BIT,
623 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300624 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300625 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300626 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700627 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700628 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600629 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800630 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100631 REQ_F_LTIMEOUT_ACTIVE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700632
633 /* not a real bit, just to check we're not overflowing the space */
634 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300635};
636
637enum {
638 /* ctx owns file */
639 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
640 /* drain existing IO first */
641 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
642 /* linked sqes */
643 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
644 /* doesn't sever on completion < 0 */
645 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
646 /* IOSQE_ASYNC */
647 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700648 /* IOSQE_BUFFER_SELECT */
649 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300650
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300651 /* fail rest of links */
652 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
653 /* on inflight list */
654 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
655 /* read/write uses file position */
656 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
657 /* must not punt to workers */
658 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100659 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300660 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300661 /* regular file */
662 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300663 /* needs cleanup */
664 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700665 /* already went through poll handler */
666 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700667 /* buffer already selected */
668 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600669 /* doesn't need file table for this request */
670 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800671 /* io_wq_work is initialized */
672 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100673 /* linked timeout is active, i.e. prepared by link's head */
674 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700675};
676
677struct async_poll {
678 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600679 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300680};
681
Jens Axboe09bb8392019-03-13 12:39:28 -0600682/*
683 * NOTE! Each of the iocb union members has the file pointer
684 * as the first entry in their struct definition. So you can
685 * access the file pointer through any of the sub-structs,
686 * or directly as just 'ki_filp' in this struct.
687 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700688struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700689 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600690 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700691 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700692 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000693 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700694 struct io_accept accept;
695 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700696 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700697 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100698 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700699 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700700 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700701 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700702 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000703 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700704 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700705 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700706 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300707 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700708 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700709 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600710 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600711 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600712 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300713 /* use only after cleaning per-op data, see io_clean_op() */
714 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700715 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700716
Jens Axboee8c2bc12020-08-15 18:44:09 -0700717 /* opcode allocated if it needs to store data for async defer */
718 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700719 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800720 /* polled IO has completed */
721 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700722
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700723 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300724 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700725
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300726 struct io_ring_ctx *ctx;
727 unsigned int flags;
728 refcount_t refs;
729 struct task_struct *task;
730 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700731
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000732 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000733 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700734
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300735 /*
736 * 1. used with ctx->iopoll_list with reads/writes
737 * 2. to track reqs with ->files (see io_op_def::file_table)
738 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300739 struct list_head inflight_entry;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300740 struct callback_head task_work;
741 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
742 struct hlist_node hash_node;
743 struct async_poll *apoll;
744 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700745};
746
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300747struct io_defer_entry {
748 struct list_head list;
749 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300750 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300751};
752
Jens Axboedef596e2019-01-09 08:59:42 -0700753#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700754
Jens Axboe013538b2020-06-22 09:29:15 -0600755struct io_comp_state {
756 unsigned int nr;
757 struct list_head list;
758 struct io_ring_ctx *ctx;
759};
760
Jens Axboe9a56a232019-01-09 09:06:50 -0700761struct io_submit_state {
762 struct blk_plug plug;
763
764 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700765 * io_kiocb alloc cache
766 */
767 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300768 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700769
Jens Axboe27926b62020-10-28 09:33:23 -0600770 bool plug_started;
771
Jens Axboe2579f912019-01-09 09:10:43 -0700772 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600773 * Batch completion logic
774 */
775 struct io_comp_state comp;
776
777 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700778 * File reference cache
779 */
780 struct file *file;
781 unsigned int fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +0000782 unsigned int file_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700783 unsigned int ios_left;
784};
785
Jens Axboed3656342019-12-18 09:50:26 -0700786struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700787 /* needs req->file assigned */
788 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600789 /* don't fail if file grab fails */
790 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700791 /* hash wq insertion if file is a regular file */
792 unsigned hash_reg_file : 1;
793 /* unbound wq insertion if file is a non-regular file */
794 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700795 /* opcode is not supported by this kernel */
796 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700797 /* set if opcode supports polled "wait" */
798 unsigned pollin : 1;
799 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700800 /* op supports buffer selection */
801 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700802 /* must always have async data allocated */
803 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600804 /* should block plug */
805 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700806 /* size of async data needed, if any */
807 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600808 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700809};
810
Jens Axboe09186822020-10-13 15:01:40 -0600811static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300812 [IORING_OP_NOP] = {},
813 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700814 .needs_file = 1,
815 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700816 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700817 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700818 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600819 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700820 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600821 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_file = 1,
825 .hash_reg_file = 1,
826 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700827 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700828 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600829 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700830 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600831 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
832 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700833 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300834 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700835 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600836 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700837 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300838 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700839 .needs_file = 1,
840 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700841 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600842 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700843 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600844 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700847 .needs_file = 1,
848 .hash_reg_file = 1,
849 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700850 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600851 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700852 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600853 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
854 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700855 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300856 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700857 .needs_file = 1,
858 .unbound_nonreg_file = 1,
859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_POLL_REMOVE] = {},
861 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700862 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600863 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700864 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300865 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700866 .needs_file = 1,
867 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700868 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700869 .needs_async_data = 1,
870 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000871 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700872 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300873 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700874 .needs_file = 1,
875 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700876 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700877 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700878 .needs_async_data = 1,
879 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000880 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700883 .needs_async_data = 1,
884 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600885 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700886 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000887 [IORING_OP_TIMEOUT_REMOVE] = {
888 /* used by timeout updates' prep() */
889 .work_flags = IO_WQ_WORK_MM,
890 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300891 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700892 .needs_file = 1,
893 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700894 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600895 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_ASYNC_CANCEL] = {},
898 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700899 .needs_async_data = 1,
900 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600901 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700902 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300903 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700904 .needs_file = 1,
905 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700906 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700907 .needs_async_data = 1,
908 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600909 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700910 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300911 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700912 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600913 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700914 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300915 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600916 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600917 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700918 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300919 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600920 .needs_file = 1,
921 .needs_file_no_error = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600922 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700923 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300924 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600925 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700926 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300927 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600928 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
929 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700930 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300931 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700932 .needs_file = 1,
933 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700934 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700935 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600936 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700937 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600938 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700939 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300940 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700941 .needs_file = 1,
942 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700943 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600944 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700945 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600946 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
947 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700948 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300949 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700950 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600951 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600954 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700955 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300956 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700957 .needs_file = 1,
958 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700959 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600960 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700961 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300962 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700963 .needs_file = 1,
964 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700965 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700966 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600967 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700968 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300969 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600970 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -0600971 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -0700972 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700973 [IORING_OP_EPOLL_CTL] = {
974 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600975 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700976 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300977 [IORING_OP_SPLICE] = {
978 .needs_file = 1,
979 .hash_reg_file = 1,
980 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600981 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700982 },
983 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700984 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300985 [IORING_OP_TEE] = {
986 .needs_file = 1,
987 .hash_reg_file = 1,
988 .unbound_nonreg_file = 1,
989 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600990 [IORING_OP_SHUTDOWN] = {
991 .needs_file = 1,
992 },
Jens Axboe80a261f2020-09-28 14:23:58 -0600993 [IORING_OP_RENAMEAT] = {
994 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
995 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
996 },
Jens Axboe14a11432020-09-28 14:27:37 -0600997 [IORING_OP_UNLINKAT] = {
998 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
999 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1000 },
Jens Axboed3656342019-12-18 09:50:26 -07001001};
1002
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07001003enum io_mem_account {
1004 ACCT_LOCKED,
1005 ACCT_PINNED,
1006};
1007
Pavel Begunkov90df0852021-01-04 20:43:30 +00001008static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
1009 struct task_struct *task);
1010
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001011static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001012static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001013 struct io_ring_ctx *ctx);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001014static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
1015 struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001016
Pavel Begunkov81b68a52020-07-30 18:43:46 +03001017static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
1018 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001019static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001020static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001021static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001022static void io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001023static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001024static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001025static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001026static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001027 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001028 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001029static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001030static struct file *io_file_get(struct io_submit_state *state,
1031 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc1379e22020-09-30 22:57:56 +03001032static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001033static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001034
Jens Axboeb63534c2020-06-04 11:28:00 -06001035static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1036 struct iovec **iovec, struct iov_iter *iter,
1037 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001038static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1039 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001040 struct iov_iter *iter, bool force);
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001041static void io_req_drop_files(struct io_kiocb *req);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001042static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001043
1044static struct kmem_cache *req_cachep;
1045
Jens Axboe09186822020-10-13 15:01:40 -06001046static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047
1048struct sock *io_uring_get_socket(struct file *file)
1049{
1050#if defined(CONFIG_UNIX)
1051 if (file->f_op == &io_uring_fops) {
1052 struct io_ring_ctx *ctx = file->private_data;
1053
1054 return ctx->ring_sock->sk;
1055 }
1056#endif
1057 return NULL;
1058}
1059EXPORT_SYMBOL(io_uring_get_socket);
1060
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001061#define io_for_each_link(pos, head) \
1062 for (pos = (head); pos; pos = pos->link)
1063
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001064static inline void io_clean_op(struct io_kiocb *req)
1065{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001066 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001067 __io_clean_op(req);
1068}
1069
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001070static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001071{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001072 struct io_ring_ctx *ctx = req->ctx;
1073
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001074 if (!req->fixed_rsrc_refs) {
1075 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1076 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001077 }
1078}
1079
Pavel Begunkov08d23632020-11-06 13:00:22 +00001080static bool io_match_task(struct io_kiocb *head,
1081 struct task_struct *task,
1082 struct files_struct *files)
1083{
1084 struct io_kiocb *req;
1085
Jens Axboe84965ff2021-01-23 15:51:11 -07001086 if (task && head->task != task) {
1087 /* in terms of cancelation, always match if req task is dead */
1088 if (head->task->flags & PF_EXITING)
1089 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001090 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001091 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001092 if (!files)
1093 return true;
1094
1095 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001096 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1097 continue;
1098 if (req->file && req->file->f_op == &io_uring_fops)
1099 return true;
1100 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001101 req->work.identity->files == files)
1102 return true;
1103 }
1104 return false;
1105}
1106
Jens Axboe28cea78a2020-09-14 10:51:17 -06001107static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001108{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001109 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001110 struct mm_struct *mm = current->mm;
1111
1112 if (mm) {
1113 kthread_unuse_mm(mm);
1114 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001115 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001116 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001117 if (files) {
1118 struct nsproxy *nsproxy = current->nsproxy;
1119
1120 task_lock(current);
1121 current->files = NULL;
1122 current->nsproxy = NULL;
1123 task_unlock(current);
1124 put_files_struct(files);
1125 put_nsproxy(nsproxy);
1126 }
1127}
1128
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001129static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001130{
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001131 if (current->flags & PF_EXITING)
1132 return -EFAULT;
1133
Jens Axboe28cea78a2020-09-14 10:51:17 -06001134 if (!current->files) {
1135 struct files_struct *files;
1136 struct nsproxy *nsproxy;
1137
1138 task_lock(ctx->sqo_task);
1139 files = ctx->sqo_task->files;
1140 if (!files) {
1141 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001142 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001143 }
1144 atomic_inc(&files->count);
1145 get_nsproxy(ctx->sqo_task->nsproxy);
1146 nsproxy = ctx->sqo_task->nsproxy;
1147 task_unlock(ctx->sqo_task);
1148
1149 task_lock(current);
1150 current->files = files;
1151 current->nsproxy = nsproxy;
1152 task_unlock(current);
1153 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001154 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001155}
1156
1157static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1158{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001159 struct mm_struct *mm;
1160
Pavel Begunkov621fadc2021-01-11 04:00:31 +00001161 if (current->flags & PF_EXITING)
1162 return -EFAULT;
Jens Axboe4b70cf92020-11-02 10:39:05 -07001163 if (current->mm)
1164 return 0;
1165
1166 /* Should never happen */
1167 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL)))
1168 return -EFAULT;
1169
1170 task_lock(ctx->sqo_task);
1171 mm = ctx->sqo_task->mm;
1172 if (unlikely(!mm || !mmget_not_zero(mm)))
1173 mm = NULL;
1174 task_unlock(ctx->sqo_task);
1175
1176 if (mm) {
1177 kthread_use_mm(mm);
1178 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001179 }
1180
Jens Axboe4b70cf92020-11-02 10:39:05 -07001181 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001182}
1183
Jens Axboe28cea78a2020-09-14 10:51:17 -06001184static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1185 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001186{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001187 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001188 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001189
1190 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001191 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001192 if (unlikely(ret))
1193 return ret;
1194 }
1195
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001196 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1197 ret = __io_sq_thread_acquire_files(ctx);
1198 if (unlikely(ret))
1199 return ret;
1200 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001201
1202 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001203}
1204
Dennis Zhou91d8f512020-09-16 13:41:05 -07001205static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1206 struct cgroup_subsys_state **cur_css)
1207
1208{
1209#ifdef CONFIG_BLK_CGROUP
1210 /* puts the old one when swapping */
1211 if (*cur_css != ctx->sqo_blkcg_css) {
1212 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1213 *cur_css = ctx->sqo_blkcg_css;
1214 }
1215#endif
1216}
1217
1218static void io_sq_thread_unassociate_blkcg(void)
1219{
1220#ifdef CONFIG_BLK_CGROUP
1221 kthread_associate_blkcg(NULL);
1222#endif
1223}
1224
Jens Axboec40f6372020-06-25 15:39:59 -06001225static inline void req_set_fail_links(struct io_kiocb *req)
1226{
1227 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1228 req->flags |= REQ_F_FAIL_LINK;
1229}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001230
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001231/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001232 * None of these are dereferenced, they are simply used to check if any of
1233 * them have changed. If we're under current and check they are still the
1234 * same, we're fine to grab references to them for actual out-of-line use.
1235 */
1236static void io_init_identity(struct io_identity *id)
1237{
1238 id->files = current->files;
1239 id->mm = current->mm;
1240#ifdef CONFIG_BLK_CGROUP
1241 rcu_read_lock();
1242 id->blkcg_css = blkcg_css();
1243 rcu_read_unlock();
1244#endif
1245 id->creds = current_cred();
1246 id->nsproxy = current->nsproxy;
1247 id->fs = current->fs;
1248 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001249#ifdef CONFIG_AUDIT
1250 id->loginuid = current->loginuid;
1251 id->sessionid = current->sessionid;
1252#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001253 refcount_set(&id->count, 1);
1254}
1255
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001256static inline void __io_req_init_async(struct io_kiocb *req)
1257{
1258 memset(&req->work, 0, sizeof(req->work));
1259 req->flags |= REQ_F_WORK_INITIALIZED;
1260}
1261
Jens Axboe1e6fa522020-10-15 08:46:24 -06001262/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001263 * Note: must call io_req_init_async() for the first time you
1264 * touch any members of io_wq_work.
1265 */
1266static inline void io_req_init_async(struct io_kiocb *req)
1267{
Jens Axboe500a3732020-10-15 17:38:03 -06001268 struct io_uring_task *tctx = current->io_uring;
1269
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001270 if (req->flags & REQ_F_WORK_INITIALIZED)
1271 return;
1272
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001273 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001274
1275 /* Grab a ref if this isn't our static identity */
1276 req->work.identity = tctx->identity;
1277 if (tctx->identity != &tctx->__identity)
1278 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001279}
1280
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001281static inline bool io_async_submit(struct io_ring_ctx *ctx)
1282{
1283 return ctx->flags & IORING_SETUP_SQPOLL;
1284}
1285
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1287{
1288 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1289
Jens Axboe0f158b42020-05-14 17:18:39 -06001290 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001291}
1292
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001293static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1294{
1295 return !req->timeout.off;
1296}
1297
Jens Axboe2b188cc2019-01-07 10:46:33 -07001298static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1299{
1300 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001301 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001302
1303 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1304 if (!ctx)
1305 return NULL;
1306
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001307 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1308 if (!ctx->fallback_req)
1309 goto err;
1310
Jens Axboe78076bb2019-12-04 19:56:40 -07001311 /*
1312 * Use 5 bits less than the max cq entries, that should give us around
1313 * 32 entries per hash list if totally full and uniformly spread.
1314 */
1315 hash_bits = ilog2(p->cq_entries);
1316 hash_bits -= 5;
1317 if (hash_bits <= 0)
1318 hash_bits = 1;
1319 ctx->cancel_hash_bits = hash_bits;
1320 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1321 GFP_KERNEL);
1322 if (!ctx->cancel_hash)
1323 goto err;
1324 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1325
Roman Gushchin21482892019-05-07 10:01:48 -07001326 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001327 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1328 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329
1330 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001331 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001332 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001333 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001334 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001335 init_completion(&ctx->ref_comp);
1336 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001337 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001338 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339 mutex_init(&ctx->uring_lock);
1340 init_waitqueue_head(&ctx->wait);
1341 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001342 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001343 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001344 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001345 spin_lock_init(&ctx->inflight_lock);
1346 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001347 spin_lock_init(&ctx->rsrc_ref_lock);
1348 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001349 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1350 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001351 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001352err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001353 if (ctx->fallback_req)
1354 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001355 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001356 kfree(ctx);
1357 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001358}
1359
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001360static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001361{
Jens Axboe2bc99302020-07-09 09:43:27 -06001362 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1363 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001364
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001365 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001366 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001367 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001368
Bob Liu9d858b22019-11-13 18:06:25 +08001369 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001370}
1371
Jens Axboede0617e2019-04-06 21:51:27 -06001372static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001373{
Hristo Venev75b28af2019-08-26 17:23:46 +00001374 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001375
Pavel Begunkov07910152020-01-17 03:52:46 +03001376 /* order cqe stores with ring update */
1377 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001378}
1379
Jens Axboe5c3462c2020-10-15 09:02:33 -06001380static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001381{
Jens Axboe500a3732020-10-15 17:38:03 -06001382 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001383 return;
1384 if (refcount_dec_and_test(&req->work.identity->count))
1385 kfree(req->work.identity);
1386}
1387
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001388static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001389{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001390 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001391 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001392
1393 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001394
Jens Axboedfead8a2020-10-14 10:12:37 -06001395 if (req->work.flags & IO_WQ_WORK_MM) {
Jens Axboe98447d62020-10-14 10:48:51 -06001396 mmdrop(req->work.identity->mm);
Jens Axboedfead8a2020-10-14 10:12:37 -06001397 req->work.flags &= ~IO_WQ_WORK_MM;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001398 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07001399#ifdef CONFIG_BLK_CGROUP
Jens Axboedfead8a2020-10-14 10:12:37 -06001400 if (req->work.flags & IO_WQ_WORK_BLKCG) {
Jens Axboe98447d62020-10-14 10:48:51 -06001401 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001402 req->work.flags &= ~IO_WQ_WORK_BLKCG;
Jens Axboecccf0ee2020-01-27 16:34:48 -07001403 }
Jens Axboedfead8a2020-10-14 10:12:37 -06001404#endif
1405 if (req->work.flags & IO_WQ_WORK_CREDS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001406 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001407 req->work.flags &= ~IO_WQ_WORK_CREDS;
1408 }
1409 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001410 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001411
Jens Axboe98447d62020-10-14 10:48:51 -06001412 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001413 if (--fs->users)
1414 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001415 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001416 if (fs)
1417 free_fs_struct(fs);
Jens Axboedfead8a2020-10-14 10:12:37 -06001418 req->work.flags &= ~IO_WQ_WORK_FS;
Jens Axboeff002b32020-02-07 16:05:21 -07001419 }
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001420 if (req->flags & REQ_F_INFLIGHT)
1421 io_req_drop_files(req);
Jens Axboe51a4cc12020-08-10 10:55:56 -06001422
Jens Axboe5c3462c2020-10-15 09:02:33 -06001423 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001424}
1425
1426/*
1427 * Create a private copy of io_identity, since some fields don't match
1428 * the current context.
1429 */
1430static bool io_identity_cow(struct io_kiocb *req)
1431{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001432 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001433 const struct cred *creds = NULL;
1434 struct io_identity *id;
1435
1436 if (req->work.flags & IO_WQ_WORK_CREDS)
1437 creds = req->work.identity->creds;
1438
1439 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1440 if (unlikely(!id)) {
1441 req->work.flags |= IO_WQ_WORK_CANCEL;
1442 return false;
1443 }
1444
1445 /*
1446 * We can safely just re-init the creds we copied Either the field
1447 * matches the current one, or we haven't grabbed it yet. The only
1448 * exception is ->creds, through registered personalities, so handle
1449 * that one separately.
1450 */
1451 io_init_identity(id);
1452 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001453 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001454
1455 /* add one for this request */
1456 refcount_inc(&id->count);
1457
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001458 /* drop tctx and req identity references, if needed */
1459 if (tctx->identity != &tctx->__identity &&
1460 refcount_dec_and_test(&tctx->identity->count))
1461 kfree(tctx->identity);
1462 if (req->work.identity != &tctx->__identity &&
1463 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001464 kfree(req->work.identity);
1465
1466 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001467 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001468 return true;
1469}
1470
1471static bool io_grab_identity(struct io_kiocb *req)
1472{
1473 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001474 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001475 struct io_ring_ctx *ctx = req->ctx;
1476
Jens Axboe69228332020-10-20 14:28:41 -06001477 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1478 if (id->fsize != rlimit(RLIMIT_FSIZE))
1479 return false;
1480 req->work.flags |= IO_WQ_WORK_FSIZE;
1481 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001482#ifdef CONFIG_BLK_CGROUP
1483 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1484 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1485 rcu_read_lock();
1486 if (id->blkcg_css != blkcg_css()) {
1487 rcu_read_unlock();
1488 return false;
1489 }
1490 /*
1491 * This should be rare, either the cgroup is dying or the task
1492 * is moving cgroups. Just punt to root for the handful of ios.
1493 */
1494 if (css_tryget_online(id->blkcg_css))
1495 req->work.flags |= IO_WQ_WORK_BLKCG;
1496 rcu_read_unlock();
1497 }
1498#endif
1499 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1500 if (id->creds != current_cred())
1501 return false;
1502 get_cred(id->creds);
1503 req->work.flags |= IO_WQ_WORK_CREDS;
1504 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001505#ifdef CONFIG_AUDIT
1506 if (!uid_eq(current->loginuid, id->loginuid) ||
1507 current->sessionid != id->sessionid)
1508 return false;
1509#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001510 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1511 (def->work_flags & IO_WQ_WORK_FS)) {
1512 if (current->fs != id->fs)
1513 return false;
1514 spin_lock(&id->fs->lock);
1515 if (!id->fs->in_exec) {
1516 id->fs->users++;
1517 req->work.flags |= IO_WQ_WORK_FS;
1518 } else {
1519 req->work.flags |= IO_WQ_WORK_CANCEL;
1520 }
1521 spin_unlock(&current->fs->lock);
1522 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001523 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1524 (def->work_flags & IO_WQ_WORK_FILES) &&
1525 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1526 if (id->files != current->files ||
1527 id->nsproxy != current->nsproxy)
1528 return false;
1529 atomic_inc(&id->files->count);
1530 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001531
Jens Axboe02a13672021-01-23 15:49:31 -07001532 if (!(req->flags & REQ_F_INFLIGHT)) {
1533 req->flags |= REQ_F_INFLIGHT;
1534
1535 spin_lock_irq(&ctx->inflight_lock);
1536 list_add(&req->inflight_entry, &ctx->inflight_list);
1537 spin_unlock_irq(&ctx->inflight_lock);
1538 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001539 req->work.flags |= IO_WQ_WORK_FILES;
1540 }
Jens Axboe77788772020-12-29 10:50:46 -07001541 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1542 (def->work_flags & IO_WQ_WORK_MM)) {
1543 if (id->mm != current->mm)
1544 return false;
1545 mmgrab(id->mm);
1546 req->work.flags |= IO_WQ_WORK_MM;
1547 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001548
1549 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001550}
1551
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001552static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001553{
Jens Axboed3656342019-12-18 09:50:26 -07001554 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001555 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001556
Pavel Begunkov16d59802020-07-12 16:16:47 +03001557 io_req_init_async(req);
1558
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001559 if (req->flags & REQ_F_FORCE_ASYNC)
1560 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1561
Jens Axboed3656342019-12-18 09:50:26 -07001562 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001563 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001564 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001565 } else {
1566 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001567 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001568 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001569
Jens Axboe1e6fa522020-10-15 08:46:24 -06001570 /* if we fail grabbing identity, we must COW, regrab, and retry */
1571 if (io_grab_identity(req))
1572 return;
1573
1574 if (!io_identity_cow(req))
1575 return;
1576
1577 /* can't fail at this point */
1578 if (!io_grab_identity(req))
1579 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001580}
1581
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001582static void io_prep_async_link(struct io_kiocb *req)
1583{
1584 struct io_kiocb *cur;
1585
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001586 io_for_each_link(cur, req)
1587 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001588}
1589
Jens Axboe7271ef32020-08-10 09:55:22 -06001590static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001591{
Jackie Liua197f662019-11-08 08:09:12 -07001592 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001593 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001594
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001595 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1596 &req->work, req->flags);
1597 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001598 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001599}
1600
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001601static void io_queue_async_work(struct io_kiocb *req)
1602{
Jens Axboe7271ef32020-08-10 09:55:22 -06001603 struct io_kiocb *link;
1604
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001605 /* init ->work of the whole link before punting */
1606 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001607 link = __io_queue_async_work(req);
1608
1609 if (link)
1610 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001611}
1612
Jens Axboe5262f562019-09-17 12:26:57 -06001613static void io_kill_timeout(struct io_kiocb *req)
1614{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001615 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001616 int ret;
1617
Jens Axboee8c2bc12020-08-15 18:44:09 -07001618 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001619 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001620 atomic_set(&req->ctx->cq_timeouts,
1621 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001622 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001623 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001624 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001625 }
1626}
1627
Jens Axboe76e1b642020-09-26 15:05:03 -06001628/*
1629 * Returns true if we found and killed one or more timeouts
1630 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001631static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1632 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001633{
1634 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001635 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001636
1637 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001638 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001639 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001640 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001641 canceled++;
1642 }
Jens Axboef3606e32020-09-22 08:18:24 -06001643 }
Jens Axboe5262f562019-09-17 12:26:57 -06001644 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001645 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001646}
1647
Pavel Begunkov04518942020-05-26 20:34:05 +03001648static void __io_queue_deferred(struct io_ring_ctx *ctx)
1649{
1650 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001651 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1652 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001653
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001654 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001655 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001656 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001657 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001658 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001659 } while (!list_empty(&ctx->defer_list));
1660}
1661
Pavel Begunkov360428f2020-05-30 14:54:17 +03001662static void io_flush_timeouts(struct io_ring_ctx *ctx)
1663{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001664 u32 seq;
1665
1666 if (list_empty(&ctx->timeout_list))
1667 return;
1668
1669 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1670
1671 do {
1672 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001673 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001674 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001675
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001676 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001677 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001678
1679 /*
1680 * Since seq can easily wrap around over time, subtract
1681 * the last seq at which timeouts were flushed before comparing.
1682 * Assuming not more than 2^31-1 events have happened since,
1683 * these subtractions won't have wrapped, so we can check if
1684 * target is in [last_seq, current_seq] by comparing the two.
1685 */
1686 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1687 events_got = seq - ctx->cq_last_tm_flush;
1688 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001689 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001690
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001691 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001692 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001693 } while (!list_empty(&ctx->timeout_list));
1694
1695 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001696}
1697
Jens Axboede0617e2019-04-06 21:51:27 -06001698static void io_commit_cqring(struct io_ring_ctx *ctx)
1699{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001700 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001701 __io_commit_cqring(ctx);
1702
Pavel Begunkov04518942020-05-26 20:34:05 +03001703 if (unlikely(!list_empty(&ctx->defer_list)))
1704 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001705}
1706
Jens Axboe90554202020-09-03 12:12:41 -06001707static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1708{
1709 struct io_rings *r = ctx->rings;
1710
1711 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1712}
1713
Jens Axboe2b188cc2019-01-07 10:46:33 -07001714static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1715{
Hristo Venev75b28af2019-08-26 17:23:46 +00001716 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717 unsigned tail;
1718
1719 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001720 /*
1721 * writes to the cq entry need to come after reading head; the
1722 * control dependency is enough as we're using WRITE_ONCE to
1723 * fill the cq entry
1724 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001725 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726 return NULL;
1727
1728 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001729 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730}
1731
Jens Axboef2842ab2020-01-08 11:04:00 -07001732static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1733{
Jens Axboef0b493e2020-02-01 21:30:11 -07001734 if (!ctx->cq_ev_fd)
1735 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001736 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1737 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001738 if (!ctx->eventfd_async)
1739 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001740 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001741}
1742
Pavel Begunkove23de152020-12-17 00:24:37 +00001743static inline unsigned __io_cqring_events(struct io_ring_ctx *ctx)
1744{
1745 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1746}
1747
Jens Axboeb41e9852020-02-17 09:52:41 -07001748static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001749{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001750 /* see waitqueue_active() comment */
1751 smp_mb();
1752
Jens Axboe8c838782019-03-12 15:48:16 -06001753 if (waitqueue_active(&ctx->wait))
1754 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001755 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1756 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001757 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001758 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001759 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001760 wake_up_interruptible(&ctx->cq_wait);
1761 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1762 }
Jens Axboe8c838782019-03-12 15:48:16 -06001763}
1764
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001765static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1766{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001767 /* see waitqueue_active() comment */
1768 smp_mb();
1769
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001770 if (ctx->flags & IORING_SETUP_SQPOLL) {
1771 if (waitqueue_active(&ctx->wait))
1772 wake_up(&ctx->wait);
1773 }
1774 if (io_should_trigger_evfd(ctx))
1775 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001776 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001777 wake_up_interruptible(&ctx->cq_wait);
1778 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1779 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001780}
1781
Jens Axboec4a2ed72019-11-21 21:01:26 -07001782/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001783static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1784 struct task_struct *tsk,
1785 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001787 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001788 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001789 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001790 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001791 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001792 LIST_HEAD(list);
1793
Pavel Begunkove23de152020-12-17 00:24:37 +00001794 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1795 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001796
Jens Axboeb18032b2021-01-24 16:58:56 -07001797 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001798 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001799 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001800 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001801 continue;
1802
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001803 cqe = io_get_cqring(ctx);
1804 if (!cqe && !force)
1805 break;
1806
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001807 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001808 if (cqe) {
1809 WRITE_ONCE(cqe->user_data, req->user_data);
1810 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001811 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001812 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001813 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001814 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001815 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001816 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001817 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001818 }
1819
Pavel Begunkov09e88402020-12-17 00:24:38 +00001820 all_flushed = list_empty(&ctx->cq_overflow_list);
1821 if (all_flushed) {
1822 clear_bit(0, &ctx->sq_check_overflow);
1823 clear_bit(0, &ctx->cq_check_overflow);
1824 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1825 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001826
Jens Axboeb18032b2021-01-24 16:58:56 -07001827 if (posted)
1828 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001829 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001830 if (posted)
1831 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001832
1833 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001834 req = list_first_entry(&list, struct io_kiocb, compl.list);
1835 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001836 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001837 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001838
Pavel Begunkov09e88402020-12-17 00:24:38 +00001839 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001840}
1841
Pavel Begunkov6c503152021-01-04 20:36:36 +00001842static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1843 struct task_struct *tsk,
1844 struct files_struct *files)
1845{
1846 if (test_bit(0, &ctx->cq_check_overflow)) {
1847 /* iopoll syncs against uring_lock, not completion_lock */
1848 if (ctx->flags & IORING_SETUP_IOPOLL)
1849 mutex_lock(&ctx->uring_lock);
1850 __io_cqring_overflow_flush(ctx, force, tsk, files);
1851 if (ctx->flags & IORING_SETUP_IOPOLL)
1852 mutex_unlock(&ctx->uring_lock);
1853 }
1854}
1855
Jens Axboebcda7ba2020-02-23 16:42:51 -07001856static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001858 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001859 struct io_uring_cqe *cqe;
1860
Jens Axboe78e19bb2019-11-06 15:21:34 -07001861 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001862
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863 /*
1864 * If we can't get a cq entry, userspace overflowed the
1865 * submission (by quite a lot). Increment the overflow count in
1866 * the ring.
1867 */
1868 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001869 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001870 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001871 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001872 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001873 } else if (ctx->cq_overflow_flushed ||
1874 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001875 /*
1876 * If we're in ring overflow flush mode, or in task cancel mode,
1877 * then we cannot store the request for later flushing, we need
1878 * to drop it on the floor.
1879 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001880 ctx->cached_cq_overflow++;
1881 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001882 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001883 if (list_empty(&ctx->cq_overflow_list)) {
1884 set_bit(0, &ctx->sq_check_overflow);
1885 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001886 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001887 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001888 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001889 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001890 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001891 refcount_inc(&req->refs);
1892 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893 }
1894}
1895
Jens Axboebcda7ba2020-02-23 16:42:51 -07001896static void io_cqring_fill_event(struct io_kiocb *req, long res)
1897{
1898 __io_cqring_fill_event(req, res, 0);
1899}
1900
Jens Axboee1e16092020-06-22 09:17:17 -06001901static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001903 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001904 unsigned long flags;
1905
1906 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001907 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908 io_commit_cqring(ctx);
1909 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1910
Jens Axboe8c838782019-03-12 15:48:16 -06001911 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912}
1913
Jens Axboe229a7b62020-06-22 10:13:11 -06001914static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001915{
Jens Axboe229a7b62020-06-22 10:13:11 -06001916 struct io_ring_ctx *ctx = cs->ctx;
1917
1918 spin_lock_irq(&ctx->completion_lock);
1919 while (!list_empty(&cs->list)) {
1920 struct io_kiocb *req;
1921
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001922 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1923 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001924 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001925
1926 /*
1927 * io_free_req() doesn't care about completion_lock unless one
1928 * of these flags is set. REQ_F_WORK_INITIALIZED is in the list
1929 * because of a potential deadlock with req->work.fs->lock
1930 */
1931 if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT
1932 |REQ_F_WORK_INITIALIZED)) {
Jens Axboe229a7b62020-06-22 10:13:11 -06001933 spin_unlock_irq(&ctx->completion_lock);
1934 io_put_req(req);
1935 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001936 } else {
1937 io_put_req(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001938 }
1939 }
1940 io_commit_cqring(ctx);
1941 spin_unlock_irq(&ctx->completion_lock);
1942
1943 io_cqring_ev_posted(ctx);
1944 cs->nr = 0;
1945}
1946
1947static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1948 struct io_comp_state *cs)
1949{
1950 if (!cs) {
1951 io_cqring_add_event(req, res, cflags);
1952 io_put_req(req);
1953 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001954 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001955 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001956 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001957 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001958 if (++cs->nr >= 32)
1959 io_submit_flush_completions(cs);
1960 }
Jens Axboee1e16092020-06-22 09:17:17 -06001961}
1962
1963static void io_req_complete(struct io_kiocb *req, long res)
1964{
Jens Axboe229a7b62020-06-22 10:13:11 -06001965 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001966}
1967
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001968static inline bool io_is_fallback_req(struct io_kiocb *req)
1969{
1970 return req == (struct io_kiocb *)
1971 ((unsigned long) req->ctx->fallback_req & ~1UL);
1972}
1973
1974static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1975{
1976 struct io_kiocb *req;
1977
1978 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001979 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001980 return req;
1981
1982 return NULL;
1983}
1984
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001985static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1986 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001987{
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001988 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001989 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001990 size_t sz;
1991 int ret;
1992
1993 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001994 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1995
1996 /*
1997 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1998 * retry single alloc to be on the safe side.
1999 */
2000 if (unlikely(ret <= 0)) {
2001 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2002 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07002003 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06002004 ret = 1;
2005 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002006 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002007 }
2008
Pavel Begunkov291b2822020-09-30 22:57:01 +03002009 state->free_reqs--;
2010 return state->reqs[state->free_reqs];
Jens Axboe0ddf92e2019-11-08 08:52:53 -07002011fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03002012 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002013}
2014
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002015static inline void io_put_file(struct io_kiocb *req, struct file *file,
2016 bool fixed)
2017{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002018 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002019 fput(file);
2020}
2021
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002022static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002023{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002024 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002025
Jens Axboee8c2bc12020-08-15 18:44:09 -07002026 if (req->async_data)
2027 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002028 if (req->file)
2029 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002030 if (req->fixed_rsrc_refs)
2031 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002032 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002033}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002034
Pavel Begunkov216578e2020-10-13 09:44:00 +01002035static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002036{
Jens Axboe0f212202020-09-13 13:09:39 -06002037 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06002038 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002039
Pavel Begunkov216578e2020-10-13 09:44:00 +01002040 io_dismantle_req(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002041
Jens Axboed8a6df12020-10-15 16:24:45 -06002042 percpu_counter_dec(&tctx->inflight);
Jens Axboefdaf0832020-10-30 09:37:30 -06002043 if (atomic_read(&tctx->in_idle))
Jens Axboe0f212202020-09-13 13:09:39 -06002044 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002045 put_task_struct(req->task);
2046
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03002047 if (likely(!io_is_fallback_req(req)))
2048 kmem_cache_free(req_cachep, req);
2049 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002050 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
2051 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002052}
2053
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002054static inline void io_remove_next_linked(struct io_kiocb *req)
2055{
2056 struct io_kiocb *nxt = req->link;
2057
2058 req->link = nxt->link;
2059 nxt->link = NULL;
2060}
2061
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002062static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002063{
Jackie Liua197f662019-11-08 08:09:12 -07002064 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002065 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002066 bool cancelled = false;
2067 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002068
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002069 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002070 link = req->link;
2071
Pavel Begunkov900fad42020-10-19 16:39:16 +01002072 /*
2073 * Can happen if a linked timeout fired and link had been like
2074 * req -> link t-out -> link t-out [-> ...]
2075 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002076 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2077 struct io_timeout_data *io = link->async_data;
2078 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002079
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002080 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002081 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002082 ret = hrtimer_try_to_cancel(&io->timer);
2083 if (ret != -1) {
2084 io_cqring_fill_event(link, -ECANCELED);
2085 io_commit_cqring(ctx);
2086 cancelled = true;
2087 }
2088 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002089 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002090 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002091
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002092 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002093 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002094 io_put_req(link);
2095 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002096}
2097
Jens Axboe4d7dd462019-11-20 13:03:52 -07002098
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002099static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002100{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002101 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002102 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002103 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002104
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002105 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002106 link = req->link;
2107 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002108
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002109 while (link) {
2110 nxt = link->link;
2111 link->link = NULL;
2112
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002113 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002114 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002115
2116 /*
2117 * It's ok to free under spinlock as they're not linked anymore,
2118 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2119 * work.fs->lock.
2120 */
2121 if (link->flags & REQ_F_WORK_INITIALIZED)
2122 io_put_req_deferred(link, 2);
2123 else
2124 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002125 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002126 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002127 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002128 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002129
Jens Axboe2665abf2019-11-05 12:40:47 -07002130 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002131}
2132
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002133static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002134{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002135 if (req->flags & REQ_F_LINK_TIMEOUT)
2136 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002137
Jens Axboe9e645e112019-05-10 16:07:28 -06002138 /*
2139 * If LINK is set, we have dependent requests in this chain. If we
2140 * didn't fail this request, queue the first one up, moving any other
2141 * dependencies to the next request. In case of failure, fail the rest
2142 * of the chain.
2143 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002144 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2145 struct io_kiocb *nxt = req->link;
2146
2147 req->link = NULL;
2148 return nxt;
2149 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002150 io_fail_links(req);
2151 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002152}
Jens Axboe2665abf2019-11-05 12:40:47 -07002153
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002154static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002155{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002156 if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT)))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002157 return NULL;
2158 return __io_req_find_next(req);
2159}
2160
Jens Axboe355fb9e2020-10-22 20:19:35 -06002161static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002162{
2163 struct task_struct *tsk = req->task;
2164 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002165 enum task_work_notify_mode notify;
2166 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002167
Jens Axboe6200b0a2020-09-13 14:38:30 -06002168 if (tsk->flags & PF_EXITING)
2169 return -ESRCH;
2170
Jens Axboec2c4c832020-07-01 15:37:11 -06002171 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002172 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2173 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2174 * processing task_work. There's no reliable way to tell if TWA_RESUME
2175 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002176 */
Jens Axboe91989c72020-10-16 09:02:26 -06002177 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002178 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002179 notify = TWA_SIGNAL;
2180
Jens Axboe87c43112020-09-30 21:00:14 -06002181 ret = task_work_add(tsk, &req->task_work, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002182 if (!ret)
2183 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002184
Jens Axboec2c4c832020-07-01 15:37:11 -06002185 return ret;
2186}
2187
Jens Axboec40f6372020-06-25 15:39:59 -06002188static void __io_req_task_cancel(struct io_kiocb *req, int error)
2189{
2190 struct io_ring_ctx *ctx = req->ctx;
2191
2192 spin_lock_irq(&ctx->completion_lock);
2193 io_cqring_fill_event(req, error);
2194 io_commit_cqring(ctx);
2195 spin_unlock_irq(&ctx->completion_lock);
2196
2197 io_cqring_ev_posted(ctx);
2198 req_set_fail_links(req);
2199 io_double_put_req(req);
2200}
2201
2202static void io_req_task_cancel(struct callback_head *cb)
2203{
2204 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002205 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002206
2207 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002208 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002209}
2210
2211static void __io_req_task_submit(struct io_kiocb *req)
2212{
2213 struct io_ring_ctx *ctx = req->ctx;
2214
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002215 mutex_lock(&ctx->uring_lock);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00002216 if (!ctx->sqo_dead &&
2217 !__io_sq_thread_acquire_mm(ctx) &&
2218 !__io_sq_thread_acquire_files(ctx))
Pavel Begunkovc1379e22020-09-30 22:57:56 +03002219 __io_queue_sqe(req, NULL);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002220 else
Jens Axboec40f6372020-06-25 15:39:59 -06002221 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002222 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002223}
2224
Jens Axboec40f6372020-06-25 15:39:59 -06002225static void io_req_task_submit(struct callback_head *cb)
2226{
2227 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06002228 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002229
2230 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002231 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002232}
2233
2234static void io_req_task_queue(struct io_kiocb *req)
2235{
Jens Axboec40f6372020-06-25 15:39:59 -06002236 int ret;
2237
2238 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06002239 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002240
Jens Axboe355fb9e2020-10-22 20:19:35 -06002241 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002242 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002243 struct task_struct *tsk;
2244
Jens Axboec40f6372020-06-25 15:39:59 -06002245 init_task_work(&req->task_work, io_req_task_cancel);
2246 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002247 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06002248 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06002249 }
Jens Axboec40f6372020-06-25 15:39:59 -06002250}
2251
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002252static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002253{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002254 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002255
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002256 if (nxt)
2257 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002258}
2259
Jens Axboe9e645e112019-05-10 16:07:28 -06002260static void io_free_req(struct io_kiocb *req)
2261{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002262 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002263 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002264}
2265
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002266struct req_batch {
2267 void *reqs[IO_IOPOLL_BATCH];
2268 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002269
2270 struct task_struct *task;
2271 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002272};
2273
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002274static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002275{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002276 rb->to_free = 0;
2277 rb->task_refs = 0;
2278 rb->task = NULL;
2279}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002280
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002281static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
2282 struct req_batch *rb)
2283{
2284 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
2285 percpu_ref_put_many(&ctx->refs, rb->to_free);
2286 rb->to_free = 0;
2287}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002288
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002289static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2290 struct req_batch *rb)
2291{
2292 if (rb->to_free)
2293 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002294 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002295 struct io_uring_task *tctx = rb->task->io_uring;
2296
2297 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboec93cc9e2021-01-16 11:52:11 -07002298 if (atomic_read(&tctx->in_idle))
2299 wake_up(&tctx->wait);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002300 put_task_struct_many(rb->task, rb->task_refs);
2301 rb->task = NULL;
2302 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002303}
2304
2305static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
2306{
2307 if (unlikely(io_is_fallback_req(req))) {
2308 io_free_req(req);
2309 return;
2310 }
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002311 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002312
Jens Axboee3bc8e92020-09-24 08:45:57 -06002313 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06002314 if (rb->task) {
Jens Axboed8a6df12020-10-15 16:24:45 -06002315 struct io_uring_task *tctx = rb->task->io_uring;
2316
2317 percpu_counter_sub(&tctx->inflight, rb->task_refs);
Jens Axboec93cc9e2021-01-16 11:52:11 -07002318 if (atomic_read(&tctx->in_idle))
2319 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002320 put_task_struct_many(rb->task, rb->task_refs);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002321 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002322 rb->task = req->task;
2323 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002324 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002325 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002326
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002327 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002328 rb->reqs[rb->to_free++] = req;
2329 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
2330 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002331}
2332
Jens Axboeba816ad2019-09-28 11:36:45 -06002333/*
2334 * Drop reference to request, return next in chain (if there is one) if this
2335 * was the last reference to this request.
2336 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002337static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002338{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002339 struct io_kiocb *nxt = NULL;
2340
Jens Axboe2a44f462020-02-25 13:25:41 -07002341 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002342 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002343 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002344 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002345 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002346}
2347
Jens Axboe2b188cc2019-01-07 10:46:33 -07002348static void io_put_req(struct io_kiocb *req)
2349{
Jens Axboedef596e2019-01-09 08:59:42 -07002350 if (refcount_dec_and_test(&req->refs))
2351 io_free_req(req);
2352}
2353
Pavel Begunkov216578e2020-10-13 09:44:00 +01002354static void io_put_req_deferred_cb(struct callback_head *cb)
2355{
2356 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2357
2358 io_free_req(req);
2359}
2360
2361static void io_free_req_deferred(struct io_kiocb *req)
2362{
2363 int ret;
2364
2365 init_task_work(&req->task_work, io_put_req_deferred_cb);
Jens Axboe355fb9e2020-10-22 20:19:35 -06002366 ret = io_req_task_work_add(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002367 if (unlikely(ret)) {
2368 struct task_struct *tsk;
2369
2370 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06002371 task_work_add(tsk, &req->task_work, TWA_NONE);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002372 wake_up_process(tsk);
2373 }
2374}
2375
2376static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2377{
2378 if (refcount_sub_and_test(refs, &req->refs))
2379 io_free_req_deferred(req);
2380}
2381
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002382static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002383{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002384 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002385
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002386 /*
2387 * A ref is owned by io-wq in which context we're. So, if that's the
2388 * last one, it's safe to steal next work. False negatives are Ok,
2389 * it just will be re-punted async in io_put_work()
2390 */
2391 if (refcount_read(&req->refs) != 1)
2392 return NULL;
2393
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002394 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002395 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002396}
2397
Jens Axboe978db572019-11-14 22:39:04 -07002398static void io_double_put_req(struct io_kiocb *req)
2399{
2400 /* drop both submit and complete references */
2401 if (refcount_sub_and_test(2, &req->refs))
2402 io_free_req(req);
2403}
2404
Pavel Begunkov6c503152021-01-04 20:36:36 +00002405static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002406{
2407 /* See comment at the top of this file */
2408 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002409 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002410}
2411
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002412static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2413{
2414 struct io_rings *rings = ctx->rings;
2415
2416 /* make sure SQ entry isn't read before tail */
2417 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2418}
2419
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002420static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002421{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002422 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002423
Jens Axboebcda7ba2020-02-23 16:42:51 -07002424 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2425 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002426 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002427 kfree(kbuf);
2428 return cflags;
2429}
2430
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002431static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2432{
2433 struct io_buffer *kbuf;
2434
2435 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2436 return io_put_kbuf(req, kbuf);
2437}
2438
Jens Axboe4c6e2772020-07-01 11:29:10 -06002439static inline bool io_run_task_work(void)
2440{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002441 /*
2442 * Not safe to run on exiting task, and the task_work handling will
2443 * not add work to such a task.
2444 */
2445 if (unlikely(current->flags & PF_EXITING))
2446 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002447 if (current->task_works) {
2448 __set_current_state(TASK_RUNNING);
2449 task_work_run();
2450 return true;
2451 }
2452
2453 return false;
2454}
2455
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002456static void io_iopoll_queue(struct list_head *again)
2457{
2458 struct io_kiocb *req;
2459
2460 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002461 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2462 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002463 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002464 } while (!list_empty(again));
2465}
2466
Jens Axboedef596e2019-01-09 08:59:42 -07002467/*
2468 * Find and free completed poll iocbs
2469 */
2470static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2471 struct list_head *done)
2472{
Jens Axboe8237e042019-12-28 10:48:22 -07002473 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002474 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002475 LIST_HEAD(again);
2476
2477 /* order with ->result store in io_complete_rw_iopoll() */
2478 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002479
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002480 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002481 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002482 int cflags = 0;
2483
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002484 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002485 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002486 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002487 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002488 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002489 continue;
2490 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002491 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002492
Jens Axboebcda7ba2020-02-23 16:42:51 -07002493 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002494 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002495
2496 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002497 (*nr_events)++;
2498
Pavel Begunkovc3524382020-06-28 12:52:32 +03002499 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002500 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002501 }
Jens Axboedef596e2019-01-09 08:59:42 -07002502
Jens Axboe09bb8392019-03-13 12:39:28 -06002503 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002504 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002505 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002506
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002507 if (!list_empty(&again))
2508 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002509}
2510
Jens Axboedef596e2019-01-09 08:59:42 -07002511static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2512 long min)
2513{
2514 struct io_kiocb *req, *tmp;
2515 LIST_HEAD(done);
2516 bool spin;
2517 int ret;
2518
2519 /*
2520 * Only spin for completions if we don't have multiple devices hanging
2521 * off our complete list, and we're under the requested amount.
2522 */
2523 spin = !ctx->poll_multi_file && *nr_events < min;
2524
2525 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002526 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002527 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002528
2529 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002530 * Move completed and retryable entries to our local lists.
2531 * If we find a request that requires polling, break out
2532 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002533 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002534 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002535 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002536 continue;
2537 }
2538 if (!list_empty(&done))
2539 break;
2540
2541 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2542 if (ret < 0)
2543 break;
2544
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002545 /* iopoll may have completed current req */
2546 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002547 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002548
Jens Axboedef596e2019-01-09 08:59:42 -07002549 if (ret && spin)
2550 spin = false;
2551 ret = 0;
2552 }
2553
2554 if (!list_empty(&done))
2555 io_iopoll_complete(ctx, nr_events, &done);
2556
2557 return ret;
2558}
2559
2560/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002561 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002562 * non-spinning poll check - we'll still enter the driver poll loop, but only
2563 * as a non-spinning completion check.
2564 */
2565static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2566 long min)
2567{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002568 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002569 int ret;
2570
2571 ret = io_do_iopoll(ctx, nr_events, min);
2572 if (ret < 0)
2573 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002574 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002575 return 0;
2576 }
2577
2578 return 1;
2579}
2580
2581/*
2582 * We can't just wait for polled events to come to us, we have to actively
2583 * find and complete them.
2584 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002585static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002586{
2587 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2588 return;
2589
2590 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002591 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002592 unsigned int nr_events = 0;
2593
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002594 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002595
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002596 /* let it sleep and repeat later if can't complete a request */
2597 if (nr_events == 0)
2598 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002599 /*
2600 * Ensure we allow local-to-the-cpu processing to take place,
2601 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002602 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002603 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002604 if (need_resched()) {
2605 mutex_unlock(&ctx->uring_lock);
2606 cond_resched();
2607 mutex_lock(&ctx->uring_lock);
2608 }
Jens Axboedef596e2019-01-09 08:59:42 -07002609 }
2610 mutex_unlock(&ctx->uring_lock);
2611}
2612
Pavel Begunkov7668b922020-07-07 16:36:21 +03002613static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002614{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002615 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002616 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002617
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002618 /*
2619 * We disallow the app entering submit/complete with polling, but we
2620 * still need to lock the ring to prevent racing with polled issue
2621 * that got punted to a workqueue.
2622 */
2623 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002624 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002625 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002626 * Don't enter poll loop if we already have events pending.
2627 * If we do, we can potentially be spinning for commands that
2628 * already triggered a CQE (eg in error).
2629 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002630 if (test_bit(0, &ctx->cq_check_overflow))
2631 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2632 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002633 break;
2634
2635 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002636 * If a submit got punted to a workqueue, we can have the
2637 * application entering polling for a command before it gets
2638 * issued. That app will hold the uring_lock for the duration
2639 * of the poll right here, so we need to take a breather every
2640 * now and then to ensure that the issue has a chance to add
2641 * the poll to the issued list. Otherwise we can spin here
2642 * forever, while the workqueue is stuck trying to acquire the
2643 * very same mutex.
2644 */
2645 if (!(++iters & 7)) {
2646 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002647 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002648 mutex_lock(&ctx->uring_lock);
2649 }
2650
Pavel Begunkov7668b922020-07-07 16:36:21 +03002651 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002652 if (ret <= 0)
2653 break;
2654 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002655 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002656
Jens Axboe500f9fb2019-08-19 12:15:59 -06002657 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002658 return ret;
2659}
2660
Jens Axboe491381ce2019-10-17 09:20:46 -06002661static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002662{
Jens Axboe491381ce2019-10-17 09:20:46 -06002663 /*
2664 * Tell lockdep we inherited freeze protection from submission
2665 * thread.
2666 */
2667 if (req->flags & REQ_F_ISREG) {
2668 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002669
Jens Axboe491381ce2019-10-17 09:20:46 -06002670 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002671 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002672 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002673}
2674
Jens Axboea1d7c392020-06-22 11:09:46 -06002675static void io_complete_rw_common(struct kiocb *kiocb, long res,
2676 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002677{
Jens Axboe9adbd452019-12-20 08:45:55 -07002678 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002679 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002680
Jens Axboe491381ce2019-10-17 09:20:46 -06002681 if (kiocb->ki_flags & IOCB_WRITE)
2682 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002684 if (res != req->result)
2685 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002686 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002687 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002688 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002689}
2690
Jens Axboeb63534c2020-06-04 11:28:00 -06002691#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002692static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002693{
2694 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2695 ssize_t ret = -ECANCELED;
2696 struct iov_iter iter;
2697 int rw;
2698
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002699 /* already prepared */
2700 if (req->async_data)
2701 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002702
2703 switch (req->opcode) {
2704 case IORING_OP_READV:
2705 case IORING_OP_READ_FIXED:
2706 case IORING_OP_READ:
2707 rw = READ;
2708 break;
2709 case IORING_OP_WRITEV:
2710 case IORING_OP_WRITE_FIXED:
2711 case IORING_OP_WRITE:
2712 rw = WRITE;
2713 break;
2714 default:
2715 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2716 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002717 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002718 }
2719
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002720 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2721 if (ret < 0)
2722 return false;
2723 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2724 if (!ret)
Jens Axboeb63534c2020-06-04 11:28:00 -06002725 return true;
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002726 kfree(iovec);
Jens Axboeb63534c2020-06-04 11:28:00 -06002727 return false;
2728}
Jens Axboeb63534c2020-06-04 11:28:00 -06002729#endif
2730
2731static bool io_rw_reissue(struct io_kiocb *req, long res)
2732{
2733#ifdef CONFIG_BLOCK
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002734 umode_t mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002735 int ret;
2736
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002737 if (res != -EAGAIN && res != -EOPNOTSUPP)
Jens Axboe355afae2020-09-02 09:30:31 -06002738 return false;
Pavel Begunkovbf6182b6d2021-01-19 13:32:34 +00002739 mode = file_inode(req->file)->i_mode;
2740 if ((!S_ISBLK(mode) && !S_ISREG(mode)) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002741 return false;
2742
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002743 lockdep_assert_held(&req->ctx->uring_lock);
2744
Jens Axboe28cea78a2020-09-14 10:51:17 -06002745 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002746
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002747 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002748 refcount_inc(&req->refs);
2749 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002750 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002751 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002752 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002753#endif
2754 return false;
2755}
2756
Jens Axboea1d7c392020-06-22 11:09:46 -06002757static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2758 struct io_comp_state *cs)
2759{
2760 if (!io_rw_reissue(req, res))
2761 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002762}
2763
2764static void io_complete_rw(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 Axboeba816ad2019-09-28 11:36:45 -06002767
Jens Axboea1d7c392020-06-22 11:09:46 -06002768 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002769}
2770
Jens Axboedef596e2019-01-09 08:59:42 -07002771static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2772{
Jens Axboe9adbd452019-12-20 08:45:55 -07002773 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002774
Jens Axboe491381ce2019-10-17 09:20:46 -06002775 if (kiocb->ki_flags & IOCB_WRITE)
2776 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002777
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002778 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002779 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002780
2781 WRITE_ONCE(req->result, res);
2782 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002783 smp_wmb();
2784 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002785}
2786
2787/*
2788 * After the iocb has been issued, it's safe to be found on the poll list.
2789 * Adding the kiocb to the list AFTER submission ensures that we don't
2790 * find it from a io_iopoll_getevents() thread before the issuer is done
2791 * accessing the kiocb cookie.
2792 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002793static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002794{
2795 struct io_ring_ctx *ctx = req->ctx;
2796
2797 /*
2798 * Track whether we have multiple files in our lists. This will impact
2799 * how we do polling eventually, not spinning if we're on potentially
2800 * different devices.
2801 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002802 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002803 ctx->poll_multi_file = false;
2804 } else if (!ctx->poll_multi_file) {
2805 struct io_kiocb *list_req;
2806
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002807 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002808 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002809 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002810 ctx->poll_multi_file = true;
2811 }
2812
2813 /*
2814 * For fast devices, IO may have already completed. If it has, add
2815 * it to the front so we find it first.
2816 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002817 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002818 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002819 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002820 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002821
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002822 /*
2823 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2824 * task context or in io worker task context. If current task context is
2825 * sq thread, we don't need to check whether should wake up sq thread.
2826 */
2827 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002828 wq_has_sleeper(&ctx->sq_data->wait))
2829 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002830}
2831
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002832static inline void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002833{
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002834 fput_many(state->file, state->file_refs);
2835 state->file_refs = 0;
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002836}
2837
2838static inline void io_state_file_put(struct io_submit_state *state)
2839{
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002840 if (state->file_refs)
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002841 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002842}
2843
2844/*
2845 * Get as many references to a file as we have IOs left in this submission,
2846 * assuming most submissions are for one file, or at least that each file
2847 * has more than one submission.
2848 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002849static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002850{
2851 if (!state)
2852 return fget(fd);
2853
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002854 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002855 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002856 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002857 return state->file;
2858 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002859 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002860 }
2861 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002862 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002863 return NULL;
2864
2865 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002866 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002867 return state->file;
2868}
2869
Jens Axboe4503b762020-06-01 10:00:27 -06002870static bool io_bdev_nowait(struct block_device *bdev)
2871{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002872 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002873}
2874
Jens Axboe2b188cc2019-01-07 10:46:33 -07002875/*
2876 * If we tracked the file through the SCM inflight mechanism, we could support
2877 * any file. For now, just ensure that anything potentially problematic is done
2878 * inline.
2879 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002880static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002881{
2882 umode_t mode = file_inode(file)->i_mode;
2883
Jens Axboe4503b762020-06-01 10:00:27 -06002884 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002885 if (IS_ENABLED(CONFIG_BLOCK) &&
2886 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002887 return true;
2888 return false;
2889 }
2890 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002891 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002892 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002893 if (IS_ENABLED(CONFIG_BLOCK) &&
2894 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002895 file->f_op != &io_uring_fops)
2896 return true;
2897 return false;
2898 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002899
Jens Axboec5b85622020-06-09 19:23:05 -06002900 /* any ->read/write should understand O_NONBLOCK */
2901 if (file->f_flags & O_NONBLOCK)
2902 return true;
2903
Jens Axboeaf197f52020-04-28 13:15:06 -06002904 if (!(file->f_mode & FMODE_NOWAIT))
2905 return false;
2906
2907 if (rw == READ)
2908 return file->f_op->read_iter != NULL;
2909
2910 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002911}
2912
Pavel Begunkova88fc402020-09-30 22:57:53 +03002913static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002914{
Jens Axboedef596e2019-01-09 08:59:42 -07002915 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002916 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002917 unsigned ioprio;
2918 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002919
Jens Axboe491381ce2019-10-17 09:20:46 -06002920 if (S_ISREG(file_inode(req->file)->i_mode))
2921 req->flags |= REQ_F_ISREG;
2922
Jens Axboe2b188cc2019-01-07 10:46:33 -07002923 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002924 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2925 req->flags |= REQ_F_CUR_POS;
2926 kiocb->ki_pos = req->file->f_pos;
2927 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002928 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002929 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2930 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2931 if (unlikely(ret))
2932 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002933
2934 ioprio = READ_ONCE(sqe->ioprio);
2935 if (ioprio) {
2936 ret = ioprio_check_cap(ioprio);
2937 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002938 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939
2940 kiocb->ki_ioprio = ioprio;
2941 } else
2942 kiocb->ki_ioprio = get_current_ioprio();
2943
Stefan Bühler8449eed2019-04-27 20:34:19 +02002944 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002945 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002946 req->flags |= REQ_F_NOWAIT;
2947
Jens Axboedef596e2019-01-09 08:59:42 -07002948 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002949 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2950 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002951 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952
Jens Axboedef596e2019-01-09 08:59:42 -07002953 kiocb->ki_flags |= IOCB_HIPRI;
2954 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002955 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002956 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002957 if (kiocb->ki_flags & IOCB_HIPRI)
2958 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002959 kiocb->ki_complete = io_complete_rw;
2960 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002961
Jens Axboe3529d8c2019-12-19 18:24:38 -07002962 req->rw.addr = READ_ONCE(sqe->addr);
2963 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002964 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002965 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002966}
2967
2968static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2969{
2970 switch (ret) {
2971 case -EIOCBQUEUED:
2972 break;
2973 case -ERESTARTSYS:
2974 case -ERESTARTNOINTR:
2975 case -ERESTARTNOHAND:
2976 case -ERESTART_RESTARTBLOCK:
2977 /*
2978 * We can't just restart the syscall, since previously
2979 * submitted sqes may already be in progress. Just fail this
2980 * IO with EINTR.
2981 */
2982 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002983 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002984 default:
2985 kiocb->ki_complete(kiocb, ret, 0);
2986 }
2987}
2988
Jens Axboea1d7c392020-06-22 11:09:46 -06002989static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2990 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002991{
Jens Axboeba042912019-12-25 16:33:42 -07002992 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002993 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002994
Jens Axboe227c0c92020-08-13 11:51:40 -06002995 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002996 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002997 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002998 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002999 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003000 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003001 }
3002
Jens Axboeba042912019-12-25 16:33:42 -07003003 if (req->flags & REQ_F_CUR_POS)
3004 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03003005 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06003006 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06003007 else
3008 io_rw_done(kiocb, ret);
3009}
3010
Jens Axboe9adbd452019-12-20 08:45:55 -07003011static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03003012 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07003013{
Jens Axboe9adbd452019-12-20 08:45:55 -07003014 struct io_ring_ctx *ctx = req->ctx;
3015 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003016 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003017 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003018 size_t offset;
3019 u64 buf_addr;
3020
Jens Axboeedafcce2019-01-09 09:16:05 -07003021 if (unlikely(buf_index >= ctx->nr_user_bufs))
3022 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003023 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3024 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003025 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003026
3027 /* overflow */
3028 if (buf_addr + len < buf_addr)
3029 return -EFAULT;
3030 /* not inside the mapped region */
3031 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3032 return -EFAULT;
3033
3034 /*
3035 * May not be a start of buffer, set size appropriately
3036 * and advance us to the beginning.
3037 */
3038 offset = buf_addr - imu->ubuf;
3039 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003040
3041 if (offset) {
3042 /*
3043 * Don't use iov_iter_advance() here, as it's really slow for
3044 * using the latter parts of a big fixed buffer - it iterates
3045 * over each segment manually. We can cheat a bit here, because
3046 * we know that:
3047 *
3048 * 1) it's a BVEC iter, we set it up
3049 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3050 * first and last bvec
3051 *
3052 * So just find our index, and adjust the iterator afterwards.
3053 * If the offset is within the first bvec (or the whole first
3054 * bvec, just use iov_iter_advance(). This makes it easier
3055 * since we can just skip the first segment, which may not
3056 * be PAGE_SIZE aligned.
3057 */
3058 const struct bio_vec *bvec = imu->bvec;
3059
3060 if (offset <= bvec->bv_len) {
3061 iov_iter_advance(iter, offset);
3062 } else {
3063 unsigned long seg_skip;
3064
3065 /* skip first vec */
3066 offset -= bvec->bv_len;
3067 seg_skip = 1 + (offset >> PAGE_SHIFT);
3068
3069 iter->bvec = bvec + seg_skip;
3070 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003071 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003072 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003073 }
3074 }
3075
Jens Axboe5e559562019-11-13 16:12:46 -07003076 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003077}
3078
Jens Axboebcda7ba2020-02-23 16:42:51 -07003079static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3080{
3081 if (needs_lock)
3082 mutex_unlock(&ctx->uring_lock);
3083}
3084
3085static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3086{
3087 /*
3088 * "Normal" inline submissions always hold the uring_lock, since we
3089 * grab it from the system call. Same is true for the SQPOLL offload.
3090 * The only exception is when we've detached the request and issue it
3091 * from an async worker thread, grab the lock for that case.
3092 */
3093 if (needs_lock)
3094 mutex_lock(&ctx->uring_lock);
3095}
3096
3097static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3098 int bgid, struct io_buffer *kbuf,
3099 bool needs_lock)
3100{
3101 struct io_buffer *head;
3102
3103 if (req->flags & REQ_F_BUFFER_SELECTED)
3104 return kbuf;
3105
3106 io_ring_submit_lock(req->ctx, needs_lock);
3107
3108 lockdep_assert_held(&req->ctx->uring_lock);
3109
3110 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3111 if (head) {
3112 if (!list_empty(&head->list)) {
3113 kbuf = list_last_entry(&head->list, struct io_buffer,
3114 list);
3115 list_del(&kbuf->list);
3116 } else {
3117 kbuf = head;
3118 idr_remove(&req->ctx->io_buffer_idr, bgid);
3119 }
3120 if (*len > kbuf->len)
3121 *len = kbuf->len;
3122 } else {
3123 kbuf = ERR_PTR(-ENOBUFS);
3124 }
3125
3126 io_ring_submit_unlock(req->ctx, needs_lock);
3127
3128 return kbuf;
3129}
3130
Jens Axboe4d954c22020-02-27 07:31:19 -07003131static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3132 bool needs_lock)
3133{
3134 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003135 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003136
3137 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003138 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003139 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3140 if (IS_ERR(kbuf))
3141 return kbuf;
3142 req->rw.addr = (u64) (unsigned long) kbuf;
3143 req->flags |= REQ_F_BUFFER_SELECTED;
3144 return u64_to_user_ptr(kbuf->addr);
3145}
3146
3147#ifdef CONFIG_COMPAT
3148static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3149 bool needs_lock)
3150{
3151 struct compat_iovec __user *uiov;
3152 compat_ssize_t clen;
3153 void __user *buf;
3154 ssize_t len;
3155
3156 uiov = u64_to_user_ptr(req->rw.addr);
3157 if (!access_ok(uiov, sizeof(*uiov)))
3158 return -EFAULT;
3159 if (__get_user(clen, &uiov->iov_len))
3160 return -EFAULT;
3161 if (clen < 0)
3162 return -EINVAL;
3163
3164 len = clen;
3165 buf = io_rw_buffer_select(req, &len, needs_lock);
3166 if (IS_ERR(buf))
3167 return PTR_ERR(buf);
3168 iov[0].iov_base = buf;
3169 iov[0].iov_len = (compat_size_t) len;
3170 return 0;
3171}
3172#endif
3173
3174static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3175 bool needs_lock)
3176{
3177 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3178 void __user *buf;
3179 ssize_t len;
3180
3181 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3182 return -EFAULT;
3183
3184 len = iov[0].iov_len;
3185 if (len < 0)
3186 return -EINVAL;
3187 buf = io_rw_buffer_select(req, &len, needs_lock);
3188 if (IS_ERR(buf))
3189 return PTR_ERR(buf);
3190 iov[0].iov_base = buf;
3191 iov[0].iov_len = len;
3192 return 0;
3193}
3194
3195static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3196 bool needs_lock)
3197{
Jens Axboedddb3e22020-06-04 11:27:01 -06003198 if (req->flags & REQ_F_BUFFER_SELECTED) {
3199 struct io_buffer *kbuf;
3200
3201 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3202 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3203 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003204 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003205 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003206 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003207 return -EINVAL;
3208
3209#ifdef CONFIG_COMPAT
3210 if (req->ctx->compat)
3211 return io_compat_import(req, iov, needs_lock);
3212#endif
3213
3214 return __io_iov_buffer_select(req, iov, needs_lock);
3215}
3216
Pavel Begunkov2846c482020-11-07 13:16:27 +00003217static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboe8452fd02020-08-18 13:58:33 -07003218 struct iovec **iovec, struct iov_iter *iter,
3219 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003220{
Jens Axboe9adbd452019-12-20 08:45:55 -07003221 void __user *buf = u64_to_user_ptr(req->rw.addr);
3222 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003223 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003224 u8 opcode;
3225
Jens Axboed625c6e2019-12-17 19:53:05 -07003226 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03003227 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003228 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003229 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003230 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003231
Jens Axboebcda7ba2020-02-23 16:42:51 -07003232 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003233 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003234 return -EINVAL;
3235
Jens Axboe3a6820f2019-12-22 15:19:35 -07003236 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003237 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003238 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003239 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003240 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003241 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003242 }
3243
Jens Axboe3a6820f2019-12-22 15:19:35 -07003244 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3245 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003246 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003247 }
3248
Jens Axboe4d954c22020-02-27 07:31:19 -07003249 if (req->flags & REQ_F_BUFFER_SELECT) {
3250 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003251 if (!ret) {
3252 ret = (*iovec)->iov_len;
3253 iov_iter_init(iter, rw, *iovec, 1, ret);
3254 }
Jens Axboe4d954c22020-02-27 07:31:19 -07003255 *iovec = NULL;
3256 return ret;
3257 }
3258
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003259 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3260 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003261}
3262
Jens Axboe0fef9482020-08-26 10:36:20 -06003263static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3264{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003265 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003266}
3267
Jens Axboe32960612019-09-23 11:05:34 -06003268/*
3269 * For files that don't have ->read_iter() and ->write_iter(), handle them
3270 * by looping over ->read() or ->write() manually.
3271 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003272static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003273{
Jens Axboe4017eb92020-10-22 14:14:12 -06003274 struct kiocb *kiocb = &req->rw.kiocb;
3275 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003276 ssize_t ret = 0;
3277
3278 /*
3279 * Don't support polled IO through this interface, and we can't
3280 * support non-blocking either. For the latter, this just causes
3281 * the kiocb to be handled from an async context.
3282 */
3283 if (kiocb->ki_flags & IOCB_HIPRI)
3284 return -EOPNOTSUPP;
3285 if (kiocb->ki_flags & IOCB_NOWAIT)
3286 return -EAGAIN;
3287
3288 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003289 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003290 ssize_t nr;
3291
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003292 if (!iov_iter_is_bvec(iter)) {
3293 iovec = iov_iter_iovec(iter);
3294 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003295 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3296 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003297 }
3298
Jens Axboe32960612019-09-23 11:05:34 -06003299 if (rw == READ) {
3300 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003301 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003302 } else {
3303 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003304 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003305 }
3306
3307 if (nr < 0) {
3308 if (!ret)
3309 ret = nr;
3310 break;
3311 }
3312 ret += nr;
3313 if (nr != iovec.iov_len)
3314 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003315 req->rw.len -= nr;
3316 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003317 iov_iter_advance(iter, nr);
3318 }
3319
3320 return ret;
3321}
3322
Jens Axboeff6165b2020-08-13 09:47:43 -06003323static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3324 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003325{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003326 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003327
Jens Axboeff6165b2020-08-13 09:47:43 -06003328 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003329 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003330 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003331 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003332 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003333 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003334 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003335 unsigned iov_off = 0;
3336
3337 rw->iter.iov = rw->fast_iov;
3338 if (iter->iov != fast_iov) {
3339 iov_off = iter->iov - fast_iov;
3340 rw->iter.iov += iov_off;
3341 }
3342 if (rw->fast_iov != fast_iov)
3343 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003344 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003345 } else {
3346 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003347 }
3348}
3349
Jens Axboee8c2bc12020-08-15 18:44:09 -07003350static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003351{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003352 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3353 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3354 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003355}
3356
Jens Axboee8c2bc12020-08-15 18:44:09 -07003357static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003358{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003359 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003360 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003361
Jens Axboee8c2bc12020-08-15 18:44:09 -07003362 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003363}
3364
Jens Axboeff6165b2020-08-13 09:47:43 -06003365static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3366 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003367 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003368{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003369 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003370 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003371 if (!req->async_data) {
3372 if (__io_alloc_async_data(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003373 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003374
Jens Axboeff6165b2020-08-13 09:47:43 -06003375 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003376 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003377 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003378}
3379
Pavel Begunkov73debe62020-09-30 22:57:54 +03003380static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003381{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003382 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003383 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003384 ssize_t ret;
3385
Pavel Begunkov2846c482020-11-07 13:16:27 +00003386 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003387 if (unlikely(ret < 0))
3388 return ret;
3389
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003390 iorw->bytes_done = 0;
3391 iorw->free_iovec = iov;
3392 if (iov)
3393 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003394 return 0;
3395}
3396
Pavel Begunkov73debe62020-09-30 22:57:54 +03003397static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003398{
3399 ssize_t ret;
3400
Pavel Begunkova88fc402020-09-30 22:57:53 +03003401 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003402 if (ret)
3403 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003404
Jens Axboe3529d8c2019-12-19 18:24:38 -07003405 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3406 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003407
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003408 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003409 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003410 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003411 return io_rw_prep_async(req, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003412}
3413
Jens Axboec1dd91d2020-08-03 16:43:59 -06003414/*
3415 * This is our waitqueue callback handler, registered through lock_page_async()
3416 * when we initially tried to do the IO with the iocb armed our waitqueue.
3417 * This gets called when the page is unlocked, and we generally expect that to
3418 * happen when the page IO is completed and the page is now uptodate. This will
3419 * queue a task_work based retry of the operation, attempting to copy the data
3420 * again. If the latter fails because the page was NOT uptodate, then we will
3421 * do a thread based blocking retry of the operation. That's the unexpected
3422 * slow path.
3423 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003424static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3425 int sync, void *arg)
3426{
3427 struct wait_page_queue *wpq;
3428 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003429 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003430 int ret;
3431
3432 wpq = container_of(wait, struct wait_page_queue, wait);
3433
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003434 if (!wake_page_match(wpq, key))
3435 return 0;
3436
Hao Xuc8d317a2020-09-29 20:00:45 +08003437 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003438 list_del_init(&wait->entry);
3439
Pavel Begunkove7375122020-07-12 20:42:04 +03003440 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003441 percpu_ref_get(&req->ctx->refs);
3442
Jens Axboebcf5a062020-05-22 09:24:42 -06003443 /* submit ref gets dropped, acquire a new one */
3444 refcount_inc(&req->refs);
Jens Axboe355fb9e2020-10-22 20:19:35 -06003445 ret = io_req_task_work_add(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003446 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003447 struct task_struct *tsk;
3448
Jens Axboebcf5a062020-05-22 09:24:42 -06003449 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003450 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003451 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06003452 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboec2c4c832020-07-01 15:37:11 -06003453 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003454 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003455 return 1;
3456}
3457
Jens Axboec1dd91d2020-08-03 16:43:59 -06003458/*
3459 * This controls whether a given IO request should be armed for async page
3460 * based retry. If we return false here, the request is handed to the async
3461 * worker threads for retry. If we're doing buffered reads on a regular file,
3462 * we prepare a private wait_page_queue entry and retry the operation. This
3463 * will either succeed because the page is now uptodate and unlocked, or it
3464 * will register a callback when the page is unlocked at IO completion. Through
3465 * that callback, io_uring uses task_work to setup a retry of the operation.
3466 * That retry will attempt the buffered read again. The retry will generally
3467 * succeed, or in rare cases where it fails, we then fall back to using the
3468 * async worker threads for a blocking retry.
3469 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003470static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003471{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003472 struct io_async_rw *rw = req->async_data;
3473 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003474 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003475
3476 /* never retry for NOWAIT, we just complete with -EAGAIN */
3477 if (req->flags & REQ_F_NOWAIT)
3478 return false;
3479
Jens Axboe227c0c92020-08-13 11:51:40 -06003480 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003481 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003482 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003483
Jens Axboebcf5a062020-05-22 09:24:42 -06003484 /*
3485 * just use poll if we can, and don't attempt if the fs doesn't
3486 * support callback based unlocks
3487 */
3488 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3489 return false;
3490
Jens Axboe3b2a4432020-08-16 10:58:43 -07003491 wait->wait.func = io_async_buf_func;
3492 wait->wait.private = req;
3493 wait->wait.flags = 0;
3494 INIT_LIST_HEAD(&wait->wait.entry);
3495 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003496 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003497 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003498 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003499}
3500
3501static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3502{
3503 if (req->file->f_op->read_iter)
3504 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003505 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003506 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003507 else
3508 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003509}
3510
Jens Axboea1d7c392020-06-22 11:09:46 -06003511static int io_read(struct io_kiocb *req, bool force_nonblock,
3512 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003513{
3514 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003515 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003516 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003517 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003518 ssize_t io_size, ret, ret2;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003519 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003520
Pavel Begunkov2846c482020-11-07 13:16:27 +00003521 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003522 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003523 iovec = NULL;
3524 } else {
3525 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3526 if (ret < 0)
3527 return ret;
3528 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003529 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003530 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003531 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003532
Jens Axboefd6c2e42019-12-18 12:19:41 -07003533 /* Ensure we clear previously set non-block flag */
3534 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003535 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003536 else
3537 kiocb->ki_flags |= IOCB_NOWAIT;
3538
Jens Axboefd6c2e42019-12-18 12:19:41 -07003539
Pavel Begunkov24c74672020-06-21 13:09:51 +03003540 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003541 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3542 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003543 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003544
Pavel Begunkov632546c2020-11-07 13:16:26 +00003545 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003546 if (unlikely(ret))
3547 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003548
Jens Axboe227c0c92020-08-13 11:51:40 -06003549 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003550
Jens Axboe227c0c92020-08-13 11:51:40 -06003551 if (!ret) {
3552 goto done;
3553 } else if (ret == -EIOCBQUEUED) {
3554 ret = 0;
3555 goto out_free;
3556 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003557 /* IOPOLL retry should happen for io-wq threads */
3558 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003559 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003560 /* no retry on NONBLOCK marked file */
3561 if (req->file->f_flags & O_NONBLOCK)
3562 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003563 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003564 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003565 ret = 0;
3566 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003567 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003568 /* make sure -ERESTARTSYS -> -EINTR is done */
3569 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003570 }
3571
3572 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003573 if (!iov_iter_count(iter) || !force_nonblock ||
Pavel Begunkov9a173342021-01-21 12:01:08 +00003574 (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG))
Jens Axboe227c0c92020-08-13 11:51:40 -06003575 goto done;
3576
3577 io_size -= ret;
3578copy_iov:
3579 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3580 if (ret2) {
3581 ret = ret2;
3582 goto out_free;
3583 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003584 if (no_async)
3585 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003586 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003587 /* it's copied and will be cleaned with ->io */
3588 iovec = NULL;
3589 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003590 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003591retry:
Jens Axboee8c2bc12020-08-15 18:44:09 -07003592 rw->bytes_done += ret;
Jens Axboe227c0c92020-08-13 11:51:40 -06003593 /* if we can retry, do so with the callbacks armed */
3594 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003595 kiocb->ki_flags &= ~IOCB_WAITQ;
3596 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003597 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003598
3599 /*
3600 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3601 * get -EIOCBQUEUED, then we'll get a notification when the desired
3602 * page gets unlocked. We can also get a partial read here, and if we
3603 * do, then just retry at the new offset.
3604 */
3605 ret = io_iter_do_read(req, iter);
3606 if (ret == -EIOCBQUEUED) {
3607 ret = 0;
3608 goto out_free;
3609 } else if (ret > 0 && ret < io_size) {
3610 /* we got some bytes, but not all. retry. */
3611 goto retry;
3612 }
3613done:
3614 kiocb_done(kiocb, ret, cs);
3615 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003616out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003617 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003618 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003619 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003620 return ret;
3621}
3622
Pavel Begunkov73debe62020-09-30 22:57:54 +03003623static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003624{
3625 ssize_t ret;
3626
Pavel Begunkova88fc402020-09-30 22:57:53 +03003627 ret = io_prep_rw(req, sqe);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003628 if (ret)
3629 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003630
Jens Axboe3529d8c2019-12-19 18:24:38 -07003631 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3632 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003633
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003634 /* either don't need iovec imported or already have it */
Pavel Begunkov2d199892020-09-30 22:57:35 +03003635 if (!req->async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003636 return 0;
Pavel Begunkov73debe62020-09-30 22:57:54 +03003637 return io_rw_prep_async(req, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003638}
3639
Jens Axboea1d7c392020-06-22 11:09:46 -06003640static int io_write(struct io_kiocb *req, bool force_nonblock,
3641 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003642{
3643 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003644 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003645 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003646 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003647 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003648
Pavel Begunkov2846c482020-11-07 13:16:27 +00003649 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003650 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003651 iovec = NULL;
3652 } else {
3653 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3654 if (ret < 0)
3655 return ret;
3656 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003657 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003658 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003659
Jens Axboefd6c2e42019-12-18 12:19:41 -07003660 /* Ensure we clear previously set non-block flag */
3661 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003662 kiocb->ki_flags &= ~IOCB_NOWAIT;
3663 else
3664 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003665
Pavel Begunkov24c74672020-06-21 13:09:51 +03003666 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003667 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003668 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003669
Jens Axboe10d59342019-12-09 20:16:22 -07003670 /* file path doesn't support NOWAIT for non-direct_IO */
3671 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3672 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003673 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003674
Pavel Begunkov632546c2020-11-07 13:16:26 +00003675 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003676 if (unlikely(ret))
3677 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003678
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003679 /*
3680 * Open-code file_start_write here to grab freeze protection,
3681 * which will be released by another thread in
3682 * io_complete_rw(). Fool lockdep by telling it the lock got
3683 * released so that it doesn't complain about the held lock when
3684 * we return to userspace.
3685 */
3686 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003687 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003688 __sb_writers_release(file_inode(req->file)->i_sb,
3689 SB_FREEZE_WRITE);
3690 }
3691 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003692
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003693 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003694 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003695 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003696 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003697 else
3698 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003699
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003700 /*
3701 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3702 * retry them without IOCB_NOWAIT.
3703 */
3704 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3705 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003706 /* no retry on NONBLOCK marked file */
3707 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3708 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003709 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003710 /* IOPOLL retry should happen for io-wq threads */
3711 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3712 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003713done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003714 kiocb_done(kiocb, ret2, cs);
3715 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003716copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003717 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003718 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003719 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003720 if (!ret)
3721 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003722 }
Jens Axboe31b51512019-01-18 22:56:34 -07003723out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003724 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003725 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003726 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003727 return ret;
3728}
3729
Jens Axboe80a261f2020-09-28 14:23:58 -06003730static int io_renameat_prep(struct io_kiocb *req,
3731 const struct io_uring_sqe *sqe)
3732{
3733 struct io_rename *ren = &req->rename;
3734 const char __user *oldf, *newf;
3735
3736 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3737 return -EBADF;
3738
3739 ren->old_dfd = READ_ONCE(sqe->fd);
3740 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3741 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3742 ren->new_dfd = READ_ONCE(sqe->len);
3743 ren->flags = READ_ONCE(sqe->rename_flags);
3744
3745 ren->oldpath = getname(oldf);
3746 if (IS_ERR(ren->oldpath))
3747 return PTR_ERR(ren->oldpath);
3748
3749 ren->newpath = getname(newf);
3750 if (IS_ERR(ren->newpath)) {
3751 putname(ren->oldpath);
3752 return PTR_ERR(ren->newpath);
3753 }
3754
3755 req->flags |= REQ_F_NEED_CLEANUP;
3756 return 0;
3757}
3758
3759static int io_renameat(struct io_kiocb *req, bool force_nonblock)
3760{
3761 struct io_rename *ren = &req->rename;
3762 int ret;
3763
3764 if (force_nonblock)
3765 return -EAGAIN;
3766
3767 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3768 ren->newpath, ren->flags);
3769
3770 req->flags &= ~REQ_F_NEED_CLEANUP;
3771 if (ret < 0)
3772 req_set_fail_links(req);
3773 io_req_complete(req, ret);
3774 return 0;
3775}
3776
Jens Axboe14a11432020-09-28 14:27:37 -06003777static int io_unlinkat_prep(struct io_kiocb *req,
3778 const struct io_uring_sqe *sqe)
3779{
3780 struct io_unlink *un = &req->unlink;
3781 const char __user *fname;
3782
3783 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3784 return -EBADF;
3785
3786 un->dfd = READ_ONCE(sqe->fd);
3787
3788 un->flags = READ_ONCE(sqe->unlink_flags);
3789 if (un->flags & ~AT_REMOVEDIR)
3790 return -EINVAL;
3791
3792 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3793 un->filename = getname(fname);
3794 if (IS_ERR(un->filename))
3795 return PTR_ERR(un->filename);
3796
3797 req->flags |= REQ_F_NEED_CLEANUP;
3798 return 0;
3799}
3800
3801static int io_unlinkat(struct io_kiocb *req, bool force_nonblock)
3802{
3803 struct io_unlink *un = &req->unlink;
3804 int ret;
3805
3806 if (force_nonblock)
3807 return -EAGAIN;
3808
3809 if (un->flags & AT_REMOVEDIR)
3810 ret = do_rmdir(un->dfd, un->filename);
3811 else
3812 ret = do_unlinkat(un->dfd, un->filename);
3813
3814 req->flags &= ~REQ_F_NEED_CLEANUP;
3815 if (ret < 0)
3816 req_set_fail_links(req);
3817 io_req_complete(req, ret);
3818 return 0;
3819}
3820
Jens Axboe36f4fa62020-09-05 11:14:22 -06003821static int io_shutdown_prep(struct io_kiocb *req,
3822 const struct io_uring_sqe *sqe)
3823{
3824#if defined(CONFIG_NET)
3825 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3826 return -EINVAL;
3827 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3828 sqe->buf_index)
3829 return -EINVAL;
3830
3831 req->shutdown.how = READ_ONCE(sqe->len);
3832 return 0;
3833#else
3834 return -EOPNOTSUPP;
3835#endif
3836}
3837
3838static int io_shutdown(struct io_kiocb *req, bool force_nonblock)
3839{
3840#if defined(CONFIG_NET)
3841 struct socket *sock;
3842 int ret;
3843
3844 if (force_nonblock)
3845 return -EAGAIN;
3846
Linus Torvalds48aba792020-12-16 12:44:05 -08003847 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003848 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003849 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003850
3851 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003852 if (ret < 0)
3853 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003854 io_req_complete(req, ret);
3855 return 0;
3856#else
3857 return -EOPNOTSUPP;
3858#endif
3859}
3860
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003861static int __io_splice_prep(struct io_kiocb *req,
3862 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003863{
3864 struct io_splice* sp = &req->splice;
3865 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003866
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003867 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3868 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003869
3870 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003871 sp->len = READ_ONCE(sqe->len);
3872 sp->flags = READ_ONCE(sqe->splice_flags);
3873
3874 if (unlikely(sp->flags & ~valid_flags))
3875 return -EINVAL;
3876
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003877 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3878 (sp->flags & SPLICE_F_FD_IN_FIXED));
3879 if (!sp->file_in)
3880 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003881 req->flags |= REQ_F_NEED_CLEANUP;
3882
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003883 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3884 /*
3885 * Splice operation will be punted aync, and here need to
3886 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3887 */
3888 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003889 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003890 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003891
3892 return 0;
3893}
3894
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003895static int io_tee_prep(struct io_kiocb *req,
3896 const struct io_uring_sqe *sqe)
3897{
3898 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3899 return -EINVAL;
3900 return __io_splice_prep(req, sqe);
3901}
3902
3903static int io_tee(struct io_kiocb *req, bool force_nonblock)
3904{
3905 struct io_splice *sp = &req->splice;
3906 struct file *in = sp->file_in;
3907 struct file *out = sp->file_out;
3908 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3909 long ret = 0;
3910
3911 if (force_nonblock)
3912 return -EAGAIN;
3913 if (sp->len)
3914 ret = do_tee(in, out, sp->len, flags);
3915
3916 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3917 req->flags &= ~REQ_F_NEED_CLEANUP;
3918
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003919 if (ret != sp->len)
3920 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003921 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003922 return 0;
3923}
3924
3925static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3926{
3927 struct io_splice* sp = &req->splice;
3928
3929 sp->off_in = READ_ONCE(sqe->splice_off_in);
3930 sp->off_out = READ_ONCE(sqe->off);
3931 return __io_splice_prep(req, sqe);
3932}
3933
Pavel Begunkov014db002020-03-03 21:33:12 +03003934static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003935{
3936 struct io_splice *sp = &req->splice;
3937 struct file *in = sp->file_in;
3938 struct file *out = sp->file_out;
3939 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3940 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003941 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003942
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003943 if (force_nonblock)
3944 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003945
3946 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3947 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003948
Jens Axboe948a7742020-05-17 14:21:38 -06003949 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003950 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003951
3952 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3953 req->flags &= ~REQ_F_NEED_CLEANUP;
3954
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003955 if (ret != sp->len)
3956 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003957 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003958 return 0;
3959}
3960
Jens Axboe2b188cc2019-01-07 10:46:33 -07003961/*
3962 * IORING_OP_NOP just posts a completion event, nothing else.
3963 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003964static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003965{
3966 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003967
Jens Axboedef596e2019-01-09 08:59:42 -07003968 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3969 return -EINVAL;
3970
Jens Axboe229a7b62020-06-22 10:13:11 -06003971 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003972 return 0;
3973}
3974
Jens Axboe3529d8c2019-12-19 18:24:38 -07003975static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003976{
Jens Axboe6b063142019-01-10 22:13:58 -07003977 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003978
Jens Axboe09bb8392019-03-13 12:39:28 -06003979 if (!req->file)
3980 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003981
Jens Axboe6b063142019-01-10 22:13:58 -07003982 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003983 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003984 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003985 return -EINVAL;
3986
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003987 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3988 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3989 return -EINVAL;
3990
3991 req->sync.off = READ_ONCE(sqe->off);
3992 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003993 return 0;
3994}
3995
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003996static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003997{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003998 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003999 int ret;
4000
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004001 /* fsync always requires a blocking context */
4002 if (force_nonblock)
4003 return -EAGAIN;
4004
Jens Axboe9adbd452019-12-20 08:45:55 -07004005 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004006 end > 0 ? end : LLONG_MAX,
4007 req->sync.flags & IORING_FSYNC_DATASYNC);
4008 if (ret < 0)
4009 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004010 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004011 return 0;
4012}
4013
Jens Axboed63d1b52019-12-10 10:38:56 -07004014static int io_fallocate_prep(struct io_kiocb *req,
4015 const struct io_uring_sqe *sqe)
4016{
4017 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
4018 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004019 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4020 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004021
4022 req->sync.off = READ_ONCE(sqe->off);
4023 req->sync.len = READ_ONCE(sqe->addr);
4024 req->sync.mode = READ_ONCE(sqe->len);
4025 return 0;
4026}
4027
Pavel Begunkov014db002020-03-03 21:33:12 +03004028static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07004029{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004030 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004031
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004032 /* fallocate always requiring blocking context */
4033 if (force_nonblock)
4034 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004035 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4036 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004037 if (ret < 0)
4038 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004039 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004040 return 0;
4041}
4042
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004043static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004044{
Jens Axboef8748882020-01-08 17:47:02 -07004045 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004046 int ret;
4047
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004048 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004049 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004050 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004051 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004052
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004053 /* open.how should be already initialised */
4054 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004055 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004056
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004057 req->open.dfd = READ_ONCE(sqe->fd);
4058 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004059 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004060 if (IS_ERR(req->open.filename)) {
4061 ret = PTR_ERR(req->open.filename);
4062 req->open.filename = NULL;
4063 return ret;
4064 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004065 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004066 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004067 return 0;
4068}
4069
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004070static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4071{
4072 u64 flags, mode;
4073
Jens Axboe14587a462020-09-05 11:36:08 -06004074 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004075 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004076 mode = READ_ONCE(sqe->len);
4077 flags = READ_ONCE(sqe->open_flags);
4078 req->open.how = build_open_how(flags, mode);
4079 return __io_openat_prep(req, sqe);
4080}
4081
Jens Axboecebdb982020-01-08 17:59:24 -07004082static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4083{
4084 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004085 size_t len;
4086 int ret;
4087
Jens Axboe14587a462020-09-05 11:36:08 -06004088 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004089 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004090 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4091 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004092 if (len < OPEN_HOW_SIZE_VER0)
4093 return -EINVAL;
4094
4095 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4096 len);
4097 if (ret)
4098 return ret;
4099
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004100 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004101}
4102
Pavel Begunkov014db002020-03-03 21:33:12 +03004103static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004104{
4105 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004106 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004107 bool nonblock_set;
4108 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004109 int ret;
4110
Jens Axboecebdb982020-01-08 17:59:24 -07004111 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004112 if (ret)
4113 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004114 nonblock_set = op.open_flag & O_NONBLOCK;
4115 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
4116 if (force_nonblock) {
4117 /*
4118 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4119 * it'll always -EAGAIN
4120 */
4121 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4122 return -EAGAIN;
4123 op.lookup_flags |= LOOKUP_CACHED;
4124 op.open_flag |= O_NONBLOCK;
4125 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004126
Jens Axboe4022e7a2020-03-19 19:23:18 -06004127 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004128 if (ret < 0)
4129 goto err;
4130
4131 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004132 /* only retry if RESOLVE_CACHED wasn't already set by application */
4133 if ((!resolve_nonblock && force_nonblock) && file == ERR_PTR(-EAGAIN)) {
4134 /*
4135 * We could hang on to this 'fd', but seems like marginal
4136 * gain for something that is now known to be a slower path.
4137 * So just put it, and we'll get a new one when we retry.
4138 */
4139 put_unused_fd(ret);
4140 return -EAGAIN;
4141 }
4142
Jens Axboe15b71ab2019-12-11 11:20:36 -07004143 if (IS_ERR(file)) {
4144 put_unused_fd(ret);
4145 ret = PTR_ERR(file);
4146 } else {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004147 if (force_nonblock && !nonblock_set)
4148 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004149 fsnotify_open(file);
4150 fd_install(ret, file);
4151 }
4152err:
4153 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004154 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004155 if (ret < 0)
4156 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004157 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004158 return 0;
4159}
4160
Pavel Begunkov014db002020-03-03 21:33:12 +03004161static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07004162{
Pavel Begunkov014db002020-03-03 21:33:12 +03004163 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07004164}
4165
Jens Axboe067524e2020-03-02 16:32:28 -07004166static int io_remove_buffers_prep(struct io_kiocb *req,
4167 const struct io_uring_sqe *sqe)
4168{
4169 struct io_provide_buf *p = &req->pbuf;
4170 u64 tmp;
4171
4172 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4173 return -EINVAL;
4174
4175 tmp = READ_ONCE(sqe->fd);
4176 if (!tmp || tmp > USHRT_MAX)
4177 return -EINVAL;
4178
4179 memset(p, 0, sizeof(*p));
4180 p->nbufs = tmp;
4181 p->bgid = READ_ONCE(sqe->buf_group);
4182 return 0;
4183}
4184
4185static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4186 int bgid, unsigned nbufs)
4187{
4188 unsigned i = 0;
4189
4190 /* shouldn't happen */
4191 if (!nbufs)
4192 return 0;
4193
4194 /* the head kbuf is the list itself */
4195 while (!list_empty(&buf->list)) {
4196 struct io_buffer *nxt;
4197
4198 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4199 list_del(&nxt->list);
4200 kfree(nxt);
4201 if (++i == nbufs)
4202 return i;
4203 }
4204 i++;
4205 kfree(buf);
4206 idr_remove(&ctx->io_buffer_idr, bgid);
4207
4208 return i;
4209}
4210
Jens Axboe229a7b62020-06-22 10:13:11 -06004211static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
4212 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07004213{
4214 struct io_provide_buf *p = &req->pbuf;
4215 struct io_ring_ctx *ctx = req->ctx;
4216 struct io_buffer *head;
4217 int ret = 0;
4218
4219 io_ring_submit_lock(ctx, !force_nonblock);
4220
4221 lockdep_assert_held(&ctx->uring_lock);
4222
4223 ret = -ENOENT;
4224 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4225 if (head)
4226 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004227 if (ret < 0)
4228 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004229
4230 /* need to hold the lock to complete IOPOLL requests */
4231 if (ctx->flags & IORING_SETUP_IOPOLL) {
4232 __io_req_complete(req, ret, 0, cs);
4233 io_ring_submit_unlock(ctx, !force_nonblock);
4234 } else {
4235 io_ring_submit_unlock(ctx, !force_nonblock);
4236 __io_req_complete(req, ret, 0, cs);
4237 }
Jens Axboe067524e2020-03-02 16:32:28 -07004238 return 0;
4239}
4240
Jens Axboeddf0322d2020-02-23 16:41:33 -07004241static int io_provide_buffers_prep(struct io_kiocb *req,
4242 const struct io_uring_sqe *sqe)
4243{
4244 struct io_provide_buf *p = &req->pbuf;
4245 u64 tmp;
4246
4247 if (sqe->ioprio || sqe->rw_flags)
4248 return -EINVAL;
4249
4250 tmp = READ_ONCE(sqe->fd);
4251 if (!tmp || tmp > USHRT_MAX)
4252 return -E2BIG;
4253 p->nbufs = tmp;
4254 p->addr = READ_ONCE(sqe->addr);
4255 p->len = READ_ONCE(sqe->len);
4256
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004257 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004258 return -EFAULT;
4259
4260 p->bgid = READ_ONCE(sqe->buf_group);
4261 tmp = READ_ONCE(sqe->off);
4262 if (tmp > USHRT_MAX)
4263 return -E2BIG;
4264 p->bid = tmp;
4265 return 0;
4266}
4267
4268static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4269{
4270 struct io_buffer *buf;
4271 u64 addr = pbuf->addr;
4272 int i, bid = pbuf->bid;
4273
4274 for (i = 0; i < pbuf->nbufs; i++) {
4275 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4276 if (!buf)
4277 break;
4278
4279 buf->addr = addr;
4280 buf->len = pbuf->len;
4281 buf->bid = bid;
4282 addr += pbuf->len;
4283 bid++;
4284 if (!*head) {
4285 INIT_LIST_HEAD(&buf->list);
4286 *head = buf;
4287 } else {
4288 list_add_tail(&buf->list, &(*head)->list);
4289 }
4290 }
4291
4292 return i ? i : -ENOMEM;
4293}
4294
Jens Axboe229a7b62020-06-22 10:13:11 -06004295static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
4296 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004297{
4298 struct io_provide_buf *p = &req->pbuf;
4299 struct io_ring_ctx *ctx = req->ctx;
4300 struct io_buffer *head, *list;
4301 int ret = 0;
4302
4303 io_ring_submit_lock(ctx, !force_nonblock);
4304
4305 lockdep_assert_held(&ctx->uring_lock);
4306
4307 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4308
4309 ret = io_add_buffers(p, &head);
4310 if (ret < 0)
4311 goto out;
4312
4313 if (!list) {
4314 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4315 GFP_KERNEL);
4316 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004317 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004318 goto out;
4319 }
4320 }
4321out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004322 if (ret < 0)
4323 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004324
4325 /* need to hold the lock to complete IOPOLL requests */
4326 if (ctx->flags & IORING_SETUP_IOPOLL) {
4327 __io_req_complete(req, ret, 0, cs);
4328 io_ring_submit_unlock(ctx, !force_nonblock);
4329 } else {
4330 io_ring_submit_unlock(ctx, !force_nonblock);
4331 __io_req_complete(req, ret, 0, cs);
4332 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004333 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004334}
4335
Jens Axboe3e4827b2020-01-08 15:18:09 -07004336static int io_epoll_ctl_prep(struct io_kiocb *req,
4337 const struct io_uring_sqe *sqe)
4338{
4339#if defined(CONFIG_EPOLL)
4340 if (sqe->ioprio || sqe->buf_index)
4341 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004342 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004343 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004344
4345 req->epoll.epfd = READ_ONCE(sqe->fd);
4346 req->epoll.op = READ_ONCE(sqe->len);
4347 req->epoll.fd = READ_ONCE(sqe->off);
4348
4349 if (ep_op_has_event(req->epoll.op)) {
4350 struct epoll_event __user *ev;
4351
4352 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4353 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4354 return -EFAULT;
4355 }
4356
4357 return 0;
4358#else
4359 return -EOPNOTSUPP;
4360#endif
4361}
4362
Jens Axboe229a7b62020-06-22 10:13:11 -06004363static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
4364 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004365{
4366#if defined(CONFIG_EPOLL)
4367 struct io_epoll *ie = &req->epoll;
4368 int ret;
4369
4370 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4371 if (force_nonblock && ret == -EAGAIN)
4372 return -EAGAIN;
4373
4374 if (ret < 0)
4375 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004376 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004377 return 0;
4378#else
4379 return -EOPNOTSUPP;
4380#endif
4381}
4382
Jens Axboec1ca7572019-12-25 22:18:28 -07004383static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4384{
4385#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4386 if (sqe->ioprio || sqe->buf_index || sqe->off)
4387 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004388 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4389 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004390
4391 req->madvise.addr = READ_ONCE(sqe->addr);
4392 req->madvise.len = READ_ONCE(sqe->len);
4393 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4394 return 0;
4395#else
4396 return -EOPNOTSUPP;
4397#endif
4398}
4399
Pavel Begunkov014db002020-03-03 21:33:12 +03004400static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07004401{
4402#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4403 struct io_madvise *ma = &req->madvise;
4404 int ret;
4405
4406 if (force_nonblock)
4407 return -EAGAIN;
4408
Minchan Kim0726b012020-10-17 16:14:50 -07004409 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004410 if (ret < 0)
4411 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004412 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004413 return 0;
4414#else
4415 return -EOPNOTSUPP;
4416#endif
4417}
4418
Jens Axboe4840e412019-12-25 22:03:45 -07004419static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4420{
4421 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4422 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004423 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4424 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004425
4426 req->fadvise.offset = READ_ONCE(sqe->off);
4427 req->fadvise.len = READ_ONCE(sqe->len);
4428 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4429 return 0;
4430}
4431
Pavel Begunkov014db002020-03-03 21:33:12 +03004432static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07004433{
4434 struct io_fadvise *fa = &req->fadvise;
4435 int ret;
4436
Jens Axboe3e694262020-02-01 09:22:49 -07004437 if (force_nonblock) {
4438 switch (fa->advice) {
4439 case POSIX_FADV_NORMAL:
4440 case POSIX_FADV_RANDOM:
4441 case POSIX_FADV_SEQUENTIAL:
4442 break;
4443 default:
4444 return -EAGAIN;
4445 }
4446 }
Jens Axboe4840e412019-12-25 22:03:45 -07004447
4448 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4449 if (ret < 0)
4450 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004451 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004452 return 0;
4453}
4454
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004455static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4456{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004457 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004458 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004459 if (sqe->ioprio || sqe->buf_index)
4460 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004461 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004462 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004463
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004464 req->statx.dfd = READ_ONCE(sqe->fd);
4465 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004466 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004467 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4468 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004469
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004470 return 0;
4471}
4472
Pavel Begunkov014db002020-03-03 21:33:12 +03004473static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004474{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004475 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004476 int ret;
4477
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004478 if (force_nonblock) {
4479 /* only need file table for an actual valid fd */
4480 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4481 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004482 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004483 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004484
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004485 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4486 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004487
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004488 if (ret < 0)
4489 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004490 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004491 return 0;
4492}
4493
Jens Axboeb5dba592019-12-11 14:02:38 -07004494static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4495{
4496 /*
4497 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004498 * leave the 'file' in an undeterminate state, and here need to modify
4499 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004500 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004501 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004502
Jens Axboe14587a462020-09-05 11:36:08 -06004503 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004504 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004505 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4506 sqe->rw_flags || sqe->buf_index)
4507 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004508 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004509 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004510
4511 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004512 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004513 return -EBADF;
4514
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004515 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004516 return 0;
4517}
4518
Jens Axboe229a7b62020-06-22 10:13:11 -06004519static int io_close(struct io_kiocb *req, bool force_nonblock,
4520 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004521{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004522 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004523 int ret;
4524
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004525 /* might be already done during nonblock submission */
4526 if (!close->put_file) {
Eric W. Biederman9fe83c42020-11-20 17:14:40 -06004527 ret = close_fd_get_file(close->fd, &close->put_file);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004528 if (ret < 0)
4529 return (ret == -ENOENT) ? -EBADF : ret;
4530 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004531
4532 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004533 if (close->put_file->f_op->flush && force_nonblock) {
Jens Axboe607ec892021-01-19 10:10:54 -07004534 /* not safe to cancel at this point */
4535 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004536 /* was never set, but play safe */
4537 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004538 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004539 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004540 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004541 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004542
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004543 /* No ->flush() or already async, safely close from here */
Jens Axboe98447d62020-10-14 10:48:51 -06004544 ret = filp_close(close->put_file, req->work.identity->files);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004545 if (ret < 0)
4546 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004547 fput(close->put_file);
4548 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004549 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004550 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004551}
4552
Jens Axboe3529d8c2019-12-19 18:24:38 -07004553static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004554{
4555 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004556
4557 if (!req->file)
4558 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004559
4560 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4561 return -EINVAL;
4562 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4563 return -EINVAL;
4564
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004565 req->sync.off = READ_ONCE(sqe->off);
4566 req->sync.len = READ_ONCE(sqe->len);
4567 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004568 return 0;
4569}
4570
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004571static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004572{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004573 int ret;
4574
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004575 /* sync_file_range always requires a blocking context */
4576 if (force_nonblock)
4577 return -EAGAIN;
4578
Jens Axboe9adbd452019-12-20 08:45:55 -07004579 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004580 req->sync.flags);
4581 if (ret < 0)
4582 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004583 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004584 return 0;
4585}
4586
YueHaibing469956e2020-03-04 15:53:52 +08004587#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004588static int io_setup_async_msg(struct io_kiocb *req,
4589 struct io_async_msghdr *kmsg)
4590{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004591 struct io_async_msghdr *async_msg = req->async_data;
4592
4593 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004594 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004595 if (io_alloc_async_data(req)) {
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004596 if (kmsg->iov != kmsg->fast_iov)
4597 kfree(kmsg->iov);
4598 return -ENOMEM;
4599 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004600 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004601 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004602 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004603 return -EAGAIN;
4604}
4605
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004606static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4607 struct io_async_msghdr *iomsg)
4608{
4609 iomsg->iov = iomsg->fast_iov;
4610 iomsg->msg.msg_name = &iomsg->addr;
4611 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4612 req->sr_msg.msg_flags, &iomsg->iov);
4613}
4614
Jens Axboe3529d8c2019-12-19 18:24:38 -07004615static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004616{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004617 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004618 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004619 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004620
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004621 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4622 return -EINVAL;
4623
Jens Axboee47293f2019-12-20 08:58:21 -07004624 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004625 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004626 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004627
Jens Axboed8768362020-02-27 14:17:49 -07004628#ifdef CONFIG_COMPAT
4629 if (req->ctx->compat)
4630 sr->msg_flags |= MSG_CMSG_COMPAT;
4631#endif
4632
Jens Axboee8c2bc12020-08-15 18:44:09 -07004633 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004634 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004635 ret = io_sendmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004636 if (!ret)
4637 req->flags |= REQ_F_NEED_CLEANUP;
4638 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004639}
4640
Jens Axboe229a7b62020-06-22 10:13:11 -06004641static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4642 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004643{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004644 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004645 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004646 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004647 int ret;
4648
Florent Revestdba4a922020-12-04 12:36:04 +01004649 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004650 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004651 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004652
Jens Axboee8c2bc12020-08-15 18:44:09 -07004653 if (req->async_data) {
4654 kmsg = req->async_data;
4655 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004656 /* if iov is set, it's allocated already */
4657 if (!kmsg->iov)
4658 kmsg->iov = kmsg->fast_iov;
4659 kmsg->msg.msg_iter.iov = kmsg->iov;
4660 } else {
4661 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004662 if (ret)
4663 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004664 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004665 }
4666
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004667 flags = req->sr_msg.msg_flags;
4668 if (flags & MSG_DONTWAIT)
4669 req->flags |= REQ_F_NOWAIT;
4670 else if (force_nonblock)
4671 flags |= MSG_DONTWAIT;
4672
4673 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4674 if (force_nonblock && ret == -EAGAIN)
4675 return io_setup_async_msg(req, kmsg);
4676 if (ret == -ERESTARTSYS)
4677 ret = -EINTR;
4678
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004679 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004680 kfree(kmsg->iov);
4681 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004682 if (ret < 0)
4683 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004684 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004685 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004686}
4687
Jens Axboe229a7b62020-06-22 10:13:11 -06004688static int io_send(struct io_kiocb *req, bool force_nonblock,
4689 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004690{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004691 struct io_sr_msg *sr = &req->sr_msg;
4692 struct msghdr msg;
4693 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004694 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004695 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004696 int ret;
4697
Florent Revestdba4a922020-12-04 12:36:04 +01004698 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004699 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004700 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004701
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004702 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4703 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004704 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004705
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004706 msg.msg_name = NULL;
4707 msg.msg_control = NULL;
4708 msg.msg_controllen = 0;
4709 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004710
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004711 flags = req->sr_msg.msg_flags;
4712 if (flags & MSG_DONTWAIT)
4713 req->flags |= REQ_F_NOWAIT;
4714 else if (force_nonblock)
4715 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004716
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004717 msg.msg_flags = flags;
4718 ret = sock_sendmsg(sock, &msg);
4719 if (force_nonblock && ret == -EAGAIN)
4720 return -EAGAIN;
4721 if (ret == -ERESTARTSYS)
4722 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004723
Jens Axboe03b12302019-12-02 18:50:25 -07004724 if (ret < 0)
4725 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004726 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004727 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004728}
4729
Pavel Begunkov1400e692020-07-12 20:41:05 +03004730static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4731 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004732{
4733 struct io_sr_msg *sr = &req->sr_msg;
4734 struct iovec __user *uiov;
4735 size_t iov_len;
4736 int ret;
4737
Pavel Begunkov1400e692020-07-12 20:41:05 +03004738 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4739 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004740 if (ret)
4741 return ret;
4742
4743 if (req->flags & REQ_F_BUFFER_SELECT) {
4744 if (iov_len > 1)
4745 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004746 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004747 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004748 sr->len = iomsg->iov[0].iov_len;
4749 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004750 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004751 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004752 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004753 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
4754 &iomsg->iov, &iomsg->msg.msg_iter,
4755 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004756 if (ret > 0)
4757 ret = 0;
4758 }
4759
4760 return ret;
4761}
4762
4763#ifdef CONFIG_COMPAT
4764static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004765 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004766{
4767 struct compat_msghdr __user *msg_compat;
4768 struct io_sr_msg *sr = &req->sr_msg;
4769 struct compat_iovec __user *uiov;
4770 compat_uptr_t ptr;
4771 compat_size_t len;
4772 int ret;
4773
Pavel Begunkov270a5942020-07-12 20:41:04 +03004774 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004775 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004776 &ptr, &len);
4777 if (ret)
4778 return ret;
4779
4780 uiov = compat_ptr(ptr);
4781 if (req->flags & REQ_F_BUFFER_SELECT) {
4782 compat_ssize_t clen;
4783
4784 if (len > 1)
4785 return -EINVAL;
4786 if (!access_ok(uiov, sizeof(*uiov)))
4787 return -EFAULT;
4788 if (__get_user(clen, &uiov->iov_len))
4789 return -EFAULT;
4790 if (clen < 0)
4791 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004792 sr->len = clen;
4793 iomsg->iov[0].iov_len = clen;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004794 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004795 } else {
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004796 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
4797 UIO_FASTIOV, &iomsg->iov,
4798 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004799 if (ret < 0)
4800 return ret;
4801 }
4802
4803 return 0;
4804}
Jens Axboe03b12302019-12-02 18:50:25 -07004805#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004806
Pavel Begunkov1400e692020-07-12 20:41:05 +03004807static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4808 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004809{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004810 iomsg->msg.msg_name = &iomsg->addr;
4811 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004812
4813#ifdef CONFIG_COMPAT
4814 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004815 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004816#endif
4817
Pavel Begunkov1400e692020-07-12 20:41:05 +03004818 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004819}
4820
Jens Axboebcda7ba2020-02-23 16:42:51 -07004821static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004822 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004823{
4824 struct io_sr_msg *sr = &req->sr_msg;
4825 struct io_buffer *kbuf;
4826
Jens Axboebcda7ba2020-02-23 16:42:51 -07004827 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4828 if (IS_ERR(kbuf))
4829 return kbuf;
4830
4831 sr->kbuf = kbuf;
4832 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004833 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004834}
4835
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004836static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4837{
4838 return io_put_kbuf(req, req->sr_msg.kbuf);
4839}
4840
Jens Axboe3529d8c2019-12-19 18:24:38 -07004841static int io_recvmsg_prep(struct io_kiocb *req,
4842 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004843{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004844 struct io_async_msghdr *async_msg = req->async_data;
Jens Axboee47293f2019-12-20 08:58:21 -07004845 struct io_sr_msg *sr = &req->sr_msg;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004846 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004847
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004848 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4849 return -EINVAL;
4850
Jens Axboe3529d8c2019-12-19 18:24:38 -07004851 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004852 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004853 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004854 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004855
Jens Axboed8768362020-02-27 14:17:49 -07004856#ifdef CONFIG_COMPAT
4857 if (req->ctx->compat)
4858 sr->msg_flags |= MSG_CMSG_COMPAT;
4859#endif
4860
Jens Axboee8c2bc12020-08-15 18:44:09 -07004861 if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
Jens Axboe06b76d42019-12-19 14:44:26 -07004862 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004863 ret = io_recvmsg_copy_hdr(req, async_msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004864 if (!ret)
4865 req->flags |= REQ_F_NEED_CLEANUP;
4866 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004867}
4868
Jens Axboe229a7b62020-06-22 10:13:11 -06004869static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4870 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004871{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004872 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004873 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004874 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004875 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004876 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004877
Florent Revestdba4a922020-12-04 12:36:04 +01004878 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004879 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004880 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004881
Jens Axboee8c2bc12020-08-15 18:44:09 -07004882 if (req->async_data) {
4883 kmsg = req->async_data;
4884 kmsg->msg.msg_name = &kmsg->addr;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004885 /* if iov is set, it's allocated already */
4886 if (!kmsg->iov)
4887 kmsg->iov = kmsg->fast_iov;
4888 kmsg->msg.msg_iter.iov = kmsg->iov;
4889 } else {
4890 ret = io_recvmsg_copy_hdr(req, &iomsg);
4891 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004892 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004893 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004894 }
4895
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004896 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004897 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004898 if (IS_ERR(kbuf))
4899 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004900 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4901 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4902 1, req->sr_msg.len);
4903 }
4904
4905 flags = req->sr_msg.msg_flags;
4906 if (flags & MSG_DONTWAIT)
4907 req->flags |= REQ_F_NOWAIT;
4908 else if (force_nonblock)
4909 flags |= MSG_DONTWAIT;
4910
4911 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4912 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004913 if (force_nonblock && ret == -EAGAIN)
4914 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004915 if (ret == -ERESTARTSYS)
4916 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004917
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004918 if (req->flags & REQ_F_BUFFER_SELECTED)
4919 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004920 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004921 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004922 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004923 if (ret < 0)
4924 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004925 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004926 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004927}
4928
Jens Axboe229a7b62020-06-22 10:13:11 -06004929static int io_recv(struct io_kiocb *req, bool force_nonblock,
4930 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004931{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004932 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004933 struct io_sr_msg *sr = &req->sr_msg;
4934 struct msghdr msg;
4935 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004936 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004937 struct iovec iov;
4938 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004939 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004940
Florent Revestdba4a922020-12-04 12:36:04 +01004941 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004942 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004943 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004944
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004945 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004946 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004947 if (IS_ERR(kbuf))
4948 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004949 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004950 }
4951
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004952 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004953 if (unlikely(ret))
4954 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004955
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004956 msg.msg_name = NULL;
4957 msg.msg_control = NULL;
4958 msg.msg_controllen = 0;
4959 msg.msg_namelen = 0;
4960 msg.msg_iocb = NULL;
4961 msg.msg_flags = 0;
4962
4963 flags = req->sr_msg.msg_flags;
4964 if (flags & MSG_DONTWAIT)
4965 req->flags |= REQ_F_NOWAIT;
4966 else if (force_nonblock)
4967 flags |= MSG_DONTWAIT;
4968
4969 ret = sock_recvmsg(sock, &msg, flags);
4970 if (force_nonblock && ret == -EAGAIN)
4971 return -EAGAIN;
4972 if (ret == -ERESTARTSYS)
4973 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004974out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004975 if (req->flags & REQ_F_BUFFER_SELECTED)
4976 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004977 if (ret < 0)
4978 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004979 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004980 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004981}
4982
Jens Axboe3529d8c2019-12-19 18:24:38 -07004983static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004984{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004985 struct io_accept *accept = &req->accept;
4986
Jens Axboe14587a462020-09-05 11:36:08 -06004987 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004988 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004989 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004990 return -EINVAL;
4991
Jens Axboed55e5f52019-12-11 16:12:15 -07004992 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4993 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004994 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004995 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004996 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004997}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004998
Jens Axboe229a7b62020-06-22 10:13:11 -06004999static int io_accept(struct io_kiocb *req, bool force_nonblock,
5000 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005001{
5002 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005003 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005004 int ret;
5005
Jiufei Xuee697dee2020-06-10 13:41:59 +08005006 if (req->file->f_flags & O_NONBLOCK)
5007 req->flags |= REQ_F_NOWAIT;
5008
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005009 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06005010 accept->addr_len, accept->flags,
5011 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005012 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005013 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005014 if (ret < 0) {
5015 if (ret == -ERESTARTSYS)
5016 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005017 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005018 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005019 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005020 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005021}
5022
Jens Axboe3529d8c2019-12-19 18:24:38 -07005023static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005024{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005025 struct io_connect *conn = &req->connect;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005026 struct io_async_connect *io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005027
Jens Axboe14587a462020-09-05 11:36:08 -06005028 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005029 return -EINVAL;
5030 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
5031 return -EINVAL;
5032
Jens Axboe3529d8c2019-12-19 18:24:38 -07005033 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5034 conn->addr_len = READ_ONCE(sqe->addr2);
5035
5036 if (!io)
5037 return 0;
5038
5039 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005040 &io->address);
Jens Axboef499a022019-12-02 16:28:46 -07005041}
5042
Jens Axboe229a7b62020-06-22 10:13:11 -06005043static int io_connect(struct io_kiocb *req, bool force_nonblock,
5044 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005045{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005046 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005047 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005048 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005049
Jens Axboee8c2bc12020-08-15 18:44:09 -07005050 if (req->async_data) {
5051 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005052 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005053 ret = move_addr_to_kernel(req->connect.addr,
5054 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005055 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005056 if (ret)
5057 goto out;
5058 io = &__io;
5059 }
5060
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005061 file_flags = force_nonblock ? O_NONBLOCK : 0;
5062
Jens Axboee8c2bc12020-08-15 18:44:09 -07005063 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005064 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005065 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005066 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005067 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005068 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005069 ret = -ENOMEM;
5070 goto out;
5071 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005072 io = req->async_data;
5073 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005074 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005075 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005076 if (ret == -ERESTARTSYS)
5077 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005078out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005079 if (ret < 0)
5080 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005081 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005082 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005083}
YueHaibing469956e2020-03-04 15:53:52 +08005084#else /* !CONFIG_NET */
5085static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5086{
Jens Axboef8e85cf2019-11-23 14:24:24 -07005087 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005088}
5089
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005090static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
5091 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005092{
YueHaibing469956e2020-03-04 15:53:52 +08005093 return -EOPNOTSUPP;
5094}
5095
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005096static int io_send(struct io_kiocb *req, bool force_nonblock,
5097 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005098{
5099 return -EOPNOTSUPP;
5100}
5101
5102static int io_recvmsg_prep(struct io_kiocb *req,
5103 const struct io_uring_sqe *sqe)
5104{
5105 return -EOPNOTSUPP;
5106}
5107
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005108static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
5109 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005110{
5111 return -EOPNOTSUPP;
5112}
5113
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005114static int io_recv(struct io_kiocb *req, bool force_nonblock,
5115 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005116{
5117 return -EOPNOTSUPP;
5118}
5119
5120static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5121{
5122 return -EOPNOTSUPP;
5123}
5124
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005125static int io_accept(struct io_kiocb *req, bool force_nonblock,
5126 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005127{
5128 return -EOPNOTSUPP;
5129}
5130
5131static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5132{
5133 return -EOPNOTSUPP;
5134}
5135
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07005136static int io_connect(struct io_kiocb *req, bool force_nonblock,
5137 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08005138{
5139 return -EOPNOTSUPP;
5140}
5141#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005142
Jens Axboed7718a92020-02-14 22:23:12 -07005143struct io_poll_table {
5144 struct poll_table_struct pt;
5145 struct io_kiocb *req;
5146 int error;
5147};
5148
Jens Axboed7718a92020-02-14 22:23:12 -07005149static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5150 __poll_t mask, task_work_func_t func)
5151{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005152 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005153
5154 /* for instances that support it check for an event match first: */
5155 if (mask && !(mask & poll->events))
5156 return 0;
5157
5158 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5159
5160 list_del_init(&poll->wait.entry);
5161
Jens Axboed7718a92020-02-14 22:23:12 -07005162 req->result = mask;
5163 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06005164 percpu_ref_get(&req->ctx->refs);
5165
Jens Axboed7718a92020-02-14 22:23:12 -07005166 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005167 * If this fails, then the task is exiting. When a task exits, the
5168 * work gets canceled, so just cancel this request as well instead
5169 * of executing it. We can't safely execute it anyway, as we may not
5170 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005171 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005172 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005173 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06005174 struct task_struct *tsk;
5175
Jens Axboee3aabf92020-05-18 11:04:17 -06005176 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005177 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboe91989c72020-10-16 09:02:26 -06005178 task_work_add(tsk, &req->task_work, TWA_NONE);
Jens Axboece593a62020-06-30 12:39:05 -06005179 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005180 }
Jens Axboed7718a92020-02-14 22:23:12 -07005181 return 1;
5182}
5183
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005184static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5185 __acquires(&req->ctx->completion_lock)
5186{
5187 struct io_ring_ctx *ctx = req->ctx;
5188
5189 if (!req->result && !READ_ONCE(poll->canceled)) {
5190 struct poll_table_struct pt = { ._key = poll->events };
5191
5192 req->result = vfs_poll(req->file, &pt) & poll->events;
5193 }
5194
5195 spin_lock_irq(&ctx->completion_lock);
5196 if (!req->result && !READ_ONCE(poll->canceled)) {
5197 add_wait_queue(poll->head, &poll->wait);
5198 return true;
5199 }
5200
5201 return false;
5202}
5203
Jens Axboed4e7cd32020-08-15 11:44:50 -07005204static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005205{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005206 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005207 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005208 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005209 return req->apoll->double_poll;
5210}
5211
5212static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5213{
5214 if (req->opcode == IORING_OP_POLL_ADD)
5215 return &req->poll;
5216 return &req->apoll->poll;
5217}
5218
5219static void io_poll_remove_double(struct io_kiocb *req)
5220{
5221 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005222
5223 lockdep_assert_held(&req->ctx->completion_lock);
5224
5225 if (poll && poll->head) {
5226 struct wait_queue_head *head = poll->head;
5227
5228 spin_lock(&head->lock);
5229 list_del_init(&poll->wait.entry);
5230 if (poll->wait.private)
5231 refcount_dec(&req->refs);
5232 poll->head = NULL;
5233 spin_unlock(&head->lock);
5234 }
5235}
5236
5237static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5238{
5239 struct io_ring_ctx *ctx = req->ctx;
5240
Jens Axboed4e7cd32020-08-15 11:44:50 -07005241 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005242 req->poll.done = true;
5243 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5244 io_commit_cqring(ctx);
5245}
5246
Jens Axboe18bceab2020-05-15 11:56:54 -06005247static void io_poll_task_func(struct callback_head *cb)
5248{
5249 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005250 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005251 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005252
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005253 if (io_poll_rewait(req, &req->poll)) {
5254 spin_unlock_irq(&ctx->completion_lock);
5255 } else {
5256 hash_del(&req->hash_node);
5257 io_poll_complete(req, req->result, 0);
5258 spin_unlock_irq(&ctx->completion_lock);
5259
5260 nxt = io_put_req_find_next(req);
5261 io_cqring_ev_posted(ctx);
5262 if (nxt)
5263 __io_req_task_submit(nxt);
5264 }
5265
Jens Axboe6d816e02020-08-11 08:04:14 -06005266 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005267}
5268
5269static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5270 int sync, void *key)
5271{
5272 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005273 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005274 __poll_t mask = key_to_poll(key);
5275
5276 /* for instances that support it check for an event match first: */
5277 if (mask && !(mask & poll->events))
5278 return 0;
5279
Jens Axboe8706e042020-09-28 08:38:54 -06005280 list_del_init(&wait->entry);
5281
Jens Axboe807abcb2020-07-17 17:09:27 -06005282 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005283 bool done;
5284
Jens Axboe807abcb2020-07-17 17:09:27 -06005285 spin_lock(&poll->head->lock);
5286 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005287 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005288 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005289 /* make sure double remove sees this as being gone */
5290 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005291 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005292 if (!done) {
5293 /* use wait func handler, so it matches the rq type */
5294 poll->wait.func(&poll->wait, mode, sync, key);
5295 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005296 }
5297 refcount_dec(&req->refs);
5298 return 1;
5299}
5300
5301static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5302 wait_queue_func_t wake_func)
5303{
5304 poll->head = NULL;
5305 poll->done = false;
5306 poll->canceled = false;
5307 poll->events = events;
5308 INIT_LIST_HEAD(&poll->wait.entry);
5309 init_waitqueue_func_entry(&poll->wait, wake_func);
5310}
5311
5312static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005313 struct wait_queue_head *head,
5314 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005315{
5316 struct io_kiocb *req = pt->req;
5317
5318 /*
5319 * If poll->head is already set, it's because the file being polled
5320 * uses multiple waitqueues for poll handling (eg one for read, one
5321 * for write). Setup a separate io_poll_iocb if this happens.
5322 */
5323 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005324 struct io_poll_iocb *poll_one = poll;
5325
Jens Axboe18bceab2020-05-15 11:56:54 -06005326 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005327 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005328 pt->error = -EINVAL;
5329 return;
5330 }
5331 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5332 if (!poll) {
5333 pt->error = -ENOMEM;
5334 return;
5335 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005336 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005337 refcount_inc(&req->refs);
5338 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005339 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005340 }
5341
5342 pt->error = 0;
5343 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005344
5345 if (poll->events & EPOLLEXCLUSIVE)
5346 add_wait_queue_exclusive(head, &poll->wait);
5347 else
5348 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005349}
5350
5351static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5352 struct poll_table_struct *p)
5353{
5354 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005355 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005356
Jens Axboe807abcb2020-07-17 17:09:27 -06005357 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005358}
5359
Jens Axboed7718a92020-02-14 22:23:12 -07005360static void io_async_task_func(struct callback_head *cb)
5361{
5362 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5363 struct async_poll *apoll = req->apoll;
5364 struct io_ring_ctx *ctx = req->ctx;
5365
5366 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5367
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005368 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005369 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005370 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005371 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005372 }
5373
Jens Axboe31067252020-05-17 17:43:31 -06005374 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005375 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005376 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005377
Jens Axboed4e7cd32020-08-15 11:44:50 -07005378 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005379 spin_unlock_irq(&ctx->completion_lock);
5380
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005381 if (!READ_ONCE(apoll->poll.canceled))
5382 __io_req_task_submit(req);
5383 else
5384 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005385
Jens Axboe6d816e02020-08-11 08:04:14 -06005386 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005387 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005388 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005389}
5390
5391static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5392 void *key)
5393{
5394 struct io_kiocb *req = wait->private;
5395 struct io_poll_iocb *poll = &req->apoll->poll;
5396
5397 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5398 key_to_poll(key));
5399
5400 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5401}
5402
5403static void io_poll_req_insert(struct io_kiocb *req)
5404{
5405 struct io_ring_ctx *ctx = req->ctx;
5406 struct hlist_head *list;
5407
5408 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5409 hlist_add_head(&req->hash_node, list);
5410}
5411
5412static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5413 struct io_poll_iocb *poll,
5414 struct io_poll_table *ipt, __poll_t mask,
5415 wait_queue_func_t wake_func)
5416 __acquires(&ctx->completion_lock)
5417{
5418 struct io_ring_ctx *ctx = req->ctx;
5419 bool cancel = false;
5420
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005421 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005422 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005423 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005424 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005425
5426 ipt->pt._key = mask;
5427 ipt->req = req;
5428 ipt->error = -EINVAL;
5429
Jens Axboed7718a92020-02-14 22:23:12 -07005430 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5431
5432 spin_lock_irq(&ctx->completion_lock);
5433 if (likely(poll->head)) {
5434 spin_lock(&poll->head->lock);
5435 if (unlikely(list_empty(&poll->wait.entry))) {
5436 if (ipt->error)
5437 cancel = true;
5438 ipt->error = 0;
5439 mask = 0;
5440 }
5441 if (mask || ipt->error)
5442 list_del_init(&poll->wait.entry);
5443 else if (cancel)
5444 WRITE_ONCE(poll->canceled, true);
5445 else if (!poll->done) /* actually waiting for an event */
5446 io_poll_req_insert(req);
5447 spin_unlock(&poll->head->lock);
5448 }
5449
5450 return mask;
5451}
5452
5453static bool io_arm_poll_handler(struct io_kiocb *req)
5454{
5455 const struct io_op_def *def = &io_op_defs[req->opcode];
5456 struct io_ring_ctx *ctx = req->ctx;
5457 struct async_poll *apoll;
5458 struct io_poll_table ipt;
5459 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005460 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005461
5462 if (!req->file || !file_can_poll(req->file))
5463 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005464 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005465 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005466 if (def->pollin)
5467 rw = READ;
5468 else if (def->pollout)
5469 rw = WRITE;
5470 else
5471 return false;
5472 /* if we can't nonblock try, then no point in arming a poll handler */
5473 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005474 return false;
5475
5476 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5477 if (unlikely(!apoll))
5478 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005479 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005480
5481 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005482 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005483
Nathan Chancellor8755d972020-03-02 16:01:19 -07005484 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005485 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005486 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005487 if (def->pollout)
5488 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005489
5490 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5491 if ((req->opcode == IORING_OP_RECVMSG) &&
5492 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5493 mask &= ~POLLIN;
5494
Jens Axboed7718a92020-02-14 22:23:12 -07005495 mask |= POLLERR | POLLPRI;
5496
5497 ipt.pt._qproc = io_async_queue_proc;
5498
5499 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5500 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005501 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005502 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005503 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005504 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005505 kfree(apoll);
5506 return false;
5507 }
5508 spin_unlock_irq(&ctx->completion_lock);
5509 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5510 apoll->poll.events);
5511 return true;
5512}
5513
5514static bool __io_poll_remove_one(struct io_kiocb *req,
5515 struct io_poll_iocb *poll)
5516{
Jens Axboeb41e9852020-02-17 09:52:41 -07005517 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005518
5519 spin_lock(&poll->head->lock);
5520 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005521 if (!list_empty(&poll->wait.entry)) {
5522 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005523 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005524 }
5525 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005526 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005527 return do_complete;
5528}
5529
5530static bool io_poll_remove_one(struct io_kiocb *req)
5531{
5532 bool do_complete;
5533
Jens Axboed4e7cd32020-08-15 11:44:50 -07005534 io_poll_remove_double(req);
5535
Jens Axboed7718a92020-02-14 22:23:12 -07005536 if (req->opcode == IORING_OP_POLL_ADD) {
5537 do_complete = __io_poll_remove_one(req, &req->poll);
5538 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005539 struct async_poll *apoll = req->apoll;
5540
Jens Axboed7718a92020-02-14 22:23:12 -07005541 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005542 do_complete = __io_poll_remove_one(req, &apoll->poll);
5543 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005544 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005545 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005546 kfree(apoll);
5547 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005548 }
5549
Jens Axboeb41e9852020-02-17 09:52:41 -07005550 if (do_complete) {
5551 io_cqring_fill_event(req, -ECANCELED);
5552 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005553 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005554 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005555 }
5556
5557 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005558}
5559
Jens Axboe76e1b642020-09-26 15:05:03 -06005560/*
5561 * Returns true if we found and killed one or more poll requests
5562 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005563static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5564 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005565{
Jens Axboe78076bb2019-12-04 19:56:40 -07005566 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005567 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005568 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005569
5570 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005571 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5572 struct hlist_head *list;
5573
5574 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005575 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005576 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005577 posted += io_poll_remove_one(req);
5578 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005579 }
5580 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005581
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005582 if (posted)
5583 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005584
5585 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005586}
5587
Jens Axboe47f46762019-11-09 17:43:02 -07005588static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5589{
Jens Axboe78076bb2019-12-04 19:56:40 -07005590 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005591 struct io_kiocb *req;
5592
Jens Axboe78076bb2019-12-04 19:56:40 -07005593 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5594 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005595 if (sqe_addr != req->user_data)
5596 continue;
5597 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005598 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005599 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005600 }
5601
5602 return -ENOENT;
5603}
5604
Jens Axboe3529d8c2019-12-19 18:24:38 -07005605static int io_poll_remove_prep(struct io_kiocb *req,
5606 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005607{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005608 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5609 return -EINVAL;
5610 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5611 sqe->poll_events)
5612 return -EINVAL;
5613
Pavel Begunkov018043b2020-10-27 23:17:18 +00005614 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005615 return 0;
5616}
5617
5618/*
5619 * Find a running poll command that matches one specified in sqe->addr,
5620 * and remove it if found.
5621 */
5622static int io_poll_remove(struct io_kiocb *req)
5623{
5624 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005625 int ret;
5626
Jens Axboe221c5eb2019-01-17 09:41:58 -07005627 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005628 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005629 spin_unlock_irq(&ctx->completion_lock);
5630
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005631 if (ret < 0)
5632 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005633 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005634 return 0;
5635}
5636
Jens Axboe221c5eb2019-01-17 09:41:58 -07005637static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5638 void *key)
5639{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005640 struct io_kiocb *req = wait->private;
5641 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005642
Jens Axboed7718a92020-02-14 22:23:12 -07005643 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005644}
5645
Jens Axboe221c5eb2019-01-17 09:41:58 -07005646static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5647 struct poll_table_struct *p)
5648{
5649 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5650
Jens Axboee8c2bc12020-08-15 18:44:09 -07005651 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005652}
5653
Jens Axboe3529d8c2019-12-19 18:24:38 -07005654static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005655{
5656 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005657 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005658
5659 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5660 return -EINVAL;
5661 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5662 return -EINVAL;
5663
Jiufei Xue5769a352020-06-17 17:53:55 +08005664 events = READ_ONCE(sqe->poll32_events);
5665#ifdef __BIG_ENDIAN
5666 events = swahw32(events);
5667#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005668 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5669 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005670 return 0;
5671}
5672
Pavel Begunkov014db002020-03-03 21:33:12 +03005673static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005674{
5675 struct io_poll_iocb *poll = &req->poll;
5676 struct io_ring_ctx *ctx = req->ctx;
5677 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005678 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005679
Jens Axboed7718a92020-02-14 22:23:12 -07005680 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005681
Jens Axboed7718a92020-02-14 22:23:12 -07005682 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5683 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005684
Jens Axboe8c838782019-03-12 15:48:16 -06005685 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005686 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005687 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005688 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005689 spin_unlock_irq(&ctx->completion_lock);
5690
Jens Axboe8c838782019-03-12 15:48:16 -06005691 if (mask) {
5692 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005693 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005694 }
Jens Axboe8c838782019-03-12 15:48:16 -06005695 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005696}
5697
Jens Axboe5262f562019-09-17 12:26:57 -06005698static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5699{
Jens Axboead8a48a2019-11-15 08:49:11 -07005700 struct io_timeout_data *data = container_of(timer,
5701 struct io_timeout_data, timer);
5702 struct io_kiocb *req = data->req;
5703 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005704 unsigned long flags;
5705
Jens Axboe5262f562019-09-17 12:26:57 -06005706 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005707 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005708 atomic_set(&req->ctx->cq_timeouts,
5709 atomic_read(&req->ctx->cq_timeouts) + 1);
5710
Jens Axboe78e19bb2019-11-06 15:21:34 -07005711 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005712 io_commit_cqring(ctx);
5713 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5714
5715 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005716 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005717 io_put_req(req);
5718 return HRTIMER_NORESTART;
5719}
5720
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005721static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5722 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005723{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005724 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005725 struct io_kiocb *req;
5726 int ret = -ENOENT;
5727
5728 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5729 if (user_data == req->user_data) {
5730 ret = 0;
5731 break;
5732 }
5733 }
5734
5735 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005736 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005737
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005738 io = req->async_data;
5739 ret = hrtimer_try_to_cancel(&io->timer);
5740 if (ret == -1)
5741 return ERR_PTR(-EALREADY);
5742 list_del_init(&req->timeout.list);
5743 return req;
5744}
5745
5746static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5747{
5748 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5749
5750 if (IS_ERR(req))
5751 return PTR_ERR(req);
5752
5753 req_set_fail_links(req);
5754 io_cqring_fill_event(req, -ECANCELED);
5755 io_put_req_deferred(req, 1);
5756 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005757}
5758
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005759static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5760 struct timespec64 *ts, enum hrtimer_mode mode)
5761{
5762 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5763 struct io_timeout_data *data;
5764
5765 if (IS_ERR(req))
5766 return PTR_ERR(req);
5767
5768 req->timeout.off = 0; /* noseq */
5769 data = req->async_data;
5770 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5771 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5772 data->timer.function = io_timeout_fn;
5773 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5774 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005775}
5776
Jens Axboe3529d8c2019-12-19 18:24:38 -07005777static int io_timeout_remove_prep(struct io_kiocb *req,
5778 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005779{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005780 struct io_timeout_rem *tr = &req->timeout_rem;
5781
Jens Axboeb29472e2019-12-17 18:50:29 -07005782 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5783 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005784 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5785 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005786 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005787 return -EINVAL;
5788
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005789 tr->addr = READ_ONCE(sqe->addr);
5790 tr->flags = READ_ONCE(sqe->timeout_flags);
5791 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5792 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5793 return -EINVAL;
5794 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5795 return -EFAULT;
5796 } else if (tr->flags) {
5797 /* timeout removal doesn't support flags */
5798 return -EINVAL;
5799 }
5800
Jens Axboeb29472e2019-12-17 18:50:29 -07005801 return 0;
5802}
5803
Jens Axboe11365042019-10-16 09:08:32 -06005804/*
5805 * Remove or update an existing timeout command
5806 */
Jens Axboefc4df992019-12-10 14:38:45 -07005807static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005808{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005809 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005810 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005811 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005812
Jens Axboe11365042019-10-16 09:08:32 -06005813 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005814 if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) {
5815 enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS)
5816 ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
5817
5818 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
5819 } else {
5820 ret = io_timeout_cancel(ctx, tr->addr);
5821 }
Jens Axboe11365042019-10-16 09:08:32 -06005822
Jens Axboe47f46762019-11-09 17:43:02 -07005823 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005824 io_commit_cqring(ctx);
5825 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005826 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005827 if (ret < 0)
5828 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005829 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005830 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005831}
5832
Jens Axboe3529d8c2019-12-19 18:24:38 -07005833static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005834 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005835{
Jens Axboead8a48a2019-11-15 08:49:11 -07005836 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005837 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005838 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005839
Jens Axboead8a48a2019-11-15 08:49:11 -07005840 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005841 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005842 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005843 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005844 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005845 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005846 flags = READ_ONCE(sqe->timeout_flags);
5847 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005848 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005849
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005850 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005851
Jens Axboee8c2bc12020-08-15 18:44:09 -07005852 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005853 return -ENOMEM;
5854
Jens Axboee8c2bc12020-08-15 18:44:09 -07005855 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005856 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005857
5858 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005859 return -EFAULT;
5860
Jens Axboe11365042019-10-16 09:08:32 -06005861 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005862 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005863 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005864 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005865
Jens Axboead8a48a2019-11-15 08:49:11 -07005866 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5867 return 0;
5868}
5869
Jens Axboefc4df992019-12-10 14:38:45 -07005870static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005871{
Jens Axboead8a48a2019-11-15 08:49:11 -07005872 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005873 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005874 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005875 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005876
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005877 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005878
Jens Axboe5262f562019-09-17 12:26:57 -06005879 /*
5880 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005881 * timeout event to be satisfied. If it isn't set, then this is
5882 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005883 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005884 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005885 entry = ctx->timeout_list.prev;
5886 goto add;
5887 }
Jens Axboe5262f562019-09-17 12:26:57 -06005888
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005889 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5890 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005891
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005892 /* Update the last seq here in case io_flush_timeouts() hasn't.
5893 * This is safe because ->completion_lock is held, and submissions
5894 * and completions are never mixed in the same ->completion_lock section.
5895 */
5896 ctx->cq_last_tm_flush = tail;
5897
Jens Axboe5262f562019-09-17 12:26:57 -06005898 /*
5899 * Insertion sort, ensuring the first entry in the list is always
5900 * the one we need first.
5901 */
Jens Axboe5262f562019-09-17 12:26:57 -06005902 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005903 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5904 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005905
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005906 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005907 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005908 /* nxt.seq is behind @tail, otherwise would've been completed */
5909 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005910 break;
5911 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005912add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005913 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005914 data->timer.function = io_timeout_fn;
5915 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005916 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005917 return 0;
5918}
5919
Jens Axboe62755e32019-10-28 21:49:21 -06005920static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005921{
Jens Axboe62755e32019-10-28 21:49:21 -06005922 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005923
Jens Axboe62755e32019-10-28 21:49:21 -06005924 return req->user_data == (unsigned long) data;
5925}
5926
Jens Axboee977d6d2019-11-05 12:39:45 -07005927static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005928{
Jens Axboe62755e32019-10-28 21:49:21 -06005929 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005930 int ret = 0;
5931
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005932 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005933 switch (cancel_ret) {
5934 case IO_WQ_CANCEL_OK:
5935 ret = 0;
5936 break;
5937 case IO_WQ_CANCEL_RUNNING:
5938 ret = -EALREADY;
5939 break;
5940 case IO_WQ_CANCEL_NOTFOUND:
5941 ret = -ENOENT;
5942 break;
5943 }
5944
Jens Axboee977d6d2019-11-05 12:39:45 -07005945 return ret;
5946}
5947
Jens Axboe47f46762019-11-09 17:43:02 -07005948static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5949 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005950 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005951{
5952 unsigned long flags;
5953 int ret;
5954
5955 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5956 if (ret != -ENOENT) {
5957 spin_lock_irqsave(&ctx->completion_lock, flags);
5958 goto done;
5959 }
5960
5961 spin_lock_irqsave(&ctx->completion_lock, flags);
5962 ret = io_timeout_cancel(ctx, sqe_addr);
5963 if (ret != -ENOENT)
5964 goto done;
5965 ret = io_poll_cancel(ctx, sqe_addr);
5966done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005967 if (!ret)
5968 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005969 io_cqring_fill_event(req, ret);
5970 io_commit_cqring(ctx);
5971 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5972 io_cqring_ev_posted(ctx);
5973
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005974 if (ret < 0)
5975 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005976 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005977}
5978
Jens Axboe3529d8c2019-12-19 18:24:38 -07005979static int io_async_cancel_prep(struct io_kiocb *req,
5980 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005981{
Jens Axboefbf23842019-12-17 18:45:56 -07005982 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005983 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005984 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5985 return -EINVAL;
5986 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005987 return -EINVAL;
5988
Jens Axboefbf23842019-12-17 18:45:56 -07005989 req->cancel.addr = READ_ONCE(sqe->addr);
5990 return 0;
5991}
5992
Pavel Begunkov014db002020-03-03 21:33:12 +03005993static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005994{
5995 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005996
Pavel Begunkov014db002020-03-03 21:33:12 +03005997 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005998 return 0;
5999}
6000
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006001static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006002 const struct io_uring_sqe *sqe)
6003{
Jens Axboe6ca56f82020-09-18 16:51:19 -06006004 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
6005 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006006 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6007 return -EINVAL;
6008 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006009 return -EINVAL;
6010
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006011 req->rsrc_update.offset = READ_ONCE(sqe->off);
6012 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6013 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006014 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006015 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006016 return 0;
6017}
6018
Jens Axboe229a7b62020-06-22 10:13:11 -06006019static int io_files_update(struct io_kiocb *req, bool force_nonblock,
6020 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006021{
6022 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006023 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006024 int ret;
6025
Jens Axboef86cd202020-01-29 13:46:44 -07006026 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006027 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006028
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006029 up.offset = req->rsrc_update.offset;
6030 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006031
6032 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006033 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006034 mutex_unlock(&ctx->uring_lock);
6035
6036 if (ret < 0)
6037 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06006038 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006039 return 0;
6040}
6041
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006042static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006043{
Jens Axboed625c6e2019-12-17 19:53:05 -07006044 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006045 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006046 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006047 case IORING_OP_READV:
6048 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006049 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006050 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006051 case IORING_OP_WRITEV:
6052 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006053 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006054 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006055 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006056 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006057 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006058 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006059 case IORING_OP_FSYNC:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006060 return io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006061 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006062 return io_prep_sfr(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006063 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006064 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006065 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006066 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006067 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006068 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006069 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006070 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006071 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006072 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006073 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006074 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006075 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006076 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006077 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006078 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006079 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006080 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006081 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006082 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006083 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006084 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006085 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006086 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006087 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006088 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006089 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006090 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006091 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006092 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006093 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006094 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006095 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006096 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006097 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006098 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006099 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006100 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006101 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006102 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006103 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006104 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006105 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006106 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006107 case IORING_OP_SHUTDOWN:
6108 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006109 case IORING_OP_RENAMEAT:
6110 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006111 case IORING_OP_UNLINKAT:
6112 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006113 }
6114
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006115 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6116 req->opcode);
6117 return-EINVAL;
6118}
6119
Jens Axboedef596e2019-01-09 08:59:42 -07006120static int io_req_defer_prep(struct io_kiocb *req,
6121 const struct io_uring_sqe *sqe)
Jens Axboedef596e2019-01-09 08:59:42 -07006122{
Jens Axboedef596e2019-01-09 08:59:42 -07006123 if (!sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006124 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006125 if (io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006126 return -EAGAIN;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006127 return io_req_prep(req, sqe);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006128}
6129
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006130static u32 io_get_sequence(struct io_kiocb *req)
6131{
6132 struct io_kiocb *pos;
6133 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006134 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006135
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006136 io_for_each_link(pos, req)
6137 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006138
6139 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6140 return total_submitted - nr_reqs;
6141}
6142
Jens Axboe3529d8c2019-12-19 18:24:38 -07006143static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006144{
6145 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006146 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006148 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006149
6150 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006151 if (likely(list_empty_careful(&ctx->defer_list) &&
6152 !(req->flags & REQ_F_IO_DRAIN)))
6153 return 0;
6154
6155 seq = io_get_sequence(req);
6156 /* Still a chance to pass the sequence check */
6157 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006158 return 0;
6159
Jens Axboee8c2bc12020-08-15 18:44:09 -07006160 if (!req->async_data) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03006161 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006162 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03006163 return ret;
6164 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006165 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006166 de = kmalloc(sizeof(*de), GFP_KERNEL);
6167 if (!de)
6168 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006169
6170 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006171 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006172 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006173 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006174 io_queue_async_work(req);
6175 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006176 }
6177
6178 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006179 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006180 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006181 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006182 spin_unlock_irq(&ctx->completion_lock);
6183 return -EIOCBQUEUED;
6184}
Jens Axboeedafcce2019-01-09 09:16:05 -07006185
Jens Axboef573d382020-09-22 10:19:24 -06006186static void io_req_drop_files(struct io_kiocb *req)
6187{
6188 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc98de082020-11-15 12:56:32 +00006189 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboef573d382020-09-22 10:19:24 -06006190 unsigned long flags;
6191
Jens Axboe02a13672021-01-23 15:49:31 -07006192 if (req->work.flags & IO_WQ_WORK_FILES) {
6193 put_files_struct(req->work.identity->files);
6194 put_nsproxy(req->work.identity->nsproxy);
6195 }
Pavel Begunkovdfea9fc2020-12-18 13:12:21 +00006196 spin_lock_irqsave(&ctx->inflight_lock, flags);
6197 list_del(&req->inflight_entry);
6198 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
6199 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboedfead8a2020-10-14 10:12:37 -06006200 req->work.flags &= ~IO_WQ_WORK_FILES;
Pavel Begunkovdfea9fc2020-12-18 13:12:21 +00006201 if (atomic_read(&tctx->in_idle))
6202 wake_up(&tctx->wait);
Jens Axboef573d382020-09-22 10:19:24 -06006203}
6204
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006205static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006206{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006207 if (req->flags & REQ_F_BUFFER_SELECTED) {
6208 switch (req->opcode) {
6209 case IORING_OP_READV:
6210 case IORING_OP_READ_FIXED:
6211 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006212 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006213 break;
6214 case IORING_OP_RECVMSG:
6215 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006216 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006217 break;
6218 }
6219 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006220 }
6221
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006222 if (req->flags & REQ_F_NEED_CLEANUP) {
6223 switch (req->opcode) {
6224 case IORING_OP_READV:
6225 case IORING_OP_READ_FIXED:
6226 case IORING_OP_READ:
6227 case IORING_OP_WRITEV:
6228 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006229 case IORING_OP_WRITE: {
6230 struct io_async_rw *io = req->async_data;
6231 if (io->free_iovec)
6232 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006233 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006234 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006235 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006236 case IORING_OP_SENDMSG: {
6237 struct io_async_msghdr *io = req->async_data;
6238 if (io->iov != io->fast_iov)
6239 kfree(io->iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006240 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006241 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006242 case IORING_OP_SPLICE:
6243 case IORING_OP_TEE:
6244 io_put_file(req, req->splice.file_in,
6245 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6246 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006247 case IORING_OP_OPENAT:
6248 case IORING_OP_OPENAT2:
6249 if (req->open.filename)
6250 putname(req->open.filename);
6251 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006252 case IORING_OP_RENAMEAT:
6253 putname(req->rename.oldpath);
6254 putname(req->rename.newpath);
6255 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006256 case IORING_OP_UNLINKAT:
6257 putname(req->unlink.filename);
6258 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006259 }
6260 req->flags &= ~REQ_F_NEED_CLEANUP;
6261 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006262}
6263
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006264static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
6265 struct io_comp_state *cs)
Jens Axboeedafcce2019-01-09 09:16:05 -07006266{
Jens Axboeedafcce2019-01-09 09:16:05 -07006267 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006268 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006269
Jens Axboed625c6e2019-12-17 19:53:05 -07006270 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006271 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06006272 ret = io_nop(req, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07006273 break;
6274 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006275 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006276 case IORING_OP_READ:
Jens Axboea1d7c392020-06-22 11:09:46 -06006277 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006278 break;
6279 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006280 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006281 case IORING_OP_WRITE:
Jens Axboea1d7c392020-06-22 11:09:46 -06006282 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006283 break;
6284 case IORING_OP_FSYNC:
Pavel Begunkov014db002020-03-03 21:33:12 +03006285 ret = io_fsync(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006286 break;
6287 case IORING_OP_POLL_ADD:
Pavel Begunkov014db002020-03-03 21:33:12 +03006288 ret = io_poll_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006289 break;
6290 case IORING_OP_POLL_REMOVE:
Jens Axboeb76da702019-11-20 13:05:32 -07006291 ret = io_poll_remove(req);
6292 break;
6293 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006294 ret = io_sync_file_range(req, force_nonblock);
Jens Axboeb76da702019-11-20 13:05:32 -07006295 break;
6296 case IORING_OP_SENDMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006297 ret = io_sendmsg(req, force_nonblock, cs);
6298 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006299 case IORING_OP_SEND:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006300 ret = io_send(req, force_nonblock, cs);
Jens Axboeb76da702019-11-20 13:05:32 -07006301 break;
6302 case IORING_OP_RECVMSG:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006303 ret = io_recvmsg(req, force_nonblock, cs);
6304 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006305 case IORING_OP_RECV:
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006306 ret = io_recv(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006307 break;
6308 case IORING_OP_TIMEOUT:
6309 ret = io_timeout(req);
6310 break;
6311 case IORING_OP_TIMEOUT_REMOVE:
6312 ret = io_timeout_remove(req);
6313 break;
6314 case IORING_OP_ACCEPT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006315 ret = io_accept(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316 break;
6317 case IORING_OP_CONNECT:
Jens Axboe229a7b62020-06-22 10:13:11 -06006318 ret = io_connect(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006319 break;
6320 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov014db002020-03-03 21:33:12 +03006321 ret = io_async_cancel(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006322 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006323 case IORING_OP_FALLOCATE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006324 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07006325 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006326 case IORING_OP_OPENAT:
Pavel Begunkov014db002020-03-03 21:33:12 +03006327 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006328 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006329 case IORING_OP_CLOSE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006330 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07006331 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006332 case IORING_OP_FILES_UPDATE:
Jens Axboe229a7b62020-06-22 10:13:11 -06006333 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006334 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006335 case IORING_OP_STATX:
Pavel Begunkov014db002020-03-03 21:33:12 +03006336 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006337 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006338 case IORING_OP_FADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006339 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07006340 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006341 case IORING_OP_MADVISE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006342 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07006343 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006344 case IORING_OP_OPENAT2:
Pavel Begunkov014db002020-03-03 21:33:12 +03006345 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07006346 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006347 case IORING_OP_EPOLL_CTL:
Jens Axboe229a7b62020-06-22 10:13:11 -06006348 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006349 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006350 case IORING_OP_SPLICE:
Pavel Begunkov014db002020-03-03 21:33:12 +03006351 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006352 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006353 case IORING_OP_PROVIDE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006354 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006355 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006356 case IORING_OP_REMOVE_BUFFERS:
Jens Axboe229a7b62020-06-22 10:13:11 -06006357 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006358 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006359 case IORING_OP_TEE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006360 ret = io_tee(req, force_nonblock);
6361 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006362 case IORING_OP_SHUTDOWN:
6363 ret = io_shutdown(req, force_nonblock);
6364 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006365 case IORING_OP_RENAMEAT:
6366 ret = io_renameat(req, force_nonblock);
6367 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006368 case IORING_OP_UNLINKAT:
6369 ret = io_unlinkat(req, force_nonblock);
6370 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006371 default:
6372 ret = -EINVAL;
6373 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006374 }
6375
6376 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006377 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006378
Jens Axboeb5325762020-05-19 21:20:27 -06006379 /* If the op doesn't have a file, we're not polling for it */
6380 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006381 const bool in_async = io_wq_current_is_worker();
6382
Jens Axboe11ba8202020-01-15 21:51:17 -07006383 /* workqueue context doesn't hold uring_lock, grab it now */
6384 if (in_async)
6385 mutex_lock(&ctx->uring_lock);
6386
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006387 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006388
6389 if (in_async)
6390 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006391 }
6392
6393 return 0;
6394}
6395
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006396static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006397{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006398 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006399 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006400 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006401
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006402 timeout = io_prep_linked_timeout(req);
6403 if (timeout)
6404 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006405
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006406 /* if NO_CANCEL is set, we must still run the work */
6407 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6408 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006409 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006410 }
Jens Axboe31b51512019-01-18 22:56:34 -07006411
Jens Axboe561fb042019-10-24 07:25:42 -06006412 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006413 do {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006414 ret = io_issue_sqe(req, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006415 /*
6416 * We can get EAGAIN for polled IO even though we're
6417 * forcing a sync submission from here, since we can't
6418 * wait for request slots on the block side.
6419 */
6420 if (ret != -EAGAIN)
6421 break;
6422 cond_resched();
6423 } while (1);
6424 }
Jens Axboe31b51512019-01-18 22:56:34 -07006425
Jens Axboe561fb042019-10-24 07:25:42 -06006426 if (ret) {
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006427 struct io_ring_ctx *lock_ctx = NULL;
Xiaoguang Wangdad1b122020-12-06 22:22:42 +00006428
Xiaoguang Wangc07e6712020-12-14 23:49:41 +08006429 if (req->ctx->flags & IORING_SETUP_IOPOLL)
6430 lock_ctx = req->ctx;
6431
6432 /*
6433 * io_iopoll_complete() does not hold completion_lock to
6434 * complete polled io, so here for polled io, we can not call
6435 * io_req_complete() directly, otherwise there maybe concurrent
6436 * access to cqring, defer_list, etc, which is not safe. Given
6437 * that io_iopoll_complete() is always called under uring_lock,
6438 * so here for polled io, we also get uring_lock to complete
6439 * it.
6440 */
6441 if (lock_ctx)
6442 mutex_lock(&lock_ctx->uring_lock);
6443
6444 req_set_fail_links(req);
6445 io_req_complete(req, ret);
6446
6447 if (lock_ctx)
6448 mutex_unlock(&lock_ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07006449 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006450
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006451 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006452}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006453
Jens Axboe65e19f52019-10-26 07:20:21 -06006454static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6455 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006456{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006457 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006458
Jens Axboe05f3fb32019-12-09 11:22:50 -07006459 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006460 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006461}
6462
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006463static struct file *io_file_get(struct io_submit_state *state,
6464 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006465{
6466 struct io_ring_ctx *ctx = req->ctx;
6467 struct file *file;
6468
6469 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006470 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006471 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006472 fd = array_index_nospec(fd, ctx->nr_user_files);
6473 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006474 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006475 } else {
6476 trace_io_uring_file_get(ctx, fd);
6477 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006478 }
6479
Pavel Begunkovf609cbb2021-01-28 18:39:24 +00006480 if (file && file->f_op == &io_uring_fops &&
6481 !(req->flags & REQ_F_INFLIGHT)) {
Jens Axboe02a13672021-01-23 15:49:31 -07006482 io_req_init_async(req);
6483 req->flags |= REQ_F_INFLIGHT;
6484
6485 spin_lock_irq(&ctx->inflight_lock);
6486 list_add(&req->inflight_entry, &ctx->inflight_list);
6487 spin_unlock_irq(&ctx->inflight_lock);
6488 }
6489
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006490 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006491}
6492
Jens Axboe2665abf2019-11-05 12:40:47 -07006493static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6494{
Jens Axboead8a48a2019-11-15 08:49:11 -07006495 struct io_timeout_data *data = container_of(timer,
6496 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006497 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006498 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006499 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006500
6501 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006502 prev = req->timeout.head;
6503 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006504
6505 /*
6506 * We don't expect the list to be empty, that will only happen if we
6507 * race with the completion of the linked work.
6508 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006509 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006510 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006511 else
6512 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006513 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6514
6515 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006516 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006517 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006518 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006519 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006520 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006521 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006522 return HRTIMER_NORESTART;
6523}
6524
Jens Axboe7271ef32020-08-10 09:55:22 -06006525static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006526{
Jens Axboe76a46e02019-11-10 23:34:16 -07006527 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006528 * If the back reference is NULL, then our linked request finished
6529 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006530 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006531 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006532 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006533
Jens Axboead8a48a2019-11-15 08:49:11 -07006534 data->timer.function = io_link_timeout_fn;
6535 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6536 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006537 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006538}
6539
6540static void io_queue_linked_timeout(struct io_kiocb *req)
6541{
6542 struct io_ring_ctx *ctx = req->ctx;
6543
6544 spin_lock_irq(&ctx->completion_lock);
6545 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006546 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006547
Jens Axboe2665abf2019-11-05 12:40:47 -07006548 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006549 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006550}
6551
Jens Axboead8a48a2019-11-15 08:49:11 -07006552static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006553{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006554 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006555
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006556 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6557 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006558 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006559
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006560 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006561 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006562 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006563 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006564}
6565
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006566static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006567{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006568 struct io_kiocb *linked_timeout;
Jens Axboe193155c2020-02-22 23:22:19 -07006569 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006570 int ret;
6571
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006572again:
6573 linked_timeout = io_prep_linked_timeout(req);
6574
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006575 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6576 (req->work.flags & IO_WQ_WORK_CREDS) &&
Jens Axboe98447d62020-10-14 10:48:51 -06006577 req->work.identity->creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006578 if (old_creds)
6579 revert_creds(old_creds);
Jens Axboe98447d62020-10-14 10:48:51 -06006580 if (old_creds == req->work.identity->creds)
Jens Axboe193155c2020-02-22 23:22:19 -07006581 old_creds = NULL; /* restored original creds */
6582 else
Jens Axboe98447d62020-10-14 10:48:51 -06006583 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006584 }
6585
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006586 ret = io_issue_sqe(req, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006587
6588 /*
6589 * We async punt it if the file wasn't marked NOWAIT, or if the file
6590 * doesn't support non-blocking read/write attempts
6591 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006592 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006593 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006594 /*
6595 * Queued up for async execution, worker will release
6596 * submit reference when the iocb is actually submitted.
6597 */
6598 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006599 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006600
Pavel Begunkovf063c542020-07-25 14:41:59 +03006601 if (linked_timeout)
6602 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006603 } else if (likely(!ret)) {
6604 /* drop submission reference */
6605 req = io_put_req_find_next(req);
6606 if (linked_timeout)
6607 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006608
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006609 if (req) {
6610 if (!(req->flags & REQ_F_FORCE_ASYNC))
6611 goto again;
6612 io_queue_async_work(req);
6613 }
6614 } else {
Pavel Begunkov652532a2020-07-03 22:15:07 +03006615 /* un-prep timeout, so it'll be killed as any other linked */
6616 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006617 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006618 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006619 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006620 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006621
Jens Axboe193155c2020-02-22 23:22:19 -07006622 if (old_creds)
6623 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006624}
6625
Jens Axboef13fad72020-06-22 09:34:30 -06006626static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6627 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006628{
6629 int ret;
6630
Jens Axboe3529d8c2019-12-19 18:24:38 -07006631 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006632 if (ret) {
6633 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006634fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006635 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006636 io_put_req(req);
6637 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006638 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006639 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006640 if (!req->async_data) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006641 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006642 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006643 goto fail_req;
6644 }
Jens Axboece35a472019-12-17 08:04:44 -07006645 io_queue_async_work(req);
6646 } else {
Pavel Begunkovc1379e22020-09-30 22:57:56 +03006647 if (sqe) {
6648 ret = io_req_prep(req, sqe);
6649 if (unlikely(ret))
6650 goto fail_req;
6651 }
6652 __io_queue_sqe(req, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006653 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006654}
6655
Jens Axboef13fad72020-06-22 09:34:30 -06006656static inline void io_queue_link_head(struct io_kiocb *req,
6657 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006658{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006659 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006660 io_put_req(req);
6661 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006662 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006663 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006664}
6665
Pavel Begunkov863e0562020-10-27 23:25:35 +00006666struct io_submit_link {
6667 struct io_kiocb *head;
6668 struct io_kiocb *last;
6669};
6670
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006671static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov863e0562020-10-27 23:25:35 +00006672 struct io_submit_link *link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006673{
Jackie Liua197f662019-11-08 08:09:12 -07006674 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006675 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006676
Jens Axboe9e645e112019-05-10 16:07:28 -06006677 /*
6678 * If we already have a head request, queue this one for async
6679 * submittal once the head completes. If we don't have a head but
6680 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6681 * submitted sync once the chain is complete. If none of those
6682 * conditions are true (normal request), then just queue it.
6683 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006684 if (link->head) {
6685 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006686
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006687 /*
6688 * Taking sequential execution of a link, draining both sides
6689 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6690 * requests in the link. So, it drains the head and the
6691 * next after the link request. The last one is done via
6692 * drain_next flag to persist the effect across calls.
6693 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006694 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006695 head->flags |= REQ_F_IO_DRAIN;
6696 ctx->drain_next = 1;
6697 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006698 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006699 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006700 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006701 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006702 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006703 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006704 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006705 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006706 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006707
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006708 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006709 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006710 io_queue_link_head(head, cs);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006711 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006712 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006713 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006714 if (unlikely(ctx->drain_next)) {
6715 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006716 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006717 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006718 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006719 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006720 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006721 req->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006722 link->head = req;
6723 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006724 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006725 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006726 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006727 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006728
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006729 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006730}
6731
Jens Axboe9a56a232019-01-09 09:06:50 -07006732/*
6733 * Batched submission is done, ensure local IO is flushed out.
6734 */
6735static void io_submit_state_end(struct io_submit_state *state)
6736{
Jens Axboef13fad72020-06-22 09:34:30 -06006737 if (!list_empty(&state->comp.list))
6738 io_submit_flush_completions(&state->comp);
Jens Axboe27926b62020-10-28 09:33:23 -06006739 if (state->plug_started)
6740 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006741 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006742 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006743 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006744}
6745
6746/*
6747 * Start submission side cache.
6748 */
6749static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006750 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006751{
Jens Axboe27926b62020-10-28 09:33:23 -06006752 state->plug_started = false;
Jens Axboe013538b2020-06-22 09:29:15 -06006753 state->comp.nr = 0;
6754 INIT_LIST_HEAD(&state->comp.list);
6755 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006756 state->free_reqs = 0;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00006757 state->file_refs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006758 state->ios_left = max_ios;
6759}
6760
Jens Axboe2b188cc2019-01-07 10:46:33 -07006761static void io_commit_sqring(struct io_ring_ctx *ctx)
6762{
Hristo Venev75b28af2019-08-26 17:23:46 +00006763 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006764
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006765 /*
6766 * Ensure any loads from the SQEs are done at this point,
6767 * since once we write the new head, the application could
6768 * write new data to them.
6769 */
6770 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006771}
6772
6773/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006774 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006775 * that is mapped by userspace. This means that care needs to be taken to
6776 * ensure that reads are stable, as we cannot rely on userspace always
6777 * being a good citizen. If members of the sqe are validated and then later
6778 * used, it's important that those reads are done through READ_ONCE() to
6779 * prevent a re-load down the line.
6780 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006781static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006782{
Hristo Venev75b28af2019-08-26 17:23:46 +00006783 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006784 unsigned head;
6785
6786 /*
6787 * The cached sq head (or cq tail) serves two purposes:
6788 *
6789 * 1) allows us to batch the cost of updating the user visible
6790 * head updates.
6791 * 2) allows the kernel side to track the head on its own, even
6792 * though the application is the one updating it.
6793 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006794 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006795 if (likely(head < ctx->sq_entries))
6796 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797
6798 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006799 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006800 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006801 return NULL;
6802}
6803
6804static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6805{
6806 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006807}
6808
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006809/*
6810 * Check SQE restrictions (opcode and flags).
6811 *
6812 * Returns 'true' if SQE is allowed, 'false' otherwise.
6813 */
6814static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6815 struct io_kiocb *req,
6816 unsigned int sqe_flags)
6817{
6818 if (!ctx->restricted)
6819 return true;
6820
6821 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6822 return false;
6823
6824 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6825 ctx->restrictions.sqe_flags_required)
6826 return false;
6827
6828 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6829 ctx->restrictions.sqe_flags_required))
6830 return false;
6831
6832 return true;
6833}
6834
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006835#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6836 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6837 IOSQE_BUFFER_SELECT)
6838
6839static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6840 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006841 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006842{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006843 unsigned int sqe_flags;
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006844 int id, ret;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006845
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006846 req->opcode = READ_ONCE(sqe->opcode);
6847 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006848 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006849 req->file = NULL;
6850 req->ctx = ctx;
6851 req->flags = 0;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006852 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006853 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006854 /* one is dropped after submission, the other at completion */
6855 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006856 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006857 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006858
6859 if (unlikely(req->opcode >= IORING_OP_LAST))
6860 return -EINVAL;
6861
Jens Axboe28cea78a2020-09-14 10:51:17 -06006862 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006863 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006864
6865 sqe_flags = READ_ONCE(sqe->flags);
6866 /* enforce forwards compatibility on users */
6867 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6868 return -EINVAL;
6869
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006870 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6871 return -EACCES;
6872
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006873 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6874 !io_op_defs[req->opcode].buffer_select)
6875 return -EOPNOTSUPP;
6876
6877 id = READ_ONCE(sqe->personality);
6878 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006879 struct io_identity *iod;
6880
Jens Axboe1e6fa522020-10-15 08:46:24 -06006881 iod = idr_find(&ctx->personality_idr, id);
6882 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006883 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006884 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006885
6886 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006887 get_cred(iod->creds);
6888 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006889 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006890 }
6891
6892 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006893 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006894
Jens Axboe27926b62020-10-28 09:33:23 -06006895 /*
6896 * Plug now if we have more than 1 IO left after this, and the target
6897 * is potentially a read/write to block based storage.
6898 */
6899 if (!state->plug_started && state->ios_left > 1 &&
6900 io_op_defs[req->opcode].plug) {
6901 blk_start_plug(&state->plug);
6902 state->plug_started = true;
6903 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006904
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006905 ret = 0;
6906 if (io_op_defs[req->opcode].needs_file) {
6907 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006908
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006909 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
6910 if (unlikely(!req->file &&
6911 !io_op_defs[req->opcode].needs_file_no_error))
6912 ret = -EBADF;
6913 }
6914
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006915 state->ios_left--;
6916 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006917}
6918
Jens Axboe0f212202020-09-13 13:09:39 -06006919static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006920{
Jens Axboeac8691c2020-06-01 08:30:41 -06006921 struct io_submit_state state;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006922 struct io_submit_link link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006923 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006924
Jens Axboec4a2ed72019-11-21 21:01:26 -07006925 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006926 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006927 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006928 return -EBUSY;
6929 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006930
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006931 /* make sure SQ entry isn't read before tail */
6932 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006933
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006934 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6935 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006936
Jens Axboed8a6df12020-10-15 16:24:45 -06006937 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006938 refcount_add(nr, &current->usage);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006939
Jens Axboe6c271ce2019-01-10 11:22:30 -07006940 io_submit_state_start(&state, ctx, nr);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006941 link.head = NULL;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006942
Jens Axboe6c271ce2019-01-10 11:22:30 -07006943 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006944 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006945 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006946 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006947
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006948 sqe = io_get_sqe(ctx);
6949 if (unlikely(!sqe)) {
6950 io_consume_sqe(ctx);
6951 break;
6952 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006953 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006954 if (unlikely(!req)) {
6955 if (!submitted)
6956 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006957 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006958 }
Pavel Begunkov709b3022020-04-08 08:58:43 +03006959 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006960 /* will complete beyond this point, count as submitted */
6961 submitted++;
6962
Pavel Begunkov692d8362020-10-10 18:34:13 +01006963 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006964 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006965fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006966 io_put_req(req);
6967 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006968 break;
6969 }
6970
Jens Axboe354420f2020-01-08 18:55:15 -07006971 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006972 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006973 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006974 if (err)
6975 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006976 }
6977
Pavel Begunkov9466f432020-01-25 22:34:01 +03006978 if (unlikely(submitted != nr)) {
6979 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006980 struct io_uring_task *tctx = current->io_uring;
6981 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006982
Jens Axboed8a6df12020-10-15 16:24:45 -06006983 percpu_ref_put_many(&ctx->refs, unused);
6984 percpu_counter_sub(&tctx->inflight, unused);
6985 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006986 }
Pavel Begunkov863e0562020-10-27 23:25:35 +00006987 if (link.head)
6988 io_queue_link_head(link.head, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006989 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006990
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006991 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6992 io_commit_sqring(ctx);
6993
Jens Axboe6c271ce2019-01-10 11:22:30 -07006994 return submitted;
6995}
6996
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006997static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6998{
6999 /* Tell userspace we may need a wakeup call */
7000 spin_lock_irq(&ctx->completion_lock);
7001 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
7002 spin_unlock_irq(&ctx->completion_lock);
7003}
7004
7005static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7006{
7007 spin_lock_irq(&ctx->completion_lock);
7008 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
7009 spin_unlock_irq(&ctx->completion_lock);
7010}
7011
Xiaoguang Wang08369242020-11-03 14:15:59 +08007012static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007013{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007014 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007015 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007016
Jens Axboec8d1ba52020-09-14 11:07:26 -06007017 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007018 /* if we're handling multiple rings, cap submit size for fairness */
7019 if (cap_entries && to_submit > 8)
7020 to_submit = 8;
7021
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007022 if (!list_empty(&ctx->iopoll_list) || to_submit) {
7023 unsigned nr_events = 0;
7024
Xiaoguang Wang08369242020-11-03 14:15:59 +08007025 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007026 if (!list_empty(&ctx->iopoll_list))
7027 io_do_iopoll(ctx, &nr_events, 0);
7028
Pavel Begunkovd9d05212021-01-08 20:57:25 +00007029 if (to_submit && !ctx->sqo_dead &&
7030 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007031 ret = io_submit_sqes(ctx, to_submit);
7032 mutex_unlock(&ctx->uring_lock);
7033 }
Jens Axboe90554202020-09-03 12:12:41 -06007034
7035 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
7036 wake_up(&ctx->sqo_sq_wait);
7037
Xiaoguang Wang08369242020-11-03 14:15:59 +08007038 return ret;
7039}
7040
7041static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7042{
7043 struct io_ring_ctx *ctx;
7044 unsigned sq_thread_idle = 0;
7045
7046 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7047 if (sq_thread_idle < ctx->sq_thread_idle)
7048 sq_thread_idle = ctx->sq_thread_idle;
7049 }
7050
7051 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007052}
7053
Jens Axboe69fb2132020-09-14 11:16:23 -06007054static void io_sqd_init_new(struct io_sq_data *sqd)
7055{
7056 struct io_ring_ctx *ctx;
7057
7058 while (!list_empty(&sqd->ctx_new_list)) {
7059 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007060 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
7061 complete(&ctx->sq_thread_comp);
7062 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007063
7064 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007065}
7066
Jens Axboe6c271ce2019-01-10 11:22:30 -07007067static int io_sq_thread(void *data)
7068{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007069 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007070 struct files_struct *old_files = current->files;
7071 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007072 const struct cred *old_cred = NULL;
7073 struct io_sq_data *sqd = data;
7074 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007075 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007076 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007077
Jens Axboe28cea78a2020-09-14 10:51:17 -06007078 task_lock(current);
7079 current->files = NULL;
7080 current->nsproxy = NULL;
7081 task_unlock(current);
7082
Jens Axboe69fb2132020-09-14 11:16:23 -06007083 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007084 int ret;
7085 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007086
7087 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007088 * Any changes to the sqd lists are synchronized through the
7089 * kthread parking. This synchronizes the thread vs users,
7090 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007091 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007092 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007093 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007094 /*
7095 * When sq thread is unparked, in case the previous park operation
7096 * comes from io_put_sq_data(), which means that sq thread is going
7097 * to be stopped, so here needs to have a check.
7098 */
7099 if (kthread_should_stop())
7100 break;
7101 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007102
Xiaoguang Wang08369242020-11-03 14:15:59 +08007103 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007104 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007105 timeout = jiffies + sqd->sq_thread_idle;
7106 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007107
Xiaoguang Wang08369242020-11-03 14:15:59 +08007108 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007109 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007110 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7111 if (current->cred != ctx->creds) {
7112 if (old_cred)
7113 revert_creds(old_cred);
7114 old_cred = override_creds(ctx->creds);
7115 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007116 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007117#ifdef CONFIG_AUDIT
7118 current->loginuid = ctx->loginuid;
7119 current->sessionid = ctx->sessionid;
7120#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007121
Xiaoguang Wang08369242020-11-03 14:15:59 +08007122 ret = __io_sq_thread(ctx, cap_entries);
7123 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7124 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007125
Jens Axboe28cea78a2020-09-14 10:51:17 -06007126 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007127 }
7128
Xiaoguang Wang08369242020-11-03 14:15:59 +08007129 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007130 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007131 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007132 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007133 if (sqt_spin)
7134 timeout = jiffies + sqd->sq_thread_idle;
7135 continue;
7136 }
7137
7138 if (kthread_should_park())
7139 continue;
7140
7141 needs_sched = true;
7142 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7143 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7144 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7145 !list_empty_careful(&ctx->iopoll_list)) {
7146 needs_sched = false;
7147 break;
7148 }
7149 if (io_sqring_entries(ctx)) {
7150 needs_sched = false;
7151 break;
7152 }
7153 }
7154
7155 if (needs_sched) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007156 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7157 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007158
Jens Axboe69fb2132020-09-14 11:16:23 -06007159 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007160 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7161 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007162 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007163
7164 finish_wait(&sqd->wait, &wait);
7165 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007166 }
7167
Jens Axboe4c6e2772020-07-01 11:29:10 -06007168 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007169 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007170
Dennis Zhou91d8f512020-09-16 13:41:05 -07007171 if (cur_css)
7172 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007173 if (old_cred)
7174 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007175
Jens Axboe28cea78a2020-09-14 10:51:17 -06007176 task_lock(current);
7177 current->files = old_files;
7178 current->nsproxy = old_nsproxy;
7179 task_unlock(current);
7180
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007181 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007182
Jens Axboe6c271ce2019-01-10 11:22:30 -07007183 return 0;
7184}
7185
Jens Axboebda52162019-09-24 13:47:15 -06007186struct io_wait_queue {
7187 struct wait_queue_entry wq;
7188 struct io_ring_ctx *ctx;
7189 unsigned to_wait;
7190 unsigned nr_timeouts;
7191};
7192
Pavel Begunkov6c503152021-01-04 20:36:36 +00007193static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007194{
7195 struct io_ring_ctx *ctx = iowq->ctx;
7196
7197 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007198 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007199 * started waiting. For timeouts, we always want to return to userspace,
7200 * regardless of event count.
7201 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007202 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007203 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7204}
7205
7206static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7207 int wake_flags, void *key)
7208{
7209 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7210 wq);
7211
Pavel Begunkov6c503152021-01-04 20:36:36 +00007212 /*
7213 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7214 * the task, and the next invocation will do it.
7215 */
7216 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7217 return autoremove_wake_function(curr, mode, wake_flags, key);
7218 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007219}
7220
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007221static int io_run_task_work_sig(void)
7222{
7223 if (io_run_task_work())
7224 return 1;
7225 if (!signal_pending(current))
7226 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007227 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7228 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007229 return -EINTR;
7230}
7231
Jens Axboe2b188cc2019-01-07 10:46:33 -07007232/*
7233 * Wait until events become available, if we don't already have some. The
7234 * application must reap them itself, as they reside on the shared cq ring.
7235 */
7236static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007237 const sigset_t __user *sig, size_t sigsz,
7238 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007239{
Jens Axboebda52162019-09-24 13:47:15 -06007240 struct io_wait_queue iowq = {
7241 .wq = {
7242 .private = current,
7243 .func = io_wake_function,
7244 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7245 },
7246 .ctx = ctx,
7247 .to_wait = min_events,
7248 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007249 struct io_rings *rings = ctx->rings;
Hao Xuc73ebb62020-11-03 10:54:37 +08007250 struct timespec64 ts;
7251 signed long timeout = 0;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08007252 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007253
Jens Axboeb41e9852020-02-17 09:52:41 -07007254 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007255 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7256 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007257 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007258 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007259 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007260 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007261
7262 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007263#ifdef CONFIG_COMPAT
7264 if (in_compat_syscall())
7265 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007266 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007267 else
7268#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007269 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007270
Jens Axboe2b188cc2019-01-07 10:46:33 -07007271 if (ret)
7272 return ret;
7273 }
7274
Hao Xuc73ebb62020-11-03 10:54:37 +08007275 if (uts) {
7276 if (get_timespec64(&ts, uts))
7277 return -EFAULT;
7278 timeout = timespec64_to_jiffies(&ts);
7279 }
7280
Jens Axboebda52162019-09-24 13:47:15 -06007281 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007282 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007283 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007284 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007285 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7286 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06007287 /* make sure we run task_work before checking for signals */
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007288 ret = io_run_task_work_sig();
Hao Xu6195ba02021-01-27 15:14:09 +08007289 if (ret > 0) {
7290 finish_wait(&ctx->wait, &iowq.wq);
Jens Axboe4c6e2772020-07-01 11:29:10 -06007291 continue;
Hao Xu6195ba02021-01-27 15:14:09 +08007292 }
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007293 else if (ret < 0)
Jens Axboece593a62020-06-30 12:39:05 -06007294 break;
Pavel Begunkov6c503152021-01-04 20:36:36 +00007295 if (io_should_wake(&iowq))
Jens Axboebda52162019-09-24 13:47:15 -06007296 break;
Hao Xu6195ba02021-01-27 15:14:09 +08007297 if (test_bit(0, &ctx->cq_check_overflow)) {
7298 finish_wait(&ctx->wait, &iowq.wq);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007299 continue;
Hao Xu6195ba02021-01-27 15:14:09 +08007300 }
Hao Xuc73ebb62020-11-03 10:54:37 +08007301 if (uts) {
7302 timeout = schedule_timeout(timeout);
7303 if (timeout == 0) {
7304 ret = -ETIME;
7305 break;
7306 }
7307 } else {
7308 schedule();
7309 }
Jens Axboebda52162019-09-24 13:47:15 -06007310 } while (1);
7311 finish_wait(&ctx->wait, &iowq.wq);
7312
Jens Axboeb7db41c2020-07-04 08:55:50 -06007313 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007314
Hristo Venev75b28af2019-08-26 17:23:46 +00007315 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007316}
7317
Jens Axboe6b063142019-01-10 22:13:58 -07007318static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7319{
7320#if defined(CONFIG_UNIX)
7321 if (ctx->ring_sock) {
7322 struct sock *sock = ctx->ring_sock->sk;
7323 struct sk_buff *skb;
7324
7325 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7326 kfree_skb(skb);
7327 }
7328#else
7329 int i;
7330
Jens Axboe65e19f52019-10-26 07:20:21 -06007331 for (i = 0; i < ctx->nr_user_files; i++) {
7332 struct file *file;
7333
7334 file = io_file_from_index(ctx, i);
7335 if (file)
7336 fput(file);
7337 }
Jens Axboe6b063142019-01-10 22:13:58 -07007338#endif
7339}
7340
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007341static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007342{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007343 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007344
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007345 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007346 complete(&data->done);
7347}
7348
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007349static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7350{
7351 spin_lock_bh(&ctx->rsrc_ref_lock);
7352}
7353
7354static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7355{
7356 spin_unlock_bh(&ctx->rsrc_ref_lock);
7357}
7358
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007359static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7360 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007361 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007362{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007363 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007364 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007365 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007366 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007367 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007368}
7369
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007370static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7371 struct io_ring_ctx *ctx,
7372 struct fixed_rsrc_ref_node *backup_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007373{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007374 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007375 int ret;
Jens Axboe65e19f52019-10-26 07:20:21 -06007376
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007377 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007378 ref_node = data->node;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007379 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007380 if (ref_node)
7381 percpu_ref_kill(&ref_node->refs);
7382
7383 percpu_ref_kill(&data->refs);
7384
7385 /* wait for all refs nodes to complete */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007386 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007387 do {
7388 ret = wait_for_completion_interruptible(&data->done);
7389 if (!ret)
7390 break;
7391 ret = io_run_task_work_sig();
7392 if (ret < 0) {
7393 percpu_ref_resurrect(&data->refs);
7394 reinit_completion(&data->done);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007395 io_sqe_rsrc_set_node(ctx, data, backup_node);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007396 return ret;
7397 }
7398 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007399
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007400 destroy_fixed_rsrc_ref_node(backup_node);
7401 return 0;
7402}
7403
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007404static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7405{
7406 struct fixed_rsrc_data *data;
7407
7408 data = kzalloc(sizeof(*data), GFP_KERNEL);
7409 if (!data)
7410 return NULL;
7411
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007412 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007413 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7414 kfree(data);
7415 return NULL;
7416 }
7417 data->ctx = ctx;
7418 init_completion(&data->done);
7419 return data;
7420}
7421
7422static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7423{
7424 percpu_ref_exit(&data->refs);
7425 kfree(data->table);
7426 kfree(data);
7427}
7428
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007429static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7430{
7431 struct fixed_rsrc_data *data = ctx->file_data;
7432 struct fixed_rsrc_ref_node *backup_node;
7433 unsigned nr_tables, i;
7434 int ret;
7435
7436 if (!data)
7437 return -ENXIO;
7438 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7439 if (!backup_node)
7440 return -ENOMEM;
7441 init_fixed_file_ref_node(ctx, backup_node);
7442
7443 ret = io_rsrc_ref_quiesce(data, ctx, backup_node);
7444 if (ret)
7445 return ret;
7446
Jens Axboe6b063142019-01-10 22:13:58 -07007447 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007448 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7449 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007450 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007451 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007452 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007453 ctx->nr_user_files = 0;
7454 return 0;
7455}
7456
Jens Axboe534ca6d2020-09-02 13:52:19 -06007457static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007458{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007459 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007460 /*
7461 * The park is a bit of a work-around, without it we get
7462 * warning spews on shutdown with SQPOLL set and affinity
7463 * set to a single CPU.
7464 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007465 if (sqd->thread) {
7466 kthread_park(sqd->thread);
7467 kthread_stop(sqd->thread);
7468 }
7469
7470 kfree(sqd);
7471 }
7472}
7473
Jens Axboeaa061652020-09-02 14:50:27 -06007474static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7475{
7476 struct io_ring_ctx *ctx_attach;
7477 struct io_sq_data *sqd;
7478 struct fd f;
7479
7480 f = fdget(p->wq_fd);
7481 if (!f.file)
7482 return ERR_PTR(-ENXIO);
7483 if (f.file->f_op != &io_uring_fops) {
7484 fdput(f);
7485 return ERR_PTR(-EINVAL);
7486 }
7487
7488 ctx_attach = f.file->private_data;
7489 sqd = ctx_attach->sq_data;
7490 if (!sqd) {
7491 fdput(f);
7492 return ERR_PTR(-EINVAL);
7493 }
7494
7495 refcount_inc(&sqd->refs);
7496 fdput(f);
7497 return sqd;
7498}
7499
Jens Axboe534ca6d2020-09-02 13:52:19 -06007500static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7501{
7502 struct io_sq_data *sqd;
7503
Jens Axboeaa061652020-09-02 14:50:27 -06007504 if (p->flags & IORING_SETUP_ATTACH_WQ)
7505 return io_attach_sq_data(p);
7506
Jens Axboe534ca6d2020-09-02 13:52:19 -06007507 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7508 if (!sqd)
7509 return ERR_PTR(-ENOMEM);
7510
7511 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007512 INIT_LIST_HEAD(&sqd->ctx_list);
7513 INIT_LIST_HEAD(&sqd->ctx_new_list);
7514 mutex_init(&sqd->ctx_lock);
7515 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007516 init_waitqueue_head(&sqd->wait);
7517 return sqd;
7518}
7519
Jens Axboe69fb2132020-09-14 11:16:23 -06007520static void io_sq_thread_unpark(struct io_sq_data *sqd)
7521 __releases(&sqd->lock)
7522{
7523 if (!sqd->thread)
7524 return;
7525 kthread_unpark(sqd->thread);
7526 mutex_unlock(&sqd->lock);
7527}
7528
7529static void io_sq_thread_park(struct io_sq_data *sqd)
7530 __acquires(&sqd->lock)
7531{
7532 if (!sqd->thread)
7533 return;
7534 mutex_lock(&sqd->lock);
7535 kthread_park(sqd->thread);
7536}
7537
Jens Axboe534ca6d2020-09-02 13:52:19 -06007538static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7539{
7540 struct io_sq_data *sqd = ctx->sq_data;
7541
7542 if (sqd) {
7543 if (sqd->thread) {
7544 /*
7545 * We may arrive here from the error branch in
7546 * io_sq_offload_create() where the kthread is created
7547 * without being waked up, thus wake it up now to make
7548 * sure the wait will complete.
7549 */
7550 wake_up_process(sqd->thread);
7551 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007552
7553 io_sq_thread_park(sqd);
7554 }
7555
7556 mutex_lock(&sqd->ctx_lock);
7557 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007558 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007559 mutex_unlock(&sqd->ctx_lock);
7560
Xiaoguang Wang08369242020-11-03 14:15:59 +08007561 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007562 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007563
7564 io_put_sq_data(sqd);
7565 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007566 }
7567}
7568
Jens Axboe6b063142019-01-10 22:13:58 -07007569static void io_finish_async(struct io_ring_ctx *ctx)
7570{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007571 io_sq_thread_stop(ctx);
7572
Jens Axboe561fb042019-10-24 07:25:42 -06007573 if (ctx->io_wq) {
7574 io_wq_destroy(ctx->io_wq);
7575 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007576 }
7577}
7578
7579#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007580/*
7581 * Ensure the UNIX gc is aware of our file set, so we are certain that
7582 * the io_uring can be safely unregistered on process exit, even if we have
7583 * loops in the file referencing.
7584 */
7585static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7586{
7587 struct sock *sk = ctx->ring_sock->sk;
7588 struct scm_fp_list *fpl;
7589 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007590 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007591
Jens Axboe6b063142019-01-10 22:13:58 -07007592 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7593 if (!fpl)
7594 return -ENOMEM;
7595
7596 skb = alloc_skb(0, GFP_KERNEL);
7597 if (!skb) {
7598 kfree(fpl);
7599 return -ENOMEM;
7600 }
7601
7602 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007603
Jens Axboe08a45172019-10-03 08:11:03 -06007604 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007605 fpl->user = get_uid(ctx->user);
7606 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007607 struct file *file = io_file_from_index(ctx, i + offset);
7608
7609 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007610 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007611 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007612 unix_inflight(fpl->user, fpl->fp[nr_files]);
7613 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007614 }
7615
Jens Axboe08a45172019-10-03 08:11:03 -06007616 if (nr_files) {
7617 fpl->max = SCM_MAX_FD;
7618 fpl->count = nr_files;
7619 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007620 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007621 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7622 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007623
Jens Axboe08a45172019-10-03 08:11:03 -06007624 for (i = 0; i < nr_files; i++)
7625 fput(fpl->fp[i]);
7626 } else {
7627 kfree_skb(skb);
7628 kfree(fpl);
7629 }
Jens Axboe6b063142019-01-10 22:13:58 -07007630
7631 return 0;
7632}
7633
7634/*
7635 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7636 * causes regular reference counting to break down. We rely on the UNIX
7637 * garbage collection to take care of this problem for us.
7638 */
7639static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7640{
7641 unsigned left, total;
7642 int ret = 0;
7643
7644 total = 0;
7645 left = ctx->nr_user_files;
7646 while (left) {
7647 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007648
7649 ret = __io_sqe_files_scm(ctx, this_files, total);
7650 if (ret)
7651 break;
7652 left -= this_files;
7653 total += this_files;
7654 }
7655
7656 if (!ret)
7657 return 0;
7658
7659 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007660 struct file *file = io_file_from_index(ctx, total);
7661
7662 if (file)
7663 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007664 total++;
7665 }
7666
7667 return ret;
7668}
7669#else
7670static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7671{
7672 return 0;
7673}
7674#endif
7675
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007676static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007677 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007678{
7679 int i;
7680
7681 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007682 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007683 unsigned this_files;
7684
7685 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7686 table->files = kcalloc(this_files, sizeof(struct file *),
7687 GFP_KERNEL);
7688 if (!table->files)
7689 break;
7690 nr_files -= this_files;
7691 }
7692
7693 if (i == nr_tables)
7694 return 0;
7695
7696 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007697 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007698 kfree(table->files);
7699 }
7700 return 1;
7701}
7702
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007703static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007704{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007705 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007706#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007707 struct sock *sock = ctx->ring_sock->sk;
7708 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7709 struct sk_buff *skb;
7710 int i;
7711
7712 __skb_queue_head_init(&list);
7713
7714 /*
7715 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7716 * remove this entry and rearrange the file array.
7717 */
7718 skb = skb_dequeue(head);
7719 while (skb) {
7720 struct scm_fp_list *fp;
7721
7722 fp = UNIXCB(skb).fp;
7723 for (i = 0; i < fp->count; i++) {
7724 int left;
7725
7726 if (fp->fp[i] != file)
7727 continue;
7728
7729 unix_notinflight(fp->user, fp->fp[i]);
7730 left = fp->count - 1 - i;
7731 if (left) {
7732 memmove(&fp->fp[i], &fp->fp[i + 1],
7733 left * sizeof(struct file *));
7734 }
7735 fp->count--;
7736 if (!fp->count) {
7737 kfree_skb(skb);
7738 skb = NULL;
7739 } else {
7740 __skb_queue_tail(&list, skb);
7741 }
7742 fput(file);
7743 file = NULL;
7744 break;
7745 }
7746
7747 if (!file)
7748 break;
7749
7750 __skb_queue_tail(&list, skb);
7751
7752 skb = skb_dequeue(head);
7753 }
7754
7755 if (skb_peek(&list)) {
7756 spin_lock_irq(&head->lock);
7757 while ((skb = __skb_dequeue(&list)) != NULL)
7758 __skb_queue_tail(head, skb);
7759 spin_unlock_irq(&head->lock);
7760 }
7761#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007762 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007763#endif
7764}
7765
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007766static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007767{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007768 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7769 struct io_ring_ctx *ctx = rsrc_data->ctx;
7770 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007771
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007772 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7773 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007774 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007775 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007776 }
7777
Xiaoguang Wang05589552020-03-31 14:05:18 +08007778 percpu_ref_exit(&ref_node->refs);
7779 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007780 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007781}
7782
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007783static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007784{
7785 struct io_ring_ctx *ctx;
7786 struct llist_node *node;
7787
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007788 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7789 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007790
7791 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007792 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007793 struct llist_node *next = node->next;
7794
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007795 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7796 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007797 node = next;
7798 }
7799}
7800
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007801static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007802{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007803 struct fixed_rsrc_ref_node *ref_node;
7804 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007805 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007806 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007807 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007808
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007809 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7810 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007811 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007812
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007813 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007814 ref_node->done = true;
7815
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007816 while (!list_empty(&ctx->rsrc_ref_list)) {
7817 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007818 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007819 /* recycle ref nodes in order */
7820 if (!ref_node->done)
7821 break;
7822 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007823 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007824 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007825 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007826
7827 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007828 delay = 0;
7829
Jens Axboe4a38aed22020-05-14 17:21:15 -06007830 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007831 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007832 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007833 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007834}
7835
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007836static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007837 struct io_ring_ctx *ctx)
7838{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007839 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007840
7841 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7842 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007843 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007844
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007845 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007846 0, GFP_KERNEL)) {
7847 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007848 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007849 }
7850 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007851 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007852 ref_node->done = false;
7853 return ref_node;
7854}
7855
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007856static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7857 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007858{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007859 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007860 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007861}
7862
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007863static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007864{
7865 percpu_ref_exit(&ref_node->refs);
7866 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007867}
7868
7869static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7870 unsigned nr_args)
7871{
7872 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007873 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007874 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007875 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007876 struct fixed_rsrc_ref_node *ref_node;
7877 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007878
7879 if (ctx->file_data)
7880 return -EBUSY;
7881 if (!nr_args)
7882 return -EINVAL;
7883 if (nr_args > IORING_MAX_FIXED_FILES)
7884 return -EMFILE;
7885
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007886 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007887 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007888 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007889
7890 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007891 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007892 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007893 if (!file_data->table)
7894 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007895
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007896 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007897 goto out_free;
Jens Axboe55cbc252020-10-14 07:35:57 -06007898 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007899
7900 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007901 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007902 unsigned index;
7903
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007904 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7905 ret = -EFAULT;
7906 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007907 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007908 /* allow sparse sets */
7909 if (fd == -1)
7910 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007911
Jens Axboe05f3fb32019-12-09 11:22:50 -07007912 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007913 ret = -EBADF;
7914 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007915 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007916
7917 /*
7918 * Don't allow io_uring instances to be registered. If UNIX
7919 * isn't enabled, then this causes a reference cycle and this
7920 * instance can never get freed. If UNIX is enabled we'll
7921 * handle it just fine, but there's still no point in allowing
7922 * a ring fd as it doesn't support regular read/write anyway.
7923 */
7924 if (file->f_op == &io_uring_fops) {
7925 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007926 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007927 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007928 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7929 index = i & IORING_FILE_TABLE_MASK;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007930 table->files[index] = file;
7931 }
7932
Jens Axboe05f3fb32019-12-09 11:22:50 -07007933 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007934 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007935 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007936 return ret;
7937 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007938
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007939 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007940 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007941 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007942 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007943 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007944 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007945
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007946 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007947 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007948out_fput:
7949 for (i = 0; i < ctx->nr_user_files; i++) {
7950 file = io_file_from_index(ctx, i);
7951 if (file)
7952 fput(file);
7953 }
7954 for (i = 0; i < nr_tables; i++)
7955 kfree(file_data->table[i].files);
7956 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007957out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007958 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007959 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007960 return ret;
7961}
7962
Jens Axboec3a31e62019-10-03 13:59:56 -06007963static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7964 int index)
7965{
7966#if defined(CONFIG_UNIX)
7967 struct sock *sock = ctx->ring_sock->sk;
7968 struct sk_buff_head *head = &sock->sk_receive_queue;
7969 struct sk_buff *skb;
7970
7971 /*
7972 * See if we can merge this file into an existing skb SCM_RIGHTS
7973 * file set. If there's no room, fall back to allocating a new skb
7974 * and filling it in.
7975 */
7976 spin_lock_irq(&head->lock);
7977 skb = skb_peek(head);
7978 if (skb) {
7979 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7980
7981 if (fpl->count < SCM_MAX_FD) {
7982 __skb_unlink(skb, head);
7983 spin_unlock_irq(&head->lock);
7984 fpl->fp[fpl->count] = get_file(file);
7985 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7986 fpl->count++;
7987 spin_lock_irq(&head->lock);
7988 __skb_queue_head(head, skb);
7989 } else {
7990 skb = NULL;
7991 }
7992 }
7993 spin_unlock_irq(&head->lock);
7994
7995 if (skb) {
7996 fput(file);
7997 return 0;
7998 }
7999
8000 return __io_sqe_files_scm(ctx, 1, index);
8001#else
8002 return 0;
8003#endif
8004}
8005
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008006static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008007{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008008 struct io_rsrc_put *prsrc;
8009 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008010
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008011 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8012 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08008013 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008014
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008015 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008016 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008017
Hillf Dantona5318d32020-03-23 17:47:15 +08008018 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008019}
8020
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008021static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
8022 struct file *file)
8023{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008024 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008025}
8026
Jens Axboe05f3fb32019-12-09 11:22:50 -07008027static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008028 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008029 unsigned nr_args)
8030{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008031 struct fixed_rsrc_data *data = ctx->file_data;
8032 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008033 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008034 __s32 __user *fds;
8035 int fd, i, err;
8036 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008037 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008038
Jens Axboe05f3fb32019-12-09 11:22:50 -07008039 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06008040 return -EOVERFLOW;
8041 if (done > ctx->nr_user_files)
8042 return -EINVAL;
8043
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008044 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00008045 if (!ref_node)
8046 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008047 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008048
Jens Axboec3a31e62019-10-03 13:59:56 -06008049 done = 0;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008050 fds = u64_to_user_ptr(up->data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008051 while (nr_args) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008052 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06008053 unsigned index;
8054
Jens Axboec3a31e62019-10-03 13:59:56 -06008055 err = 0;
8056 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
8057 err = -EFAULT;
8058 break;
8059 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008060 i = array_index_nospec(up->offset, ctx->nr_user_files);
8061 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06008062 index = i & IORING_FILE_TABLE_MASK;
8063 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08008064 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08008065 err = io_queue_file_removal(data, file);
8066 if (err)
8067 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06008068 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008069 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008070 }
8071 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008072 file = fget(fd);
8073 if (!file) {
8074 err = -EBADF;
8075 break;
8076 }
8077 /*
8078 * Don't allow io_uring instances to be registered. If
8079 * UNIX isn't enabled, then this causes a reference
8080 * cycle and this instance can never get freed. If UNIX
8081 * is enabled we'll handle it just fine, but there's
8082 * still no point in allowing a ring fd as it doesn't
8083 * support regular read/write anyway.
8084 */
8085 if (file->f_op == &io_uring_fops) {
8086 fput(file);
8087 err = -EBADF;
8088 break;
8089 }
Jens Axboe65e19f52019-10-26 07:20:21 -06008090 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008091 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008092 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08008093 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008094 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008095 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008096 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008097 }
8098 nr_args--;
8099 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008100 up->offset++;
8101 }
8102
Xiaoguang Wang05589552020-03-31 14:05:18 +08008103 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008104 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008105 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008106 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008107 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008108
8109 return done ? done : err;
8110}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008111
Jens Axboe05f3fb32019-12-09 11:22:50 -07008112static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8113 unsigned nr_args)
8114{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008115 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008116
8117 if (!ctx->file_data)
8118 return -ENXIO;
8119 if (!nr_args)
8120 return -EINVAL;
8121 if (copy_from_user(&up, arg, sizeof(up)))
8122 return -EFAULT;
8123 if (up.resv)
8124 return -EINVAL;
8125
8126 return __io_sqe_files_update(ctx, &up, nr_args);
8127}
Jens Axboec3a31e62019-10-03 13:59:56 -06008128
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008129static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008130{
8131 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8132
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008133 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07008134 io_put_req(req);
8135}
8136
Pavel Begunkov24369c22020-01-28 03:15:48 +03008137static int io_init_wq_offload(struct io_ring_ctx *ctx,
8138 struct io_uring_params *p)
8139{
8140 struct io_wq_data data;
8141 struct fd f;
8142 struct io_ring_ctx *ctx_attach;
8143 unsigned int concurrency;
8144 int ret = 0;
8145
8146 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008147 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008148 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008149
8150 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8151 /* Do QD, or 4 * CPUS, whatever is smallest */
8152 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8153
8154 ctx->io_wq = io_wq_create(concurrency, &data);
8155 if (IS_ERR(ctx->io_wq)) {
8156 ret = PTR_ERR(ctx->io_wq);
8157 ctx->io_wq = NULL;
8158 }
8159 return ret;
8160 }
8161
8162 f = fdget(p->wq_fd);
8163 if (!f.file)
8164 return -EBADF;
8165
8166 if (f.file->f_op != &io_uring_fops) {
8167 ret = -EINVAL;
8168 goto out_fput;
8169 }
8170
8171 ctx_attach = f.file->private_data;
8172 /* @io_wq is protected by holding the fd */
8173 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8174 ret = -EINVAL;
8175 goto out_fput;
8176 }
8177
8178 ctx->io_wq = ctx_attach->io_wq;
8179out_fput:
8180 fdput(f);
8181 return ret;
8182}
8183
Jens Axboe0f212202020-09-13 13:09:39 -06008184static int io_uring_alloc_task_context(struct task_struct *task)
8185{
8186 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008187 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008188
8189 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8190 if (unlikely(!tctx))
8191 return -ENOMEM;
8192
Jens Axboed8a6df12020-10-15 16:24:45 -06008193 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8194 if (unlikely(ret)) {
8195 kfree(tctx);
8196 return ret;
8197 }
8198
Jens Axboe0f212202020-09-13 13:09:39 -06008199 xa_init(&tctx->xa);
8200 init_waitqueue_head(&tctx->wait);
8201 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008202 atomic_set(&tctx->in_idle, 0);
8203 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008204 io_init_identity(&tctx->__identity);
8205 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008206 task->io_uring = tctx;
8207 return 0;
8208}
8209
8210void __io_uring_free(struct task_struct *tsk)
8211{
8212 struct io_uring_task *tctx = tsk->io_uring;
8213
8214 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008215 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8216 if (tctx->identity != &tctx->__identity)
8217 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008218 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008219 kfree(tctx);
8220 tsk->io_uring = NULL;
8221}
8222
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008223static int io_sq_offload_create(struct io_ring_ctx *ctx,
8224 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008225{
8226 int ret;
8227
Jens Axboe6c271ce2019-01-10 11:22:30 -07008228 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008229 struct io_sq_data *sqd;
8230
Jens Axboe3ec482d2019-04-08 10:51:01 -06008231 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008232 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008233 goto err;
8234
Jens Axboe534ca6d2020-09-02 13:52:19 -06008235 sqd = io_get_sq_data(p);
8236 if (IS_ERR(sqd)) {
8237 ret = PTR_ERR(sqd);
8238 goto err;
8239 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008240
Jens Axboe534ca6d2020-09-02 13:52:19 -06008241 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008242 io_sq_thread_park(sqd);
8243 mutex_lock(&sqd->ctx_lock);
8244 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8245 mutex_unlock(&sqd->ctx_lock);
8246 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008247
Jens Axboe917257d2019-04-13 09:28:55 -06008248 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8249 if (!ctx->sq_thread_idle)
8250 ctx->sq_thread_idle = HZ;
8251
Jens Axboeaa061652020-09-02 14:50:27 -06008252 if (sqd->thread)
8253 goto done;
8254
Jens Axboe6c271ce2019-01-10 11:22:30 -07008255 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008256 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008257
Jens Axboe917257d2019-04-13 09:28:55 -06008258 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008259 if (cpu >= nr_cpu_ids)
8260 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008261 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008262 goto err;
8263
Jens Axboe69fb2132020-09-14 11:16:23 -06008264 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008265 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008266 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008267 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008268 "io_uring-sq");
8269 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008270 if (IS_ERR(sqd->thread)) {
8271 ret = PTR_ERR(sqd->thread);
8272 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008273 goto err;
8274 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008275 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008276 if (ret)
8277 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008278 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8279 /* Can't have SQ_AFF without SQPOLL */
8280 ret = -EINVAL;
8281 goto err;
8282 }
8283
Jens Axboeaa061652020-09-02 14:50:27 -06008284done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008285 ret = io_init_wq_offload(ctx, p);
8286 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008287 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008288
8289 return 0;
8290err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008291 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008292 return ret;
8293}
8294
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008295static void io_sq_offload_start(struct io_ring_ctx *ctx)
8296{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008297 struct io_sq_data *sqd = ctx->sq_data;
8298
8299 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8300 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008301}
8302
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008303static inline void __io_unaccount_mem(struct user_struct *user,
8304 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008305{
8306 atomic_long_sub(nr_pages, &user->locked_vm);
8307}
8308
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008309static inline int __io_account_mem(struct user_struct *user,
8310 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008311{
8312 unsigned long page_limit, cur_pages, new_pages;
8313
8314 /* Don't allow more pages than we can safely lock */
8315 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8316
8317 do {
8318 cur_pages = atomic_long_read(&user->locked_vm);
8319 new_pages = cur_pages + nr_pages;
8320 if (new_pages > page_limit)
8321 return -ENOMEM;
8322 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8323 new_pages) != cur_pages);
8324
8325 return 0;
8326}
8327
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008328static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8329 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008330{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008331 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008332 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008333
Jens Axboe2aede0e2020-09-14 10:45:53 -06008334 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008335 if (acct == ACCT_LOCKED) {
8336 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008337 ctx->mm_account->locked_vm -= nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008338 mmap_write_unlock(ctx->mm_account);
8339 }else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008340 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008341 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008342 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008343}
8344
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008345static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
8346 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008347{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008348 int ret;
8349
8350 if (ctx->limit_mem) {
8351 ret = __io_account_mem(ctx->user, nr_pages);
8352 if (ret)
8353 return ret;
8354 }
8355
Jens Axboe2aede0e2020-09-14 10:45:53 -06008356 if (ctx->mm_account) {
Jens Axboe4bc4a912020-12-17 07:53:33 -07008357 if (acct == ACCT_LOCKED) {
8358 mmap_write_lock(ctx->mm_account);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008359 ctx->mm_account->locked_vm += nr_pages;
Jens Axboe4bc4a912020-12-17 07:53:33 -07008360 mmap_write_unlock(ctx->mm_account);
8361 } else if (acct == ACCT_PINNED) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008362 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Jens Axboe4bc4a912020-12-17 07:53:33 -07008363 }
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008364 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008365
8366 return 0;
8367}
8368
Jens Axboe2b188cc2019-01-07 10:46:33 -07008369static void io_mem_free(void *ptr)
8370{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008371 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008372
Mark Rutland52e04ef2019-04-30 17:30:21 +01008373 if (!ptr)
8374 return;
8375
8376 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008377 if (put_page_testzero(page))
8378 free_compound_page(page);
8379}
8380
8381static void *io_mem_alloc(size_t size)
8382{
8383 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
8384 __GFP_NORETRY;
8385
8386 return (void *) __get_free_pages(gfp_flags, get_order(size));
8387}
8388
Hristo Venev75b28af2019-08-26 17:23:46 +00008389static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8390 size_t *sq_offset)
8391{
8392 struct io_rings *rings;
8393 size_t off, sq_array_size;
8394
8395 off = struct_size(rings, cqes, cq_entries);
8396 if (off == SIZE_MAX)
8397 return SIZE_MAX;
8398
8399#ifdef CONFIG_SMP
8400 off = ALIGN(off, SMP_CACHE_BYTES);
8401 if (off == 0)
8402 return SIZE_MAX;
8403#endif
8404
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008405 if (sq_offset)
8406 *sq_offset = off;
8407
Hristo Venev75b28af2019-08-26 17:23:46 +00008408 sq_array_size = array_size(sizeof(u32), sq_entries);
8409 if (sq_array_size == SIZE_MAX)
8410 return SIZE_MAX;
8411
8412 if (check_add_overflow(off, sq_array_size, &off))
8413 return SIZE_MAX;
8414
Hristo Venev75b28af2019-08-26 17:23:46 +00008415 return off;
8416}
8417
Jens Axboe2b188cc2019-01-07 10:46:33 -07008418static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
8419{
Hristo Venev75b28af2019-08-26 17:23:46 +00008420 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008421
Hristo Venev75b28af2019-08-26 17:23:46 +00008422 pages = (size_t)1 << get_order(
8423 rings_size(sq_entries, cq_entries, NULL));
8424 pages += (size_t)1 << get_order(
8425 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008426
Hristo Venev75b28af2019-08-26 17:23:46 +00008427 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008428}
8429
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008430static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008431{
8432 int i, j;
8433
8434 if (!ctx->user_bufs)
8435 return -ENXIO;
8436
8437 for (i = 0; i < ctx->nr_user_bufs; i++) {
8438 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8439
8440 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008441 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008442
Jens Axboede293932020-09-17 16:19:16 -06008443 if (imu->acct_pages)
8444 io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008445 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008446 imu->nr_bvecs = 0;
8447 }
8448
8449 kfree(ctx->user_bufs);
8450 ctx->user_bufs = NULL;
8451 ctx->nr_user_bufs = 0;
8452 return 0;
8453}
8454
8455static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8456 void __user *arg, unsigned index)
8457{
8458 struct iovec __user *src;
8459
8460#ifdef CONFIG_COMPAT
8461 if (ctx->compat) {
8462 struct compat_iovec __user *ciovs;
8463 struct compat_iovec ciov;
8464
8465 ciovs = (struct compat_iovec __user *) arg;
8466 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8467 return -EFAULT;
8468
Jens Axboed55e5f52019-12-11 16:12:15 -07008469 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008470 dst->iov_len = ciov.iov_len;
8471 return 0;
8472 }
8473#endif
8474 src = (struct iovec __user *) arg;
8475 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8476 return -EFAULT;
8477 return 0;
8478}
8479
Jens Axboede293932020-09-17 16:19:16 -06008480/*
8481 * Not super efficient, but this is just a registration time. And we do cache
8482 * the last compound head, so generally we'll only do a full search if we don't
8483 * match that one.
8484 *
8485 * We check if the given compound head page has already been accounted, to
8486 * avoid double accounting it. This allows us to account the full size of the
8487 * page, not just the constituent pages of a huge page.
8488 */
8489static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8490 int nr_pages, struct page *hpage)
8491{
8492 int i, j;
8493
8494 /* check current page array */
8495 for (i = 0; i < nr_pages; i++) {
8496 if (!PageCompound(pages[i]))
8497 continue;
8498 if (compound_head(pages[i]) == hpage)
8499 return true;
8500 }
8501
8502 /* check previously registered pages */
8503 for (i = 0; i < ctx->nr_user_bufs; i++) {
8504 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8505
8506 for (j = 0; j < imu->nr_bvecs; j++) {
8507 if (!PageCompound(imu->bvec[j].bv_page))
8508 continue;
8509 if (compound_head(imu->bvec[j].bv_page) == hpage)
8510 return true;
8511 }
8512 }
8513
8514 return false;
8515}
8516
8517static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8518 int nr_pages, struct io_mapped_ubuf *imu,
8519 struct page **last_hpage)
8520{
8521 int i, ret;
8522
8523 for (i = 0; i < nr_pages; i++) {
8524 if (!PageCompound(pages[i])) {
8525 imu->acct_pages++;
8526 } else {
8527 struct page *hpage;
8528
8529 hpage = compound_head(pages[i]);
8530 if (hpage == *last_hpage)
8531 continue;
8532 *last_hpage = hpage;
8533 if (headpage_already_acct(ctx, pages, i, hpage))
8534 continue;
8535 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8536 }
8537 }
8538
8539 if (!imu->acct_pages)
8540 return 0;
8541
8542 ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
8543 if (ret)
8544 imu->acct_pages = 0;
8545 return ret;
8546}
8547
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008548static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8549 struct io_mapped_ubuf *imu,
8550 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008551{
8552 struct vm_area_struct **vmas = NULL;
8553 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008554 unsigned long off, start, end, ubuf;
8555 size_t size;
8556 int ret, pret, nr_pages, i;
8557
8558 ubuf = (unsigned long) iov->iov_base;
8559 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8560 start = ubuf >> PAGE_SHIFT;
8561 nr_pages = end - start;
8562
8563 ret = -ENOMEM;
8564
8565 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8566 if (!pages)
8567 goto done;
8568
8569 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8570 GFP_KERNEL);
8571 if (!vmas)
8572 goto done;
8573
8574 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8575 GFP_KERNEL);
8576 if (!imu->bvec)
8577 goto done;
8578
8579 ret = 0;
8580 mmap_read_lock(current->mm);
8581 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8582 pages, vmas);
8583 if (pret == nr_pages) {
8584 /* don't support file backed memory */
8585 for (i = 0; i < nr_pages; i++) {
8586 struct vm_area_struct *vma = vmas[i];
8587
8588 if (vma->vm_file &&
8589 !is_file_hugepages(vma->vm_file)) {
8590 ret = -EOPNOTSUPP;
8591 break;
8592 }
8593 }
8594 } else {
8595 ret = pret < 0 ? pret : -EFAULT;
8596 }
8597 mmap_read_unlock(current->mm);
8598 if (ret) {
8599 /*
8600 * if we did partial map, or found file backed vmas,
8601 * release any pages we did get
8602 */
8603 if (pret > 0)
8604 unpin_user_pages(pages, pret);
8605 kvfree(imu->bvec);
8606 goto done;
8607 }
8608
8609 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8610 if (ret) {
8611 unpin_user_pages(pages, pret);
8612 kvfree(imu->bvec);
8613 goto done;
8614 }
8615
8616 off = ubuf & ~PAGE_MASK;
8617 size = iov->iov_len;
8618 for (i = 0; i < nr_pages; i++) {
8619 size_t vec_len;
8620
8621 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8622 imu->bvec[i].bv_page = pages[i];
8623 imu->bvec[i].bv_len = vec_len;
8624 imu->bvec[i].bv_offset = off;
8625 off = 0;
8626 size -= vec_len;
8627 }
8628 /* store original address for later verification */
8629 imu->ubuf = ubuf;
8630 imu->len = iov->iov_len;
8631 imu->nr_bvecs = nr_pages;
8632 ret = 0;
8633done:
8634 kvfree(pages);
8635 kvfree(vmas);
8636 return ret;
8637}
8638
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008639static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008640{
Jens Axboeedafcce2019-01-09 09:16:05 -07008641 if (ctx->user_bufs)
8642 return -EBUSY;
8643 if (!nr_args || nr_args > UIO_MAXIOV)
8644 return -EINVAL;
8645
8646 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8647 GFP_KERNEL);
8648 if (!ctx->user_bufs)
8649 return -ENOMEM;
8650
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008651 return 0;
8652}
8653
8654static int io_buffer_validate(struct iovec *iov)
8655{
8656 /*
8657 * Don't impose further limits on the size and buffer
8658 * constraints here, we'll -EINVAL later when IO is
8659 * submitted if they are wrong.
8660 */
8661 if (!iov->iov_base || !iov->iov_len)
8662 return -EFAULT;
8663
8664 /* arbitrary limit, but we need something */
8665 if (iov->iov_len > SZ_1G)
8666 return -EFAULT;
8667
8668 return 0;
8669}
8670
8671static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8672 unsigned int nr_args)
8673{
8674 int i, ret;
8675 struct iovec iov;
8676 struct page *last_hpage = NULL;
8677
8678 ret = io_buffers_map_alloc(ctx, nr_args);
8679 if (ret)
8680 return ret;
8681
Jens Axboeedafcce2019-01-09 09:16:05 -07008682 for (i = 0; i < nr_args; i++) {
8683 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008684
8685 ret = io_copy_iov(ctx, &iov, arg, i);
8686 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008687 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008688
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008689 ret = io_buffer_validate(&iov);
8690 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008691 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008692
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008693 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8694 if (ret)
8695 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008696
8697 ctx->nr_user_bufs++;
8698 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008699
8700 if (ret)
8701 io_sqe_buffers_unregister(ctx);
8702
Jens Axboeedafcce2019-01-09 09:16:05 -07008703 return ret;
8704}
8705
Jens Axboe9b402842019-04-11 11:45:41 -06008706static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8707{
8708 __s32 __user *fds = arg;
8709 int fd;
8710
8711 if (ctx->cq_ev_fd)
8712 return -EBUSY;
8713
8714 if (copy_from_user(&fd, fds, sizeof(*fds)))
8715 return -EFAULT;
8716
8717 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8718 if (IS_ERR(ctx->cq_ev_fd)) {
8719 int ret = PTR_ERR(ctx->cq_ev_fd);
8720 ctx->cq_ev_fd = NULL;
8721 return ret;
8722 }
8723
8724 return 0;
8725}
8726
8727static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8728{
8729 if (ctx->cq_ev_fd) {
8730 eventfd_ctx_put(ctx->cq_ev_fd);
8731 ctx->cq_ev_fd = NULL;
8732 return 0;
8733 }
8734
8735 return -ENXIO;
8736}
8737
Jens Axboe5a2e7452020-02-23 16:23:11 -07008738static int __io_destroy_buffers(int id, void *p, void *data)
8739{
8740 struct io_ring_ctx *ctx = data;
8741 struct io_buffer *buf = p;
8742
Jens Axboe067524e2020-03-02 16:32:28 -07008743 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008744 return 0;
8745}
8746
8747static void io_destroy_buffers(struct io_ring_ctx *ctx)
8748{
8749 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8750 idr_destroy(&ctx->io_buffer_idr);
8751}
8752
Jens Axboe2b188cc2019-01-07 10:46:33 -07008753static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8754{
Jens Axboe6b063142019-01-10 22:13:58 -07008755 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008756 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008757
8758 if (ctx->sqo_task) {
8759 put_task_struct(ctx->sqo_task);
8760 ctx->sqo_task = NULL;
8761 mmdrop(ctx->mm_account);
8762 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008763 }
Jens Axboedef596e2019-01-09 08:59:42 -07008764
Dennis Zhou91d8f512020-09-16 13:41:05 -07008765#ifdef CONFIG_BLK_CGROUP
8766 if (ctx->sqo_blkcg_css)
8767 css_put(ctx->sqo_blkcg_css);
8768#endif
8769
Jens Axboe6b063142019-01-10 22:13:58 -07008770 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008771 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008772 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008773 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008774
Jens Axboe2b188cc2019-01-07 10:46:33 -07008775#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008776 if (ctx->ring_sock) {
8777 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008778 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008779 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008780#endif
8781
Hristo Venev75b28af2019-08-26 17:23:46 +00008782 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008783 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008784
8785 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008786 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008787 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008788 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008789 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008790 kfree(ctx);
8791}
8792
8793static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8794{
8795 struct io_ring_ctx *ctx = file->private_data;
8796 __poll_t mask = 0;
8797
8798 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008799 /*
8800 * synchronizes with barrier from wq_has_sleeper call in
8801 * io_commit_cqring
8802 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008803 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008804 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008805 mask |= EPOLLOUT | EPOLLWRNORM;
Pavel Begunkov6c503152021-01-04 20:36:36 +00008806 io_cqring_overflow_flush(ctx, false, NULL, NULL);
8807 if (io_cqring_events(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008808 mask |= EPOLLIN | EPOLLRDNORM;
8809
8810 return mask;
8811}
8812
8813static int io_uring_fasync(int fd, struct file *file, int on)
8814{
8815 struct io_ring_ctx *ctx = file->private_data;
8816
8817 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8818}
8819
Jens Axboe071698e2020-01-28 10:04:42 -07008820static int io_remove_personalities(int id, void *p, void *data)
8821{
8822 struct io_ring_ctx *ctx = data;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008823 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008824
Jens Axboe1e6fa522020-10-15 08:46:24 -06008825 iod = idr_remove(&ctx->personality_idr, id);
8826 if (iod) {
8827 put_cred(iod->creds);
8828 if (refcount_dec_and_test(&iod->count))
8829 kfree(iod);
8830 }
Jens Axboe071698e2020-01-28 10:04:42 -07008831 return 0;
8832}
8833
Jens Axboe85faa7b2020-04-09 18:14:00 -06008834static void io_ring_exit_work(struct work_struct *work)
8835{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008836 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8837 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008838
Jens Axboe56952e92020-06-17 15:00:04 -06008839 /*
8840 * If we're doing polled IO and end up having requests being
8841 * submitted async (out-of-line), then completions can come in while
8842 * we're waiting for refs to drop. We need to reap these manually,
8843 * as nobody else will be looking for them.
8844 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008845 do {
Pavel Begunkov90df0852021-01-04 20:43:30 +00008846 __io_uring_cancel_task_requests(ctx, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008847 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008848 io_ring_ctx_free(ctx);
8849}
8850
Jens Axboe00c18642020-12-20 10:45:02 -07008851static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8852{
8853 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8854
8855 return req->ctx == data;
8856}
8857
Jens Axboe2b188cc2019-01-07 10:46:33 -07008858static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8859{
8860 mutex_lock(&ctx->uring_lock);
8861 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008862
8863 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8864 ctx->sqo_dead = 1;
8865
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008866 /* if force is set, the ring is going away. always drop after that */
8867 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008868 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008869 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008870 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008871 mutex_unlock(&ctx->uring_lock);
8872
Pavel Begunkov6b819282020-11-06 13:00:25 +00008873 io_kill_timeouts(ctx, NULL, NULL);
8874 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008875
8876 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008877 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008878
Jens Axboe15dff282019-11-13 09:09:23 -07008879 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008880 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008881
8882 /*
8883 * Do this upfront, so we won't have a grace period where the ring
8884 * is closed but resources aren't reaped yet. This can cause
8885 * spurious failure in setting up a new ring.
8886 */
Jens Axboe760618f2020-07-24 12:53:31 -06008887 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8888 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008889
Jens Axboe85faa7b2020-04-09 18:14:00 -06008890 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008891 /*
8892 * Use system_unbound_wq to avoid spawning tons of event kworkers
8893 * if we're exiting a ton of rings at the same time. It just adds
8894 * noise and overhead, there's no discernable change in runtime
8895 * over using system_wq.
8896 */
8897 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008898}
8899
8900static int io_uring_release(struct inode *inode, struct file *file)
8901{
8902 struct io_ring_ctx *ctx = file->private_data;
8903
8904 file->private_data = NULL;
8905 io_ring_ctx_wait_and_kill(ctx);
8906 return 0;
8907}
8908
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008909struct io_task_cancel {
8910 struct task_struct *task;
8911 struct files_struct *files;
8912};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008913
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008914static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008915{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008916 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008917 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008918 bool ret;
8919
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008920 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008921 unsigned long flags;
8922 struct io_ring_ctx *ctx = req->ctx;
8923
8924 /* protect against races with linked timeouts */
8925 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008926 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008927 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8928 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008929 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008930 }
8931 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008932}
8933
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008934static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008935 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008936 struct files_struct *files)
8937{
8938 struct io_defer_entry *de = NULL;
8939 LIST_HEAD(list);
8940
8941 spin_lock_irq(&ctx->completion_lock);
8942 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008943 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008944 list_cut_position(&list, &ctx->defer_list, &de->list);
8945 break;
8946 }
8947 }
8948 spin_unlock_irq(&ctx->completion_lock);
8949
8950 while (!list_empty(&list)) {
8951 de = list_first_entry(&list, struct io_defer_entry, list);
8952 list_del_init(&de->list);
8953 req_set_fail_links(de->req);
8954 io_put_req(de->req);
8955 io_req_complete(de->req, -ECANCELED);
8956 kfree(de);
8957 }
8958}
8959
Pavel Begunkovca70f002021-01-26 15:28:27 +00008960static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8961 struct task_struct *task,
8962 struct files_struct *files)
8963{
8964 struct io_kiocb *req;
8965 int cnt = 0;
8966
8967 spin_lock_irq(&ctx->inflight_lock);
8968 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8969 cnt += io_match_task(req, task, files);
8970 spin_unlock_irq(&ctx->inflight_lock);
8971 return cnt;
8972}
8973
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008974static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008975 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008976 struct files_struct *files)
8977{
Jens Axboefcb323c2019-10-24 12:39:47 -06008978 while (!list_empty_careful(&ctx->inflight_list)) {
Pavel Begunkovbee749b2020-11-25 02:19:23 +00008979 struct io_task_cancel cancel = { .task = task, .files = files };
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008980 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008981 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008982
Pavel Begunkovca70f002021-01-26 15:28:27 +00008983 inflight = io_uring_count_inflight(ctx, task, files);
8984 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008985 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008986
8987 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true);
8988 io_poll_remove_all(ctx, task, files);
8989 io_kill_timeouts(ctx, task, files);
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00008990 io_cqring_overflow_flush(ctx, true, task, files);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008991 /* cancellations _may_ trigger task work */
8992 io_run_task_work();
Pavel Begunkovca70f002021-01-26 15:28:27 +00008993
8994 prepare_to_wait(&task->io_uring->wait, &wait,
8995 TASK_UNINTERRUPTIBLE);
8996 if (inflight == io_uring_count_inflight(ctx, task, files))
8997 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008998 finish_wait(&task->io_uring->wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008999 }
9000}
9001
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009002static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
9003 struct task_struct *task)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009004{
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009005 while (1) {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009006 struct io_task_cancel cancel = { .task = task, .files = NULL, };
Jens Axboe0f212202020-09-13 13:09:39 -06009007 enum io_wq_cancel cret;
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009008 bool ret = false;
Jens Axboe0f212202020-09-13 13:09:39 -06009009
Pavel Begunkov90df0852021-01-04 20:43:30 +00009010 if (ctx->io_wq) {
9011 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
9012 &cancel, true);
9013 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9014 }
Jens Axboe0f212202020-09-13 13:09:39 -06009015
9016 /* SQPOLL thread does its own polling */
9017 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
9018 while (!list_empty_careful(&ctx->iopoll_list)) {
9019 io_iopoll_try_reap_events(ctx);
9020 ret = true;
9021 }
9022 }
9023
Pavel Begunkov6b819282020-11-06 13:00:25 +00009024 ret |= io_poll_remove_all(ctx, task, NULL);
9025 ret |= io_kill_timeouts(ctx, task, NULL);
Pavel Begunkov55583d72020-12-20 13:21:43 +00009026 ret |= io_run_task_work();
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009027 if (!ret)
9028 break;
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009029 cond_resched();
Jens Axboe0f212202020-09-13 13:09:39 -06009030 }
Jens Axboe0f212202020-09-13 13:09:39 -06009031}
9032
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009033static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
9034{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009035 mutex_lock(&ctx->uring_lock);
9036 ctx->sqo_dead = 1;
9037 mutex_unlock(&ctx->uring_lock);
9038
9039 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00009040 if (ctx->rings)
9041 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009042}
9043
Jens Axboe0f212202020-09-13 13:09:39 -06009044/*
9045 * We need to iteratively cancel requests, in case a request has dependent
9046 * hard links. These persist even for failure of cancelations, hence keep
9047 * looping until none are found.
9048 */
9049static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
9050 struct files_struct *files)
9051{
9052 struct task_struct *task = current;
9053
Jens Axboefdaf0832020-10-30 09:37:30 -06009054 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009055 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06009056 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06009057 atomic_inc(&task->io_uring->in_idle);
9058 io_sq_thread_park(ctx->sq_data);
9059 }
Jens Axboe0f212202020-09-13 13:09:39 -06009060
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009061 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06009062 io_cqring_overflow_flush(ctx, true, task, files);
9063
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00009064 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009065 if (!files)
9066 __io_uring_cancel_task_requests(ctx, task);
Jens Axboefdaf0832020-10-30 09:37:30 -06009067
9068 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9069 atomic_dec(&task->io_uring->in_idle);
9070 /*
9071 * If the files that are going away are the ones in the thread
9072 * identity, clear them out.
9073 */
9074 if (task->io_uring->identity->files == files)
9075 task->io_uring->identity->files = NULL;
9076 io_sq_thread_unpark(ctx->sq_data);
9077 }
Jens Axboe0f212202020-09-13 13:09:39 -06009078}
9079
9080/*
9081 * Note that this task has used io_uring. We use it for cancelation purposes.
9082 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009083static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009084{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009085 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009086 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009087
9088 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009089 ret = io_uring_alloc_task_context(current);
9090 if (unlikely(ret))
9091 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009092 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009093 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009094 if (tctx->last != file) {
9095 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009096
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009097 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009098 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009099 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9100 file, GFP_KERNEL));
9101 if (ret) {
9102 fput(file);
9103 return ret;
9104 }
Jens Axboe0f212202020-09-13 13:09:39 -06009105 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009106 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009107 }
9108
Jens Axboefdaf0832020-10-30 09:37:30 -06009109 /*
9110 * This is race safe in that the task itself is doing this, hence it
9111 * cannot be going through the exit/cancel paths at the same time.
9112 * This cannot be modified while exit/cancel is running.
9113 */
9114 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9115 tctx->sqpoll = true;
9116
Jens Axboe0f212202020-09-13 13:09:39 -06009117 return 0;
9118}
9119
9120/*
9121 * Remove this io_uring_file -> task mapping.
9122 */
9123static void io_uring_del_task_file(struct file *file)
9124{
9125 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009126
9127 if (tctx->last == file)
9128 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009129 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009130 if (file)
9131 fput(file);
9132}
9133
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009134static void io_uring_remove_task_files(struct io_uring_task *tctx)
9135{
9136 struct file *file;
9137 unsigned long index;
9138
9139 xa_for_each(&tctx->xa, index, file)
9140 io_uring_del_task_file(file);
9141}
9142
Jens Axboe0f212202020-09-13 13:09:39 -06009143void __io_uring_files_cancel(struct files_struct *files)
9144{
9145 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009146 struct file *file;
9147 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009148
9149 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009150 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009151 xa_for_each(&tctx->xa, index, file)
9152 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009153 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009154
9155 if (files)
9156 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009157}
9158
9159static s64 tctx_inflight(struct io_uring_task *tctx)
9160{
9161 unsigned long index;
9162 struct file *file;
9163 s64 inflight;
9164
9165 inflight = percpu_counter_sum(&tctx->inflight);
9166 if (!tctx->sqpoll)
9167 return inflight;
9168
9169 /*
9170 * If we have SQPOLL rings, then we need to iterate and find them, and
9171 * add the pending count for those.
9172 */
9173 xa_for_each(&tctx->xa, index, file) {
9174 struct io_ring_ctx *ctx = file->private_data;
9175
9176 if (ctx->flags & IORING_SETUP_SQPOLL) {
9177 struct io_uring_task *__tctx = ctx->sqo_task->io_uring;
9178
9179 inflight += percpu_counter_sum(&__tctx->inflight);
9180 }
9181 }
9182
9183 return inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009184}
9185
Jens Axboe0f212202020-09-13 13:09:39 -06009186/*
9187 * Find any io_uring fd that this task has registered or done IO on, and cancel
9188 * requests.
9189 */
9190void __io_uring_task_cancel(void)
9191{
9192 struct io_uring_task *tctx = current->io_uring;
9193 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009194 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009195
9196 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009197 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009198
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009199 /* trigger io_disable_sqo_submit() */
9200 if (tctx->sqpoll)
9201 __io_uring_files_cancel(NULL);
9202
Jens Axboed8a6df12020-10-15 16:24:45 -06009203 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009204 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009205 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009206 if (!inflight)
9207 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009208 __io_uring_files_cancel(NULL);
9209
9210 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9211
9212 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009213 * If we've seen completions, retry without waiting. This
9214 * avoids a race where a completion comes in before we did
9215 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009216 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009217 if (inflight == tctx_inflight(tctx))
9218 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009219 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009220 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009221
Jens Axboefdaf0832020-10-30 09:37:30 -06009222 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009223
9224 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009225}
9226
Jens Axboefcb323c2019-10-24 12:39:47 -06009227static int io_uring_flush(struct file *file, void *data)
9228{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009229 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009230 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009231
Jens Axboe84965ff2021-01-23 15:51:11 -07009232 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
9233 io_uring_cancel_task_requests(ctx, NULL);
9234
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009235 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009236 return 0;
9237
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009238 /* we should have cancelled and erased it before PF_EXITING */
9239 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9240 xa_load(&tctx->xa, (unsigned long)file));
9241
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009242 /*
9243 * fput() is pending, will be 2 if the only other ref is our potential
9244 * task file note. If the task is exiting, drop regardless of count.
9245 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009246 if (atomic_long_read(&file->f_count) != 2)
9247 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009248
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009249 if (ctx->flags & IORING_SETUP_SQPOLL) {
9250 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009251 WARN_ON_ONCE(ctx->sqo_task != current &&
9252 xa_load(&tctx->xa, (unsigned long)file));
9253 /* sqo_dead check is for when this happens after cancellation */
9254 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009255 !xa_load(&tctx->xa, (unsigned long)file));
9256
9257 io_disable_sqo_submit(ctx);
9258 }
9259
9260 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9261 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009262 return 0;
9263}
9264
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009265static void *io_uring_validate_mmap_request(struct file *file,
9266 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009267{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009268 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009269 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009270 struct page *page;
9271 void *ptr;
9272
9273 switch (offset) {
9274 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009275 case IORING_OFF_CQ_RING:
9276 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009277 break;
9278 case IORING_OFF_SQES:
9279 ptr = ctx->sq_sqes;
9280 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009281 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009282 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009283 }
9284
9285 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009286 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009287 return ERR_PTR(-EINVAL);
9288
9289 return ptr;
9290}
9291
9292#ifdef CONFIG_MMU
9293
9294static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9295{
9296 size_t sz = vma->vm_end - vma->vm_start;
9297 unsigned long pfn;
9298 void *ptr;
9299
9300 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9301 if (IS_ERR(ptr))
9302 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303
9304 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9305 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9306}
9307
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009308#else /* !CONFIG_MMU */
9309
9310static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9311{
9312 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9313}
9314
9315static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9316{
9317 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9318}
9319
9320static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9321 unsigned long addr, unsigned long len,
9322 unsigned long pgoff, unsigned long flags)
9323{
9324 void *ptr;
9325
9326 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9327 if (IS_ERR(ptr))
9328 return PTR_ERR(ptr);
9329
9330 return (unsigned long) ptr;
9331}
9332
9333#endif /* !CONFIG_MMU */
9334
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009335static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009336{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009337 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009338 DEFINE_WAIT(wait);
9339
9340 do {
9341 if (!io_sqring_full(ctx))
9342 break;
9343
9344 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9345
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009346 if (unlikely(ctx->sqo_dead)) {
9347 ret = -EOWNERDEAD;
9348 goto out;
9349 }
9350
Jens Axboe90554202020-09-03 12:12:41 -06009351 if (!io_sqring_full(ctx))
9352 break;
9353
9354 schedule();
9355 } while (!signal_pending(current));
9356
9357 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009358out:
9359 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009360}
9361
Hao Xuc73ebb62020-11-03 10:54:37 +08009362static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9363 struct __kernel_timespec __user **ts,
9364 const sigset_t __user **sig)
9365{
9366 struct io_uring_getevents_arg arg;
9367
9368 /*
9369 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9370 * is just a pointer to the sigset_t.
9371 */
9372 if (!(flags & IORING_ENTER_EXT_ARG)) {
9373 *sig = (const sigset_t __user *) argp;
9374 *ts = NULL;
9375 return 0;
9376 }
9377
9378 /*
9379 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9380 * timespec and sigset_t pointers if good.
9381 */
9382 if (*argsz != sizeof(arg))
9383 return -EINVAL;
9384 if (copy_from_user(&arg, argp, sizeof(arg)))
9385 return -EFAULT;
9386 *sig = u64_to_user_ptr(arg.sigmask);
9387 *argsz = arg.sigmask_sz;
9388 *ts = u64_to_user_ptr(arg.ts);
9389 return 0;
9390}
9391
Jens Axboe2b188cc2019-01-07 10:46:33 -07009392SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009393 u32, min_complete, u32, flags, const void __user *, argp,
9394 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009395{
9396 struct io_ring_ctx *ctx;
9397 long ret = -EBADF;
9398 int submitted = 0;
9399 struct fd f;
9400
Jens Axboe4c6e2772020-07-01 11:29:10 -06009401 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009402
Jens Axboe90554202020-09-03 12:12:41 -06009403 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009404 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009405 return -EINVAL;
9406
9407 f = fdget(fd);
9408 if (!f.file)
9409 return -EBADF;
9410
9411 ret = -EOPNOTSUPP;
9412 if (f.file->f_op != &io_uring_fops)
9413 goto out_fput;
9414
9415 ret = -ENXIO;
9416 ctx = f.file->private_data;
9417 if (!percpu_ref_tryget(&ctx->refs))
9418 goto out_fput;
9419
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009420 ret = -EBADFD;
9421 if (ctx->flags & IORING_SETUP_R_DISABLED)
9422 goto out;
9423
Jens Axboe6c271ce2019-01-10 11:22:30 -07009424 /*
9425 * For SQ polling, the thread will do all submissions and completions.
9426 * Just return the requested submit count, and wake the thread if
9427 * we were asked to.
9428 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009429 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009430 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009431 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009432
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009433 ret = -EOWNERDEAD;
9434 if (unlikely(ctx->sqo_dead))
9435 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009436 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009437 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009438 if (flags & IORING_ENTER_SQ_WAIT) {
9439 ret = io_sqpoll_wait_sq(ctx);
9440 if (ret)
9441 goto out;
9442 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009443 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009444 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009445 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009446 if (unlikely(ret))
9447 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009448 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009449 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009450 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009451
9452 if (submitted != to_submit)
9453 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009454 }
9455 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009456 const sigset_t __user *sig;
9457 struct __kernel_timespec __user *ts;
9458
9459 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9460 if (unlikely(ret))
9461 goto out;
9462
Jens Axboe2b188cc2019-01-07 10:46:33 -07009463 min_complete = min(min_complete, ctx->cq_entries);
9464
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009465 /*
9466 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9467 * space applications don't need to do io completion events
9468 * polling again, they can rely on io_sq_thread to do polling
9469 * work, which can reduce cpu usage and uring_lock contention.
9470 */
9471 if (ctx->flags & IORING_SETUP_IOPOLL &&
9472 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009473 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009474 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009475 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009476 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009477 }
9478
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009479out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009480 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009481out_fput:
9482 fdput(f);
9483 return submitted ? submitted : ret;
9484}
9485
Tobias Klauserbebdb652020-02-26 18:38:32 +01009486#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009487static int io_uring_show_cred(int id, void *p, void *data)
9488{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009489 struct io_identity *iod = p;
9490 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009491 struct seq_file *m = data;
9492 struct user_namespace *uns = seq_user_ns(m);
9493 struct group_info *gi;
9494 kernel_cap_t cap;
9495 unsigned __capi;
9496 int g;
9497
9498 seq_printf(m, "%5d\n", id);
9499 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9500 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9501 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9502 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9503 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9504 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9505 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9506 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9507 seq_puts(m, "\n\tGroups:\t");
9508 gi = cred->group_info;
9509 for (g = 0; g < gi->ngroups; g++) {
9510 seq_put_decimal_ull(m, g ? " " : "",
9511 from_kgid_munged(uns, gi->gid[g]));
9512 }
9513 seq_puts(m, "\n\tCapEff:\t");
9514 cap = cred->cap_effective;
9515 CAP_FOR_EACH_U32(__capi)
9516 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9517 seq_putc(m, '\n');
9518 return 0;
9519}
9520
9521static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9522{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009523 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009524 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009525 int i;
9526
Jens Axboefad8e0d2020-09-28 08:57:48 -06009527 /*
9528 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9529 * since fdinfo case grabs it in the opposite direction of normal use
9530 * cases. If we fail to get the lock, we just don't iterate any
9531 * structures that could be going away outside the io_uring mutex.
9532 */
9533 has_lock = mutex_trylock(&ctx->uring_lock);
9534
Joseph Qidbbe9c62020-09-29 09:01:22 -06009535 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9536 sq = ctx->sq_data;
9537
9538 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9539 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009540 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009541 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00009542 struct fixed_rsrc_table *table;
Jens Axboe87ce9552020-01-30 08:25:34 -07009543 struct file *f;
9544
9545 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
9546 f = table->files[i & IORING_FILE_TABLE_MASK];
9547 if (f)
9548 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9549 else
9550 seq_printf(m, "%5u: <none>\n", i);
9551 }
9552 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009553 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009554 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9555
9556 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9557 (unsigned int) buf->len);
9558 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009559 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009560 seq_printf(m, "Personalities:\n");
9561 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9562 }
Jens Axboed7718a92020-02-14 22:23:12 -07009563 seq_printf(m, "PollList:\n");
9564 spin_lock_irq(&ctx->completion_lock);
9565 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9566 struct hlist_head *list = &ctx->cancel_hash[i];
9567 struct io_kiocb *req;
9568
9569 hlist_for_each_entry(req, list, hash_node)
9570 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9571 req->task->task_works != NULL);
9572 }
9573 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009574 if (has_lock)
9575 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009576}
9577
9578static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9579{
9580 struct io_ring_ctx *ctx = f->private_data;
9581
9582 if (percpu_ref_tryget(&ctx->refs)) {
9583 __io_uring_show_fdinfo(ctx, m);
9584 percpu_ref_put(&ctx->refs);
9585 }
9586}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009587#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009588
Jens Axboe2b188cc2019-01-07 10:46:33 -07009589static const struct file_operations io_uring_fops = {
9590 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009591 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009592 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009593#ifndef CONFIG_MMU
9594 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9595 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9596#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009597 .poll = io_uring_poll,
9598 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009599#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009600 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009601#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009602};
9603
9604static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9605 struct io_uring_params *p)
9606{
Hristo Venev75b28af2019-08-26 17:23:46 +00009607 struct io_rings *rings;
9608 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009609
Jens Axboebd740482020-08-05 12:58:23 -06009610 /* make sure these are sane, as we already accounted them */
9611 ctx->sq_entries = p->sq_entries;
9612 ctx->cq_entries = p->cq_entries;
9613
Hristo Venev75b28af2019-08-26 17:23:46 +00009614 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9615 if (size == SIZE_MAX)
9616 return -EOVERFLOW;
9617
9618 rings = io_mem_alloc(size);
9619 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009620 return -ENOMEM;
9621
Hristo Venev75b28af2019-08-26 17:23:46 +00009622 ctx->rings = rings;
9623 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9624 rings->sq_ring_mask = p->sq_entries - 1;
9625 rings->cq_ring_mask = p->cq_entries - 1;
9626 rings->sq_ring_entries = p->sq_entries;
9627 rings->cq_ring_entries = p->cq_entries;
9628 ctx->sq_mask = rings->sq_ring_mask;
9629 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009630
9631 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009632 if (size == SIZE_MAX) {
9633 io_mem_free(ctx->rings);
9634 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009635 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009636 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009637
9638 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009639 if (!ctx->sq_sqes) {
9640 io_mem_free(ctx->rings);
9641 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009642 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009643 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009644
Jens Axboe2b188cc2019-01-07 10:46:33 -07009645 return 0;
9646}
9647
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009648static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9649{
9650 int ret, fd;
9651
9652 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9653 if (fd < 0)
9654 return fd;
9655
9656 ret = io_uring_add_task_file(ctx, file);
9657 if (ret) {
9658 put_unused_fd(fd);
9659 return ret;
9660 }
9661 fd_install(fd, file);
9662 return fd;
9663}
9664
Jens Axboe2b188cc2019-01-07 10:46:33 -07009665/*
9666 * Allocate an anonymous fd, this is what constitutes the application
9667 * visible backing of an io_uring instance. The application mmaps this
9668 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9669 * we have to tie this fd to a socket for file garbage collection purposes.
9670 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009671static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009672{
9673 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009674#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009675 int ret;
9676
Jens Axboe2b188cc2019-01-07 10:46:33 -07009677 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9678 &ctx->ring_sock);
9679 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009680 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009681#endif
9682
Jens Axboe2b188cc2019-01-07 10:46:33 -07009683 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9684 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009685#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009686 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009687 sock_release(ctx->ring_sock);
9688 ctx->ring_sock = NULL;
9689 } else {
9690 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009691 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009692#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009693 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009694}
9695
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009696static int io_uring_create(unsigned entries, struct io_uring_params *p,
9697 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009698{
9699 struct user_struct *user = NULL;
9700 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009701 struct file *file;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009702 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009703 int ret;
9704
Jens Axboe8110c1a2019-12-28 15:39:54 -07009705 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009706 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009707 if (entries > IORING_MAX_ENTRIES) {
9708 if (!(p->flags & IORING_SETUP_CLAMP))
9709 return -EINVAL;
9710 entries = IORING_MAX_ENTRIES;
9711 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009712
9713 /*
9714 * Use twice as many entries for the CQ ring. It's possible for the
9715 * application to drive a higher depth than the size of the SQ ring,
9716 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009717 * some flexibility in overcommitting a bit. If the application has
9718 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9719 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009720 */
9721 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009722 if (p->flags & IORING_SETUP_CQSIZE) {
9723 /*
9724 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9725 * to a power-of-two, if it isn't already. We do NOT impose
9726 * any cq vs sq ring sizing.
9727 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009728 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009729 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009730 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9731 if (!(p->flags & IORING_SETUP_CLAMP))
9732 return -EINVAL;
9733 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9734 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009735 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9736 if (p->cq_entries < p->sq_entries)
9737 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009738 } else {
9739 p->cq_entries = 2 * p->sq_entries;
9740 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009741
9742 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009743 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009744
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009745 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009746 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009747 ring_pages(p->sq_entries, p->cq_entries));
9748 if (ret) {
9749 free_uid(user);
9750 return ret;
9751 }
9752 }
9753
9754 ctx = io_ring_ctx_alloc(p);
9755 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009756 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009757 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009758 p->cq_entries));
9759 free_uid(user);
9760 return -ENOMEM;
9761 }
9762 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009763 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009764 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009765#ifdef CONFIG_AUDIT
9766 ctx->loginuid = current->loginuid;
9767 ctx->sessionid = current->sessionid;
9768#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009769 ctx->sqo_task = get_task_struct(current);
9770
9771 /*
9772 * This is just grabbed for accounting purposes. When a process exits,
9773 * the mm is exited and dropped before the files, hence we need to hang
9774 * on to this mm purely for the purposes of being able to unaccount
9775 * memory (locked/pinned vm). It's not used for anything else.
9776 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009777 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009778 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009779
Dennis Zhou91d8f512020-09-16 13:41:05 -07009780#ifdef CONFIG_BLK_CGROUP
9781 /*
9782 * The sq thread will belong to the original cgroup it was inited in.
9783 * If the cgroup goes offline (e.g. disabling the io controller), then
9784 * issued bios will be associated with the closest cgroup later in the
9785 * block layer.
9786 */
9787 rcu_read_lock();
9788 ctx->sqo_blkcg_css = blkcg_css();
9789 ret = css_tryget_online(ctx->sqo_blkcg_css);
9790 rcu_read_unlock();
9791 if (!ret) {
9792 /* don't init against a dying cgroup, have the user try again */
9793 ctx->sqo_blkcg_css = NULL;
9794 ret = -ENODEV;
9795 goto err;
9796 }
9797#endif
Jens Axboe6c271ce2019-01-10 11:22:30 -07009798
Jens Axboe2b188cc2019-01-07 10:46:33 -07009799 /*
9800 * Account memory _before_ installing the file descriptor. Once
9801 * the descriptor is installed, it can get closed at any time. Also
Jens Axboe2b188cc2019-01-07 10:46:33 -07009802 * do this before hitting the general error path, as ring freeing
Hristo Venev75b28af2019-08-26 17:23:46 +00009803 * will un-account as well.
9804 */
9805 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9806 ACCT_LOCKED);
9807 ctx->limit_mem = limit_mem;
9808
9809 ret = io_allocate_scq_urings(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009810 if (ret)
9811 goto err;
Hristo Venev75b28af2019-08-26 17:23:46 +00009812
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009813 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009814 if (ret)
9815 goto err;
9816
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009817 if (!(p->flags & IORING_SETUP_R_DISABLED))
9818 io_sq_offload_start(ctx);
9819
Jens Axboe2b188cc2019-01-07 10:46:33 -07009820 memset(&p->sq_off, 0, sizeof(p->sq_off));
9821 p->sq_off.head = offsetof(struct io_rings, sq.head);
9822 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9823 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9824 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9825 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9826 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9827 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
9828
9829 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009830 p->cq_off.head = offsetof(struct io_rings, cq.head);
9831 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9832 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9833 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9834 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9835 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009836 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009837
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009838 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9839 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009840 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009841 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9842 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009843
9844 if (copy_to_user(params, p, sizeof(*p))) {
9845 ret = -EFAULT;
9846 goto err;
9847 }
Jens Axboed1719f72020-07-30 13:43:53 -06009848
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009849 file = io_uring_get_file(ctx);
9850 if (IS_ERR(file)) {
9851 ret = PTR_ERR(file);
9852 goto err;
9853 }
9854
Jens Axboed1719f72020-07-30 13:43:53 -06009855 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009856 * Install ring fd as the very last thing, so we don't risk someone
9857 * having closed it before we finish setup
9858 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009859 ret = io_uring_install_fd(ctx, file);
9860 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009861 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009862 /* fput will clean it up */
9863 fput(file);
9864 return ret;
9865 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009866
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009867 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009868 return ret;
9869err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009870 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009871 io_ring_ctx_wait_and_kill(ctx);
9872 return ret;
9873}
9874
9875/*
9876 * Sets up an aio uring context, and returns the fd. Applications asks for a
9877 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9878 * params structure passed in.
9879 */
9880static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9881{
9882 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009883 int i;
9884
9885 if (copy_from_user(&p, params, sizeof(p)))
9886 return -EFAULT;
9887 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9888 if (p.resv[i])
9889 return -EINVAL;
9890 }
9891
Jens Axboe6c271ce2019-01-10 11:22:30 -07009892 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009893 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009894 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9895 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009896 return -EINVAL;
9897
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009898 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009899}
9900
9901SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9902 struct io_uring_params __user *, params)
9903{
9904 return io_uring_setup(entries, params);
9905}
9906
Jens Axboe66f4af92020-01-16 15:36:52 -07009907static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9908{
9909 struct io_uring_probe *p;
9910 size_t size;
9911 int i, ret;
9912
9913 size = struct_size(p, ops, nr_args);
9914 if (size == SIZE_MAX)
9915 return -EOVERFLOW;
9916 p = kzalloc(size, GFP_KERNEL);
9917 if (!p)
9918 return -ENOMEM;
9919
9920 ret = -EFAULT;
9921 if (copy_from_user(p, arg, size))
9922 goto out;
9923 ret = -EINVAL;
9924 if (memchr_inv(p, 0, size))
9925 goto out;
9926
9927 p->last_op = IORING_OP_LAST - 1;
9928 if (nr_args > IORING_OP_LAST)
9929 nr_args = IORING_OP_LAST;
9930
9931 for (i = 0; i < nr_args; i++) {
9932 p->ops[i].op = i;
9933 if (!io_op_defs[i].not_supported)
9934 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9935 }
9936 p->ops_len = i;
9937
9938 ret = 0;
9939 if (copy_to_user(arg, p, size))
9940 ret = -EFAULT;
9941out:
9942 kfree(p);
9943 return ret;
9944}
9945
Jens Axboe071698e2020-01-28 10:04:42 -07009946static int io_register_personality(struct io_ring_ctx *ctx)
9947{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009948 struct io_identity *id;
9949 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009950
Jens Axboe1e6fa522020-10-15 08:46:24 -06009951 id = kmalloc(sizeof(*id), GFP_KERNEL);
9952 if (unlikely(!id))
9953 return -ENOMEM;
9954
9955 io_init_identity(id);
9956 id->creds = get_current_cred();
9957
9958 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9959 if (ret < 0) {
9960 put_cred(id->creds);
9961 kfree(id);
9962 }
9963 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009964}
9965
9966static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9967{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009968 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07009969
Jens Axboe1e6fa522020-10-15 08:46:24 -06009970 iod = idr_remove(&ctx->personality_idr, id);
9971 if (iod) {
9972 put_cred(iod->creds);
9973 if (refcount_dec_and_test(&iod->count))
9974 kfree(iod);
Jens Axboe071698e2020-01-28 10:04:42 -07009975 return 0;
9976 }
9977
9978 return -EINVAL;
9979}
9980
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009981static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9982 unsigned int nr_args)
9983{
9984 struct io_uring_restriction *res;
9985 size_t size;
9986 int i, ret;
9987
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009988 /* Restrictions allowed only if rings started disabled */
9989 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9990 return -EBADFD;
9991
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009992 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009993 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009994 return -EBUSY;
9995
9996 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9997 return -EINVAL;
9998
9999 size = array_size(nr_args, sizeof(*res));
10000 if (size == SIZE_MAX)
10001 return -EOVERFLOW;
10002
10003 res = memdup_user(arg, size);
10004 if (IS_ERR(res))
10005 return PTR_ERR(res);
10006
10007 ret = 0;
10008
10009 for (i = 0; i < nr_args; i++) {
10010 switch (res[i].opcode) {
10011 case IORING_RESTRICTION_REGISTER_OP:
10012 if (res[i].register_op >= IORING_REGISTER_LAST) {
10013 ret = -EINVAL;
10014 goto out;
10015 }
10016
10017 __set_bit(res[i].register_op,
10018 ctx->restrictions.register_op);
10019 break;
10020 case IORING_RESTRICTION_SQE_OP:
10021 if (res[i].sqe_op >= IORING_OP_LAST) {
10022 ret = -EINVAL;
10023 goto out;
10024 }
10025
10026 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10027 break;
10028 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10029 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10030 break;
10031 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10032 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10033 break;
10034 default:
10035 ret = -EINVAL;
10036 goto out;
10037 }
10038 }
10039
10040out:
10041 /* Reset all restrictions if an error happened */
10042 if (ret != 0)
10043 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10044 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010045 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010046
10047 kfree(res);
10048 return ret;
10049}
10050
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010051static int io_register_enable_rings(struct io_ring_ctx *ctx)
10052{
10053 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10054 return -EBADFD;
10055
10056 if (ctx->restrictions.registered)
10057 ctx->restricted = 1;
10058
10059 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10060
10061 io_sq_offload_start(ctx);
10062
10063 return 0;
10064}
10065
Jens Axboe071698e2020-01-28 10:04:42 -070010066static bool io_register_op_must_quiesce(int op)
10067{
10068 switch (op) {
10069 case IORING_UNREGISTER_FILES:
10070 case IORING_REGISTER_FILES_UPDATE:
10071 case IORING_REGISTER_PROBE:
10072 case IORING_REGISTER_PERSONALITY:
10073 case IORING_UNREGISTER_PERSONALITY:
10074 return false;
10075 default:
10076 return true;
10077 }
10078}
10079
Jens Axboeedafcce2019-01-09 09:16:05 -070010080static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10081 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010082 __releases(ctx->uring_lock)
10083 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010084{
10085 int ret;
10086
Jens Axboe35fa71a2019-04-22 10:23:23 -060010087 /*
10088 * We're inside the ring mutex, if the ref is already dying, then
10089 * someone else killed the ctx or is already going through
10090 * io_uring_register().
10091 */
10092 if (percpu_ref_is_dying(&ctx->refs))
10093 return -ENXIO;
10094
Jens Axboe071698e2020-01-28 10:04:42 -070010095 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010096 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010097
Jens Axboe05f3fb32019-12-09 11:22:50 -070010098 /*
10099 * Drop uring mutex before waiting for references to exit. If
10100 * another thread is currently inside io_uring_enter() it might
10101 * need to grab the uring_lock to make progress. If we hold it
10102 * here across the drain wait, then we can deadlock. It's safe
10103 * to drop the mutex here, since no new references will come in
10104 * after we've killed the percpu ref.
10105 */
10106 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010107 do {
10108 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10109 if (!ret)
10110 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010111 ret = io_run_task_work_sig();
10112 if (ret < 0)
10113 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010114 } while (1);
10115
Jens Axboe05f3fb32019-12-09 11:22:50 -070010116 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010117
Jens Axboec1503682020-01-08 08:26:07 -070010118 if (ret) {
10119 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010120 goto out_quiesce;
10121 }
10122 }
10123
10124 if (ctx->restricted) {
10125 if (opcode >= IORING_REGISTER_LAST) {
10126 ret = -EINVAL;
10127 goto out;
10128 }
10129
10130 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10131 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010132 goto out;
10133 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010134 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010135
10136 switch (opcode) {
10137 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010138 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010139 break;
10140 case IORING_UNREGISTER_BUFFERS:
10141 ret = -EINVAL;
10142 if (arg || nr_args)
10143 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010144 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010145 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010146 case IORING_REGISTER_FILES:
10147 ret = io_sqe_files_register(ctx, arg, nr_args);
10148 break;
10149 case IORING_UNREGISTER_FILES:
10150 ret = -EINVAL;
10151 if (arg || nr_args)
10152 break;
10153 ret = io_sqe_files_unregister(ctx);
10154 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010155 case IORING_REGISTER_FILES_UPDATE:
10156 ret = io_sqe_files_update(ctx, arg, nr_args);
10157 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010158 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010159 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010160 ret = -EINVAL;
10161 if (nr_args != 1)
10162 break;
10163 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010164 if (ret)
10165 break;
10166 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10167 ctx->eventfd_async = 1;
10168 else
10169 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010170 break;
10171 case IORING_UNREGISTER_EVENTFD:
10172 ret = -EINVAL;
10173 if (arg || nr_args)
10174 break;
10175 ret = io_eventfd_unregister(ctx);
10176 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010177 case IORING_REGISTER_PROBE:
10178 ret = -EINVAL;
10179 if (!arg || nr_args > 256)
10180 break;
10181 ret = io_probe(ctx, arg, nr_args);
10182 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010183 case IORING_REGISTER_PERSONALITY:
10184 ret = -EINVAL;
10185 if (arg || nr_args)
10186 break;
10187 ret = io_register_personality(ctx);
10188 break;
10189 case IORING_UNREGISTER_PERSONALITY:
10190 ret = -EINVAL;
10191 if (arg)
10192 break;
10193 ret = io_unregister_personality(ctx, nr_args);
10194 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010195 case IORING_REGISTER_ENABLE_RINGS:
10196 ret = -EINVAL;
10197 if (arg || nr_args)
10198 break;
10199 ret = io_register_enable_rings(ctx);
10200 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010201 case IORING_REGISTER_RESTRICTIONS:
10202 ret = io_register_restrictions(ctx, arg, nr_args);
10203 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010204 default:
10205 ret = -EINVAL;
10206 break;
10207 }
10208
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010209out:
Jens Axboe071698e2020-01-28 10:04:42 -070010210 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010211 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010212 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010213out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -060010214 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010215 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010216 return ret;
10217}
10218
10219SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10220 void __user *, arg, unsigned int, nr_args)
10221{
10222 struct io_ring_ctx *ctx;
10223 long ret = -EBADF;
10224 struct fd f;
10225
10226 f = fdget(fd);
10227 if (!f.file)
10228 return -EBADF;
10229
10230 ret = -EOPNOTSUPP;
10231 if (f.file->f_op != &io_uring_fops)
10232 goto out_fput;
10233
10234 ctx = f.file->private_data;
10235
10236 mutex_lock(&ctx->uring_lock);
10237 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10238 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010239 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10240 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010241out_fput:
10242 fdput(f);
10243 return ret;
10244}
10245
Jens Axboe2b188cc2019-01-07 10:46:33 -070010246static int __init io_uring_init(void)
10247{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010248#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10249 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10250 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10251} while (0)
10252
10253#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10254 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10255 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10256 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10257 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10258 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10259 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10260 BUILD_BUG_SQE_ELEM(8, __u64, off);
10261 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10262 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010263 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010264 BUILD_BUG_SQE_ELEM(24, __u32, len);
10265 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10266 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10267 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10268 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010269 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10270 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010271 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10272 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10273 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10274 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10275 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10276 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10277 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10278 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010279 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010280 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10281 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10282 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010283 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010284
Jens Axboed3656342019-12-18 09:50:26 -070010285 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010286 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -070010287 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
10288 return 0;
10289};
10290__initcall(io_uring_init);