blob: 8e14e71bf6accfedab72e9c037c829e923a41a5b [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
Pavel Begunkovd068b502021-05-16 22:58:11 +010014 * through a control-dependency in io_get_cqe (smp_store_release to
Stefan Bühler1e84b972019-04-24 23:54:16 +020015 * 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 Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070076#include <linux/eventpoll.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030077#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070078#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060079#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060080#include <linux/io_uring.h>
Nadav Amitef98eb02021-08-07 17:13:41 -070081#include <linux/tracehook.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Olivier Langlois4ce8ad92021-06-23 11:50:18 -070093#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
Jens Axboe65e19f52019-10-26 07:20:21 -060094
wangyangbo187f08c2021-08-19 13:56:57 +080095/* only define max */
Pavel Begunkov042b0d82021-08-09 13:04:01 +010096#define IORING_MAX_FIXED_FILES (1U << 15)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020097#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
98 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -070099
wangyangbo187f08c2021-08-19 13:56:57 +0800100#define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100101#define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
102#define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
103
Pavel Begunkov489809e2021-05-14 12:06:44 +0100104#define IORING_MAX_REG_BUFFERS (1U << 14)
105
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000106#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
107 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
108 IOSQE_BUFFER_SELECT)
Pavel Begunkovc8543572021-06-17 18:14:04 +0100109#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
110 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000111
Pavel Begunkov09899b12021-06-14 02:36:22 +0100112#define IO_TCTX_REFS_CACHE_NR (1U << 10)
113
Jens Axboe2b188cc2019-01-07 10:46:33 -0700114struct io_uring {
115 u32 head ____cacheline_aligned_in_smp;
116 u32 tail ____cacheline_aligned_in_smp;
117};
118
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 * This data is shared with the application through the mmap at offsets
121 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 *
123 * The offsets to the member fields are published through struct
124 * io_sqring_offsets when calling io_uring_setup.
125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
128 * Head and tail offsets into the ring; the offsets need to be
129 * masked to get valid indices.
130 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 * The kernel controls head of the sq ring and the tail of the cq ring,
132 * and the application controls tail of the sq ring and the head of the
133 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000135 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 * ring_entries - 1)
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_ring_mask, cq_ring_mask;
141 /* Ring sizes (constant, power of 2) */
142 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
144 * Number of invalid entries dropped by the kernel due to
145 * invalid index stored in array
146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application (i.e. get number of "new events" by comparing to
149 * cached value).
150 *
151 * After a new SQ head value was read by the application this
152 * counter includes all submissions that were dropped reaching
153 * the new SQ head (and possibly more).
154 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000155 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200156 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200157 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 *
159 * Written by the kernel, shouldn't be modified by the
160 * application.
161 *
162 * The application needs a full memory barrier before checking
163 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200166 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200167 * Runtime CQ flags
168 *
169 * Written by the application, shouldn't be modified by the
170 * kernel.
171 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100172 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200173 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200174 * Number of completion events lost because the queue was full;
175 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800176 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200177 * the completion queue.
178 *
179 * Written by the kernel, shouldn't be modified by the
180 * application (i.e. get number of "new events" by comparing to
181 * cached value).
182 *
183 * As completion events come in out of order this counter is not
184 * ordered with any other data.
185 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000186 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200187 /*
188 * Ring buffer of completion events.
189 *
190 * The kernel writes completion events fresh every time they are
191 * produced, so the application is allowed to modify pending
192 * entries.
193 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000194 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700195};
196
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000197enum io_uring_cmd_flags {
198 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000199 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000200};
201
Jens Axboeedafcce2019-01-09 09:16:05 -0700202struct io_mapped_ubuf {
203 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100204 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700205 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600206 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100207 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700208};
209
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000210struct io_ring_ctx;
211
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000212struct io_overflow_cqe {
213 struct io_uring_cqe cqe;
214 struct list_head list;
215};
216
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100217struct io_fixed_file {
218 /* file * with additional FFS_* flags */
219 unsigned long file_ptr;
220};
221
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000222struct io_rsrc_put {
223 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100224 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000225 union {
226 void *rsrc;
227 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100228 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000229 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000230};
231
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100232struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100233 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700234};
235
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100236struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800237 struct percpu_ref refs;
238 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000239 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100240 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600241 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000242 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800243};
244
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100245typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
246
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100247struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700248 struct io_ring_ctx *ctx;
249
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100250 u64 **tags;
251 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100252 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100253 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700254 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800255 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700256};
257
Jens Axboe5a2e7452020-02-23 16:23:11 -0700258struct io_buffer {
259 struct list_head list;
260 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300261 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700262 __u16 bid;
263};
264
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200265struct io_restriction {
266 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
267 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
268 u8 sqe_flags_allowed;
269 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200270 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200271};
272
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700273enum {
274 IO_SQ_THREAD_SHOULD_STOP = 0,
275 IO_SQ_THREAD_SHOULD_PARK,
276};
277
Jens Axboe534ca6d2020-09-02 13:52:19 -0600278struct io_sq_data {
279 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000280 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000281 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600282
283 /* ctx's that are using this sqd */
284 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600285
Jens Axboe534ca6d2020-09-02 13:52:19 -0600286 struct task_struct *thread;
287 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800288
289 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700290 int sq_cpu;
291 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700292 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700293
294 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700295 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600296};
297
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000298#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000299#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000300#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000301
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000302struct io_submit_link {
303 struct io_kiocb *head;
304 struct io_kiocb *last;
305};
306
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000307struct io_submit_state {
308 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000309 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000310
311 /*
312 * io_kiocb alloc cache
313 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000314 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000315 unsigned int free_reqs;
316
317 bool plug_started;
318
319 /*
320 * Batch completion logic
321 */
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +0100322 struct io_kiocb *compl_reqs[IO_COMPL_BATCH];
323 unsigned int compl_nr;
324 /* inline/task_work completion list, under ->uring_lock */
325 struct list_head free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000326
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000327 unsigned int ios_left;
328};
329
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100331 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332 struct {
333 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700334
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100335 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700336 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800337 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800338 unsigned int drain_next: 1;
339 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200340 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100341 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100342 unsigned int drain_active: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100343 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100345 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100346 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100347 struct mutex uring_lock;
348
Hristo Venev75b28af2019-08-26 17:23:46 +0000349 /*
350 * Ring buffer of indices into array of io_uring_sqe, which is
351 * mmapped by the application using the IORING_OFF_SQES offset.
352 *
353 * This indirection could e.g. be used to assign fixed
354 * io_uring_sqe entries to operations and only submit them to
355 * the queue when needed.
356 *
357 * The kernel modifies neither the indices array nor the entries
358 * array.
359 */
360 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100361 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362 unsigned cached_sq_head;
363 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600364 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100365
366 /*
367 * Fixed resources fast path, should be accessed only under
368 * uring_lock, and updated through io_uring_register(2)
369 */
370 struct io_rsrc_node *rsrc_node;
371 struct io_file_table file_table;
372 unsigned nr_user_files;
373 unsigned nr_user_bufs;
374 struct io_mapped_ubuf **user_bufs;
375
376 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600377 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700378 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100379 struct xarray io_buffers;
380 struct xarray personalities;
381 u32 pers_next;
382 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383 } ____cacheline_aligned_in_smp;
384
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100385 /* IRQ completion list, under ->completion_lock */
386 struct list_head locked_free_list;
387 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700388
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100389 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600390 struct io_sq_data *sq_data; /* if using sq thread polling */
391
Jens Axboe90554202020-09-03 12:12:41 -0600392 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600393 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000394
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100395 unsigned long check_cq_overflow;
396
Jens Axboe206aefd2019-11-07 18:27:42 -0700397 struct {
398 unsigned cached_cq_tail;
399 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700400 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100401 struct wait_queue_head poll_wait;
402 struct wait_queue_head cq_wait;
403 unsigned cq_extra;
404 atomic_t cq_timeouts;
405 struct fasync_struct *cq_fasync;
406 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700407 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408
409 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700411
Jens Axboe89850fc2021-08-10 15:11:51 -0600412 spinlock_t timeout_lock;
413
Jens Axboedef596e2019-01-09 08:59:42 -0700414 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300415 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700416 * io_uring instances that don't use IORING_SETUP_SQPOLL.
417 * For SQPOLL, only the single threaded io_sq_thread() will
418 * manipulate the list, hence no extra locking is needed there.
419 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300420 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700421 struct hlist_head *cancel_hash;
422 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800423 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600425
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200426 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700427
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100428 /* slow path rsrc auxilary data, used by update/register */
429 struct {
430 struct io_rsrc_node *rsrc_backup_node;
431 struct io_mapped_ubuf *dummy_ubuf;
432 struct io_rsrc_data *file_data;
433 struct io_rsrc_data *buf_data;
434
435 struct delayed_work rsrc_put_work;
436 struct llist_head rsrc_put_llist;
437 struct list_head rsrc_ref_list;
438 spinlock_t rsrc_ref_lock;
439 };
440
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700441 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100442 struct {
443 #if defined(CONFIG_UNIX)
444 struct socket *ring_sock;
445 #endif
446 /* hashed buffered write serialization */
447 struct io_wq_hash *hash_map;
448
449 /* Only used for accounting purposes */
450 struct user_struct *user;
451 struct mm_struct *mm_account;
452
453 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100454 struct llist_head fallback_llist;
455 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100456 struct work_struct exit_work;
457 struct list_head tctx_list;
458 struct completion ref_comp;
459 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460};
461
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100462struct io_uring_task {
463 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100464 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100465 struct xarray xa;
466 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100467 const struct io_ring_ctx *last;
468 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100469 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100470 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100471 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100472
473 spinlock_t task_lock;
474 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100475 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100476 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100477};
478
Jens Axboe09bb8392019-03-13 12:39:28 -0600479/*
480 * First field must be the file pointer in all the
481 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
482 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700483struct io_poll_iocb {
484 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000485 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700486 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600487 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700488 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700489 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700490};
491
Pavel Begunkov9d805892021-04-13 02:58:40 +0100492struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000493 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100494 u64 old_user_data;
495 u64 new_user_data;
496 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600497 bool update_events;
498 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000499};
500
Jens Axboeb5dba592019-12-11 14:02:38 -0700501struct io_close {
502 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700503 int fd;
504};
505
Jens Axboead8a48a2019-11-15 08:49:11 -0700506struct io_timeout_data {
507 struct io_kiocb *req;
508 struct hrtimer timer;
509 struct timespec64 ts;
510 enum hrtimer_mode mode;
511};
512
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700513struct io_accept {
514 struct file *file;
515 struct sockaddr __user *addr;
516 int __user *addr_len;
517 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600518 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700519};
520
521struct io_sync {
522 struct file *file;
523 loff_t len;
524 loff_t off;
525 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700526 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700527};
528
Jens Axboefbf23842019-12-17 18:45:56 -0700529struct io_cancel {
530 struct file *file;
531 u64 addr;
532};
533
Jens Axboeb29472e2019-12-17 18:50:29 -0700534struct io_timeout {
535 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300536 u32 off;
537 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300538 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000539 /* head of the link, used by linked timeouts only */
540 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600541 /* for linked completions */
542 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700543};
544
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100545struct io_timeout_rem {
546 struct file *file;
547 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000548
549 /* timeout update */
550 struct timespec64 ts;
551 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100552};
553
Jens Axboe9adbd452019-12-20 08:45:55 -0700554struct io_rw {
555 /* NOTE: kiocb has the file as the first member, so don't do it here */
556 struct kiocb kiocb;
557 u64 addr;
558 u64 len;
559};
560
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700561struct io_connect {
562 struct file *file;
563 struct sockaddr __user *addr;
564 int addr_len;
565};
566
Jens Axboee47293f2019-12-20 08:58:21 -0700567struct io_sr_msg {
568 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700569 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100570 struct compat_msghdr __user *umsg_compat;
571 struct user_msghdr __user *umsg;
572 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700573 };
Jens Axboee47293f2019-12-20 08:58:21 -0700574 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700575 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700576 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700577 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700578};
579
Jens Axboe15b71ab2019-12-11 11:20:36 -0700580struct io_open {
581 struct file *file;
582 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700583 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700584 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600585 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700586};
587
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000588struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700589 struct file *file;
590 u64 arg;
591 u32 nr_args;
592 u32 offset;
593};
594
Jens Axboe4840e412019-12-25 22:03:45 -0700595struct io_fadvise {
596 struct file *file;
597 u64 offset;
598 u32 len;
599 u32 advice;
600};
601
Jens Axboec1ca7572019-12-25 22:18:28 -0700602struct io_madvise {
603 struct file *file;
604 u64 addr;
605 u32 len;
606 u32 advice;
607};
608
Jens Axboe3e4827b2020-01-08 15:18:09 -0700609struct io_epoll {
610 struct file *file;
611 int epfd;
612 int op;
613 int fd;
614 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700615};
616
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300617struct io_splice {
618 struct file *file_out;
619 struct file *file_in;
620 loff_t off_out;
621 loff_t off_in;
622 u64 len;
623 unsigned int flags;
624};
625
Jens Axboeddf0322d2020-02-23 16:41:33 -0700626struct io_provide_buf {
627 struct file *file;
628 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100629 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700630 __u32 bgid;
631 __u16 nbufs;
632 __u16 bid;
633};
634
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700635struct io_statx {
636 struct file *file;
637 int dfd;
638 unsigned int mask;
639 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700640 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700641 struct statx __user *buffer;
642};
643
Jens Axboe36f4fa62020-09-05 11:14:22 -0600644struct io_shutdown {
645 struct file *file;
646 int how;
647};
648
Jens Axboe80a261f2020-09-28 14:23:58 -0600649struct io_rename {
650 struct file *file;
651 int old_dfd;
652 int new_dfd;
653 struct filename *oldpath;
654 struct filename *newpath;
655 int flags;
656};
657
Jens Axboe14a11432020-09-28 14:27:37 -0600658struct io_unlink {
659 struct file *file;
660 int dfd;
661 int flags;
662 struct filename *filename;
663};
664
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700665struct io_mkdir {
666 struct file *file;
667 int dfd;
668 umode_t mode;
669 struct filename *filename;
670};
671
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300672struct io_completion {
673 struct file *file;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000674 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300675};
676
Jens Axboef499a022019-12-02 16:28:46 -0700677struct io_async_connect {
678 struct sockaddr_storage address;
679};
680
Jens Axboe03b12302019-12-02 18:50:25 -0700681struct io_async_msghdr {
682 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000683 /* points to an allocated iov, if NULL we use fast_iov instead */
684 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700685 struct sockaddr __user *uaddr;
686 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700687 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700688};
689
Jens Axboef67676d2019-12-02 11:03:47 -0700690struct io_async_rw {
691 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600692 const struct iovec *free_iovec;
693 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600694 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600695 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700696};
697
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300698enum {
699 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
700 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
701 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
702 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
703 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700704 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300705
Pavel Begunkovdddca222021-04-27 16:13:52 +0100706 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100707 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300708 REQ_F_INFLIGHT_BIT,
709 REQ_F_CUR_POS_BIT,
710 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300711 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300712 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700713 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700714 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000715 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600716 REQ_F_REISSUE_BIT,
Pavel Begunkov8c130822021-03-22 01:58:32 +0000717 REQ_F_DONT_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100718 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100719 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100720 REQ_F_ARM_LTIMEOUT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700721 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100722 REQ_F_NOWAIT_READ_BIT,
723 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700724 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700725
726 /* not a real bit, just to check we're not overflowing the space */
727 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300728};
729
730enum {
731 /* ctx owns file */
732 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
733 /* drain existing IO first */
734 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
735 /* linked sqes */
736 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
737 /* doesn't sever on completion < 0 */
738 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
739 /* IOSQE_ASYNC */
740 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700741 /* IOSQE_BUFFER_SELECT */
742 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300743
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300744 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100745 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000746 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300747 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
748 /* read/write uses file position */
749 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
750 /* must not punt to workers */
751 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100752 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300753 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300754 /* needs cleanup */
755 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700756 /* already went through poll handler */
757 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700758 /* buffer already selected */
759 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000760 /* completion is deferred through io_comp_state */
761 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600762 /* caller should reissue async */
763 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov8c130822021-03-22 01:58:32 +0000764 /* don't attempt request reissue, see io_rw_reissue() */
765 REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700766 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100767 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700768 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100769 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700770 /* regular file */
771 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100772 /* has creds assigned */
773 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100774 /* skip refcounting if not set */
775 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100776 /* there is a linked timeout that has to be armed */
777 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700778};
779
780struct async_poll {
781 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600782 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300783};
784
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100785typedef void (*io_req_tw_func_t)(struct io_kiocb *req);
786
Jens Axboe7cbf1722021-02-10 00:03:20 +0000787struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100788 union {
789 struct io_wq_work_node node;
790 struct llist_node fallback_node;
791 };
792 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000793};
794
Pavel Begunkov992da012021-06-10 16:37:37 +0100795enum {
796 IORING_RSRC_FILE = 0,
797 IORING_RSRC_BUFFER = 1,
798};
799
Jens Axboe09bb8392019-03-13 12:39:28 -0600800/*
801 * NOTE! Each of the iocb union members has the file pointer
802 * as the first entry in their struct definition. So you can
803 * access the file pointer through any of the sub-structs,
804 * or directly as just 'ki_filp' in this struct.
805 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700806struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700807 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600808 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700809 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700810 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100811 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700812 struct io_accept accept;
813 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700814 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700815 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100816 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700817 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700818 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700819 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700820 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000821 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700822 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700823 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700824 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300825 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700826 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700827 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600828 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600829 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600830 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700831 struct io_mkdir mkdir;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300832 /* use only after cleaning per-op data, see io_clean_op() */
833 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700834 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700835
Jens Axboee8c2bc12020-08-15 18:44:09 -0700836 /* opcode allocated if it needs to store data for async defer */
837 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700838 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800839 /* polled IO has completed */
840 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700842 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300843 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700844
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300845 struct io_ring_ctx *ctx;
846 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700847 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300848 struct task_struct *task;
849 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700850
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000851 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000852 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700853
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100854 /* used with ctx->iopoll_list with reads/writes */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300855 struct list_head inflight_entry;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100856 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300857 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
858 struct hlist_node hash_node;
859 struct async_poll *apoll;
860 struct io_wq_work work;
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100861 const struct cred *creds;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +0100862
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100863 /* store used ubuf, so we can prevent reloading */
864 struct io_mapped_ubuf *imu;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865};
866
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000867struct io_tctx_node {
868 struct list_head ctx_node;
869 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000870 struct io_ring_ctx *ctx;
871};
872
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300873struct io_defer_entry {
874 struct list_head list;
875 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300876 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300877};
878
Jens Axboed3656342019-12-18 09:50:26 -0700879struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700880 /* needs req->file assigned */
881 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700882 /* hash wq insertion if file is a regular file */
883 unsigned hash_reg_file : 1;
884 /* unbound wq insertion if file is a non-regular file */
885 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700886 /* opcode is not supported by this kernel */
887 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700888 /* set if opcode supports polled "wait" */
889 unsigned pollin : 1;
890 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700891 /* op supports buffer selection */
892 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000893 /* do prep async if is going to be punted */
894 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600895 /* should block plug */
896 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700897 /* size of async data needed, if any */
898 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700899};
900
Jens Axboe09186822020-10-13 15:01:40 -0600901static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300902 [IORING_OP_NOP] = {},
903 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700904 .needs_file = 1,
905 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700906 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700907 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000908 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600909 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700910 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700911 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300912 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700913 .needs_file = 1,
914 .hash_reg_file = 1,
915 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700916 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000917 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600918 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700919 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700922 .needs_file = 1,
923 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300924 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700925 .needs_file = 1,
926 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700927 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600928 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700929 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700930 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300931 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700932 .needs_file = 1,
933 .hash_reg_file = 1,
934 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700935 .pollout = 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 Axboed3656342019-12-18 09:50:26 -0700938 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300939 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700940 .needs_file = 1,
941 .unbound_nonreg_file = 1,
942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_POLL_REMOVE] = {},
944 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700945 .needs_file = 1,
946 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300947 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700948 .needs_file = 1,
949 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700950 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000951 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700952 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700953 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700955 .needs_file = 1,
956 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700957 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700958 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000959 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700960 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700961 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300962 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700963 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700964 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000965 [IORING_OP_TIMEOUT_REMOVE] = {
966 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000967 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300968 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700969 .needs_file = 1,
970 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700971 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700972 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300973 [IORING_OP_ASYNC_CANCEL] = {},
974 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700975 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700976 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300977 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700978 .needs_file = 1,
979 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700980 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000981 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700982 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700983 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300984 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700985 .needs_file = 1,
986 },
Jens Axboe44526be2021-02-15 13:32:18 -0700987 [IORING_OP_OPENAT] = {},
988 [IORING_OP_CLOSE] = {},
989 [IORING_OP_FILES_UPDATE] = {},
990 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300991 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700992 .needs_file = 1,
993 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700994 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700995 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600996 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700997 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700998 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300999 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001000 .needs_file = 1,
1001 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001002 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001003 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001004 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001005 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001006 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001007 .needs_file = 1,
1008 },
Jens Axboe44526be2021-02-15 13:32:18 -07001009 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001010 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001011 .needs_file = 1,
1012 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001013 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001014 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001015 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001016 .needs_file = 1,
1017 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001018 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001019 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001020 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001021 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001022 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001023 [IORING_OP_EPOLL_CTL] = {
1024 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001025 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001026 [IORING_OP_SPLICE] = {
1027 .needs_file = 1,
1028 .hash_reg_file = 1,
1029 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001030 },
1031 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001032 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001033 [IORING_OP_TEE] = {
1034 .needs_file = 1,
1035 .hash_reg_file = 1,
1036 .unbound_nonreg_file = 1,
1037 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001038 [IORING_OP_SHUTDOWN] = {
1039 .needs_file = 1,
1040 },
Jens Axboe44526be2021-02-15 13:32:18 -07001041 [IORING_OP_RENAMEAT] = {},
1042 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001043 [IORING_OP_MKDIRAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001044};
1045
Pavel Begunkov0756a862021-08-15 10:40:25 +01001046/* requests with any of those set should undergo io_disarm_next() */
1047#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1048
Pavel Begunkov7a612352021-03-09 00:37:59 +00001049static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001050static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001051static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1052 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001053 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001054static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001055
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001056static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1057 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001058static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001059static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001060static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001061static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001062static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001063 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001064 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001065static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001066static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001067 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001068static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001069static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001070
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001071static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01001072static void io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001073static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001074
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075static struct kmem_cache *req_cachep;
1076
Jens Axboe09186822020-10-13 15:01:40 -06001077static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001078
1079struct sock *io_uring_get_socket(struct file *file)
1080{
1081#if defined(CONFIG_UNIX)
1082 if (file->f_op == &io_uring_fops) {
1083 struct io_ring_ctx *ctx = file->private_data;
1084
1085 return ctx->ring_sock->sk;
1086 }
1087#endif
1088 return NULL;
1089}
1090EXPORT_SYMBOL(io_uring_get_socket);
1091
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001092#define io_for_each_link(pos, head) \
1093 for (pos = (head); pos; pos = pos->link)
1094
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001095/*
1096 * Shamelessly stolen from the mm implementation of page reference checking,
1097 * see commit f958d7b528b1 for details.
1098 */
1099#define req_ref_zero_or_close_to_overflow(req) \
1100 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1101
1102static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1103{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001104 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001105 return atomic_inc_not_zero(&req->refs);
1106}
1107
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001108static inline bool req_ref_put_and_test(struct io_kiocb *req)
1109{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001110 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1111 return true;
1112
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001113 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1114 return atomic_dec_and_test(&req->refs);
1115}
1116
1117static inline void req_ref_put(struct io_kiocb *req)
1118{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001119 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001120 WARN_ON_ONCE(req_ref_put_and_test(req));
1121}
1122
1123static inline void req_ref_get(struct io_kiocb *req)
1124{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001125 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001126 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1127 atomic_inc(&req->refs);
1128}
1129
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001130static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001131{
1132 if (!(req->flags & REQ_F_REFCOUNT)) {
1133 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001134 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001135 }
1136}
1137
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001138static inline void io_req_set_refcount(struct io_kiocb *req)
1139{
1140 __io_req_set_refcount(req, 1);
1141}
1142
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001143static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001144{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001145 struct io_ring_ctx *ctx = req->ctx;
1146
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001147 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001148 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001149 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001150 }
1151}
1152
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001153static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1154{
1155 bool got = percpu_ref_tryget(ref);
1156
1157 /* already at zero, wait for ->release() */
1158 if (!got)
1159 wait_for_completion(compl);
1160 percpu_ref_resurrect(ref);
1161 if (got)
1162 percpu_ref_put(ref);
1163}
1164
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001165static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1166 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001167{
1168 struct io_kiocb *req;
1169
Pavel Begunkov68207682021-03-22 01:58:25 +00001170 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001171 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001172 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001173 return true;
1174
1175 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001176 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001177 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001178 }
1179 return false;
1180}
1181
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001182static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001183{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001184 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001185}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001186
Jens Axboe2b188cc2019-01-07 10:46:33 -07001187static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1188{
1189 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1190
Jens Axboe0f158b42020-05-14 17:18:39 -06001191 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001192}
1193
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001194static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1195{
1196 return !req->timeout.off;
1197}
1198
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001199static void io_fallback_req_func(struct work_struct *work)
1200{
1201 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1202 fallback_work.work);
1203 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1204 struct io_kiocb *req, *tmp;
1205
1206 percpu_ref_get(&ctx->refs);
1207 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
1208 req->io_task_work.func(req);
1209 percpu_ref_put(&ctx->refs);
1210}
1211
Jens Axboe2b188cc2019-01-07 10:46:33 -07001212static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1213{
1214 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001215 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216
1217 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1218 if (!ctx)
1219 return NULL;
1220
Jens Axboe78076bb2019-12-04 19:56:40 -07001221 /*
1222 * Use 5 bits less than the max cq entries, that should give us around
1223 * 32 entries per hash list if totally full and uniformly spread.
1224 */
1225 hash_bits = ilog2(p->cq_entries);
1226 hash_bits -= 5;
1227 if (hash_bits <= 0)
1228 hash_bits = 1;
1229 ctx->cancel_hash_bits = hash_bits;
1230 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1231 GFP_KERNEL);
1232 if (!ctx->cancel_hash)
1233 goto err;
1234 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1235
Pavel Begunkov62248432021-04-28 13:11:29 +01001236 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1237 if (!ctx->dummy_ubuf)
1238 goto err;
1239 /* set invalid range, so io_import_fixed() fails meeting it */
1240 ctx->dummy_ubuf->ubuf = -1UL;
1241
Roman Gushchin21482892019-05-07 10:01:48 -07001242 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001243 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1244 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001245
1246 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001247 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001248 INIT_LIST_HEAD(&ctx->sqd_list);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001249 init_waitqueue_head(&ctx->poll_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001250 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001251 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001252 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001253 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001255 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001257 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001258 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001259 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001260 INIT_LIST_HEAD(&ctx->timeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001261 spin_lock_init(&ctx->rsrc_ref_lock);
1262 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001263 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1264 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001265 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001266 INIT_LIST_HEAD(&ctx->submit_state.free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001267 INIT_LIST_HEAD(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001268 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001270err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001271 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001272 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001273 kfree(ctx);
1274 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001275}
1276
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001277static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1278{
1279 struct io_rings *r = ctx->rings;
1280
1281 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1282 ctx->cq_extra--;
1283}
1284
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001285static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001286{
Jens Axboe2bc99302020-07-09 09:43:27 -06001287 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1288 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001289
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001290 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001291 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001292
Bob Liu9d858b22019-11-13 18:06:25 +08001293 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001294}
1295
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001296#define FFS_ASYNC_READ 0x1UL
1297#define FFS_ASYNC_WRITE 0x2UL
1298#ifdef CONFIG_64BIT
1299#define FFS_ISREG 0x4UL
1300#else
1301#define FFS_ISREG 0x0UL
1302#endif
1303#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1304
1305static inline bool io_req_ffs_set(struct io_kiocb *req)
1306{
1307 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1308}
1309
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001310static void io_req_track_inflight(struct io_kiocb *req)
1311{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001312 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001313 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001314 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001315 }
1316}
1317
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001318static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1319{
1320 req->flags &= ~REQ_F_LINK_TIMEOUT;
1321}
1322
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001323static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1324{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001325 if (WARN_ON_ONCE(!req->link))
1326 return NULL;
1327
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001328 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1329 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001330
1331 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001332 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001333 __io_req_set_refcount(req->link, 2);
1334 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001335}
1336
1337static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1338{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001339 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001340 return NULL;
1341 return __io_prep_linked_timeout(req);
1342}
1343
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001344static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001345{
Jens Axboed3656342019-12-18 09:50:26 -07001346 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001347 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001348
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001349 if (!(req->flags & REQ_F_CREDS)) {
1350 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001351 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001352 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001353
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001354 req->work.list.next = NULL;
1355 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001356 if (req->flags & REQ_F_FORCE_ASYNC)
1357 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1358
Jens Axboed3656342019-12-18 09:50:26 -07001359 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001360 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001361 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001362 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001363 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001364 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001365 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001366
1367 switch (req->opcode) {
1368 case IORING_OP_SPLICE:
1369 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001370 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1371 req->work.flags |= IO_WQ_WORK_UNBOUND;
1372 break;
1373 }
Jens Axboe561fb042019-10-24 07:25:42 -06001374}
1375
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001376static void io_prep_async_link(struct io_kiocb *req)
1377{
1378 struct io_kiocb *cur;
1379
Pavel Begunkov44eff402021-07-26 14:14:31 +01001380 if (req->flags & REQ_F_LINK_TIMEOUT) {
1381 struct io_ring_ctx *ctx = req->ctx;
1382
Jens Axboe79ebeae2021-08-10 15:18:27 -06001383 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001384 io_for_each_link(cur, req)
1385 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001386 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001387 } else {
1388 io_for_each_link(cur, req)
1389 io_prep_async_work(cur);
1390 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001391}
1392
Pavel Begunkovebf93662021-03-01 18:20:47 +00001393static void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001394{
Jackie Liua197f662019-11-08 08:09:12 -07001395 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001396 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001397 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001398
Jens Axboe3bfe6102021-02-16 14:15:30 -07001399 BUG_ON(!tctx);
1400 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001401
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001402 /* init ->work of the whole link before punting */
1403 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001404
1405 /*
1406 * Not expected to happen, but if we do have a bug where this _can_
1407 * happen, catch it here and ensure the request is marked as
1408 * canceled. That will make io-wq go through the usual work cancel
1409 * procedure rather than attempt to run this request (or create a new
1410 * worker for it).
1411 */
1412 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1413 req->work.flags |= IO_WQ_WORK_CANCEL;
1414
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001415 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1416 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001417 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001418 if (link)
1419 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001420}
1421
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001422static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001423 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001424 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001425{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001426 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001427
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001428 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001429 atomic_set(&req->ctx->cq_timeouts,
1430 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001431 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001432 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001433 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001434 }
1435}
1436
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001437static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001438{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001439 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001440 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1441 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001442
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001443 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001444 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001445 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001446 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001447 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001448 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001449}
1450
Pavel Begunkov360428f2020-05-30 14:54:17 +03001451static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001452 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001453{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001454 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001455
Jens Axboe79ebeae2021-08-10 15:18:27 -06001456 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001457 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001458 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001459 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001460 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001461
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001462 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001463 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001464
1465 /*
1466 * Since seq can easily wrap around over time, subtract
1467 * the last seq at which timeouts were flushed before comparing.
1468 * Assuming not more than 2^31-1 events have happened since,
1469 * these subtractions won't have wrapped, so we can check if
1470 * target is in [last_seq, current_seq] by comparing the two.
1471 */
1472 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1473 events_got = seq - ctx->cq_last_tm_flush;
1474 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001475 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001476
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001477 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001478 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001479 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001480 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001481 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001482}
1483
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001484static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001485{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001486 if (ctx->off_timeout_used)
1487 io_flush_timeouts(ctx);
1488 if (ctx->drain_active)
1489 io_queue_deferred(ctx);
1490}
1491
1492static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1493{
1494 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1495 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001496 /* order cqe stores with ring update */
1497 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001498}
1499
Jens Axboe90554202020-09-03 12:12:41 -06001500static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1501{
1502 struct io_rings *r = ctx->rings;
1503
Pavel Begunkova566c552021-05-16 22:58:08 +01001504 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001505}
1506
Pavel Begunkov888aae22021-01-19 13:32:39 +00001507static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1508{
1509 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1510}
1511
Pavel Begunkovd068b502021-05-16 22:58:11 +01001512static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001513{
Hristo Venev75b28af2019-08-26 17:23:46 +00001514 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001515 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001516
Stefan Bühler115e12e2019-04-24 23:54:18 +02001517 /*
1518 * writes to the cq entry need to come after reading head; the
1519 * control dependency is enough as we're using WRITE_ONCE to
1520 * fill the cq entry
1521 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001522 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001523 return NULL;
1524
Pavel Begunkov888aae22021-01-19 13:32:39 +00001525 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001526 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001527}
1528
Jens Axboef2842ab2020-01-08 11:04:00 -07001529static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1530{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001531 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001532 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001533 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1534 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001535 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001536}
1537
Jens Axboe2c5d7632021-08-21 07:21:19 -06001538/*
1539 * This should only get called when at least one event has been posted.
1540 * Some applications rely on the eventfd notification count only changing
1541 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1542 * 1:1 relationship between how many times this function is called (and
1543 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1544 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001545static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001546{
Jens Axboe5fd46172021-08-06 14:04:31 -06001547 /*
1548 * wake_up_all() may seem excessive, but io_wake_function() and
1549 * io_should_wake() handle the termination of the loop and only
1550 * wake as many waiters as we need to.
1551 */
1552 if (wq_has_sleeper(&ctx->cq_wait))
1553 wake_up_all(&ctx->cq_wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001554 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1555 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001556 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001557 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001558 if (waitqueue_active(&ctx->poll_wait)) {
1559 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001560 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1561 }
Jens Axboe8c838782019-03-12 15:48:16 -06001562}
1563
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001564static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1565{
1566 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe5fd46172021-08-06 14:04:31 -06001567 if (wq_has_sleeper(&ctx->cq_wait))
1568 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001569 }
1570 if (io_should_trigger_evfd(ctx))
1571 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001572 if (waitqueue_active(&ctx->poll_wait)) {
1573 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001574 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1575 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001576}
1577
Jens Axboec4a2ed72019-11-21 21:01:26 -07001578/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001579static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001580{
Jens Axboeb18032b2021-01-24 16:58:56 -07001581 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001582
Pavel Begunkova566c552021-05-16 22:58:08 +01001583 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001584 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001585
Jens Axboeb18032b2021-01-24 16:58:56 -07001586 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001587 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001588 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001589 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001590 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001591
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001592 if (!cqe && !force)
1593 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001594 ocqe = list_first_entry(&ctx->cq_overflow_list,
1595 struct io_overflow_cqe, list);
1596 if (cqe)
1597 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1598 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001599 io_account_cq_overflow(ctx);
1600
Jens Axboeb18032b2021-01-24 16:58:56 -07001601 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001602 list_del(&ocqe->list);
1603 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001604 }
1605
Pavel Begunkov09e88402020-12-17 00:24:38 +00001606 all_flushed = list_empty(&ctx->cq_overflow_list);
1607 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001608 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001609 WRITE_ONCE(ctx->rings->sq_flags,
1610 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001611 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001612
Jens Axboeb18032b2021-01-24 16:58:56 -07001613 if (posted)
1614 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001615 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001616 if (posted)
1617 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001618 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001619}
1620
Pavel Begunkov90f67362021-08-09 20:18:12 +01001621static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001622{
Jens Axboeca0a2652021-03-04 17:15:48 -07001623 bool ret = true;
1624
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001625 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001626 /* iopoll syncs against uring_lock, not completion_lock */
1627 if (ctx->flags & IORING_SETUP_IOPOLL)
1628 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001629 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001630 if (ctx->flags & IORING_SETUP_IOPOLL)
1631 mutex_unlock(&ctx->uring_lock);
1632 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001633
1634 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001635}
1636
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001637/* must to be called somewhat shortly after putting a request */
1638static inline void io_put_task(struct task_struct *task, int nr)
1639{
1640 struct io_uring_task *tctx = task->io_uring;
1641
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001642 if (likely(task == current)) {
1643 tctx->cached_refs += nr;
1644 } else {
1645 percpu_counter_sub(&tctx->inflight, nr);
1646 if (unlikely(atomic_read(&tctx->in_idle)))
1647 wake_up(&tctx->wait);
1648 put_task_struct_many(task, nr);
1649 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001650}
1651
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001652static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1653 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001654{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001655 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001656
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001657 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1658 if (!ocqe) {
1659 /*
1660 * If we're in ring overflow flush mode, or in task cancel mode,
1661 * or cannot allocate an overflow entry, then we need to drop it
1662 * on the floor.
1663 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001664 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001665 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001666 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001667 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001668 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001669 WRITE_ONCE(ctx->rings->sq_flags,
1670 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1671
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001672 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001673 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001674 ocqe->cqe.res = res;
1675 ocqe->cqe.flags = cflags;
1676 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1677 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678}
1679
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001680static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1681 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001682{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001683 struct io_uring_cqe *cqe;
1684
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001685 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686
1687 /*
1688 * If we can't get a cq entry, userspace overflowed the
1689 * submission (by quite a lot). Increment the overflow count in
1690 * the ring.
1691 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001692 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001694 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001695 WRITE_ONCE(cqe->res, res);
1696 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001697 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001698 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001699 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001700}
1701
Pavel Begunkov8d133262021-04-11 01:46:33 +01001702/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001703static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1704 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001705{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001706 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001707}
1708
Pavel Begunkov7a612352021-03-09 00:37:59 +00001709static void io_req_complete_post(struct io_kiocb *req, long res,
1710 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001711{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001712 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713
Jens Axboe79ebeae2021-08-10 15:18:27 -06001714 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001715 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001716 /*
1717 * If we're the last reference to this request, add to our locked
1718 * free_list cache.
1719 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001720 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001721 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001722 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001723 io_disarm_next(req);
1724 if (req->link) {
1725 io_req_task_queue(req->link);
1726 req->link = NULL;
1727 }
1728 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001729 io_dismantle_req(req);
1730 io_put_task(req->task, 1);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001731 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001732 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001733 } else {
1734 if (!percpu_ref_tryget(&ctx->refs))
1735 req = NULL;
1736 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001737 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001738 spin_unlock(&ctx->completion_lock);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001739
Pavel Begunkov180f8292021-03-14 20:57:09 +00001740 if (req) {
1741 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001742 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001743 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744}
1745
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001746static inline bool io_req_needs_clean(struct io_kiocb *req)
1747{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001748 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001749}
1750
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001751static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001752 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001753{
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001754 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001755 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001756 req->result = res;
1757 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001758 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001759}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001760
Pavel Begunkov889fca72021-02-10 00:03:09 +00001761static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1762 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001763{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001764 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1765 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001766 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001767 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001768}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001769
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001770static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001771{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001772 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001773}
1774
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001775static void io_req_complete_failed(struct io_kiocb *req, long res)
1776{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001777 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001778 io_req_complete_post(req, res, 0);
1779}
1780
Pavel Begunkov864ea922021-08-09 13:04:08 +01001781/*
1782 * Don't initialise the fields below on every allocation, but do that in
1783 * advance and keep them valid across allocations.
1784 */
1785static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1786{
1787 req->ctx = ctx;
1788 req->link = NULL;
1789 req->async_data = NULL;
1790 /* not necessary, but safer to zero */
1791 req->result = 0;
1792}
1793
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001794static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001795 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001796{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001797 spin_lock(&ctx->completion_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001798 list_splice_init(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001799 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001800 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001801}
1802
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001803/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001804static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001805{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001806 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001807 int nr;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001808
Jens Axboec7dae4b2021-02-09 19:53:37 -07001809 /*
1810 * If we have more than a batch's worth of requests in our IRQ side
1811 * locked cache, grab the lock and move them over to our submission
1812 * side cache.
1813 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001814 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001815 io_flush_cached_locked_reqs(ctx, state);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001816
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001817 nr = state->free_reqs;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001818 while (!list_empty(&state->free_list)) {
1819 struct io_kiocb *req = list_first_entry(&state->free_list,
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001820 struct io_kiocb, inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001821
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001822 list_del(&req->inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001823 state->reqs[nr++] = req;
1824 if (nr == ARRAY_SIZE(state->reqs))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001825 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001826 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001827
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001828 state->free_reqs = nr;
1829 return nr != 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830}
1831
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001832/*
1833 * A request might get retired back into the request caches even before opcode
1834 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1835 * Because of that, io_alloc_req() should be called only under ->uring_lock
1836 * and with extra caution to not get a request that is still worked on.
1837 */
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001838static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001839 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001840{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001841 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001842 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1843 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001844
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01001845 BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001846
Pavel Begunkov864ea922021-08-09 13:04:08 +01001847 if (likely(state->free_reqs || io_flush_cached_reqs(ctx)))
1848 goto got_req;
Jens Axboe2579f912019-01-09 09:10:43 -07001849
Pavel Begunkov864ea922021-08-09 13:04:08 +01001850 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1851 state->reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001852
Pavel Begunkov864ea922021-08-09 13:04:08 +01001853 /*
1854 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1855 * retry single alloc to be on the safe side.
1856 */
1857 if (unlikely(ret <= 0)) {
1858 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1859 if (!state->reqs[0])
1860 return NULL;
1861 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001862 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001863
1864 for (i = 0; i < ret; i++)
1865 io_preinit_req(state->reqs[i], ctx);
1866 state->free_reqs = ret;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001867got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001868 state->free_reqs--;
1869 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001870}
1871
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001872static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001873{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001874 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001875 fput(file);
1876}
1877
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001878static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001879{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001880 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001881
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001882 if (io_req_needs_clean(req))
1883 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001884 if (!(flags & REQ_F_FIXED_FILE))
1885 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001886 if (req->fixed_rsrc_refs)
1887 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001888 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00001889 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001890 req->async_data = NULL;
1891 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001892}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001893
Pavel Begunkov216578e2020-10-13 09:44:00 +01001894static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001895{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001896 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001897
Pavel Begunkov216578e2020-10-13 09:44:00 +01001898 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001899 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001900
Jens Axboe79ebeae2021-08-10 15:18:27 -06001901 spin_lock(&ctx->completion_lock);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001902 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001903 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001904 spin_unlock(&ctx->completion_lock);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001905
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001906 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001907}
1908
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001909static inline void io_remove_next_linked(struct io_kiocb *req)
1910{
1911 struct io_kiocb *nxt = req->link;
1912
1913 req->link = nxt->link;
1914 nxt->link = NULL;
1915}
1916
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001917static bool io_kill_linked_timeout(struct io_kiocb *req)
1918 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06001919 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001920{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001921 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001922
Pavel Begunkovb97e7362021-08-15 10:40:23 +01001923 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001924 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001925
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001926 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001927 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001928 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001929 io_cqring_fill_event(link->ctx, link->user_data,
1930 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001931 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001932 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001933 }
1934 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001935 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001936}
1937
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001938static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001939 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001940{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001941 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06001942
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001943 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001944 while (link) {
1945 nxt = link->link;
1946 link->link = NULL;
1947
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001948 trace_io_uring_fail_link(req, link);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001949 io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001950 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001951 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001952 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001953}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001954
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001955static bool io_disarm_next(struct io_kiocb *req)
1956 __must_hold(&req->ctx->completion_lock)
1957{
1958 bool posted = false;
1959
Pavel Begunkov0756a862021-08-15 10:40:25 +01001960 if (req->flags & REQ_F_ARM_LTIMEOUT) {
1961 struct io_kiocb *link = req->link;
1962
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001963 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01001964 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
1965 io_remove_next_linked(req);
1966 io_cqring_fill_event(link->ctx, link->user_data,
1967 -ECANCELED, 0);
1968 io_put_req_deferred(link);
1969 posted = true;
1970 }
1971 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06001972 struct io_ring_ctx *ctx = req->ctx;
1973
1974 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001975 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06001976 spin_unlock_irq(&ctx->timeout_lock);
1977 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001978 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01001979 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001980 posted |= (req->link != NULL);
1981 io_fail_links(req);
1982 }
1983 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06001984}
1985
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001986static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001987{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001988 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07001989
Jens Axboe9e645e112019-05-10 16:07:28 -06001990 /*
1991 * If LINK is set, we have dependent requests in this chain. If we
1992 * didn't fail this request, queue the first one up, moving any other
1993 * dependencies to the next request. In case of failure, fail the rest
1994 * of the chain.
1995 */
Pavel Begunkov0756a862021-08-15 10:40:25 +01001996 if (req->flags & IO_DISARM_MASK) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001997 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001998 bool posted;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001999
Jens Axboe79ebeae2021-08-10 15:18:27 -06002000 spin_lock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002001 posted = io_disarm_next(req);
2002 if (posted)
2003 io_commit_cqring(req->ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002004 spin_unlock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002005 if (posted)
2006 io_cqring_ev_posted(ctx);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002007 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002008 nxt = req->link;
2009 req->link = NULL;
2010 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002011}
Jens Axboe2665abf2019-11-05 12:40:47 -07002012
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002013static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002014{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002015 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002016 return NULL;
2017 return __io_req_find_next(req);
2018}
2019
Pavel Begunkov2c323952021-02-28 22:04:53 +00002020static void ctx_flush_and_put(struct io_ring_ctx *ctx)
2021{
2022 if (!ctx)
2023 return;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002024 if (ctx->submit_state.compl_nr) {
Pavel Begunkov2c323952021-02-28 22:04:53 +00002025 mutex_lock(&ctx->uring_lock);
Hao Xu99c8bc52021-08-21 06:19:54 +08002026 if (ctx->submit_state.compl_nr)
2027 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002028 mutex_unlock(&ctx->uring_lock);
2029 }
2030 percpu_ref_put(&ctx->refs);
2031}
2032
Jens Axboe7cbf1722021-02-10 00:03:20 +00002033static void tctx_task_work(struct callback_head *cb)
2034{
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002035 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002036 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2037 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002038
Pavel Begunkov16f72072021-06-17 18:14:09 +01002039 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002040 struct io_wq_work_node *node;
2041
2042 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002043 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002044 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002045 if (!node)
2046 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002047 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002048 if (!node)
2049 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002050
Pavel Begunkov6294f362021-08-10 17:53:55 +01002051 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002052 struct io_wq_work_node *next = node->next;
2053 struct io_kiocb *req = container_of(node, struct io_kiocb,
2054 io_task_work.node);
2055
2056 if (req->ctx != ctx) {
2057 ctx_flush_and_put(ctx);
2058 ctx = req->ctx;
2059 percpu_ref_get(&ctx->refs);
2060 }
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002061 req->io_task_work.func(req);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002062 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002063 } while (node);
2064
Jens Axboe7cbf1722021-02-10 00:03:20 +00002065 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002066 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002067
2068 ctx_flush_and_put(ctx);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002069}
2070
Pavel Begunkove09ee512021-07-01 13:26:05 +01002071static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002072{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002073 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002074 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002075 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002076 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002077 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002078 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002079
2080 WARN_ON_ONCE(!tctx);
2081
Jens Axboe0b81e802021-02-16 10:33:53 -07002082 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002083 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002084 running = tctx->task_running;
2085 if (!running)
2086 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002087 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002088
2089 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002090 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002091 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002092
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002093 /*
2094 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2095 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2096 * processing task_work. There's no reliable way to tell if TWA_RESUME
2097 * will do the job.
2098 */
2099 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002100 if (!task_work_add(tsk, &tctx->task_work, notify)) {
2101 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002102 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002103 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002104
Pavel Begunkove09ee512021-07-01 13:26:05 +01002105 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002106 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002107 node = tctx->task_list.first;
2108 INIT_WQ_LIST(&tctx->task_list);
2109 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002110
Pavel Begunkove09ee512021-07-01 13:26:05 +01002111 while (node) {
2112 req = container_of(node, struct io_kiocb, io_task_work.node);
2113 node = node->next;
2114 if (llist_add(&req->io_task_work.fallback_node,
2115 &req->ctx->fallback_llist))
2116 schedule_delayed_work(&req->ctx->fallback_work, 1);
2117 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002118}
2119
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002120static void io_req_task_cancel(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06002121{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002122 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002123
Pavel Begunkove83acd72021-02-28 22:35:09 +00002124 /* ctx is guaranteed to stay alive while we hold uring_lock */
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002125 mutex_lock(&ctx->uring_lock);
Pavel Begunkov25935532021-03-19 17:22:40 +00002126 io_req_complete_failed(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002127 mutex_unlock(&ctx->uring_lock);
Jens Axboec40f6372020-06-25 15:39:59 -06002128}
2129
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002130static void io_req_task_submit(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06002131{
2132 struct io_ring_ctx *ctx = req->ctx;
2133
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002134 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002135 mutex_lock(&ctx->uring_lock);
Jens Axboe316319e2021-08-19 09:41:42 -06002136 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002137 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002138 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002139 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002140 io_req_complete_failed(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002141 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002142}
2143
Pavel Begunkova3df76982021-02-18 22:32:52 +00002144static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2145{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002146 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002147 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002148 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002149}
2150
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002151static void io_req_task_queue(struct io_kiocb *req)
2152{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002153 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002154 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002155}
2156
Jens Axboe773af692021-07-27 10:25:55 -06002157static void io_req_task_queue_reissue(struct io_kiocb *req)
2158{
2159 req->io_task_work.func = io_queue_async_work;
2160 io_req_task_work_add(req);
2161}
2162
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002163static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002164{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002165 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002166
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002167 if (nxt)
2168 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002169}
2170
Jens Axboe9e645e112019-05-10 16:07:28 -06002171static void io_free_req(struct io_kiocb *req)
2172{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002173 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002174 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002175}
2176
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002177struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002178 struct task_struct *task;
2179 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002180 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002181};
2182
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002183static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002184{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002185 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002186 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002187 rb->task = NULL;
2188}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002189
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002190static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2191 struct req_batch *rb)
2192{
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002193 if (rb->ctx_refs)
2194 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkove98e49b2021-08-18 17:01:43 +01002195 if (rb->task)
Pavel Begunkove9dbe222021-08-09 13:04:20 +01002196 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002197}
2198
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002199static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2200 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002201{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002202 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002203 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002204
Jens Axboee3bc8e92020-09-24 08:45:57 -06002205 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002206 if (rb->task)
2207 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002208 rb->task = req->task;
2209 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002210 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002211 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002212 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002213
Pavel Begunkovbd759042021-02-12 03:23:50 +00002214 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002215 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002216 else
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002217 list_add(&req->inflight_entry, &state->free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002218}
2219
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01002220static void io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002221 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002222{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002223 struct io_submit_state *state = &ctx->submit_state;
2224 int i, nr = state->compl_nr;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002225 struct req_batch rb;
2226
Jens Axboe79ebeae2021-08-10 15:18:27 -06002227 spin_lock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002228 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002229 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002230
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002231 __io_cqring_fill_event(ctx, req->user_data, req->result,
2232 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002233 }
2234 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002235 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002236 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002237
2238 io_init_req_batch(&rb);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002239 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002240 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov905c1722021-02-10 00:03:14 +00002241
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002242 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002243 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002244 }
2245
2246 io_req_free_batch_finish(ctx, &rb);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002247 state->compl_nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002248}
2249
Jens Axboeba816ad2019-09-28 11:36:45 -06002250/*
2251 * Drop reference to request, return next in chain (if there is one) if this
2252 * was the last reference to this request.
2253 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002254static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002255{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002256 struct io_kiocb *nxt = NULL;
2257
Jens Axboede9b4cc2021-02-24 13:28:27 -07002258 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002259 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002260 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002261 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002262 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002263}
2264
Pavel Begunkov0d850352021-03-19 17:22:37 +00002265static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002266{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002267 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002268 io_free_req(req);
2269}
2270
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002271static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002272{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002273 if (req_ref_put_and_test(req)) {
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002274 req->io_task_work.func = io_free_req;
2275 io_req_task_work_add(req);
2276 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002277}
2278
Pavel Begunkov6c503152021-01-04 20:36:36 +00002279static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002280{
2281 /* See comment at the top of this file */
2282 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002283 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002284}
2285
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002286static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2287{
2288 struct io_rings *rings = ctx->rings;
2289
2290 /* make sure SQ entry isn't read before tail */
2291 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2292}
2293
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002294static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002295{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002296 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002297
Jens Axboebcda7ba2020-02-23 16:42:51 -07002298 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2299 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002300 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002301 kfree(kbuf);
2302 return cflags;
2303}
2304
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002305static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2306{
2307 struct io_buffer *kbuf;
2308
Pavel Begunkovae421d92021-08-17 20:28:08 +01002309 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2310 return 0;
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002311 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2312 return io_put_kbuf(req, kbuf);
2313}
2314
Jens Axboe4c6e2772020-07-01 11:29:10 -06002315static inline bool io_run_task_work(void)
2316{
Nadav Amitef98eb02021-08-07 17:13:41 -07002317 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002318 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002319 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002320 return true;
2321 }
2322
2323 return false;
2324}
2325
Jens Axboedef596e2019-01-09 08:59:42 -07002326/*
2327 * Find and free completed poll iocbs
2328 */
2329static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002330 struct list_head *done)
Jens Axboedef596e2019-01-09 08:59:42 -07002331{
Jens Axboe8237e042019-12-28 10:48:22 -07002332 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002333 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002334
2335 /* order with ->result store in io_complete_rw_iopoll() */
2336 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002337
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002338 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002339 while (!list_empty(done)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002340 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002341 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002342
Pavel Begunkova8576af2021-08-15 10:40:21 +01002343 if (READ_ONCE(req->result) == -EAGAIN &&
Pavel Begunkov8c130822021-03-22 01:58:32 +00002344 !(req->flags & REQ_F_DONT_REISSUE)) {
Pavel Begunkovf1613402021-02-11 18:28:21 +00002345 req->iopoll_completed = 0;
Jens Axboe773af692021-07-27 10:25:55 -06002346 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002347 continue;
Pavel Begunkovf1613402021-02-11 18:28:21 +00002348 }
2349
Pavel Begunkovae421d92021-08-17 20:28:08 +01002350 __io_cqring_fill_event(ctx, req->user_data, req->result,
2351 io_put_rw_kbuf(req));
Jens Axboedef596e2019-01-09 08:59:42 -07002352 (*nr_events)++;
2353
Jens Axboede9b4cc2021-02-24 13:28:27 -07002354 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002355 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002356 }
Jens Axboedef596e2019-01-09 08:59:42 -07002357
Jens Axboe09bb8392019-03-13 12:39:28 -06002358 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002359 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002360 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002361}
2362
Jens Axboedef596e2019-01-09 08:59:42 -07002363static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002364 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002365{
2366 struct io_kiocb *req, *tmp;
2367 LIST_HEAD(done);
2368 bool spin;
Jens Axboedef596e2019-01-09 08:59:42 -07002369
2370 /*
2371 * Only spin for completions if we don't have multiple devices hanging
2372 * off our complete list, and we're under the requested amount.
2373 */
Hao Xu915b3dd2021-06-28 05:37:30 +08002374 spin = !ctx->poll_multi_queue && *nr_events < min;
Jens Axboedef596e2019-01-09 08:59:42 -07002375
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002376 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002377 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002378 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002379
2380 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002381 * Move completed and retryable entries to our local lists.
2382 * If we find a request that requires polling, break out
2383 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002384 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002385 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002386 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002387 continue;
2388 }
2389 if (!list_empty(&done))
2390 break;
2391
2392 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002393 if (unlikely(ret < 0))
2394 return ret;
2395 else if (ret)
2396 spin = false;
Jens Axboedef596e2019-01-09 08:59:42 -07002397
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002398 /* iopoll may have completed current req */
2399 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002400 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002401 }
2402
2403 if (!list_empty(&done))
Pavel Begunkova8576af2021-08-15 10:40:21 +01002404 io_iopoll_complete(ctx, nr_events, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002405
Pavel Begunkova2416e12021-08-09 13:04:09 +01002406 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002407}
2408
2409/*
Jens Axboedef596e2019-01-09 08:59:42 -07002410 * We can't just wait for polled events to come to us, we have to actively
2411 * find and complete them.
2412 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002413static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002414{
2415 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2416 return;
2417
2418 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002419 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002420 unsigned int nr_events = 0;
2421
Pavel Begunkova8576af2021-08-15 10:40:21 +01002422 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002423
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002424 /* let it sleep and repeat later if can't complete a request */
2425 if (nr_events == 0)
2426 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002427 /*
2428 * Ensure we allow local-to-the-cpu processing to take place,
2429 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002430 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002431 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002432 if (need_resched()) {
2433 mutex_unlock(&ctx->uring_lock);
2434 cond_resched();
2435 mutex_lock(&ctx->uring_lock);
2436 }
Jens Axboedef596e2019-01-09 08:59:42 -07002437 }
2438 mutex_unlock(&ctx->uring_lock);
2439}
2440
Pavel Begunkov7668b922020-07-07 16:36:21 +03002441static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002442{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002443 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002444 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002445
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002446 /*
2447 * We disallow the app entering submit/complete with polling, but we
2448 * still need to lock the ring to prevent racing with polled issue
2449 * that got punted to a workqueue.
2450 */
2451 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002452 /*
2453 * Don't enter poll loop if we already have events pending.
2454 * If we do, we can potentially be spinning for commands that
2455 * already triggered a CQE (eg in error).
2456 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002457 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002458 __io_cqring_overflow_flush(ctx, false);
2459 if (io_cqring_events(ctx))
2460 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002461 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002462 /*
2463 * If a submit got punted to a workqueue, we can have the
2464 * application entering polling for a command before it gets
2465 * issued. That app will hold the uring_lock for the duration
2466 * of the poll right here, so we need to take a breather every
2467 * now and then to ensure that the issue has a chance to add
2468 * the poll to the issued list. Otherwise we can spin here
2469 * forever, while the workqueue is stuck trying to acquire the
2470 * very same mutex.
2471 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002472 if (list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002473 u32 tail = ctx->cached_cq_tail;
2474
Jens Axboe500f9fb2019-08-19 12:15:59 -06002475 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002476 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002477 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002478
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002479 /* some requests don't go through iopoll_list */
2480 if (tail != ctx->cached_cq_tail ||
2481 list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002482 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002483 }
Pavel Begunkova8576af2021-08-15 10:40:21 +01002484 ret = io_do_iopoll(ctx, &nr_events, min);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002485 } while (!ret && nr_events < min && !need_resched());
2486out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002487 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002488 return ret;
2489}
2490
Jens Axboe491381ce2019-10-17 09:20:46 -06002491static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002492{
Jens Axboe491381ce2019-10-17 09:20:46 -06002493 /*
2494 * Tell lockdep we inherited freeze protection from submission
2495 * thread.
2496 */
2497 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002498 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002499
Pavel Begunkov1c986792021-03-22 01:58:31 +00002500 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2501 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002502 }
2503}
2504
Jens Axboeb63534c2020-06-04 11:28:00 -06002505#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002506static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002507{
Pavel Begunkovab454432021-03-22 01:58:33 +00002508 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002509
Pavel Begunkovab454432021-03-22 01:58:33 +00002510 if (!rw)
2511 return !io_req_prep_async(req);
2512 /* may have left rw->iter inconsistent on -EIOCBQUEUED */
2513 iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter));
2514 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002515}
Jens Axboeb63534c2020-06-04 11:28:00 -06002516
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002517static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002518{
Jens Axboe355afae2020-09-02 09:30:31 -06002519 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002520 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002521
Jens Axboe355afae2020-09-02 09:30:31 -06002522 if (!S_ISBLK(mode) && !S_ISREG(mode))
2523 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002524 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2525 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002526 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002527 /*
2528 * If ref is dying, we might be running poll reap from the exit work.
2529 * Don't attempt to reissue from that path, just let it fail with
2530 * -EAGAIN.
2531 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002532 if (percpu_ref_is_dying(&ctx->refs))
2533 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002534 /*
2535 * Play it safe and assume not safe to re-import and reissue if we're
2536 * not in the original thread group (or in task context).
2537 */
2538 if (!same_thread_group(req->task, current) || !in_task())
2539 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002540 return true;
2541}
Jens Axboee82ad482021-04-02 19:45:34 -06002542#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002543static bool io_resubmit_prep(struct io_kiocb *req)
2544{
2545 return false;
2546}
Jens Axboee82ad482021-04-02 19:45:34 -06002547static bool io_rw_should_reissue(struct io_kiocb *req)
2548{
2549 return false;
2550}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002551#endif
2552
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002553static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002554{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002555 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2556 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002557 if (res != req->result) {
2558 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2559 io_rw_should_reissue(req)) {
2560 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002561 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002562 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002563 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002564 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002565 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002566 return false;
2567}
2568
2569static void io_req_task_complete(struct io_kiocb *req)
2570{
Pavel Begunkovae421d92021-08-17 20:28:08 +01002571 __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req));
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002572}
2573
2574static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2575 unsigned int issue_flags)
2576{
2577 if (__io_complete_rw_common(req, res))
2578 return;
2579 io_req_task_complete(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06002580}
2581
2582static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2583{
Jens Axboe9adbd452019-12-20 08:45:55 -07002584 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002585
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002586 if (__io_complete_rw_common(req, res))
2587 return;
2588 req->result = res;
2589 req->io_task_work.func = io_req_task_complete;
2590 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002591}
2592
Jens Axboedef596e2019-01-09 08:59:42 -07002593static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2594{
Jens Axboe9adbd452019-12-20 08:45:55 -07002595 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002596
Jens Axboe491381ce2019-10-17 09:20:46 -06002597 if (kiocb->ki_flags & IOCB_WRITE)
2598 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002599 if (unlikely(res != req->result)) {
Jens Axboea1ff1e32021-04-12 06:40:02 -06002600 if (!(res == -EAGAIN && io_rw_should_reissue(req) &&
2601 io_resubmit_prep(req))) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002602 req_set_fail(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002603 req->flags |= REQ_F_DONT_REISSUE;
2604 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002605 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002606
2607 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002608 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002609 smp_wmb();
2610 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002611}
2612
2613/*
2614 * After the iocb has been issued, it's safe to be found on the poll list.
2615 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002616 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002617 * accessing the kiocb cookie.
2618 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002619static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002620{
2621 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002622 const bool in_async = io_wq_current_is_worker();
2623
2624 /* workqueue context doesn't hold uring_lock, grab it now */
2625 if (unlikely(in_async))
2626 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002627
2628 /*
2629 * Track whether we have multiple files in our lists. This will impact
2630 * how we do polling eventually, not spinning if we're on potentially
2631 * different devices.
2632 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002633 if (list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002634 ctx->poll_multi_queue = false;
2635 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002636 struct io_kiocb *list_req;
Hao Xu915b3dd2021-06-28 05:37:30 +08002637 unsigned int queue_num0, queue_num1;
Jens Axboedef596e2019-01-09 08:59:42 -07002638
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002639 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002640 inflight_entry);
Hao Xu915b3dd2021-06-28 05:37:30 +08002641
2642 if (list_req->file != req->file) {
2643 ctx->poll_multi_queue = true;
2644 } else {
2645 queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie);
2646 queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie);
2647 if (queue_num0 != queue_num1)
2648 ctx->poll_multi_queue = true;
2649 }
Jens Axboedef596e2019-01-09 08:59:42 -07002650 }
2651
2652 /*
2653 * For fast devices, IO may have already completed. If it has, add
2654 * it to the front so we find it first.
2655 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002656 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002657 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002658 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002659 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002660
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002661 if (unlikely(in_async)) {
2662 /*
2663 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2664 * in sq thread task context or in io worker task context. If
2665 * current task context is sq thread, we don't need to check
2666 * whether should wake up sq thread.
2667 */
2668 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2669 wq_has_sleeper(&ctx->sq_data->wait))
2670 wake_up(&ctx->sq_data->wait);
2671
2672 mutex_unlock(&ctx->uring_lock);
2673 }
Jens Axboedef596e2019-01-09 08:59:42 -07002674}
2675
Jens Axboe4503b762020-06-01 10:00:27 -06002676static bool io_bdev_nowait(struct block_device *bdev)
2677{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002678 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002679}
2680
Jens Axboe2b188cc2019-01-07 10:46:33 -07002681/*
2682 * If we tracked the file through the SCM inflight mechanism, we could support
2683 * any file. For now, just ensure that anything potentially problematic is done
2684 * inline.
2685 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002686static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002687{
2688 umode_t mode = file_inode(file)->i_mode;
2689
Jens Axboe4503b762020-06-01 10:00:27 -06002690 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002691 if (IS_ENABLED(CONFIG_BLOCK) &&
2692 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002693 return true;
2694 return false;
2695 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002696 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002697 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002698 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002699 if (IS_ENABLED(CONFIG_BLOCK) &&
2700 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002701 file->f_op != &io_uring_fops)
2702 return true;
2703 return false;
2704 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002705
Jens Axboec5b85622020-06-09 19:23:05 -06002706 /* any ->read/write should understand O_NONBLOCK */
2707 if (file->f_flags & O_NONBLOCK)
2708 return true;
2709
Jens Axboeaf197f52020-04-28 13:15:06 -06002710 if (!(file->f_mode & FMODE_NOWAIT))
2711 return false;
2712
2713 if (rw == READ)
2714 return file->f_op->read_iter != NULL;
2715
2716 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002717}
2718
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002719static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002720{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002721 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002722 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002723 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002724 return true;
2725
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002726 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002727}
2728
Pavel Begunkova88fc402020-09-30 22:57:53 +03002729static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730{
Jens Axboedef596e2019-01-09 08:59:42 -07002731 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002732 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002733 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002734 unsigned ioprio;
2735 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002736
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002737 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002738 req->flags |= REQ_F_ISREG;
2739
Jens Axboe2b188cc2019-01-07 10:46:33 -07002740 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002741 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002742 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002743 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002744 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002745 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002746 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2747 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2748 if (unlikely(ret))
2749 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002750
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002751 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2752 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2753 req->flags |= REQ_F_NOWAIT;
2754
Jens Axboe2b188cc2019-01-07 10:46:33 -07002755 ioprio = READ_ONCE(sqe->ioprio);
2756 if (ioprio) {
2757 ret = ioprio_check_cap(ioprio);
2758 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002759 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002760
2761 kiocb->ki_ioprio = ioprio;
2762 } else
2763 kiocb->ki_ioprio = get_current_ioprio();
2764
Jens Axboedef596e2019-01-09 08:59:42 -07002765 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002766 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2767 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002768 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002769
Jens Axboedef596e2019-01-09 08:59:42 -07002770 kiocb->ki_flags |= IOCB_HIPRI;
2771 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002772 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002773 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002774 if (kiocb->ki_flags & IOCB_HIPRI)
2775 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002776 kiocb->ki_complete = io_complete_rw;
2777 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002778
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002779 if (req->opcode == IORING_OP_READ_FIXED ||
2780 req->opcode == IORING_OP_WRITE_FIXED) {
2781 req->imu = NULL;
2782 io_req_set_rsrc_node(req);
2783 }
2784
Jens Axboe3529d8c2019-12-19 18:24:38 -07002785 req->rw.addr = READ_ONCE(sqe->addr);
2786 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002787 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002788 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002789}
2790
2791static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2792{
2793 switch (ret) {
2794 case -EIOCBQUEUED:
2795 break;
2796 case -ERESTARTSYS:
2797 case -ERESTARTNOINTR:
2798 case -ERESTARTNOHAND:
2799 case -ERESTART_RESTARTBLOCK:
2800 /*
2801 * We can't just restart the syscall, since previously
2802 * submitted sqes may already be in progress. Just fail this
2803 * IO with EINTR.
2804 */
2805 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002806 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002807 default:
2808 kiocb->ki_complete(kiocb, ret, 0);
2809 }
2810}
2811
Jens Axboea1d7c392020-06-22 11:09:46 -06002812static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002813 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002814{
Jens Axboeba042912019-12-25 16:33:42 -07002815 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002816 struct io_async_rw *io = req->async_data;
Pavel Begunkov97284632021-04-08 19:28:03 +01002817 bool check_reissue = kiocb->ki_complete == io_complete_rw;
Jens Axboeba042912019-12-25 16:33:42 -07002818
Jens Axboe227c0c92020-08-13 11:51:40 -06002819 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002820 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002821 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002822 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002823 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002824 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002825 }
2826
Jens Axboeba042912019-12-25 16:33:42 -07002827 if (req->flags & REQ_F_CUR_POS)
2828 req->file->f_pos = kiocb->ki_pos;
Hao Xue149bd742021-06-28 05:48:05 +08002829 if (ret >= 0 && check_reissue)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002830 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002831 else
2832 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002833
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01002834 if (check_reissue && (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002835 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002836 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002837 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002838 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002839 req_set_fail(req);
Pavel Begunkovae421d92021-08-17 20:28:08 +01002840 __io_req_complete(req, issue_flags, ret,
2841 io_put_rw_kbuf(req));
Pavel Begunkov97284632021-04-08 19:28:03 +01002842 }
2843 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002844}
2845
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002846static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2847 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002848{
Jens Axboe9adbd452019-12-20 08:45:55 -07002849 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002850 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002851 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002852
Pavel Begunkov75769e32021-04-01 15:43:54 +01002853 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002854 return -EFAULT;
2855 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002856 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002857 return -EFAULT;
2858
2859 /*
2860 * May not be a start of buffer, set size appropriately
2861 * and advance us to the beginning.
2862 */
2863 offset = buf_addr - imu->ubuf;
2864 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002865
2866 if (offset) {
2867 /*
2868 * Don't use iov_iter_advance() here, as it's really slow for
2869 * using the latter parts of a big fixed buffer - it iterates
2870 * over each segment manually. We can cheat a bit here, because
2871 * we know that:
2872 *
2873 * 1) it's a BVEC iter, we set it up
2874 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2875 * first and last bvec
2876 *
2877 * So just find our index, and adjust the iterator afterwards.
2878 * If the offset is within the first bvec (or the whole first
2879 * bvec, just use iov_iter_advance(). This makes it easier
2880 * since we can just skip the first segment, which may not
2881 * be PAGE_SIZE aligned.
2882 */
2883 const struct bio_vec *bvec = imu->bvec;
2884
2885 if (offset <= bvec->bv_len) {
2886 iov_iter_advance(iter, offset);
2887 } else {
2888 unsigned long seg_skip;
2889
2890 /* skip first vec */
2891 offset -= bvec->bv_len;
2892 seg_skip = 1 + (offset >> PAGE_SHIFT);
2893
2894 iter->bvec = bvec + seg_skip;
2895 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002896 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002897 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002898 }
2899 }
2900
Pavel Begunkov847595d2021-02-04 13:52:06 +00002901 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002902}
2903
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002904static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
2905{
2906 struct io_ring_ctx *ctx = req->ctx;
2907 struct io_mapped_ubuf *imu = req->imu;
2908 u16 index, buf_index = req->buf_index;
2909
2910 if (likely(!imu)) {
2911 if (unlikely(buf_index >= ctx->nr_user_bufs))
2912 return -EFAULT;
2913 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2914 imu = READ_ONCE(ctx->user_bufs[index]);
2915 req->imu = imu;
2916 }
2917 return __io_import_fixed(req, rw, iter, imu);
2918}
2919
Jens Axboebcda7ba2020-02-23 16:42:51 -07002920static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2921{
2922 if (needs_lock)
2923 mutex_unlock(&ctx->uring_lock);
2924}
2925
2926static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2927{
2928 /*
2929 * "Normal" inline submissions always hold the uring_lock, since we
2930 * grab it from the system call. Same is true for the SQPOLL offload.
2931 * The only exception is when we've detached the request and issue it
2932 * from an async worker thread, grab the lock for that case.
2933 */
2934 if (needs_lock)
2935 mutex_lock(&ctx->uring_lock);
2936}
2937
2938static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2939 int bgid, struct io_buffer *kbuf,
2940 bool needs_lock)
2941{
2942 struct io_buffer *head;
2943
2944 if (req->flags & REQ_F_BUFFER_SELECTED)
2945 return kbuf;
2946
2947 io_ring_submit_lock(req->ctx, needs_lock);
2948
2949 lockdep_assert_held(&req->ctx->uring_lock);
2950
Jens Axboe9e15c3a2021-03-13 12:29:43 -07002951 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002952 if (head) {
2953 if (!list_empty(&head->list)) {
2954 kbuf = list_last_entry(&head->list, struct io_buffer,
2955 list);
2956 list_del(&kbuf->list);
2957 } else {
2958 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07002959 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002960 }
2961 if (*len > kbuf->len)
2962 *len = kbuf->len;
2963 } else {
2964 kbuf = ERR_PTR(-ENOBUFS);
2965 }
2966
2967 io_ring_submit_unlock(req->ctx, needs_lock);
2968
2969 return kbuf;
2970}
2971
Jens Axboe4d954c22020-02-27 07:31:19 -07002972static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2973 bool needs_lock)
2974{
2975 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002976 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002977
2978 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002979 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002980 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2981 if (IS_ERR(kbuf))
2982 return kbuf;
2983 req->rw.addr = (u64) (unsigned long) kbuf;
2984 req->flags |= REQ_F_BUFFER_SELECTED;
2985 return u64_to_user_ptr(kbuf->addr);
2986}
2987
2988#ifdef CONFIG_COMPAT
2989static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2990 bool needs_lock)
2991{
2992 struct compat_iovec __user *uiov;
2993 compat_ssize_t clen;
2994 void __user *buf;
2995 ssize_t len;
2996
2997 uiov = u64_to_user_ptr(req->rw.addr);
2998 if (!access_ok(uiov, sizeof(*uiov)))
2999 return -EFAULT;
3000 if (__get_user(clen, &uiov->iov_len))
3001 return -EFAULT;
3002 if (clen < 0)
3003 return -EINVAL;
3004
3005 len = clen;
3006 buf = io_rw_buffer_select(req, &len, needs_lock);
3007 if (IS_ERR(buf))
3008 return PTR_ERR(buf);
3009 iov[0].iov_base = buf;
3010 iov[0].iov_len = (compat_size_t) len;
3011 return 0;
3012}
3013#endif
3014
3015static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3016 bool needs_lock)
3017{
3018 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3019 void __user *buf;
3020 ssize_t len;
3021
3022 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3023 return -EFAULT;
3024
3025 len = iov[0].iov_len;
3026 if (len < 0)
3027 return -EINVAL;
3028 buf = io_rw_buffer_select(req, &len, needs_lock);
3029 if (IS_ERR(buf))
3030 return PTR_ERR(buf);
3031 iov[0].iov_base = buf;
3032 iov[0].iov_len = len;
3033 return 0;
3034}
3035
3036static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3037 bool needs_lock)
3038{
Jens Axboedddb3e22020-06-04 11:27:01 -06003039 if (req->flags & REQ_F_BUFFER_SELECTED) {
3040 struct io_buffer *kbuf;
3041
3042 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3043 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3044 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003045 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003046 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003047 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003048 return -EINVAL;
3049
3050#ifdef CONFIG_COMPAT
3051 if (req->ctx->compat)
3052 return io_compat_import(req, iov, needs_lock);
3053#endif
3054
3055 return __io_iov_buffer_select(req, iov, needs_lock);
3056}
3057
Pavel Begunkov847595d2021-02-04 13:52:06 +00003058static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3059 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003060{
Jens Axboe9adbd452019-12-20 08:45:55 -07003061 void __user *buf = u64_to_user_ptr(req->rw.addr);
3062 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003063 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003064 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003065
Pavel Begunkov7d009162019-11-25 23:14:40 +03003066 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003067 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003068 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003069 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003070
Jens Axboebcda7ba2020-02-23 16:42:51 -07003071 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003072 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003073 return -EINVAL;
3074
Jens Axboe3a6820f2019-12-22 15:19:35 -07003075 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003076 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003077 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003078 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003079 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003080 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003081 }
3082
Jens Axboe3a6820f2019-12-22 15:19:35 -07003083 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3084 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003085 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003086 }
3087
Jens Axboe4d954c22020-02-27 07:31:19 -07003088 if (req->flags & REQ_F_BUFFER_SELECT) {
3089 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003090 if (!ret)
3091 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003092 *iovec = NULL;
3093 return ret;
3094 }
3095
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003096 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3097 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003098}
3099
Jens Axboe0fef9482020-08-26 10:36:20 -06003100static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3101{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003102 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003103}
3104
Jens Axboe32960612019-09-23 11:05:34 -06003105/*
3106 * For files that don't have ->read_iter() and ->write_iter(), handle them
3107 * by looping over ->read() or ->write() manually.
3108 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003109static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003110{
Jens Axboe4017eb92020-10-22 14:14:12 -06003111 struct kiocb *kiocb = &req->rw.kiocb;
3112 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003113 ssize_t ret = 0;
3114
3115 /*
3116 * Don't support polled IO through this interface, and we can't
3117 * support non-blocking either. For the latter, this just causes
3118 * the kiocb to be handled from an async context.
3119 */
3120 if (kiocb->ki_flags & IOCB_HIPRI)
3121 return -EOPNOTSUPP;
3122 if (kiocb->ki_flags & IOCB_NOWAIT)
3123 return -EAGAIN;
3124
3125 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003126 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003127 ssize_t nr;
3128
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003129 if (!iov_iter_is_bvec(iter)) {
3130 iovec = iov_iter_iovec(iter);
3131 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003132 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3133 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003134 }
3135
Jens Axboe32960612019-09-23 11:05:34 -06003136 if (rw == READ) {
3137 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003138 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003139 } else {
3140 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003141 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003142 }
3143
3144 if (nr < 0) {
3145 if (!ret)
3146 ret = nr;
3147 break;
3148 }
3149 ret += nr;
3150 if (nr != iovec.iov_len)
3151 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003152 req->rw.len -= nr;
3153 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003154 iov_iter_advance(iter, nr);
3155 }
3156
3157 return ret;
3158}
3159
Jens Axboeff6165b2020-08-13 09:47:43 -06003160static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3161 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003162{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003163 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003164
Jens Axboeff6165b2020-08-13 09:47:43 -06003165 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003166 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003167 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003168 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003169 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003170 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003171 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003172 unsigned iov_off = 0;
3173
3174 rw->iter.iov = rw->fast_iov;
3175 if (iter->iov != fast_iov) {
3176 iov_off = iter->iov - fast_iov;
3177 rw->iter.iov += iov_off;
3178 }
3179 if (rw->fast_iov != fast_iov)
3180 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003181 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003182 } else {
3183 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003184 }
3185}
3186
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003187static inline int io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003188{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003189 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3190 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3191 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003192}
3193
Jens Axboeff6165b2020-08-13 09:47:43 -06003194static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3195 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003196 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003197{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003198 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003199 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003200 if (!req->async_data) {
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003201 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003202 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003203 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003204 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003205
Jens Axboeff6165b2020-08-13 09:47:43 -06003206 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003207 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003208 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003209}
3210
Pavel Begunkov73debe62020-09-30 22:57:54 +03003211static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003212{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003213 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003214 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003215 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003216
Pavel Begunkov2846c482020-11-07 13:16:27 +00003217 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003218 if (unlikely(ret < 0))
3219 return ret;
3220
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003221 iorw->bytes_done = 0;
3222 iorw->free_iovec = iov;
3223 if (iov)
3224 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003225 return 0;
3226}
3227
Pavel Begunkov73debe62020-09-30 22:57:54 +03003228static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003229{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003230 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3231 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003232 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003233}
3234
Jens Axboec1dd91d2020-08-03 16:43:59 -06003235/*
3236 * This is our waitqueue callback handler, registered through lock_page_async()
3237 * when we initially tried to do the IO with the iocb armed our waitqueue.
3238 * This gets called when the page is unlocked, and we generally expect that to
3239 * happen when the page IO is completed and the page is now uptodate. This will
3240 * queue a task_work based retry of the operation, attempting to copy the data
3241 * again. If the latter fails because the page was NOT uptodate, then we will
3242 * do a thread based blocking retry of the operation. That's the unexpected
3243 * slow path.
3244 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003245static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3246 int sync, void *arg)
3247{
3248 struct wait_page_queue *wpq;
3249 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003250 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003251
3252 wpq = container_of(wait, struct wait_page_queue, wait);
3253
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003254 if (!wake_page_match(wpq, key))
3255 return 0;
3256
Hao Xuc8d317a2020-09-29 20:00:45 +08003257 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003258 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003259 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003260 return 1;
3261}
3262
Jens Axboec1dd91d2020-08-03 16:43:59 -06003263/*
3264 * This controls whether a given IO request should be armed for async page
3265 * based retry. If we return false here, the request is handed to the async
3266 * worker threads for retry. If we're doing buffered reads on a regular file,
3267 * we prepare a private wait_page_queue entry and retry the operation. This
3268 * will either succeed because the page is now uptodate and unlocked, or it
3269 * will register a callback when the page is unlocked at IO completion. Through
3270 * that callback, io_uring uses task_work to setup a retry of the operation.
3271 * That retry will attempt the buffered read again. The retry will generally
3272 * succeed, or in rare cases where it fails, we then fall back to using the
3273 * async worker threads for a blocking retry.
3274 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003275static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003276{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003277 struct io_async_rw *rw = req->async_data;
3278 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003279 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003280
3281 /* never retry for NOWAIT, we just complete with -EAGAIN */
3282 if (req->flags & REQ_F_NOWAIT)
3283 return false;
3284
Jens Axboe227c0c92020-08-13 11:51:40 -06003285 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003286 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003287 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003288
Jens Axboebcf5a062020-05-22 09:24:42 -06003289 /*
3290 * just use poll if we can, and don't attempt if the fs doesn't
3291 * support callback based unlocks
3292 */
3293 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3294 return false;
3295
Jens Axboe3b2a4432020-08-16 10:58:43 -07003296 wait->wait.func = io_async_buf_func;
3297 wait->wait.private = req;
3298 wait->wait.flags = 0;
3299 INIT_LIST_HEAD(&wait->wait.entry);
3300 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003301 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003302 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003303 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003304}
3305
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003306static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003307{
3308 if (req->file->f_op->read_iter)
3309 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003310 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003311 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003312 else
3313 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003314}
3315
Pavel Begunkov889fca72021-02-10 00:03:09 +00003316static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003317{
3318 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003319 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003320 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003321 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003322 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003323 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003324
Pavel Begunkov2846c482020-11-07 13:16:27 +00003325 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003326 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003327 iovec = NULL;
3328 } else {
3329 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3330 if (ret < 0)
3331 return ret;
3332 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003333 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003334 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003335
Jens Axboefd6c2e42019-12-18 12:19:41 -07003336 /* Ensure we clear previously set non-block flag */
3337 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003338 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003339 else
3340 kiocb->ki_flags |= IOCB_NOWAIT;
3341
Pavel Begunkov24c74672020-06-21 13:09:51 +03003342 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003343 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003344 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003345 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003346 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003347
Pavel Begunkov632546c2020-11-07 13:16:26 +00003348 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003349 if (unlikely(ret)) {
3350 kfree(iovec);
3351 return ret;
3352 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003353
Jens Axboe227c0c92020-08-13 11:51:40 -06003354 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003355
Jens Axboe230d50d2021-04-01 20:41:15 -06003356 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003357 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003358 /* IOPOLL retry should happen for io-wq threads */
3359 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003360 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003361 /* no retry on NONBLOCK nor RWF_NOWAIT */
3362 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003363 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003364 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003365 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003366 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003367 } else if (ret == -EIOCBQUEUED) {
3368 goto out_free;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003369 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003370 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003371 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003372 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003373 }
3374
Jens Axboe227c0c92020-08-13 11:51:40 -06003375 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003376 if (ret2)
3377 return ret2;
3378
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003379 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003380 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003381 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003382 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003383
Pavel Begunkovb23df912021-02-04 13:52:04 +00003384 do {
3385 io_size -= ret;
3386 rw->bytes_done += ret;
3387 /* if we can retry, do so with the callbacks armed */
3388 if (!io_rw_should_retry(req)) {
3389 kiocb->ki_flags &= ~IOCB_WAITQ;
3390 return -EAGAIN;
3391 }
3392
3393 /*
3394 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3395 * we get -EIOCBQUEUED, then we'll get a notification when the
3396 * desired page gets unlocked. We can also get a partial read
3397 * here, and if we do, then just retry at the new offset.
3398 */
3399 ret = io_iter_do_read(req, iter);
3400 if (ret == -EIOCBQUEUED)
3401 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003402 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003403 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003404 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003405done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003406 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003407out_free:
3408 /* it's faster to check here then delegate to kfree */
3409 if (iovec)
3410 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003411 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003412}
3413
Pavel Begunkov73debe62020-09-30 22:57:54 +03003414static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003415{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003416 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3417 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003418 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003419}
3420
Pavel Begunkov889fca72021-02-10 00:03:09 +00003421static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003422{
3423 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003424 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003425 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003426 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003427 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003428 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003429
Pavel Begunkov2846c482020-11-07 13:16:27 +00003430 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003431 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003432 iovec = NULL;
3433 } else {
3434 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3435 if (ret < 0)
3436 return ret;
3437 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003438 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003439 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003440
Jens Axboefd6c2e42019-12-18 12:19:41 -07003441 /* Ensure we clear previously set non-block flag */
3442 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003443 kiocb->ki_flags &= ~IOCB_NOWAIT;
3444 else
3445 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003446
Pavel Begunkov24c74672020-06-21 13:09:51 +03003447 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003448 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003449 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003450
Jens Axboe10d59342019-12-09 20:16:22 -07003451 /* file path doesn't support NOWAIT for non-direct_IO */
3452 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3453 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003454 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003455
Pavel Begunkov632546c2020-11-07 13:16:26 +00003456 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003457 if (unlikely(ret))
3458 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003459
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003460 /*
3461 * Open-code file_start_write here to grab freeze protection,
3462 * which will be released by another thread in
3463 * io_complete_rw(). Fool lockdep by telling it the lock got
3464 * released so that it doesn't complain about the held lock when
3465 * we return to userspace.
3466 */
3467 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003468 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003469 __sb_writers_release(file_inode(req->file)->i_sb,
3470 SB_FREEZE_WRITE);
3471 }
3472 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003473
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003474 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003475 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003476 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003477 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003478 else
3479 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003480
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003481 if (req->flags & REQ_F_REISSUE) {
3482 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003483 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003484 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003485
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003486 /*
3487 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3488 * retry them without IOCB_NOWAIT.
3489 */
3490 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3491 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003492 /* no retry on NONBLOCK nor RWF_NOWAIT */
3493 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003494 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003495 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003496 /* IOPOLL retry should happen for io-wq threads */
3497 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3498 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003499done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003500 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003501 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003502copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003503 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003504 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003505 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003506 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003507 }
Jens Axboe31b51512019-01-18 22:56:34 -07003508out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003509 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003510 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003511 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003512 return ret;
3513}
3514
Jens Axboe80a261f2020-09-28 14:23:58 -06003515static int io_renameat_prep(struct io_kiocb *req,
3516 const struct io_uring_sqe *sqe)
3517{
3518 struct io_rename *ren = &req->rename;
3519 const char __user *oldf, *newf;
3520
Jens Axboeed7eb252021-06-23 09:04:13 -06003521 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3522 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003523 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003524 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003525 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3526 return -EBADF;
3527
3528 ren->old_dfd = READ_ONCE(sqe->fd);
3529 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3530 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3531 ren->new_dfd = READ_ONCE(sqe->len);
3532 ren->flags = READ_ONCE(sqe->rename_flags);
3533
3534 ren->oldpath = getname(oldf);
3535 if (IS_ERR(ren->oldpath))
3536 return PTR_ERR(ren->oldpath);
3537
3538 ren->newpath = getname(newf);
3539 if (IS_ERR(ren->newpath)) {
3540 putname(ren->oldpath);
3541 return PTR_ERR(ren->newpath);
3542 }
3543
3544 req->flags |= REQ_F_NEED_CLEANUP;
3545 return 0;
3546}
3547
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003548static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003549{
3550 struct io_rename *ren = &req->rename;
3551 int ret;
3552
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003553 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003554 return -EAGAIN;
3555
3556 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3557 ren->newpath, ren->flags);
3558
3559 req->flags &= ~REQ_F_NEED_CLEANUP;
3560 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003561 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003562 io_req_complete(req, ret);
3563 return 0;
3564}
3565
Jens Axboe14a11432020-09-28 14:27:37 -06003566static int io_unlinkat_prep(struct io_kiocb *req,
3567 const struct io_uring_sqe *sqe)
3568{
3569 struct io_unlink *un = &req->unlink;
3570 const char __user *fname;
3571
Jens Axboe22634bc2021-06-23 09:07:45 -06003572 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3573 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003574 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3575 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003576 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003577 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3578 return -EBADF;
3579
3580 un->dfd = READ_ONCE(sqe->fd);
3581
3582 un->flags = READ_ONCE(sqe->unlink_flags);
3583 if (un->flags & ~AT_REMOVEDIR)
3584 return -EINVAL;
3585
3586 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3587 un->filename = getname(fname);
3588 if (IS_ERR(un->filename))
3589 return PTR_ERR(un->filename);
3590
3591 req->flags |= REQ_F_NEED_CLEANUP;
3592 return 0;
3593}
3594
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003595static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003596{
3597 struct io_unlink *un = &req->unlink;
3598 int ret;
3599
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003600 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003601 return -EAGAIN;
3602
3603 if (un->flags & AT_REMOVEDIR)
3604 ret = do_rmdir(un->dfd, un->filename);
3605 else
3606 ret = do_unlinkat(un->dfd, un->filename);
3607
3608 req->flags &= ~REQ_F_NEED_CLEANUP;
3609 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003610 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003611 io_req_complete(req, ret);
3612 return 0;
3613}
3614
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003615static int io_mkdirat_prep(struct io_kiocb *req,
3616 const struct io_uring_sqe *sqe)
3617{
3618 struct io_mkdir *mkd = &req->mkdir;
3619 const char __user *fname;
3620
3621 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3622 return -EINVAL;
3623 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3624 sqe->splice_fd_in)
3625 return -EINVAL;
3626 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3627 return -EBADF;
3628
3629 mkd->dfd = READ_ONCE(sqe->fd);
3630 mkd->mode = READ_ONCE(sqe->len);
3631
3632 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3633 mkd->filename = getname(fname);
3634 if (IS_ERR(mkd->filename))
3635 return PTR_ERR(mkd->filename);
3636
3637 req->flags |= REQ_F_NEED_CLEANUP;
3638 return 0;
3639}
3640
3641static int io_mkdirat(struct io_kiocb *req, int issue_flags)
3642{
3643 struct io_mkdir *mkd = &req->mkdir;
3644 int ret;
3645
3646 if (issue_flags & IO_URING_F_NONBLOCK)
3647 return -EAGAIN;
3648
3649 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3650
3651 req->flags &= ~REQ_F_NEED_CLEANUP;
3652 if (ret < 0)
3653 req_set_fail(req);
3654 io_req_complete(req, ret);
3655 return 0;
3656}
3657
Jens Axboe36f4fa62020-09-05 11:14:22 -06003658static int io_shutdown_prep(struct io_kiocb *req,
3659 const struct io_uring_sqe *sqe)
3660{
3661#if defined(CONFIG_NET)
3662 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3663 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003664 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3665 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003666 return -EINVAL;
3667
3668 req->shutdown.how = READ_ONCE(sqe->len);
3669 return 0;
3670#else
3671 return -EOPNOTSUPP;
3672#endif
3673}
3674
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003675static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003676{
3677#if defined(CONFIG_NET)
3678 struct socket *sock;
3679 int ret;
3680
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003681 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003682 return -EAGAIN;
3683
Linus Torvalds48aba792020-12-16 12:44:05 -08003684 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003685 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003686 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003687
3688 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003689 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003690 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003691 io_req_complete(req, ret);
3692 return 0;
3693#else
3694 return -EOPNOTSUPP;
3695#endif
3696}
3697
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003698static int __io_splice_prep(struct io_kiocb *req,
3699 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003700{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003701 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003702 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003703
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003704 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3705 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003706
3707 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003708 sp->len = READ_ONCE(sqe->len);
3709 sp->flags = READ_ONCE(sqe->splice_flags);
3710
3711 if (unlikely(sp->flags & ~valid_flags))
3712 return -EINVAL;
3713
Pavel Begunkov62906e82021-08-10 14:52:47 +01003714 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003715 (sp->flags & SPLICE_F_FD_IN_FIXED));
3716 if (!sp->file_in)
3717 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003718 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003719 return 0;
3720}
3721
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003722static int io_tee_prep(struct io_kiocb *req,
3723 const struct io_uring_sqe *sqe)
3724{
3725 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3726 return -EINVAL;
3727 return __io_splice_prep(req, sqe);
3728}
3729
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003730static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003731{
3732 struct io_splice *sp = &req->splice;
3733 struct file *in = sp->file_in;
3734 struct file *out = sp->file_out;
3735 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3736 long ret = 0;
3737
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003738 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003739 return -EAGAIN;
3740 if (sp->len)
3741 ret = do_tee(in, out, sp->len, flags);
3742
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003743 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3744 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003745 req->flags &= ~REQ_F_NEED_CLEANUP;
3746
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003747 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003748 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003749 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003750 return 0;
3751}
3752
3753static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3754{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003755 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003756
3757 sp->off_in = READ_ONCE(sqe->splice_off_in);
3758 sp->off_out = READ_ONCE(sqe->off);
3759 return __io_splice_prep(req, sqe);
3760}
3761
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003762static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003763{
3764 struct io_splice *sp = &req->splice;
3765 struct file *in = sp->file_in;
3766 struct file *out = sp->file_out;
3767 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3768 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003769 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003770
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003771 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003772 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003773
3774 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3775 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003776
Jens Axboe948a7742020-05-17 14:21:38 -06003777 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003778 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003779
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003780 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3781 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003782 req->flags &= ~REQ_F_NEED_CLEANUP;
3783
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003784 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003785 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003786 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003787 return 0;
3788}
3789
Jens Axboe2b188cc2019-01-07 10:46:33 -07003790/*
3791 * IORING_OP_NOP just posts a completion event, nothing else.
3792 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003793static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003794{
3795 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003796
Jens Axboedef596e2019-01-09 08:59:42 -07003797 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3798 return -EINVAL;
3799
Pavel Begunkov889fca72021-02-10 00:03:09 +00003800 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003801 return 0;
3802}
3803
Pavel Begunkov1155c762021-02-18 18:29:38 +00003804static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003805{
Jens Axboe6b063142019-01-10 22:13:58 -07003806 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003807
Jens Axboe09bb8392019-03-13 12:39:28 -06003808 if (!req->file)
3809 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003810
Jens Axboe6b063142019-01-10 22:13:58 -07003811 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003812 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003813 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
3814 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003815 return -EINVAL;
3816
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003817 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3818 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3819 return -EINVAL;
3820
3821 req->sync.off = READ_ONCE(sqe->off);
3822 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003823 return 0;
3824}
3825
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003826static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003827{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003828 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003829 int ret;
3830
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003831 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003832 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003833 return -EAGAIN;
3834
Jens Axboe9adbd452019-12-20 08:45:55 -07003835 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003836 end > 0 ? end : LLONG_MAX,
3837 req->sync.flags & IORING_FSYNC_DATASYNC);
3838 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003839 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003840 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003841 return 0;
3842}
3843
Jens Axboed63d1b52019-12-10 10:38:56 -07003844static int io_fallocate_prep(struct io_kiocb *req,
3845 const struct io_uring_sqe *sqe)
3846{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003847 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
3848 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07003849 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003850 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3851 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003852
3853 req->sync.off = READ_ONCE(sqe->off);
3854 req->sync.len = READ_ONCE(sqe->addr);
3855 req->sync.mode = READ_ONCE(sqe->len);
3856 return 0;
3857}
3858
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003859static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003860{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003861 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003862
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003863 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003864 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003865 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003866 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3867 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003868 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003869 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003870 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003871 return 0;
3872}
3873
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003874static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003875{
Jens Axboef8748882020-01-08 17:47:02 -07003876 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003877 int ret;
3878
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01003879 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3880 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003881 if (unlikely(sqe->ioprio || sqe->buf_index || sqe->splice_fd_in))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003882 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003883 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003884 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003885
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003886 /* open.how should be already initialised */
3887 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003888 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003889
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003890 req->open.dfd = READ_ONCE(sqe->fd);
3891 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003892 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003893 if (IS_ERR(req->open.filename)) {
3894 ret = PTR_ERR(req->open.filename);
3895 req->open.filename = NULL;
3896 return ret;
3897 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003898 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003899 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003900 return 0;
3901}
3902
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003903static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3904{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01003905 u64 mode = READ_ONCE(sqe->len);
3906 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003907
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003908 req->open.how = build_open_how(flags, mode);
3909 return __io_openat_prep(req, sqe);
3910}
3911
Jens Axboecebdb982020-01-08 17:59:24 -07003912static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3913{
3914 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003915 size_t len;
3916 int ret;
3917
Jens Axboecebdb982020-01-08 17:59:24 -07003918 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3919 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003920 if (len < OPEN_HOW_SIZE_VER0)
3921 return -EINVAL;
3922
3923 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3924 len);
3925 if (ret)
3926 return ret;
3927
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003928 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003929}
3930
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003931static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003932{
3933 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003934 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003935 bool nonblock_set;
3936 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003937 int ret;
3938
Jens Axboecebdb982020-01-08 17:59:24 -07003939 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003940 if (ret)
3941 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003942 nonblock_set = op.open_flag & O_NONBLOCK;
3943 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003944 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003945 /*
3946 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3947 * it'll always -EAGAIN
3948 */
3949 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3950 return -EAGAIN;
3951 op.lookup_flags |= LOOKUP_CACHED;
3952 op.open_flag |= O_NONBLOCK;
3953 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003954
Jens Axboe4022e7a2020-03-19 19:23:18 -06003955 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003956 if (ret < 0)
3957 goto err;
3958
3959 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003960 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003961 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003962 * We could hang on to this 'fd' on retrying, but seems like
3963 * marginal gain for something that is now known to be a slower
3964 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07003965 */
3966 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003967
3968 ret = PTR_ERR(file);
3969 /* only retry if RESOLVE_CACHED wasn't already set by application */
3970 if (ret == -EAGAIN &&
3971 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
3972 return -EAGAIN;
3973 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003974 }
3975
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003976 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
3977 file->f_flags &= ~O_NONBLOCK;
3978 fsnotify_open(file);
3979 fd_install(ret, file);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003980err:
3981 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003982 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003983 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003984 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01003985 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003986 return 0;
3987}
3988
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003989static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003990{
Pavel Begunkove45cff52021-02-28 22:35:14 +00003991 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07003992}
3993
Jens Axboe067524e2020-03-02 16:32:28 -07003994static int io_remove_buffers_prep(struct io_kiocb *req,
3995 const struct io_uring_sqe *sqe)
3996{
3997 struct io_provide_buf *p = &req->pbuf;
3998 u64 tmp;
3999
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004000 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4001 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004002 return -EINVAL;
4003
4004 tmp = READ_ONCE(sqe->fd);
4005 if (!tmp || tmp > USHRT_MAX)
4006 return -EINVAL;
4007
4008 memset(p, 0, sizeof(*p));
4009 p->nbufs = tmp;
4010 p->bgid = READ_ONCE(sqe->buf_group);
4011 return 0;
4012}
4013
4014static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4015 int bgid, unsigned nbufs)
4016{
4017 unsigned i = 0;
4018
4019 /* shouldn't happen */
4020 if (!nbufs)
4021 return 0;
4022
4023 /* the head kbuf is the list itself */
4024 while (!list_empty(&buf->list)) {
4025 struct io_buffer *nxt;
4026
4027 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4028 list_del(&nxt->list);
4029 kfree(nxt);
4030 if (++i == nbufs)
4031 return i;
4032 }
4033 i++;
4034 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004035 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004036
4037 return i;
4038}
4039
Pavel Begunkov889fca72021-02-10 00:03:09 +00004040static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004041{
4042 struct io_provide_buf *p = &req->pbuf;
4043 struct io_ring_ctx *ctx = req->ctx;
4044 struct io_buffer *head;
4045 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004046 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004047
4048 io_ring_submit_lock(ctx, !force_nonblock);
4049
4050 lockdep_assert_held(&ctx->uring_lock);
4051
4052 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004053 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004054 if (head)
4055 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004056 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004057 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004058
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004059 /* complete before unlock, IOPOLL may need the lock */
4060 __io_req_complete(req, issue_flags, ret, 0);
4061 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004062 return 0;
4063}
4064
Jens Axboeddf0322d2020-02-23 16:41:33 -07004065static int io_provide_buffers_prep(struct io_kiocb *req,
4066 const struct io_uring_sqe *sqe)
4067{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004068 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004069 struct io_provide_buf *p = &req->pbuf;
4070 u64 tmp;
4071
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004072 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004073 return -EINVAL;
4074
4075 tmp = READ_ONCE(sqe->fd);
4076 if (!tmp || tmp > USHRT_MAX)
4077 return -E2BIG;
4078 p->nbufs = tmp;
4079 p->addr = READ_ONCE(sqe->addr);
4080 p->len = READ_ONCE(sqe->len);
4081
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004082 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4083 &size))
4084 return -EOVERFLOW;
4085 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4086 return -EOVERFLOW;
4087
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004088 size = (unsigned long)p->len * p->nbufs;
4089 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004090 return -EFAULT;
4091
4092 p->bgid = READ_ONCE(sqe->buf_group);
4093 tmp = READ_ONCE(sqe->off);
4094 if (tmp > USHRT_MAX)
4095 return -E2BIG;
4096 p->bid = tmp;
4097 return 0;
4098}
4099
4100static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4101{
4102 struct io_buffer *buf;
4103 u64 addr = pbuf->addr;
4104 int i, bid = pbuf->bid;
4105
4106 for (i = 0; i < pbuf->nbufs; i++) {
4107 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4108 if (!buf)
4109 break;
4110
4111 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004112 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004113 buf->bid = bid;
4114 addr += pbuf->len;
4115 bid++;
4116 if (!*head) {
4117 INIT_LIST_HEAD(&buf->list);
4118 *head = buf;
4119 } else {
4120 list_add_tail(&buf->list, &(*head)->list);
4121 }
4122 }
4123
4124 return i ? i : -ENOMEM;
4125}
4126
Pavel Begunkov889fca72021-02-10 00:03:09 +00004127static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004128{
4129 struct io_provide_buf *p = &req->pbuf;
4130 struct io_ring_ctx *ctx = req->ctx;
4131 struct io_buffer *head, *list;
4132 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004133 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004134
4135 io_ring_submit_lock(ctx, !force_nonblock);
4136
4137 lockdep_assert_held(&ctx->uring_lock);
4138
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004139 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004140
4141 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004142 if (ret >= 0 && !list) {
4143 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4144 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004145 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004146 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004147 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004148 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004149 /* complete before unlock, IOPOLL may need the lock */
4150 __io_req_complete(req, issue_flags, ret, 0);
4151 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004152 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004153}
4154
Jens Axboe3e4827b2020-01-08 15:18:09 -07004155static int io_epoll_ctl_prep(struct io_kiocb *req,
4156 const struct io_uring_sqe *sqe)
4157{
4158#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004159 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004160 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004161 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004162 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004163
4164 req->epoll.epfd = READ_ONCE(sqe->fd);
4165 req->epoll.op = READ_ONCE(sqe->len);
4166 req->epoll.fd = READ_ONCE(sqe->off);
4167
4168 if (ep_op_has_event(req->epoll.op)) {
4169 struct epoll_event __user *ev;
4170
4171 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4172 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4173 return -EFAULT;
4174 }
4175
4176 return 0;
4177#else
4178 return -EOPNOTSUPP;
4179#endif
4180}
4181
Pavel Begunkov889fca72021-02-10 00:03:09 +00004182static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004183{
4184#if defined(CONFIG_EPOLL)
4185 struct io_epoll *ie = &req->epoll;
4186 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004187 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004188
4189 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4190 if (force_nonblock && ret == -EAGAIN)
4191 return -EAGAIN;
4192
4193 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004194 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004195 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004196 return 0;
4197#else
4198 return -EOPNOTSUPP;
4199#endif
4200}
4201
Jens Axboec1ca7572019-12-25 22:18:28 -07004202static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4203{
4204#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004205 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004206 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004207 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4208 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004209
4210 req->madvise.addr = READ_ONCE(sqe->addr);
4211 req->madvise.len = READ_ONCE(sqe->len);
4212 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4213 return 0;
4214#else
4215 return -EOPNOTSUPP;
4216#endif
4217}
4218
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004219static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004220{
4221#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4222 struct io_madvise *ma = &req->madvise;
4223 int ret;
4224
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004225 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004226 return -EAGAIN;
4227
Minchan Kim0726b012020-10-17 16:14:50 -07004228 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004229 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004230 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004231 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004232 return 0;
4233#else
4234 return -EOPNOTSUPP;
4235#endif
4236}
4237
Jens Axboe4840e412019-12-25 22:03:45 -07004238static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4239{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004240 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004241 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004242 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4243 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004244
4245 req->fadvise.offset = READ_ONCE(sqe->off);
4246 req->fadvise.len = READ_ONCE(sqe->len);
4247 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4248 return 0;
4249}
4250
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004251static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004252{
4253 struct io_fadvise *fa = &req->fadvise;
4254 int ret;
4255
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004256 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004257 switch (fa->advice) {
4258 case POSIX_FADV_NORMAL:
4259 case POSIX_FADV_RANDOM:
4260 case POSIX_FADV_SEQUENTIAL:
4261 break;
4262 default:
4263 return -EAGAIN;
4264 }
4265 }
Jens Axboe4840e412019-12-25 22:03:45 -07004266
4267 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4268 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004269 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004270 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004271 return 0;
4272}
4273
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004274static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4275{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004276 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004277 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004278 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004279 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004280 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004281 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004282
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004283 req->statx.dfd = READ_ONCE(sqe->fd);
4284 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004285 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004286 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4287 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004288
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004289 return 0;
4290}
4291
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004292static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004293{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004294 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004295 int ret;
4296
Pavel Begunkov59d70012021-03-22 01:58:30 +00004297 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004298 return -EAGAIN;
4299
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004300 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4301 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004302
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004303 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004304 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004305 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004306 return 0;
4307}
4308
Jens Axboeb5dba592019-12-11 14:02:38 -07004309static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4310{
Jens Axboe14587a462020-09-05 11:36:08 -06004311 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004312 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004313 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004314 sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeb5dba592019-12-11 14:02:38 -07004315 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004316 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004317 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004318
4319 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004320 return 0;
4321}
4322
Pavel Begunkov889fca72021-02-10 00:03:09 +00004323static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004324{
Jens Axboe9eac1902021-01-19 15:50:37 -07004325 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004326 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004327 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004328 struct file *file = NULL;
4329 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004330
Jens Axboe9eac1902021-01-19 15:50:37 -07004331 spin_lock(&files->file_lock);
4332 fdt = files_fdtable(files);
4333 if (close->fd >= fdt->max_fds) {
4334 spin_unlock(&files->file_lock);
4335 goto err;
4336 }
4337 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004338 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004339 spin_unlock(&files->file_lock);
4340 file = NULL;
4341 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004342 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004343
4344 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004345 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004346 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004347 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004348 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004349
Jens Axboe9eac1902021-01-19 15:50:37 -07004350 ret = __close_fd_get_file(close->fd, &file);
4351 spin_unlock(&files->file_lock);
4352 if (ret < 0) {
4353 if (ret == -ENOENT)
4354 ret = -EBADF;
4355 goto err;
4356 }
4357
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004358 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004359 ret = filp_close(file, current->files);
4360err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004361 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004362 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004363 if (file)
4364 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004365 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004366 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004367}
4368
Pavel Begunkov1155c762021-02-18 18:29:38 +00004369static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004370{
4371 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004372
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004373 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4374 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004375 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4376 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004377 return -EINVAL;
4378
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004379 req->sync.off = READ_ONCE(sqe->off);
4380 req->sync.len = READ_ONCE(sqe->len);
4381 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004382 return 0;
4383}
4384
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004385static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004386{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004387 int ret;
4388
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004389 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004390 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004391 return -EAGAIN;
4392
Jens Axboe9adbd452019-12-20 08:45:55 -07004393 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004394 req->sync.flags);
4395 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004396 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004397 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004398 return 0;
4399}
4400
YueHaibing469956e2020-03-04 15:53:52 +08004401#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004402static int io_setup_async_msg(struct io_kiocb *req,
4403 struct io_async_msghdr *kmsg)
4404{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004405 struct io_async_msghdr *async_msg = req->async_data;
4406
4407 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004408 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004409 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004410 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004411 return -ENOMEM;
4412 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004413 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004414 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004415 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004416 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004417 /* if were using fast_iov, set it to the new one */
4418 if (!async_msg->free_iov)
4419 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4420
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004421 return -EAGAIN;
4422}
4423
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004424static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4425 struct io_async_msghdr *iomsg)
4426{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004427 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004428 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004429 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004430 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004431}
4432
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004433static int io_sendmsg_prep_async(struct io_kiocb *req)
4434{
4435 int ret;
4436
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004437 ret = io_sendmsg_copy_hdr(req, req->async_data);
4438 if (!ret)
4439 req->flags |= REQ_F_NEED_CLEANUP;
4440 return ret;
4441}
4442
Jens Axboe3529d8c2019-12-19 18:24:38 -07004443static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004444{
Jens Axboee47293f2019-12-20 08:58:21 -07004445 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004446
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004447 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4448 return -EINVAL;
4449
Pavel Begunkov270a5942020-07-12 20:41:04 +03004450 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004451 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004452 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4453 if (sr->msg_flags & MSG_DONTWAIT)
4454 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004455
Jens Axboed8768362020-02-27 14:17:49 -07004456#ifdef CONFIG_COMPAT
4457 if (req->ctx->compat)
4458 sr->msg_flags |= MSG_CMSG_COMPAT;
4459#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004460 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004461}
4462
Pavel Begunkov889fca72021-02-10 00:03:09 +00004463static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004464{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004465 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004466 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004467 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004468 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004469 int ret;
4470
Florent Revestdba4a922020-12-04 12:36:04 +01004471 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004472 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004473 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004474
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004475 kmsg = req->async_data;
4476 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004477 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004478 if (ret)
4479 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004480 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004481 }
4482
Pavel Begunkov04411802021-04-01 15:44:00 +01004483 flags = req->sr_msg.msg_flags;
4484 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004485 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004486 if (flags & MSG_WAITALL)
4487 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4488
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004489 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004490 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004491 return io_setup_async_msg(req, kmsg);
4492 if (ret == -ERESTARTSYS)
4493 ret = -EINTR;
4494
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004495 /* fast path, check for non-NULL to avoid function call */
4496 if (kmsg->free_iov)
4497 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004498 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004499 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004500 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004501 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004502 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004503}
4504
Pavel Begunkov889fca72021-02-10 00:03:09 +00004505static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004506{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004507 struct io_sr_msg *sr = &req->sr_msg;
4508 struct msghdr msg;
4509 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004510 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004511 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004512 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004513 int ret;
4514
Florent Revestdba4a922020-12-04 12:36:04 +01004515 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004516 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004517 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004518
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004519 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4520 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004521 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004522
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004523 msg.msg_name = NULL;
4524 msg.msg_control = NULL;
4525 msg.msg_controllen = 0;
4526 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004527
Pavel Begunkov04411802021-04-01 15:44:00 +01004528 flags = req->sr_msg.msg_flags;
4529 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004530 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004531 if (flags & MSG_WAITALL)
4532 min_ret = iov_iter_count(&msg.msg_iter);
4533
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004534 msg.msg_flags = flags;
4535 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004536 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004537 return -EAGAIN;
4538 if (ret == -ERESTARTSYS)
4539 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004540
Stefan Metzmacher00312752021-03-20 20:33:36 +01004541 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004542 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004543 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004544 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004545}
4546
Pavel Begunkov1400e692020-07-12 20:41:05 +03004547static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4548 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004549{
4550 struct io_sr_msg *sr = &req->sr_msg;
4551 struct iovec __user *uiov;
4552 size_t iov_len;
4553 int ret;
4554
Pavel Begunkov1400e692020-07-12 20:41:05 +03004555 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4556 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004557 if (ret)
4558 return ret;
4559
4560 if (req->flags & REQ_F_BUFFER_SELECT) {
4561 if (iov_len > 1)
4562 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004563 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004564 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004565 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004566 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004567 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004568 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004569 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004570 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004571 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004572 if (ret > 0)
4573 ret = 0;
4574 }
4575
4576 return ret;
4577}
4578
4579#ifdef CONFIG_COMPAT
4580static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004581 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004582{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004583 struct io_sr_msg *sr = &req->sr_msg;
4584 struct compat_iovec __user *uiov;
4585 compat_uptr_t ptr;
4586 compat_size_t len;
4587 int ret;
4588
Pavel Begunkov4af34172021-04-11 01:46:30 +01004589 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4590 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004591 if (ret)
4592 return ret;
4593
4594 uiov = compat_ptr(ptr);
4595 if (req->flags & REQ_F_BUFFER_SELECT) {
4596 compat_ssize_t clen;
4597
4598 if (len > 1)
4599 return -EINVAL;
4600 if (!access_ok(uiov, sizeof(*uiov)))
4601 return -EFAULT;
4602 if (__get_user(clen, &uiov->iov_len))
4603 return -EFAULT;
4604 if (clen < 0)
4605 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004606 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004607 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004608 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004609 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004610 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004611 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004612 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004613 if (ret < 0)
4614 return ret;
4615 }
4616
4617 return 0;
4618}
Jens Axboe03b12302019-12-02 18:50:25 -07004619#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004620
Pavel Begunkov1400e692020-07-12 20:41:05 +03004621static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4622 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004623{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004624 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004625
4626#ifdef CONFIG_COMPAT
4627 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004628 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004629#endif
4630
Pavel Begunkov1400e692020-07-12 20:41:05 +03004631 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004632}
4633
Jens Axboebcda7ba2020-02-23 16:42:51 -07004634static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004635 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004636{
4637 struct io_sr_msg *sr = &req->sr_msg;
4638 struct io_buffer *kbuf;
4639
Jens Axboebcda7ba2020-02-23 16:42:51 -07004640 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4641 if (IS_ERR(kbuf))
4642 return kbuf;
4643
4644 sr->kbuf = kbuf;
4645 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004646 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004647}
4648
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004649static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4650{
4651 return io_put_kbuf(req, req->sr_msg.kbuf);
4652}
4653
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004654static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004655{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004656 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004657
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004658 ret = io_recvmsg_copy_hdr(req, req->async_data);
4659 if (!ret)
4660 req->flags |= REQ_F_NEED_CLEANUP;
4661 return ret;
4662}
4663
4664static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4665{
4666 struct io_sr_msg *sr = &req->sr_msg;
4667
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004668 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4669 return -EINVAL;
4670
Pavel Begunkov270a5942020-07-12 20:41:04 +03004671 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004672 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004673 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004674 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4675 if (sr->msg_flags & MSG_DONTWAIT)
4676 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004677
Jens Axboed8768362020-02-27 14:17:49 -07004678#ifdef CONFIG_COMPAT
4679 if (req->ctx->compat)
4680 sr->msg_flags |= MSG_CMSG_COMPAT;
4681#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004682 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004683}
4684
Pavel Begunkov889fca72021-02-10 00:03:09 +00004685static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004686{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004687 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004688 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004689 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004690 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004691 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004692 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004693 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004694
Florent Revestdba4a922020-12-04 12:36:04 +01004695 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004696 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004697 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004698
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004699 kmsg = req->async_data;
4700 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004701 ret = io_recvmsg_copy_hdr(req, &iomsg);
4702 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004703 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004704 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004705 }
4706
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004707 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004708 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004709 if (IS_ERR(kbuf))
4710 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004711 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004712 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4713 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004714 1, req->sr_msg.len);
4715 }
4716
Pavel Begunkov04411802021-04-01 15:44:00 +01004717 flags = req->sr_msg.msg_flags;
4718 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004719 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004720 if (flags & MSG_WAITALL)
4721 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4722
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004723 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4724 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004725 if (force_nonblock && ret == -EAGAIN)
4726 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004727 if (ret == -ERESTARTSYS)
4728 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004729
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004730 if (req->flags & REQ_F_BUFFER_SELECTED)
4731 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004732 /* fast path, check for non-NULL to avoid function call */
4733 if (kmsg->free_iov)
4734 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004735 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004736 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004737 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004738 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004739 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004740}
4741
Pavel Begunkov889fca72021-02-10 00:03:09 +00004742static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004743{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004744 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004745 struct io_sr_msg *sr = &req->sr_msg;
4746 struct msghdr msg;
4747 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004748 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004749 struct iovec iov;
4750 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004751 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004752 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004753 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004754
Florent Revestdba4a922020-12-04 12:36:04 +01004755 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004756 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004757 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004758
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004759 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004760 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004761 if (IS_ERR(kbuf))
4762 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004763 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004764 }
4765
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004766 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004767 if (unlikely(ret))
4768 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004769
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004770 msg.msg_name = NULL;
4771 msg.msg_control = NULL;
4772 msg.msg_controllen = 0;
4773 msg.msg_namelen = 0;
4774 msg.msg_iocb = NULL;
4775 msg.msg_flags = 0;
4776
Pavel Begunkov04411802021-04-01 15:44:00 +01004777 flags = req->sr_msg.msg_flags;
4778 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004779 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004780 if (flags & MSG_WAITALL)
4781 min_ret = iov_iter_count(&msg.msg_iter);
4782
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004783 ret = sock_recvmsg(sock, &msg, flags);
4784 if (force_nonblock && ret == -EAGAIN)
4785 return -EAGAIN;
4786 if (ret == -ERESTARTSYS)
4787 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004788out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004789 if (req->flags & REQ_F_BUFFER_SELECTED)
4790 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01004791 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004792 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004793 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004794 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004795}
4796
Jens Axboe3529d8c2019-12-19 18:24:38 -07004797static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004798{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004799 struct io_accept *accept = &req->accept;
4800
Jens Axboe14587a462020-09-05 11:36:08 -06004801 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004802 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004803 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004804 return -EINVAL;
4805
Jens Axboed55e5f52019-12-11 16:12:15 -07004806 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4807 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004808 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004809 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004810 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004811}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004812
Pavel Begunkov889fca72021-02-10 00:03:09 +00004813static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004814{
4815 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004816 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004817 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004818 int ret;
4819
Jiufei Xuee697dee2020-06-10 13:41:59 +08004820 if (req->file->f_flags & O_NONBLOCK)
4821 req->flags |= REQ_F_NOWAIT;
4822
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004823 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004824 accept->addr_len, accept->flags,
4825 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004826 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004827 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004828 if (ret < 0) {
4829 if (ret == -ERESTARTSYS)
4830 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004831 req_set_fail(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004832 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004833 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004834 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004835}
4836
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004837static int io_connect_prep_async(struct io_kiocb *req)
4838{
4839 struct io_async_connect *io = req->async_data;
4840 struct io_connect *conn = &req->connect;
4841
4842 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4843}
4844
Jens Axboe3529d8c2019-12-19 18:24:38 -07004845static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004846{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004847 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004848
Jens Axboe14587a462020-09-05 11:36:08 -06004849 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004850 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004851 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
4852 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004853 return -EINVAL;
4854
Jens Axboe3529d8c2019-12-19 18:24:38 -07004855 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4856 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004857 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004858}
4859
Pavel Begunkov889fca72021-02-10 00:03:09 +00004860static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004861{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004862 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004863 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004864 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004865 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004866
Jens Axboee8c2bc12020-08-15 18:44:09 -07004867 if (req->async_data) {
4868 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004869 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004870 ret = move_addr_to_kernel(req->connect.addr,
4871 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004872 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004873 if (ret)
4874 goto out;
4875 io = &__io;
4876 }
4877
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004878 file_flags = force_nonblock ? O_NONBLOCK : 0;
4879
Jens Axboee8c2bc12020-08-15 18:44:09 -07004880 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004881 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004882 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004883 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004884 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004885 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004886 ret = -ENOMEM;
4887 goto out;
4888 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004889 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004890 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004891 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004892 if (ret == -ERESTARTSYS)
4893 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004894out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004895 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004896 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004897 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004898 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004899}
YueHaibing469956e2020-03-04 15:53:52 +08004900#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004901#define IO_NETOP_FN(op) \
4902static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4903{ \
4904 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004905}
4906
Jens Axboe99a10082021-02-19 09:35:19 -07004907#define IO_NETOP_PREP(op) \
4908IO_NETOP_FN(op) \
4909static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4910{ \
4911 return -EOPNOTSUPP; \
4912} \
4913
4914#define IO_NETOP_PREP_ASYNC(op) \
4915IO_NETOP_PREP(op) \
4916static int io_##op##_prep_async(struct io_kiocb *req) \
4917{ \
4918 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004919}
4920
Jens Axboe99a10082021-02-19 09:35:19 -07004921IO_NETOP_PREP_ASYNC(sendmsg);
4922IO_NETOP_PREP_ASYNC(recvmsg);
4923IO_NETOP_PREP_ASYNC(connect);
4924IO_NETOP_PREP(accept);
4925IO_NETOP_FN(send);
4926IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004927#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004928
Jens Axboed7718a92020-02-14 22:23:12 -07004929struct io_poll_table {
4930 struct poll_table_struct pt;
4931 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01004932 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07004933 int error;
4934};
4935
Jens Axboed7718a92020-02-14 22:23:12 -07004936static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01004937 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07004938{
Jens Axboed7718a92020-02-14 22:23:12 -07004939 /* for instances that support it check for an event match first: */
4940 if (mask && !(mask & poll->events))
4941 return 0;
4942
4943 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4944
4945 list_del_init(&poll->wait.entry);
4946
Jens Axboed7718a92020-02-14 22:23:12 -07004947 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01004948 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004949
Jens Axboed7718a92020-02-14 22:23:12 -07004950 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004951 * If this fails, then the task is exiting. When a task exits, the
4952 * work gets canceled, so just cancel this request as well instead
4953 * of executing it. We can't safely execute it anyway, as we may not
4954 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004955 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01004956 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004957 return 1;
4958}
4959
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004960static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4961 __acquires(&req->ctx->completion_lock)
4962{
4963 struct io_ring_ctx *ctx = req->ctx;
4964
Jens Axboe316319e2021-08-19 09:41:42 -06004965 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01004966 if (unlikely(req->task->flags & PF_EXITING))
4967 WRITE_ONCE(poll->canceled, true);
4968
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004969 if (!req->result && !READ_ONCE(poll->canceled)) {
4970 struct poll_table_struct pt = { ._key = poll->events };
4971
4972 req->result = vfs_poll(req->file, &pt) & poll->events;
4973 }
4974
Jens Axboe79ebeae2021-08-10 15:18:27 -06004975 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004976 if (!req->result && !READ_ONCE(poll->canceled)) {
4977 add_wait_queue(poll->head, &poll->wait);
4978 return true;
4979 }
4980
4981 return false;
4982}
4983
Jens Axboed4e7cd32020-08-15 11:44:50 -07004984static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004985{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004986 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004987 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004988 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004989 return req->apoll->double_poll;
4990}
4991
4992static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4993{
4994 if (req->opcode == IORING_OP_POLL_ADD)
4995 return &req->poll;
4996 return &req->apoll->poll;
4997}
4998
4999static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005000 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005001{
5002 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005003
5004 lockdep_assert_held(&req->ctx->completion_lock);
5005
5006 if (poll && poll->head) {
5007 struct wait_queue_head *head = poll->head;
5008
Jens Axboe79ebeae2021-08-10 15:18:27 -06005009 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005010 list_del_init(&poll->wait.entry);
5011 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005012 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005013 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005014 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005015 }
5016}
5017
Pavel Begunkove27414b2021-04-09 09:13:20 +01005018static bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005019 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005020{
5021 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005022 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005023 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005024
Pavel Begunkove27414b2021-04-09 09:13:20 +01005025 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005026 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005027 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005028 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005029 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005030 }
Jens Axboeb69de282021-03-17 08:37:41 -06005031 if (req->poll.events & EPOLLONESHOT)
5032 flags = 0;
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005033 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005034 req->poll.done = true;
5035 flags = 0;
5036 }
Hao Xu7b289c32021-04-13 15:20:39 +08005037 if (flags & IORING_CQE_F_MORE)
5038 ctx->cq_extra++;
5039
Jens Axboe18bceab2020-05-15 11:56:54 -06005040 io_commit_cqring(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005041 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005042}
5043
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005044static void io_poll_task_func(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005045{
Jens Axboe6d816e02020-08-11 08:04:14 -06005046 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005047 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005048
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005049 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005050 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005051 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005052 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005053
Pavel Begunkove27414b2021-04-09 09:13:20 +01005054 done = io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005055 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005056 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005057 hash_del(&req->hash_node);
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005058 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005059 req->result = 0;
5060 add_wait_queue(req->poll.head, &req->poll.wait);
5061 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005062 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005063 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005064
Jens Axboe88e41cf2021-02-22 22:08:01 -07005065 if (done) {
5066 nxt = io_put_req_find_next(req);
5067 if (nxt)
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005068 io_req_task_submit(nxt);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005069 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005070 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005071}
5072
5073static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5074 int sync, void *key)
5075{
5076 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005077 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005078 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005079 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005080
5081 /* for instances that support it check for an event match first: */
5082 if (mask && !(mask & poll->events))
5083 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005084 if (!(poll->events & EPOLLONESHOT))
5085 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005086
Jens Axboe8706e042020-09-28 08:38:54 -06005087 list_del_init(&wait->entry);
5088
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005089 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005090 bool done;
5091
Jens Axboe79ebeae2021-08-10 15:18:27 -06005092 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005093 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005094 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005095 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005096 /* make sure double remove sees this as being gone */
5097 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005098 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005099 if (!done) {
5100 /* use wait func handler, so it matches the rq type */
5101 poll->wait.func(&poll->wait, mode, sync, key);
5102 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005103 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005104 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005105 return 1;
5106}
5107
5108static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5109 wait_queue_func_t wake_func)
5110{
5111 poll->head = NULL;
5112 poll->done = false;
5113 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005114#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5115 /* mask in events that we always want/need */
5116 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005117 INIT_LIST_HEAD(&poll->wait.entry);
5118 init_waitqueue_func_entry(&poll->wait, wake_func);
5119}
5120
5121static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005122 struct wait_queue_head *head,
5123 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005124{
5125 struct io_kiocb *req = pt->req;
5126
5127 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005128 * The file being polled uses multiple waitqueues for poll handling
5129 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5130 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005131 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005132 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005133 struct io_poll_iocb *poll_one = poll;
5134
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005135 /* double add on the same waitqueue head, ignore */
5136 if (poll_one->head == head)
5137 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005138 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005139 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005140 if ((*poll_ptr)->head == head)
5141 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005142 pt->error = -EINVAL;
5143 return;
5144 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005145 /*
5146 * Can't handle multishot for double wait for now, turn it
5147 * into one-shot mode.
5148 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005149 if (!(poll_one->events & EPOLLONESHOT))
5150 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005151 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5152 if (!poll) {
5153 pt->error = -ENOMEM;
5154 return;
5155 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005156 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005157 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005158 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005159 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005160 }
5161
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005162 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005163 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005164
5165 if (poll->events & EPOLLEXCLUSIVE)
5166 add_wait_queue_exclusive(head, &poll->wait);
5167 else
5168 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005169}
5170
5171static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5172 struct poll_table_struct *p)
5173{
5174 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005175 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005176
Jens Axboe807abcb2020-07-17 17:09:27 -06005177 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005178}
5179
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005180static void io_async_task_func(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005181{
Jens Axboed7718a92020-02-14 22:23:12 -07005182 struct async_poll *apoll = req->apoll;
5183 struct io_ring_ctx *ctx = req->ctx;
5184
Olivier Langlois236daeae2021-05-31 02:36:37 -04005185 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005186
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005187 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005188 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005189 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005190 }
5191
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005192 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005193 io_poll_remove_double(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005194 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005195
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005196 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005197 io_req_task_submit(req);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005198 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005199 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005200}
5201
5202static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5203 void *key)
5204{
5205 struct io_kiocb *req = wait->private;
5206 struct io_poll_iocb *poll = &req->apoll->poll;
5207
5208 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5209 key_to_poll(key));
5210
5211 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5212}
5213
5214static void io_poll_req_insert(struct io_kiocb *req)
5215{
5216 struct io_ring_ctx *ctx = req->ctx;
5217 struct hlist_head *list;
5218
5219 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5220 hlist_add_head(&req->hash_node, list);
5221}
5222
5223static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5224 struct io_poll_iocb *poll,
5225 struct io_poll_table *ipt, __poll_t mask,
5226 wait_queue_func_t wake_func)
5227 __acquires(&ctx->completion_lock)
5228{
5229 struct io_ring_ctx *ctx = req->ctx;
5230 bool cancel = false;
5231
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005232 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005233 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005234 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005235 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005236
5237 ipt->pt._key = mask;
5238 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005239 ipt->error = 0;
5240 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005241
Jens Axboed7718a92020-02-14 22:23:12 -07005242 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005243 if (unlikely(!ipt->nr_entries) && !ipt->error)
5244 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005245
Jens Axboe79ebeae2021-08-10 15:18:27 -06005246 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005247 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005248 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005249 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005250 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005251 if (unlikely(list_empty(&poll->wait.entry))) {
5252 if (ipt->error)
5253 cancel = true;
5254 ipt->error = 0;
5255 mask = 0;
5256 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005257 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005258 list_del_init(&poll->wait.entry);
5259 else if (cancel)
5260 WRITE_ONCE(poll->canceled, true);
5261 else if (!poll->done) /* actually waiting for an event */
5262 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005263 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005264 }
5265
5266 return mask;
5267}
5268
Olivier Langlois59b735a2021-06-22 05:17:39 -07005269enum {
5270 IO_APOLL_OK,
5271 IO_APOLL_ABORTED,
5272 IO_APOLL_READY
5273};
5274
5275static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005276{
5277 const struct io_op_def *def = &io_op_defs[req->opcode];
5278 struct io_ring_ctx *ctx = req->ctx;
5279 struct async_poll *apoll;
5280 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005281 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005282 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005283
5284 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005285 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005286 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005287 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005288 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005289 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005290
5291 if (def->pollin) {
5292 rw = READ;
5293 mask |= POLLIN | POLLRDNORM;
5294
5295 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5296 if ((req->opcode == IORING_OP_RECVMSG) &&
5297 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5298 mask &= ~POLLIN;
5299 } else {
5300 rw = WRITE;
5301 mask |= POLLOUT | POLLWRNORM;
5302 }
5303
Jens Axboe9dab14b2020-08-25 12:27:50 -06005304 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005305 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005306 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005307
5308 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5309 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005310 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005311 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005312 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005313 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005314 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005315 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005316
5317 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5318 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005319 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005320 if (ret || ipt.error)
5321 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5322
Olivier Langlois236daeae2021-05-31 02:36:37 -04005323 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5324 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005325 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005326}
5327
5328static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005329 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005330 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005331{
Jens Axboeb41e9852020-02-17 09:52:41 -07005332 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005333
Jens Axboe50826202021-02-23 09:02:26 -07005334 if (!poll->head)
5335 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005336 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005337 if (do_cancel)
5338 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005339 if (!list_empty(&poll->wait.entry)) {
5340 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005341 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005342 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005343 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005344 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005345 return do_complete;
5346}
5347
Pavel Begunkov5d709042021-08-09 20:18:13 +01005348static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005349 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005350{
5351 bool do_complete;
5352
Jens Axboed4e7cd32020-08-15 11:44:50 -07005353 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005354 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005355
Jens Axboeb41e9852020-02-17 09:52:41 -07005356 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005357 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005358 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005359 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005360 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005361 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005362 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005363}
5364
Jens Axboe76e1b642020-09-26 15:05:03 -06005365/*
5366 * Returns true if we found and killed one or more poll requests
5367 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005368static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005369 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005370{
Jens Axboe78076bb2019-12-04 19:56:40 -07005371 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005372 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005373 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005374
Jens Axboe79ebeae2021-08-10 15:18:27 -06005375 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005376 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5377 struct hlist_head *list;
5378
5379 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005380 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005381 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005382 posted += io_poll_remove_one(req);
5383 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005384 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005385 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005386
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005387 if (posted)
5388 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005389
5390 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005391}
5392
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005393static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5394 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005395 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005396{
Jens Axboe78076bb2019-12-04 19:56:40 -07005397 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005398 struct io_kiocb *req;
5399
Jens Axboe78076bb2019-12-04 19:56:40 -07005400 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5401 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005402 if (sqe_addr != req->user_data)
5403 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005404 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5405 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005406 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005407 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005408 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005409}
5410
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005411static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5412 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005413 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005414{
5415 struct io_kiocb *req;
5416
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005417 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005418 if (!req)
5419 return -ENOENT;
5420 if (io_poll_remove_one(req))
5421 return 0;
5422
5423 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005424}
5425
Pavel Begunkov9096af32021-04-14 13:38:36 +01005426static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5427 unsigned int flags)
5428{
5429 u32 events;
5430
5431 events = READ_ONCE(sqe->poll32_events);
5432#ifdef __BIG_ENDIAN
5433 events = swahw32(events);
5434#endif
5435 if (!(flags & IORING_POLL_ADD_MULTI))
5436 events |= EPOLLONESHOT;
5437 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5438}
5439
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005440static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005441 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005442{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005443 struct io_poll_update *upd = &req->poll_update;
5444 u32 flags;
5445
Jens Axboe221c5eb2019-01-17 09:41:58 -07005446 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5447 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005448 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005449 return -EINVAL;
5450 flags = READ_ONCE(sqe->len);
5451 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5452 IORING_POLL_ADD_MULTI))
5453 return -EINVAL;
5454 /* meaningless without update */
5455 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005456 return -EINVAL;
5457
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005458 upd->old_user_data = READ_ONCE(sqe->addr);
5459 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5460 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005461
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005462 upd->new_user_data = READ_ONCE(sqe->off);
5463 if (!upd->update_user_data && upd->new_user_data)
5464 return -EINVAL;
5465 if (upd->update_events)
5466 upd->events = io_poll_parse_events(sqe, flags);
5467 else if (sqe->poll32_events)
5468 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005469
Jens Axboe221c5eb2019-01-17 09:41:58 -07005470 return 0;
5471}
5472
Jens Axboe221c5eb2019-01-17 09:41:58 -07005473static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5474 void *key)
5475{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005476 struct io_kiocb *req = wait->private;
5477 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005478
Jens Axboed7718a92020-02-14 22:23:12 -07005479 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005480}
5481
Jens Axboe221c5eb2019-01-17 09:41:58 -07005482static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5483 struct poll_table_struct *p)
5484{
5485 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5486
Jens Axboee8c2bc12020-08-15 18:44:09 -07005487 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005488}
5489
Jens Axboe3529d8c2019-12-19 18:24:38 -07005490static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005491{
5492 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005493 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005494
5495 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5496 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005497 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005498 return -EINVAL;
5499 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005500 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005501 return -EINVAL;
5502
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005503 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005504 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005505 return 0;
5506}
5507
Pavel Begunkov61e98202021-02-10 00:03:08 +00005508static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005509{
5510 struct io_poll_iocb *poll = &req->poll;
5511 struct io_ring_ctx *ctx = req->ctx;
5512 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005513 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005514
Jens Axboed7718a92020-02-14 22:23:12 -07005515 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005516
Jens Axboed7718a92020-02-14 22:23:12 -07005517 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5518 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005519
Jens Axboe8c838782019-03-12 15:48:16 -06005520 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005521 ipt.error = 0;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005522 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005523 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005524 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005525
Jens Axboe8c838782019-03-12 15:48:16 -06005526 if (mask) {
5527 io_cqring_ev_posted(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005528 if (poll->events & EPOLLONESHOT)
5529 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005530 }
Jens Axboe8c838782019-03-12 15:48:16 -06005531 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005532}
5533
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005534static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005535{
5536 struct io_ring_ctx *ctx = req->ctx;
5537 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005538 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005539 int ret;
5540
Jens Axboe79ebeae2021-08-10 15:18:27 -06005541 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005542 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005543 if (!preq) {
5544 ret = -ENOENT;
5545 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005546 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005547
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005548 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5549 completing = true;
5550 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5551 goto err;
5552 }
5553
Jens Axboecb3b200e2021-04-06 09:49:31 -06005554 /*
5555 * Don't allow racy completion with singleshot, as we cannot safely
5556 * update those. For multishot, if we're racing with completion, just
5557 * let completion re-add it.
5558 */
5559 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5560 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5561 ret = -EALREADY;
5562 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005563 }
5564 /* we now have a detached poll request. reissue. */
5565 ret = 0;
5566err:
Jens Axboeb69de282021-03-17 08:37:41 -06005567 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005568 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005569 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005570 io_req_complete(req, ret);
5571 return 0;
5572 }
5573 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005574 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005575 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005576 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005577 preq->poll.events |= IO_POLL_UNMASK;
5578 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005579 if (req->poll_update.update_user_data)
5580 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005581 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005582
Jens Axboeb69de282021-03-17 08:37:41 -06005583 /* complete update request, we're done with it */
5584 io_req_complete(req, ret);
5585
Jens Axboecb3b200e2021-04-06 09:49:31 -06005586 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005587 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005588 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005589 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005590 io_req_complete(preq, ret);
5591 }
Jens Axboeb69de282021-03-17 08:37:41 -06005592 }
5593 return 0;
5594}
5595
Jens Axboe89850fc2021-08-10 15:11:51 -06005596static void io_req_task_timeout(struct io_kiocb *req)
5597{
Jens Axboe89850fc2021-08-10 15:11:51 -06005598 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005599 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005600}
5601
Jens Axboe5262f562019-09-17 12:26:57 -06005602static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5603{
Jens Axboead8a48a2019-11-15 08:49:11 -07005604 struct io_timeout_data *data = container_of(timer,
5605 struct io_timeout_data, timer);
5606 struct io_kiocb *req = data->req;
5607 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005608 unsigned long flags;
5609
Jens Axboe89850fc2021-08-10 15:11:51 -06005610 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005611 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005612 atomic_set(&req->ctx->cq_timeouts,
5613 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005614 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005615
Jens Axboe89850fc2021-08-10 15:11:51 -06005616 req->io_task_work.func = io_req_task_timeout;
5617 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005618 return HRTIMER_NORESTART;
5619}
5620
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005621static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5622 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005623 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005624{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005625 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005626 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005627 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005628
5629 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005630 found = user_data == req->user_data;
5631 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005632 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005633 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005634 if (!found)
5635 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005636
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005637 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005638 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005639 return ERR_PTR(-EALREADY);
5640 list_del_init(&req->timeout.list);
5641 return req;
5642}
5643
5644static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005645 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005646 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005647{
5648 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5649
5650 if (IS_ERR(req))
5651 return PTR_ERR(req);
5652
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005653 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005654 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005655 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005656 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005657}
5658
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005659static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5660 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06005661 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005662{
5663 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5664 struct io_timeout_data *data;
5665
5666 if (IS_ERR(req))
5667 return PTR_ERR(req);
5668
5669 req->timeout.off = 0; /* noseq */
5670 data = req->async_data;
5671 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5672 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5673 data->timer.function = io_timeout_fn;
5674 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5675 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005676}
5677
Jens Axboe3529d8c2019-12-19 18:24:38 -07005678static int io_timeout_remove_prep(struct io_kiocb *req,
5679 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005680{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005681 struct io_timeout_rem *tr = &req->timeout_rem;
5682
Jens Axboeb29472e2019-12-17 18:50:29 -07005683 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5684 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005685 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5686 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005687 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07005688 return -EINVAL;
5689
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005690 tr->addr = READ_ONCE(sqe->addr);
5691 tr->flags = READ_ONCE(sqe->timeout_flags);
5692 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5693 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5694 return -EINVAL;
5695 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5696 return -EFAULT;
5697 } else if (tr->flags) {
5698 /* timeout removal doesn't support flags */
5699 return -EINVAL;
5700 }
5701
Jens Axboeb29472e2019-12-17 18:50:29 -07005702 return 0;
5703}
5704
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005705static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5706{
5707 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5708 : HRTIMER_MODE_REL;
5709}
5710
Jens Axboe11365042019-10-16 09:08:32 -06005711/*
5712 * Remove or update an existing timeout command
5713 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005714static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005715{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005716 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005717 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005718 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005719
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005720 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
5721 spin_lock(&ctx->completion_lock);
5722 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005723 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005724 spin_unlock_irq(&ctx->timeout_lock);
5725 spin_unlock(&ctx->completion_lock);
5726 } else {
5727 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005728 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5729 io_translate_timeout_mode(tr->flags));
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005730 spin_unlock_irq(&ctx->timeout_lock);
5731 }
Jens Axboe11365042019-10-16 09:08:32 -06005732
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005733 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005734 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005735 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06005736 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005737}
5738
Jens Axboe3529d8c2019-12-19 18:24:38 -07005739static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005740 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005741{
Jens Axboead8a48a2019-11-15 08:49:11 -07005742 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005743 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005744 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005745
Jens Axboead8a48a2019-11-15 08:49:11 -07005746 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005747 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005748 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
5749 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06005750 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005751 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005752 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005753 flags = READ_ONCE(sqe->timeout_flags);
5754 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005755 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005756
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005757 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01005758 if (unlikely(off && !req->ctx->off_timeout_used))
5759 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07005760
Jens Axboee8c2bc12020-08-15 18:44:09 -07005761 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005762 return -ENOMEM;
5763
Jens Axboee8c2bc12020-08-15 18:44:09 -07005764 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005765 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005766
5767 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005768 return -EFAULT;
5769
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005770 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005771 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01005772
5773 if (is_timeout_link) {
5774 struct io_submit_link *link = &req->ctx->submit_state.link;
5775
5776 if (!link->head)
5777 return -EINVAL;
5778 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
5779 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01005780 req->timeout.head = link->last;
5781 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01005782 }
Jens Axboead8a48a2019-11-15 08:49:11 -07005783 return 0;
5784}
5785
Pavel Begunkov61e98202021-02-10 00:03:08 +00005786static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005787{
Jens Axboead8a48a2019-11-15 08:49:11 -07005788 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005789 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005790 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005791 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005792
Jens Axboe89850fc2021-08-10 15:11:51 -06005793 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005794
Jens Axboe5262f562019-09-17 12:26:57 -06005795 /*
5796 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005797 * timeout event to be satisfied. If it isn't set, then this is
5798 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005799 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005800 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005801 entry = ctx->timeout_list.prev;
5802 goto add;
5803 }
Jens Axboe5262f562019-09-17 12:26:57 -06005804
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005805 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5806 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005807
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005808 /* Update the last seq here in case io_flush_timeouts() hasn't.
5809 * This is safe because ->completion_lock is held, and submissions
5810 * and completions are never mixed in the same ->completion_lock section.
5811 */
5812 ctx->cq_last_tm_flush = tail;
5813
Jens Axboe5262f562019-09-17 12:26:57 -06005814 /*
5815 * Insertion sort, ensuring the first entry in the list is always
5816 * the one we need first.
5817 */
Jens Axboe5262f562019-09-17 12:26:57 -06005818 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005819 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5820 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005821
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005822 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005823 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005824 /* nxt.seq is behind @tail, otherwise would've been completed */
5825 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005826 break;
5827 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005828add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005829 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005830 data->timer.function = io_timeout_fn;
5831 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06005832 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005833 return 0;
5834}
5835
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005836struct io_cancel_data {
5837 struct io_ring_ctx *ctx;
5838 u64 user_data;
5839};
5840
Jens Axboe62755e32019-10-28 21:49:21 -06005841static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005842{
Jens Axboe62755e32019-10-28 21:49:21 -06005843 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005844 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06005845
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005846 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06005847}
5848
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005849static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
5850 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06005851{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005852 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06005853 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005854 int ret = 0;
5855
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005856 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005857 return -ENOENT;
5858
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005859 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005860 switch (cancel_ret) {
5861 case IO_WQ_CANCEL_OK:
5862 ret = 0;
5863 break;
5864 case IO_WQ_CANCEL_RUNNING:
5865 ret = -EALREADY;
5866 break;
5867 case IO_WQ_CANCEL_NOTFOUND:
5868 ret = -ENOENT;
5869 break;
5870 }
5871
Jens Axboee977d6d2019-11-05 12:39:45 -07005872 return ret;
5873}
5874
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005875static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07005876{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005877 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005878 int ret;
5879
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005880 WARN_ON_ONCE(req->task != current);
5881
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005882 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01005883 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005884 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01005885
5886 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005887 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07005888 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005889 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07005890 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01005891 goto out;
5892 ret = io_poll_cancel(ctx, sqe_addr, false);
5893out:
5894 spin_unlock(&ctx->completion_lock);
5895 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005896}
5897
Jens Axboe3529d8c2019-12-19 18:24:38 -07005898static int io_async_cancel_prep(struct io_kiocb *req,
5899 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005900{
Jens Axboefbf23842019-12-17 18:45:56 -07005901 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005902 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005903 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5904 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005905 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
5906 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07005907 return -EINVAL;
5908
Jens Axboefbf23842019-12-17 18:45:56 -07005909 req->cancel.addr = READ_ONCE(sqe->addr);
5910 return 0;
5911}
5912
Pavel Begunkov61e98202021-02-10 00:03:08 +00005913static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005914{
5915 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005916 u64 sqe_addr = req->cancel.addr;
5917 struct io_tctx_node *node;
5918 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07005919
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005920 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00005921 if (ret != -ENOENT)
5922 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005923
5924 /* slow path, try all io-wq's */
5925 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
5926 ret = -ENOENT;
5927 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
5928 struct io_uring_task *tctx = node->task->io_uring;
5929
Pavel Begunkov58f99372021-03-12 16:25:55 +00005930 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
5931 if (ret != -ENOENT)
5932 break;
5933 }
5934 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00005935done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00005936 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005937 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005938 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005939 return 0;
5940}
5941
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005942static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005943 const struct io_uring_sqe *sqe)
5944{
Daniele Albano61710e42020-07-18 14:15:16 -06005945 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5946 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005947 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005948 return -EINVAL;
5949
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005950 req->rsrc_update.offset = READ_ONCE(sqe->off);
5951 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5952 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005953 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005954 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005955 return 0;
5956}
5957
Pavel Begunkov889fca72021-02-10 00:03:09 +00005958static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005959{
5960 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01005961 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005962 int ret;
5963
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005964 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005965 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005966
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005967 up.offset = req->rsrc_update.offset;
5968 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01005969 up.nr = 0;
5970 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01005971 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005972
5973 mutex_lock(&ctx->uring_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01005974 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01005975 &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005976 mutex_unlock(&ctx->uring_lock);
5977
5978 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005979 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005980 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005981 return 0;
5982}
5983
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005984static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005985{
Jens Axboed625c6e2019-12-17 19:53:05 -07005986 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005987 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005988 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005989 case IORING_OP_READV:
5990 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005991 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005992 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005993 case IORING_OP_WRITEV:
5994 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005995 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005996 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005997 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005998 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005999 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006000 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006001 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006002 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006003 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006004 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006005 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006006 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006007 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006008 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006009 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006010 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006011 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006012 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006013 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006014 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006015 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006016 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006017 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006018 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006019 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006020 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006021 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006022 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006023 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006024 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006025 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006026 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006027 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006028 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006029 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006030 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006031 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006032 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006033 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006034 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006035 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006036 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006037 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006038 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006039 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006040 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006041 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006042 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006043 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006044 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006045 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006046 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006047 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006048 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006049 case IORING_OP_SHUTDOWN:
6050 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006051 case IORING_OP_RENAMEAT:
6052 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006053 case IORING_OP_UNLINKAT:
6054 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006055 case IORING_OP_MKDIRAT:
6056 return io_mkdirat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006057 }
6058
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006059 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6060 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006061 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006062}
6063
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006064static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006065{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006066 if (!io_op_defs[req->opcode].needs_async_setup)
6067 return 0;
6068 if (WARN_ON_ONCE(req->async_data))
6069 return -EFAULT;
6070 if (io_alloc_async_data(req))
6071 return -EAGAIN;
6072
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006073 switch (req->opcode) {
6074 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006075 return io_rw_prep_async(req, READ);
6076 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006077 return io_rw_prep_async(req, WRITE);
6078 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006079 return io_sendmsg_prep_async(req);
6080 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006081 return io_recvmsg_prep_async(req);
6082 case IORING_OP_CONNECT:
6083 return io_connect_prep_async(req);
6084 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006085 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6086 req->opcode);
6087 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006088}
6089
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006090static u32 io_get_sequence(struct io_kiocb *req)
6091{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006092 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006093
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006094 /* need original cached_sq_head, but it was increased for each req */
6095 io_for_each_link(req, req)
6096 seq--;
6097 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006098}
6099
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006100static bool io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006101{
Pavel Begunkov3c199662021-06-15 16:47:57 +01006102 struct io_kiocb *pos;
Jens Axboedef596e2019-01-09 08:59:42 -07006103 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006104 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006105 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006106 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07006107
Pavel Begunkov3c199662021-06-15 16:47:57 +01006108 /*
6109 * If we need to drain a request in the middle of a link, drain the
6110 * head request and the next request/link after the current link.
6111 * Considering sequential execution of links, IOSQE_IO_DRAIN will be
6112 * maintained for every request of our link.
6113 */
6114 if (ctx->drain_next) {
6115 req->flags |= REQ_F_IO_DRAIN;
6116 ctx->drain_next = false;
6117 }
6118 /* not interested in head, start from the first linked */
6119 io_for_each_link(pos, req->link) {
6120 if (pos->flags & REQ_F_IO_DRAIN) {
6121 ctx->drain_next = true;
6122 req->flags |= REQ_F_IO_DRAIN;
6123 break;
6124 }
6125 }
6126
Jens Axboedef596e2019-01-09 08:59:42 -07006127 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006128 if (likely(list_empty_careful(&ctx->defer_list) &&
Pavel Begunkov10c66902021-06-15 16:47:56 +01006129 !(req->flags & REQ_F_IO_DRAIN))) {
6130 ctx->drain_active = false;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006131 return false;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006132 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006133
6134 seq = io_get_sequence(req);
6135 /* Still a chance to pass the sequence check */
6136 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006137 return false;
Jens Axboedef596e2019-01-09 08:59:42 -07006138
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006139 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006140 if (ret)
Pavel Begunkov1b487732021-07-11 22:41:13 +01006141 goto fail;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006142 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006143 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006144 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006145 ret = -ENOMEM;
6146fail:
6147 io_req_complete_failed(req, ret);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006148 return true;
6149 }
Jens Axboe31b51512019-01-18 22:56:34 -07006150
Jens Axboe79ebeae2021-08-10 15:18:27 -06006151 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006152 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006153 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006154 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006155 io_queue_async_work(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006156 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006157 }
6158
6159 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006160 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006161 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006162 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006163 spin_unlock(&ctx->completion_lock);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006164 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006165}
6166
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006167static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006168{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006169 if (req->flags & REQ_F_BUFFER_SELECTED) {
6170 switch (req->opcode) {
6171 case IORING_OP_READV:
6172 case IORING_OP_READ_FIXED:
6173 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006174 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006175 break;
6176 case IORING_OP_RECVMSG:
6177 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006178 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006179 break;
6180 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006181 }
6182
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006183 if (req->flags & REQ_F_NEED_CLEANUP) {
6184 switch (req->opcode) {
6185 case IORING_OP_READV:
6186 case IORING_OP_READ_FIXED:
6187 case IORING_OP_READ:
6188 case IORING_OP_WRITEV:
6189 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006190 case IORING_OP_WRITE: {
6191 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006192
6193 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006194 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006195 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006196 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006197 case IORING_OP_SENDMSG: {
6198 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006199
6200 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006201 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006202 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006203 case IORING_OP_SPLICE:
6204 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006205 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6206 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006207 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006208 case IORING_OP_OPENAT:
6209 case IORING_OP_OPENAT2:
6210 if (req->open.filename)
6211 putname(req->open.filename);
6212 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006213 case IORING_OP_RENAMEAT:
6214 putname(req->rename.oldpath);
6215 putname(req->rename.newpath);
6216 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006217 case IORING_OP_UNLINKAT:
6218 putname(req->unlink.filename);
6219 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006220 case IORING_OP_MKDIRAT:
6221 putname(req->mkdir.filename);
6222 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006223 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006224 }
Jens Axboe75652a302021-04-15 09:52:40 -06006225 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6226 kfree(req->apoll->double_poll);
6227 kfree(req->apoll);
6228 req->apoll = NULL;
6229 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006230 if (req->flags & REQ_F_INFLIGHT) {
6231 struct io_uring_task *tctx = req->task->io_uring;
6232
6233 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006234 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006235 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006236 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006237
6238 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006239}
6240
Pavel Begunkov889fca72021-02-10 00:03:09 +00006241static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006242{
Jens Axboeedafcce2019-01-09 09:16:05 -07006243 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006244 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006245 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006246
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006247 if ((req->flags & REQ_F_CREDS) && req->creds != current_cred())
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006248 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006249
Jens Axboed625c6e2019-12-17 19:53:05 -07006250 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006251 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006252 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006253 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006254 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006255 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006256 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006257 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006258 break;
6259 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006260 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006261 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006262 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006263 break;
6264 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006265 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006266 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006267 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006268 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006269 break;
6270 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006271 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006272 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006273 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006274 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006275 break;
6276 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006277 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006278 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006279 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006280 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006281 break;
6282 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006283 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006284 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006285 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006286 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006287 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006288 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006289 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290 break;
6291 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006292 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006293 break;
6294 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006295 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006296 break;
6297 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006298 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006299 break;
6300 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006301 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006302 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006303 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006304 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006305 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006306 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006307 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006308 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006309 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006310 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006311 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006312 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006313 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006314 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006315 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006316 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006317 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006318 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006319 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006320 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006321 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006322 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006323 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006324 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006325 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006326 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006327 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006328 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006329 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006330 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006331 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006332 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006333 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006334 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006335 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006336 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006337 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006338 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006339 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006340 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006341 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006342 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006343 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006344 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006345 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006346 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006347 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006348 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006349 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006350 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006351 case IORING_OP_MKDIRAT:
6352 ret = io_mkdirat(req, issue_flags);
6353 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006354 default:
6355 ret = -EINVAL;
6356 break;
6357 }
Jens Axboe31b51512019-01-18 22:56:34 -07006358
Jens Axboe5730b272021-02-27 15:57:30 -07006359 if (creds)
6360 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006361 if (ret)
6362 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006363 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006364 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6365 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006366
6367 return 0;
6368}
6369
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006370static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6371{
6372 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6373
6374 req = io_put_req_find_next(req);
6375 return req ? &req->work : NULL;
6376}
6377
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006378static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006379{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006380 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006381 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006382 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006383
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006384 /* one will be dropped by ->io_free_work() after returning to io-wq */
6385 if (!(req->flags & REQ_F_REFCOUNT))
6386 __io_req_set_refcount(req, 2);
6387 else
6388 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006389
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006390 timeout = io_prep_linked_timeout(req);
6391 if (timeout)
6392 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006393
Jens Axboe4014d942021-01-19 15:53:54 -07006394 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006395 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006396
Jens Axboe561fb042019-10-24 07:25:42 -06006397 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006398 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006399 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006400 /*
6401 * We can get EAGAIN for polled IO even though we're
6402 * forcing a sync submission from here, since we can't
6403 * wait for request slots on the block side.
6404 */
6405 if (ret != -EAGAIN)
6406 break;
6407 cond_resched();
6408 } while (1);
6409 }
Jens Axboe31b51512019-01-18 22:56:34 -07006410
Pavel Begunkova3df76982021-02-18 22:32:52 +00006411 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006412 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006413 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006414}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006415
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006416static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006417 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006418{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006419 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006420}
6421
Jens Axboe09bb8392019-03-13 12:39:28 -06006422static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6423 int index)
6424{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006425 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006426
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006427 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006428}
6429
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006430static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006431{
6432 unsigned long file_ptr = (unsigned long) file;
6433
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006434 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006435 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006436 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006437 file_ptr |= FFS_ASYNC_WRITE;
6438 if (S_ISREG(file_inode(file)->i_mode))
6439 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006440 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006441}
6442
Pavel Begunkovac177052021-08-09 13:04:02 +01006443static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6444 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006445{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006446 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006447 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006448
Pavel Begunkovac177052021-08-09 13:04:02 +01006449 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6450 return NULL;
6451 fd = array_index_nospec(fd, ctx->nr_user_files);
6452 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6453 file = (struct file *) (file_ptr & FFS_MASK);
6454 file_ptr &= ~FFS_MASK;
6455 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006456 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006457 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006458 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006459}
6460
Pavel Begunkovac177052021-08-09 13:04:02 +01006461static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006462 struct io_kiocb *req, int fd)
6463{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006464 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006465
6466 trace_io_uring_file_get(ctx, fd);
6467
6468 /* we don't allow fixed io_uring files */
6469 if (file && unlikely(file->f_op == &io_uring_fops))
6470 io_req_track_inflight(req);
6471 return file;
6472}
6473
6474static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006475 struct io_kiocb *req, int fd, bool fixed)
6476{
6477 if (fixed)
6478 return io_file_get_fixed(ctx, req, fd);
6479 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006480 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006481}
6482
Jens Axboe89b263f2021-08-10 15:14:18 -06006483static void io_req_task_link_timeout(struct io_kiocb *req)
6484{
6485 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006486 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006487
6488 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006489 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006490 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006491 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006492 } else {
6493 io_req_complete_post(req, -ETIME, 0);
6494 }
6495}
6496
Jens Axboe2665abf2019-11-05 12:40:47 -07006497static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6498{
Jens Axboead8a48a2019-11-15 08:49:11 -07006499 struct io_timeout_data *data = container_of(timer,
6500 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006501 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006503 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006504
Jens Axboe89b263f2021-08-10 15:14:18 -06006505 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006506 prev = req->timeout.head;
6507 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006508
6509 /*
6510 * We don't expect the list to be empty, that will only happen if we
6511 * race with the completion of the linked work.
6512 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006513 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006514 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006515 if (!req_ref_inc_not_zero(prev))
6516 prev = NULL;
6517 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006518 req->timeout.prev = prev;
6519 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006520
Jens Axboe89b263f2021-08-10 15:14:18 -06006521 req->io_task_work.func = io_req_task_link_timeout;
6522 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006523 return HRTIMER_NORESTART;
6524}
6525
Pavel Begunkovde968c12021-03-19 17:22:33 +00006526static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006527{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006528 struct io_ring_ctx *ctx = req->ctx;
6529
Jens Axboe89b263f2021-08-10 15:14:18 -06006530 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006531 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006532 * If the back reference is NULL, then our linked request finished
6533 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006534 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006535 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006536 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006537
Jens Axboead8a48a2019-11-15 08:49:11 -07006538 data->timer.function = io_link_timeout_fn;
6539 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6540 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006541 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006542 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006543 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006544 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006545}
6546
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006547static void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006548 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006549{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006550 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006551 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006552
Olivier Langlois59b735a2021-06-22 05:17:39 -07006553issue_sqe:
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006554 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006555
6556 /*
6557 * We async punt it if the file wasn't marked NOWAIT, or if the file
6558 * doesn't support non-blocking read/write attempts
6559 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006560 if (likely(!ret)) {
Pavel Begunkove342c802021-01-19 13:32:47 +00006561 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006562 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006563 struct io_submit_state *state = &ctx->submit_state;
Jens Axboee65ef562019-03-12 10:16:44 -06006564
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006565 state->compl_reqs[state->compl_nr++] = req;
6566 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006567 io_submit_flush_completions(ctx);
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006568 return;
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006569 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006570
6571 linked_timeout = io_prep_linked_timeout(req);
6572 if (linked_timeout)
6573 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006574 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006575 linked_timeout = io_prep_linked_timeout(req);
6576
Olivier Langlois59b735a2021-06-22 05:17:39 -07006577 switch (io_arm_poll_handler(req)) {
6578 case IO_APOLL_READY:
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006579 if (linked_timeout)
6580 io_unprep_linked_timeout(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006581 goto issue_sqe;
6582 case IO_APOLL_ABORTED:
Pavel Begunkov18400382021-03-19 17:22:34 +00006583 /*
6584 * Queued up for async execution, worker will release
6585 * submit reference when the iocb is actually submitted.
6586 */
6587 io_queue_async_work(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006588 break;
Pavel Begunkov18400382021-03-19 17:22:34 +00006589 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006590
6591 if (linked_timeout)
6592 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006593 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006594 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006595 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006596}
6597
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006598static inline void io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006599 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006600{
Pavel Begunkov10c66902021-06-15 16:47:56 +01006601 if (unlikely(req->ctx->drain_active) && io_drain_req(req))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006602 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08006603
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006604 if (likely(!(req->flags & REQ_F_FORCE_ASYNC))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006605 __io_queue_sqe(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006606 } else {
6607 int ret = io_req_prep_async(req);
6608
6609 if (unlikely(ret))
6610 io_req_complete_failed(req, ret);
6611 else
6612 io_queue_async_work(req);
Jens Axboece35a472019-12-17 08:04:44 -07006613 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006614}
6615
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006616/*
6617 * Check SQE restrictions (opcode and flags).
6618 *
6619 * Returns 'true' if SQE is allowed, 'false' otherwise.
6620 */
6621static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6622 struct io_kiocb *req,
6623 unsigned int sqe_flags)
6624{
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006625 if (likely(!ctx->restricted))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006626 return true;
6627
6628 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6629 return false;
6630
6631 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6632 ctx->restrictions.sqe_flags_required)
6633 return false;
6634
6635 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6636 ctx->restrictions.sqe_flags_required))
6637 return false;
6638
6639 return true;
6640}
6641
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006642static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006643 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006644 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006645{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006646 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006647 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006648 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006649
Pavel Begunkov864ea922021-08-09 13:04:08 +01006650 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006651 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006652 /* same numerical values with corresponding REQ_F_*, safe to copy */
6653 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006654 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006655 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006656 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006657 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006658
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006659 /* enforce forwards compatibility on users */
Pavel Begunkovdddca222021-04-27 16:13:52 +01006660 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006661 return -EINVAL;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006662 if (unlikely(req->opcode >= IORING_OP_LAST))
6663 return -EINVAL;
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006664 if (!io_check_restriction(ctx, req, sqe_flags))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006665 return -EACCES;
6666
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006667 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6668 !io_op_defs[req->opcode].buffer_select)
6669 return -EOPNOTSUPP;
Pavel Begunkov3c199662021-06-15 16:47:57 +01006670 if (unlikely(sqe_flags & IOSQE_IO_DRAIN))
6671 ctx->drain_active = true;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006672
Jens Axboe003e8dc2021-03-06 09:22:27 -07006673 personality = READ_ONCE(sqe->personality);
6674 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006675 req->creds = xa_load(&ctx->personalities, personality);
6676 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07006677 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006678 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006679 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006680 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006681 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006682
Jens Axboe27926b62020-10-28 09:33:23 -06006683 /*
6684 * Plug now if we have more than 1 IO left after this, and the target
6685 * is potentially a read/write to block based storage.
6686 */
6687 if (!state->plug_started && state->ios_left > 1 &&
6688 io_op_defs[req->opcode].plug) {
6689 blk_start_plug(&state->plug);
6690 state->plug_started = true;
6691 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006692
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006693 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01006694 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01006695 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00006696 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006697 ret = -EBADF;
6698 }
6699
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006700 state->ios_left--;
6701 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006702}
6703
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006704static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006705 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006706 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006707{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006708 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006709 int ret;
6710
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006711 ret = io_init_req(ctx, req, sqe);
6712 if (unlikely(ret)) {
6713fail_req:
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006714 if (link->head) {
6715 /* fail even hard links since we don't submit */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006716 req_set_fail(link->head);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006717 io_req_complete_failed(link->head, -ECANCELED);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006718 link->head = NULL;
6719 }
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006720 io_req_complete_failed(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006721 return ret;
6722 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006723
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006724 ret = io_req_prep(req, sqe);
6725 if (unlikely(ret))
6726 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006727
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006728 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04006729 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
6730 req->flags, true,
6731 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006732
Jens Axboe6c271ce2019-01-10 11:22:30 -07006733 /*
6734 * If we already have a head request, queue this one for async
6735 * submittal once the head completes. If we don't have a head but
6736 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6737 * submitted sync once the chain is complete. If none of those
6738 * conditions are true (normal request), then just queue it.
6739 */
6740 if (link->head) {
6741 struct io_kiocb *head = link->head;
6742
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006743 ret = io_req_prep_async(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006744 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006745 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006746 trace_io_uring_link(ctx, req, head);
6747 link->last->link = req;
6748 link->last = req;
6749
6750 /* last request of a link, enqueue the link */
6751 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
6752 link->head = NULL;
Pavel Begunkov5e159202021-06-14 23:37:26 +01006753 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006754 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006755 } else {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006756 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006757 link->head = req;
6758 link->last = req;
6759 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006760 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006761 }
6762 }
6763
6764 return 0;
6765}
6766
6767/*
6768 * Batched submission is done, ensure local IO is flushed out.
6769 */
6770static void io_submit_state_end(struct io_submit_state *state,
6771 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006772{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006773 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006774 io_queue_sqe(state->link.head);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006775 if (state->compl_nr)
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006776 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006777 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006778 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06006779}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006780
Jens Axboe9e645e112019-05-10 16:07:28 -06006781/*
6782 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006783 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006784static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006785 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006786{
6787 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006788 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006789 /* set only head, no need to init link_last in advance */
6790 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006791}
6792
Jens Axboe193155c2020-02-22 23:22:19 -07006793static void io_commit_sqring(struct io_ring_ctx *ctx)
6794{
Jens Axboe75c6a032020-01-28 10:15:23 -07006795 struct io_rings *rings = ctx->rings;
6796
6797 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006798 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006799 * since once we write the new head, the application could
6800 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006801 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006802 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006803}
6804
Jens Axboe9e645e112019-05-10 16:07:28 -06006805/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01006806 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006807 * that is mapped by userspace. This means that care needs to be taken to
6808 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006809 * being a good citizen. If members of the sqe are validated and then later
6810 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006811 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006812 */
6813static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006814{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01006815 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01006816 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06006817
6818 /*
6819 * The cached sq head (or cq tail) serves two purposes:
6820 *
6821 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006822 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006823 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006824 * though the application is the one updating it.
6825 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01006826 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006827 if (likely(head < ctx->sq_entries))
6828 return &ctx->sq_sqes[head];
6829
6830 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01006831 ctx->cq_extra--;
6832 WRITE_ONCE(ctx->rings->sq_dropped,
6833 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03006834 return NULL;
6835}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006836
Jens Axboe0f212202020-09-13 13:09:39 -06006837static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006838 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006839{
Pavel Begunkov09899b12021-06-14 02:36:22 +01006840 struct io_uring_task *tctx;
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006841 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006842
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006843 /* make sure SQ entry isn't read before tail */
6844 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006845 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6846 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006847
Pavel Begunkov09899b12021-06-14 02:36:22 +01006848 tctx = current->io_uring;
6849 tctx->cached_refs -= nr;
6850 if (unlikely(tctx->cached_refs < 0)) {
6851 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
6852
6853 percpu_counter_add(&tctx->inflight, refill);
6854 refcount_add(refill, &current->usage);
6855 tctx->cached_refs += refill;
6856 }
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006857 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006858
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006859 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006860 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006861 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006862
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006863 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006864 if (unlikely(!req)) {
6865 if (!submitted)
6866 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006867 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006868 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006869 sqe = io_get_sqe(ctx);
6870 if (unlikely(!sqe)) {
6871 kmem_cache_free(req_cachep, req);
6872 break;
6873 }
Jens Axboed3656342019-12-18 09:50:26 -07006874 /* will complete beyond this point, count as submitted */
6875 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006876 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006877 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006878 }
6879
Pavel Begunkov9466f432020-01-25 22:34:01 +03006880 if (unlikely(submitted != nr)) {
6881 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006882 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006883
Pavel Begunkov09899b12021-06-14 02:36:22 +01006884 current->io_uring->cached_refs += unused;
Jens Axboed8a6df12020-10-15 16:24:45 -06006885 percpu_ref_put_many(&ctx->refs, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006886 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006887
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006888 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006889 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6890 io_commit_sqring(ctx);
6891
Jens Axboe6c271ce2019-01-10 11:22:30 -07006892 return submitted;
6893}
6894
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006895static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
6896{
6897 return READ_ONCE(sqd->state);
6898}
6899
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006900static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6901{
6902 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06006903 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07006904 WRITE_ONCE(ctx->rings->sq_flags,
6905 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006906 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006907}
6908
6909static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6910{
Jens Axboe79ebeae2021-08-10 15:18:27 -06006911 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07006912 WRITE_ONCE(ctx->rings->sq_flags,
6913 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006914 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006915}
6916
Xiaoguang Wang08369242020-11-03 14:15:59 +08006917static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006918{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006919 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006920 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006921
Jens Axboec8d1ba52020-09-14 11:07:26 -06006922 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006923 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07006924 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
6925 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06006926
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006927 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6928 unsigned nr_events = 0;
Pavel Begunkov948e1942021-06-24 15:09:55 +01006929 const struct cred *creds = NULL;
6930
6931 if (ctx->sq_creds != current_cred())
6932 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006933
Xiaoguang Wang08369242020-11-03 14:15:59 +08006934 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006935 if (!list_empty(&ctx->iopoll_list))
Pavel Begunkova8576af2021-08-15 10:40:21 +01006936 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006937
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01006938 /*
6939 * Don't submit if refs are dying, good for io_uring_register(),
6940 * but also it is relied upon by io_ring_exit_work()
6941 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00006942 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
6943 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006944 ret = io_submit_sqes(ctx, to_submit);
6945 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006946
Pavel Begunkovacfb3812021-05-16 22:58:03 +01006947 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
6948 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01006949 if (creds)
6950 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01006951 }
Jens Axboe90554202020-09-03 12:12:41 -06006952
Xiaoguang Wang08369242020-11-03 14:15:59 +08006953 return ret;
6954}
6955
6956static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6957{
6958 struct io_ring_ctx *ctx;
6959 unsigned sq_thread_idle = 0;
6960
Pavel Begunkovc9dca272021-03-10 13:13:55 +00006961 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6962 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006963 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006964}
6965
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006966static bool io_sqd_handle_event(struct io_sq_data *sqd)
6967{
6968 bool did_sig = false;
6969 struct ksignal ksig;
6970
6971 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
6972 signal_pending(current)) {
6973 mutex_unlock(&sqd->lock);
6974 if (signal_pending(current))
6975 did_sig = get_signal(&ksig);
6976 cond_resched();
6977 mutex_lock(&sqd->lock);
6978 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006979 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
6980}
6981
Jens Axboe6c271ce2019-01-10 11:22:30 -07006982static int io_sq_thread(void *data)
6983{
Jens Axboe69fb2132020-09-14 11:16:23 -06006984 struct io_sq_data *sqd = data;
6985 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006986 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006987 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08006988 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006989
Pavel Begunkov696ee882021-04-01 09:55:04 +01006990 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006991 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06006992
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006993 if (sqd->sq_cpu != -1)
6994 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
6995 else
6996 set_cpus_allowed_ptr(current, cpu_online_mask);
6997 current->flags |= PF_NO_SETAFFINITY;
6998
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00006999 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007000 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007001 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007002
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007003 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7004 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007005 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007006 timeout = jiffies + sqd->sq_thread_idle;
7007 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007008
Jens Axboee95eee22020-09-08 09:11:32 -06007009 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007010 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007011 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007012
Xiaoguang Wang08369242020-11-03 14:15:59 +08007013 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7014 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007015 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007016 if (io_run_task_work())
7017 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007018
Xiaoguang Wang08369242020-11-03 14:15:59 +08007019 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007020 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007021 if (sqt_spin)
7022 timeout = jiffies + sqd->sq_thread_idle;
7023 continue;
7024 }
7025
Xiaoguang Wang08369242020-11-03 14:15:59 +08007026 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007027 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007028 bool needs_sched = true;
7029
Hao Xu724cb4f2021-04-21 23:19:11 +08007030 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007031 io_ring_set_wakeup_flag(ctx);
7032
Hao Xu724cb4f2021-04-21 23:19:11 +08007033 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7034 !list_empty_careful(&ctx->iopoll_list)) {
7035 needs_sched = false;
7036 break;
7037 }
7038 if (io_sqring_entries(ctx)) {
7039 needs_sched = false;
7040 break;
7041 }
7042 }
7043
7044 if (needs_sched) {
7045 mutex_unlock(&sqd->lock);
7046 schedule();
7047 mutex_lock(&sqd->lock);
7048 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007049 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7050 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007051 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007052
7053 finish_wait(&sqd->wait, &wait);
7054 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007055 }
7056
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007057 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007058 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007059 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007060 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007061 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007062 mutex_unlock(&sqd->lock);
7063
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007064 complete(&sqd->exited);
7065 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007066}
7067
Jens Axboebda52162019-09-24 13:47:15 -06007068struct io_wait_queue {
7069 struct wait_queue_entry wq;
7070 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007071 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007072 unsigned nr_timeouts;
7073};
7074
Pavel Begunkov6c503152021-01-04 20:36:36 +00007075static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007076{
7077 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007078 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007079
7080 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007081 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007082 * started waiting. For timeouts, we always want to return to userspace,
7083 * regardless of event count.
7084 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007085 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007086}
7087
7088static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7089 int wake_flags, void *key)
7090{
7091 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7092 wq);
7093
Pavel Begunkov6c503152021-01-04 20:36:36 +00007094 /*
7095 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7096 * the task, and the next invocation will do it.
7097 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007098 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007099 return autoremove_wake_function(curr, mode, wake_flags, key);
7100 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007101}
7102
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007103static int io_run_task_work_sig(void)
7104{
7105 if (io_run_task_work())
7106 return 1;
7107 if (!signal_pending(current))
7108 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007109 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007110 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007111 return -EINTR;
7112}
7113
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007114/* when returns >0, the caller should retry */
7115static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7116 struct io_wait_queue *iowq,
7117 signed long *timeout)
7118{
7119 int ret;
7120
7121 /* make sure we run task_work before checking for signals */
7122 ret = io_run_task_work_sig();
7123 if (ret || io_should_wake(iowq))
7124 return ret;
7125 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007126 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007127 return 1;
7128
7129 *timeout = schedule_timeout(*timeout);
7130 return !*timeout ? -ETIME : 1;
7131}
7132
Jens Axboe2b188cc2019-01-07 10:46:33 -07007133/*
7134 * Wait until events become available, if we don't already have some. The
7135 * application must reap them itself, as they reside on the shared cq ring.
7136 */
7137static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007138 const sigset_t __user *sig, size_t sigsz,
7139 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007140{
Pavel Begunkov902910992021-08-09 09:07:32 -06007141 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007142 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007143 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7144 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007145
Jens Axboeb41e9852020-02-17 09:52:41 -07007146 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007147 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007148 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007149 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007150 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007151 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007152 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007153
7154 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007155#ifdef CONFIG_COMPAT
7156 if (in_compat_syscall())
7157 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007158 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007159 else
7160#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007161 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007162
Jens Axboe2b188cc2019-01-07 10:46:33 -07007163 if (ret)
7164 return ret;
7165 }
7166
Hao Xuc73ebb62020-11-03 10:54:37 +08007167 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007168 struct timespec64 ts;
7169
Hao Xuc73ebb62020-11-03 10:54:37 +08007170 if (get_timespec64(&ts, uts))
7171 return -EFAULT;
7172 timeout = timespec64_to_jiffies(&ts);
7173 }
7174
Pavel Begunkov902910992021-08-09 09:07:32 -06007175 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7176 iowq.wq.private = current;
7177 INIT_LIST_HEAD(&iowq.wq.entry);
7178 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007179 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007180 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007181
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007182 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007183 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007184 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007185 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007186 ret = -EBUSY;
7187 break;
7188 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007189 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007190 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007191 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007192 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007193 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007194 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007195
Jens Axboeb7db41c2020-07-04 08:55:50 -06007196 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007197
Hristo Venev75b28af2019-08-26 17:23:46 +00007198 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007199}
7200
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007201static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007202{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007203 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007204
7205 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007206 kfree(table[i]);
7207 kfree(table);
7208}
7209
7210static void **io_alloc_page_table(size_t size)
7211{
7212 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7213 size_t init_size = size;
7214 void **table;
7215
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007216 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007217 if (!table)
7218 return NULL;
7219
7220 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007221 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007222
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007223 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007224 if (!table[i]) {
7225 io_free_page_table(table, init_size);
7226 return NULL;
7227 }
7228 size -= this_size;
7229 }
7230 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007231}
7232
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007233static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7234{
7235 percpu_ref_exit(&ref_node->refs);
7236 kfree(ref_node);
7237}
7238
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007239static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7240{
7241 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7242 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7243 unsigned long flags;
7244 bool first_add = false;
7245
7246 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7247 node->done = true;
7248
7249 while (!list_empty(&ctx->rsrc_ref_list)) {
7250 node = list_first_entry(&ctx->rsrc_ref_list,
7251 struct io_rsrc_node, node);
7252 /* recycle ref nodes in order */
7253 if (!node->done)
7254 break;
7255 list_del(&node->node);
7256 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7257 }
7258 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7259
7260 if (first_add)
7261 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7262}
7263
7264static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7265{
7266 struct io_rsrc_node *ref_node;
7267
7268 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7269 if (!ref_node)
7270 return NULL;
7271
7272 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7273 0, GFP_KERNEL)) {
7274 kfree(ref_node);
7275 return NULL;
7276 }
7277 INIT_LIST_HEAD(&ref_node->node);
7278 INIT_LIST_HEAD(&ref_node->rsrc_list);
7279 ref_node->done = false;
7280 return ref_node;
7281}
7282
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007283static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7284 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007285{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007286 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7287 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007288
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007289 if (data_to_kill) {
7290 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007291
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007292 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007293 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007294 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007295 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007296
Pavel Begunkov3e942492021-04-11 01:46:34 +01007297 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007298 percpu_ref_kill(&rsrc_node->refs);
7299 ctx->rsrc_node = NULL;
7300 }
7301
7302 if (!ctx->rsrc_node) {
7303 ctx->rsrc_node = ctx->rsrc_backup_node;
7304 ctx->rsrc_backup_node = NULL;
7305 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007306}
7307
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007308static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007309{
7310 if (ctx->rsrc_backup_node)
7311 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007312 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007313 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7314}
7315
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007316static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007317{
7318 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007319
Pavel Begunkov215c3902021-04-01 15:43:48 +01007320 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007321 if (data->quiesce)
7322 return -ENXIO;
7323
7324 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007325 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007326 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007327 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007328 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007329 io_rsrc_node_switch(ctx, data);
7330
Pavel Begunkov3e942492021-04-11 01:46:34 +01007331 /* kill initial ref, already quiesced if zero */
7332 if (atomic_dec_and_test(&data->refs))
7333 break;
Jens Axboec018db42021-08-09 08:15:50 -06007334 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007335 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007336 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007337 if (!ret) {
7338 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007339 break;
Jens Axboec018db42021-08-09 08:15:50 -06007340 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007341
Pavel Begunkov3e942492021-04-11 01:46:34 +01007342 atomic_inc(&data->refs);
7343 /* wait for all works potentially completing data->done */
7344 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007345 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007346
Hao Xu8bad28d2021-02-19 17:19:36 +08007347 ret = io_run_task_work_sig();
7348 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007349 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007350 data->quiesce = false;
7351
Hao Xu8bad28d2021-02-19 17:19:36 +08007352 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007353}
7354
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007355static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7356{
7357 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7358 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7359
7360 return &data->tags[table_idx][off];
7361}
7362
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007363static void io_rsrc_data_free(struct io_rsrc_data *data)
7364{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007365 size_t size = data->nr * sizeof(data->tags[0][0]);
7366
7367 if (data->tags)
7368 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007369 kfree(data);
7370}
7371
Pavel Begunkovd878c812021-06-14 02:36:18 +01007372static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7373 u64 __user *utags, unsigned nr,
7374 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007375{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007376 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007377 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007378 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007379
7380 data = kzalloc(sizeof(*data), GFP_KERNEL);
7381 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007382 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007383 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007384 if (!data->tags) {
7385 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007386 return -ENOMEM;
7387 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007388
7389 data->nr = nr;
7390 data->ctx = ctx;
7391 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007392 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007393 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007394 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007395 u64 *tag_slot = io_get_tag_slot(data, i);
7396
7397 if (copy_from_user(tag_slot, &utags[i],
7398 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007399 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007400 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007401 }
7402
Pavel Begunkov3e942492021-04-11 01:46:34 +01007403 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007404 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007405 *pdata = data;
7406 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007407fail:
7408 io_rsrc_data_free(data);
7409 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007410}
7411
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007412static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7413{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007414 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7415 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007416 return !!table->files;
7417}
7418
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007419static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007420{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007421 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007422 table->files = NULL;
7423}
7424
Jens Axboe2b188cc2019-01-07 10:46:33 -07007425static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7426{
7427#if defined(CONFIG_UNIX)
7428 if (ctx->ring_sock) {
7429 struct sock *sock = ctx->ring_sock->sk;
7430 struct sk_buff *skb;
7431
7432 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7433 kfree_skb(skb);
7434 }
7435#else
7436 int i;
7437
7438 for (i = 0; i < ctx->nr_user_files; i++) {
7439 struct file *file;
7440
7441 file = io_file_from_index(ctx, i);
7442 if (file)
7443 fput(file);
7444 }
7445#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007446 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007447 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007448 ctx->file_data = NULL;
7449 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007450}
7451
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007452static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7453{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007454 int ret;
7455
Pavel Begunkov08480402021-04-13 02:58:38 +01007456 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007457 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007458 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7459 if (!ret)
7460 __io_sqe_files_unregister(ctx);
7461 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007462}
7463
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007464static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007465 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007466{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007467 WARN_ON_ONCE(sqd->thread == current);
7468
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007469 /*
7470 * Do the dance but not conditional clear_bit() because it'd race with
7471 * other threads incrementing park_pending and setting the bit.
7472 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007473 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007474 if (atomic_dec_return(&sqd->park_pending))
7475 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007476 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007477}
7478
Jens Axboe86e0d672021-03-05 08:44:39 -07007479static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007480 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007481{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007482 WARN_ON_ONCE(sqd->thread == current);
7483
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007484 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007485 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007486 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007487 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007488 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007489}
7490
7491static void io_sq_thread_stop(struct io_sq_data *sqd)
7492{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007493 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007494 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007495
Jens Axboe05962f92021-03-06 13:58:48 -07007496 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007497 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007498 if (sqd->thread)
7499 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007500 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007501 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007502}
7503
Jens Axboe534ca6d2020-09-02 13:52:19 -06007504static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007505{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007506 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007507 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7508
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007509 io_sq_thread_stop(sqd);
7510 kfree(sqd);
7511 }
7512}
7513
7514static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7515{
7516 struct io_sq_data *sqd = ctx->sq_data;
7517
7518 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007519 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007520 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007521 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007522 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007523
7524 io_put_sq_data(sqd);
7525 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007526 }
7527}
7528
Jens Axboeaa061652020-09-02 14:50:27 -06007529static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7530{
7531 struct io_ring_ctx *ctx_attach;
7532 struct io_sq_data *sqd;
7533 struct fd f;
7534
7535 f = fdget(p->wq_fd);
7536 if (!f.file)
7537 return ERR_PTR(-ENXIO);
7538 if (f.file->f_op != &io_uring_fops) {
7539 fdput(f);
7540 return ERR_PTR(-EINVAL);
7541 }
7542
7543 ctx_attach = f.file->private_data;
7544 sqd = ctx_attach->sq_data;
7545 if (!sqd) {
7546 fdput(f);
7547 return ERR_PTR(-EINVAL);
7548 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007549 if (sqd->task_tgid != current->tgid) {
7550 fdput(f);
7551 return ERR_PTR(-EPERM);
7552 }
Jens Axboeaa061652020-09-02 14:50:27 -06007553
7554 refcount_inc(&sqd->refs);
7555 fdput(f);
7556 return sqd;
7557}
7558
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007559static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7560 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007561{
7562 struct io_sq_data *sqd;
7563
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007564 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007565 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7566 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007567 if (!IS_ERR(sqd)) {
7568 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007569 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007570 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007571 /* fall through for EPERM case, setup new sqd/task */
7572 if (PTR_ERR(sqd) != -EPERM)
7573 return sqd;
7574 }
Jens Axboeaa061652020-09-02 14:50:27 -06007575
Jens Axboe534ca6d2020-09-02 13:52:19 -06007576 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7577 if (!sqd)
7578 return ERR_PTR(-ENOMEM);
7579
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007580 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007581 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007582 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007583 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007584 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007585 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007586 return sqd;
7587}
7588
Jens Axboe6b063142019-01-10 22:13:58 -07007589#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007590/*
7591 * Ensure the UNIX gc is aware of our file set, so we are certain that
7592 * the io_uring can be safely unregistered on process exit, even if we have
7593 * loops in the file referencing.
7594 */
7595static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7596{
7597 struct sock *sk = ctx->ring_sock->sk;
7598 struct scm_fp_list *fpl;
7599 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007600 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007601
Jens Axboe6b063142019-01-10 22:13:58 -07007602 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7603 if (!fpl)
7604 return -ENOMEM;
7605
7606 skb = alloc_skb(0, GFP_KERNEL);
7607 if (!skb) {
7608 kfree(fpl);
7609 return -ENOMEM;
7610 }
7611
7612 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007613
Jens Axboe08a45172019-10-03 08:11:03 -06007614 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007615 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007616 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007617 struct file *file = io_file_from_index(ctx, i + offset);
7618
7619 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007620 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007621 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007622 unix_inflight(fpl->user, fpl->fp[nr_files]);
7623 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007624 }
7625
Jens Axboe08a45172019-10-03 08:11:03 -06007626 if (nr_files) {
7627 fpl->max = SCM_MAX_FD;
7628 fpl->count = nr_files;
7629 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007630 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007631 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7632 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007633
Jens Axboe08a45172019-10-03 08:11:03 -06007634 for (i = 0; i < nr_files; i++)
7635 fput(fpl->fp[i]);
7636 } else {
7637 kfree_skb(skb);
7638 kfree(fpl);
7639 }
Jens Axboe6b063142019-01-10 22:13:58 -07007640
7641 return 0;
7642}
7643
7644/*
7645 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7646 * causes regular reference counting to break down. We rely on the UNIX
7647 * garbage collection to take care of this problem for us.
7648 */
7649static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7650{
7651 unsigned left, total;
7652 int ret = 0;
7653
7654 total = 0;
7655 left = ctx->nr_user_files;
7656 while (left) {
7657 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007658
7659 ret = __io_sqe_files_scm(ctx, this_files, total);
7660 if (ret)
7661 break;
7662 left -= this_files;
7663 total += this_files;
7664 }
7665
7666 if (!ret)
7667 return 0;
7668
7669 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007670 struct file *file = io_file_from_index(ctx, total);
7671
7672 if (file)
7673 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007674 total++;
7675 }
7676
7677 return ret;
7678}
7679#else
7680static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7681{
7682 return 0;
7683}
7684#endif
7685
Pavel Begunkov47e90392021-04-01 15:43:56 +01007686static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007687{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007688 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007689#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007690 struct sock *sock = ctx->ring_sock->sk;
7691 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7692 struct sk_buff *skb;
7693 int i;
7694
7695 __skb_queue_head_init(&list);
7696
7697 /*
7698 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7699 * remove this entry and rearrange the file array.
7700 */
7701 skb = skb_dequeue(head);
7702 while (skb) {
7703 struct scm_fp_list *fp;
7704
7705 fp = UNIXCB(skb).fp;
7706 for (i = 0; i < fp->count; i++) {
7707 int left;
7708
7709 if (fp->fp[i] != file)
7710 continue;
7711
7712 unix_notinflight(fp->user, fp->fp[i]);
7713 left = fp->count - 1 - i;
7714 if (left) {
7715 memmove(&fp->fp[i], &fp->fp[i + 1],
7716 left * sizeof(struct file *));
7717 }
7718 fp->count--;
7719 if (!fp->count) {
7720 kfree_skb(skb);
7721 skb = NULL;
7722 } else {
7723 __skb_queue_tail(&list, skb);
7724 }
7725 fput(file);
7726 file = NULL;
7727 break;
7728 }
7729
7730 if (!file)
7731 break;
7732
7733 __skb_queue_tail(&list, skb);
7734
7735 skb = skb_dequeue(head);
7736 }
7737
7738 if (skb_peek(&list)) {
7739 spin_lock_irq(&head->lock);
7740 while ((skb = __skb_dequeue(&list)) != NULL)
7741 __skb_queue_tail(head, skb);
7742 spin_unlock_irq(&head->lock);
7743 }
7744#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007745 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007746#endif
7747}
7748
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007749static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007750{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007751 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007752 struct io_ring_ctx *ctx = rsrc_data->ctx;
7753 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007754
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007755 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7756 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007757
7758 if (prsrc->tag) {
7759 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007760
7761 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007762 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007763 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01007764 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007765 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007766 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007767 io_cqring_ev_posted(ctx);
7768 io_ring_submit_unlock(ctx, lock_ring);
7769 }
7770
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007771 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007772 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007773 }
7774
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007775 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01007776 if (atomic_dec_and_test(&rsrc_data->refs))
7777 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007778}
7779
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007780static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007781{
7782 struct io_ring_ctx *ctx;
7783 struct llist_node *node;
7784
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007785 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7786 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007787
7788 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007789 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007790 struct llist_node *next = node->next;
7791
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007792 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007793 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007794 node = next;
7795 }
7796}
7797
Jens Axboe05f3fb32019-12-09 11:22:50 -07007798static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01007799 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007800{
7801 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007802 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007803 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007804 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007805
7806 if (ctx->file_data)
7807 return -EBUSY;
7808 if (!nr_args)
7809 return -EINVAL;
7810 if (nr_args > IORING_MAX_FIXED_FILES)
7811 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01007812 if (nr_args > rlimit(RLIMIT_NOFILE))
7813 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007814 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007815 if (ret)
7816 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007817 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
7818 &ctx->file_data);
7819 if (ret)
7820 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007821
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007822 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007823 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007824 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007825
Jens Axboe05f3fb32019-12-09 11:22:50 -07007826 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01007827 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007828 ret = -EFAULT;
7829 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007830 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007831 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01007832 if (fd == -1) {
7833 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007834 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01007835 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007836 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007837 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007838
Jens Axboe05f3fb32019-12-09 11:22:50 -07007839 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007840 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007841 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007842 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007843
7844 /*
7845 * Don't allow io_uring instances to be registered. If UNIX
7846 * isn't enabled, then this causes a reference cycle and this
7847 * instance can never get freed. If UNIX is enabled we'll
7848 * handle it just fine, but there's still no point in allowing
7849 * a ring fd as it doesn't support regular read/write anyway.
7850 */
7851 if (file->f_op == &io_uring_fops) {
7852 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007853 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007854 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007855 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007856 }
7857
Jens Axboe05f3fb32019-12-09 11:22:50 -07007858 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007859 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01007860 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007861 return ret;
7862 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007863
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007864 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007865 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007866out_fput:
7867 for (i = 0; i < ctx->nr_user_files; i++) {
7868 file = io_file_from_index(ctx, i);
7869 if (file)
7870 fput(file);
7871 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007872 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007873 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007874out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007875 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007876 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007877 return ret;
7878}
7879
Jens Axboec3a31e62019-10-03 13:59:56 -06007880static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7881 int index)
7882{
7883#if defined(CONFIG_UNIX)
7884 struct sock *sock = ctx->ring_sock->sk;
7885 struct sk_buff_head *head = &sock->sk_receive_queue;
7886 struct sk_buff *skb;
7887
7888 /*
7889 * See if we can merge this file into an existing skb SCM_RIGHTS
7890 * file set. If there's no room, fall back to allocating a new skb
7891 * and filling it in.
7892 */
7893 spin_lock_irq(&head->lock);
7894 skb = skb_peek(head);
7895 if (skb) {
7896 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7897
7898 if (fpl->count < SCM_MAX_FD) {
7899 __skb_unlink(skb, head);
7900 spin_unlock_irq(&head->lock);
7901 fpl->fp[fpl->count] = get_file(file);
7902 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7903 fpl->count++;
7904 spin_lock_irq(&head->lock);
7905 __skb_queue_head(head, skb);
7906 } else {
7907 skb = NULL;
7908 }
7909 }
7910 spin_unlock_irq(&head->lock);
7911
7912 if (skb) {
7913 fput(file);
7914 return 0;
7915 }
7916
7917 return __io_sqe_files_scm(ctx, 1, index);
7918#else
7919 return 0;
7920#endif
7921}
7922
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007923static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
Pavel Begunkove7c78372021-04-01 15:43:45 +01007924 struct io_rsrc_node *node, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007925{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007926 struct io_rsrc_put *prsrc;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007927
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007928 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7929 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007930 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007931
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007932 prsrc->tag = *io_get_tag_slot(data, idx);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007933 prsrc->rsrc = rsrc;
Pavel Begunkove7c78372021-04-01 15:43:45 +01007934 list_add(&prsrc->list, &node->rsrc_list);
Hillf Dantona5318d32020-03-23 17:47:15 +08007935 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007936}
7937
7938static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007939 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007940 unsigned nr_args)
7941{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007942 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007943 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007944 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01007945 struct io_fixed_file *file_slot;
7946 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007947 int fd, i, err = 0;
7948 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007949 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007950
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007951 if (!ctx->file_data)
7952 return -ENXIO;
7953 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06007954 return -EINVAL;
7955
Pavel Begunkov67973b92021-01-26 13:51:09 +00007956 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007957 u64 tag = 0;
7958
7959 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
7960 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007961 err = -EFAULT;
7962 break;
7963 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007964 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
7965 err = -EINVAL;
7966 break;
7967 }
noah4e0377a2021-01-26 15:23:28 -05007968 if (fd == IORING_REGISTER_FILES_SKIP)
7969 continue;
7970
Pavel Begunkov67973b92021-01-26 13:51:09 +00007971 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007972 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007973
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01007974 if (file_slot->file_ptr) {
7975 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007976 err = io_queue_rsrc_removal(data, up->offset + done,
7977 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08007978 if (err)
7979 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01007980 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007981 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007982 }
7983 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007984 file = fget(fd);
7985 if (!file) {
7986 err = -EBADF;
7987 break;
7988 }
7989 /*
7990 * Don't allow io_uring instances to be registered. If
7991 * UNIX isn't enabled, then this causes a reference
7992 * cycle and this instance can never get freed. If UNIX
7993 * is enabled we'll handle it just fine, but there's
7994 * still no point in allowing a ring fd as it doesn't
7995 * support regular read/write anyway.
7996 */
7997 if (file->f_op == &io_uring_fops) {
7998 fput(file);
7999 err = -EBADF;
8000 break;
8001 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008002 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008003 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008004 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008005 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008006 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008007 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008008 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008009 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008010 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008011 }
8012
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008013 if (needs_switch)
8014 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008015 return done ? done : err;
8016}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008017
Jens Axboe685fe7f2021-03-08 09:37:51 -07008018static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8019 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008020{
Jens Axboee9418942021-02-19 12:33:30 -07008021 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008022 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008023 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008024
Yang Yingliang362a9e62021-07-20 16:38:05 +08008025 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008026 hash = ctx->hash_map;
8027 if (!hash) {
8028 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008029 if (!hash) {
8030 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008031 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008032 }
Jens Axboee9418942021-02-19 12:33:30 -07008033 refcount_set(&hash->refs, 1);
8034 init_waitqueue_head(&hash->wait);
8035 ctx->hash_map = hash;
8036 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008037 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008038
8039 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008040 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008041 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008042 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008043
Jens Axboed25e3a32021-02-16 11:41:41 -07008044 /* Do QD, or 4 * CPUS, whatever is smallest */
8045 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008046
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008047 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008048}
8049
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008050static int io_uring_alloc_task_context(struct task_struct *task,
8051 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008052{
8053 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008054 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008055
Pavel Begunkov09899b12021-06-14 02:36:22 +01008056 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008057 if (unlikely(!tctx))
8058 return -ENOMEM;
8059
Jens Axboed8a6df12020-10-15 16:24:45 -06008060 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8061 if (unlikely(ret)) {
8062 kfree(tctx);
8063 return ret;
8064 }
8065
Jens Axboe685fe7f2021-03-08 09:37:51 -07008066 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008067 if (IS_ERR(tctx->io_wq)) {
8068 ret = PTR_ERR(tctx->io_wq);
8069 percpu_counter_destroy(&tctx->inflight);
8070 kfree(tctx);
8071 return ret;
8072 }
8073
Jens Axboe0f212202020-09-13 13:09:39 -06008074 xa_init(&tctx->xa);
8075 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008076 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008077 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008078 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008079 spin_lock_init(&tctx->task_lock);
8080 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008081 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008082 return 0;
8083}
8084
8085void __io_uring_free(struct task_struct *tsk)
8086{
8087 struct io_uring_task *tctx = tsk->io_uring;
8088
8089 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008090 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008091 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008092
Jens Axboed8a6df12020-10-15 16:24:45 -06008093 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008094 kfree(tctx);
8095 tsk->io_uring = NULL;
8096}
8097
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008098static int io_sq_offload_create(struct io_ring_ctx *ctx,
8099 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008100{
8101 int ret;
8102
Jens Axboed25e3a32021-02-16 11:41:41 -07008103 /* Retain compatibility with failing for an invalid attach attempt */
8104 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8105 IORING_SETUP_ATTACH_WQ) {
8106 struct fd f;
8107
8108 f = fdget(p->wq_fd);
8109 if (!f.file)
8110 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008111 if (f.file->f_op != &io_uring_fops) {
8112 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008113 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008114 }
8115 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008116 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008117 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008118 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008119 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008120 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008121
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008122 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008123 if (IS_ERR(sqd)) {
8124 ret = PTR_ERR(sqd);
8125 goto err;
8126 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008127
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008128 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008129 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008130 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8131 if (!ctx->sq_thread_idle)
8132 ctx->sq_thread_idle = HZ;
8133
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008134 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008135 list_add(&ctx->sqd_list, &sqd->ctx_list);
8136 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008137 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008138 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008139 io_sq_thread_unpark(sqd);
8140
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008141 if (ret < 0)
8142 goto err;
8143 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008144 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008145
Jens Axboe6c271ce2019-01-10 11:22:30 -07008146 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008147 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008148
Jens Axboe917257d2019-04-13 09:28:55 -06008149 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008150 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008151 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008152 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008153 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008154 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008155 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008156
8157 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008158 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008159 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8160 if (IS_ERR(tsk)) {
8161 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008162 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008163 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008164
Jens Axboe46fe18b2021-03-04 12:39:36 -07008165 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008166 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008167 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008168 if (ret)
8169 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008170 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8171 /* Can't have SQ_AFF without SQPOLL */
8172 ret = -EINVAL;
8173 goto err;
8174 }
8175
Jens Axboe2b188cc2019-01-07 10:46:33 -07008176 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008177err_sqpoll:
8178 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008179err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008180 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008181 return ret;
8182}
8183
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008184static inline void __io_unaccount_mem(struct user_struct *user,
8185 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008186{
8187 atomic_long_sub(nr_pages, &user->locked_vm);
8188}
8189
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008190static inline int __io_account_mem(struct user_struct *user,
8191 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008192{
8193 unsigned long page_limit, cur_pages, new_pages;
8194
8195 /* Don't allow more pages than we can safely lock */
8196 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8197
8198 do {
8199 cur_pages = atomic_long_read(&user->locked_vm);
8200 new_pages = cur_pages + nr_pages;
8201 if (new_pages > page_limit)
8202 return -ENOMEM;
8203 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8204 new_pages) != cur_pages);
8205
8206 return 0;
8207}
8208
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008209static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008210{
Jens Axboe62e398b2021-02-21 16:19:37 -07008211 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008212 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008213
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008214 if (ctx->mm_account)
8215 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008216}
8217
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008218static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008219{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008220 int ret;
8221
Jens Axboe62e398b2021-02-21 16:19:37 -07008222 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008223 ret = __io_account_mem(ctx->user, nr_pages);
8224 if (ret)
8225 return ret;
8226 }
8227
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008228 if (ctx->mm_account)
8229 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008230
8231 return 0;
8232}
8233
Jens Axboe2b188cc2019-01-07 10:46:33 -07008234static void io_mem_free(void *ptr)
8235{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008236 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008237
Mark Rutland52e04ef2019-04-30 17:30:21 +01008238 if (!ptr)
8239 return;
8240
8241 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008242 if (put_page_testzero(page))
8243 free_compound_page(page);
8244}
8245
8246static void *io_mem_alloc(size_t size)
8247{
8248 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008249 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008250
8251 return (void *) __get_free_pages(gfp_flags, get_order(size));
8252}
8253
Hristo Venev75b28af2019-08-26 17:23:46 +00008254static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8255 size_t *sq_offset)
8256{
8257 struct io_rings *rings;
8258 size_t off, sq_array_size;
8259
8260 off = struct_size(rings, cqes, cq_entries);
8261 if (off == SIZE_MAX)
8262 return SIZE_MAX;
8263
8264#ifdef CONFIG_SMP
8265 off = ALIGN(off, SMP_CACHE_BYTES);
8266 if (off == 0)
8267 return SIZE_MAX;
8268#endif
8269
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008270 if (sq_offset)
8271 *sq_offset = off;
8272
Hristo Venev75b28af2019-08-26 17:23:46 +00008273 sq_array_size = array_size(sizeof(u32), sq_entries);
8274 if (sq_array_size == SIZE_MAX)
8275 return SIZE_MAX;
8276
8277 if (check_add_overflow(off, sq_array_size, &off))
8278 return SIZE_MAX;
8279
Hristo Venev75b28af2019-08-26 17:23:46 +00008280 return off;
8281}
8282
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008283static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008284{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008285 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008286 unsigned int i;
8287
Pavel Begunkov62248432021-04-28 13:11:29 +01008288 if (imu != ctx->dummy_ubuf) {
8289 for (i = 0; i < imu->nr_bvecs; i++)
8290 unpin_user_page(imu->bvec[i].bv_page);
8291 if (imu->acct_pages)
8292 io_unaccount_mem(ctx, imu->acct_pages);
8293 kvfree(imu);
8294 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008295 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008296}
8297
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008298static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8299{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008300 io_buffer_unmap(ctx, &prsrc->buf);
8301 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008302}
8303
8304static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008305{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008306 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008307
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008308 for (i = 0; i < ctx->nr_user_bufs; i++)
8309 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008310 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008311 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008312 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008313 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008314 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008315}
8316
Jens Axboeedafcce2019-01-09 09:16:05 -07008317static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8318{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008319 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008320
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008321 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008322 return -ENXIO;
8323
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008324 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8325 if (!ret)
8326 __io_sqe_buffers_unregister(ctx);
8327 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008328}
8329
8330static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8331 void __user *arg, unsigned index)
8332{
8333 struct iovec __user *src;
8334
8335#ifdef CONFIG_COMPAT
8336 if (ctx->compat) {
8337 struct compat_iovec __user *ciovs;
8338 struct compat_iovec ciov;
8339
8340 ciovs = (struct compat_iovec __user *) arg;
8341 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8342 return -EFAULT;
8343
Jens Axboed55e5f52019-12-11 16:12:15 -07008344 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008345 dst->iov_len = ciov.iov_len;
8346 return 0;
8347 }
8348#endif
8349 src = (struct iovec __user *) arg;
8350 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8351 return -EFAULT;
8352 return 0;
8353}
8354
Jens Axboede293932020-09-17 16:19:16 -06008355/*
8356 * Not super efficient, but this is just a registration time. And we do cache
8357 * the last compound head, so generally we'll only do a full search if we don't
8358 * match that one.
8359 *
8360 * We check if the given compound head page has already been accounted, to
8361 * avoid double accounting it. This allows us to account the full size of the
8362 * page, not just the constituent pages of a huge page.
8363 */
8364static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8365 int nr_pages, struct page *hpage)
8366{
8367 int i, j;
8368
8369 /* check current page array */
8370 for (i = 0; i < nr_pages; i++) {
8371 if (!PageCompound(pages[i]))
8372 continue;
8373 if (compound_head(pages[i]) == hpage)
8374 return true;
8375 }
8376
8377 /* check previously registered pages */
8378 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008379 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008380
8381 for (j = 0; j < imu->nr_bvecs; j++) {
8382 if (!PageCompound(imu->bvec[j].bv_page))
8383 continue;
8384 if (compound_head(imu->bvec[j].bv_page) == hpage)
8385 return true;
8386 }
8387 }
8388
8389 return false;
8390}
8391
8392static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8393 int nr_pages, struct io_mapped_ubuf *imu,
8394 struct page **last_hpage)
8395{
8396 int i, ret;
8397
Pavel Begunkov216e5832021-05-29 12:01:02 +01008398 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008399 for (i = 0; i < nr_pages; i++) {
8400 if (!PageCompound(pages[i])) {
8401 imu->acct_pages++;
8402 } else {
8403 struct page *hpage;
8404
8405 hpage = compound_head(pages[i]);
8406 if (hpage == *last_hpage)
8407 continue;
8408 *last_hpage = hpage;
8409 if (headpage_already_acct(ctx, pages, i, hpage))
8410 continue;
8411 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8412 }
8413 }
8414
8415 if (!imu->acct_pages)
8416 return 0;
8417
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008418 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008419 if (ret)
8420 imu->acct_pages = 0;
8421 return ret;
8422}
8423
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008424static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008425 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008426 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008427{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008428 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008429 struct vm_area_struct **vmas = NULL;
8430 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008431 unsigned long off, start, end, ubuf;
8432 size_t size;
8433 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008434
Pavel Begunkov62248432021-04-28 13:11:29 +01008435 if (!iov->iov_base) {
8436 *pimu = ctx->dummy_ubuf;
8437 return 0;
8438 }
8439
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008440 ubuf = (unsigned long) iov->iov_base;
8441 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8442 start = ubuf >> PAGE_SHIFT;
8443 nr_pages = end - start;
8444
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008445 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008446 ret = -ENOMEM;
8447
8448 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8449 if (!pages)
8450 goto done;
8451
8452 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8453 GFP_KERNEL);
8454 if (!vmas)
8455 goto done;
8456
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008457 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008458 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008459 goto done;
8460
8461 ret = 0;
8462 mmap_read_lock(current->mm);
8463 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8464 pages, vmas);
8465 if (pret == nr_pages) {
8466 /* don't support file backed memory */
8467 for (i = 0; i < nr_pages; i++) {
8468 struct vm_area_struct *vma = vmas[i];
8469
Pavel Begunkov40dad762021-06-09 15:26:54 +01008470 if (vma_is_shmem(vma))
8471 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008472 if (vma->vm_file &&
8473 !is_file_hugepages(vma->vm_file)) {
8474 ret = -EOPNOTSUPP;
8475 break;
8476 }
8477 }
8478 } else {
8479 ret = pret < 0 ? pret : -EFAULT;
8480 }
8481 mmap_read_unlock(current->mm);
8482 if (ret) {
8483 /*
8484 * if we did partial map, or found file backed vmas,
8485 * release any pages we did get
8486 */
8487 if (pret > 0)
8488 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008489 goto done;
8490 }
8491
8492 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8493 if (ret) {
8494 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008495 goto done;
8496 }
8497
8498 off = ubuf & ~PAGE_MASK;
8499 size = iov->iov_len;
8500 for (i = 0; i < nr_pages; i++) {
8501 size_t vec_len;
8502
8503 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8504 imu->bvec[i].bv_page = pages[i];
8505 imu->bvec[i].bv_len = vec_len;
8506 imu->bvec[i].bv_offset = off;
8507 off = 0;
8508 size -= vec_len;
8509 }
8510 /* store original address for later verification */
8511 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008512 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008513 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008514 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008515 ret = 0;
8516done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008517 if (ret)
8518 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008519 kvfree(pages);
8520 kvfree(vmas);
8521 return ret;
8522}
8523
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008524static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008525{
Pavel Begunkov87094462021-04-11 01:46:36 +01008526 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8527 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008528}
8529
8530static int io_buffer_validate(struct iovec *iov)
8531{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008532 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8533
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008534 /*
8535 * Don't impose further limits on the size and buffer
8536 * constraints here, we'll -EINVAL later when IO is
8537 * submitted if they are wrong.
8538 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008539 if (!iov->iov_base)
8540 return iov->iov_len ? -EFAULT : 0;
8541 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008542 return -EFAULT;
8543
8544 /* arbitrary limit, but we need something */
8545 if (iov->iov_len > SZ_1G)
8546 return -EFAULT;
8547
Pavel Begunkov50e96982021-03-24 22:59:01 +00008548 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8549 return -EOVERFLOW;
8550
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008551 return 0;
8552}
8553
8554static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008555 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008556{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008557 struct page *last_hpage = NULL;
8558 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008559 int i, ret;
8560 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008561
Pavel Begunkov87094462021-04-11 01:46:36 +01008562 if (ctx->user_bufs)
8563 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01008564 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01008565 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008566 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008567 if (ret)
8568 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008569 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
8570 if (ret)
8571 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008572 ret = io_buffers_map_alloc(ctx, nr_args);
8573 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08008574 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008575 return ret;
8576 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008577
Pavel Begunkov87094462021-04-11 01:46:36 +01008578 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07008579 ret = io_copy_iov(ctx, &iov, arg, i);
8580 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008581 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008582 ret = io_buffer_validate(&iov);
8583 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008584 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008585 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008586 ret = -EINVAL;
8587 break;
8588 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008589
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008590 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
8591 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008592 if (ret)
8593 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008594 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008595
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008596 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008597
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008598 ctx->buf_data = data;
8599 if (ret)
8600 __io_sqe_buffers_unregister(ctx);
8601 else
8602 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07008603 return ret;
8604}
8605
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008606static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
8607 struct io_uring_rsrc_update2 *up,
8608 unsigned int nr_args)
8609{
8610 u64 __user *tags = u64_to_user_ptr(up->tags);
8611 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008612 struct page *last_hpage = NULL;
8613 bool needs_switch = false;
8614 __u32 done;
8615 int i, err;
8616
8617 if (!ctx->buf_data)
8618 return -ENXIO;
8619 if (up->offset + nr_args > ctx->nr_user_bufs)
8620 return -EINVAL;
8621
8622 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008623 struct io_mapped_ubuf *imu;
8624 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008625 u64 tag = 0;
8626
8627 err = io_copy_iov(ctx, &iov, iovs, done);
8628 if (err)
8629 break;
8630 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
8631 err = -EFAULT;
8632 break;
8633 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008634 err = io_buffer_validate(&iov);
8635 if (err)
8636 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008637 if (!iov.iov_base && tag) {
8638 err = -EINVAL;
8639 break;
8640 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008641 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
8642 if (err)
8643 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008644
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008645 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01008646 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008647 err = io_queue_rsrc_removal(ctx->buf_data, offset,
8648 ctx->rsrc_node, ctx->user_bufs[i]);
8649 if (unlikely(err)) {
8650 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008651 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008652 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008653 ctx->user_bufs[i] = NULL;
8654 needs_switch = true;
8655 }
8656
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008657 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008658 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008659 }
8660
8661 if (needs_switch)
8662 io_rsrc_node_switch(ctx, ctx->buf_data);
8663 return done ? done : err;
8664}
8665
Jens Axboe9b402842019-04-11 11:45:41 -06008666static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8667{
8668 __s32 __user *fds = arg;
8669 int fd;
8670
8671 if (ctx->cq_ev_fd)
8672 return -EBUSY;
8673
8674 if (copy_from_user(&fd, fds, sizeof(*fds)))
8675 return -EFAULT;
8676
8677 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8678 if (IS_ERR(ctx->cq_ev_fd)) {
8679 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01008680
Jens Axboe9b402842019-04-11 11:45:41 -06008681 ctx->cq_ev_fd = NULL;
8682 return ret;
8683 }
8684
8685 return 0;
8686}
8687
8688static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8689{
8690 if (ctx->cq_ev_fd) {
8691 eventfd_ctx_put(ctx->cq_ev_fd);
8692 ctx->cq_ev_fd = NULL;
8693 return 0;
8694 }
8695
8696 return -ENXIO;
8697}
8698
Jens Axboe5a2e7452020-02-23 16:23:11 -07008699static void io_destroy_buffers(struct io_ring_ctx *ctx)
8700{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07008701 struct io_buffer *buf;
8702 unsigned long index;
8703
8704 xa_for_each(&ctx->io_buffers, index, buf)
8705 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008706}
8707
Pavel Begunkov72558342021-08-09 20:18:09 +01008708static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008709{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008710 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008711
Pavel Begunkovbb943b82021-08-09 20:18:10 +01008712 list_for_each_entry_safe(req, nxt, list, inflight_entry) {
8713 list_del(&req->inflight_entry);
Jens Axboe1b4c3512021-02-10 00:03:19 +00008714 kmem_cache_free(req_cachep, req);
8715 }
8716}
8717
Jens Axboe4010fec2021-02-27 15:04:18 -07008718static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008719{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008720 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008721
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008722 mutex_lock(&ctx->uring_lock);
8723
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008724 if (state->free_reqs) {
8725 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
8726 state->free_reqs = 0;
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008727 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008728
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008729 io_flush_cached_locked_reqs(ctx, state);
8730 io_req_cache_free(&state->free_list);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008731 mutex_unlock(&ctx->uring_lock);
8732}
8733
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008734static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008735{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008736 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008737 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008738}
8739
Jens Axboe2b188cc2019-01-07 10:46:33 -07008740static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8741{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008742 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008743
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008744 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008745 mmdrop(ctx->mm_account);
8746 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008747 }
Jens Axboedef596e2019-01-09 08:59:42 -07008748
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008749 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
8750 io_wait_rsrc_data(ctx->buf_data);
8751 io_wait_rsrc_data(ctx->file_data);
8752
Hao Xu8bad28d2021-02-19 17:19:36 +08008753 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008754 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008755 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008756 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01008757 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01008758 if (ctx->rings)
8759 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08008760 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008761 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008762 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01008763 if (ctx->sq_creds)
8764 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07008765
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008766 /* there are no registered resources left, nobody uses it */
8767 if (ctx->rsrc_node)
8768 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00008769 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008770 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008771 flush_delayed_work(&ctx->rsrc_put_work);
8772
8773 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
8774 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008775
8776#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008777 if (ctx->ring_sock) {
8778 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008779 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008780 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008781#endif
8782
Hristo Venev75b28af2019-08-26 17:23:46 +00008783 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008784 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008785
8786 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008787 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07008788 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07008789 if (ctx->hash_map)
8790 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008791 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01008792 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008793 kfree(ctx);
8794}
8795
8796static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8797{
8798 struct io_ring_ctx *ctx = file->private_data;
8799 __poll_t mask = 0;
8800
Pavel Begunkov311997b2021-06-14 23:37:28 +01008801 poll_wait(file, &ctx->poll_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008802 /*
8803 * synchronizes with barrier from wq_has_sleeper call in
8804 * io_commit_cqring
8805 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008806 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008807 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008808 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008809
8810 /*
8811 * Don't flush cqring overflow list here, just do a simple check.
8812 * Otherwise there could possible be ABBA deadlock:
8813 * CPU0 CPU1
8814 * ---- ----
8815 * lock(&ctx->uring_lock);
8816 * lock(&ep->mtx);
8817 * lock(&ctx->uring_lock);
8818 * lock(&ep->mtx);
8819 *
8820 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8821 * pushs them to do the flush.
8822 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01008823 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008824 mask |= EPOLLIN | EPOLLRDNORM;
8825
8826 return mask;
8827}
8828
8829static int io_uring_fasync(int fd, struct file *file, int on)
8830{
8831 struct io_ring_ctx *ctx = file->private_data;
8832
8833 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8834}
8835
Yejune Deng0bead8c2020-12-24 11:02:20 +08008836static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008837{
Jens Axboe4379bf82021-02-15 13:40:22 -07008838 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008839
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008840 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07008841 if (creds) {
8842 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008843 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008844 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008845
8846 return -EINVAL;
8847}
8848
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008849struct io_tctx_exit {
8850 struct callback_head task_work;
8851 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00008852 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008853};
8854
8855static void io_tctx_exit_cb(struct callback_head *cb)
8856{
8857 struct io_uring_task *tctx = current->io_uring;
8858 struct io_tctx_exit *work;
8859
8860 work = container_of(cb, struct io_tctx_exit, task_work);
8861 /*
8862 * When @in_idle, we're in cancellation and it's racy to remove the
8863 * node. It'll be removed by the end of cancellation, just ignore it.
8864 */
8865 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01008866 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008867 complete(&work->completion);
8868}
8869
Pavel Begunkov28090c12021-04-25 23:34:45 +01008870static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8871{
8872 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8873
8874 return req->ctx == data;
8875}
8876
Jens Axboe85faa7b2020-04-09 18:14:00 -06008877static void io_ring_exit_work(struct work_struct *work)
8878{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008879 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008880 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01008881 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008882 struct io_tctx_exit exit;
8883 struct io_tctx_node *node;
8884 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06008885
Jens Axboe56952e92020-06-17 15:00:04 -06008886 /*
8887 * If we're doing polled IO and end up having requests being
8888 * submitted async (out-of-line), then completions can come in while
8889 * we're waiting for refs to drop. We need to reap these manually,
8890 * as nobody else will be looking for them.
8891 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008892 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008893 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01008894 if (ctx->sq_data) {
8895 struct io_sq_data *sqd = ctx->sq_data;
8896 struct task_struct *tsk;
8897
8898 io_sq_thread_park(sqd);
8899 tsk = sqd->thread;
8900 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
8901 io_wq_cancel_cb(tsk->io_uring->io_wq,
8902 io_cancel_ctx_cb, ctx, true);
8903 io_sq_thread_unpark(sqd);
8904 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008905
Pavel Begunkov58d3be22021-08-09 13:04:17 +01008906 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
8907 /* there is little hope left, don't run it too often */
8908 interval = HZ * 60;
8909 }
8910 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008911
Pavel Begunkov7f006512021-04-14 13:38:34 +01008912 init_completion(&exit.completion);
8913 init_task_work(&exit.task_work, io_tctx_exit_cb);
8914 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01008915 /*
8916 * Some may use context even when all refs and requests have been put,
8917 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01008918 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01008919 * this lock/unlock section also waits them to finish.
8920 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008921 mutex_lock(&ctx->uring_lock);
8922 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008923 WARN_ON_ONCE(time_after(jiffies, timeout));
8924
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008925 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
8926 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01008927 /* don't spin on a single task if cancellation failed */
8928 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008929 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
8930 if (WARN_ON_ONCE(ret))
8931 continue;
8932 wake_up_process(node->task);
8933
8934 mutex_unlock(&ctx->uring_lock);
8935 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008936 mutex_lock(&ctx->uring_lock);
8937 }
8938 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008939 spin_lock(&ctx->completion_lock);
8940 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008941
Jens Axboe85faa7b2020-04-09 18:14:00 -06008942 io_ring_ctx_free(ctx);
8943}
8944
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008945/* Returns true if we found and killed one or more timeouts */
8946static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008947 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008948{
8949 struct io_kiocb *req, *tmp;
8950 int canceled = 0;
8951
Jens Axboe79ebeae2021-08-10 15:18:27 -06008952 spin_lock(&ctx->completion_lock);
8953 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008954 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008955 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008956 io_kill_timeout(req, -ECANCELED);
8957 canceled++;
8958 }
8959 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06008960 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01008961 if (canceled != 0)
8962 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008963 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008964 if (canceled != 0)
8965 io_cqring_ev_posted(ctx);
8966 return canceled != 0;
8967}
8968
Jens Axboe2b188cc2019-01-07 10:46:33 -07008969static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8970{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008971 unsigned long index;
8972 struct creds *creds;
8973
Jens Axboe2b188cc2019-01-07 10:46:33 -07008974 mutex_lock(&ctx->uring_lock);
8975 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00008976 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00008977 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008978 xa_for_each(&ctx->personalities, index, creds)
8979 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008980 mutex_unlock(&ctx->uring_lock);
8981
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008982 io_kill_timeouts(ctx, NULL, true);
8983 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008984
Jens Axboe15dff282019-11-13 09:09:23 -07008985 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008986 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008987
Jens Axboe85faa7b2020-04-09 18:14:00 -06008988 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008989 /*
8990 * Use system_unbound_wq to avoid spawning tons of event kworkers
8991 * if we're exiting a ton of rings at the same time. It just adds
8992 * noise and overhead, there's no discernable change in runtime
8993 * over using system_wq.
8994 */
8995 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008996}
8997
8998static int io_uring_release(struct inode *inode, struct file *file)
8999{
9000 struct io_ring_ctx *ctx = file->private_data;
9001
9002 file->private_data = NULL;
9003 io_ring_ctx_wait_and_kill(ctx);
9004 return 0;
9005}
9006
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009007struct io_task_cancel {
9008 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009009 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009010};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009011
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009012static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009013{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009014 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009015 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009016 bool ret;
9017
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009018 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009019 struct io_ring_ctx *ctx = req->ctx;
9020
9021 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009022 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009023 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009024 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009025 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009026 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009027 }
9028 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009029}
9030
Pavel Begunkove1915f72021-03-11 23:29:35 +00009031static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009032 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009033{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009034 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009035 LIST_HEAD(list);
9036
Jens Axboe79ebeae2021-08-10 15:18:27 -06009037 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009038 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009039 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009040 list_cut_position(&list, &ctx->defer_list, &de->list);
9041 break;
9042 }
9043 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009044 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009045 if (list_empty(&list))
9046 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009047
9048 while (!list_empty(&list)) {
9049 de = list_first_entry(&list, struct io_defer_entry, list);
9050 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009051 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009052 kfree(de);
9053 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009054 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009055}
9056
Pavel Begunkov1b007642021-03-06 11:02:17 +00009057static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9058{
9059 struct io_tctx_node *node;
9060 enum io_wq_cancel cret;
9061 bool ret = false;
9062
9063 mutex_lock(&ctx->uring_lock);
9064 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9065 struct io_uring_task *tctx = node->task->io_uring;
9066
9067 /*
9068 * io_wq will stay alive while we hold uring_lock, because it's
9069 * killed after ctx nodes, which requires to take the lock.
9070 */
9071 if (!tctx || !tctx->io_wq)
9072 continue;
9073 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9074 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9075 }
9076 mutex_unlock(&ctx->uring_lock);
9077
9078 return ret;
9079}
9080
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009081static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9082 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009083 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009084{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009085 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009086 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009087
9088 while (1) {
9089 enum io_wq_cancel cret;
9090 bool ret = false;
9091
Pavel Begunkov1b007642021-03-06 11:02:17 +00009092 if (!task) {
9093 ret |= io_uring_try_cancel_iowq(ctx);
9094 } else if (tctx && tctx->io_wq) {
9095 /*
9096 * Cancels requests of all rings, not only @ctx, but
9097 * it's fine as the task is in exit/exec.
9098 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009099 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009100 &cancel, true);
9101 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9102 }
9103
9104 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009105 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009106 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009107 while (!list_empty_careful(&ctx->iopoll_list)) {
9108 io_iopoll_try_reap_events(ctx);
9109 ret = true;
9110 }
9111 }
9112
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009113 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9114 ret |= io_poll_remove_all(ctx, task, cancel_all);
9115 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009116 if (task)
9117 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009118 if (!ret)
9119 break;
9120 cond_resched();
9121 }
9122}
9123
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009124static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009125{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009126 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009127 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009128 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009129
9130 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009131 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009132 if (unlikely(ret))
9133 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009134 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009135 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009136 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9137 node = kmalloc(sizeof(*node), GFP_KERNEL);
9138 if (!node)
9139 return -ENOMEM;
9140 node->ctx = ctx;
9141 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009142
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009143 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9144 node, GFP_KERNEL));
9145 if (ret) {
9146 kfree(node);
9147 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009148 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009149
9150 mutex_lock(&ctx->uring_lock);
9151 list_add(&node->ctx_node, &ctx->tctx_list);
9152 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009153 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009154 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009155 return 0;
9156}
9157
9158/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009159 * Note that this task has used io_uring. We use it for cancelation purposes.
9160 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009161static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009162{
9163 struct io_uring_task *tctx = current->io_uring;
9164
9165 if (likely(tctx && tctx->last == ctx))
9166 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009167 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009168}
9169
9170/*
Jens Axboe0f212202020-09-13 13:09:39 -06009171 * Remove this io_uring_file -> task mapping.
9172 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009173static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009174{
9175 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009176 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009177
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009178 if (!tctx)
9179 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009180 node = xa_erase(&tctx->xa, index);
9181 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009182 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009183
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009184 WARN_ON_ONCE(current != node->task);
9185 WARN_ON_ONCE(list_empty(&node->ctx_node));
9186
9187 mutex_lock(&node->ctx->uring_lock);
9188 list_del(&node->ctx_node);
9189 mutex_unlock(&node->ctx->uring_lock);
9190
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009191 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009192 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009193 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009194}
9195
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009196static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009197{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009198 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009199 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009200 unsigned long index;
9201
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009202 xa_for_each(&tctx->xa, index, node)
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009203 io_uring_del_tctx_node(index);
Marco Elverb16ef422021-05-27 11:25:48 +02009204 if (wq) {
9205 /*
9206 * Must be after io_uring_del_task_file() (removes nodes under
9207 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9208 */
9209 tctx->io_wq = NULL;
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009210 io_wq_put_and_exit(wq);
Marco Elverb16ef422021-05-27 11:25:48 +02009211 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009212}
9213
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009214static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009215{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009216 if (tracked)
9217 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009218 return percpu_counter_sum(&tctx->inflight);
9219}
9220
Pavel Begunkov09899b12021-06-14 02:36:22 +01009221static void io_uring_drop_tctx_refs(struct task_struct *task)
9222{
9223 struct io_uring_task *tctx = task->io_uring;
9224 unsigned int refs = tctx->cached_refs;
9225
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009226 if (refs) {
9227 tctx->cached_refs = 0;
9228 percpu_counter_sub(&tctx->inflight, refs);
9229 put_task_struct_many(task, refs);
9230 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009231}
9232
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009233/*
9234 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9235 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9236 */
9237static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009238{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009239 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009240 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009241 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009242 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009243
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009244 WARN_ON_ONCE(sqd && sqd->thread != current);
9245
Palash Oswal6d042ff2021-04-27 18:21:49 +05309246 if (!current->io_uring)
9247 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009248 if (tctx->io_wq)
9249 io_wq_exit_start(tctx->io_wq);
9250
Jens Axboefdaf0832020-10-30 09:37:30 -06009251 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009252 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009253 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009254 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009255 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009256 if (!inflight)
9257 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009258
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009259 if (!sqd) {
9260 struct io_tctx_node *node;
9261 unsigned long index;
9262
9263 xa_for_each(&tctx->xa, index, node) {
9264 /* sqpoll task will cancel all its requests */
9265 if (node->ctx->sq_data)
9266 continue;
9267 io_uring_try_cancel_requests(node->ctx, current,
9268 cancel_all);
9269 }
9270 } else {
9271 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9272 io_uring_try_cancel_requests(ctx, current,
9273 cancel_all);
9274 }
9275
9276 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009277 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009278 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009279 * If we've seen completions, retry without waiting. This
9280 * avoids a race where a completion comes in before we did
9281 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009282 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009283 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009284 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009285 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009286 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009287 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009288
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009289 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009290 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009291 /* for exec all current's requests should be gone, kill tctx */
9292 __io_uring_free(current);
9293 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009294}
9295
Hao Xuf552a272021-08-12 12:14:35 +08009296void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009297{
Hao Xuf552a272021-08-12 12:14:35 +08009298 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009299}
9300
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009301static void *io_uring_validate_mmap_request(struct file *file,
9302 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009304 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009305 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009306 struct page *page;
9307 void *ptr;
9308
9309 switch (offset) {
9310 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009311 case IORING_OFF_CQ_RING:
9312 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009313 break;
9314 case IORING_OFF_SQES:
9315 ptr = ctx->sq_sqes;
9316 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009317 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009318 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009319 }
9320
9321 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009322 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009323 return ERR_PTR(-EINVAL);
9324
9325 return ptr;
9326}
9327
9328#ifdef CONFIG_MMU
9329
9330static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9331{
9332 size_t sz = vma->vm_end - vma->vm_start;
9333 unsigned long pfn;
9334 void *ptr;
9335
9336 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9337 if (IS_ERR(ptr))
9338 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009339
9340 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9341 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9342}
9343
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009344#else /* !CONFIG_MMU */
9345
9346static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9347{
9348 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9349}
9350
9351static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9352{
9353 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9354}
9355
9356static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9357 unsigned long addr, unsigned long len,
9358 unsigned long pgoff, unsigned long flags)
9359{
9360 void *ptr;
9361
9362 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9363 if (IS_ERR(ptr))
9364 return PTR_ERR(ptr);
9365
9366 return (unsigned long) ptr;
9367}
9368
9369#endif /* !CONFIG_MMU */
9370
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009371static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009372{
9373 DEFINE_WAIT(wait);
9374
9375 do {
9376 if (!io_sqring_full(ctx))
9377 break;
Jens Axboe90554202020-09-03 12:12:41 -06009378 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9379
9380 if (!io_sqring_full(ctx))
9381 break;
Jens Axboe90554202020-09-03 12:12:41 -06009382 schedule();
9383 } while (!signal_pending(current));
9384
9385 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009386 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009387}
9388
Hao Xuc73ebb62020-11-03 10:54:37 +08009389static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9390 struct __kernel_timespec __user **ts,
9391 const sigset_t __user **sig)
9392{
9393 struct io_uring_getevents_arg arg;
9394
9395 /*
9396 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9397 * is just a pointer to the sigset_t.
9398 */
9399 if (!(flags & IORING_ENTER_EXT_ARG)) {
9400 *sig = (const sigset_t __user *) argp;
9401 *ts = NULL;
9402 return 0;
9403 }
9404
9405 /*
9406 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9407 * timespec and sigset_t pointers if good.
9408 */
9409 if (*argsz != sizeof(arg))
9410 return -EINVAL;
9411 if (copy_from_user(&arg, argp, sizeof(arg)))
9412 return -EFAULT;
9413 *sig = u64_to_user_ptr(arg.sigmask);
9414 *argsz = arg.sigmask_sz;
9415 *ts = u64_to_user_ptr(arg.ts);
9416 return 0;
9417}
9418
Jens Axboe2b188cc2019-01-07 10:46:33 -07009419SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009420 u32, min_complete, u32, flags, const void __user *, argp,
9421 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009422{
9423 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009424 int submitted = 0;
9425 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009426 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009427
Jens Axboe4c6e2772020-07-01 11:29:10 -06009428 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009429
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009430 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9431 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009432 return -EINVAL;
9433
9434 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009435 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009436 return -EBADF;
9437
9438 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009439 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009440 goto out_fput;
9441
9442 ret = -ENXIO;
9443 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009444 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009445 goto out_fput;
9446
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009447 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009448 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009449 goto out;
9450
Jens Axboe6c271ce2019-01-10 11:22:30 -07009451 /*
9452 * For SQ polling, the thread will do all submissions and completions.
9453 * Just return the requested submit count, and wake the thread if
9454 * we were asked to.
9455 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009456 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009457 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009458 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009459
Jens Axboe21f96522021-08-14 09:04:40 -06009460 if (unlikely(ctx->sq_data->thread == NULL)) {
9461 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009462 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009463 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009464 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009465 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009466 if (flags & IORING_ENTER_SQ_WAIT) {
9467 ret = io_sqpoll_wait_sq(ctx);
9468 if (ret)
9469 goto out;
9470 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009471 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009472 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009473 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009474 if (unlikely(ret))
9475 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009476 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009477 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009478 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009479
9480 if (submitted != to_submit)
9481 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009482 }
9483 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009484 const sigset_t __user *sig;
9485 struct __kernel_timespec __user *ts;
9486
9487 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9488 if (unlikely(ret))
9489 goto out;
9490
Jens Axboe2b188cc2019-01-07 10:46:33 -07009491 min_complete = min(min_complete, ctx->cq_entries);
9492
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009493 /*
9494 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9495 * space applications don't need to do io completion events
9496 * polling again, they can rely on io_sq_thread to do polling
9497 * work, which can reduce cpu usage and uring_lock contention.
9498 */
9499 if (ctx->flags & IORING_SETUP_IOPOLL &&
9500 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009501 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009502 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009503 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009504 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009505 }
9506
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009507out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009508 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009509out_fput:
9510 fdput(f);
9511 return submitted ? submitted : ret;
9512}
9513
Tobias Klauserbebdb652020-02-26 18:38:32 +01009514#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009515static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9516 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009517{
Jens Axboe87ce9552020-01-30 08:25:34 -07009518 struct user_namespace *uns = seq_user_ns(m);
9519 struct group_info *gi;
9520 kernel_cap_t cap;
9521 unsigned __capi;
9522 int g;
9523
9524 seq_printf(m, "%5d\n", id);
9525 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9526 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9527 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9528 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9529 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9530 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9531 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9532 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9533 seq_puts(m, "\n\tGroups:\t");
9534 gi = cred->group_info;
9535 for (g = 0; g < gi->ngroups; g++) {
9536 seq_put_decimal_ull(m, g ? " " : "",
9537 from_kgid_munged(uns, gi->gid[g]));
9538 }
9539 seq_puts(m, "\n\tCapEff:\t");
9540 cap = cred->cap_effective;
9541 CAP_FOR_EACH_U32(__capi)
9542 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9543 seq_putc(m, '\n');
9544 return 0;
9545}
9546
9547static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9548{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009549 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009550 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009551 int i;
9552
Jens Axboefad8e0d2020-09-28 08:57:48 -06009553 /*
9554 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9555 * since fdinfo case grabs it in the opposite direction of normal use
9556 * cases. If we fail to get the lock, we just don't iterate any
9557 * structures that could be going away outside the io_uring mutex.
9558 */
9559 has_lock = mutex_trylock(&ctx->uring_lock);
9560
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009561 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009562 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009563 if (!sq->thread)
9564 sq = NULL;
9565 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009566
9567 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9568 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009569 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009570 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -07009571 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009572
Jens Axboe87ce9552020-01-30 08:25:34 -07009573 if (f)
9574 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9575 else
9576 seq_printf(m, "%5u: <none>\n", i);
9577 }
9578 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009579 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009580 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +01009581 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -07009582
Pavel Begunkov4751f532021-04-01 15:43:55 +01009583 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -07009584 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009585 if (has_lock && !xa_empty(&ctx->personalities)) {
9586 unsigned long index;
9587 const struct cred *cred;
9588
Jens Axboe87ce9552020-01-30 08:25:34 -07009589 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009590 xa_for_each(&ctx->personalities, index, cred)
9591 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -07009592 }
Jens Axboed7718a92020-02-14 22:23:12 -07009593 seq_printf(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -06009594 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -07009595 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9596 struct hlist_head *list = &ctx->cancel_hash[i];
9597 struct io_kiocb *req;
9598
9599 hlist_for_each_entry(req, list, hash_node)
9600 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9601 req->task->task_works != NULL);
9602 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009603 spin_unlock(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009604 if (has_lock)
9605 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009606}
9607
9608static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9609{
9610 struct io_ring_ctx *ctx = f->private_data;
9611
9612 if (percpu_ref_tryget(&ctx->refs)) {
9613 __io_uring_show_fdinfo(ctx, m);
9614 percpu_ref_put(&ctx->refs);
9615 }
9616}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009617#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009618
Jens Axboe2b188cc2019-01-07 10:46:33 -07009619static const struct file_operations io_uring_fops = {
9620 .release = io_uring_release,
9621 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009622#ifndef CONFIG_MMU
9623 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9624 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9625#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009626 .poll = io_uring_poll,
9627 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009628#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009629 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009630#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009631};
9632
9633static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9634 struct io_uring_params *p)
9635{
Hristo Venev75b28af2019-08-26 17:23:46 +00009636 struct io_rings *rings;
9637 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009638
Jens Axboebd740482020-08-05 12:58:23 -06009639 /* make sure these are sane, as we already accounted them */
9640 ctx->sq_entries = p->sq_entries;
9641 ctx->cq_entries = p->cq_entries;
9642
Hristo Venev75b28af2019-08-26 17:23:46 +00009643 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9644 if (size == SIZE_MAX)
9645 return -EOVERFLOW;
9646
9647 rings = io_mem_alloc(size);
9648 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009649 return -ENOMEM;
9650
Hristo Venev75b28af2019-08-26 17:23:46 +00009651 ctx->rings = rings;
9652 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9653 rings->sq_ring_mask = p->sq_entries - 1;
9654 rings->cq_ring_mask = p->cq_entries - 1;
9655 rings->sq_ring_entries = p->sq_entries;
9656 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009657
9658 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009659 if (size == SIZE_MAX) {
9660 io_mem_free(ctx->rings);
9661 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009662 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009663 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009664
9665 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009666 if (!ctx->sq_sqes) {
9667 io_mem_free(ctx->rings);
9668 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009669 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009670 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009671
Jens Axboe2b188cc2019-01-07 10:46:33 -07009672 return 0;
9673}
9674
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009675static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9676{
9677 int ret, fd;
9678
9679 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9680 if (fd < 0)
9681 return fd;
9682
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009683 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009684 if (ret) {
9685 put_unused_fd(fd);
9686 return ret;
9687 }
9688 fd_install(fd, file);
9689 return fd;
9690}
9691
Jens Axboe2b188cc2019-01-07 10:46:33 -07009692/*
9693 * Allocate an anonymous fd, this is what constitutes the application
9694 * visible backing of an io_uring instance. The application mmaps this
9695 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9696 * we have to tie this fd to a socket for file garbage collection purposes.
9697 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009698static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009699{
9700 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009701#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009702 int ret;
9703
Jens Axboe2b188cc2019-01-07 10:46:33 -07009704 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9705 &ctx->ring_sock);
9706 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009707 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009708#endif
9709
Jens Axboe2b188cc2019-01-07 10:46:33 -07009710 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9711 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009712#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009713 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009714 sock_release(ctx->ring_sock);
9715 ctx->ring_sock = NULL;
9716 } else {
9717 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009718 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009719#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009720 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009721}
9722
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009723static int io_uring_create(unsigned entries, struct io_uring_params *p,
9724 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009725{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009726 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009727 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009728 int ret;
9729
Jens Axboe8110c1a2019-12-28 15:39:54 -07009730 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009731 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009732 if (entries > IORING_MAX_ENTRIES) {
9733 if (!(p->flags & IORING_SETUP_CLAMP))
9734 return -EINVAL;
9735 entries = IORING_MAX_ENTRIES;
9736 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009737
9738 /*
9739 * Use twice as many entries for the CQ ring. It's possible for the
9740 * application to drive a higher depth than the size of the SQ ring,
9741 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009742 * some flexibility in overcommitting a bit. If the application has
9743 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9744 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009745 */
9746 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009747 if (p->flags & IORING_SETUP_CQSIZE) {
9748 /*
9749 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9750 * to a power-of-two, if it isn't already. We do NOT impose
9751 * any cq vs sq ring sizing.
9752 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009753 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009754 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009755 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9756 if (!(p->flags & IORING_SETUP_CLAMP))
9757 return -EINVAL;
9758 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9759 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009760 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9761 if (p->cq_entries < p->sq_entries)
9762 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009763 } else {
9764 p->cq_entries = 2 * p->sq_entries;
9765 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009766
Jens Axboe2b188cc2019-01-07 10:46:33 -07009767 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009768 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009769 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009770 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009771 if (!capable(CAP_IPC_LOCK))
9772 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -06009773
9774 /*
9775 * This is just grabbed for accounting purposes. When a process exits,
9776 * the mm is exited and dropped before the files, hence we need to hang
9777 * on to this mm purely for the purposes of being able to unaccount
9778 * memory (locked/pinned vm). It's not used for anything else.
9779 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009780 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009781 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009782
Jens Axboe2b188cc2019-01-07 10:46:33 -07009783 ret = io_allocate_scq_urings(ctx, p);
9784 if (ret)
9785 goto err;
9786
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009787 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009788 if (ret)
9789 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009790 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +01009791 ret = io_rsrc_node_switch_start(ctx);
9792 if (ret)
9793 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009794 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009795
Jens Axboe2b188cc2019-01-07 10:46:33 -07009796 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009797 p->sq_off.head = offsetof(struct io_rings, sq.head);
9798 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9799 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9800 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9801 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9802 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9803 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009804
9805 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009806 p->cq_off.head = offsetof(struct io_rings, cq.head);
9807 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9808 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9809 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9810 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9811 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009812 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009813
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009814 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9815 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009816 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009817 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +01009818 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
9819 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009820
9821 if (copy_to_user(params, p, sizeof(*p))) {
9822 ret = -EFAULT;
9823 goto err;
9824 }
Jens Axboed1719f72020-07-30 13:43:53 -06009825
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009826 file = io_uring_get_file(ctx);
9827 if (IS_ERR(file)) {
9828 ret = PTR_ERR(file);
9829 goto err;
9830 }
9831
Jens Axboed1719f72020-07-30 13:43:53 -06009832 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009833 * Install ring fd as the very last thing, so we don't risk someone
9834 * having closed it before we finish setup
9835 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009836 ret = io_uring_install_fd(ctx, file);
9837 if (ret < 0) {
9838 /* fput will clean it up */
9839 fput(file);
9840 return ret;
9841 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009842
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009843 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009844 return ret;
9845err:
9846 io_ring_ctx_wait_and_kill(ctx);
9847 return ret;
9848}
9849
9850/*
9851 * Sets up an aio uring context, and returns the fd. Applications asks for a
9852 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9853 * params structure passed in.
9854 */
9855static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9856{
9857 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009858 int i;
9859
9860 if (copy_from_user(&p, params, sizeof(p)))
9861 return -EFAULT;
9862 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9863 if (p.resv[i])
9864 return -EINVAL;
9865 }
9866
Jens Axboe6c271ce2019-01-10 11:22:30 -07009867 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009868 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009869 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9870 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009871 return -EINVAL;
9872
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009873 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009874}
9875
9876SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9877 struct io_uring_params __user *, params)
9878{
9879 return io_uring_setup(entries, params);
9880}
9881
Jens Axboe66f4af92020-01-16 15:36:52 -07009882static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9883{
9884 struct io_uring_probe *p;
9885 size_t size;
9886 int i, ret;
9887
9888 size = struct_size(p, ops, nr_args);
9889 if (size == SIZE_MAX)
9890 return -EOVERFLOW;
9891 p = kzalloc(size, GFP_KERNEL);
9892 if (!p)
9893 return -ENOMEM;
9894
9895 ret = -EFAULT;
9896 if (copy_from_user(p, arg, size))
9897 goto out;
9898 ret = -EINVAL;
9899 if (memchr_inv(p, 0, size))
9900 goto out;
9901
9902 p->last_op = IORING_OP_LAST - 1;
9903 if (nr_args > IORING_OP_LAST)
9904 nr_args = IORING_OP_LAST;
9905
9906 for (i = 0; i < nr_args; i++) {
9907 p->ops[i].op = i;
9908 if (!io_op_defs[i].not_supported)
9909 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9910 }
9911 p->ops_len = i;
9912
9913 ret = 0;
9914 if (copy_to_user(arg, p, size))
9915 ret = -EFAULT;
9916out:
9917 kfree(p);
9918 return ret;
9919}
9920
Jens Axboe071698e2020-01-28 10:04:42 -07009921static int io_register_personality(struct io_ring_ctx *ctx)
9922{
Jens Axboe4379bf82021-02-15 13:40:22 -07009923 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009924 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009925 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009926
Jens Axboe4379bf82021-02-15 13:40:22 -07009927 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009928
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009929 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
9930 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -06009931 if (ret < 0) {
9932 put_cred(creds);
9933 return ret;
9934 }
9935 return id;
Jens Axboe071698e2020-01-28 10:04:42 -07009936}
9937
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009938static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9939 unsigned int nr_args)
9940{
9941 struct io_uring_restriction *res;
9942 size_t size;
9943 int i, ret;
9944
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009945 /* Restrictions allowed only if rings started disabled */
9946 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9947 return -EBADFD;
9948
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009949 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009950 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009951 return -EBUSY;
9952
9953 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9954 return -EINVAL;
9955
9956 size = array_size(nr_args, sizeof(*res));
9957 if (size == SIZE_MAX)
9958 return -EOVERFLOW;
9959
9960 res = memdup_user(arg, size);
9961 if (IS_ERR(res))
9962 return PTR_ERR(res);
9963
9964 ret = 0;
9965
9966 for (i = 0; i < nr_args; i++) {
9967 switch (res[i].opcode) {
9968 case IORING_RESTRICTION_REGISTER_OP:
9969 if (res[i].register_op >= IORING_REGISTER_LAST) {
9970 ret = -EINVAL;
9971 goto out;
9972 }
9973
9974 __set_bit(res[i].register_op,
9975 ctx->restrictions.register_op);
9976 break;
9977 case IORING_RESTRICTION_SQE_OP:
9978 if (res[i].sqe_op >= IORING_OP_LAST) {
9979 ret = -EINVAL;
9980 goto out;
9981 }
9982
9983 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9984 break;
9985 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9986 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9987 break;
9988 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9989 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9990 break;
9991 default:
9992 ret = -EINVAL;
9993 goto out;
9994 }
9995 }
9996
9997out:
9998 /* Reset all restrictions if an error happened */
9999 if (ret != 0)
10000 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10001 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010002 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010003
10004 kfree(res);
10005 return ret;
10006}
10007
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010008static int io_register_enable_rings(struct io_ring_ctx *ctx)
10009{
10010 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10011 return -EBADFD;
10012
10013 if (ctx->restrictions.registered)
10014 ctx->restricted = 1;
10015
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010016 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10017 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10018 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010019 return 0;
10020}
10021
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010022static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010023 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010024 unsigned nr_args)
10025{
10026 __u32 tmp;
10027 int err;
10028
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010029 if (up->resv)
10030 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010031 if (check_add_overflow(up->offset, nr_args, &tmp))
10032 return -EOVERFLOW;
10033 err = io_rsrc_node_switch_start(ctx);
10034 if (err)
10035 return err;
10036
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010037 switch (type) {
10038 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010039 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010040 case IORING_RSRC_BUFFER:
10041 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010042 }
10043 return -EINVAL;
10044}
10045
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010046static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10047 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010048{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010049 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010050
10051 if (!nr_args)
10052 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010053 memset(&up, 0, sizeof(up));
10054 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10055 return -EFAULT;
10056 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10057}
10058
10059static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010060 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010061{
10062 struct io_uring_rsrc_update2 up;
10063
10064 if (size != sizeof(up))
10065 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010066 if (copy_from_user(&up, arg, sizeof(up)))
10067 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010068 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010069 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010070 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010071}
10072
Pavel Begunkov792e3582021-04-25 14:32:21 +010010073static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010074 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010075{
10076 struct io_uring_rsrc_register rr;
10077
10078 /* keep it extendible */
10079 if (size != sizeof(rr))
10080 return -EINVAL;
10081
10082 memset(&rr, 0, sizeof(rr));
10083 if (copy_from_user(&rr, arg, size))
10084 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010085 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010086 return -EINVAL;
10087
Pavel Begunkov992da012021-06-10 16:37:37 +010010088 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010089 case IORING_RSRC_FILE:
10090 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10091 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010092 case IORING_RSRC_BUFFER:
10093 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10094 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010095 }
10096 return -EINVAL;
10097}
10098
Jens Axboefe764212021-06-17 10:19:54 -060010099static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10100 unsigned len)
10101{
10102 struct io_uring_task *tctx = current->io_uring;
10103 cpumask_var_t new_mask;
10104 int ret;
10105
10106 if (!tctx || !tctx->io_wq)
10107 return -EINVAL;
10108
10109 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10110 return -ENOMEM;
10111
10112 cpumask_clear(new_mask);
10113 if (len > cpumask_size())
10114 len = cpumask_size();
10115
10116 if (copy_from_user(new_mask, arg, len)) {
10117 free_cpumask_var(new_mask);
10118 return -EFAULT;
10119 }
10120
10121 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10122 free_cpumask_var(new_mask);
10123 return ret;
10124}
10125
10126static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10127{
10128 struct io_uring_task *tctx = current->io_uring;
10129
10130 if (!tctx || !tctx->io_wq)
10131 return -EINVAL;
10132
10133 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10134}
10135
Jens Axboe071698e2020-01-28 10:04:42 -070010136static bool io_register_op_must_quiesce(int op)
10137{
10138 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010139 case IORING_REGISTER_BUFFERS:
10140 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010141 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010142 case IORING_UNREGISTER_FILES:
10143 case IORING_REGISTER_FILES_UPDATE:
10144 case IORING_REGISTER_PROBE:
10145 case IORING_REGISTER_PERSONALITY:
10146 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010147 case IORING_REGISTER_FILES2:
10148 case IORING_REGISTER_FILES_UPDATE2:
10149 case IORING_REGISTER_BUFFERS2:
10150 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010151 case IORING_REGISTER_IOWQ_AFF:
10152 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe071698e2020-01-28 10:04:42 -070010153 return false;
10154 default:
10155 return true;
10156 }
10157}
10158
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010159static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10160{
10161 long ret;
10162
10163 percpu_ref_kill(&ctx->refs);
10164
10165 /*
10166 * Drop uring mutex before waiting for references to exit. If another
10167 * thread is currently inside io_uring_enter() it might need to grab the
10168 * uring_lock to make progress. If we hold it here across the drain
10169 * wait, then we can deadlock. It's safe to drop the mutex here, since
10170 * no new references will come in after we've killed the percpu ref.
10171 */
10172 mutex_unlock(&ctx->uring_lock);
10173 do {
10174 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10175 if (!ret)
10176 break;
10177 ret = io_run_task_work_sig();
10178 } while (ret >= 0);
10179 mutex_lock(&ctx->uring_lock);
10180
10181 if (ret)
10182 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10183 return ret;
10184}
10185
Jens Axboeedafcce2019-01-09 09:16:05 -070010186static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10187 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010188 __releases(ctx->uring_lock)
10189 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010190{
10191 int ret;
10192
Jens Axboe35fa71a2019-04-22 10:23:23 -060010193 /*
10194 * We're inside the ring mutex, if the ref is already dying, then
10195 * someone else killed the ctx or is already going through
10196 * io_uring_register().
10197 */
10198 if (percpu_ref_is_dying(&ctx->refs))
10199 return -ENXIO;
10200
Pavel Begunkov75c40212021-04-15 13:07:40 +010010201 if (ctx->restricted) {
10202 if (opcode >= IORING_REGISTER_LAST)
10203 return -EINVAL;
10204 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10205 if (!test_bit(opcode, ctx->restrictions.register_op))
10206 return -EACCES;
10207 }
10208
Jens Axboe071698e2020-01-28 10:04:42 -070010209 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010210 ret = io_ctx_quiesce(ctx);
10211 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010212 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010213 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010214
10215 switch (opcode) {
10216 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010217 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010218 break;
10219 case IORING_UNREGISTER_BUFFERS:
10220 ret = -EINVAL;
10221 if (arg || nr_args)
10222 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010223 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010224 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010225 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010226 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010227 break;
10228 case IORING_UNREGISTER_FILES:
10229 ret = -EINVAL;
10230 if (arg || nr_args)
10231 break;
10232 ret = io_sqe_files_unregister(ctx);
10233 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010234 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010235 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010236 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010237 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010238 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010239 ret = -EINVAL;
10240 if (nr_args != 1)
10241 break;
10242 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010243 if (ret)
10244 break;
10245 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10246 ctx->eventfd_async = 1;
10247 else
10248 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010249 break;
10250 case IORING_UNREGISTER_EVENTFD:
10251 ret = -EINVAL;
10252 if (arg || nr_args)
10253 break;
10254 ret = io_eventfd_unregister(ctx);
10255 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010256 case IORING_REGISTER_PROBE:
10257 ret = -EINVAL;
10258 if (!arg || nr_args > 256)
10259 break;
10260 ret = io_probe(ctx, arg, nr_args);
10261 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010262 case IORING_REGISTER_PERSONALITY:
10263 ret = -EINVAL;
10264 if (arg || nr_args)
10265 break;
10266 ret = io_register_personality(ctx);
10267 break;
10268 case IORING_UNREGISTER_PERSONALITY:
10269 ret = -EINVAL;
10270 if (arg)
10271 break;
10272 ret = io_unregister_personality(ctx, nr_args);
10273 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010274 case IORING_REGISTER_ENABLE_RINGS:
10275 ret = -EINVAL;
10276 if (arg || nr_args)
10277 break;
10278 ret = io_register_enable_rings(ctx);
10279 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010280 case IORING_REGISTER_RESTRICTIONS:
10281 ret = io_register_restrictions(ctx, arg, nr_args);
10282 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010283 case IORING_REGISTER_FILES2:
10284 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010285 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010286 case IORING_REGISTER_FILES_UPDATE2:
10287 ret = io_register_rsrc_update(ctx, arg, nr_args,
10288 IORING_RSRC_FILE);
10289 break;
10290 case IORING_REGISTER_BUFFERS2:
10291 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10292 break;
10293 case IORING_REGISTER_BUFFERS_UPDATE:
10294 ret = io_register_rsrc_update(ctx, arg, nr_args,
10295 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010296 break;
Jens Axboefe764212021-06-17 10:19:54 -060010297 case IORING_REGISTER_IOWQ_AFF:
10298 ret = -EINVAL;
10299 if (!arg || !nr_args)
10300 break;
10301 ret = io_register_iowq_aff(ctx, arg, nr_args);
10302 break;
10303 case IORING_UNREGISTER_IOWQ_AFF:
10304 ret = -EINVAL;
10305 if (arg || nr_args)
10306 break;
10307 ret = io_unregister_iowq_aff(ctx);
10308 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010309 default:
10310 ret = -EINVAL;
10311 break;
10312 }
10313
Jens Axboe071698e2020-01-28 10:04:42 -070010314 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010315 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010316 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010317 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010318 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010319 return ret;
10320}
10321
10322SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10323 void __user *, arg, unsigned int, nr_args)
10324{
10325 struct io_ring_ctx *ctx;
10326 long ret = -EBADF;
10327 struct fd f;
10328
10329 f = fdget(fd);
10330 if (!f.file)
10331 return -EBADF;
10332
10333 ret = -EOPNOTSUPP;
10334 if (f.file->f_op != &io_uring_fops)
10335 goto out_fput;
10336
10337 ctx = f.file->private_data;
10338
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010339 io_run_task_work();
10340
Jens Axboeedafcce2019-01-09 09:16:05 -070010341 mutex_lock(&ctx->uring_lock);
10342 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10343 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010344 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10345 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010346out_fput:
10347 fdput(f);
10348 return ret;
10349}
10350
Jens Axboe2b188cc2019-01-07 10:46:33 -070010351static int __init io_uring_init(void)
10352{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010353#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10354 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10355 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10356} while (0)
10357
10358#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10359 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10360 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10361 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10362 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10363 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10364 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10365 BUILD_BUG_SQE_ELEM(8, __u64, off);
10366 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10367 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010368 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010369 BUILD_BUG_SQE_ELEM(24, __u32, len);
10370 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10371 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10372 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10373 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010374 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10375 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010376 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10377 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10378 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10379 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10380 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10381 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10382 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10383 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010384 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010385 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10386 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010387 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010388 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010389 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010390
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010391 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10392 sizeof(struct io_uring_rsrc_update));
10393 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10394 sizeof(struct io_uring_rsrc_update2));
10395 /* should fit into one byte */
10396 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
10397
Jens Axboed3656342019-12-18 09:50:26 -070010398 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010399 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010400
Jens Axboe91f245d2021-02-09 13:48:50 -070010401 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10402 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010403 return 0;
10404};
10405__initcall(io_uring_init);