blob: 7918a320104d16c2dd5051f8dcce346b677a173d [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 Begunkov68fe2562021-09-15 12:03:38 +0100106#define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \
107 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
108
109#define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN)
110
Pavel Begunkovc8543572021-06-17 18:14:04 +0100111#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
Pavel Begunkovd886e182021-10-04 20:02:56 +0100112 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \
113 REQ_F_ASYNC_DATA)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000114
Pavel Begunkov09899b12021-06-14 02:36:22 +0100115#define IO_TCTX_REFS_CACHE_NR (1U << 10)
116
Jens Axboe2b188cc2019-01-07 10:46:33 -0700117struct io_uring {
118 u32 head ____cacheline_aligned_in_smp;
119 u32 tail ____cacheline_aligned_in_smp;
120};
121
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 * This data is shared with the application through the mmap at offsets
124 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 *
126 * The offsets to the member fields are published through struct
127 * io_sqring_offsets when calling io_uring_setup.
128 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000129struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 /*
131 * Head and tail offsets into the ring; the offsets need to be
132 * masked to get valid indices.
133 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 * The kernel controls head of the sq ring and the tail of the cq ring,
135 * and the application controls tail of the sq ring and the head of the
136 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200137 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000138 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200139 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200141 * ring_entries - 1)
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_ring_mask, cq_ring_mask;
144 /* Ring sizes (constant, power of 2) */
145 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200146 /*
147 * Number of invalid entries dropped by the kernel due to
148 * invalid index stored in array
149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application (i.e. get number of "new events" by comparing to
152 * cached value).
153 *
154 * After a new SQ head value was read by the application this
155 * counter includes all submissions that were dropped reaching
156 * the new SQ head (and possibly more).
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200160 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200161 *
162 * Written by the kernel, shouldn't be modified by the
163 * application.
164 *
165 * The application needs a full memory barrier before checking
166 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
167 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000168 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200169 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200170 * Runtime CQ flags
171 *
172 * Written by the application, shouldn't be modified by the
173 * kernel.
174 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100175 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200176 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200177 * Number of completion events lost because the queue was full;
178 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800179 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200180 * the completion queue.
181 *
182 * Written by the kernel, shouldn't be modified by the
183 * application (i.e. get number of "new events" by comparing to
184 * cached value).
185 *
186 * As completion events come in out of order this counter is not
187 * ordered with any other data.
188 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000189 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200190 /*
191 * Ring buffer of completion events.
192 *
193 * The kernel writes completion events fresh every time they are
194 * produced, so the application is allowed to modify pending
195 * entries.
196 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000197 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700198};
199
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000200enum io_uring_cmd_flags {
201 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000202 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000203};
204
Jens Axboeedafcce2019-01-09 09:16:05 -0700205struct io_mapped_ubuf {
206 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100207 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700208 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600209 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100210 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700211};
212
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000213struct io_ring_ctx;
214
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000215struct io_overflow_cqe {
216 struct io_uring_cqe cqe;
217 struct list_head list;
218};
219
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100220struct io_fixed_file {
221 /* file * with additional FFS_* flags */
222 unsigned long file_ptr;
223};
224
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000225struct io_rsrc_put {
226 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100227 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000228 union {
229 void *rsrc;
230 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100231 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000232 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000233};
234
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100235struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100236 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700237};
238
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100239struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800240 struct percpu_ref refs;
241 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000242 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100243 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600244 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000245 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800246};
247
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100248typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
249
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100250struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700251 struct io_ring_ctx *ctx;
252
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100253 u64 **tags;
254 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100255 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100256 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700257 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800258 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700259};
260
Jens Axboe5a2e7452020-02-23 16:23:11 -0700261struct io_buffer {
262 struct list_head list;
263 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300264 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700265 __u16 bid;
266};
267
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200268struct io_restriction {
269 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
270 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
271 u8 sqe_flags_allowed;
272 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200273 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200274};
275
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700276enum {
277 IO_SQ_THREAD_SHOULD_STOP = 0,
278 IO_SQ_THREAD_SHOULD_PARK,
279};
280
Jens Axboe534ca6d2020-09-02 13:52:19 -0600281struct io_sq_data {
282 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000283 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000284 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600285
286 /* ctx's that are using this sqd */
287 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600288
Jens Axboe534ca6d2020-09-02 13:52:19 -0600289 struct task_struct *thread;
290 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800291
292 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700293 int sq_cpu;
294 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700295 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700296
297 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700298 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600299};
300
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000301#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000302#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000303#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000304
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000305struct io_submit_link {
306 struct io_kiocb *head;
307 struct io_kiocb *last;
308};
309
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000310struct io_submit_state {
Pavel Begunkov5a158c62021-10-06 16:06:48 +0100311 /* inline/task_work completion list, under ->uring_lock */
312 struct io_wq_work_node free_list;
313 /* batch completion logic */
314 struct io_wq_work_list compl_reqs;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000315 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000316
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000317 bool plug_started;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +0100318 bool need_plug;
Pavel Begunkov5a158c62021-10-06 16:06:48 +0100319 struct blk_plug plug;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000320};
321
Jens Axboe2b188cc2019-01-07 10:46:33 -0700322struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100323 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700324 struct {
325 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700326
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100327 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800329 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800330 unsigned int drain_next: 1;
331 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200332 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100333 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100334 unsigned int drain_active: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100335 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700336
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100337 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100338 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100339 struct mutex uring_lock;
340
Hristo Venev75b28af2019-08-26 17:23:46 +0000341 /*
342 * Ring buffer of indices into array of io_uring_sqe, which is
343 * mmapped by the application using the IORING_OFF_SQES offset.
344 *
345 * This indirection could e.g. be used to assign fixed
346 * io_uring_sqe entries to operations and only submit them to
347 * the queue when needed.
348 *
349 * The kernel modifies neither the indices array nor the entries
350 * array.
351 */
352 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100353 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700354 unsigned cached_sq_head;
355 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600356 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100357
358 /*
359 * Fixed resources fast path, should be accessed only under
360 * uring_lock, and updated through io_uring_register(2)
361 */
362 struct io_rsrc_node *rsrc_node;
363 struct io_file_table file_table;
364 unsigned nr_user_files;
365 unsigned nr_user_bufs;
366 struct io_mapped_ubuf **user_bufs;
367
368 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600369 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600370 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700371 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100372 struct xarray io_buffers;
373 struct xarray personalities;
374 u32 pers_next;
375 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376 } ____cacheline_aligned_in_smp;
377
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100378 /* IRQ completion list, under ->completion_lock */
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +0100379 struct io_wq_work_list locked_free_list;
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100380 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700381
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100382 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600383 struct io_sq_data *sq_data; /* if using sq thread polling */
384
Jens Axboe90554202020-09-03 12:12:41 -0600385 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600386 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000387
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100388 unsigned long check_cq_overflow;
389
Jens Axboe206aefd2019-11-07 18:27:42 -0700390 struct {
391 unsigned cached_cq_tail;
392 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700393 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100394 struct wait_queue_head cq_wait;
395 unsigned cq_extra;
396 atomic_t cq_timeouts;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100397 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700398 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700399
400 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700401 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700402
Jens Axboe89850fc2021-08-10 15:11:51 -0600403 spinlock_t timeout_lock;
404
Jens Axboedef596e2019-01-09 08:59:42 -0700405 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300406 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700407 * io_uring instances that don't use IORING_SETUP_SQPOLL.
408 * For SQPOLL, only the single threaded io_sq_thread() will
409 * manipulate the list, hence no extra locking is needed there.
410 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +0100411 struct io_wq_work_list iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700412 struct hlist_head *cancel_hash;
413 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800414 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700415 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600416
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200417 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700418
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100419 /* slow path rsrc auxilary data, used by update/register */
420 struct {
421 struct io_rsrc_node *rsrc_backup_node;
422 struct io_mapped_ubuf *dummy_ubuf;
423 struct io_rsrc_data *file_data;
424 struct io_rsrc_data *buf_data;
425
426 struct delayed_work rsrc_put_work;
427 struct llist_head rsrc_put_llist;
428 struct list_head rsrc_ref_list;
429 spinlock_t rsrc_ref_lock;
430 };
431
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700432 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100433 struct {
434 #if defined(CONFIG_UNIX)
435 struct socket *ring_sock;
436 #endif
437 /* hashed buffered write serialization */
438 struct io_wq_hash *hash_map;
439
440 /* Only used for accounting purposes */
441 struct user_struct *user;
442 struct mm_struct *mm_account;
443
444 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100445 struct llist_head fallback_llist;
446 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100447 struct work_struct exit_work;
448 struct list_head tctx_list;
449 struct completion ref_comp;
450 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700451};
452
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100453struct io_uring_task {
454 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100455 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100456 struct xarray xa;
457 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100458 const struct io_ring_ctx *last;
459 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100460 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100461 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100462 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100463
464 spinlock_t task_lock;
465 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100466 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100467 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100468};
469
Jens Axboe09bb8392019-03-13 12:39:28 -0600470/*
471 * First field must be the file pointer in all the
472 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
473 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700474struct io_poll_iocb {
475 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000476 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700477 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600478 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700479 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700480 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700481};
482
Pavel Begunkov9d805892021-04-13 02:58:40 +0100483struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000484 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100485 u64 old_user_data;
486 u64 new_user_data;
487 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600488 bool update_events;
489 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000490};
491
Jens Axboeb5dba592019-12-11 14:02:38 -0700492struct io_close {
493 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700494 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100495 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700496};
497
Jens Axboead8a48a2019-11-15 08:49:11 -0700498struct io_timeout_data {
499 struct io_kiocb *req;
500 struct hrtimer timer;
501 struct timespec64 ts;
502 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600503 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700504};
505
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700506struct io_accept {
507 struct file *file;
508 struct sockaddr __user *addr;
509 int __user *addr_len;
510 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100511 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600512 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700513};
514
515struct io_sync {
516 struct file *file;
517 loff_t len;
518 loff_t off;
519 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700520 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700521};
522
Jens Axboefbf23842019-12-17 18:45:56 -0700523struct io_cancel {
524 struct file *file;
525 u64 addr;
526};
527
Jens Axboeb29472e2019-12-17 18:50:29 -0700528struct io_timeout {
529 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300530 u32 off;
531 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300532 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000533 /* head of the link, used by linked timeouts only */
534 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600535 /* for linked completions */
536 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700537};
538
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100539struct io_timeout_rem {
540 struct file *file;
541 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000542
543 /* timeout update */
544 struct timespec64 ts;
545 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600546 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100547};
548
Jens Axboe9adbd452019-12-20 08:45:55 -0700549struct io_rw {
550 /* NOTE: kiocb has the file as the first member, so don't do it here */
551 struct kiocb kiocb;
552 u64 addr;
553 u64 len;
554};
555
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700556struct io_connect {
557 struct file *file;
558 struct sockaddr __user *addr;
559 int addr_len;
560};
561
Jens Axboee47293f2019-12-20 08:58:21 -0700562struct io_sr_msg {
563 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700564 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100565 struct compat_msghdr __user *umsg_compat;
566 struct user_msghdr __user *umsg;
567 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700568 };
Jens Axboee47293f2019-12-20 08:58:21 -0700569 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700570 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700571 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700572};
573
Jens Axboe15b71ab2019-12-11 11:20:36 -0700574struct io_open {
575 struct file *file;
576 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100577 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700578 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700579 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600580 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700581};
582
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000583struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700584 struct file *file;
585 u64 arg;
586 u32 nr_args;
587 u32 offset;
588};
589
Jens Axboe4840e412019-12-25 22:03:45 -0700590struct io_fadvise {
591 struct file *file;
592 u64 offset;
593 u32 len;
594 u32 advice;
595};
596
Jens Axboec1ca7572019-12-25 22:18:28 -0700597struct io_madvise {
598 struct file *file;
599 u64 addr;
600 u32 len;
601 u32 advice;
602};
603
Jens Axboe3e4827b2020-01-08 15:18:09 -0700604struct io_epoll {
605 struct file *file;
606 int epfd;
607 int op;
608 int fd;
609 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700610};
611
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300612struct io_splice {
613 struct file *file_out;
614 struct file *file_in;
615 loff_t off_out;
616 loff_t off_in;
617 u64 len;
618 unsigned int flags;
619};
620
Jens Axboeddf0322d2020-02-23 16:41:33 -0700621struct io_provide_buf {
622 struct file *file;
623 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100624 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700625 __u32 bgid;
626 __u16 nbufs;
627 __u16 bid;
628};
629
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700630struct io_statx {
631 struct file *file;
632 int dfd;
633 unsigned int mask;
634 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700635 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700636 struct statx __user *buffer;
637};
638
Jens Axboe36f4fa62020-09-05 11:14:22 -0600639struct io_shutdown {
640 struct file *file;
641 int how;
642};
643
Jens Axboe80a261f2020-09-28 14:23:58 -0600644struct io_rename {
645 struct file *file;
646 int old_dfd;
647 int new_dfd;
648 struct filename *oldpath;
649 struct filename *newpath;
650 int flags;
651};
652
Jens Axboe14a11432020-09-28 14:27:37 -0600653struct io_unlink {
654 struct file *file;
655 int dfd;
656 int flags;
657 struct filename *filename;
658};
659
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700660struct io_mkdir {
661 struct file *file;
662 int dfd;
663 umode_t mode;
664 struct filename *filename;
665};
666
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700667struct io_symlink {
668 struct file *file;
669 int new_dfd;
670 struct filename *oldpath;
671 struct filename *newpath;
672};
673
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700674struct io_hardlink {
675 struct file *file;
676 int old_dfd;
677 int new_dfd;
678 struct filename *oldpath;
679 struct filename *newpath;
680 int flags;
681};
682
Jens Axboef499a022019-12-02 16:28:46 -0700683struct io_async_connect {
684 struct sockaddr_storage address;
685};
686
Jens Axboe03b12302019-12-02 18:50:25 -0700687struct io_async_msghdr {
688 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000689 /* points to an allocated iov, if NULL we use fast_iov instead */
690 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700691 struct sockaddr __user *uaddr;
692 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700693 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700694};
695
Jens Axboef67676d2019-12-02 11:03:47 -0700696struct io_async_rw {
697 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600698 const struct iovec *free_iovec;
699 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600700 struct iov_iter_state iter_state;
Jens Axboe227c0c92020-08-13 11:51:40 -0600701 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600702 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700703};
704
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300705enum {
706 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
707 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
708 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
709 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
710 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700711 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300712
Pavel Begunkovdddca222021-04-27 16:13:52 +0100713 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100714 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300715 REQ_F_INFLIGHT_BIT,
716 REQ_F_CUR_POS_BIT,
717 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300718 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300719 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700720 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700721 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000722 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600723 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100724 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100725 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100726 REQ_F_ARM_LTIMEOUT_BIT,
Pavel Begunkovd886e182021-10-04 20:02:56 +0100727 REQ_F_ASYNC_DATA_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700728 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100729 REQ_F_NOWAIT_READ_BIT,
730 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700731 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700732
733 /* not a real bit, just to check we're not overflowing the space */
734 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300735};
736
737enum {
738 /* ctx owns file */
739 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
740 /* drain existing IO first */
741 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
742 /* linked sqes */
743 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
744 /* doesn't sever on completion < 0 */
745 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
746 /* IOSQE_ASYNC */
747 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700748 /* IOSQE_BUFFER_SELECT */
749 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300750
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300751 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100752 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000753 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300754 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
755 /* read/write uses file position */
756 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
757 /* must not punt to workers */
758 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100759 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300760 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300761 /* needs cleanup */
762 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700763 /* already went through poll handler */
764 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700765 /* buffer already selected */
766 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000767 /* completion is deferred through io_comp_state */
768 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600769 /* caller should reissue async */
770 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700771 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100772 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700773 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100774 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700775 /* regular file */
776 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100777 /* has creds assigned */
778 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100779 /* skip refcounting if not set */
780 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100781 /* there is a linked timeout that has to be armed */
782 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Pavel Begunkovd886e182021-10-04 20:02:56 +0100783 /* ->async_data allocated */
784 REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700785};
786
787struct async_poll {
788 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600789 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300790};
791
Pavel Begunkovf237c302021-08-18 12:42:46 +0100792typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100793
Jens Axboe7cbf1722021-02-10 00:03:20 +0000794struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100795 union {
796 struct io_wq_work_node node;
797 struct llist_node fallback_node;
798 };
799 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000800};
801
Pavel Begunkov992da012021-06-10 16:37:37 +0100802enum {
803 IORING_RSRC_FILE = 0,
804 IORING_RSRC_BUFFER = 1,
805};
806
Jens Axboe09bb8392019-03-13 12:39:28 -0600807/*
808 * NOTE! Each of the iocb union members has the file pointer
809 * as the first entry in their struct definition. So you can
810 * access the file pointer through any of the sub-structs,
811 * or directly as just 'ki_filp' in this struct.
812 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700814 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600815 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700816 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700817 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100818 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700819 struct io_accept accept;
820 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700821 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700822 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100823 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700824 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700825 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700826 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700827 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000828 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700829 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700830 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700831 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300832 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700833 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700834 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600835 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600836 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600837 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700838 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700839 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700840 struct io_hardlink hardlink;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700841 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700842
Jens Axboed625c6e2019-12-17 19:53:05 -0700843 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800844 /* polled IO has completed */
845 u8 iopoll_completed;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700846 u16 buf_index;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100847 unsigned int flags;
848
849 u64 user_data;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300850 u32 result;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100851 u32 cflags;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700852
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300853 struct io_ring_ctx *ctx;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300854 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700855
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000856 struct percpu_ref *fixed_rsrc_refs;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100857 /* store used ubuf, so we can prevent reloading */
858 struct io_mapped_ubuf *imu;
Jens Axboed7718a92020-02-14 22:23:12 -0700859
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100860 /* used by request caches, completion batching and iopoll */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100861 struct io_wq_work_node comp_list;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100862 atomic_t refs;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100863 struct io_kiocb *link;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100864 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300865 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
866 struct hlist_node hash_node;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100867 /* internal polling, see IORING_FEAT_FAST_POLL */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300868 struct async_poll *apoll;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100869 /* opcode allocated if it needs to store data for async defer */
870 void *async_data;
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100871 struct io_wq_work work;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100872 /* custom credentials, valid IFF REQ_F_CREDS is set */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100873 const struct cred *creds;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100874 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
Pavel Begunkov30d51dd2021-10-01 18:07:03 +0100875 struct io_buffer *kbuf;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700876};
877
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000878struct io_tctx_node {
879 struct list_head ctx_node;
880 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000881 struct io_ring_ctx *ctx;
882};
883
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300884struct io_defer_entry {
885 struct list_head list;
886 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300887 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300888};
889
Jens Axboed3656342019-12-18 09:50:26 -0700890struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700891 /* needs req->file assigned */
892 unsigned needs_file : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100893 /* should block plug */
894 unsigned plug : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700895 /* hash wq insertion if file is a regular file */
896 unsigned hash_reg_file : 1;
897 /* unbound wq insertion if file is a non-regular file */
898 unsigned unbound_nonreg_file : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700899 /* set if opcode supports polled "wait" */
900 unsigned pollin : 1;
901 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700902 /* op supports buffer selection */
903 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000904 /* do prep async if is going to be punted */
905 unsigned needs_async_setup : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100906 /* opcode is not supported by this kernel */
907 unsigned not_supported : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700908 /* size of async data needed, if any */
909 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700910};
911
Jens Axboe09186822020-10-13 15:01:40 -0600912static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300913 [IORING_OP_NOP] = {},
914 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700915 .needs_file = 1,
916 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700917 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700918 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000919 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600920 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700921 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700922 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300923 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700924 .needs_file = 1,
925 .hash_reg_file = 1,
926 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700927 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000928 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600929 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700930 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700931 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300932 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700933 .needs_file = 1,
934 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700936 .needs_file = 1,
937 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700938 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600939 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700940 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700941 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300942 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700943 .needs_file = 1,
944 .hash_reg_file = 1,
945 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700946 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600947 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700948 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700949 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300950 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700951 .needs_file = 1,
952 .unbound_nonreg_file = 1,
953 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_POLL_REMOVE] = {},
955 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700956 .needs_file = 1,
957 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300958 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700959 .needs_file = 1,
960 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700961 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000962 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700963 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700964 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300965 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700966 .needs_file = 1,
967 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700968 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700969 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000970 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700971 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700972 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300973 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700974 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700975 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000976 [IORING_OP_TIMEOUT_REMOVE] = {
977 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000978 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300979 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700980 .needs_file = 1,
981 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700982 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700983 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300984 [IORING_OP_ASYNC_CANCEL] = {},
985 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700986 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700987 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300988 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700989 .needs_file = 1,
990 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700991 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000992 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700993 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700994 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300995 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700996 .needs_file = 1,
997 },
Jens Axboe44526be2021-02-15 13:32:18 -0700998 [IORING_OP_OPENAT] = {},
999 [IORING_OP_CLOSE] = {},
1000 [IORING_OP_FILES_UPDATE] = {},
1001 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001002 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001003 .needs_file = 1,
1004 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001005 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001006 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001007 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001008 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001009 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001010 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001011 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001012 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001013 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001014 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001015 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001016 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001017 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001018 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001019 .needs_file = 1,
1020 },
Jens Axboe44526be2021-02-15 13:32:18 -07001021 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001022 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001023 .needs_file = 1,
1024 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001025 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001026 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001027 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001028 .needs_file = 1,
1029 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001030 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001031 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001032 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001033 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001034 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001035 [IORING_OP_EPOLL_CTL] = {
1036 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001037 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001038 [IORING_OP_SPLICE] = {
1039 .needs_file = 1,
1040 .hash_reg_file = 1,
1041 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001042 },
1043 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001044 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001045 [IORING_OP_TEE] = {
1046 .needs_file = 1,
1047 .hash_reg_file = 1,
1048 .unbound_nonreg_file = 1,
1049 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001050 [IORING_OP_SHUTDOWN] = {
1051 .needs_file = 1,
1052 },
Jens Axboe44526be2021-02-15 13:32:18 -07001053 [IORING_OP_RENAMEAT] = {},
1054 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001055 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001056 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001057 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001058};
1059
Pavel Begunkov0756a862021-08-15 10:40:25 +01001060/* requests with any of those set should undergo io_disarm_next() */
1061#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1062
Pavel Begunkov7a612352021-03-09 00:37:59 +00001063static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001064static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001065static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1066 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001067 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001068static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001069
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001070static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001071 s32 res, u32 cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001072static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001073static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001074static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001075static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001076static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001077 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001078 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001079static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001080static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001081 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001082static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001083static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001084
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001085static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkovc4501782021-09-08 16:40:52 +01001086static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001087static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001088
Pavel Begunkovb9445592021-08-25 12:25:45 +01001089static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1090 unsigned int issue_flags, u32 slot_index);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01001091static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1092
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001093static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001094
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095static struct kmem_cache *req_cachep;
1096
Jens Axboe09186822020-10-13 15:01:40 -06001097static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098
1099struct sock *io_uring_get_socket(struct file *file)
1100{
1101#if defined(CONFIG_UNIX)
1102 if (file->f_op == &io_uring_fops) {
1103 struct io_ring_ctx *ctx = file->private_data;
1104
1105 return ctx->ring_sock->sk;
1106 }
1107#endif
1108 return NULL;
1109}
1110EXPORT_SYMBOL(io_uring_get_socket);
1111
Pavel Begunkovf237c302021-08-18 12:42:46 +01001112static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1113{
1114 if (!*locked) {
1115 mutex_lock(&ctx->uring_lock);
1116 *locked = true;
1117 }
1118}
1119
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001120#define io_for_each_link(pos, head) \
1121 for (pos = (head); pos; pos = pos->link)
1122
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001123/*
1124 * Shamelessly stolen from the mm implementation of page reference checking,
1125 * see commit f958d7b528b1 for details.
1126 */
1127#define req_ref_zero_or_close_to_overflow(req) \
1128 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1129
1130static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1131{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001132 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001133 return atomic_inc_not_zero(&req->refs);
1134}
1135
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001136static inline bool req_ref_put_and_test(struct io_kiocb *req)
1137{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001138 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1139 return true;
1140
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001141 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1142 return atomic_dec_and_test(&req->refs);
1143}
1144
1145static inline void req_ref_put(struct io_kiocb *req)
1146{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001147 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001148 WARN_ON_ONCE(req_ref_put_and_test(req));
1149}
1150
1151static inline void req_ref_get(struct io_kiocb *req)
1152{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001153 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001154 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1155 atomic_inc(&req->refs);
1156}
1157
Pavel Begunkovc4501782021-09-08 16:40:52 +01001158static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1159{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001160 if (!wq_list_empty(&ctx->submit_state.compl_reqs))
Pavel Begunkovc4501782021-09-08 16:40:52 +01001161 __io_submit_flush_completions(ctx);
1162}
1163
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001164static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001165{
1166 if (!(req->flags & REQ_F_REFCOUNT)) {
1167 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001168 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001169 }
1170}
1171
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001172static inline void io_req_set_refcount(struct io_kiocb *req)
1173{
1174 __io_req_set_refcount(req, 1);
1175}
1176
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001177static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001178{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001179 struct io_ring_ctx *ctx = req->ctx;
1180
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001181 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001182 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001183 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001184 }
1185}
1186
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001187static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1188{
1189 bool got = percpu_ref_tryget(ref);
1190
1191 /* already at zero, wait for ->release() */
1192 if (!got)
1193 wait_for_completion(compl);
1194 percpu_ref_resurrect(ref);
1195 if (got)
1196 percpu_ref_put(ref);
1197}
1198
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001199static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1200 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001201{
1202 struct io_kiocb *req;
1203
Pavel Begunkov68207682021-03-22 01:58:25 +00001204 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001205 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001206 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001207 return true;
1208
1209 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001210 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001211 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001212 }
1213 return false;
1214}
1215
Pavel Begunkovd886e182021-10-04 20:02:56 +01001216static inline bool req_has_async_data(struct io_kiocb *req)
1217{
1218 return req->flags & REQ_F_ASYNC_DATA;
1219}
1220
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001221static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001222{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001223 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001224}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001225
Hao Xua8295b92021-08-27 17:46:09 +08001226static inline void req_fail_link_node(struct io_kiocb *req, int res)
1227{
1228 req_set_fail(req);
1229 req->result = res;
1230}
1231
Pavel Begunkovc0724812021-10-04 20:02:54 +01001232static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001233{
1234 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1235
Jens Axboe0f158b42020-05-14 17:18:39 -06001236 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001237}
1238
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001239static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1240{
1241 return !req->timeout.off;
1242}
1243
Pavel Begunkovc0724812021-10-04 20:02:54 +01001244static __cold void io_fallback_req_func(struct work_struct *work)
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001245{
1246 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1247 fallback_work.work);
1248 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1249 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001250 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001251
1252 percpu_ref_get(&ctx->refs);
1253 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001254 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001255
Pavel Begunkovf237c302021-08-18 12:42:46 +01001256 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001257 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001258 mutex_unlock(&ctx->uring_lock);
1259 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001260 percpu_ref_put(&ctx->refs);
1261}
1262
Pavel Begunkovc0724812021-10-04 20:02:54 +01001263static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264{
1265 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001266 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267
1268 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1269 if (!ctx)
1270 return NULL;
1271
Jens Axboe78076bb2019-12-04 19:56:40 -07001272 /*
1273 * Use 5 bits less than the max cq entries, that should give us around
1274 * 32 entries per hash list if totally full and uniformly spread.
1275 */
1276 hash_bits = ilog2(p->cq_entries);
1277 hash_bits -= 5;
1278 if (hash_bits <= 0)
1279 hash_bits = 1;
1280 ctx->cancel_hash_bits = hash_bits;
1281 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1282 GFP_KERNEL);
1283 if (!ctx->cancel_hash)
1284 goto err;
1285 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1286
Pavel Begunkov62248432021-04-28 13:11:29 +01001287 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1288 if (!ctx->dummy_ubuf)
1289 goto err;
1290 /* set invalid range, so io_import_fixed() fails meeting it */
1291 ctx->dummy_ubuf->ubuf = -1UL;
1292
Roman Gushchin21482892019-05-07 10:01:48 -07001293 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001294 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1295 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001296
1297 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001298 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001299 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001300 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001301 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001302 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001303 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001304 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001305 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001306 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001307 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01001308 INIT_WQ_LIST(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001309 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001310 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001311 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001312 spin_lock_init(&ctx->rsrc_ref_lock);
1313 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001314 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1315 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001316 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001317 ctx->submit_state.free_list.next = NULL;
1318 INIT_WQ_LIST(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001319 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001320 INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001321 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001322err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001323 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001324 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001325 kfree(ctx);
1326 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001327}
1328
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001329static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1330{
1331 struct io_rings *r = ctx->rings;
1332
1333 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1334 ctx->cq_extra--;
1335}
1336
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001337static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001338{
Jens Axboe2bc99302020-07-09 09:43:27 -06001339 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1340 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001341
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001342 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001343 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001344
Bob Liu9d858b22019-11-13 18:06:25 +08001345 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001346}
1347
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001348#define FFS_ASYNC_READ 0x1UL
1349#define FFS_ASYNC_WRITE 0x2UL
1350#ifdef CONFIG_64BIT
1351#define FFS_ISREG 0x4UL
1352#else
1353#define FFS_ISREG 0x0UL
1354#endif
1355#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1356
1357static inline bool io_req_ffs_set(struct io_kiocb *req)
1358{
1359 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1360}
1361
Pavel Begunkovc0724812021-10-04 20:02:54 +01001362static inline void io_req_track_inflight(struct io_kiocb *req)
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001363{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001364 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001365 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001366 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001367 }
1368}
1369
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001370static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1371{
1372 req->flags &= ~REQ_F_LINK_TIMEOUT;
1373}
1374
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001375static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1376{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001377 if (WARN_ON_ONCE(!req->link))
1378 return NULL;
1379
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001380 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1381 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001382
1383 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001384 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001385 __io_req_set_refcount(req->link, 2);
1386 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001387}
1388
1389static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1390{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001391 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001392 return NULL;
1393 return __io_prep_linked_timeout(req);
1394}
1395
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001396static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001397{
Jens Axboed3656342019-12-18 09:50:26 -07001398 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001399 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001400
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001401 if (!(req->flags & REQ_F_CREDS)) {
1402 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001403 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001404 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001405
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001406 req->work.list.next = NULL;
1407 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001408 if (req->flags & REQ_F_FORCE_ASYNC)
1409 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1410
Jens Axboed3656342019-12-18 09:50:26 -07001411 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001412 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001413 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001414 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001415 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001416 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001417 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001418
1419 switch (req->opcode) {
1420 case IORING_OP_SPLICE:
1421 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001422 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1423 req->work.flags |= IO_WQ_WORK_UNBOUND;
1424 break;
1425 }
Jens Axboe561fb042019-10-24 07:25:42 -06001426}
1427
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001428static void io_prep_async_link(struct io_kiocb *req)
1429{
1430 struct io_kiocb *cur;
1431
Pavel Begunkov44eff402021-07-26 14:14:31 +01001432 if (req->flags & REQ_F_LINK_TIMEOUT) {
1433 struct io_ring_ctx *ctx = req->ctx;
1434
Jens Axboe79ebeae2021-08-10 15:18:27 -06001435 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001436 io_for_each_link(cur, req)
1437 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001438 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001439 } else {
1440 io_for_each_link(cur, req)
1441 io_prep_async_work(cur);
1442 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001443}
1444
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001445static inline void io_req_add_compl_list(struct io_kiocb *req)
1446{
1447 struct io_submit_state *state = &req->ctx->submit_state;
1448
1449 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
1450}
1451
Pavel Begunkovf237c302021-08-18 12:42:46 +01001452static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001453{
Jackie Liua197f662019-11-08 08:09:12 -07001454 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001455 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001456 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001457
Pavel Begunkovf237c302021-08-18 12:42:46 +01001458 /* must not take the lock, NULL it as a precaution */
1459 locked = NULL;
1460
Jens Axboe3bfe6102021-02-16 14:15:30 -07001461 BUG_ON(!tctx);
1462 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001463
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001464 /* init ->work of the whole link before punting */
1465 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001466
1467 /*
1468 * Not expected to happen, but if we do have a bug where this _can_
1469 * happen, catch it here and ensure the request is marked as
1470 * canceled. That will make io-wq go through the usual work cancel
1471 * procedure rather than attempt to run this request (or create a new
1472 * worker for it).
1473 */
1474 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1475 req->work.flags |= IO_WQ_WORK_CANCEL;
1476
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001477 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1478 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001479 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001480 if (link)
1481 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001482}
1483
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001484static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001485 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001486 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001487{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001488 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001489
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001490 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001491 if (status)
1492 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001493 atomic_set(&req->ctx->cq_timeouts,
1494 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001495 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001496 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001497 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001498 }
1499}
1500
Pavel Begunkovc0724812021-10-04 20:02:54 +01001501static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001502{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001503 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001504 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1505 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001506
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001507 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001508 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001509 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001510 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001511 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001512 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001513}
1514
Pavel Begunkovc0724812021-10-04 20:02:54 +01001515static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001516 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001517{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001518 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001519
Jens Axboe79ebeae2021-08-10 15:18:27 -06001520 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001521 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001522 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001523 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001524 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001525
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001526 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001527 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001528
1529 /*
1530 * Since seq can easily wrap around over time, subtract
1531 * the last seq at which timeouts were flushed before comparing.
1532 * Assuming not more than 2^31-1 events have happened since,
1533 * these subtractions won't have wrapped, so we can check if
1534 * target is in [last_seq, current_seq] by comparing the two.
1535 */
1536 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1537 events_got = seq - ctx->cq_last_tm_flush;
1538 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001539 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001540
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001541 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001542 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001543 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001544 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001545 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001546}
1547
Pavel Begunkovc0724812021-10-04 20:02:54 +01001548static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001549{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001550 if (ctx->off_timeout_used)
1551 io_flush_timeouts(ctx);
1552 if (ctx->drain_active)
1553 io_queue_deferred(ctx);
1554}
1555
1556static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1557{
1558 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1559 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001560 /* order cqe stores with ring update */
1561 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001562}
1563
Jens Axboe90554202020-09-03 12:12:41 -06001564static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1565{
1566 struct io_rings *r = ctx->rings;
1567
Pavel Begunkova566c552021-05-16 22:58:08 +01001568 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001569}
1570
Pavel Begunkov888aae22021-01-19 13:32:39 +00001571static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1572{
1573 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1574}
1575
Pavel Begunkovd068b502021-05-16 22:58:11 +01001576static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577{
Hristo Venev75b28af2019-08-26 17:23:46 +00001578 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001579 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001580
Stefan Bühler115e12e2019-04-24 23:54:18 +02001581 /*
1582 * writes to the cq entry need to come after reading head; the
1583 * control dependency is enough as we're using WRITE_ONCE to
1584 * fill the cq entry
1585 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001586 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001587 return NULL;
1588
Pavel Begunkov888aae22021-01-19 13:32:39 +00001589 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001590 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001591}
1592
Jens Axboef2842ab2020-01-08 11:04:00 -07001593static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1594{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001595 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001596 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001597 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1598 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001599 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001600}
1601
Jens Axboe2c5d7632021-08-21 07:21:19 -06001602/*
1603 * This should only get called when at least one event has been posted.
1604 * Some applications rely on the eventfd notification count only changing
1605 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1606 * 1:1 relationship between how many times this function is called (and
1607 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1608 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001609static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001610{
Jens Axboe5fd46172021-08-06 14:04:31 -06001611 /*
1612 * wake_up_all() may seem excessive, but io_wake_function() and
1613 * io_should_wake() handle the termination of the loop and only
1614 * wake as many waiters as we need to.
1615 */
1616 if (wq_has_sleeper(&ctx->cq_wait))
1617 wake_up_all(&ctx->cq_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001618 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001619 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001620}
1621
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001622static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1623{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001624 /* see waitqueue_active() comment */
1625 smp_mb();
1626
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001627 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001628 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001629 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001630 }
1631 if (io_should_trigger_evfd(ctx))
1632 eventfd_signal(ctx->cq_ev_fd, 1);
1633}
1634
Jens Axboec4a2ed72019-11-21 21:01:26 -07001635/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001636static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001637{
Jens Axboeb18032b2021-01-24 16:58:56 -07001638 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001639
Pavel Begunkova566c552021-05-16 22:58:08 +01001640 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001641 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001642
Jens Axboeb18032b2021-01-24 16:58:56 -07001643 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001644 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001645 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001646 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001647 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001648
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001649 if (!cqe && !force)
1650 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001651 ocqe = list_first_entry(&ctx->cq_overflow_list,
1652 struct io_overflow_cqe, list);
1653 if (cqe)
1654 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1655 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001656 io_account_cq_overflow(ctx);
1657
Jens Axboeb18032b2021-01-24 16:58:56 -07001658 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001659 list_del(&ocqe->list);
1660 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001661 }
1662
Pavel Begunkov09e88402020-12-17 00:24:38 +00001663 all_flushed = list_empty(&ctx->cq_overflow_list);
1664 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001665 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001666 WRITE_ONCE(ctx->rings->sq_flags,
1667 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001668 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001669
Jens Axboeb18032b2021-01-24 16:58:56 -07001670 if (posted)
1671 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001672 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001673 if (posted)
1674 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001675 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001676}
1677
Pavel Begunkov90f67362021-08-09 20:18:12 +01001678static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001679{
Jens Axboeca0a2652021-03-04 17:15:48 -07001680 bool ret = true;
1681
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001682 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001683 /* iopoll syncs against uring_lock, not completion_lock */
1684 if (ctx->flags & IORING_SETUP_IOPOLL)
1685 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001686 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001687 if (ctx->flags & IORING_SETUP_IOPOLL)
1688 mutex_unlock(&ctx->uring_lock);
1689 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001690
1691 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001692}
1693
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001694/* must to be called somewhat shortly after putting a request */
1695static inline void io_put_task(struct task_struct *task, int nr)
1696{
1697 struct io_uring_task *tctx = task->io_uring;
1698
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001699 if (likely(task == current)) {
1700 tctx->cached_refs += nr;
1701 } else {
1702 percpu_counter_sub(&tctx->inflight, nr);
1703 if (unlikely(atomic_read(&tctx->in_idle)))
1704 wake_up(&tctx->wait);
1705 put_task_struct_many(task, nr);
1706 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001707}
1708
Pavel Begunkov9a108672021-08-27 11:55:01 +01001709static void io_task_refs_refill(struct io_uring_task *tctx)
1710{
1711 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1712
1713 percpu_counter_add(&tctx->inflight, refill);
1714 refcount_add(refill, &current->usage);
1715 tctx->cached_refs += refill;
1716}
1717
1718static inline void io_get_task_refs(int nr)
1719{
1720 struct io_uring_task *tctx = current->io_uring;
1721
1722 tctx->cached_refs -= nr;
1723 if (unlikely(tctx->cached_refs < 0))
1724 io_task_refs_refill(tctx);
1725}
1726
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001727static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001728 s32 res, u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001730 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001732 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1733 if (!ocqe) {
1734 /*
1735 * If we're in ring overflow flush mode, or in task cancel mode,
1736 * or cannot allocate an overflow entry, then we need to drop it
1737 * on the floor.
1738 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001739 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001740 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001742 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001743 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001744 WRITE_ONCE(ctx->rings->sq_flags,
1745 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1746
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001747 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001748 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001749 ocqe->cqe.res = res;
1750 ocqe->cqe.flags = cflags;
1751 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1752 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001753}
1754
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001755static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001756 s32 res, u32 cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001757{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758 struct io_uring_cqe *cqe;
1759
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001760 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001761
1762 /*
1763 * If we can't get a cq entry, userspace overflowed the
1764 * submission (by quite a lot). Increment the overflow count in
1765 * the ring.
1766 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001767 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001768 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001769 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001770 WRITE_ONCE(cqe->res, res);
1771 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001772 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001773 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001774 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775}
1776
Pavel Begunkov8d133262021-04-11 01:46:33 +01001777/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001778static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001779 s32 res, u32 cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001780{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001781 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001782}
1783
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001784static void io_req_complete_post(struct io_kiocb *req, s32 res,
1785 u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001787 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788
Jens Axboe79ebeae2021-08-10 15:18:27 -06001789 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001790 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001791 /*
1792 * If we're the last reference to this request, add to our locked
1793 * free_list cache.
1794 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001795 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001796 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001797 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001798 io_disarm_next(req);
1799 if (req->link) {
1800 io_req_task_queue(req->link);
1801 req->link = NULL;
1802 }
1803 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001804 io_dismantle_req(req);
1805 io_put_task(req->task, 1);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001806 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001807 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001808 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001809 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001810 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001811 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001812}
1813
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001814static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
1815 u32 cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001816{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001817 req->result = res;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +01001818 req->cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001819 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001820}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001821
Pavel Begunkov889fca72021-02-10 00:03:09 +00001822static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001823 s32 res, u32 cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001824{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001825 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1826 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001827 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001828 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001829}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001830
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001831static inline void io_req_complete(struct io_kiocb *req, s32 res)
Jens Axboee1e16092020-06-22 09:17:17 -06001832{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001833 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001834}
1835
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001836static void io_req_complete_failed(struct io_kiocb *req, s32 res)
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001837{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001838 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001839 io_req_complete_post(req, res, 0);
1840}
1841
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001842static void io_req_complete_fail_submit(struct io_kiocb *req)
1843{
1844 /*
1845 * We don't submit, fail them all, for that replace hardlinks with
1846 * normal links. Extra REQ_F_LINK is tolerated.
1847 */
1848 req->flags &= ~REQ_F_HARDLINK;
1849 req->flags |= REQ_F_LINK;
1850 io_req_complete_failed(req, req->result);
1851}
1852
Pavel Begunkov864ea922021-08-09 13:04:08 +01001853/*
1854 * Don't initialise the fields below on every allocation, but do that in
1855 * advance and keep them valid across allocations.
1856 */
1857static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1858{
1859 req->ctx = ctx;
1860 req->link = NULL;
1861 req->async_data = NULL;
1862 /* not necessary, but safer to zero */
1863 req->result = 0;
1864}
1865
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001866static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001867 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001868{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001869 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001870 wq_list_splice(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001871 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001872 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001873}
1874
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001875/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001876static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001877{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001878 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001879
Jens Axboec7dae4b2021-02-09 19:53:37 -07001880 /*
1881 * If we have more than a batch's worth of requests in our IRQ side
1882 * locked cache, grab the lock and move them over to our submission
1883 * side cache.
1884 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001885 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001886 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001887 return !!state->free_list.next;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001888}
1889
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001890/*
1891 * A request might get retired back into the request caches even before opcode
1892 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1893 * Because of that, io_alloc_req() should be called only under ->uring_lock
1894 * and with extra caution to not get a request that is still worked on.
1895 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01001896static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001897 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001899 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001900 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001901 void *reqs[IO_REQ_ALLOC_BATCH];
1902 struct io_kiocb *req;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001903 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001904
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001905 if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001906 return true;
Jens Axboe2579f912019-01-09 09:10:43 -07001907
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001908 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001909
Pavel Begunkov864ea922021-08-09 13:04:08 +01001910 /*
1911 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1912 * retry single alloc to be on the safe side.
1913 */
1914 if (unlikely(ret <= 0)) {
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001915 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1916 if (!reqs[0])
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001917 return false;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001918 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001920
Pavel Begunkov37f0e762021-10-04 20:02:53 +01001921 percpu_ref_get_many(&ctx->refs, ret);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001922 for (i = 0; i < ret; i++) {
1923 req = reqs[i];
1924
1925 io_preinit_req(req, ctx);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001926 wq_stack_add_head(&req->comp_list, &state->free_list);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001927 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001928 return true;
1929}
1930
1931static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
1932{
1933 if (unlikely(!ctx->submit_state.free_list.next))
1934 return __io_alloc_req_refill(ctx);
1935 return true;
1936}
1937
1938static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
1939{
1940 struct io_wq_work_node *node;
1941
1942 node = wq_stack_extract(&ctx->submit_state.free_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001943 return container_of(node, struct io_kiocb, comp_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001944}
1945
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001946static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001947{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001948 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001949 fput(file);
1950}
1951
Pavel Begunkov6b639522021-09-08 16:40:50 +01001952static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001953{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001954 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001955
Pavel Begunkov867f8fa2021-10-04 20:02:58 +01001956 if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001957 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001958 if (!(flags & REQ_F_FIXED_FILE))
1959 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001960 if (req->fixed_rsrc_refs)
1961 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001962}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001963
Pavel Begunkovc0724812021-10-04 20:02:54 +01001964static __cold void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001965{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001966 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001967
Pavel Begunkov216578e2020-10-13 09:44:00 +01001968 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001969 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001970
Jens Axboe79ebeae2021-08-10 15:18:27 -06001971 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001972 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001973 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001974 spin_unlock(&ctx->completion_lock);
Jens Axboee65ef562019-03-12 10:16:44 -06001975}
1976
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001977static inline void io_remove_next_linked(struct io_kiocb *req)
1978{
1979 struct io_kiocb *nxt = req->link;
1980
1981 req->link = nxt->link;
1982 nxt->link = NULL;
1983}
1984
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001985static bool io_kill_linked_timeout(struct io_kiocb *req)
1986 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06001987 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001988{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001989 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001990
Pavel Begunkovb97e7362021-08-15 10:40:23 +01001991 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001992 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001993
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001994 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001995 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001996 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001997 list_del(&link->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001998 io_cqring_fill_event(link->ctx, link->user_data,
1999 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002000 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002001 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002002 }
2003 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002004 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002005}
2006
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002007static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002008 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002009{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002010 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06002011
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002012 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002013 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002014 long res = -ECANCELED;
2015
2016 if (link->flags & REQ_F_FAIL)
2017 res = link->result;
2018
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002019 nxt = link->link;
2020 link->link = NULL;
2021
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002022 trace_io_uring_fail_link(req, link);
Hao Xua8295b92021-08-27 17:46:09 +08002023 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002024 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002025 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002026 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002027}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002028
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002029static bool io_disarm_next(struct io_kiocb *req)
2030 __must_hold(&req->ctx->completion_lock)
2031{
2032 bool posted = false;
2033
Pavel Begunkov0756a862021-08-15 10:40:25 +01002034 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2035 struct io_kiocb *link = req->link;
2036
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002037 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002038 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2039 io_remove_next_linked(req);
2040 io_cqring_fill_event(link->ctx, link->user_data,
2041 -ECANCELED, 0);
2042 io_put_req_deferred(link);
2043 posted = true;
2044 }
2045 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002046 struct io_ring_ctx *ctx = req->ctx;
2047
2048 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002049 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002050 spin_unlock_irq(&ctx->timeout_lock);
2051 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002052 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002053 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002054 posted |= (req->link != NULL);
2055 io_fail_links(req);
2056 }
2057 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002058}
2059
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002060static void __io_req_find_next_prep(struct io_kiocb *req)
2061{
2062 struct io_ring_ctx *ctx = req->ctx;
2063 bool posted;
2064
2065 spin_lock(&ctx->completion_lock);
2066 posted = io_disarm_next(req);
2067 if (posted)
2068 io_commit_cqring(req->ctx);
2069 spin_unlock(&ctx->completion_lock);
2070 if (posted)
2071 io_cqring_ev_posted(ctx);
2072}
2073
2074static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002075{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002076 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002077
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002078 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2079 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002080 /*
2081 * If LINK is set, we have dependent requests in this chain. If we
2082 * didn't fail this request, queue the first one up, moving any other
2083 * dependencies to the next request. In case of failure, fail the rest
2084 * of the chain.
2085 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002086 if (unlikely(req->flags & IO_DISARM_MASK))
2087 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002088 nxt = req->link;
2089 req->link = NULL;
2090 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002091}
Jens Axboe2665abf2019-11-05 12:40:47 -07002092
Pavel Begunkovf237c302021-08-18 12:42:46 +01002093static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002094{
2095 if (!ctx)
2096 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002097 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002098 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002099 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002100 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002101 }
2102 percpu_ref_put(&ctx->refs);
2103}
2104
Jens Axboe7cbf1722021-02-10 00:03:20 +00002105static void tctx_task_work(struct callback_head *cb)
2106{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002107 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002108 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002109 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2110 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002111
Pavel Begunkov16f72072021-06-17 18:14:09 +01002112 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002113 struct io_wq_work_node *node;
2114
Pavel Begunkovc4501782021-09-08 16:40:52 +01002115 if (!tctx->task_list.first && locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002116 io_submit_flush_completions(ctx);
2117
Pavel Begunkov3f184072021-06-17 18:14:06 +01002118 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002119 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002120 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002121 if (!node)
2122 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002123 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002124 if (!node)
2125 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002126
Pavel Begunkov6294f362021-08-10 17:53:55 +01002127 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002128 struct io_wq_work_node *next = node->next;
2129 struct io_kiocb *req = container_of(node, struct io_kiocb,
2130 io_task_work.node);
2131
2132 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002133 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002134 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002135 /* if not contended, grab and improve batching */
2136 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002137 percpu_ref_get(&ctx->refs);
2138 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002139 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002140 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002141 } while (node);
2142
Jens Axboe7cbf1722021-02-10 00:03:20 +00002143 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002144 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002145
Pavel Begunkovf237c302021-08-18 12:42:46 +01002146 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002147}
2148
Pavel Begunkove09ee512021-07-01 13:26:05 +01002149static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002150{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002151 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002152 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002153 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002154 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002155 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002156 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002157
2158 WARN_ON_ONCE(!tctx);
2159
Jens Axboe0b81e802021-02-16 10:33:53 -07002160 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002161 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002162 running = tctx->task_running;
2163 if (!running)
2164 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002165 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002166
2167 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002168 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002169 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002170
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002171 /*
2172 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2173 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2174 * processing task_work. There's no reliable way to tell if TWA_RESUME
2175 * will do the job.
2176 */
2177 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002178 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2179 if (notify == TWA_NONE)
2180 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002181 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002182 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002183
Pavel Begunkove09ee512021-07-01 13:26:05 +01002184 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002185 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002186 node = tctx->task_list.first;
2187 INIT_WQ_LIST(&tctx->task_list);
2188 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002189
Pavel Begunkove09ee512021-07-01 13:26:05 +01002190 while (node) {
2191 req = container_of(node, struct io_kiocb, io_task_work.node);
2192 node = node->next;
2193 if (llist_add(&req->io_task_work.fallback_node,
2194 &req->ctx->fallback_llist))
2195 schedule_delayed_work(&req->ctx->fallback_work, 1);
2196 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002197}
2198
Pavel Begunkovf237c302021-08-18 12:42:46 +01002199static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002200{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002201 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002202
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002203 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002204 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002205 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002206}
2207
Pavel Begunkovf237c302021-08-18 12:42:46 +01002208static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002209{
2210 struct io_ring_ctx *ctx = req->ctx;
2211
Pavel Begunkovf237c302021-08-18 12:42:46 +01002212 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002213 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002214 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002215 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002216 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002217 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002218}
2219
Pavel Begunkova3df76982021-02-18 22:32:52 +00002220static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2221{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002222 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002223 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002224 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002225}
2226
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002227static void io_req_task_queue(struct io_kiocb *req)
2228{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002229 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002230 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002231}
2232
Jens Axboe773af692021-07-27 10:25:55 -06002233static void io_req_task_queue_reissue(struct io_kiocb *req)
2234{
2235 req->io_task_work.func = io_queue_async_work;
2236 io_req_task_work_add(req);
2237}
2238
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002239static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002240{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002241 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002242
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002243 if (nxt)
2244 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002245}
2246
Jens Axboe9e645e112019-05-10 16:07:28 -06002247static void io_free_req(struct io_kiocb *req)
2248{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002249 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002250 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002251}
2252
Pavel Begunkovf237c302021-08-18 12:42:46 +01002253static void io_free_req_work(struct io_kiocb *req, bool *locked)
2254{
2255 io_free_req(req);
2256}
2257
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002258static void io_free_batch_list(struct io_ring_ctx *ctx,
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002259 struct io_wq_work_node *node)
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002260 __must_hold(&ctx->uring_lock)
2261{
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002262 struct task_struct *task = NULL;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002263 int task_refs = 0;
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002264
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002265 do {
2266 struct io_kiocb *req = container_of(node, struct io_kiocb,
2267 comp_list);
2268
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002269 if (!req_ref_put_and_test(req)) {
2270 node = req->comp_list.next;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002271 continue;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002272 }
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002273
2274 io_queue_next(req);
2275 io_dismantle_req(req);
2276
2277 if (req->task != task) {
2278 if (task)
2279 io_put_task(task, task_refs);
2280 task = req->task;
2281 task_refs = 0;
2282 }
2283 task_refs++;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002284 node = req->comp_list.next;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002285 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002286 } while (node);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002287
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002288 if (task)
2289 io_put_task(task, task_refs);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002290}
2291
Pavel Begunkovc4501782021-09-08 16:40:52 +01002292static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002293 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002294{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002295 struct io_wq_work_node *node, *prev;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002296 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002297
Jens Axboe79ebeae2021-08-10 15:18:27 -06002298 spin_lock(&ctx->completion_lock);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002299 wq_list_for_each(node, prev, &state->compl_reqs) {
2300 struct io_kiocb *req = container_of(node, struct io_kiocb,
2301 comp_list);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002302
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002303 __io_cqring_fill_event(ctx, req->user_data, req->result,
Pavel Begunkovd17e56e2021-10-04 20:02:57 +01002304 req->cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002305 }
2306 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002307 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002308 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002309
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002310 io_free_batch_list(ctx, state->compl_reqs.first);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002311 INIT_WQ_LIST(&state->compl_reqs);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002312}
2313
Jens Axboeba816ad2019-09-28 11:36:45 -06002314/*
2315 * Drop reference to request, return next in chain (if there is one) if this
2316 * was the last reference to this request.
2317 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002318static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002319{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002320 struct io_kiocb *nxt = NULL;
2321
Jens Axboede9b4cc2021-02-24 13:28:27 -07002322 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002323 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002324 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002325 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002326 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002327}
2328
Pavel Begunkov0d850352021-03-19 17:22:37 +00002329static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002330{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002331 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002332 io_free_req(req);
2333}
2334
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002335static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002336{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002337 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002338 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002339 io_req_task_work_add(req);
2340 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002341}
2342
Pavel Begunkov6c503152021-01-04 20:36:36 +00002343static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002344{
2345 /* See comment at the top of this file */
2346 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002347 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002348}
2349
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002350static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2351{
2352 struct io_rings *rings = ctx->rings;
2353
2354 /* make sure SQ entry isn't read before tail */
2355 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2356}
2357
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002358static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002359{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002360 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002361
Jens Axboebcda7ba2020-02-23 16:42:51 -07002362 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2363 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002364 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002365 kfree(kbuf);
2366 return cflags;
2367}
2368
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002369static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2370{
Pavel Begunkovae421d92021-08-17 20:28:08 +01002371 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2372 return 0;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01002373 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002374}
2375
Jens Axboe4c6e2772020-07-01 11:29:10 -06002376static inline bool io_run_task_work(void)
2377{
Nadav Amitef98eb02021-08-07 17:13:41 -07002378 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002379 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002380 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002381 return true;
2382 }
2383
2384 return false;
2385}
2386
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002387static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
Jens Axboedef596e2019-01-09 08:59:42 -07002388{
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002389 struct io_wq_work_node *pos, *start, *prev;
Christoph Hellwigd729cf92021-10-12 13:12:20 +02002390 unsigned int poll_flags = BLK_POLL_NOSLEEP;
Jens Axboeb688f112021-10-12 09:28:46 -06002391 DEFINE_IO_COMP_BATCH(iob);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002392 int nr_events = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002393
2394 /*
2395 * Only spin for completions if we don't have multiple devices hanging
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002396 * off our complete list.
Jens Axboedef596e2019-01-09 08:59:42 -07002397 */
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002398 if (ctx->poll_multi_queue || force_nonspin)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002399 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002400
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002401 wq_list_for_each(pos, start, &ctx->iopoll_list) {
2402 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002403 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002404 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002405
2406 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002407 * Move completed and retryable entries to our local lists.
2408 * If we find a request that requires polling, break out
2409 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002410 */
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002411 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002412 break;
2413
Jens Axboeb688f112021-10-12 09:28:46 -06002414 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002415 if (unlikely(ret < 0))
2416 return ret;
2417 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002418 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002419
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002420 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002421 if (!rq_list_empty(iob.req_list) ||
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002422 READ_ONCE(req->iopoll_completed))
2423 break;
Jens Axboedef596e2019-01-09 08:59:42 -07002424 }
2425
Jens Axboeb688f112021-10-12 09:28:46 -06002426 if (!rq_list_empty(iob.req_list))
2427 iob.complete(&iob);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002428 else if (!pos)
2429 return 0;
2430
2431 prev = start;
2432 wq_list_for_each_resume(pos, prev) {
2433 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
2434
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002435 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
2436 if (!smp_load_acquire(&req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002437 break;
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002438 __io_cqring_fill_event(ctx, req->user_data, req->result,
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002439 io_put_rw_kbuf(req));
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002440 nr_events++;
2441 }
Jens Axboedef596e2019-01-09 08:59:42 -07002442
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002443 if (unlikely(!nr_events))
2444 return 0;
2445
2446 io_commit_cqring(ctx);
2447 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002448 pos = start ? start->next : ctx->iopoll_list.first;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002449 wq_list_cut(&ctx->iopoll_list, prev, start);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002450 io_free_batch_list(ctx, pos);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002451 return nr_events;
Jens Axboedef596e2019-01-09 08:59:42 -07002452}
2453
2454/*
Jens Axboedef596e2019-01-09 08:59:42 -07002455 * We can't just wait for polled events to come to us, we have to actively
2456 * find and complete them.
2457 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01002458static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002459{
2460 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2461 return;
2462
2463 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002464 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002465 /* let it sleep and repeat later if can't complete a request */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002466 if (io_do_iopoll(ctx, true) == 0)
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002467 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002468 /*
2469 * Ensure we allow local-to-the-cpu processing to take place,
2470 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002471 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002472 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002473 if (need_resched()) {
2474 mutex_unlock(&ctx->uring_lock);
2475 cond_resched();
2476 mutex_lock(&ctx->uring_lock);
2477 }
Jens Axboedef596e2019-01-09 08:59:42 -07002478 }
2479 mutex_unlock(&ctx->uring_lock);
2480}
2481
Pavel Begunkov7668b922020-07-07 16:36:21 +03002482static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002483{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002484 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002485 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002486
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002487 /*
2488 * We disallow the app entering submit/complete with polling, but we
2489 * still need to lock the ring to prevent racing with polled issue
2490 * that got punted to a workqueue.
2491 */
2492 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002493 /*
2494 * Don't enter poll loop if we already have events pending.
2495 * If we do, we can potentially be spinning for commands that
2496 * already triggered a CQE (eg in error).
2497 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002498 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002499 __io_cqring_overflow_flush(ctx, false);
2500 if (io_cqring_events(ctx))
2501 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002502 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002503 /*
2504 * If a submit got punted to a workqueue, we can have the
2505 * application entering polling for a command before it gets
2506 * issued. That app will hold the uring_lock for the duration
2507 * of the poll right here, so we need to take a breather every
2508 * now and then to ensure that the issue has a chance to add
2509 * the poll to the issued list. Otherwise we can spin here
2510 * forever, while the workqueue is stuck trying to acquire the
2511 * very same mutex.
2512 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002513 if (wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002514 u32 tail = ctx->cached_cq_tail;
2515
Jens Axboe500f9fb2019-08-19 12:15:59 -06002516 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002517 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002518 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002519
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002520 /* some requests don't go through iopoll_list */
2521 if (tail != ctx->cached_cq_tail ||
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002522 wq_list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002523 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002524 }
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002525 ret = io_do_iopoll(ctx, !min);
2526 if (ret < 0)
2527 break;
2528 nr_events += ret;
2529 ret = 0;
2530 } while (nr_events < min && !need_resched());
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002531out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002532 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002533 return ret;
2534}
2535
Jens Axboe491381ce2019-10-17 09:20:46 -06002536static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002537{
Jens Axboe491381ce2019-10-17 09:20:46 -06002538 /*
2539 * Tell lockdep we inherited freeze protection from submission
2540 * thread.
2541 */
2542 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002543 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002544
Pavel Begunkov1c986792021-03-22 01:58:31 +00002545 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2546 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547 }
2548}
2549
Jens Axboeb63534c2020-06-04 11:28:00 -06002550#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002551static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002552{
Pavel Begunkovab454432021-03-22 01:58:33 +00002553 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002554
Pavel Begunkovd886e182021-10-04 20:02:56 +01002555 if (!req_has_async_data(req))
Pavel Begunkovab454432021-03-22 01:58:33 +00002556 return !io_req_prep_async(req);
Jens Axboecd658692021-09-10 11:19:14 -06002557 iov_iter_restore(&rw->iter, &rw->iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002558 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002559}
Jens Axboeb63534c2020-06-04 11:28:00 -06002560
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002561static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002562{
Jens Axboe355afae2020-09-02 09:30:31 -06002563 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002564 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002565
Jens Axboe355afae2020-09-02 09:30:31 -06002566 if (!S_ISBLK(mode) && !S_ISREG(mode))
2567 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002568 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2569 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002570 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002571 /*
2572 * If ref is dying, we might be running poll reap from the exit work.
2573 * Don't attempt to reissue from that path, just let it fail with
2574 * -EAGAIN.
2575 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002576 if (percpu_ref_is_dying(&ctx->refs))
2577 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002578 /*
2579 * Play it safe and assume not safe to re-import and reissue if we're
2580 * not in the original thread group (or in task context).
2581 */
2582 if (!same_thread_group(req->task, current) || !in_task())
2583 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002584 return true;
2585}
Jens Axboee82ad482021-04-02 19:45:34 -06002586#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002587static bool io_resubmit_prep(struct io_kiocb *req)
2588{
2589 return false;
2590}
Jens Axboee82ad482021-04-02 19:45:34 -06002591static bool io_rw_should_reissue(struct io_kiocb *req)
2592{
2593 return false;
2594}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002595#endif
2596
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002597static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002598{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002599 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2600 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002601 if (res != req->result) {
2602 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2603 io_rw_should_reissue(req)) {
2604 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002605 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002606 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002607 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002608 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002609 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002610 return false;
2611}
2612
Pavel Begunkovf237c302021-08-18 12:42:46 +01002613static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002614{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002615 unsigned int cflags = io_put_rw_kbuf(req);
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01002616 int res = req->result;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002617
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002618 if (*locked) {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002619 io_req_complete_state(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002620 io_req_add_compl_list(req);
2621 } else {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002622 io_req_complete_post(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002623 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002624}
2625
2626static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2627 unsigned int issue_flags)
2628{
2629 if (__io_complete_rw_common(req, res))
2630 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002631 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002632}
2633
2634static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2635{
Jens Axboe9adbd452019-12-20 08:45:55 -07002636 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002637
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002638 if (__io_complete_rw_common(req, res))
2639 return;
2640 req->result = res;
2641 req->io_task_work.func = io_req_task_complete;
2642 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643}
2644
Jens Axboedef596e2019-01-09 08:59:42 -07002645static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2646{
Jens Axboe9adbd452019-12-20 08:45:55 -07002647 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002648
Jens Axboe491381ce2019-10-17 09:20:46 -06002649 if (kiocb->ki_flags & IOCB_WRITE)
2650 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002651 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002652 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2653 req->flags |= REQ_F_REISSUE;
2654 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002655 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002656 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002657
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002658 req->result = res;
2659 /* order with io_iopoll_complete() checking ->iopoll_completed */
2660 smp_store_release(&req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002661}
2662
2663/*
2664 * After the iocb has been issued, it's safe to be found on the poll list.
2665 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002666 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002667 * accessing the kiocb cookie.
2668 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002669static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002670{
2671 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002672 const bool in_async = io_wq_current_is_worker();
2673
2674 /* workqueue context doesn't hold uring_lock, grab it now */
2675 if (unlikely(in_async))
2676 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002677
2678 /*
2679 * Track whether we have multiple files in our lists. This will impact
2680 * how we do polling eventually, not spinning if we're on potentially
2681 * different devices.
2682 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002683 if (wq_list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002684 ctx->poll_multi_queue = false;
2685 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002686 struct io_kiocb *list_req;
2687
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002688 list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
2689 comp_list);
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002690 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002691 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002692 }
2693
2694 /*
2695 * For fast devices, IO may have already completed. If it has, add
2696 * it to the front so we find it first.
2697 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002698 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002699 wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002700 else
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002701 wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002702
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002703 if (unlikely(in_async)) {
2704 /*
2705 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2706 * in sq thread task context or in io worker task context. If
2707 * current task context is sq thread, we don't need to check
2708 * whether should wake up sq thread.
2709 */
2710 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2711 wq_has_sleeper(&ctx->sq_data->wait))
2712 wake_up(&ctx->sq_data->wait);
2713
2714 mutex_unlock(&ctx->uring_lock);
2715 }
Jens Axboedef596e2019-01-09 08:59:42 -07002716}
2717
Jens Axboe4503b762020-06-01 10:00:27 -06002718static bool io_bdev_nowait(struct block_device *bdev)
2719{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002720 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002721}
2722
Jens Axboe2b188cc2019-01-07 10:46:33 -07002723/*
2724 * If we tracked the file through the SCM inflight mechanism, we could support
2725 * any file. For now, just ensure that anything potentially problematic is done
2726 * inline.
2727 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002728static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002729{
2730 umode_t mode = file_inode(file)->i_mode;
2731
Jens Axboe4503b762020-06-01 10:00:27 -06002732 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002733 if (IS_ENABLED(CONFIG_BLOCK) &&
2734 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002735 return true;
2736 return false;
2737 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002738 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002739 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002740 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002741 if (IS_ENABLED(CONFIG_BLOCK) &&
2742 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002743 file->f_op != &io_uring_fops)
2744 return true;
2745 return false;
2746 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002747
Jens Axboec5b85622020-06-09 19:23:05 -06002748 /* any ->read/write should understand O_NONBLOCK */
2749 if (file->f_flags & O_NONBLOCK)
2750 return true;
2751
Jens Axboeaf197f52020-04-28 13:15:06 -06002752 if (!(file->f_mode & FMODE_NOWAIT))
2753 return false;
2754
2755 if (rw == READ)
2756 return file->f_op->read_iter != NULL;
2757
2758 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002759}
2760
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002761static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002762{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002763 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002764 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002765 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002766 return true;
2767
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002768 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002769}
2770
Jens Axboe5d329e12021-09-14 11:08:37 -06002771static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2772 int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002773{
Jens Axboedef596e2019-01-09 08:59:42 -07002774 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002775 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002776 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002777 unsigned ioprio;
2778 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002779
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002780 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002781 req->flags |= REQ_F_ISREG;
2782
Jens Axboe2b188cc2019-01-07 10:46:33 -07002783 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002784 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002785 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002786 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002787 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002788 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002789 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2790 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2791 if (unlikely(ret))
2792 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002793
Jens Axboe5d329e12021-09-14 11:08:37 -06002794 /*
2795 * If the file is marked O_NONBLOCK, still allow retry for it if it
2796 * supports async. Otherwise it's impossible to use O_NONBLOCK files
2797 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2798 */
2799 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2800 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002801 req->flags |= REQ_F_NOWAIT;
2802
Jens Axboe2b188cc2019-01-07 10:46:33 -07002803 ioprio = READ_ONCE(sqe->ioprio);
2804 if (ioprio) {
2805 ret = ioprio_check_cap(ioprio);
2806 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002807 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002808
2809 kiocb->ki_ioprio = ioprio;
2810 } else
2811 kiocb->ki_ioprio = get_current_ioprio();
2812
Jens Axboedef596e2019-01-09 08:59:42 -07002813 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002814 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2815 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002816 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002817
Jens Axboe394918e2021-03-08 11:40:23 -07002818 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002819 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002820 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002821 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002822 if (kiocb->ki_flags & IOCB_HIPRI)
2823 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002824 kiocb->ki_complete = io_complete_rw;
2825 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002826
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002827 if (req->opcode == IORING_OP_READ_FIXED ||
2828 req->opcode == IORING_OP_WRITE_FIXED) {
2829 req->imu = NULL;
2830 io_req_set_rsrc_node(req);
2831 }
2832
Jens Axboe3529d8c2019-12-19 18:24:38 -07002833 req->rw.addr = READ_ONCE(sqe->addr);
2834 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002835 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002836 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002837}
2838
2839static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2840{
2841 switch (ret) {
2842 case -EIOCBQUEUED:
2843 break;
2844 case -ERESTARTSYS:
2845 case -ERESTARTNOINTR:
2846 case -ERESTARTNOHAND:
2847 case -ERESTART_RESTARTBLOCK:
2848 /*
2849 * We can't just restart the syscall, since previously
2850 * submitted sqes may already be in progress. Just fail this
2851 * IO with EINTR.
2852 */
2853 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002854 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002855 default:
2856 kiocb->ki_complete(kiocb, ret, 0);
2857 }
2858}
2859
Jens Axboea1d7c392020-06-22 11:09:46 -06002860static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002861 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002862{
Jens Axboeba042912019-12-25 16:33:42 -07002863 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002864 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002865
Jens Axboe227c0c92020-08-13 11:51:40 -06002866 /* add previously done IO, if any */
Pavel Begunkovd886e182021-10-04 20:02:56 +01002867 if (req_has_async_data(req) && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002868 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002869 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002870 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002871 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002872 }
2873
Jens Axboeba042912019-12-25 16:33:42 -07002874 if (req->flags & REQ_F_CUR_POS)
2875 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002876 if (ret >= 0 && (kiocb->ki_complete == io_complete_rw))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002877 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002878 else
2879 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002880
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002881 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002882 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002883 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002884 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002885 } else {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002886 unsigned int cflags = io_put_rw_kbuf(req);
2887 struct io_ring_ctx *ctx = req->ctx;
2888
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002889 req_set_fail(req);
Hao Xu14cfbb72021-10-14 22:04:00 +08002890 if (!(issue_flags & IO_URING_F_NONBLOCK)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002891 mutex_lock(&ctx->uring_lock);
2892 __io_req_complete(req, issue_flags, ret, cflags);
2893 mutex_unlock(&ctx->uring_lock);
2894 } else {
2895 __io_req_complete(req, issue_flags, ret, cflags);
2896 }
Pavel Begunkov97284632021-04-08 19:28:03 +01002897 }
2898 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002899}
2900
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002901static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2902 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002903{
Jens Axboe9adbd452019-12-20 08:45:55 -07002904 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002905 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002906 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002907
Pavel Begunkov75769e32021-04-01 15:43:54 +01002908 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002909 return -EFAULT;
2910 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002911 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002912 return -EFAULT;
2913
2914 /*
2915 * May not be a start of buffer, set size appropriately
2916 * and advance us to the beginning.
2917 */
2918 offset = buf_addr - imu->ubuf;
2919 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002920
2921 if (offset) {
2922 /*
2923 * Don't use iov_iter_advance() here, as it's really slow for
2924 * using the latter parts of a big fixed buffer - it iterates
2925 * over each segment manually. We can cheat a bit here, because
2926 * we know that:
2927 *
2928 * 1) it's a BVEC iter, we set it up
2929 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2930 * first and last bvec
2931 *
2932 * So just find our index, and adjust the iterator afterwards.
2933 * If the offset is within the first bvec (or the whole first
2934 * bvec, just use iov_iter_advance(). This makes it easier
2935 * since we can just skip the first segment, which may not
2936 * be PAGE_SIZE aligned.
2937 */
2938 const struct bio_vec *bvec = imu->bvec;
2939
2940 if (offset <= bvec->bv_len) {
2941 iov_iter_advance(iter, offset);
2942 } else {
2943 unsigned long seg_skip;
2944
2945 /* skip first vec */
2946 offset -= bvec->bv_len;
2947 seg_skip = 1 + (offset >> PAGE_SHIFT);
2948
2949 iter->bvec = bvec + seg_skip;
2950 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002951 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002952 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002953 }
2954 }
2955
Pavel Begunkov847595d2021-02-04 13:52:06 +00002956 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002957}
2958
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002959static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
2960{
2961 struct io_ring_ctx *ctx = req->ctx;
2962 struct io_mapped_ubuf *imu = req->imu;
2963 u16 index, buf_index = req->buf_index;
2964
2965 if (likely(!imu)) {
2966 if (unlikely(buf_index >= ctx->nr_user_bufs))
2967 return -EFAULT;
2968 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2969 imu = READ_ONCE(ctx->user_bufs[index]);
2970 req->imu = imu;
2971 }
2972 return __io_import_fixed(req, rw, iter, imu);
2973}
2974
Jens Axboebcda7ba2020-02-23 16:42:51 -07002975static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2976{
2977 if (needs_lock)
2978 mutex_unlock(&ctx->uring_lock);
2979}
2980
2981static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2982{
2983 /*
2984 * "Normal" inline submissions always hold the uring_lock, since we
2985 * grab it from the system call. Same is true for the SQPOLL offload.
2986 * The only exception is when we've detached the request and issue it
2987 * from an async worker thread, grab the lock for that case.
2988 */
2989 if (needs_lock)
2990 mutex_lock(&ctx->uring_lock);
2991}
2992
2993static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01002994 int bgid, bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07002995{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01002996 struct io_buffer *kbuf = req->kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002997 struct io_buffer *head;
2998
2999 if (req->flags & REQ_F_BUFFER_SELECTED)
3000 return kbuf;
3001
3002 io_ring_submit_lock(req->ctx, needs_lock);
3003
3004 lockdep_assert_held(&req->ctx->uring_lock);
3005
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003006 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003007 if (head) {
3008 if (!list_empty(&head->list)) {
3009 kbuf = list_last_entry(&head->list, struct io_buffer,
3010 list);
3011 list_del(&kbuf->list);
3012 } else {
3013 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003014 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003015 }
3016 if (*len > kbuf->len)
3017 *len = kbuf->len;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003018 req->flags |= REQ_F_BUFFER_SELECTED;
3019 req->kbuf = kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003020 } else {
3021 kbuf = ERR_PTR(-ENOBUFS);
3022 }
3023
3024 io_ring_submit_unlock(req->ctx, needs_lock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003025 return kbuf;
3026}
3027
Jens Axboe4d954c22020-02-27 07:31:19 -07003028static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3029 bool needs_lock)
3030{
3031 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003032 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003033
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003034 bgid = req->buf_index;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003035 kbuf = io_buffer_select(req, len, bgid, needs_lock);
Jens Axboe4d954c22020-02-27 07:31:19 -07003036 if (IS_ERR(kbuf))
3037 return kbuf;
Jens Axboe4d954c22020-02-27 07:31:19 -07003038 return u64_to_user_ptr(kbuf->addr);
3039}
3040
3041#ifdef CONFIG_COMPAT
3042static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3043 bool needs_lock)
3044{
3045 struct compat_iovec __user *uiov;
3046 compat_ssize_t clen;
3047 void __user *buf;
3048 ssize_t len;
3049
3050 uiov = u64_to_user_ptr(req->rw.addr);
3051 if (!access_ok(uiov, sizeof(*uiov)))
3052 return -EFAULT;
3053 if (__get_user(clen, &uiov->iov_len))
3054 return -EFAULT;
3055 if (clen < 0)
3056 return -EINVAL;
3057
3058 len = clen;
3059 buf = io_rw_buffer_select(req, &len, needs_lock);
3060 if (IS_ERR(buf))
3061 return PTR_ERR(buf);
3062 iov[0].iov_base = buf;
3063 iov[0].iov_len = (compat_size_t) len;
3064 return 0;
3065}
3066#endif
3067
3068static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3069 bool needs_lock)
3070{
3071 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3072 void __user *buf;
3073 ssize_t len;
3074
3075 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3076 return -EFAULT;
3077
3078 len = iov[0].iov_len;
3079 if (len < 0)
3080 return -EINVAL;
3081 buf = io_rw_buffer_select(req, &len, needs_lock);
3082 if (IS_ERR(buf))
3083 return PTR_ERR(buf);
3084 iov[0].iov_base = buf;
3085 iov[0].iov_len = len;
3086 return 0;
3087}
3088
3089static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3090 bool needs_lock)
3091{
Jens Axboedddb3e22020-06-04 11:27:01 -06003092 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003093 struct io_buffer *kbuf = req->kbuf;
Jens Axboedddb3e22020-06-04 11:27:01 -06003094
Jens Axboedddb3e22020-06-04 11:27:01 -06003095 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3096 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003097 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003098 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003099 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003100 return -EINVAL;
3101
3102#ifdef CONFIG_COMPAT
3103 if (req->ctx->compat)
3104 return io_compat_import(req, iov, needs_lock);
3105#endif
3106
3107 return __io_iov_buffer_select(req, iov, needs_lock);
3108}
3109
Pavel Begunkov847595d2021-02-04 13:52:06 +00003110static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3111 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003112{
Jens Axboe9adbd452019-12-20 08:45:55 -07003113 void __user *buf = u64_to_user_ptr(req->rw.addr);
3114 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003115 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003116 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003117
Pavel Begunkov7d009162019-11-25 23:14:40 +03003118 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003119 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003120 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003121 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003122
Jens Axboebcda7ba2020-02-23 16:42:51 -07003123 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003124 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003125 return -EINVAL;
3126
Jens Axboe3a6820f2019-12-22 15:19:35 -07003127 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003128 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003129 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003130 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003131 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003132 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003133 }
3134
Jens Axboe3a6820f2019-12-22 15:19:35 -07003135 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3136 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003137 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003138 }
3139
Jens Axboe4d954c22020-02-27 07:31:19 -07003140 if (req->flags & REQ_F_BUFFER_SELECT) {
3141 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003142 if (!ret)
3143 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003144 *iovec = NULL;
3145 return ret;
3146 }
3147
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003148 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3149 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003150}
3151
Jens Axboe0fef9482020-08-26 10:36:20 -06003152static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3153{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003154 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003155}
3156
Jens Axboe32960612019-09-23 11:05:34 -06003157/*
3158 * For files that don't have ->read_iter() and ->write_iter(), handle them
3159 * by looping over ->read() or ->write() manually.
3160 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003161static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003162{
Jens Axboe4017eb92020-10-22 14:14:12 -06003163 struct kiocb *kiocb = &req->rw.kiocb;
3164 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003165 ssize_t ret = 0;
3166
3167 /*
3168 * Don't support polled IO through this interface, and we can't
3169 * support non-blocking either. For the latter, this just causes
3170 * the kiocb to be handled from an async context.
3171 */
3172 if (kiocb->ki_flags & IOCB_HIPRI)
3173 return -EOPNOTSUPP;
3174 if (kiocb->ki_flags & IOCB_NOWAIT)
3175 return -EAGAIN;
3176
3177 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003178 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003179 ssize_t nr;
3180
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003181 if (!iov_iter_is_bvec(iter)) {
3182 iovec = iov_iter_iovec(iter);
3183 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003184 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3185 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003186 }
3187
Jens Axboe32960612019-09-23 11:05:34 -06003188 if (rw == READ) {
3189 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003190 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003191 } else {
3192 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003193 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003194 }
3195
3196 if (nr < 0) {
3197 if (!ret)
3198 ret = nr;
3199 break;
3200 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003201 if (!iov_iter_is_bvec(iter)) {
3202 iov_iter_advance(iter, nr);
3203 } else {
3204 req->rw.len -= nr;
3205 req->rw.addr += nr;
3206 }
Jens Axboe32960612019-09-23 11:05:34 -06003207 ret += nr;
3208 if (nr != iovec.iov_len)
3209 break;
Jens Axboe32960612019-09-23 11:05:34 -06003210 }
3211
3212 return ret;
3213}
3214
Jens Axboeff6165b2020-08-13 09:47:43 -06003215static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3216 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003217{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003218 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003219
Jens Axboeff6165b2020-08-13 09:47:43 -06003220 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003221 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003222 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003223 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003224 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003225 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003226 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003227 unsigned iov_off = 0;
3228
3229 rw->iter.iov = rw->fast_iov;
3230 if (iter->iov != fast_iov) {
3231 iov_off = iter->iov - fast_iov;
3232 rw->iter.iov += iov_off;
3233 }
3234 if (rw->fast_iov != fast_iov)
3235 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003236 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003237 } else {
3238 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003239 }
3240}
3241
Hao Xu8d4af682021-09-22 18:15:22 +08003242static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003243{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003244 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3245 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
Pavel Begunkovd886e182021-10-04 20:02:56 +01003246 if (req->async_data) {
3247 req->flags |= REQ_F_ASYNC_DATA;
3248 return false;
3249 }
3250 return true;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003251}
3252
Jens Axboeff6165b2020-08-13 09:47:43 -06003253static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3254 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003255 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003256{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003257 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003258 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003259 if (!req_has_async_data(req)) {
Jens Axboecd658692021-09-10 11:19:14 -06003260 struct io_async_rw *iorw;
3261
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003262 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003263 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003264 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003265 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003266
Jens Axboeff6165b2020-08-13 09:47:43 -06003267 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboecd658692021-09-10 11:19:14 -06003268 iorw = req->async_data;
3269 /* we've copied and mapped the iter, ensure state is saved */
3270 iov_iter_save_state(&iorw->iter, &iorw->iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003271 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003272 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003273}
3274
Pavel Begunkov73debe62020-09-30 22:57:54 +03003275static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003276{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003277 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003278 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003279 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003280
Pavel Begunkov2846c482020-11-07 13:16:27 +00003281 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003282 if (unlikely(ret < 0))
3283 return ret;
3284
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003285 iorw->bytes_done = 0;
3286 iorw->free_iovec = iov;
3287 if (iov)
3288 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecd658692021-09-10 11:19:14 -06003289 iov_iter_save_state(&iorw->iter, &iorw->iter_state);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003290 return 0;
3291}
3292
Pavel Begunkov73debe62020-09-30 22:57:54 +03003293static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003294{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003295 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3296 return -EBADF;
Jens Axboe5d329e12021-09-14 11:08:37 -06003297 return io_prep_rw(req, sqe, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003298}
3299
Jens Axboec1dd91d2020-08-03 16:43:59 -06003300/*
3301 * This is our waitqueue callback handler, registered through lock_page_async()
3302 * when we initially tried to do the IO with the iocb armed our waitqueue.
3303 * This gets called when the page is unlocked, and we generally expect that to
3304 * happen when the page IO is completed and the page is now uptodate. This will
3305 * queue a task_work based retry of the operation, attempting to copy the data
3306 * again. If the latter fails because the page was NOT uptodate, then we will
3307 * do a thread based blocking retry of the operation. That's the unexpected
3308 * slow path.
3309 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003310static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3311 int sync, void *arg)
3312{
3313 struct wait_page_queue *wpq;
3314 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003315 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003316
3317 wpq = container_of(wait, struct wait_page_queue, wait);
3318
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003319 if (!wake_page_match(wpq, key))
3320 return 0;
3321
Hao Xuc8d317a2020-09-29 20:00:45 +08003322 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003323 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003324 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003325 return 1;
3326}
3327
Jens Axboec1dd91d2020-08-03 16:43:59 -06003328/*
3329 * This controls whether a given IO request should be armed for async page
3330 * based retry. If we return false here, the request is handed to the async
3331 * worker threads for retry. If we're doing buffered reads on a regular file,
3332 * we prepare a private wait_page_queue entry and retry the operation. This
3333 * will either succeed because the page is now uptodate and unlocked, or it
3334 * will register a callback when the page is unlocked at IO completion. Through
3335 * that callback, io_uring uses task_work to setup a retry of the operation.
3336 * That retry will attempt the buffered read again. The retry will generally
3337 * succeed, or in rare cases where it fails, we then fall back to using the
3338 * async worker threads for a blocking retry.
3339 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003340static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003341{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003342 struct io_async_rw *rw = req->async_data;
3343 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003344 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003345
3346 /* never retry for NOWAIT, we just complete with -EAGAIN */
3347 if (req->flags & REQ_F_NOWAIT)
3348 return false;
3349
Jens Axboe227c0c92020-08-13 11:51:40 -06003350 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003351 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003352 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003353
Jens Axboebcf5a062020-05-22 09:24:42 -06003354 /*
3355 * just use poll if we can, and don't attempt if the fs doesn't
3356 * support callback based unlocks
3357 */
3358 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3359 return false;
3360
Jens Axboe3b2a4432020-08-16 10:58:43 -07003361 wait->wait.func = io_async_buf_func;
3362 wait->wait.private = req;
3363 wait->wait.flags = 0;
3364 INIT_LIST_HEAD(&wait->wait.entry);
3365 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003366 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003367 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003368 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003369}
3370
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003371static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003372{
3373 if (req->file->f_op->read_iter)
3374 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003375 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003376 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003377 else
3378 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003379}
3380
Ming Lei7db30432021-08-21 23:07:51 +08003381static bool need_read_all(struct io_kiocb *req)
3382{
3383 return req->flags & REQ_F_ISREG ||
3384 S_ISBLK(file_inode(req->file)->i_mode);
3385}
3386
Pavel Begunkov889fca72021-02-10 00:03:09 +00003387static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003388{
3389 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003390 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003391 struct iov_iter __iter, *iter = &__iter;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003392 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003393 struct iov_iter_state __state, *state;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003394 struct io_async_rw *rw;
Jens Axboecd658692021-09-10 11:19:14 -06003395 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003396
Pavel Begunkovd886e182021-10-04 20:02:56 +01003397 if (req_has_async_data(req)) {
3398 rw = req->async_data;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003399 iter = &rw->iter;
Jens Axboecd658692021-09-10 11:19:14 -06003400 state = &rw->iter_state;
3401 /*
3402 * We come here from an earlier attempt, restore our state to
3403 * match in case it doesn't. It's cheap enough that we don't
3404 * need to make this conditional.
3405 */
3406 iov_iter_restore(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003407 iovec = NULL;
3408 } else {
3409 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3410 if (ret < 0)
3411 return ret;
Jens Axboecd658692021-09-10 11:19:14 -06003412 state = &__state;
3413 iov_iter_save_state(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003414 }
Jens Axboecd658692021-09-10 11:19:14 -06003415 req->result = iov_iter_count(iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003416
Jens Axboefd6c2e42019-12-18 12:19:41 -07003417 /* Ensure we clear previously set non-block flag */
3418 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003419 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003420 else
3421 kiocb->ki_flags |= IOCB_NOWAIT;
3422
Pavel Begunkov24c74672020-06-21 13:09:51 +03003423 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003424 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003425 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003426 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003427 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003428
Jens Axboecd658692021-09-10 11:19:14 -06003429 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003430 if (unlikely(ret)) {
3431 kfree(iovec);
3432 return ret;
3433 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003434
Jens Axboe227c0c92020-08-13 11:51:40 -06003435 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003436
Jens Axboe230d50d2021-04-01 20:41:15 -06003437 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003438 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003439 /* IOPOLL retry should happen for io-wq threads */
3440 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003441 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003442 /* no retry on NONBLOCK nor RWF_NOWAIT */
3443 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003444 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003445 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003446 } else if (ret == -EIOCBQUEUED) {
3447 goto out_free;
Jens Axboecd658692021-09-10 11:19:14 -06003448 } else if (ret <= 0 || ret == req->result || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003449 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003450 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003451 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003452 }
3453
Jens Axboecd658692021-09-10 11:19:14 -06003454 /*
3455 * Don't depend on the iter state matching what was consumed, or being
3456 * untouched in case of error. Restore it and we'll advance it
3457 * manually if we need to.
3458 */
3459 iov_iter_restore(iter, state);
3460
Jens Axboe227c0c92020-08-13 11:51:40 -06003461 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003462 if (ret2)
3463 return ret2;
3464
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003465 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003466 rw = req->async_data;
Jens Axboecd658692021-09-10 11:19:14 -06003467 /*
3468 * Now use our persistent iterator and state, if we aren't already.
3469 * We've restored and mapped the iter to match.
3470 */
3471 if (iter != &rw->iter) {
3472 iter = &rw->iter;
3473 state = &rw->iter_state;
3474 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003475
Pavel Begunkovb23df912021-02-04 13:52:04 +00003476 do {
Jens Axboecd658692021-09-10 11:19:14 -06003477 /*
3478 * We end up here because of a partial read, either from
3479 * above or inside this loop. Advance the iter by the bytes
3480 * that were consumed.
3481 */
3482 iov_iter_advance(iter, ret);
3483 if (!iov_iter_count(iter))
3484 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003485 rw->bytes_done += ret;
Jens Axboecd658692021-09-10 11:19:14 -06003486 iov_iter_save_state(iter, state);
3487
Pavel Begunkovb23df912021-02-04 13:52:04 +00003488 /* if we can retry, do so with the callbacks armed */
3489 if (!io_rw_should_retry(req)) {
3490 kiocb->ki_flags &= ~IOCB_WAITQ;
3491 return -EAGAIN;
3492 }
3493
3494 /*
3495 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3496 * we get -EIOCBQUEUED, then we'll get a notification when the
3497 * desired page gets unlocked. We can also get a partial read
3498 * here, and if we do, then just retry at the new offset.
3499 */
3500 ret = io_iter_do_read(req, iter);
3501 if (ret == -EIOCBQUEUED)
3502 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003503 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003504 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboecd658692021-09-10 11:19:14 -06003505 iov_iter_restore(iter, state);
3506 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003507done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003508 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003509out_free:
3510 /* it's faster to check here then delegate to kfree */
3511 if (iovec)
3512 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003513 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003514}
3515
Pavel Begunkov73debe62020-09-30 22:57:54 +03003516static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003517{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003518 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3519 return -EBADF;
Jens Axboe5d329e12021-09-14 11:08:37 -06003520 return io_prep_rw(req, sqe, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003521}
3522
Pavel Begunkov889fca72021-02-10 00:03:09 +00003523static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003524{
3525 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003526 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003527 struct iov_iter __iter, *iter = &__iter;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003528 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003529 struct iov_iter_state __state, *state;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003530 struct io_async_rw *rw;
Jens Axboecd658692021-09-10 11:19:14 -06003531 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003532
Pavel Begunkovd886e182021-10-04 20:02:56 +01003533 if (req_has_async_data(req)) {
3534 rw = req->async_data;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003535 iter = &rw->iter;
Jens Axboecd658692021-09-10 11:19:14 -06003536 state = &rw->iter_state;
3537 iov_iter_restore(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003538 iovec = NULL;
3539 } else {
3540 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3541 if (ret < 0)
3542 return ret;
Jens Axboecd658692021-09-10 11:19:14 -06003543 state = &__state;
3544 iov_iter_save_state(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003545 }
Jens Axboecd658692021-09-10 11:19:14 -06003546 req->result = iov_iter_count(iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003547
Jens Axboefd6c2e42019-12-18 12:19:41 -07003548 /* Ensure we clear previously set non-block flag */
3549 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003550 kiocb->ki_flags &= ~IOCB_NOWAIT;
3551 else
3552 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003553
Pavel Begunkov24c74672020-06-21 13:09:51 +03003554 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003555 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003556 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003557
Jens Axboe10d59342019-12-09 20:16:22 -07003558 /* file path doesn't support NOWAIT for non-direct_IO */
3559 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3560 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003561 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003562
Jens Axboecd658692021-09-10 11:19:14 -06003563 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003564 if (unlikely(ret))
3565 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003566
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003567 /*
3568 * Open-code file_start_write here to grab freeze protection,
3569 * which will be released by another thread in
3570 * io_complete_rw(). Fool lockdep by telling it the lock got
3571 * released so that it doesn't complain about the held lock when
3572 * we return to userspace.
3573 */
3574 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003575 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003576 __sb_writers_release(file_inode(req->file)->i_sb,
3577 SB_FREEZE_WRITE);
3578 }
3579 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003580
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003581 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003582 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003583 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003584 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003585 else
3586 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003587
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003588 if (req->flags & REQ_F_REISSUE) {
3589 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003590 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003591 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003592
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003593 /*
3594 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3595 * retry them without IOCB_NOWAIT.
3596 */
3597 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3598 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003599 /* no retry on NONBLOCK nor RWF_NOWAIT */
3600 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003601 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003602 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003603 /* IOPOLL retry should happen for io-wq threads */
3604 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3605 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003606done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003607 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003608 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003609copy_iov:
Jens Axboecd658692021-09-10 11:19:14 -06003610 iov_iter_restore(iter, state);
Jens Axboe227c0c92020-08-13 11:51:40 -06003611 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003612 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003613 }
Jens Axboe31b51512019-01-18 22:56:34 -07003614out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003615 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003616 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003617 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003618 return ret;
3619}
3620
Jens Axboe80a261f2020-09-28 14:23:58 -06003621static int io_renameat_prep(struct io_kiocb *req,
3622 const struct io_uring_sqe *sqe)
3623{
3624 struct io_rename *ren = &req->rename;
3625 const char __user *oldf, *newf;
3626
Jens Axboeed7eb252021-06-23 09:04:13 -06003627 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3628 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003629 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003630 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003631 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3632 return -EBADF;
3633
3634 ren->old_dfd = READ_ONCE(sqe->fd);
3635 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3636 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3637 ren->new_dfd = READ_ONCE(sqe->len);
3638 ren->flags = READ_ONCE(sqe->rename_flags);
3639
3640 ren->oldpath = getname(oldf);
3641 if (IS_ERR(ren->oldpath))
3642 return PTR_ERR(ren->oldpath);
3643
3644 ren->newpath = getname(newf);
3645 if (IS_ERR(ren->newpath)) {
3646 putname(ren->oldpath);
3647 return PTR_ERR(ren->newpath);
3648 }
3649
3650 req->flags |= REQ_F_NEED_CLEANUP;
3651 return 0;
3652}
3653
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003654static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003655{
3656 struct io_rename *ren = &req->rename;
3657 int ret;
3658
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003659 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003660 return -EAGAIN;
3661
3662 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3663 ren->newpath, ren->flags);
3664
3665 req->flags &= ~REQ_F_NEED_CLEANUP;
3666 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003667 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003668 io_req_complete(req, ret);
3669 return 0;
3670}
3671
Jens Axboe14a11432020-09-28 14:27:37 -06003672static int io_unlinkat_prep(struct io_kiocb *req,
3673 const struct io_uring_sqe *sqe)
3674{
3675 struct io_unlink *un = &req->unlink;
3676 const char __user *fname;
3677
Jens Axboe22634bc2021-06-23 09:07:45 -06003678 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3679 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003680 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3681 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003682 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003683 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3684 return -EBADF;
3685
3686 un->dfd = READ_ONCE(sqe->fd);
3687
3688 un->flags = READ_ONCE(sqe->unlink_flags);
3689 if (un->flags & ~AT_REMOVEDIR)
3690 return -EINVAL;
3691
3692 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3693 un->filename = getname(fname);
3694 if (IS_ERR(un->filename))
3695 return PTR_ERR(un->filename);
3696
3697 req->flags |= REQ_F_NEED_CLEANUP;
3698 return 0;
3699}
3700
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003701static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003702{
3703 struct io_unlink *un = &req->unlink;
3704 int ret;
3705
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003706 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003707 return -EAGAIN;
3708
3709 if (un->flags & AT_REMOVEDIR)
3710 ret = do_rmdir(un->dfd, un->filename);
3711 else
3712 ret = do_unlinkat(un->dfd, un->filename);
3713
3714 req->flags &= ~REQ_F_NEED_CLEANUP;
3715 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003716 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003717 io_req_complete(req, ret);
3718 return 0;
3719}
3720
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003721static int io_mkdirat_prep(struct io_kiocb *req,
3722 const struct io_uring_sqe *sqe)
3723{
3724 struct io_mkdir *mkd = &req->mkdir;
3725 const char __user *fname;
3726
3727 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3728 return -EINVAL;
3729 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3730 sqe->splice_fd_in)
3731 return -EINVAL;
3732 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3733 return -EBADF;
3734
3735 mkd->dfd = READ_ONCE(sqe->fd);
3736 mkd->mode = READ_ONCE(sqe->len);
3737
3738 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3739 mkd->filename = getname(fname);
3740 if (IS_ERR(mkd->filename))
3741 return PTR_ERR(mkd->filename);
3742
3743 req->flags |= REQ_F_NEED_CLEANUP;
3744 return 0;
3745}
3746
3747static int io_mkdirat(struct io_kiocb *req, int issue_flags)
3748{
3749 struct io_mkdir *mkd = &req->mkdir;
3750 int ret;
3751
3752 if (issue_flags & IO_URING_F_NONBLOCK)
3753 return -EAGAIN;
3754
3755 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3756
3757 req->flags &= ~REQ_F_NEED_CLEANUP;
3758 if (ret < 0)
3759 req_set_fail(req);
3760 io_req_complete(req, ret);
3761 return 0;
3762}
3763
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003764static int io_symlinkat_prep(struct io_kiocb *req,
3765 const struct io_uring_sqe *sqe)
3766{
3767 struct io_symlink *sl = &req->symlink;
3768 const char __user *oldpath, *newpath;
3769
3770 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3771 return -EINVAL;
3772 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3773 sqe->splice_fd_in)
3774 return -EINVAL;
3775 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3776 return -EBADF;
3777
3778 sl->new_dfd = READ_ONCE(sqe->fd);
3779 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3780 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3781
3782 sl->oldpath = getname(oldpath);
3783 if (IS_ERR(sl->oldpath))
3784 return PTR_ERR(sl->oldpath);
3785
3786 sl->newpath = getname(newpath);
3787 if (IS_ERR(sl->newpath)) {
3788 putname(sl->oldpath);
3789 return PTR_ERR(sl->newpath);
3790 }
3791
3792 req->flags |= REQ_F_NEED_CLEANUP;
3793 return 0;
3794}
3795
3796static int io_symlinkat(struct io_kiocb *req, int issue_flags)
3797{
3798 struct io_symlink *sl = &req->symlink;
3799 int ret;
3800
3801 if (issue_flags & IO_URING_F_NONBLOCK)
3802 return -EAGAIN;
3803
3804 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3805
3806 req->flags &= ~REQ_F_NEED_CLEANUP;
3807 if (ret < 0)
3808 req_set_fail(req);
3809 io_req_complete(req, ret);
3810 return 0;
3811}
3812
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003813static int io_linkat_prep(struct io_kiocb *req,
3814 const struct io_uring_sqe *sqe)
3815{
3816 struct io_hardlink *lnk = &req->hardlink;
3817 const char __user *oldf, *newf;
3818
3819 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3820 return -EINVAL;
3821 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3822 return -EINVAL;
3823 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3824 return -EBADF;
3825
3826 lnk->old_dfd = READ_ONCE(sqe->fd);
3827 lnk->new_dfd = READ_ONCE(sqe->len);
3828 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3829 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3830 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3831
3832 lnk->oldpath = getname(oldf);
3833 if (IS_ERR(lnk->oldpath))
3834 return PTR_ERR(lnk->oldpath);
3835
3836 lnk->newpath = getname(newf);
3837 if (IS_ERR(lnk->newpath)) {
3838 putname(lnk->oldpath);
3839 return PTR_ERR(lnk->newpath);
3840 }
3841
3842 req->flags |= REQ_F_NEED_CLEANUP;
3843 return 0;
3844}
3845
3846static int io_linkat(struct io_kiocb *req, int issue_flags)
3847{
3848 struct io_hardlink *lnk = &req->hardlink;
3849 int ret;
3850
3851 if (issue_flags & IO_URING_F_NONBLOCK)
3852 return -EAGAIN;
3853
3854 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3855 lnk->newpath, lnk->flags);
3856
3857 req->flags &= ~REQ_F_NEED_CLEANUP;
3858 if (ret < 0)
3859 req_set_fail(req);
3860 io_req_complete(req, ret);
3861 return 0;
3862}
3863
Jens Axboe36f4fa62020-09-05 11:14:22 -06003864static int io_shutdown_prep(struct io_kiocb *req,
3865 const struct io_uring_sqe *sqe)
3866{
3867#if defined(CONFIG_NET)
3868 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3869 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003870 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3871 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003872 return -EINVAL;
3873
3874 req->shutdown.how = READ_ONCE(sqe->len);
3875 return 0;
3876#else
3877 return -EOPNOTSUPP;
3878#endif
3879}
3880
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003881static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003882{
3883#if defined(CONFIG_NET)
3884 struct socket *sock;
3885 int ret;
3886
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003887 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003888 return -EAGAIN;
3889
Linus Torvalds48aba792020-12-16 12:44:05 -08003890 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003891 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003892 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003893
3894 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003895 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003896 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003897 io_req_complete(req, ret);
3898 return 0;
3899#else
3900 return -EOPNOTSUPP;
3901#endif
3902}
3903
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003904static int __io_splice_prep(struct io_kiocb *req,
3905 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003906{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003907 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003908 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003909
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003910 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3911 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003912
3913 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003914 sp->len = READ_ONCE(sqe->len);
3915 sp->flags = READ_ONCE(sqe->splice_flags);
3916
3917 if (unlikely(sp->flags & ~valid_flags))
3918 return -EINVAL;
3919
Pavel Begunkov62906e82021-08-10 14:52:47 +01003920 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003921 (sp->flags & SPLICE_F_FD_IN_FIXED));
3922 if (!sp->file_in)
3923 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003924 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003925 return 0;
3926}
3927
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003928static int io_tee_prep(struct io_kiocb *req,
3929 const struct io_uring_sqe *sqe)
3930{
3931 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3932 return -EINVAL;
3933 return __io_splice_prep(req, sqe);
3934}
3935
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003936static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003937{
3938 struct io_splice *sp = &req->splice;
3939 struct file *in = sp->file_in;
3940 struct file *out = sp->file_out;
3941 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3942 long ret = 0;
3943
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003944 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003945 return -EAGAIN;
3946 if (sp->len)
3947 ret = do_tee(in, out, sp->len, flags);
3948
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003949 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3950 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003951 req->flags &= ~REQ_F_NEED_CLEANUP;
3952
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003953 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003954 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003955 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003956 return 0;
3957}
3958
3959static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3960{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003961 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003962
3963 sp->off_in = READ_ONCE(sqe->splice_off_in);
3964 sp->off_out = READ_ONCE(sqe->off);
3965 return __io_splice_prep(req, sqe);
3966}
3967
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003968static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003969{
3970 struct io_splice *sp = &req->splice;
3971 struct file *in = sp->file_in;
3972 struct file *out = sp->file_out;
3973 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3974 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003975 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003976
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003977 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003978 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003979
3980 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3981 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003982
Jens Axboe948a7742020-05-17 14:21:38 -06003983 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003984 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003985
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003986 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3987 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003988 req->flags &= ~REQ_F_NEED_CLEANUP;
3989
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003990 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003991 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003992 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003993 return 0;
3994}
3995
Jens Axboe2b188cc2019-01-07 10:46:33 -07003996/*
3997 * IORING_OP_NOP just posts a completion event, nothing else.
3998 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003999static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004000{
4001 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004002
Jens Axboedef596e2019-01-09 08:59:42 -07004003 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4004 return -EINVAL;
4005
Pavel Begunkov889fca72021-02-10 00:03:09 +00004006 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004007 return 0;
4008}
4009
Pavel Begunkov1155c762021-02-18 18:29:38 +00004010static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004011{
Jens Axboe6b063142019-01-10 22:13:58 -07004012 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004013
Jens Axboe09bb8392019-03-13 12:39:28 -06004014 if (!req->file)
4015 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004016
Jens Axboe6b063142019-01-10 22:13:58 -07004017 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004018 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004019 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4020 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004021 return -EINVAL;
4022
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004023 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4024 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4025 return -EINVAL;
4026
4027 req->sync.off = READ_ONCE(sqe->off);
4028 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004029 return 0;
4030}
4031
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004032static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004033{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004034 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004035 int ret;
4036
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004037 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004038 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004039 return -EAGAIN;
4040
Jens Axboe9adbd452019-12-20 08:45:55 -07004041 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004042 end > 0 ? end : LLONG_MAX,
4043 req->sync.flags & IORING_FSYNC_DATASYNC);
4044 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004045 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004046 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004047 return 0;
4048}
4049
Jens Axboed63d1b52019-12-10 10:38:56 -07004050static int io_fallocate_prep(struct io_kiocb *req,
4051 const struct io_uring_sqe *sqe)
4052{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004053 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4054 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004055 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004056 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4057 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004058
4059 req->sync.off = READ_ONCE(sqe->off);
4060 req->sync.len = READ_ONCE(sqe->addr);
4061 req->sync.mode = READ_ONCE(sqe->len);
4062 return 0;
4063}
4064
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004065static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004066{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004067 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004068
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004069 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004070 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004071 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004072 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4073 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004074 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004075 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004076 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004077 return 0;
4078}
4079
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004080static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004081{
Jens Axboef8748882020-01-08 17:47:02 -07004082 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004083 int ret;
4084
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004085 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4086 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004087 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004088 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004089 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004090 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004091
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004092 /* open.how should be already initialised */
4093 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004094 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004095
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004096 req->open.dfd = READ_ONCE(sqe->fd);
4097 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004098 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004099 if (IS_ERR(req->open.filename)) {
4100 ret = PTR_ERR(req->open.filename);
4101 req->open.filename = NULL;
4102 return ret;
4103 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004104
4105 req->open.file_slot = READ_ONCE(sqe->file_index);
4106 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4107 return -EINVAL;
4108
Jens Axboe4022e7a2020-03-19 19:23:18 -06004109 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004110 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004111 return 0;
4112}
4113
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004114static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4115{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004116 u64 mode = READ_ONCE(sqe->len);
4117 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004118
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004119 req->open.how = build_open_how(flags, mode);
4120 return __io_openat_prep(req, sqe);
4121}
4122
Jens Axboecebdb982020-01-08 17:59:24 -07004123static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4124{
4125 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004126 size_t len;
4127 int ret;
4128
Jens Axboecebdb982020-01-08 17:59:24 -07004129 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4130 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004131 if (len < OPEN_HOW_SIZE_VER0)
4132 return -EINVAL;
4133
4134 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4135 len);
4136 if (ret)
4137 return ret;
4138
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004139 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004140}
4141
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004142static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004143{
4144 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004145 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004146 bool resolve_nonblock, nonblock_set;
4147 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004148 int ret;
4149
Jens Axboecebdb982020-01-08 17:59:24 -07004150 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004151 if (ret)
4152 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004153 nonblock_set = op.open_flag & O_NONBLOCK;
4154 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004155 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004156 /*
4157 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4158 * it'll always -EAGAIN
4159 */
4160 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4161 return -EAGAIN;
4162 op.lookup_flags |= LOOKUP_CACHED;
4163 op.open_flag |= O_NONBLOCK;
4164 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004165
Pavel Begunkovb9445592021-08-25 12:25:45 +01004166 if (!fixed) {
4167 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4168 if (ret < 0)
4169 goto err;
4170 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004171
4172 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004173 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004174 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004175 * We could hang on to this 'fd' on retrying, but seems like
4176 * marginal gain for something that is now known to be a slower
4177 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004178 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004179 if (!fixed)
4180 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004181
4182 ret = PTR_ERR(file);
4183 /* only retry if RESOLVE_CACHED wasn't already set by application */
4184 if (ret == -EAGAIN &&
4185 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4186 return -EAGAIN;
4187 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004188 }
4189
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004190 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4191 file->f_flags &= ~O_NONBLOCK;
4192 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004193
4194 if (!fixed)
4195 fd_install(ret, file);
4196 else
4197 ret = io_install_fixed_file(req, file, issue_flags,
4198 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004199err:
4200 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004201 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004202 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004203 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004204 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004205 return 0;
4206}
4207
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004208static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004209{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004210 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004211}
4212
Jens Axboe067524e2020-03-02 16:32:28 -07004213static int io_remove_buffers_prep(struct io_kiocb *req,
4214 const struct io_uring_sqe *sqe)
4215{
4216 struct io_provide_buf *p = &req->pbuf;
4217 u64 tmp;
4218
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004219 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4220 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004221 return -EINVAL;
4222
4223 tmp = READ_ONCE(sqe->fd);
4224 if (!tmp || tmp > USHRT_MAX)
4225 return -EINVAL;
4226
4227 memset(p, 0, sizeof(*p));
4228 p->nbufs = tmp;
4229 p->bgid = READ_ONCE(sqe->buf_group);
4230 return 0;
4231}
4232
4233static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4234 int bgid, unsigned nbufs)
4235{
4236 unsigned i = 0;
4237
4238 /* shouldn't happen */
4239 if (!nbufs)
4240 return 0;
4241
4242 /* the head kbuf is the list itself */
4243 while (!list_empty(&buf->list)) {
4244 struct io_buffer *nxt;
4245
4246 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4247 list_del(&nxt->list);
4248 kfree(nxt);
4249 if (++i == nbufs)
4250 return i;
4251 }
4252 i++;
4253 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004254 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004255
4256 return i;
4257}
4258
Pavel Begunkov889fca72021-02-10 00:03:09 +00004259static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004260{
4261 struct io_provide_buf *p = &req->pbuf;
4262 struct io_ring_ctx *ctx = req->ctx;
4263 struct io_buffer *head;
4264 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004265 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004266
4267 io_ring_submit_lock(ctx, !force_nonblock);
4268
4269 lockdep_assert_held(&ctx->uring_lock);
4270
4271 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004272 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004273 if (head)
4274 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004275 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004276 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004277
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004278 /* complete before unlock, IOPOLL may need the lock */
4279 __io_req_complete(req, issue_flags, ret, 0);
4280 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004281 return 0;
4282}
4283
Jens Axboeddf0322d2020-02-23 16:41:33 -07004284static int io_provide_buffers_prep(struct io_kiocb *req,
4285 const struct io_uring_sqe *sqe)
4286{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004287 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004288 struct io_provide_buf *p = &req->pbuf;
4289 u64 tmp;
4290
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004291 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004292 return -EINVAL;
4293
4294 tmp = READ_ONCE(sqe->fd);
4295 if (!tmp || tmp > USHRT_MAX)
4296 return -E2BIG;
4297 p->nbufs = tmp;
4298 p->addr = READ_ONCE(sqe->addr);
4299 p->len = READ_ONCE(sqe->len);
4300
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004301 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4302 &size))
4303 return -EOVERFLOW;
4304 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4305 return -EOVERFLOW;
4306
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004307 size = (unsigned long)p->len * p->nbufs;
4308 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004309 return -EFAULT;
4310
4311 p->bgid = READ_ONCE(sqe->buf_group);
4312 tmp = READ_ONCE(sqe->off);
4313 if (tmp > USHRT_MAX)
4314 return -E2BIG;
4315 p->bid = tmp;
4316 return 0;
4317}
4318
4319static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4320{
4321 struct io_buffer *buf;
4322 u64 addr = pbuf->addr;
4323 int i, bid = pbuf->bid;
4324
4325 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004326 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004327 if (!buf)
4328 break;
4329
4330 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004331 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004332 buf->bid = bid;
4333 addr += pbuf->len;
4334 bid++;
4335 if (!*head) {
4336 INIT_LIST_HEAD(&buf->list);
4337 *head = buf;
4338 } else {
4339 list_add_tail(&buf->list, &(*head)->list);
4340 }
4341 }
4342
4343 return i ? i : -ENOMEM;
4344}
4345
Pavel Begunkov889fca72021-02-10 00:03:09 +00004346static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004347{
4348 struct io_provide_buf *p = &req->pbuf;
4349 struct io_ring_ctx *ctx = req->ctx;
4350 struct io_buffer *head, *list;
4351 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004352 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004353
4354 io_ring_submit_lock(ctx, !force_nonblock);
4355
4356 lockdep_assert_held(&ctx->uring_lock);
4357
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004358 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004359
4360 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004361 if (ret >= 0 && !list) {
4362 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4363 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004364 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004365 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004366 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004367 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004368 /* complete before unlock, IOPOLL may need the lock */
4369 __io_req_complete(req, issue_flags, ret, 0);
4370 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004371 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004372}
4373
Jens Axboe3e4827b2020-01-08 15:18:09 -07004374static int io_epoll_ctl_prep(struct io_kiocb *req,
4375 const struct io_uring_sqe *sqe)
4376{
4377#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004378 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004379 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004380 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004381 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004382
4383 req->epoll.epfd = READ_ONCE(sqe->fd);
4384 req->epoll.op = READ_ONCE(sqe->len);
4385 req->epoll.fd = READ_ONCE(sqe->off);
4386
4387 if (ep_op_has_event(req->epoll.op)) {
4388 struct epoll_event __user *ev;
4389
4390 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4391 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4392 return -EFAULT;
4393 }
4394
4395 return 0;
4396#else
4397 return -EOPNOTSUPP;
4398#endif
4399}
4400
Pavel Begunkov889fca72021-02-10 00:03:09 +00004401static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004402{
4403#if defined(CONFIG_EPOLL)
4404 struct io_epoll *ie = &req->epoll;
4405 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004406 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004407
4408 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4409 if (force_nonblock && ret == -EAGAIN)
4410 return -EAGAIN;
4411
4412 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004413 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004414 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004415 return 0;
4416#else
4417 return -EOPNOTSUPP;
4418#endif
4419}
4420
Jens Axboec1ca7572019-12-25 22:18:28 -07004421static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4422{
4423#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004424 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004425 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004426 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4427 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004428
4429 req->madvise.addr = READ_ONCE(sqe->addr);
4430 req->madvise.len = READ_ONCE(sqe->len);
4431 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4432 return 0;
4433#else
4434 return -EOPNOTSUPP;
4435#endif
4436}
4437
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004438static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004439{
4440#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4441 struct io_madvise *ma = &req->madvise;
4442 int ret;
4443
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004444 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004445 return -EAGAIN;
4446
Minchan Kim0726b012020-10-17 16:14:50 -07004447 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004448 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004449 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004450 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004451 return 0;
4452#else
4453 return -EOPNOTSUPP;
4454#endif
4455}
4456
Jens Axboe4840e412019-12-25 22:03:45 -07004457static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4458{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004459 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004460 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004461 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4462 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004463
4464 req->fadvise.offset = READ_ONCE(sqe->off);
4465 req->fadvise.len = READ_ONCE(sqe->len);
4466 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4467 return 0;
4468}
4469
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004470static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004471{
4472 struct io_fadvise *fa = &req->fadvise;
4473 int ret;
4474
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004475 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004476 switch (fa->advice) {
4477 case POSIX_FADV_NORMAL:
4478 case POSIX_FADV_RANDOM:
4479 case POSIX_FADV_SEQUENTIAL:
4480 break;
4481 default:
4482 return -EAGAIN;
4483 }
4484 }
Jens Axboe4840e412019-12-25 22:03:45 -07004485
4486 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4487 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004488 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004489 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004490 return 0;
4491}
4492
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004493static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4494{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004495 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004496 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004497 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004498 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004499 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004500 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004501
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004502 req->statx.dfd = READ_ONCE(sqe->fd);
4503 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004504 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004505 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4506 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004507
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004508 return 0;
4509}
4510
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004511static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004512{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004513 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004514 int ret;
4515
Pavel Begunkov59d70012021-03-22 01:58:30 +00004516 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004517 return -EAGAIN;
4518
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004519 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4520 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004521
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004522 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004523 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004524 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004525 return 0;
4526}
4527
Jens Axboeb5dba592019-12-11 14:02:38 -07004528static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4529{
Jens Axboe14587a462020-09-05 11:36:08 -06004530 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004531 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004532 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004533 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -07004534 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004535 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004536 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004537
4538 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004539 req->close.file_slot = READ_ONCE(sqe->file_index);
4540 if (req->close.file_slot && req->close.fd)
4541 return -EINVAL;
4542
Jens Axboeb5dba592019-12-11 14:02:38 -07004543 return 0;
4544}
4545
Pavel Begunkov889fca72021-02-10 00:03:09 +00004546static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004547{
Jens Axboe9eac1902021-01-19 15:50:37 -07004548 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004549 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004550 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004551 struct file *file = NULL;
4552 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004553
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004554 if (req->close.file_slot) {
4555 ret = io_close_fixed(req, issue_flags);
4556 goto err;
4557 }
4558
Jens Axboe9eac1902021-01-19 15:50:37 -07004559 spin_lock(&files->file_lock);
4560 fdt = files_fdtable(files);
4561 if (close->fd >= fdt->max_fds) {
4562 spin_unlock(&files->file_lock);
4563 goto err;
4564 }
4565 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004566 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004567 spin_unlock(&files->file_lock);
4568 file = NULL;
4569 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004570 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004571
4572 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004573 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004574 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004575 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004576 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004577
Jens Axboe9eac1902021-01-19 15:50:37 -07004578 ret = __close_fd_get_file(close->fd, &file);
4579 spin_unlock(&files->file_lock);
4580 if (ret < 0) {
4581 if (ret == -ENOENT)
4582 ret = -EBADF;
4583 goto err;
4584 }
4585
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004586 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004587 ret = filp_close(file, current->files);
4588err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004589 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004590 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004591 if (file)
4592 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004593 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004594 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004595}
4596
Pavel Begunkov1155c762021-02-18 18:29:38 +00004597static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004598{
4599 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004600
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004601 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4602 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004603 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4604 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004605 return -EINVAL;
4606
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004607 req->sync.off = READ_ONCE(sqe->off);
4608 req->sync.len = READ_ONCE(sqe->len);
4609 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004610 return 0;
4611}
4612
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004613static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004614{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004615 int ret;
4616
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004617 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004618 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004619 return -EAGAIN;
4620
Jens Axboe9adbd452019-12-20 08:45:55 -07004621 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004622 req->sync.flags);
4623 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004624 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004625 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004626 return 0;
4627}
4628
YueHaibing469956e2020-03-04 15:53:52 +08004629#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004630static int io_setup_async_msg(struct io_kiocb *req,
4631 struct io_async_msghdr *kmsg)
4632{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004633 struct io_async_msghdr *async_msg = req->async_data;
4634
4635 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004636 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004637 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004638 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004639 return -ENOMEM;
4640 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004641 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004642 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004643 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004644 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004645 /* if were using fast_iov, set it to the new one */
4646 if (!async_msg->free_iov)
4647 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4648
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004649 return -EAGAIN;
4650}
4651
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004652static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4653 struct io_async_msghdr *iomsg)
4654{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004655 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004656 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004657 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004658 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004659}
4660
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004661static int io_sendmsg_prep_async(struct io_kiocb *req)
4662{
4663 int ret;
4664
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004665 ret = io_sendmsg_copy_hdr(req, req->async_data);
4666 if (!ret)
4667 req->flags |= REQ_F_NEED_CLEANUP;
4668 return ret;
4669}
4670
Jens Axboe3529d8c2019-12-19 18:24:38 -07004671static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004672{
Jens Axboee47293f2019-12-20 08:58:21 -07004673 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004674
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004675 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4676 return -EINVAL;
4677
Pavel Begunkov270a5942020-07-12 20:41:04 +03004678 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004679 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004680 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4681 if (sr->msg_flags & MSG_DONTWAIT)
4682 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004683
Jens Axboed8768362020-02-27 14:17:49 -07004684#ifdef CONFIG_COMPAT
4685 if (req->ctx->compat)
4686 sr->msg_flags |= MSG_CMSG_COMPAT;
4687#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004688 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004689}
4690
Pavel Begunkov889fca72021-02-10 00:03:09 +00004691static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004692{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004693 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004694 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004695 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004696 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004697 int ret;
4698
Florent Revestdba4a922020-12-04 12:36:04 +01004699 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004700 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004701 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004702
Pavel Begunkovd886e182021-10-04 20:02:56 +01004703 if (req_has_async_data(req)) {
4704 kmsg = req->async_data;
4705 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004706 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004707 if (ret)
4708 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004709 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004710 }
4711
Pavel Begunkov04411802021-04-01 15:44:00 +01004712 flags = req->sr_msg.msg_flags;
4713 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004714 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004715 if (flags & MSG_WAITALL)
4716 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4717
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004718 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004719 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004720 return io_setup_async_msg(req, kmsg);
4721 if (ret == -ERESTARTSYS)
4722 ret = -EINTR;
4723
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004724 /* fast path, check for non-NULL to avoid function call */
4725 if (kmsg->free_iov)
4726 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004727 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004728 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004729 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004730 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004731 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004732}
4733
Pavel Begunkov889fca72021-02-10 00:03:09 +00004734static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004735{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004736 struct io_sr_msg *sr = &req->sr_msg;
4737 struct msghdr msg;
4738 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004739 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004740 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004741 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004742 int ret;
4743
Florent Revestdba4a922020-12-04 12:36:04 +01004744 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004745 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004746 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004747
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004748 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4749 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004750 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004751
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004752 msg.msg_name = NULL;
4753 msg.msg_control = NULL;
4754 msg.msg_controllen = 0;
4755 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004756
Pavel Begunkov04411802021-04-01 15:44:00 +01004757 flags = req->sr_msg.msg_flags;
4758 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004759 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004760 if (flags & MSG_WAITALL)
4761 min_ret = iov_iter_count(&msg.msg_iter);
4762
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004763 msg.msg_flags = flags;
4764 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004765 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004766 return -EAGAIN;
4767 if (ret == -ERESTARTSYS)
4768 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004769
Stefan Metzmacher00312752021-03-20 20:33:36 +01004770 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004771 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004772 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004773 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004774}
4775
Pavel Begunkov1400e692020-07-12 20:41:05 +03004776static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4777 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004778{
4779 struct io_sr_msg *sr = &req->sr_msg;
4780 struct iovec __user *uiov;
4781 size_t iov_len;
4782 int ret;
4783
Pavel Begunkov1400e692020-07-12 20:41:05 +03004784 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4785 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004786 if (ret)
4787 return ret;
4788
4789 if (req->flags & REQ_F_BUFFER_SELECT) {
4790 if (iov_len > 1)
4791 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004792 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004793 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004794 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004795 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004796 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004797 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004798 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004799 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004800 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004801 if (ret > 0)
4802 ret = 0;
4803 }
4804
4805 return ret;
4806}
4807
4808#ifdef CONFIG_COMPAT
4809static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004810 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004811{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004812 struct io_sr_msg *sr = &req->sr_msg;
4813 struct compat_iovec __user *uiov;
4814 compat_uptr_t ptr;
4815 compat_size_t len;
4816 int ret;
4817
Pavel Begunkov4af34172021-04-11 01:46:30 +01004818 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4819 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004820 if (ret)
4821 return ret;
4822
4823 uiov = compat_ptr(ptr);
4824 if (req->flags & REQ_F_BUFFER_SELECT) {
4825 compat_ssize_t clen;
4826
4827 if (len > 1)
4828 return -EINVAL;
4829 if (!access_ok(uiov, sizeof(*uiov)))
4830 return -EFAULT;
4831 if (__get_user(clen, &uiov->iov_len))
4832 return -EFAULT;
4833 if (clen < 0)
4834 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004835 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004836 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004837 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004838 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004839 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004840 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004841 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004842 if (ret < 0)
4843 return ret;
4844 }
4845
4846 return 0;
4847}
Jens Axboe03b12302019-12-02 18:50:25 -07004848#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004849
Pavel Begunkov1400e692020-07-12 20:41:05 +03004850static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4851 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004852{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004853 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004854
4855#ifdef CONFIG_COMPAT
4856 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004857 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004858#endif
4859
Pavel Begunkov1400e692020-07-12 20:41:05 +03004860 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004861}
4862
Jens Axboebcda7ba2020-02-23 16:42:51 -07004863static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004864 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004865{
4866 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004867
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01004868 return io_buffer_select(req, &sr->len, sr->bgid, needs_lock);
Jens Axboe03b12302019-12-02 18:50:25 -07004869}
4870
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004871static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4872{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01004873 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004874}
4875
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004876static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004877{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004878 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004879
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004880 ret = io_recvmsg_copy_hdr(req, req->async_data);
4881 if (!ret)
4882 req->flags |= REQ_F_NEED_CLEANUP;
4883 return ret;
4884}
4885
4886static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4887{
4888 struct io_sr_msg *sr = &req->sr_msg;
4889
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004890 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4891 return -EINVAL;
4892
Pavel Begunkov270a5942020-07-12 20:41:04 +03004893 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004894 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004895 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004896 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4897 if (sr->msg_flags & MSG_DONTWAIT)
4898 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004899
Jens Axboed8768362020-02-27 14:17:49 -07004900#ifdef CONFIG_COMPAT
4901 if (req->ctx->compat)
4902 sr->msg_flags |= MSG_CMSG_COMPAT;
4903#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004904 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004905}
4906
Pavel Begunkov889fca72021-02-10 00:03:09 +00004907static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004908{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004909 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004910 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004911 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004912 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004913 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004914 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004915 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004916
Florent Revestdba4a922020-12-04 12:36:04 +01004917 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004918 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004919 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004920
Pavel Begunkovd886e182021-10-04 20:02:56 +01004921 if (req_has_async_data(req)) {
4922 kmsg = req->async_data;
4923 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004924 ret = io_recvmsg_copy_hdr(req, &iomsg);
4925 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004926 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004927 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004928 }
4929
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004930 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004931 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004932 if (IS_ERR(kbuf))
4933 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004934 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004935 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4936 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004937 1, req->sr_msg.len);
4938 }
4939
Pavel Begunkov04411802021-04-01 15:44:00 +01004940 flags = req->sr_msg.msg_flags;
4941 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004942 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004943 if (flags & MSG_WAITALL)
4944 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4945
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004946 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4947 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004948 if (force_nonblock && ret == -EAGAIN)
4949 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004950 if (ret == -ERESTARTSYS)
4951 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004952
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004953 if (req->flags & REQ_F_BUFFER_SELECTED)
4954 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004955 /* fast path, check for non-NULL to avoid function call */
4956 if (kmsg->free_iov)
4957 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004958 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004959 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004960 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004961 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004962 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004963}
4964
Pavel Begunkov889fca72021-02-10 00:03:09 +00004965static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004966{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004967 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004968 struct io_sr_msg *sr = &req->sr_msg;
4969 struct msghdr msg;
4970 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004971 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004972 struct iovec iov;
4973 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004974 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004975 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004976 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004977
Florent Revestdba4a922020-12-04 12:36:04 +01004978 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004979 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004980 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004981
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004982 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004983 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004984 if (IS_ERR(kbuf))
4985 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004986 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004987 }
4988
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004989 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004990 if (unlikely(ret))
4991 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004992
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004993 msg.msg_name = NULL;
4994 msg.msg_control = NULL;
4995 msg.msg_controllen = 0;
4996 msg.msg_namelen = 0;
4997 msg.msg_iocb = NULL;
4998 msg.msg_flags = 0;
4999
Pavel Begunkov04411802021-04-01 15:44:00 +01005000 flags = req->sr_msg.msg_flags;
5001 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005002 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005003 if (flags & MSG_WAITALL)
5004 min_ret = iov_iter_count(&msg.msg_iter);
5005
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005006 ret = sock_recvmsg(sock, &msg, flags);
5007 if (force_nonblock && ret == -EAGAIN)
5008 return -EAGAIN;
5009 if (ret == -ERESTARTSYS)
5010 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005011out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005012 if (req->flags & REQ_F_BUFFER_SELECTED)
5013 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01005014 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005015 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005016 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005017 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005018}
5019
Jens Axboe3529d8c2019-12-19 18:24:38 -07005020static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005021{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005022 struct io_accept *accept = &req->accept;
5023
Jens Axboe14587a462020-09-05 11:36:08 -06005024 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005025 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005026 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005027 return -EINVAL;
5028
Jens Axboed55e5f52019-12-11 16:12:15 -07005029 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5030 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005031 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005032 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005033
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005034 accept->file_slot = READ_ONCE(sqe->file_index);
5035 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5036 (accept->flags & SOCK_CLOEXEC)))
5037 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005038 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5039 return -EINVAL;
5040 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5041 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005042 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005043}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005044
Pavel Begunkov889fca72021-02-10 00:03:09 +00005045static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005046{
5047 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005048 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005049 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005050 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005051 struct file *file;
5052 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005053
Jiufei Xuee697dee2020-06-10 13:41:59 +08005054 if (req->file->f_flags & O_NONBLOCK)
5055 req->flags |= REQ_F_NOWAIT;
5056
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005057 if (!fixed) {
5058 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5059 if (unlikely(fd < 0))
5060 return fd;
5061 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005062 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5063 accept->flags);
5064 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005065 if (!fixed)
5066 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005067 ret = PTR_ERR(file);
5068 if (ret == -EAGAIN && force_nonblock)
5069 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005070 if (ret == -ERESTARTSYS)
5071 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005072 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005073 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005074 fd_install(fd, file);
5075 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005076 } else {
5077 ret = io_install_fixed_file(req, file, issue_flags,
5078 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005079 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005080 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005081 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005082}
5083
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005084static int io_connect_prep_async(struct io_kiocb *req)
5085{
5086 struct io_async_connect *io = req->async_data;
5087 struct io_connect *conn = &req->connect;
5088
5089 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5090}
5091
Jens Axboe3529d8c2019-12-19 18:24:38 -07005092static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005093{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005094 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005095
Jens Axboe14587a462020-09-05 11:36:08 -06005096 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005097 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005098 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5099 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005100 return -EINVAL;
5101
Jens Axboe3529d8c2019-12-19 18:24:38 -07005102 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5103 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005104 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005105}
5106
Pavel Begunkov889fca72021-02-10 00:03:09 +00005107static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005108{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005109 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005110 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005111 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005112 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005113
Pavel Begunkovd886e182021-10-04 20:02:56 +01005114 if (req_has_async_data(req)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005115 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005116 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005117 ret = move_addr_to_kernel(req->connect.addr,
5118 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005119 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005120 if (ret)
5121 goto out;
5122 io = &__io;
5123 }
5124
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005125 file_flags = force_nonblock ? O_NONBLOCK : 0;
5126
Jens Axboee8c2bc12020-08-15 18:44:09 -07005127 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005128 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005129 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Pavel Begunkovd886e182021-10-04 20:02:56 +01005130 if (req_has_async_data(req))
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005131 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005132 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005133 ret = -ENOMEM;
5134 goto out;
5135 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005136 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005137 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005138 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005139 if (ret == -ERESTARTSYS)
5140 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005141out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005142 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005143 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005144 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005145 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005146}
YueHaibing469956e2020-03-04 15:53:52 +08005147#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005148#define IO_NETOP_FN(op) \
5149static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5150{ \
5151 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005152}
5153
Jens Axboe99a10082021-02-19 09:35:19 -07005154#define IO_NETOP_PREP(op) \
5155IO_NETOP_FN(op) \
5156static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5157{ \
5158 return -EOPNOTSUPP; \
5159} \
5160
5161#define IO_NETOP_PREP_ASYNC(op) \
5162IO_NETOP_PREP(op) \
5163static int io_##op##_prep_async(struct io_kiocb *req) \
5164{ \
5165 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005166}
5167
Jens Axboe99a10082021-02-19 09:35:19 -07005168IO_NETOP_PREP_ASYNC(sendmsg);
5169IO_NETOP_PREP_ASYNC(recvmsg);
5170IO_NETOP_PREP_ASYNC(connect);
5171IO_NETOP_PREP(accept);
5172IO_NETOP_FN(send);
5173IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005174#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005175
Jens Axboed7718a92020-02-14 22:23:12 -07005176struct io_poll_table {
5177 struct poll_table_struct pt;
5178 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005179 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005180 int error;
5181};
5182
Jens Axboed7718a92020-02-14 22:23:12 -07005183static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005184 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005185{
Jens Axboed7718a92020-02-14 22:23:12 -07005186 /* for instances that support it check for an event match first: */
5187 if (mask && !(mask & poll->events))
5188 return 0;
5189
5190 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5191
5192 list_del_init(&poll->wait.entry);
5193
Jens Axboed7718a92020-02-14 22:23:12 -07005194 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005195 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005196
Jens Axboed7718a92020-02-14 22:23:12 -07005197 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005198 * If this fails, then the task is exiting. When a task exits, the
5199 * work gets canceled, so just cancel this request as well instead
5200 * of executing it. We can't safely execute it anyway, as we may not
5201 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005202 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005203 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005204 return 1;
5205}
5206
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005207static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5208 __acquires(&req->ctx->completion_lock)
5209{
5210 struct io_ring_ctx *ctx = req->ctx;
5211
Jens Axboe316319e2021-08-19 09:41:42 -06005212 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005213 if (unlikely(req->task->flags & PF_EXITING))
5214 WRITE_ONCE(poll->canceled, true);
5215
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005216 if (!req->result && !READ_ONCE(poll->canceled)) {
5217 struct poll_table_struct pt = { ._key = poll->events };
5218
5219 req->result = vfs_poll(req->file, &pt) & poll->events;
5220 }
5221
Jens Axboe79ebeae2021-08-10 15:18:27 -06005222 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005223 if (!req->result && !READ_ONCE(poll->canceled)) {
5224 add_wait_queue(poll->head, &poll->wait);
5225 return true;
5226 }
5227
5228 return false;
5229}
5230
Jens Axboed4e7cd32020-08-15 11:44:50 -07005231static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005232{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005233 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005234 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005235 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005236 return req->apoll->double_poll;
5237}
5238
5239static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5240{
5241 if (req->opcode == IORING_OP_POLL_ADD)
5242 return &req->poll;
5243 return &req->apoll->poll;
5244}
5245
5246static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005247 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005248{
5249 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005250
5251 lockdep_assert_held(&req->ctx->completion_lock);
5252
5253 if (poll && poll->head) {
5254 struct wait_queue_head *head = poll->head;
5255
Jens Axboe79ebeae2021-08-10 15:18:27 -06005256 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005257 list_del_init(&poll->wait.entry);
5258 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005259 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005260 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005261 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005262 }
5263}
5264
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005265static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005266 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005267{
5268 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005269 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005270 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005271
Pavel Begunkove27414b2021-04-09 09:13:20 +01005272 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005273 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005274 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005275 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005276 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005277 }
Jens Axboeb69de282021-03-17 08:37:41 -06005278 if (req->poll.events & EPOLLONESHOT)
5279 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005280 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
5281 req->poll.events |= EPOLLONESHOT;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005282 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005283 }
Hao Xu7b289c32021-04-13 15:20:39 +08005284 if (flags & IORING_CQE_F_MORE)
5285 ctx->cq_extra++;
5286
Jens Axboe88e41cf2021-02-22 22:08:01 -07005287 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005288}
5289
Pavel Begunkovf237c302021-08-18 12:42:46 +01005290static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005291{
Jens Axboe6d816e02020-08-11 08:04:14 -06005292 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005293 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005294
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005295 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005296 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005297 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005298 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005299
Hao Xu5b7aa382021-09-22 18:12:38 +08005300 if (req->poll.done) {
5301 spin_unlock(&ctx->completion_lock);
5302 return;
5303 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005304 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005305 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005306 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005307 hash_del(&req->hash_node);
Hao Xubd99c712021-09-22 18:12:36 +08005308 req->poll.done = true;
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005309 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005310 req->result = 0;
5311 add_wait_queue(req->poll.head, &req->poll.wait);
5312 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005313 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005314 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005315 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005316
Jens Axboe88e41cf2021-02-22 22:08:01 -07005317 if (done) {
5318 nxt = io_put_req_find_next(req);
5319 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005320 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005321 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005322 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005323}
5324
5325static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5326 int sync, void *key)
5327{
5328 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005329 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005330 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005331 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005332
5333 /* for instances that support it check for an event match first: */
5334 if (mask && !(mask & poll->events))
5335 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005336 if (!(poll->events & EPOLLONESHOT))
5337 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005338
Jens Axboe8706e042020-09-28 08:38:54 -06005339 list_del_init(&wait->entry);
5340
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005341 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005342 bool done;
5343
Jens Axboe79ebeae2021-08-10 15:18:27 -06005344 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005345 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005346 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005347 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005348 /* make sure double remove sees this as being gone */
5349 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005350 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005351 if (!done) {
5352 /* use wait func handler, so it matches the rq type */
5353 poll->wait.func(&poll->wait, mode, sync, key);
5354 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005355 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005356 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005357 return 1;
5358}
5359
5360static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5361 wait_queue_func_t wake_func)
5362{
5363 poll->head = NULL;
5364 poll->done = false;
5365 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005366#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5367 /* mask in events that we always want/need */
5368 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005369 INIT_LIST_HEAD(&poll->wait.entry);
5370 init_waitqueue_func_entry(&poll->wait, wake_func);
5371}
5372
5373static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005374 struct wait_queue_head *head,
5375 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005376{
5377 struct io_kiocb *req = pt->req;
5378
5379 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005380 * The file being polled uses multiple waitqueues for poll handling
5381 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5382 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005383 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005384 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005385 struct io_poll_iocb *poll_one = poll;
5386
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005387 /* double add on the same waitqueue head, ignore */
5388 if (poll_one->head == head)
5389 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005390 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005391 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005392 if ((*poll_ptr)->head == head)
5393 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005394 pt->error = -EINVAL;
5395 return;
5396 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005397 /*
5398 * Can't handle multishot for double wait for now, turn it
5399 * into one-shot mode.
5400 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005401 if (!(poll_one->events & EPOLLONESHOT))
5402 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005403 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5404 if (!poll) {
5405 pt->error = -ENOMEM;
5406 return;
5407 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005408 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005409 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005410 poll->wait.private = req;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005411
Jens Axboe807abcb2020-07-17 17:09:27 -06005412 *poll_ptr = poll;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005413 if (req->opcode == IORING_OP_POLL_ADD)
5414 req->flags |= REQ_F_ASYNC_DATA;
Jens Axboe18bceab2020-05-15 11:56:54 -06005415 }
5416
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005417 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005418 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005419
5420 if (poll->events & EPOLLEXCLUSIVE)
5421 add_wait_queue_exclusive(head, &poll->wait);
5422 else
5423 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005424}
5425
5426static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5427 struct poll_table_struct *p)
5428{
5429 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005430 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005431
Jens Axboe807abcb2020-07-17 17:09:27 -06005432 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005433}
5434
Pavel Begunkovf237c302021-08-18 12:42:46 +01005435static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005436{
Jens Axboed7718a92020-02-14 22:23:12 -07005437 struct async_poll *apoll = req->apoll;
5438 struct io_ring_ctx *ctx = req->ctx;
5439
Olivier Langlois236daeae2021-05-31 02:36:37 -04005440 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005441
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005442 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005443 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005444 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005445 }
5446
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005447 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005448 io_poll_remove_double(req);
Hao Xubd99c712021-09-22 18:12:36 +08005449 apoll->poll.done = true;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005450 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005451
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005452 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005453 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005454 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005455 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005456}
5457
5458static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5459 void *key)
5460{
5461 struct io_kiocb *req = wait->private;
5462 struct io_poll_iocb *poll = &req->apoll->poll;
5463
5464 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5465 key_to_poll(key));
5466
5467 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5468}
5469
5470static void io_poll_req_insert(struct io_kiocb *req)
5471{
5472 struct io_ring_ctx *ctx = req->ctx;
5473 struct hlist_head *list;
5474
5475 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5476 hlist_add_head(&req->hash_node, list);
5477}
5478
5479static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5480 struct io_poll_iocb *poll,
5481 struct io_poll_table *ipt, __poll_t mask,
5482 wait_queue_func_t wake_func)
5483 __acquires(&ctx->completion_lock)
5484{
5485 struct io_ring_ctx *ctx = req->ctx;
5486 bool cancel = false;
5487
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005488 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005489 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005490 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005491 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005492
5493 ipt->pt._key = mask;
5494 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005495 ipt->error = 0;
5496 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005497
Jens Axboed7718a92020-02-14 22:23:12 -07005498 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005499 if (unlikely(!ipt->nr_entries) && !ipt->error)
5500 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005501
Jens Axboe79ebeae2021-08-10 15:18:27 -06005502 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005503 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005504 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005505 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005506 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005507 if (unlikely(list_empty(&poll->wait.entry))) {
5508 if (ipt->error)
5509 cancel = true;
5510 ipt->error = 0;
5511 mask = 0;
5512 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005513 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005514 list_del_init(&poll->wait.entry);
5515 else if (cancel)
5516 WRITE_ONCE(poll->canceled, true);
5517 else if (!poll->done) /* actually waiting for an event */
5518 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005519 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005520 }
5521
5522 return mask;
5523}
5524
Olivier Langlois59b735a2021-06-22 05:17:39 -07005525enum {
5526 IO_APOLL_OK,
5527 IO_APOLL_ABORTED,
5528 IO_APOLL_READY
5529};
5530
5531static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005532{
5533 const struct io_op_def *def = &io_op_defs[req->opcode];
5534 struct io_ring_ctx *ctx = req->ctx;
5535 struct async_poll *apoll;
5536 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005537 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005538 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005539
5540 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005541 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005542 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005543 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005544 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005545 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005546
5547 if (def->pollin) {
5548 rw = READ;
5549 mask |= POLLIN | POLLRDNORM;
5550
5551 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5552 if ((req->opcode == IORING_OP_RECVMSG) &&
5553 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5554 mask &= ~POLLIN;
5555 } else {
5556 rw = WRITE;
5557 mask |= POLLOUT | POLLWRNORM;
5558 }
5559
Jens Axboe9dab14b2020-08-25 12:27:50 -06005560 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005561 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005562 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005563
5564 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5565 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005566 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005567 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005568 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005569 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005570 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005571 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005572
5573 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5574 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005575 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005576 if (ret || ipt.error)
5577 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5578
Olivier Langlois236daeae2021-05-31 02:36:37 -04005579 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5580 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005581 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005582}
5583
5584static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005585 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005586 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005587{
Jens Axboeb41e9852020-02-17 09:52:41 -07005588 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005589
Jens Axboe50826202021-02-23 09:02:26 -07005590 if (!poll->head)
5591 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005592 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005593 if (do_cancel)
5594 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005595 if (!list_empty(&poll->wait.entry)) {
5596 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005597 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005598 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005599 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005600 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005601 return do_complete;
5602}
5603
Pavel Begunkov5d709042021-08-09 20:18:13 +01005604static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005605 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005606{
5607 bool do_complete;
5608
Jens Axboed4e7cd32020-08-15 11:44:50 -07005609 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005610 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005611
Jens Axboeb41e9852020-02-17 09:52:41 -07005612 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005613 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005614 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005615 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005616 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005617 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005618 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005619}
5620
Jens Axboe76e1b642020-09-26 15:05:03 -06005621/*
5622 * Returns true if we found and killed one or more poll requests
5623 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01005624static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx,
5625 struct task_struct *tsk, bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005626{
Jens Axboe78076bb2019-12-04 19:56:40 -07005627 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005628 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005629 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005630
Jens Axboe79ebeae2021-08-10 15:18:27 -06005631 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005632 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5633 struct hlist_head *list;
5634
5635 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005636 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005637 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005638 posted += io_poll_remove_one(req);
5639 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005640 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005641 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005642
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005643 if (posted)
5644 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005645
5646 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005647}
5648
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005649static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5650 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005651 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005652{
Jens Axboe78076bb2019-12-04 19:56:40 -07005653 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005654 struct io_kiocb *req;
5655
Jens Axboe78076bb2019-12-04 19:56:40 -07005656 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5657 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005658 if (sqe_addr != req->user_data)
5659 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005660 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5661 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005662 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005663 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005664 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005665}
5666
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005667static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5668 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005669 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005670{
5671 struct io_kiocb *req;
5672
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005673 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005674 if (!req)
5675 return -ENOENT;
5676 if (io_poll_remove_one(req))
5677 return 0;
5678
5679 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005680}
5681
Pavel Begunkov9096af32021-04-14 13:38:36 +01005682static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5683 unsigned int flags)
5684{
5685 u32 events;
5686
5687 events = READ_ONCE(sqe->poll32_events);
5688#ifdef __BIG_ENDIAN
5689 events = swahw32(events);
5690#endif
5691 if (!(flags & IORING_POLL_ADD_MULTI))
5692 events |= EPOLLONESHOT;
5693 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5694}
5695
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005696static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005697 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005698{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005699 struct io_poll_update *upd = &req->poll_update;
5700 u32 flags;
5701
Jens Axboe221c5eb2019-01-17 09:41:58 -07005702 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5703 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005704 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005705 return -EINVAL;
5706 flags = READ_ONCE(sqe->len);
5707 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5708 IORING_POLL_ADD_MULTI))
5709 return -EINVAL;
5710 /* meaningless without update */
5711 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005712 return -EINVAL;
5713
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005714 upd->old_user_data = READ_ONCE(sqe->addr);
5715 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5716 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005717
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005718 upd->new_user_data = READ_ONCE(sqe->off);
5719 if (!upd->update_user_data && upd->new_user_data)
5720 return -EINVAL;
5721 if (upd->update_events)
5722 upd->events = io_poll_parse_events(sqe, flags);
5723 else if (sqe->poll32_events)
5724 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005725
Jens Axboe221c5eb2019-01-17 09:41:58 -07005726 return 0;
5727}
5728
Jens Axboe221c5eb2019-01-17 09:41:58 -07005729static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5730 void *key)
5731{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005732 struct io_kiocb *req = wait->private;
5733 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005734
Jens Axboed7718a92020-02-14 22:23:12 -07005735 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005736}
5737
Jens Axboe221c5eb2019-01-17 09:41:58 -07005738static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5739 struct poll_table_struct *p)
5740{
5741 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5742
Jens Axboee8c2bc12020-08-15 18:44:09 -07005743 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005744}
5745
Jens Axboe3529d8c2019-12-19 18:24:38 -07005746static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005747{
5748 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005749 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005750
5751 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5752 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005753 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005754 return -EINVAL;
5755 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005756 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005757 return -EINVAL;
5758
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005759 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005760 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005761 return 0;
5762}
5763
Pavel Begunkov61e98202021-02-10 00:03:08 +00005764static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005765{
5766 struct io_poll_iocb *poll = &req->poll;
5767 struct io_ring_ctx *ctx = req->ctx;
5768 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005769 __poll_t mask;
Hao Xu5b7aa382021-09-22 18:12:38 +08005770 bool done;
Jens Axboe0969e782019-12-17 18:40:57 -07005771
Jens Axboed7718a92020-02-14 22:23:12 -07005772 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005773
Jens Axboed7718a92020-02-14 22:23:12 -07005774 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5775 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005776
Jens Axboe8c838782019-03-12 15:48:16 -06005777 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005778 ipt.error = 0;
Pavel Begunkoveb6e6f02021-10-04 20:02:59 +01005779 done = __io_poll_complete(req, mask);
5780 io_commit_cqring(req->ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06005781 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005782 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005783
Jens Axboe8c838782019-03-12 15:48:16 -06005784 if (mask) {
5785 io_cqring_ev_posted(ctx);
Hao Xu5b7aa382021-09-22 18:12:38 +08005786 if (done)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005787 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005788 }
Jens Axboe8c838782019-03-12 15:48:16 -06005789 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005790}
5791
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005792static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005793{
5794 struct io_ring_ctx *ctx = req->ctx;
5795 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005796 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005797 int ret;
5798
Jens Axboe79ebeae2021-08-10 15:18:27 -06005799 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005800 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005801 if (!preq) {
5802 ret = -ENOENT;
5803 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005804 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005805
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005806 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5807 completing = true;
5808 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5809 goto err;
5810 }
5811
Jens Axboecb3b200e2021-04-06 09:49:31 -06005812 /*
5813 * Don't allow racy completion with singleshot, as we cannot safely
5814 * update those. For multishot, if we're racing with completion, just
5815 * let completion re-add it.
5816 */
5817 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5818 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5819 ret = -EALREADY;
5820 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005821 }
5822 /* we now have a detached poll request. reissue. */
5823 ret = 0;
5824err:
Jens Axboeb69de282021-03-17 08:37:41 -06005825 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005826 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005827 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005828 io_req_complete(req, ret);
5829 return 0;
5830 }
5831 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005832 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005833 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005834 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005835 preq->poll.events |= IO_POLL_UNMASK;
5836 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005837 if (req->poll_update.update_user_data)
5838 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005839 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005840
Jens Axboeb69de282021-03-17 08:37:41 -06005841 /* complete update request, we're done with it */
5842 io_req_complete(req, ret);
5843
Jens Axboecb3b200e2021-04-06 09:49:31 -06005844 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005845 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005846 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005847 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005848 io_req_complete(preq, ret);
5849 }
Jens Axboeb69de282021-03-17 08:37:41 -06005850 }
5851 return 0;
5852}
5853
Pavel Begunkovf237c302021-08-18 12:42:46 +01005854static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005855{
Pavel Begunkov62245902021-10-02 19:36:14 +01005856 struct io_timeout_data *data = req->async_data;
5857
5858 if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
5859 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005860 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005861}
5862
Jens Axboe5262f562019-09-17 12:26:57 -06005863static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5864{
Jens Axboead8a48a2019-11-15 08:49:11 -07005865 struct io_timeout_data *data = container_of(timer,
5866 struct io_timeout_data, timer);
5867 struct io_kiocb *req = data->req;
5868 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005869 unsigned long flags;
5870
Jens Axboe89850fc2021-08-10 15:11:51 -06005871 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005872 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005873 atomic_set(&req->ctx->cq_timeouts,
5874 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005875 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005876
Jens Axboe89850fc2021-08-10 15:11:51 -06005877 req->io_task_work.func = io_req_task_timeout;
5878 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005879 return HRTIMER_NORESTART;
5880}
5881
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005882static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5883 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005884 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005885{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005886 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005887 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005888 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005889
5890 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005891 found = user_data == req->user_data;
5892 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005893 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005894 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005895 if (!found)
5896 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005897
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005898 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005899 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005900 return ERR_PTR(-EALREADY);
5901 list_del_init(&req->timeout.list);
5902 return req;
5903}
5904
5905static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005906 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005907 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005908{
5909 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5910
5911 if (IS_ERR(req))
5912 return PTR_ERR(req);
5913
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005914 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005915 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005916 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005917 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005918}
5919
Jens Axboe50c1df22021-08-27 17:11:06 -06005920static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
5921{
5922 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
5923 case IORING_TIMEOUT_BOOTTIME:
5924 return CLOCK_BOOTTIME;
5925 case IORING_TIMEOUT_REALTIME:
5926 return CLOCK_REALTIME;
5927 default:
5928 /* can't happen, vetted at prep time */
5929 WARN_ON_ONCE(1);
5930 fallthrough;
5931 case 0:
5932 return CLOCK_MONOTONIC;
5933 }
5934}
5935
Pavel Begunkovf1042b62021-08-28 19:54:39 -06005936static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5937 struct timespec64 *ts, enum hrtimer_mode mode)
5938 __must_hold(&ctx->timeout_lock)
5939{
5940 struct io_timeout_data *io;
5941 struct io_kiocb *req;
5942 bool found = false;
5943
5944 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
5945 found = user_data == req->user_data;
5946 if (found)
5947 break;
5948 }
5949 if (!found)
5950 return -ENOENT;
5951
5952 io = req->async_data;
5953 if (hrtimer_try_to_cancel(&io->timer) == -1)
5954 return -EALREADY;
5955 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
5956 io->timer.function = io_link_timeout_fn;
5957 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
5958 return 0;
5959}
5960
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005961static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5962 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06005963 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005964{
5965 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5966 struct io_timeout_data *data;
5967
5968 if (IS_ERR(req))
5969 return PTR_ERR(req);
5970
5971 req->timeout.off = 0; /* noseq */
5972 data = req->async_data;
5973 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06005974 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005975 data->timer.function = io_timeout_fn;
5976 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5977 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005978}
5979
Jens Axboe3529d8c2019-12-19 18:24:38 -07005980static int io_timeout_remove_prep(struct io_kiocb *req,
5981 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005982{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005983 struct io_timeout_rem *tr = &req->timeout_rem;
5984
Jens Axboeb29472e2019-12-17 18:50:29 -07005985 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5986 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005987 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5988 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005989 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07005990 return -EINVAL;
5991
Pavel Begunkovf1042b62021-08-28 19:54:39 -06005992 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005993 tr->addr = READ_ONCE(sqe->addr);
5994 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06005995 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
5996 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
5997 return -EINVAL;
5998 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
5999 tr->ltimeout = true;
6000 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006001 return -EINVAL;
6002 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6003 return -EFAULT;
6004 } else if (tr->flags) {
6005 /* timeout removal doesn't support flags */
6006 return -EINVAL;
6007 }
6008
Jens Axboeb29472e2019-12-17 18:50:29 -07006009 return 0;
6010}
6011
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006012static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6013{
6014 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6015 : HRTIMER_MODE_REL;
6016}
6017
Jens Axboe11365042019-10-16 09:08:32 -06006018/*
6019 * Remove or update an existing timeout command
6020 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006021static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006022{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006023 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006024 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006025 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006026
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006027 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6028 spin_lock(&ctx->completion_lock);
6029 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006030 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006031 spin_unlock_irq(&ctx->timeout_lock);
6032 spin_unlock(&ctx->completion_lock);
6033 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006034 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6035
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006036 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006037 if (tr->ltimeout)
6038 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6039 else
6040 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006041 spin_unlock_irq(&ctx->timeout_lock);
6042 }
Jens Axboe11365042019-10-16 09:08:32 -06006043
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006044 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006045 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006046 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006047 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006048}
6049
Jens Axboe3529d8c2019-12-19 18:24:38 -07006050static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006051 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006052{
Jens Axboead8a48a2019-11-15 08:49:11 -07006053 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006054 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006055 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006056
Jens Axboead8a48a2019-11-15 08:49:11 -07006057 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006058 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006059 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6060 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006061 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006062 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006063 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006064 flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkov62245902021-10-02 19:36:14 +01006065 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
6066 IORING_TIMEOUT_ETIME_SUCCESS))
Jens Axboe50c1df22021-08-27 17:11:06 -06006067 return -EINVAL;
6068 /* more than one clock specified is invalid, obviously */
6069 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006070 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006071
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006072 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006073 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006074 if (unlikely(off && !req->ctx->off_timeout_used))
6075 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006076
Pavel Begunkovd886e182021-10-04 20:02:56 +01006077 if (!req_has_async_data(req) && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006078 return -ENOMEM;
6079
Jens Axboee8c2bc12020-08-15 18:44:09 -07006080 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006081 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006082 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006083
6084 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006085 return -EFAULT;
6086
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006087 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006088 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006089
6090 if (is_timeout_link) {
6091 struct io_submit_link *link = &req->ctx->submit_state.link;
6092
6093 if (!link->head)
6094 return -EINVAL;
6095 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6096 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006097 req->timeout.head = link->last;
6098 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006099 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006100 return 0;
6101}
6102
Pavel Begunkov61e98202021-02-10 00:03:08 +00006103static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006104{
Jens Axboead8a48a2019-11-15 08:49:11 -07006105 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006106 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006107 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006108 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006109
Jens Axboe89850fc2021-08-10 15:11:51 -06006110 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006111
Jens Axboe5262f562019-09-17 12:26:57 -06006112 /*
6113 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006114 * timeout event to be satisfied. If it isn't set, then this is
6115 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006116 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006117 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006118 entry = ctx->timeout_list.prev;
6119 goto add;
6120 }
Jens Axboe5262f562019-09-17 12:26:57 -06006121
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006122 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6123 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006124
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006125 /* Update the last seq here in case io_flush_timeouts() hasn't.
6126 * This is safe because ->completion_lock is held, and submissions
6127 * and completions are never mixed in the same ->completion_lock section.
6128 */
6129 ctx->cq_last_tm_flush = tail;
6130
Jens Axboe5262f562019-09-17 12:26:57 -06006131 /*
6132 * Insertion sort, ensuring the first entry in the list is always
6133 * the one we need first.
6134 */
Jens Axboe5262f562019-09-17 12:26:57 -06006135 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006136 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6137 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006138
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006139 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006140 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006141 /* nxt.seq is behind @tail, otherwise would've been completed */
6142 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006143 break;
6144 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006145add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006146 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006147 data->timer.function = io_timeout_fn;
6148 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006149 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006150 return 0;
6151}
6152
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006153struct io_cancel_data {
6154 struct io_ring_ctx *ctx;
6155 u64 user_data;
6156};
6157
Jens Axboe62755e32019-10-28 21:49:21 -06006158static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006159{
Jens Axboe62755e32019-10-28 21:49:21 -06006160 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006161 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006162
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006163 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006164}
6165
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006166static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6167 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006168{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006169 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006170 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006171 int ret = 0;
6172
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006173 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006174 return -ENOENT;
6175
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006176 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006177 switch (cancel_ret) {
6178 case IO_WQ_CANCEL_OK:
6179 ret = 0;
6180 break;
6181 case IO_WQ_CANCEL_RUNNING:
6182 ret = -EALREADY;
6183 break;
6184 case IO_WQ_CANCEL_NOTFOUND:
6185 ret = -ENOENT;
6186 break;
6187 }
6188
Jens Axboee977d6d2019-11-05 12:39:45 -07006189 return ret;
6190}
6191
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006192static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006193{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006194 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006195 int ret;
6196
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006197 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006198
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006199 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006200 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006201 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006202
6203 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006204 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006205 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006206 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006207 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006208 goto out;
6209 ret = io_poll_cancel(ctx, sqe_addr, false);
6210out:
6211 spin_unlock(&ctx->completion_lock);
6212 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006213}
6214
Jens Axboe3529d8c2019-12-19 18:24:38 -07006215static int io_async_cancel_prep(struct io_kiocb *req,
6216 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006217{
Jens Axboefbf23842019-12-17 18:45:56 -07006218 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006219 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006220 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6221 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006222 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6223 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006224 return -EINVAL;
6225
Jens Axboefbf23842019-12-17 18:45:56 -07006226 req->cancel.addr = READ_ONCE(sqe->addr);
6227 return 0;
6228}
6229
Pavel Begunkov61e98202021-02-10 00:03:08 +00006230static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006231{
6232 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006233 u64 sqe_addr = req->cancel.addr;
6234 struct io_tctx_node *node;
6235 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006236
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006237 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006238 if (ret != -ENOENT)
6239 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006240
6241 /* slow path, try all io-wq's */
6242 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
6243 ret = -ENOENT;
6244 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6245 struct io_uring_task *tctx = node->task->io_uring;
6246
Pavel Begunkov58f99372021-03-12 16:25:55 +00006247 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6248 if (ret != -ENOENT)
6249 break;
6250 }
6251 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00006252done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006253 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006254 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006255 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006256 return 0;
6257}
6258
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006259static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006260 const struct io_uring_sqe *sqe)
6261{
Daniele Albano61710e42020-07-18 14:15:16 -06006262 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6263 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006264 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006265 return -EINVAL;
6266
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006267 req->rsrc_update.offset = READ_ONCE(sqe->off);
6268 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6269 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006270 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006271 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006272 return 0;
6273}
6274
Pavel Begunkov889fca72021-02-10 00:03:09 +00006275static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006276{
6277 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006278 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006279 int ret;
6280
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006281 up.offset = req->rsrc_update.offset;
6282 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006283 up.nr = 0;
6284 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006285 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006286
Jens Axboecdb31c22021-09-24 08:43:54 -06006287 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006288 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006289 &up, req->rsrc_update.nr_args);
Jens Axboecdb31c22021-09-24 08:43:54 -06006290 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Jens Axboe05f3fb32019-12-09 11:22:50 -07006291
6292 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006293 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006294 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006295 return 0;
6296}
6297
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006298static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006299{
Jens Axboed625c6e2019-12-17 19:53:05 -07006300 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006301 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006302 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006303 case IORING_OP_READV:
6304 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006305 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006306 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006307 case IORING_OP_WRITEV:
6308 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006309 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006310 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006311 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006312 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006313 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006314 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006315 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006316 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006317 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006318 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006319 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006320 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006321 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006322 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006323 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006324 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006325 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006326 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006327 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006328 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006329 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006330 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006331 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006332 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006333 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006334 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006335 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006336 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006337 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006338 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006339 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006340 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006341 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006342 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006343 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006344 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006345 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006346 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006347 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006348 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006349 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006350 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006351 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006352 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006353 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006354 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006355 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006356 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006357 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006358 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006359 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006360 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006361 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006362 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006363 case IORING_OP_SHUTDOWN:
6364 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006365 case IORING_OP_RENAMEAT:
6366 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006367 case IORING_OP_UNLINKAT:
6368 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006369 case IORING_OP_MKDIRAT:
6370 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006371 case IORING_OP_SYMLINKAT:
6372 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006373 case IORING_OP_LINKAT:
6374 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006375 }
6376
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006377 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6378 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006379 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006380}
6381
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006382static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006383{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006384 if (!io_op_defs[req->opcode].needs_async_setup)
6385 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01006386 if (WARN_ON_ONCE(req_has_async_data(req)))
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006387 return -EFAULT;
6388 if (io_alloc_async_data(req))
6389 return -EAGAIN;
6390
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006391 switch (req->opcode) {
6392 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006393 return io_rw_prep_async(req, READ);
6394 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006395 return io_rw_prep_async(req, WRITE);
6396 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006397 return io_sendmsg_prep_async(req);
6398 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006399 return io_recvmsg_prep_async(req);
6400 case IORING_OP_CONNECT:
6401 return io_connect_prep_async(req);
6402 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006403 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6404 req->opcode);
6405 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006406}
6407
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006408static u32 io_get_sequence(struct io_kiocb *req)
6409{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006410 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006411
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006412 /* need original cached_sq_head, but it was increased for each req */
6413 io_for_each_link(req, req)
6414 seq--;
6415 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006416}
6417
Pavel Begunkovc0724812021-10-04 20:02:54 +01006418static __cold void io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006419{
6420 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006421 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006422 int ret;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006423 u32 seq = io_get_sequence(req);
Jens Axboedef596e2019-01-09 08:59:42 -07006424
6425 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov5e371262021-09-24 22:00:04 +01006426 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006427queue:
Pavel Begunkov5e371262021-09-24 22:00:04 +01006428 ctx->drain_active = false;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006429 io_req_task_queue(req);
6430 return;
Pavel Begunkov5e371262021-09-24 22:00:04 +01006431 }
Jens Axboedef596e2019-01-09 08:59:42 -07006432
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006433 ret = io_req_prep_async(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006434 if (ret) {
6435fail:
6436 io_req_complete_failed(req, ret);
6437 return;
6438 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006439 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006440 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006441 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006442 ret = -ENOMEM;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006443 goto fail;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006444 }
Jens Axboe31b51512019-01-18 22:56:34 -07006445
Jens Axboe79ebeae2021-08-10 15:18:27 -06006446 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006447 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006448 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006449 kfree(de);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006450 goto queue;
Jens Axboe31b51512019-01-18 22:56:34 -07006451 }
6452
6453 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006454 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006455 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006456 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006457 spin_unlock(&ctx->completion_lock);
Jens Axboe31b51512019-01-18 22:56:34 -07006458}
6459
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006460static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006461{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006462 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01006463 kfree(req->kbuf);
6464 req->kbuf = NULL;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006465 }
6466
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006467 if (req->flags & REQ_F_NEED_CLEANUP) {
6468 switch (req->opcode) {
6469 case IORING_OP_READV:
6470 case IORING_OP_READ_FIXED:
6471 case IORING_OP_READ:
6472 case IORING_OP_WRITEV:
6473 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006474 case IORING_OP_WRITE: {
6475 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006476
6477 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006478 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006479 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006480 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006481 case IORING_OP_SENDMSG: {
6482 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006483
6484 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006485 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006486 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006487 case IORING_OP_SPLICE:
6488 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006489 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6490 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006491 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006492 case IORING_OP_OPENAT:
6493 case IORING_OP_OPENAT2:
6494 if (req->open.filename)
6495 putname(req->open.filename);
6496 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006497 case IORING_OP_RENAMEAT:
6498 putname(req->rename.oldpath);
6499 putname(req->rename.newpath);
6500 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006501 case IORING_OP_UNLINKAT:
6502 putname(req->unlink.filename);
6503 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006504 case IORING_OP_MKDIRAT:
6505 putname(req->mkdir.filename);
6506 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006507 case IORING_OP_SYMLINKAT:
6508 putname(req->symlink.oldpath);
6509 putname(req->symlink.newpath);
6510 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006511 case IORING_OP_LINKAT:
6512 putname(req->hardlink.oldpath);
6513 putname(req->hardlink.newpath);
6514 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006515 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006516 }
Jens Axboe75652a302021-04-15 09:52:40 -06006517 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6518 kfree(req->apoll->double_poll);
6519 kfree(req->apoll);
6520 req->apoll = NULL;
6521 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006522 if (req->flags & REQ_F_INFLIGHT) {
6523 struct io_uring_task *tctx = req->task->io_uring;
6524
6525 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006526 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006527 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006528 put_cred(req->creds);
Pavel Begunkovd886e182021-10-04 20:02:56 +01006529 if (req->flags & REQ_F_ASYNC_DATA) {
6530 kfree(req->async_data);
6531 req->async_data = NULL;
6532 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006533 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006534}
6535
Pavel Begunkov889fca72021-02-10 00:03:09 +00006536static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006537{
Jens Axboeedafcce2019-01-09 09:16:05 -07006538 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006539 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006540 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006541
Pavel Begunkov6878b402021-09-24 21:59:41 +01006542 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006543 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006544
Jens Axboed625c6e2019-12-17 19:53:05 -07006545 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006546 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006547 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006548 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006549 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006550 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006551 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006552 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006553 break;
6554 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006555 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006556 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006557 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006558 break;
6559 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006560 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006561 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006562 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006563 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006564 break;
6565 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006566 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006567 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006568 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006569 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006570 break;
6571 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006572 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006573 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006574 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006575 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006576 break;
6577 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006578 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006579 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006580 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006581 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006582 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006583 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006584 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006585 break;
6586 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006587 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006588 break;
6589 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006590 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006591 break;
6592 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006593 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006594 break;
6595 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006596 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006597 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006598 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006599 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006600 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006601 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006602 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006603 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006604 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006605 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006606 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006607 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006608 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006609 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006610 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006611 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006612 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006613 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006614 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006615 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006616 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006617 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006618 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006619 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006620 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006621 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006622 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006623 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006624 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006625 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006626 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006627 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006628 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006629 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006630 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006631 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006632 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006633 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006634 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006635 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006636 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006637 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006638 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006639 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006640 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006641 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006642 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006643 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006644 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006645 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006646 case IORING_OP_MKDIRAT:
6647 ret = io_mkdirat(req, issue_flags);
6648 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006649 case IORING_OP_SYMLINKAT:
6650 ret = io_symlinkat(req, issue_flags);
6651 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006652 case IORING_OP_LINKAT:
6653 ret = io_linkat(req, issue_flags);
6654 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006655 default:
6656 ret = -EINVAL;
6657 break;
6658 }
Jens Axboe31b51512019-01-18 22:56:34 -07006659
Jens Axboe5730b272021-02-27 15:57:30 -07006660 if (creds)
6661 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006662 if (ret)
6663 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006664 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006665 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6666 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006667
6668 return 0;
6669}
6670
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006671static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6672{
6673 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6674
6675 req = io_put_req_find_next(req);
6676 return req ? &req->work : NULL;
6677}
6678
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006679static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006680{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006681 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006682 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006683 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006684
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006685 /* one will be dropped by ->io_free_work() after returning to io-wq */
6686 if (!(req->flags & REQ_F_REFCOUNT))
6687 __io_req_set_refcount(req, 2);
6688 else
6689 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006690
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006691 timeout = io_prep_linked_timeout(req);
6692 if (timeout)
6693 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006694
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006695 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006696 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006697 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006698
Jens Axboe561fb042019-10-24 07:25:42 -06006699 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006700 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006701 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006702 /*
6703 * We can get EAGAIN for polled IO even though we're
6704 * forcing a sync submission from here, since we can't
6705 * wait for request slots on the block side.
6706 */
6707 if (ret != -EAGAIN)
6708 break;
6709 cond_resched();
6710 } while (1);
6711 }
Jens Axboe31b51512019-01-18 22:56:34 -07006712
Pavel Begunkova3df76982021-02-18 22:32:52 +00006713 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006714 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006715 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006716}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006717
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006718static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006719 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006720{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006721 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006722}
6723
Jens Axboe09bb8392019-03-13 12:39:28 -06006724static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6725 int index)
6726{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006727 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006728
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006729 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006730}
6731
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006732static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006733{
6734 unsigned long file_ptr = (unsigned long) file;
6735
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006736 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006737 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006738 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006739 file_ptr |= FFS_ASYNC_WRITE;
6740 if (S_ISREG(file_inode(file)->i_mode))
6741 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006742 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006743}
6744
Pavel Begunkovac177052021-08-09 13:04:02 +01006745static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6746 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006747{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006748 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006749 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006750
Pavel Begunkovac177052021-08-09 13:04:02 +01006751 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6752 return NULL;
6753 fd = array_index_nospec(fd, ctx->nr_user_files);
6754 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6755 file = (struct file *) (file_ptr & FFS_MASK);
6756 file_ptr &= ~FFS_MASK;
6757 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006758 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006759 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006760 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006761}
6762
Pavel Begunkovac177052021-08-09 13:04:02 +01006763static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006764 struct io_kiocb *req, int fd)
6765{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006766 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006767
6768 trace_io_uring_file_get(ctx, fd);
6769
6770 /* we don't allow fixed io_uring files */
6771 if (file && unlikely(file->f_op == &io_uring_fops))
6772 io_req_track_inflight(req);
6773 return file;
6774}
6775
6776static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006777 struct io_kiocb *req, int fd, bool fixed)
6778{
6779 if (fixed)
6780 return io_file_get_fixed(ctx, req, fd);
6781 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006782 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006783}
6784
Pavel Begunkovf237c302021-08-18 12:42:46 +01006785static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006786{
6787 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006788 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006789
6790 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006791 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006792 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006793 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006794 } else {
6795 io_req_complete_post(req, -ETIME, 0);
6796 }
6797}
6798
Jens Axboe2665abf2019-11-05 12:40:47 -07006799static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6800{
Jens Axboead8a48a2019-11-15 08:49:11 -07006801 struct io_timeout_data *data = container_of(timer,
6802 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006803 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006804 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006805 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006806
Jens Axboe89b263f2021-08-10 15:14:18 -06006807 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006808 prev = req->timeout.head;
6809 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006810
6811 /*
6812 * We don't expect the list to be empty, that will only happen if we
6813 * race with the completion of the linked work.
6814 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006815 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006816 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006817 if (!req_ref_inc_not_zero(prev))
6818 prev = NULL;
6819 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006820 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006821 req->timeout.prev = prev;
6822 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006823
Jens Axboe89b263f2021-08-10 15:14:18 -06006824 req->io_task_work.func = io_req_task_link_timeout;
6825 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006826 return HRTIMER_NORESTART;
6827}
6828
Pavel Begunkovde968c12021-03-19 17:22:33 +00006829static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006830{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006831 struct io_ring_ctx *ctx = req->ctx;
6832
Jens Axboe89b263f2021-08-10 15:14:18 -06006833 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006834 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006835 * If the back reference is NULL, then our linked request finished
6836 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006837 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006838 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006839 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006840
Jens Axboead8a48a2019-11-15 08:49:11 -07006841 data->timer.function = io_link_timeout_fn;
6842 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6843 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006844 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006845 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006846 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006847 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006848 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006849}
6850
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006851static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
6852 __must_hold(&req->ctx->uring_lock)
6853{
6854 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
6855
6856 switch (io_arm_poll_handler(req)) {
6857 case IO_APOLL_READY:
6858 if (linked_timeout) {
6859 io_unprep_linked_timeout(req);
6860 linked_timeout = NULL;
6861 }
6862 io_req_task_queue(req);
6863 break;
6864 case IO_APOLL_ABORTED:
6865 /*
6866 * Queued up for async execution, worker will release
6867 * submit reference when the iocb is actually submitted.
6868 */
6869 io_queue_async_work(req, NULL);
6870 break;
6871 }
6872
6873 if (linked_timeout)
6874 io_queue_linked_timeout(linked_timeout);
6875}
6876
6877static inline void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006878 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006879{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006880 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006881 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006882
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006883 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006884
Pavel Begunkovfff4e402021-10-04 20:02:48 +01006885 if (req->flags & REQ_F_COMPLETE_INLINE) {
6886 io_req_add_compl_list(req);
Pavel Begunkovd9f9d282021-09-24 22:00:00 +01006887 return;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01006888 }
Jens Axboe491381ce2019-10-17 09:20:46 -06006889 /*
6890 * We async punt it if the file wasn't marked NOWAIT, or if the file
6891 * doesn't support non-blocking read/write attempts
6892 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006893 if (likely(!ret)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006894 linked_timeout = io_prep_linked_timeout(req);
6895 if (linked_timeout)
6896 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006897 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006898 io_queue_sqe_arm_apoll(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006899 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006900 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006901 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006902}
6903
Pavel Begunkov4652fe32021-09-24 21:59:58 +01006904static void io_queue_sqe_fallback(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006905 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006906{
Pavel Begunkov4652fe32021-09-24 21:59:58 +01006907 if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01006908 io_req_complete_fail_submit(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006909 } else if (unlikely(req->ctx->drain_active)) {
6910 io_drain_req(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006911 } else {
6912 int ret = io_req_prep_async(req);
6913
6914 if (unlikely(ret))
6915 io_req_complete_failed(req, ret);
6916 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01006917 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07006918 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006919}
6920
Pavel Begunkov4652fe32021-09-24 21:59:58 +01006921static inline void io_queue_sqe(struct io_kiocb *req)
6922 __must_hold(&req->ctx->uring_lock)
6923{
6924 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
6925 __io_queue_sqe(req);
6926 else
6927 io_queue_sqe_fallback(req);
6928}
6929
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006930/*
6931 * Check SQE restrictions (opcode and flags).
6932 *
6933 * Returns 'true' if SQE is allowed, 'false' otherwise.
6934 */
6935static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6936 struct io_kiocb *req,
6937 unsigned int sqe_flags)
6938{
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006939 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6940 return false;
6941
6942 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6943 ctx->restrictions.sqe_flags_required)
6944 return false;
6945
6946 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6947 ctx->restrictions.sqe_flags_required))
6948 return false;
6949
6950 return true;
6951}
6952
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01006953static void io_init_req_drain(struct io_kiocb *req)
6954{
6955 struct io_ring_ctx *ctx = req->ctx;
6956 struct io_kiocb *head = ctx->submit_state.link.head;
6957
6958 ctx->drain_active = true;
6959 if (head) {
6960 /*
6961 * If we need to drain a request in the middle of a link, drain
6962 * the head request and the next request/link after the current
6963 * link. Considering sequential execution of links,
6964 * IOSQE_IO_DRAIN will be maintained for every request of our
6965 * link.
6966 */
6967 head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
6968 ctx->drain_next = true;
6969 }
6970}
6971
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006972static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006973 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006974 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006975{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006976 unsigned int sqe_flags;
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01006977 int personality;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01006978 u8 opcode;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006979
Pavel Begunkov864ea922021-08-09 13:04:08 +01006980 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01006981 req->opcode = opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006982 /* same numerical values with corresponding REQ_F_*, safe to copy */
6983 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006984 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006985 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006986 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006987 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006988
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01006989 if (unlikely(opcode >= IORING_OP_LAST)) {
6990 req->opcode = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006991 return -EINVAL;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01006992 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01006993 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
6994 /* enforce forwards compatibility on users */
6995 if (sqe_flags & ~SQE_VALID_FLAGS)
6996 return -EINVAL;
6997 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01006998 !io_op_defs[opcode].buffer_select)
Pavel Begunkov68fe2562021-09-15 12:03:38 +01006999 return -EOPNOTSUPP;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007000 if (sqe_flags & IOSQE_IO_DRAIN)
7001 io_init_req_drain(req);
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007002 }
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007003 if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
7004 if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
7005 return -EACCES;
7006 /* knock it to the slow queue path, will be drained there */
7007 if (ctx->drain_active)
7008 req->flags |= REQ_F_FORCE_ASYNC;
7009 /* if there is no link, we're at "next" request and need to drain */
7010 if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
7011 ctx->drain_next = false;
7012 ctx->drain_active = true;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007013 req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007014 }
7015 }
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007016
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007017 if (io_op_defs[opcode].needs_file) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007018 struct io_submit_state *state = &ctx->submit_state;
7019
7020 /*
7021 * Plug now if we have more than 2 IO left after this, and the
7022 * target is potentially a read/write to block based storage.
7023 */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007024 if (state->need_plug && io_op_defs[opcode].plug) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007025 state->plug_started = true;
7026 state->need_plug = false;
7027 blk_start_plug(&state->plug);
7028 }
7029
Pavel Begunkov62906e82021-08-10 14:52:47 +01007030 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007031 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007032 if (unlikely(!req->file))
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007033 return -EBADF;
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007034 }
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007035
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007036 personality = READ_ONCE(sqe->personality);
7037 if (personality) {
7038 req->creds = xa_load(&ctx->personalities, personality);
7039 if (!req->creds)
7040 return -EINVAL;
7041 get_cred(req->creds);
7042 req->flags |= REQ_F_CREDS;
7043 }
7044
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007045 return io_req_prep(req, sqe);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007046}
7047
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007048static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007049 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007050 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007051{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007052 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007053 int ret;
7054
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007055 ret = io_init_req(ctx, req, sqe);
7056 if (unlikely(ret)) {
Jens Axboea87acfd2021-09-11 16:04:50 -06007057 trace_io_uring_req_failed(sqe, ret);
7058
Hao Xua8295b92021-08-27 17:46:09 +08007059 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007060 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007061 /*
7062 * we can judge a link req is failed or cancelled by if
7063 * REQ_F_FAIL is set, but the head is an exception since
7064 * it may be set REQ_F_FAIL because of other req's failure
7065 * so let's leverage req->result to distinguish if a head
7066 * is set REQ_F_FAIL because of its failure or other req's
7067 * failure so that we can set the correct ret code for it.
7068 * init result here to avoid affecting the normal path.
7069 */
7070 if (!(link->head->flags & REQ_F_FAIL))
7071 req_fail_link_node(link->head, -ECANCELED);
7072 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7073 /*
7074 * the current req is a normal req, we should return
7075 * error and thus break the submittion loop.
7076 */
7077 io_req_complete_failed(req, ret);
7078 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007079 }
Hao Xua8295b92021-08-27 17:46:09 +08007080 req_fail_link_node(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007081 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007082
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007083 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007084 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7085 req->flags, true,
7086 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007087
Jens Axboe6c271ce2019-01-10 11:22:30 -07007088 /*
7089 * If we already have a head request, queue this one for async
7090 * submittal once the head completes. If we don't have a head but
7091 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7092 * submitted sync once the chain is complete. If none of those
7093 * conditions are true (normal request), then just queue it.
7094 */
7095 if (link->head) {
7096 struct io_kiocb *head = link->head;
7097
Hao Xua8295b92021-08-27 17:46:09 +08007098 if (!(req->flags & REQ_F_FAIL)) {
7099 ret = io_req_prep_async(req);
7100 if (unlikely(ret)) {
7101 req_fail_link_node(req, ret);
7102 if (!(head->flags & REQ_F_FAIL))
7103 req_fail_link_node(head, -ECANCELED);
7104 }
7105 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007106 trace_io_uring_link(ctx, req, head);
7107 link->last->link = req;
7108 link->last = req;
7109
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007110 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
7111 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007112 /* last request of a link, enqueue the link */
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007113 link->head = NULL;
7114 req = head;
7115 } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7116 link->head = req;
7117 link->last = req;
7118 return 0;
Jackie Liu4fe2c962019-09-09 20:50:40 +08007119 }
7120
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007121 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007122 return 0;
7123}
7124
7125/*
7126 * Batched submission is done, ensure local IO is flushed out.
7127 */
Pavel Begunkov553deff2021-09-24 21:59:55 +01007128static void io_submit_state_end(struct io_ring_ctx *ctx)
Jens Axboe3529d8c2019-12-19 18:24:38 -07007129{
Pavel Begunkov553deff2021-09-24 21:59:55 +01007130 struct io_submit_state *state = &ctx->submit_state;
7131
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007132 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007133 io_queue_sqe(state->link.head);
Pavel Begunkov553deff2021-09-24 21:59:55 +01007134 /* flush only after queuing links as they can generate completions */
Pavel Begunkovc4501782021-09-08 16:40:52 +01007135 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007136 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007137 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007138}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007139
Jens Axboe9e645e112019-05-10 16:07:28 -06007140/*
7141 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007142 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007143static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007144 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007145{
7146 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007147 state->need_plug = max_ios > 2;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007148 /* set only head, no need to init link_last in advance */
7149 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007150}
7151
Jens Axboe193155c2020-02-22 23:22:19 -07007152static void io_commit_sqring(struct io_ring_ctx *ctx)
7153{
Jens Axboe75c6a032020-01-28 10:15:23 -07007154 struct io_rings *rings = ctx->rings;
7155
7156 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007157 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007158 * since once we write the new head, the application could
7159 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007160 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007161 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007162}
7163
Jens Axboe9e645e112019-05-10 16:07:28 -06007164/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007165 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007166 * that is mapped by userspace. This means that care needs to be taken to
7167 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007168 * being a good citizen. If members of the sqe are validated and then later
7169 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007170 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007171 */
7172static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007173{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007174 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007175 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007176
7177 /*
7178 * The cached sq head (or cq tail) serves two purposes:
7179 *
7180 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007181 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007182 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007183 * though the application is the one updating it.
7184 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007185 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007186 if (likely(head < ctx->sq_entries))
7187 return &ctx->sq_sqes[head];
7188
7189 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007190 ctx->cq_extra--;
7191 WRITE_ONCE(ctx->rings->sq_dropped,
7192 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007193 return NULL;
7194}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007195
Jens Axboe0f212202020-09-13 13:09:39 -06007196static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007197 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007198{
Pavel Begunkov69629802021-09-24 22:00:01 +01007199 unsigned int entries = io_sqring_entries(ctx);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007200 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007201
Pavel Begunkov51d48da2021-10-04 20:02:47 +01007202 if (unlikely(!entries))
Pavel Begunkov69629802021-09-24 22:00:01 +01007203 return 0;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007204 /* make sure SQ entry isn't read before tail */
Pavel Begunkov69629802021-09-24 22:00:01 +01007205 nr = min3(nr, ctx->sq_entries, entries);
Pavel Begunkov9a108672021-08-27 11:55:01 +01007206 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007207
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007208 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov69629802021-09-24 22:00:01 +01007209 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007210 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007211 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007212
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007213 if (unlikely(!io_alloc_req_refill(ctx))) {
Pavel Begunkov196be952019-11-07 01:41:06 +03007214 if (!submitted)
7215 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007216 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007217 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007218 req = io_alloc_req(ctx);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007219 sqe = io_get_sqe(ctx);
7220 if (unlikely(!sqe)) {
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01007221 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007222 break;
7223 }
Jens Axboed3656342019-12-18 09:50:26 -07007224 /* will complete beyond this point, count as submitted */
7225 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007226 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007227 break;
Pavel Begunkov69629802021-09-24 22:00:01 +01007228 } while (submitted < nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007229
Pavel Begunkov9466f432020-01-25 22:34:01 +03007230 if (unlikely(submitted != nr)) {
7231 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007232 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007233
Pavel Begunkov09899b12021-06-14 02:36:22 +01007234 current->io_uring->cached_refs += unused;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007235 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007236
Pavel Begunkov553deff2021-09-24 21:59:55 +01007237 io_submit_state_end(ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007238 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7239 io_commit_sqring(ctx);
7240
Jens Axboe6c271ce2019-01-10 11:22:30 -07007241 return submitted;
7242}
7243
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007244static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7245{
7246 return READ_ONCE(sqd->state);
7247}
7248
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007249static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7250{
7251 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007252 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007253 WRITE_ONCE(ctx->rings->sq_flags,
7254 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007255 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007256}
7257
7258static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7259{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007260 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007261 WRITE_ONCE(ctx->rings->sq_flags,
7262 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007263 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007264}
7265
Xiaoguang Wang08369242020-11-03 14:15:59 +08007266static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007267{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007268 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007269 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007270
Jens Axboec8d1ba52020-09-14 11:07:26 -06007271 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007272 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007273 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7274 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007275
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007276 if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007277 const struct cred *creds = NULL;
7278
7279 if (ctx->sq_creds != current_cred())
7280 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007281
Xiaoguang Wang08369242020-11-03 14:15:59 +08007282 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007283 if (!wq_list_empty(&ctx->iopoll_list))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01007284 io_do_iopoll(ctx, true);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007285
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007286 /*
7287 * Don't submit if refs are dying, good for io_uring_register(),
7288 * but also it is relied upon by io_ring_exit_work()
7289 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007290 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7291 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007292 ret = io_submit_sqes(ctx, to_submit);
7293 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007294
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007295 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7296 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007297 if (creds)
7298 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007299 }
Jens Axboe90554202020-09-03 12:12:41 -06007300
Xiaoguang Wang08369242020-11-03 14:15:59 +08007301 return ret;
7302}
7303
Pavel Begunkovc0724812021-10-04 20:02:54 +01007304static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
Xiaoguang Wang08369242020-11-03 14:15:59 +08007305{
7306 struct io_ring_ctx *ctx;
7307 unsigned sq_thread_idle = 0;
7308
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007309 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7310 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007311 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007312}
7313
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007314static bool io_sqd_handle_event(struct io_sq_data *sqd)
7315{
7316 bool did_sig = false;
7317 struct ksignal ksig;
7318
7319 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7320 signal_pending(current)) {
7321 mutex_unlock(&sqd->lock);
7322 if (signal_pending(current))
7323 did_sig = get_signal(&ksig);
7324 cond_resched();
7325 mutex_lock(&sqd->lock);
7326 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007327 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7328}
7329
Jens Axboe6c271ce2019-01-10 11:22:30 -07007330static int io_sq_thread(void *data)
7331{
Jens Axboe69fb2132020-09-14 11:16:23 -06007332 struct io_sq_data *sqd = data;
7333 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007334 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007335 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007336 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007337
Pavel Begunkov696ee882021-04-01 09:55:04 +01007338 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007339 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007340
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007341 if (sqd->sq_cpu != -1)
7342 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7343 else
7344 set_cpus_allowed_ptr(current, cpu_online_mask);
7345 current->flags |= PF_NO_SETAFFINITY;
7346
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007347 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007348 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007349 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007350
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007351 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7352 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007353 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007354 timeout = jiffies + sqd->sq_thread_idle;
7355 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007356
Jens Axboee95eee22020-09-08 09:11:32 -06007357 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007358 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007359 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007360
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007361 if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007362 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007363 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007364 if (io_run_task_work())
7365 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007366
Xiaoguang Wang08369242020-11-03 14:15:59 +08007367 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007368 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007369 if (sqt_spin)
7370 timeout = jiffies + sqd->sq_thread_idle;
7371 continue;
7372 }
7373
Xiaoguang Wang08369242020-11-03 14:15:59 +08007374 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007375 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007376 bool needs_sched = true;
7377
Hao Xu724cb4f2021-04-21 23:19:11 +08007378 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007379 io_ring_set_wakeup_flag(ctx);
7380
Hao Xu724cb4f2021-04-21 23:19:11 +08007381 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007382 !wq_list_empty(&ctx->iopoll_list)) {
Hao Xu724cb4f2021-04-21 23:19:11 +08007383 needs_sched = false;
7384 break;
7385 }
7386 if (io_sqring_entries(ctx)) {
7387 needs_sched = false;
7388 break;
7389 }
7390 }
7391
7392 if (needs_sched) {
7393 mutex_unlock(&sqd->lock);
7394 schedule();
7395 mutex_lock(&sqd->lock);
7396 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007397 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7398 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007399 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007400
7401 finish_wait(&sqd->wait, &wait);
7402 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007403 }
7404
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007405 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007406 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007407 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007408 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007409 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007410 mutex_unlock(&sqd->lock);
7411
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007412 complete(&sqd->exited);
7413 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007414}
7415
Jens Axboebda52162019-09-24 13:47:15 -06007416struct io_wait_queue {
7417 struct wait_queue_entry wq;
7418 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007419 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007420 unsigned nr_timeouts;
7421};
7422
Pavel Begunkov6c503152021-01-04 20:36:36 +00007423static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007424{
7425 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007426 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007427
7428 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007429 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007430 * started waiting. For timeouts, we always want to return to userspace,
7431 * regardless of event count.
7432 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007433 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007434}
7435
7436static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7437 int wake_flags, void *key)
7438{
7439 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7440 wq);
7441
Pavel Begunkov6c503152021-01-04 20:36:36 +00007442 /*
7443 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7444 * the task, and the next invocation will do it.
7445 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007446 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007447 return autoremove_wake_function(curr, mode, wake_flags, key);
7448 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007449}
7450
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007451static int io_run_task_work_sig(void)
7452{
7453 if (io_run_task_work())
7454 return 1;
7455 if (!signal_pending(current))
7456 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007457 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007458 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007459 return -EINTR;
7460}
7461
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007462/* when returns >0, the caller should retry */
7463static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7464 struct io_wait_queue *iowq,
7465 signed long *timeout)
7466{
7467 int ret;
7468
7469 /* make sure we run task_work before checking for signals */
7470 ret = io_run_task_work_sig();
7471 if (ret || io_should_wake(iowq))
7472 return ret;
7473 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007474 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007475 return 1;
7476
7477 *timeout = schedule_timeout(*timeout);
7478 return !*timeout ? -ETIME : 1;
7479}
7480
Jens Axboe2b188cc2019-01-07 10:46:33 -07007481/*
7482 * Wait until events become available, if we don't already have some. The
7483 * application must reap them itself, as they reside on the shared cq ring.
7484 */
7485static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007486 const sigset_t __user *sig, size_t sigsz,
7487 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007488{
Pavel Begunkov902910992021-08-09 09:07:32 -06007489 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007490 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007491 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7492 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007493
Jens Axboeb41e9852020-02-17 09:52:41 -07007494 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007495 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007496 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007497 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007498 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007499 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007500 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007501
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007502 if (uts) {
7503 struct timespec64 ts;
7504
7505 if (get_timespec64(&ts, uts))
7506 return -EFAULT;
7507 timeout = timespec64_to_jiffies(&ts);
7508 }
7509
Jens Axboe2b188cc2019-01-07 10:46:33 -07007510 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007511#ifdef CONFIG_COMPAT
7512 if (in_compat_syscall())
7513 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007514 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007515 else
7516#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007517 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007518
Jens Axboe2b188cc2019-01-07 10:46:33 -07007519 if (ret)
7520 return ret;
7521 }
7522
Pavel Begunkov902910992021-08-09 09:07:32 -06007523 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7524 iowq.wq.private = current;
7525 INIT_LIST_HEAD(&iowq.wq.entry);
7526 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007527 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007528 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007529
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007530 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007531 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007532 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007533 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007534 ret = -EBUSY;
7535 break;
7536 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007537 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007538 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007539 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007540 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007541 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007542 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007543
Jens Axboeb7db41c2020-07-04 08:55:50 -06007544 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007545
Hristo Venev75b28af2019-08-26 17:23:46 +00007546 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007547}
7548
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007549static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007550{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007551 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007552
7553 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007554 kfree(table[i]);
7555 kfree(table);
7556}
7557
Pavel Begunkovc0724812021-10-04 20:02:54 +01007558static __cold void **io_alloc_page_table(size_t size)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007559{
7560 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7561 size_t init_size = size;
7562 void **table;
7563
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007564 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007565 if (!table)
7566 return NULL;
7567
7568 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007569 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007570
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007571 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007572 if (!table[i]) {
7573 io_free_page_table(table, init_size);
7574 return NULL;
7575 }
7576 size -= this_size;
7577 }
7578 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007579}
7580
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007581static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7582{
7583 percpu_ref_exit(&ref_node->refs);
7584 kfree(ref_node);
7585}
7586
Pavel Begunkovc0724812021-10-04 20:02:54 +01007587static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007588{
7589 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7590 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7591 unsigned long flags;
7592 bool first_add = false;
7593
7594 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7595 node->done = true;
7596
7597 while (!list_empty(&ctx->rsrc_ref_list)) {
7598 node = list_first_entry(&ctx->rsrc_ref_list,
7599 struct io_rsrc_node, node);
7600 /* recycle ref nodes in order */
7601 if (!node->done)
7602 break;
7603 list_del(&node->node);
7604 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7605 }
7606 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7607
7608 if (first_add)
7609 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7610}
7611
7612static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7613{
7614 struct io_rsrc_node *ref_node;
7615
7616 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7617 if (!ref_node)
7618 return NULL;
7619
7620 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7621 0, GFP_KERNEL)) {
7622 kfree(ref_node);
7623 return NULL;
7624 }
7625 INIT_LIST_HEAD(&ref_node->node);
7626 INIT_LIST_HEAD(&ref_node->rsrc_list);
7627 ref_node->done = false;
7628 return ref_node;
7629}
7630
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007631static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7632 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007633{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007634 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7635 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007636
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007637 if (data_to_kill) {
7638 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007639
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007640 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007641 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007642 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007643 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007644
Pavel Begunkov3e942492021-04-11 01:46:34 +01007645 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007646 percpu_ref_kill(&rsrc_node->refs);
7647 ctx->rsrc_node = NULL;
7648 }
7649
7650 if (!ctx->rsrc_node) {
7651 ctx->rsrc_node = ctx->rsrc_backup_node;
7652 ctx->rsrc_backup_node = NULL;
7653 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007654}
7655
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007656static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007657{
7658 if (ctx->rsrc_backup_node)
7659 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007660 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007661 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7662}
7663
Pavel Begunkovc0724812021-10-04 20:02:54 +01007664static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
7665 struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007666{
7667 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007668
Pavel Begunkov215c3902021-04-01 15:43:48 +01007669 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007670 if (data->quiesce)
7671 return -ENXIO;
7672
7673 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007674 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007675 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007676 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007677 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007678 io_rsrc_node_switch(ctx, data);
7679
Pavel Begunkov3e942492021-04-11 01:46:34 +01007680 /* kill initial ref, already quiesced if zero */
7681 if (atomic_dec_and_test(&data->refs))
7682 break;
Jens Axboec018db42021-08-09 08:15:50 -06007683 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007684 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007685 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007686 if (!ret) {
7687 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007688 break;
Jens Axboec018db42021-08-09 08:15:50 -06007689 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007690
Pavel Begunkov3e942492021-04-11 01:46:34 +01007691 atomic_inc(&data->refs);
7692 /* wait for all works potentially completing data->done */
7693 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007694 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007695
Hao Xu8bad28d2021-02-19 17:19:36 +08007696 ret = io_run_task_work_sig();
7697 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007698 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007699 data->quiesce = false;
7700
Hao Xu8bad28d2021-02-19 17:19:36 +08007701 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007702}
7703
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007704static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7705{
7706 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7707 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7708
7709 return &data->tags[table_idx][off];
7710}
7711
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007712static void io_rsrc_data_free(struct io_rsrc_data *data)
7713{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007714 size_t size = data->nr * sizeof(data->tags[0][0]);
7715
7716 if (data->tags)
7717 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007718 kfree(data);
7719}
7720
Pavel Begunkovc0724812021-10-04 20:02:54 +01007721static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7722 u64 __user *utags, unsigned nr,
7723 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007724{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007725 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007726 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007727 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007728
7729 data = kzalloc(sizeof(*data), GFP_KERNEL);
7730 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007731 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007732 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007733 if (!data->tags) {
7734 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007735 return -ENOMEM;
7736 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007737
7738 data->nr = nr;
7739 data->ctx = ctx;
7740 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007741 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007742 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007743 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007744 u64 *tag_slot = io_get_tag_slot(data, i);
7745
7746 if (copy_from_user(tag_slot, &utags[i],
7747 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007748 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007749 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007750 }
7751
Pavel Begunkov3e942492021-04-11 01:46:34 +01007752 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007753 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007754 *pdata = data;
7755 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007756fail:
7757 io_rsrc_data_free(data);
7758 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007759}
7760
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007761static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7762{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007763 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7764 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007765 return !!table->files;
7766}
7767
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007768static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007769{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007770 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007771 table->files = NULL;
7772}
7773
Jens Axboe2b188cc2019-01-07 10:46:33 -07007774static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7775{
7776#if defined(CONFIG_UNIX)
7777 if (ctx->ring_sock) {
7778 struct sock *sock = ctx->ring_sock->sk;
7779 struct sk_buff *skb;
7780
7781 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7782 kfree_skb(skb);
7783 }
7784#else
7785 int i;
7786
7787 for (i = 0; i < ctx->nr_user_files; i++) {
7788 struct file *file;
7789
7790 file = io_file_from_index(ctx, i);
7791 if (file)
7792 fput(file);
7793 }
7794#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007795 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007796 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007797 ctx->file_data = NULL;
7798 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007799}
7800
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007801static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7802{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007803 int ret;
7804
Pavel Begunkov08480402021-04-13 02:58:38 +01007805 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007806 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007807 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7808 if (!ret)
7809 __io_sqe_files_unregister(ctx);
7810 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007811}
7812
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007813static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007814 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007815{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007816 WARN_ON_ONCE(sqd->thread == current);
7817
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007818 /*
7819 * Do the dance but not conditional clear_bit() because it'd race with
7820 * other threads incrementing park_pending and setting the bit.
7821 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007822 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007823 if (atomic_dec_return(&sqd->park_pending))
7824 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007825 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007826}
7827
Jens Axboe86e0d672021-03-05 08:44:39 -07007828static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007829 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007830{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007831 WARN_ON_ONCE(sqd->thread == current);
7832
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007833 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007834 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007835 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007836 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007837 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007838}
7839
7840static void io_sq_thread_stop(struct io_sq_data *sqd)
7841{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007842 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007843 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007844
Jens Axboe05962f92021-03-06 13:58:48 -07007845 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007846 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007847 if (sqd->thread)
7848 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007849 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007850 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007851}
7852
Jens Axboe534ca6d2020-09-02 13:52:19 -06007853static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007854{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007855 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007856 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7857
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007858 io_sq_thread_stop(sqd);
7859 kfree(sqd);
7860 }
7861}
7862
7863static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7864{
7865 struct io_sq_data *sqd = ctx->sq_data;
7866
7867 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007868 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007869 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007870 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007871 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007872
7873 io_put_sq_data(sqd);
7874 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007875 }
7876}
7877
Jens Axboeaa061652020-09-02 14:50:27 -06007878static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7879{
7880 struct io_ring_ctx *ctx_attach;
7881 struct io_sq_data *sqd;
7882 struct fd f;
7883
7884 f = fdget(p->wq_fd);
7885 if (!f.file)
7886 return ERR_PTR(-ENXIO);
7887 if (f.file->f_op != &io_uring_fops) {
7888 fdput(f);
7889 return ERR_PTR(-EINVAL);
7890 }
7891
7892 ctx_attach = f.file->private_data;
7893 sqd = ctx_attach->sq_data;
7894 if (!sqd) {
7895 fdput(f);
7896 return ERR_PTR(-EINVAL);
7897 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007898 if (sqd->task_tgid != current->tgid) {
7899 fdput(f);
7900 return ERR_PTR(-EPERM);
7901 }
Jens Axboeaa061652020-09-02 14:50:27 -06007902
7903 refcount_inc(&sqd->refs);
7904 fdput(f);
7905 return sqd;
7906}
7907
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007908static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7909 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007910{
7911 struct io_sq_data *sqd;
7912
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007913 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007914 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7915 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007916 if (!IS_ERR(sqd)) {
7917 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007918 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007919 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007920 /* fall through for EPERM case, setup new sqd/task */
7921 if (PTR_ERR(sqd) != -EPERM)
7922 return sqd;
7923 }
Jens Axboeaa061652020-09-02 14:50:27 -06007924
Jens Axboe534ca6d2020-09-02 13:52:19 -06007925 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7926 if (!sqd)
7927 return ERR_PTR(-ENOMEM);
7928
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007929 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007930 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007931 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007932 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007933 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007934 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007935 return sqd;
7936}
7937
Jens Axboe6b063142019-01-10 22:13:58 -07007938#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007939/*
7940 * Ensure the UNIX gc is aware of our file set, so we are certain that
7941 * the io_uring can be safely unregistered on process exit, even if we have
7942 * loops in the file referencing.
7943 */
7944static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7945{
7946 struct sock *sk = ctx->ring_sock->sk;
7947 struct scm_fp_list *fpl;
7948 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007949 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007950
Jens Axboe6b063142019-01-10 22:13:58 -07007951 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7952 if (!fpl)
7953 return -ENOMEM;
7954
7955 skb = alloc_skb(0, GFP_KERNEL);
7956 if (!skb) {
7957 kfree(fpl);
7958 return -ENOMEM;
7959 }
7960
7961 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007962
Jens Axboe08a45172019-10-03 08:11:03 -06007963 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007964 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007965 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007966 struct file *file = io_file_from_index(ctx, i + offset);
7967
7968 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007969 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007970 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007971 unix_inflight(fpl->user, fpl->fp[nr_files]);
7972 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007973 }
7974
Jens Axboe08a45172019-10-03 08:11:03 -06007975 if (nr_files) {
7976 fpl->max = SCM_MAX_FD;
7977 fpl->count = nr_files;
7978 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007979 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007980 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7981 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007982
Jens Axboe08a45172019-10-03 08:11:03 -06007983 for (i = 0; i < nr_files; i++)
7984 fput(fpl->fp[i]);
7985 } else {
7986 kfree_skb(skb);
7987 kfree(fpl);
7988 }
Jens Axboe6b063142019-01-10 22:13:58 -07007989
7990 return 0;
7991}
7992
7993/*
7994 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7995 * causes regular reference counting to break down. We rely on the UNIX
7996 * garbage collection to take care of this problem for us.
7997 */
7998static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7999{
8000 unsigned left, total;
8001 int ret = 0;
8002
8003 total = 0;
8004 left = ctx->nr_user_files;
8005 while (left) {
8006 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008007
8008 ret = __io_sqe_files_scm(ctx, this_files, total);
8009 if (ret)
8010 break;
8011 left -= this_files;
8012 total += this_files;
8013 }
8014
8015 if (!ret)
8016 return 0;
8017
8018 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008019 struct file *file = io_file_from_index(ctx, total);
8020
8021 if (file)
8022 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008023 total++;
8024 }
8025
8026 return ret;
8027}
8028#else
8029static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8030{
8031 return 0;
8032}
8033#endif
8034
Pavel Begunkov47e90392021-04-01 15:43:56 +01008035static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008036{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008037 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008038#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008039 struct sock *sock = ctx->ring_sock->sk;
8040 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8041 struct sk_buff *skb;
8042 int i;
8043
8044 __skb_queue_head_init(&list);
8045
8046 /*
8047 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8048 * remove this entry and rearrange the file array.
8049 */
8050 skb = skb_dequeue(head);
8051 while (skb) {
8052 struct scm_fp_list *fp;
8053
8054 fp = UNIXCB(skb).fp;
8055 for (i = 0; i < fp->count; i++) {
8056 int left;
8057
8058 if (fp->fp[i] != file)
8059 continue;
8060
8061 unix_notinflight(fp->user, fp->fp[i]);
8062 left = fp->count - 1 - i;
8063 if (left) {
8064 memmove(&fp->fp[i], &fp->fp[i + 1],
8065 left * sizeof(struct file *));
8066 }
8067 fp->count--;
8068 if (!fp->count) {
8069 kfree_skb(skb);
8070 skb = NULL;
8071 } else {
8072 __skb_queue_tail(&list, skb);
8073 }
8074 fput(file);
8075 file = NULL;
8076 break;
8077 }
8078
8079 if (!file)
8080 break;
8081
8082 __skb_queue_tail(&list, skb);
8083
8084 skb = skb_dequeue(head);
8085 }
8086
8087 if (skb_peek(&list)) {
8088 spin_lock_irq(&head->lock);
8089 while ((skb = __skb_dequeue(&list)) != NULL)
8090 __skb_queue_tail(head, skb);
8091 spin_unlock_irq(&head->lock);
8092 }
8093#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008094 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008095#endif
8096}
8097
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008098static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008099{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008100 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008101 struct io_ring_ctx *ctx = rsrc_data->ctx;
8102 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008103
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008104 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8105 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008106
8107 if (prsrc->tag) {
8108 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008109
8110 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008111 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008112 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01008113 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008114 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008115 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008116 io_cqring_ev_posted(ctx);
8117 io_ring_submit_unlock(ctx, lock_ring);
8118 }
8119
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008120 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008121 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008122 }
8123
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008124 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008125 if (atomic_dec_and_test(&rsrc_data->refs))
8126 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008127}
8128
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008129static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008130{
8131 struct io_ring_ctx *ctx;
8132 struct llist_node *node;
8133
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008134 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8135 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008136
8137 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008138 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008139 struct llist_node *next = node->next;
8140
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008141 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008142 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008143 node = next;
8144 }
8145}
8146
Jens Axboe05f3fb32019-12-09 11:22:50 -07008147static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008148 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008149{
8150 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008151 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008152 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008153 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008154
8155 if (ctx->file_data)
8156 return -EBUSY;
8157 if (!nr_args)
8158 return -EINVAL;
8159 if (nr_args > IORING_MAX_FIXED_FILES)
8160 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008161 if (nr_args > rlimit(RLIMIT_NOFILE))
8162 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008163 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008164 if (ret)
8165 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008166 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8167 &ctx->file_data);
8168 if (ret)
8169 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008170
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008171 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008172 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008173 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008174
Jens Axboe05f3fb32019-12-09 11:22:50 -07008175 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008176 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008177 ret = -EFAULT;
8178 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008179 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008180 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008181 if (fd == -1) {
8182 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008183 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008184 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008185 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008186 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008187
Jens Axboe05f3fb32019-12-09 11:22:50 -07008188 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008189 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008190 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008191 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008192
8193 /*
8194 * Don't allow io_uring instances to be registered. If UNIX
8195 * isn't enabled, then this causes a reference cycle and this
8196 * instance can never get freed. If UNIX is enabled we'll
8197 * handle it just fine, but there's still no point in allowing
8198 * a ring fd as it doesn't support regular read/write anyway.
8199 */
8200 if (file->f_op == &io_uring_fops) {
8201 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008202 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008203 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008204 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008205 }
8206
Jens Axboe05f3fb32019-12-09 11:22:50 -07008207 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008208 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008209 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008210 return ret;
8211 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008212
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008213 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008214 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008215out_fput:
8216 for (i = 0; i < ctx->nr_user_files; i++) {
8217 file = io_file_from_index(ctx, i);
8218 if (file)
8219 fput(file);
8220 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008221 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008222 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008223out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008224 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008225 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008226 return ret;
8227}
8228
Jens Axboec3a31e62019-10-03 13:59:56 -06008229static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8230 int index)
8231{
8232#if defined(CONFIG_UNIX)
8233 struct sock *sock = ctx->ring_sock->sk;
8234 struct sk_buff_head *head = &sock->sk_receive_queue;
8235 struct sk_buff *skb;
8236
8237 /*
8238 * See if we can merge this file into an existing skb SCM_RIGHTS
8239 * file set. If there's no room, fall back to allocating a new skb
8240 * and filling it in.
8241 */
8242 spin_lock_irq(&head->lock);
8243 skb = skb_peek(head);
8244 if (skb) {
8245 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8246
8247 if (fpl->count < SCM_MAX_FD) {
8248 __skb_unlink(skb, head);
8249 spin_unlock_irq(&head->lock);
8250 fpl->fp[fpl->count] = get_file(file);
8251 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8252 fpl->count++;
8253 spin_lock_irq(&head->lock);
8254 __skb_queue_head(head, skb);
8255 } else {
8256 skb = NULL;
8257 }
8258 }
8259 spin_unlock_irq(&head->lock);
8260
8261 if (skb) {
8262 fput(file);
8263 return 0;
8264 }
8265
8266 return __io_sqe_files_scm(ctx, 1, index);
8267#else
8268 return 0;
8269#endif
8270}
8271
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008272static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8273 struct io_rsrc_node *node, void *rsrc)
8274{
8275 struct io_rsrc_put *prsrc;
8276
8277 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8278 if (!prsrc)
8279 return -ENOMEM;
8280
8281 prsrc->tag = *io_get_tag_slot(data, idx);
8282 prsrc->rsrc = rsrc;
8283 list_add(&prsrc->list, &node->rsrc_list);
8284 return 0;
8285}
8286
Pavel Begunkovb9445592021-08-25 12:25:45 +01008287static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8288 unsigned int issue_flags, u32 slot_index)
8289{
8290 struct io_ring_ctx *ctx = req->ctx;
8291 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008292 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008293 struct io_fixed_file *file_slot;
8294 int ret = -EBADF;
8295
8296 io_ring_submit_lock(ctx, !force_nonblock);
8297 if (file->f_op == &io_uring_fops)
8298 goto err;
8299 ret = -ENXIO;
8300 if (!ctx->file_data)
8301 goto err;
8302 ret = -EINVAL;
8303 if (slot_index >= ctx->nr_user_files)
8304 goto err;
8305
8306 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8307 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008308
8309 if (file_slot->file_ptr) {
8310 struct file *old_file;
8311
8312 ret = io_rsrc_node_switch_start(ctx);
8313 if (ret)
8314 goto err;
8315
8316 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8317 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8318 ctx->rsrc_node, old_file);
8319 if (ret)
8320 goto err;
8321 file_slot->file_ptr = 0;
8322 needs_switch = true;
8323 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008324
8325 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8326 io_fixed_file_set(file_slot, file);
8327 ret = io_sqe_file_register(ctx, file, slot_index);
8328 if (ret) {
8329 file_slot->file_ptr = 0;
8330 goto err;
8331 }
8332
8333 ret = 0;
8334err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008335 if (needs_switch)
8336 io_rsrc_node_switch(ctx, ctx->file_data);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008337 io_ring_submit_unlock(ctx, !force_nonblock);
8338 if (ret)
8339 fput(file);
8340 return ret;
8341}
8342
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008343static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8344{
8345 unsigned int offset = req->close.file_slot - 1;
8346 struct io_ring_ctx *ctx = req->ctx;
8347 struct io_fixed_file *file_slot;
8348 struct file *file;
8349 int ret, i;
8350
8351 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
8352 ret = -ENXIO;
8353 if (unlikely(!ctx->file_data))
8354 goto out;
8355 ret = -EINVAL;
8356 if (offset >= ctx->nr_user_files)
8357 goto out;
8358 ret = io_rsrc_node_switch_start(ctx);
8359 if (ret)
8360 goto out;
8361
8362 i = array_index_nospec(offset, ctx->nr_user_files);
8363 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8364 ret = -EBADF;
8365 if (!file_slot->file_ptr)
8366 goto out;
8367
8368 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8369 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8370 if (ret)
8371 goto out;
8372
8373 file_slot->file_ptr = 0;
8374 io_rsrc_node_switch(ctx, ctx->file_data);
8375 ret = 0;
8376out:
8377 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
8378 return ret;
8379}
8380
Jens Axboe05f3fb32019-12-09 11:22:50 -07008381static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008382 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008383 unsigned nr_args)
8384{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008385 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008386 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008387 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008388 struct io_fixed_file *file_slot;
8389 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008390 int fd, i, err = 0;
8391 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008392 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008393
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008394 if (!ctx->file_data)
8395 return -ENXIO;
8396 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008397 return -EINVAL;
8398
Pavel Begunkov67973b92021-01-26 13:51:09 +00008399 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008400 u64 tag = 0;
8401
8402 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8403 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008404 err = -EFAULT;
8405 break;
8406 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008407 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8408 err = -EINVAL;
8409 break;
8410 }
noah4e0377a2021-01-26 15:23:28 -05008411 if (fd == IORING_REGISTER_FILES_SKIP)
8412 continue;
8413
Pavel Begunkov67973b92021-01-26 13:51:09 +00008414 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008415 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008416
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008417 if (file_slot->file_ptr) {
8418 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008419 err = io_queue_rsrc_removal(data, up->offset + done,
8420 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008421 if (err)
8422 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008423 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008424 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008425 }
8426 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008427 file = fget(fd);
8428 if (!file) {
8429 err = -EBADF;
8430 break;
8431 }
8432 /*
8433 * Don't allow io_uring instances to be registered. If
8434 * UNIX isn't enabled, then this causes a reference
8435 * cycle and this instance can never get freed. If UNIX
8436 * is enabled we'll handle it just fine, but there's
8437 * still no point in allowing a ring fd as it doesn't
8438 * support regular read/write anyway.
8439 */
8440 if (file->f_op == &io_uring_fops) {
8441 fput(file);
8442 err = -EBADF;
8443 break;
8444 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008445 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008446 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008447 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008448 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008449 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008450 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008451 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008452 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008453 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008454 }
8455
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008456 if (needs_switch)
8457 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008458 return done ? done : err;
8459}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008460
Jens Axboe685fe7f2021-03-08 09:37:51 -07008461static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8462 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008463{
Jens Axboee9418942021-02-19 12:33:30 -07008464 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008465 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008466 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008467
Yang Yingliang362a9e62021-07-20 16:38:05 +08008468 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008469 hash = ctx->hash_map;
8470 if (!hash) {
8471 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008472 if (!hash) {
8473 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008474 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008475 }
Jens Axboee9418942021-02-19 12:33:30 -07008476 refcount_set(&hash->refs, 1);
8477 init_waitqueue_head(&hash->wait);
8478 ctx->hash_map = hash;
8479 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008480 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008481
8482 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008483 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008484 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008485 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008486
Jens Axboed25e3a32021-02-16 11:41:41 -07008487 /* Do QD, or 4 * CPUS, whatever is smallest */
8488 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008489
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008490 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008491}
8492
Pavel Begunkovc0724812021-10-04 20:02:54 +01008493static __cold int io_uring_alloc_task_context(struct task_struct *task,
8494 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008495{
8496 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008497 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008498
Pavel Begunkov09899b12021-06-14 02:36:22 +01008499 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008500 if (unlikely(!tctx))
8501 return -ENOMEM;
8502
Jens Axboed8a6df12020-10-15 16:24:45 -06008503 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8504 if (unlikely(ret)) {
8505 kfree(tctx);
8506 return ret;
8507 }
8508
Jens Axboe685fe7f2021-03-08 09:37:51 -07008509 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008510 if (IS_ERR(tctx->io_wq)) {
8511 ret = PTR_ERR(tctx->io_wq);
8512 percpu_counter_destroy(&tctx->inflight);
8513 kfree(tctx);
8514 return ret;
8515 }
8516
Jens Axboe0f212202020-09-13 13:09:39 -06008517 xa_init(&tctx->xa);
8518 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008519 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008520 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008521 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008522 spin_lock_init(&tctx->task_lock);
8523 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008524 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008525 return 0;
8526}
8527
8528void __io_uring_free(struct task_struct *tsk)
8529{
8530 struct io_uring_task *tctx = tsk->io_uring;
8531
8532 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008533 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008534 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008535
Jens Axboed8a6df12020-10-15 16:24:45 -06008536 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008537 kfree(tctx);
8538 tsk->io_uring = NULL;
8539}
8540
Pavel Begunkovc0724812021-10-04 20:02:54 +01008541static __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
8542 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008543{
8544 int ret;
8545
Jens Axboed25e3a32021-02-16 11:41:41 -07008546 /* Retain compatibility with failing for an invalid attach attempt */
8547 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8548 IORING_SETUP_ATTACH_WQ) {
8549 struct fd f;
8550
8551 f = fdget(p->wq_fd);
8552 if (!f.file)
8553 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008554 if (f.file->f_op != &io_uring_fops) {
8555 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008556 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008557 }
8558 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008559 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008560 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008561 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008562 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008563 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008564
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008565 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008566 if (IS_ERR(sqd)) {
8567 ret = PTR_ERR(sqd);
8568 goto err;
8569 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008570
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008571 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008572 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008573 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8574 if (!ctx->sq_thread_idle)
8575 ctx->sq_thread_idle = HZ;
8576
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008577 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008578 list_add(&ctx->sqd_list, &sqd->ctx_list);
8579 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008580 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008581 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008582 io_sq_thread_unpark(sqd);
8583
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008584 if (ret < 0)
8585 goto err;
8586 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008587 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008588
Jens Axboe6c271ce2019-01-10 11:22:30 -07008589 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008590 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008591
Jens Axboe917257d2019-04-13 09:28:55 -06008592 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008593 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008594 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008595 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008596 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008597 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008598 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008599
8600 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008601 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008602 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8603 if (IS_ERR(tsk)) {
8604 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008605 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008606 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008607
Jens Axboe46fe18b2021-03-04 12:39:36 -07008608 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008609 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008610 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008611 if (ret)
8612 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008613 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8614 /* Can't have SQ_AFF without SQPOLL */
8615 ret = -EINVAL;
8616 goto err;
8617 }
8618
Jens Axboe2b188cc2019-01-07 10:46:33 -07008619 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008620err_sqpoll:
8621 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008622err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008623 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008624 return ret;
8625}
8626
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008627static inline void __io_unaccount_mem(struct user_struct *user,
8628 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008629{
8630 atomic_long_sub(nr_pages, &user->locked_vm);
8631}
8632
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008633static inline int __io_account_mem(struct user_struct *user,
8634 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008635{
8636 unsigned long page_limit, cur_pages, new_pages;
8637
8638 /* Don't allow more pages than we can safely lock */
8639 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8640
8641 do {
8642 cur_pages = atomic_long_read(&user->locked_vm);
8643 new_pages = cur_pages + nr_pages;
8644 if (new_pages > page_limit)
8645 return -ENOMEM;
8646 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8647 new_pages) != cur_pages);
8648
8649 return 0;
8650}
8651
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008652static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008653{
Jens Axboe62e398b2021-02-21 16:19:37 -07008654 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008655 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008656
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008657 if (ctx->mm_account)
8658 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008659}
8660
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008661static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008662{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008663 int ret;
8664
Jens Axboe62e398b2021-02-21 16:19:37 -07008665 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008666 ret = __io_account_mem(ctx->user, nr_pages);
8667 if (ret)
8668 return ret;
8669 }
8670
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008671 if (ctx->mm_account)
8672 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008673
8674 return 0;
8675}
8676
Jens Axboe2b188cc2019-01-07 10:46:33 -07008677static void io_mem_free(void *ptr)
8678{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008679 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008680
Mark Rutland52e04ef2019-04-30 17:30:21 +01008681 if (!ptr)
8682 return;
8683
8684 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008685 if (put_page_testzero(page))
8686 free_compound_page(page);
8687}
8688
8689static void *io_mem_alloc(size_t size)
8690{
8691 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008692 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008693
8694 return (void *) __get_free_pages(gfp_flags, get_order(size));
8695}
8696
Hristo Venev75b28af2019-08-26 17:23:46 +00008697static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8698 size_t *sq_offset)
8699{
8700 struct io_rings *rings;
8701 size_t off, sq_array_size;
8702
8703 off = struct_size(rings, cqes, cq_entries);
8704 if (off == SIZE_MAX)
8705 return SIZE_MAX;
8706
8707#ifdef CONFIG_SMP
8708 off = ALIGN(off, SMP_CACHE_BYTES);
8709 if (off == 0)
8710 return SIZE_MAX;
8711#endif
8712
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008713 if (sq_offset)
8714 *sq_offset = off;
8715
Hristo Venev75b28af2019-08-26 17:23:46 +00008716 sq_array_size = array_size(sizeof(u32), sq_entries);
8717 if (sq_array_size == SIZE_MAX)
8718 return SIZE_MAX;
8719
8720 if (check_add_overflow(off, sq_array_size, &off))
8721 return SIZE_MAX;
8722
Hristo Venev75b28af2019-08-26 17:23:46 +00008723 return off;
8724}
8725
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008726static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008727{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008728 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008729 unsigned int i;
8730
Pavel Begunkov62248432021-04-28 13:11:29 +01008731 if (imu != ctx->dummy_ubuf) {
8732 for (i = 0; i < imu->nr_bvecs; i++)
8733 unpin_user_page(imu->bvec[i].bv_page);
8734 if (imu->acct_pages)
8735 io_unaccount_mem(ctx, imu->acct_pages);
8736 kvfree(imu);
8737 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008738 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008739}
8740
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008741static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8742{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008743 io_buffer_unmap(ctx, &prsrc->buf);
8744 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008745}
8746
8747static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008748{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008749 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008750
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008751 for (i = 0; i < ctx->nr_user_bufs; i++)
8752 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008753 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008754 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008755 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008756 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008757 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008758}
8759
Jens Axboeedafcce2019-01-09 09:16:05 -07008760static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8761{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008762 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008763
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008764 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008765 return -ENXIO;
8766
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008767 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8768 if (!ret)
8769 __io_sqe_buffers_unregister(ctx);
8770 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008771}
8772
8773static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8774 void __user *arg, unsigned index)
8775{
8776 struct iovec __user *src;
8777
8778#ifdef CONFIG_COMPAT
8779 if (ctx->compat) {
8780 struct compat_iovec __user *ciovs;
8781 struct compat_iovec ciov;
8782
8783 ciovs = (struct compat_iovec __user *) arg;
8784 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8785 return -EFAULT;
8786
Jens Axboed55e5f52019-12-11 16:12:15 -07008787 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008788 dst->iov_len = ciov.iov_len;
8789 return 0;
8790 }
8791#endif
8792 src = (struct iovec __user *) arg;
8793 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8794 return -EFAULT;
8795 return 0;
8796}
8797
Jens Axboede293932020-09-17 16:19:16 -06008798/*
8799 * Not super efficient, but this is just a registration time. And we do cache
8800 * the last compound head, so generally we'll only do a full search if we don't
8801 * match that one.
8802 *
8803 * We check if the given compound head page has already been accounted, to
8804 * avoid double accounting it. This allows us to account the full size of the
8805 * page, not just the constituent pages of a huge page.
8806 */
8807static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8808 int nr_pages, struct page *hpage)
8809{
8810 int i, j;
8811
8812 /* check current page array */
8813 for (i = 0; i < nr_pages; i++) {
8814 if (!PageCompound(pages[i]))
8815 continue;
8816 if (compound_head(pages[i]) == hpage)
8817 return true;
8818 }
8819
8820 /* check previously registered pages */
8821 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008822 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008823
8824 for (j = 0; j < imu->nr_bvecs; j++) {
8825 if (!PageCompound(imu->bvec[j].bv_page))
8826 continue;
8827 if (compound_head(imu->bvec[j].bv_page) == hpage)
8828 return true;
8829 }
8830 }
8831
8832 return false;
8833}
8834
8835static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8836 int nr_pages, struct io_mapped_ubuf *imu,
8837 struct page **last_hpage)
8838{
8839 int i, ret;
8840
Pavel Begunkov216e5832021-05-29 12:01:02 +01008841 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008842 for (i = 0; i < nr_pages; i++) {
8843 if (!PageCompound(pages[i])) {
8844 imu->acct_pages++;
8845 } else {
8846 struct page *hpage;
8847
8848 hpage = compound_head(pages[i]);
8849 if (hpage == *last_hpage)
8850 continue;
8851 *last_hpage = hpage;
8852 if (headpage_already_acct(ctx, pages, i, hpage))
8853 continue;
8854 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8855 }
8856 }
8857
8858 if (!imu->acct_pages)
8859 return 0;
8860
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008861 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008862 if (ret)
8863 imu->acct_pages = 0;
8864 return ret;
8865}
8866
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008867static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008868 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008869 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008870{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008871 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008872 struct vm_area_struct **vmas = NULL;
8873 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008874 unsigned long off, start, end, ubuf;
8875 size_t size;
8876 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008877
Pavel Begunkov62248432021-04-28 13:11:29 +01008878 if (!iov->iov_base) {
8879 *pimu = ctx->dummy_ubuf;
8880 return 0;
8881 }
8882
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008883 ubuf = (unsigned long) iov->iov_base;
8884 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8885 start = ubuf >> PAGE_SHIFT;
8886 nr_pages = end - start;
8887
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008888 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008889 ret = -ENOMEM;
8890
8891 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8892 if (!pages)
8893 goto done;
8894
8895 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8896 GFP_KERNEL);
8897 if (!vmas)
8898 goto done;
8899
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008900 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008901 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008902 goto done;
8903
8904 ret = 0;
8905 mmap_read_lock(current->mm);
8906 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8907 pages, vmas);
8908 if (pret == nr_pages) {
8909 /* don't support file backed memory */
8910 for (i = 0; i < nr_pages; i++) {
8911 struct vm_area_struct *vma = vmas[i];
8912
Pavel Begunkov40dad762021-06-09 15:26:54 +01008913 if (vma_is_shmem(vma))
8914 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008915 if (vma->vm_file &&
8916 !is_file_hugepages(vma->vm_file)) {
8917 ret = -EOPNOTSUPP;
8918 break;
8919 }
8920 }
8921 } else {
8922 ret = pret < 0 ? pret : -EFAULT;
8923 }
8924 mmap_read_unlock(current->mm);
8925 if (ret) {
8926 /*
8927 * if we did partial map, or found file backed vmas,
8928 * release any pages we did get
8929 */
8930 if (pret > 0)
8931 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008932 goto done;
8933 }
8934
8935 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8936 if (ret) {
8937 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008938 goto done;
8939 }
8940
8941 off = ubuf & ~PAGE_MASK;
8942 size = iov->iov_len;
8943 for (i = 0; i < nr_pages; i++) {
8944 size_t vec_len;
8945
8946 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8947 imu->bvec[i].bv_page = pages[i];
8948 imu->bvec[i].bv_len = vec_len;
8949 imu->bvec[i].bv_offset = off;
8950 off = 0;
8951 size -= vec_len;
8952 }
8953 /* store original address for later verification */
8954 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008955 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008956 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008957 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008958 ret = 0;
8959done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008960 if (ret)
8961 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008962 kvfree(pages);
8963 kvfree(vmas);
8964 return ret;
8965}
8966
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008967static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008968{
Pavel Begunkov87094462021-04-11 01:46:36 +01008969 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8970 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008971}
8972
8973static int io_buffer_validate(struct iovec *iov)
8974{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008975 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8976
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008977 /*
8978 * Don't impose further limits on the size and buffer
8979 * constraints here, we'll -EINVAL later when IO is
8980 * submitted if they are wrong.
8981 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008982 if (!iov->iov_base)
8983 return iov->iov_len ? -EFAULT : 0;
8984 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008985 return -EFAULT;
8986
8987 /* arbitrary limit, but we need something */
8988 if (iov->iov_len > SZ_1G)
8989 return -EFAULT;
8990
Pavel Begunkov50e96982021-03-24 22:59:01 +00008991 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8992 return -EOVERFLOW;
8993
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008994 return 0;
8995}
8996
8997static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008998 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008999{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009000 struct page *last_hpage = NULL;
9001 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009002 int i, ret;
9003 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009004
Pavel Begunkov87094462021-04-11 01:46:36 +01009005 if (ctx->user_bufs)
9006 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009007 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009008 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009009 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009010 if (ret)
9011 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009012 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9013 if (ret)
9014 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009015 ret = io_buffers_map_alloc(ctx, nr_args);
9016 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009017 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009018 return ret;
9019 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009020
Pavel Begunkov87094462021-04-11 01:46:36 +01009021 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009022 ret = io_copy_iov(ctx, &iov, arg, i);
9023 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009024 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009025 ret = io_buffer_validate(&iov);
9026 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009027 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009028 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009029 ret = -EINVAL;
9030 break;
9031 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009032
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009033 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9034 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009035 if (ret)
9036 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009037 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009038
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009039 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009040
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009041 ctx->buf_data = data;
9042 if (ret)
9043 __io_sqe_buffers_unregister(ctx);
9044 else
9045 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009046 return ret;
9047}
9048
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009049static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9050 struct io_uring_rsrc_update2 *up,
9051 unsigned int nr_args)
9052{
9053 u64 __user *tags = u64_to_user_ptr(up->tags);
9054 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009055 struct page *last_hpage = NULL;
9056 bool needs_switch = false;
9057 __u32 done;
9058 int i, err;
9059
9060 if (!ctx->buf_data)
9061 return -ENXIO;
9062 if (up->offset + nr_args > ctx->nr_user_bufs)
9063 return -EINVAL;
9064
9065 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009066 struct io_mapped_ubuf *imu;
9067 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009068 u64 tag = 0;
9069
9070 err = io_copy_iov(ctx, &iov, iovs, done);
9071 if (err)
9072 break;
9073 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9074 err = -EFAULT;
9075 break;
9076 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009077 err = io_buffer_validate(&iov);
9078 if (err)
9079 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009080 if (!iov.iov_base && tag) {
9081 err = -EINVAL;
9082 break;
9083 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009084 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9085 if (err)
9086 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009087
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009088 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009089 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009090 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9091 ctx->rsrc_node, ctx->user_bufs[i]);
9092 if (unlikely(err)) {
9093 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009094 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009095 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009096 ctx->user_bufs[i] = NULL;
9097 needs_switch = true;
9098 }
9099
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009100 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009101 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009102 }
9103
9104 if (needs_switch)
9105 io_rsrc_node_switch(ctx, ctx->buf_data);
9106 return done ? done : err;
9107}
9108
Jens Axboe9b402842019-04-11 11:45:41 -06009109static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9110{
9111 __s32 __user *fds = arg;
9112 int fd;
9113
9114 if (ctx->cq_ev_fd)
9115 return -EBUSY;
9116
9117 if (copy_from_user(&fd, fds, sizeof(*fds)))
9118 return -EFAULT;
9119
9120 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9121 if (IS_ERR(ctx->cq_ev_fd)) {
9122 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009123
Jens Axboe9b402842019-04-11 11:45:41 -06009124 ctx->cq_ev_fd = NULL;
9125 return ret;
9126 }
9127
9128 return 0;
9129}
9130
9131static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9132{
9133 if (ctx->cq_ev_fd) {
9134 eventfd_ctx_put(ctx->cq_ev_fd);
9135 ctx->cq_ev_fd = NULL;
9136 return 0;
9137 }
9138
9139 return -ENXIO;
9140}
9141
Jens Axboe5a2e7452020-02-23 16:23:11 -07009142static void io_destroy_buffers(struct io_ring_ctx *ctx)
9143{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009144 struct io_buffer *buf;
9145 unsigned long index;
9146
Jens Axboe8bab4c02021-09-24 07:12:27 -06009147 xa_for_each(&ctx->io_buffers, index, buf) {
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009148 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009149 cond_resched();
9150 }
Jens Axboe5a2e7452020-02-23 16:23:11 -07009151}
9152
Jens Axboe4010fec2021-02-27 15:04:18 -07009153static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009154{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009155 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009156 int nr = 0;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009157
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009158 mutex_lock(&ctx->uring_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009159 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009160
9161 while (state->free_list.next) {
9162 struct io_wq_work_node *node;
9163 struct io_kiocb *req;
9164
9165 node = wq_stack_extract(&state->free_list);
9166 req = container_of(node, struct io_kiocb, comp_list);
9167 kmem_cache_free(req_cachep, req);
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009168 nr++;
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009169 }
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009170 if (nr)
9171 percpu_ref_put_many(&ctx->refs, nr);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009172 mutex_unlock(&ctx->uring_lock);
9173}
9174
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009175static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009176{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009177 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009178 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009179}
9180
Pavel Begunkovc0724812021-10-04 20:02:54 +01009181static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009182{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009183 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009184
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009185 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009186 mmdrop(ctx->mm_account);
9187 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009188 }
Jens Axboedef596e2019-01-09 08:59:42 -07009189
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009190 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9191 io_wait_rsrc_data(ctx->buf_data);
9192 io_wait_rsrc_data(ctx->file_data);
9193
Hao Xu8bad28d2021-02-19 17:19:36 +08009194 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009195 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009196 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009197 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009198 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009199 if (ctx->rings)
9200 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009201 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009202 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009203 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009204 if (ctx->sq_creds)
9205 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009206
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009207 /* there are no registered resources left, nobody uses it */
9208 if (ctx->rsrc_node)
9209 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009210 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009211 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009212 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov756ab7c2021-10-06 16:06:47 +01009213 flush_delayed_work(&ctx->fallback_work);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009214
9215 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9216 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009217
9218#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009219 if (ctx->ring_sock) {
9220 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009221 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009222 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009223#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009224 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009225
Hristo Venev75b28af2019-08-26 17:23:46 +00009226 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009227 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009228
9229 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009230 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009231 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009232 if (ctx->hash_map)
9233 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009234 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009235 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009236 kfree(ctx);
9237}
9238
9239static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9240{
9241 struct io_ring_ctx *ctx = file->private_data;
9242 __poll_t mask = 0;
9243
Pavel Begunkovd60aa652021-10-04 20:02:52 +01009244 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009245 /*
9246 * synchronizes with barrier from wq_has_sleeper call in
9247 * io_commit_cqring
9248 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009249 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009250 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009251 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009252
9253 /*
9254 * Don't flush cqring overflow list here, just do a simple check.
9255 * Otherwise there could possible be ABBA deadlock:
9256 * CPU0 CPU1
9257 * ---- ----
9258 * lock(&ctx->uring_lock);
9259 * lock(&ep->mtx);
9260 * lock(&ctx->uring_lock);
9261 * lock(&ep->mtx);
9262 *
9263 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9264 * pushs them to do the flush.
9265 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009266 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009267 mask |= EPOLLIN | EPOLLRDNORM;
9268
9269 return mask;
9270}
9271
Yejune Deng0bead8c2020-12-24 11:02:20 +08009272static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009273{
Jens Axboe4379bf82021-02-15 13:40:22 -07009274 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009275
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009276 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009277 if (creds) {
9278 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009279 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009280 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009281
9282 return -EINVAL;
9283}
9284
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009285struct io_tctx_exit {
9286 struct callback_head task_work;
9287 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009288 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009289};
9290
Pavel Begunkovc0724812021-10-04 20:02:54 +01009291static __cold void io_tctx_exit_cb(struct callback_head *cb)
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009292{
9293 struct io_uring_task *tctx = current->io_uring;
9294 struct io_tctx_exit *work;
9295
9296 work = container_of(cb, struct io_tctx_exit, task_work);
9297 /*
9298 * When @in_idle, we're in cancellation and it's racy to remove the
9299 * node. It'll be removed by the end of cancellation, just ignore it.
9300 */
9301 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009302 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009303 complete(&work->completion);
9304}
9305
Pavel Begunkovc0724812021-10-04 20:02:54 +01009306static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
Pavel Begunkov28090c12021-04-25 23:34:45 +01009307{
9308 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9309
9310 return req->ctx == data;
9311}
9312
Pavel Begunkovc0724812021-10-04 20:02:54 +01009313static __cold void io_ring_exit_work(struct work_struct *work)
Jens Axboe85faa7b2020-04-09 18:14:00 -06009314{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009315 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009316 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009317 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009318 struct io_tctx_exit exit;
9319 struct io_tctx_node *node;
9320 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009321
Jens Axboe56952e92020-06-17 15:00:04 -06009322 /*
9323 * If we're doing polled IO and end up having requests being
9324 * submitted async (out-of-line), then completions can come in while
9325 * we're waiting for refs to drop. We need to reap these manually,
9326 * as nobody else will be looking for them.
9327 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009328 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009329 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009330 if (ctx->sq_data) {
9331 struct io_sq_data *sqd = ctx->sq_data;
9332 struct task_struct *tsk;
9333
9334 io_sq_thread_park(sqd);
9335 tsk = sqd->thread;
9336 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9337 io_wq_cancel_cb(tsk->io_uring->io_wq,
9338 io_cancel_ctx_cb, ctx, true);
9339 io_sq_thread_unpark(sqd);
9340 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009341
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009342 io_req_caches_free(ctx);
9343
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009344 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9345 /* there is little hope left, don't run it too often */
9346 interval = HZ * 60;
9347 }
9348 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009349
Pavel Begunkov7f006512021-04-14 13:38:34 +01009350 init_completion(&exit.completion);
9351 init_task_work(&exit.task_work, io_tctx_exit_cb);
9352 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009353 /*
9354 * Some may use context even when all refs and requests have been put,
9355 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009356 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009357 * this lock/unlock section also waits them to finish.
9358 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009359 mutex_lock(&ctx->uring_lock);
9360 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009361 WARN_ON_ONCE(time_after(jiffies, timeout));
9362
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009363 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9364 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009365 /* don't spin on a single task if cancellation failed */
9366 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009367 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9368 if (WARN_ON_ONCE(ret))
9369 continue;
9370 wake_up_process(node->task);
9371
9372 mutex_unlock(&ctx->uring_lock);
9373 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009374 mutex_lock(&ctx->uring_lock);
9375 }
9376 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009377 spin_lock(&ctx->completion_lock);
9378 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009379
Jens Axboe85faa7b2020-04-09 18:14:00 -06009380 io_ring_ctx_free(ctx);
9381}
9382
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009383/* Returns true if we found and killed one or more timeouts */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009384static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx,
9385 struct task_struct *tsk, bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009386{
9387 struct io_kiocb *req, *tmp;
9388 int canceled = 0;
9389
Jens Axboe79ebeae2021-08-10 15:18:27 -06009390 spin_lock(&ctx->completion_lock);
9391 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009392 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009393 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009394 io_kill_timeout(req, -ECANCELED);
9395 canceled++;
9396 }
9397 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009398 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009399 if (canceled != 0)
9400 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009401 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009402 if (canceled != 0)
9403 io_cqring_ev_posted(ctx);
9404 return canceled != 0;
9405}
9406
Pavel Begunkovc0724812021-10-04 20:02:54 +01009407static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009408{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009409 unsigned long index;
9410 struct creds *creds;
9411
Jens Axboe2b188cc2019-01-07 10:46:33 -07009412 mutex_lock(&ctx->uring_lock);
9413 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009414 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009415 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009416 xa_for_each(&ctx->personalities, index, creds)
9417 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009418 mutex_unlock(&ctx->uring_lock);
9419
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009420 io_kill_timeouts(ctx, NULL, true);
9421 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009422
Jens Axboe15dff282019-11-13 09:09:23 -07009423 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009424 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009425
Jens Axboe85faa7b2020-04-09 18:14:00 -06009426 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009427 /*
9428 * Use system_unbound_wq to avoid spawning tons of event kworkers
9429 * if we're exiting a ton of rings at the same time. It just adds
9430 * noise and overhead, there's no discernable change in runtime
9431 * over using system_wq.
9432 */
9433 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009434}
9435
9436static int io_uring_release(struct inode *inode, struct file *file)
9437{
9438 struct io_ring_ctx *ctx = file->private_data;
9439
9440 file->private_data = NULL;
9441 io_ring_ctx_wait_and_kill(ctx);
9442 return 0;
9443}
9444
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009445struct io_task_cancel {
9446 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009447 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009448};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009449
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009450static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009451{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009452 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009453 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009454 bool ret;
9455
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009456 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009457 struct io_ring_ctx *ctx = req->ctx;
9458
9459 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009460 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009461 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009462 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009463 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009464 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009465 }
9466 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009467}
9468
Pavel Begunkovc0724812021-10-04 20:02:54 +01009469static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
9470 struct task_struct *task,
9471 bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009472{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009473 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009474 LIST_HEAD(list);
9475
Jens Axboe79ebeae2021-08-10 15:18:27 -06009476 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009477 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009478 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009479 list_cut_position(&list, &ctx->defer_list, &de->list);
9480 break;
9481 }
9482 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009483 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009484 if (list_empty(&list))
9485 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009486
9487 while (!list_empty(&list)) {
9488 de = list_first_entry(&list, struct io_defer_entry, list);
9489 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009490 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009491 kfree(de);
9492 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009493 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009494}
9495
Pavel Begunkovc0724812021-10-04 20:02:54 +01009496static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
Pavel Begunkov1b007642021-03-06 11:02:17 +00009497{
9498 struct io_tctx_node *node;
9499 enum io_wq_cancel cret;
9500 bool ret = false;
9501
9502 mutex_lock(&ctx->uring_lock);
9503 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9504 struct io_uring_task *tctx = node->task->io_uring;
9505
9506 /*
9507 * io_wq will stay alive while we hold uring_lock, because it's
9508 * killed after ctx nodes, which requires to take the lock.
9509 */
9510 if (!tctx || !tctx->io_wq)
9511 continue;
9512 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9513 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9514 }
9515 mutex_unlock(&ctx->uring_lock);
9516
9517 return ret;
9518}
9519
Pavel Begunkovc0724812021-10-04 20:02:54 +01009520static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9521 struct task_struct *task,
9522 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009523{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009524 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009525 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009526
9527 while (1) {
9528 enum io_wq_cancel cret;
9529 bool ret = false;
9530
Pavel Begunkov1b007642021-03-06 11:02:17 +00009531 if (!task) {
9532 ret |= io_uring_try_cancel_iowq(ctx);
9533 } else if (tctx && tctx->io_wq) {
9534 /*
9535 * Cancels requests of all rings, not only @ctx, but
9536 * it's fine as the task is in exit/exec.
9537 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009538 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009539 &cancel, true);
9540 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9541 }
9542
9543 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009544 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009545 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01009546 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009547 io_iopoll_try_reap_events(ctx);
9548 ret = true;
9549 }
9550 }
9551
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009552 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9553 ret |= io_poll_remove_all(ctx, task, cancel_all);
9554 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009555 if (task)
9556 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009557 if (!ret)
9558 break;
9559 cond_resched();
9560 }
9561}
9562
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009563static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009564{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009565 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009566 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009567 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009568
9569 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009570 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009571 if (unlikely(ret))
9572 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009573 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009574 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009575 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9576 node = kmalloc(sizeof(*node), GFP_KERNEL);
9577 if (!node)
9578 return -ENOMEM;
9579 node->ctx = ctx;
9580 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009581
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009582 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9583 node, GFP_KERNEL));
9584 if (ret) {
9585 kfree(node);
9586 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009587 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009588
9589 mutex_lock(&ctx->uring_lock);
9590 list_add(&node->ctx_node, &ctx->tctx_list);
9591 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009592 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009593 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009594 return 0;
9595}
9596
9597/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009598 * Note that this task has used io_uring. We use it for cancelation purposes.
9599 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009600static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009601{
9602 struct io_uring_task *tctx = current->io_uring;
9603
9604 if (likely(tctx && tctx->last == ctx))
9605 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009606 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009607}
9608
9609/*
Jens Axboe0f212202020-09-13 13:09:39 -06009610 * Remove this io_uring_file -> task mapping.
9611 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009612static __cold void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009613{
9614 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009615 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009616
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009617 if (!tctx)
9618 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009619 node = xa_erase(&tctx->xa, index);
9620 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009621 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009622
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009623 WARN_ON_ONCE(current != node->task);
9624 WARN_ON_ONCE(list_empty(&node->ctx_node));
9625
9626 mutex_lock(&node->ctx->uring_lock);
9627 list_del(&node->ctx_node);
9628 mutex_unlock(&node->ctx->uring_lock);
9629
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009630 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009631 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009632 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009633}
9634
Pavel Begunkovc0724812021-10-04 20:02:54 +01009635static __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009636{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009637 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009638 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009639 unsigned long index;
9640
Jens Axboe8bab4c02021-09-24 07:12:27 -06009641 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009642 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009643 cond_resched();
9644 }
Marco Elverb16ef422021-05-27 11:25:48 +02009645 if (wq) {
9646 /*
9647 * Must be after io_uring_del_task_file() (removes nodes under
9648 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9649 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009650 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009651 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009652 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009653}
9654
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009655static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009656{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009657 if (tracked)
9658 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009659 return percpu_counter_sum(&tctx->inflight);
9660}
9661
Pavel Begunkovc0724812021-10-04 20:02:54 +01009662static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
Pavel Begunkov09899b12021-06-14 02:36:22 +01009663{
9664 struct io_uring_task *tctx = task->io_uring;
9665 unsigned int refs = tctx->cached_refs;
9666
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009667 if (refs) {
9668 tctx->cached_refs = 0;
9669 percpu_counter_sub(&tctx->inflight, refs);
9670 put_task_struct_many(task, refs);
9671 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009672}
9673
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009674/*
9675 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9676 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9677 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009678static __cold void io_uring_cancel_generic(bool cancel_all,
9679 struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009680{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009681 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009682 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009683 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009684 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009685
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009686 WARN_ON_ONCE(sqd && sqd->thread != current);
9687
Palash Oswal6d042ff2021-04-27 18:21:49 +05309688 if (!current->io_uring)
9689 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009690 if (tctx->io_wq)
9691 io_wq_exit_start(tctx->io_wq);
9692
Jens Axboefdaf0832020-10-30 09:37:30 -06009693 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009694 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009695 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009696 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009697 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009698 if (!inflight)
9699 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009700
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009701 if (!sqd) {
9702 struct io_tctx_node *node;
9703 unsigned long index;
9704
9705 xa_for_each(&tctx->xa, index, node) {
9706 /* sqpoll task will cancel all its requests */
9707 if (node->ctx->sq_data)
9708 continue;
9709 io_uring_try_cancel_requests(node->ctx, current,
9710 cancel_all);
9711 }
9712 } else {
9713 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9714 io_uring_try_cancel_requests(ctx, current,
9715 cancel_all);
9716 }
9717
9718 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009719 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009720 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009721 * If we've seen completions, retry without waiting. This
9722 * avoids a race where a completion comes in before we did
9723 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009724 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009725 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009726 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009727 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009728 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009729 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009730
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009731 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009732 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009733 /* for exec all current's requests should be gone, kill tctx */
9734 __io_uring_free(current);
9735 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009736}
9737
Hao Xuf552a272021-08-12 12:14:35 +08009738void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009739{
Hao Xuf552a272021-08-12 12:14:35 +08009740 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009741}
9742
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009743static void *io_uring_validate_mmap_request(struct file *file,
9744 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009745{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009746 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009747 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009748 struct page *page;
9749 void *ptr;
9750
9751 switch (offset) {
9752 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009753 case IORING_OFF_CQ_RING:
9754 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009755 break;
9756 case IORING_OFF_SQES:
9757 ptr = ctx->sq_sqes;
9758 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009759 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009760 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009761 }
9762
9763 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009764 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009765 return ERR_PTR(-EINVAL);
9766
9767 return ptr;
9768}
9769
9770#ifdef CONFIG_MMU
9771
Pavel Begunkovc0724812021-10-04 20:02:54 +01009772static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009773{
9774 size_t sz = vma->vm_end - vma->vm_start;
9775 unsigned long pfn;
9776 void *ptr;
9777
9778 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9779 if (IS_ERR(ptr))
9780 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009781
9782 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9783 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9784}
9785
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009786#else /* !CONFIG_MMU */
9787
9788static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9789{
9790 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9791}
9792
9793static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9794{
9795 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9796}
9797
9798static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9799 unsigned long addr, unsigned long len,
9800 unsigned long pgoff, unsigned long flags)
9801{
9802 void *ptr;
9803
9804 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9805 if (IS_ERR(ptr))
9806 return PTR_ERR(ptr);
9807
9808 return (unsigned long) ptr;
9809}
9810
9811#endif /* !CONFIG_MMU */
9812
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009813static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009814{
9815 DEFINE_WAIT(wait);
9816
9817 do {
9818 if (!io_sqring_full(ctx))
9819 break;
Jens Axboe90554202020-09-03 12:12:41 -06009820 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9821
9822 if (!io_sqring_full(ctx))
9823 break;
Jens Axboe90554202020-09-03 12:12:41 -06009824 schedule();
9825 } while (!signal_pending(current));
9826
9827 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009828 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009829}
9830
Hao Xuc73ebb62020-11-03 10:54:37 +08009831static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9832 struct __kernel_timespec __user **ts,
9833 const sigset_t __user **sig)
9834{
9835 struct io_uring_getevents_arg arg;
9836
9837 /*
9838 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9839 * is just a pointer to the sigset_t.
9840 */
9841 if (!(flags & IORING_ENTER_EXT_ARG)) {
9842 *sig = (const sigset_t __user *) argp;
9843 *ts = NULL;
9844 return 0;
9845 }
9846
9847 /*
9848 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9849 * timespec and sigset_t pointers if good.
9850 */
9851 if (*argsz != sizeof(arg))
9852 return -EINVAL;
9853 if (copy_from_user(&arg, argp, sizeof(arg)))
9854 return -EFAULT;
9855 *sig = u64_to_user_ptr(arg.sigmask);
9856 *argsz = arg.sigmask_sz;
9857 *ts = u64_to_user_ptr(arg.ts);
9858 return 0;
9859}
9860
Jens Axboe2b188cc2019-01-07 10:46:33 -07009861SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009862 u32, min_complete, u32, flags, const void __user *, argp,
9863 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009864{
9865 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009866 int submitted = 0;
9867 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009868 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009869
Jens Axboe4c6e2772020-07-01 11:29:10 -06009870 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009871
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009872 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9873 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009874 return -EINVAL;
9875
9876 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009877 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009878 return -EBADF;
9879
9880 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009881 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009882 goto out_fput;
9883
9884 ret = -ENXIO;
9885 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009886 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009887 goto out_fput;
9888
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009889 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009890 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009891 goto out;
9892
Jens Axboe6c271ce2019-01-10 11:22:30 -07009893 /*
9894 * For SQ polling, the thread will do all submissions and completions.
9895 * Just return the requested submit count, and wake the thread if
9896 * we were asked to.
9897 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009898 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009899 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009900 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009901
Jens Axboe21f96522021-08-14 09:04:40 -06009902 if (unlikely(ctx->sq_data->thread == NULL)) {
9903 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009904 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009905 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009906 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009907 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009908 if (flags & IORING_ENTER_SQ_WAIT) {
9909 ret = io_sqpoll_wait_sq(ctx);
9910 if (ret)
9911 goto out;
9912 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009913 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009914 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009915 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009916 if (unlikely(ret))
9917 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009918 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009919 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009920 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009921
9922 if (submitted != to_submit)
9923 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009924 }
9925 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009926 const sigset_t __user *sig;
9927 struct __kernel_timespec __user *ts;
9928
9929 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9930 if (unlikely(ret))
9931 goto out;
9932
Jens Axboe2b188cc2019-01-07 10:46:33 -07009933 min_complete = min(min_complete, ctx->cq_entries);
9934
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009935 /*
9936 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9937 * space applications don't need to do io completion events
9938 * polling again, they can rely on io_sq_thread to do polling
9939 * work, which can reduce cpu usage and uring_lock contention.
9940 */
9941 if (ctx->flags & IORING_SETUP_IOPOLL &&
9942 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009943 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009944 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009945 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009946 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009947 }
9948
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009949out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009950 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009951out_fput:
9952 fdput(f);
9953 return submitted ? submitted : ret;
9954}
9955
Tobias Klauserbebdb652020-02-26 18:38:32 +01009956#ifdef CONFIG_PROC_FS
Pavel Begunkovc0724812021-10-04 20:02:54 +01009957static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009958 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009959{
Jens Axboe87ce9552020-01-30 08:25:34 -07009960 struct user_namespace *uns = seq_user_ns(m);
9961 struct group_info *gi;
9962 kernel_cap_t cap;
9963 unsigned __capi;
9964 int g;
9965
9966 seq_printf(m, "%5d\n", id);
9967 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9968 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9969 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9970 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9971 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9972 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9973 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9974 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9975 seq_puts(m, "\n\tGroups:\t");
9976 gi = cred->group_info;
9977 for (g = 0; g < gi->ngroups; g++) {
9978 seq_put_decimal_ull(m, g ? " " : "",
9979 from_kgid_munged(uns, gi->gid[g]));
9980 }
9981 seq_puts(m, "\n\tCapEff:\t");
9982 cap = cred->cap_effective;
9983 CAP_FOR_EACH_U32(__capi)
9984 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9985 seq_putc(m, '\n');
9986 return 0;
9987}
9988
Pavel Begunkovc0724812021-10-04 20:02:54 +01009989static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
9990 struct seq_file *m)
Jens Axboe87ce9552020-01-30 08:25:34 -07009991{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009992 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +08009993 struct io_overflow_cqe *ocqe;
9994 struct io_rings *r = ctx->rings;
9995 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
9996 unsigned int cached_sq_head = ctx->cached_sq_head;
9997 unsigned int cached_cq_tail = ctx->cached_cq_tail;
9998 unsigned int sq_head = READ_ONCE(r->sq.head);
9999 unsigned int sq_tail = READ_ONCE(r->sq.tail);
10000 unsigned int cq_head = READ_ONCE(r->cq.head);
10001 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010002 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010003 unsigned int i;
10004
10005 /*
10006 * we may get imprecise sqe and cqe info if uring is actively running
10007 * since we get cached_sq_head and cached_cq_tail without uring_lock
10008 * and sq_tail and cq_head are changed by userspace. But it's ok since
10009 * we usually use these info when it is stuck.
10010 */
10011 seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask);
10012 seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask);
10013 seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask);
10014 seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask);
10015 seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask);
10016 seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask);
10017 seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head);
10018 for (i = cached_sq_head; i < sq_tail; i++) {
10019 unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]);
10020
10021 if (likely(sq_idx <= sq_mask)) {
10022 struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx];
10023
10024 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10025 sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data);
10026 }
10027 }
10028 seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head);
10029 for (i = cq_head; i < cached_cq_tail; i++) {
10030 struct io_uring_cqe *cqe = &r->cqes[i & cq_mask];
10031
10032 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
10033 i & cq_mask, cqe->user_data, cqe->res, cqe->flags);
10034 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010035
Jens Axboefad8e0d2020-09-28 08:57:48 -060010036 /*
10037 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10038 * since fdinfo case grabs it in the opposite direction of normal use
10039 * cases. If we fail to get the lock, we just don't iterate any
10040 * structures that could be going away outside the io_uring mutex.
10041 */
10042 has_lock = mutex_trylock(&ctx->uring_lock);
10043
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010044 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010045 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010046 if (!sq->thread)
10047 sq = NULL;
10048 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010049
10050 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10051 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010052 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010053 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010054 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010055
Jens Axboe87ce9552020-01-30 08:25:34 -070010056 if (f)
10057 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10058 else
10059 seq_printf(m, "%5u: <none>\n", i);
10060 }
10061 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010062 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010063 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010064 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010065
Pavel Begunkov4751f532021-04-01 15:43:55 +010010066 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010067 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010068 if (has_lock && !xa_empty(&ctx->personalities)) {
10069 unsigned long index;
10070 const struct cred *cred;
10071
Jens Axboe87ce9552020-01-30 08:25:34 -070010072 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010073 xa_for_each(&ctx->personalities, index, cred)
10074 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010075 }
Hao Xu83f84352021-09-13 21:08:54 +080010076 if (has_lock)
10077 mutex_unlock(&ctx->uring_lock);
10078
10079 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010080 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010081 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10082 struct hlist_head *list = &ctx->cancel_hash[i];
10083 struct io_kiocb *req;
10084
10085 hlist_for_each_entry(req, list, hash_node)
10086 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10087 req->task->task_works != NULL);
10088 }
Hao Xu83f84352021-09-13 21:08:54 +080010089
10090 seq_puts(m, "CqOverflowList:\n");
10091 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10092 struct io_uring_cqe *cqe = &ocqe->cqe;
10093
10094 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10095 cqe->user_data, cqe->res, cqe->flags);
10096
10097 }
10098
Jens Axboe79ebeae2021-08-10 15:18:27 -060010099 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010100}
10101
Pavel Begunkovc0724812021-10-04 20:02:54 +010010102static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
Jens Axboe87ce9552020-01-30 08:25:34 -070010103{
10104 struct io_ring_ctx *ctx = f->private_data;
10105
10106 if (percpu_ref_tryget(&ctx->refs)) {
10107 __io_uring_show_fdinfo(ctx, m);
10108 percpu_ref_put(&ctx->refs);
10109 }
10110}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010111#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010112
Jens Axboe2b188cc2019-01-07 10:46:33 -070010113static const struct file_operations io_uring_fops = {
10114 .release = io_uring_release,
10115 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010116#ifndef CONFIG_MMU
10117 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10118 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10119#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010120 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010121#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010122 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010123#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010124};
10125
Pavel Begunkovc0724812021-10-04 20:02:54 +010010126static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10127 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010128{
Hristo Venev75b28af2019-08-26 17:23:46 +000010129 struct io_rings *rings;
10130 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010131
Jens Axboebd740482020-08-05 12:58:23 -060010132 /* make sure these are sane, as we already accounted them */
10133 ctx->sq_entries = p->sq_entries;
10134 ctx->cq_entries = p->cq_entries;
10135
Hristo Venev75b28af2019-08-26 17:23:46 +000010136 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10137 if (size == SIZE_MAX)
10138 return -EOVERFLOW;
10139
10140 rings = io_mem_alloc(size);
10141 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010142 return -ENOMEM;
10143
Hristo Venev75b28af2019-08-26 17:23:46 +000010144 ctx->rings = rings;
10145 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10146 rings->sq_ring_mask = p->sq_entries - 1;
10147 rings->cq_ring_mask = p->cq_entries - 1;
10148 rings->sq_ring_entries = p->sq_entries;
10149 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010150
10151 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010152 if (size == SIZE_MAX) {
10153 io_mem_free(ctx->rings);
10154 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010155 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010156 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010157
10158 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010159 if (!ctx->sq_sqes) {
10160 io_mem_free(ctx->rings);
10161 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010162 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010163 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010164
Jens Axboe2b188cc2019-01-07 10:46:33 -070010165 return 0;
10166}
10167
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010168static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10169{
10170 int ret, fd;
10171
10172 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10173 if (fd < 0)
10174 return fd;
10175
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010176 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010177 if (ret) {
10178 put_unused_fd(fd);
10179 return ret;
10180 }
10181 fd_install(fd, file);
10182 return fd;
10183}
10184
Jens Axboe2b188cc2019-01-07 10:46:33 -070010185/*
10186 * Allocate an anonymous fd, this is what constitutes the application
10187 * visible backing of an io_uring instance. The application mmaps this
10188 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10189 * we have to tie this fd to a socket for file garbage collection purposes.
10190 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010191static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010192{
10193 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010194#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010195 int ret;
10196
Jens Axboe2b188cc2019-01-07 10:46:33 -070010197 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10198 &ctx->ring_sock);
10199 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010200 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010201#endif
10202
Jens Axboe2b188cc2019-01-07 10:46:33 -070010203 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
10204 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010205#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010206 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010207 sock_release(ctx->ring_sock);
10208 ctx->ring_sock = NULL;
10209 } else {
10210 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010211 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010212#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010213 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010214}
10215
Pavel Begunkovc0724812021-10-04 20:02:54 +010010216static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
10217 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010218{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010219 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010220 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010221 int ret;
10222
Jens Axboe8110c1a2019-12-28 15:39:54 -070010223 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010224 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010225 if (entries > IORING_MAX_ENTRIES) {
10226 if (!(p->flags & IORING_SETUP_CLAMP))
10227 return -EINVAL;
10228 entries = IORING_MAX_ENTRIES;
10229 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010230
10231 /*
10232 * Use twice as many entries for the CQ ring. It's possible for the
10233 * application to drive a higher depth than the size of the SQ ring,
10234 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010235 * some flexibility in overcommitting a bit. If the application has
10236 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10237 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010238 */
10239 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010240 if (p->flags & IORING_SETUP_CQSIZE) {
10241 /*
10242 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10243 * to a power-of-two, if it isn't already. We do NOT impose
10244 * any cq vs sq ring sizing.
10245 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010246 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010247 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010248 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10249 if (!(p->flags & IORING_SETUP_CLAMP))
10250 return -EINVAL;
10251 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10252 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010253 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10254 if (p->cq_entries < p->sq_entries)
10255 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010256 } else {
10257 p->cq_entries = 2 * p->sq_entries;
10258 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010259
Jens Axboe2b188cc2019-01-07 10:46:33 -070010260 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010261 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010262 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010263 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010264 if (!capable(CAP_IPC_LOCK))
10265 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010266
10267 /*
10268 * This is just grabbed for accounting purposes. When a process exits,
10269 * the mm is exited and dropped before the files, hence we need to hang
10270 * on to this mm purely for the purposes of being able to unaccount
10271 * memory (locked/pinned vm). It's not used for anything else.
10272 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010273 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010274 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010275
Jens Axboe2b188cc2019-01-07 10:46:33 -070010276 ret = io_allocate_scq_urings(ctx, p);
10277 if (ret)
10278 goto err;
10279
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010280 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010281 if (ret)
10282 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010283 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010284 ret = io_rsrc_node_switch_start(ctx);
10285 if (ret)
10286 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010287 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010288
Jens Axboe2b188cc2019-01-07 10:46:33 -070010289 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010290 p->sq_off.head = offsetof(struct io_rings, sq.head);
10291 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10292 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10293 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10294 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10295 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10296 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010297
10298 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010299 p->cq_off.head = offsetof(struct io_rings, cq.head);
10300 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10301 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10302 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10303 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10304 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010305 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010306
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010307 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10308 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010309 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010310 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010311 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
10312 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010313
10314 if (copy_to_user(params, p, sizeof(*p))) {
10315 ret = -EFAULT;
10316 goto err;
10317 }
Jens Axboed1719f72020-07-30 13:43:53 -060010318
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010319 file = io_uring_get_file(ctx);
10320 if (IS_ERR(file)) {
10321 ret = PTR_ERR(file);
10322 goto err;
10323 }
10324
Jens Axboed1719f72020-07-30 13:43:53 -060010325 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010326 * Install ring fd as the very last thing, so we don't risk someone
10327 * having closed it before we finish setup
10328 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010329 ret = io_uring_install_fd(ctx, file);
10330 if (ret < 0) {
10331 /* fput will clean it up */
10332 fput(file);
10333 return ret;
10334 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010335
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010336 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010337 return ret;
10338err:
10339 io_ring_ctx_wait_and_kill(ctx);
10340 return ret;
10341}
10342
10343/*
10344 * Sets up an aio uring context, and returns the fd. Applications asks for a
10345 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10346 * params structure passed in.
10347 */
10348static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10349{
10350 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010351 int i;
10352
10353 if (copy_from_user(&p, params, sizeof(p)))
10354 return -EFAULT;
10355 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10356 if (p.resv[i])
10357 return -EINVAL;
10358 }
10359
Jens Axboe6c271ce2019-01-10 11:22:30 -070010360 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010361 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010362 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10363 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010364 return -EINVAL;
10365
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010366 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010367}
10368
10369SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10370 struct io_uring_params __user *, params)
10371{
10372 return io_uring_setup(entries, params);
10373}
10374
Pavel Begunkovc0724812021-10-04 20:02:54 +010010375static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
10376 unsigned nr_args)
Jens Axboe66f4af92020-01-16 15:36:52 -070010377{
10378 struct io_uring_probe *p;
10379 size_t size;
10380 int i, ret;
10381
10382 size = struct_size(p, ops, nr_args);
10383 if (size == SIZE_MAX)
10384 return -EOVERFLOW;
10385 p = kzalloc(size, GFP_KERNEL);
10386 if (!p)
10387 return -ENOMEM;
10388
10389 ret = -EFAULT;
10390 if (copy_from_user(p, arg, size))
10391 goto out;
10392 ret = -EINVAL;
10393 if (memchr_inv(p, 0, size))
10394 goto out;
10395
10396 p->last_op = IORING_OP_LAST - 1;
10397 if (nr_args > IORING_OP_LAST)
10398 nr_args = IORING_OP_LAST;
10399
10400 for (i = 0; i < nr_args; i++) {
10401 p->ops[i].op = i;
10402 if (!io_op_defs[i].not_supported)
10403 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10404 }
10405 p->ops_len = i;
10406
10407 ret = 0;
10408 if (copy_to_user(arg, p, size))
10409 ret = -EFAULT;
10410out:
10411 kfree(p);
10412 return ret;
10413}
10414
Jens Axboe071698e2020-01-28 10:04:42 -070010415static int io_register_personality(struct io_ring_ctx *ctx)
10416{
Jens Axboe4379bf82021-02-15 13:40:22 -070010417 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010418 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010419 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010420
Jens Axboe4379bf82021-02-15 13:40:22 -070010421 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010422
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010423 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10424 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010425 if (ret < 0) {
10426 put_cred(creds);
10427 return ret;
10428 }
10429 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010430}
10431
Pavel Begunkovc0724812021-10-04 20:02:54 +010010432static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
10433 void __user *arg, unsigned int nr_args)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010434{
10435 struct io_uring_restriction *res;
10436 size_t size;
10437 int i, ret;
10438
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010439 /* Restrictions allowed only if rings started disabled */
10440 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10441 return -EBADFD;
10442
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010443 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010444 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010445 return -EBUSY;
10446
10447 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10448 return -EINVAL;
10449
10450 size = array_size(nr_args, sizeof(*res));
10451 if (size == SIZE_MAX)
10452 return -EOVERFLOW;
10453
10454 res = memdup_user(arg, size);
10455 if (IS_ERR(res))
10456 return PTR_ERR(res);
10457
10458 ret = 0;
10459
10460 for (i = 0; i < nr_args; i++) {
10461 switch (res[i].opcode) {
10462 case IORING_RESTRICTION_REGISTER_OP:
10463 if (res[i].register_op >= IORING_REGISTER_LAST) {
10464 ret = -EINVAL;
10465 goto out;
10466 }
10467
10468 __set_bit(res[i].register_op,
10469 ctx->restrictions.register_op);
10470 break;
10471 case IORING_RESTRICTION_SQE_OP:
10472 if (res[i].sqe_op >= IORING_OP_LAST) {
10473 ret = -EINVAL;
10474 goto out;
10475 }
10476
10477 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10478 break;
10479 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10480 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10481 break;
10482 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10483 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10484 break;
10485 default:
10486 ret = -EINVAL;
10487 goto out;
10488 }
10489 }
10490
10491out:
10492 /* Reset all restrictions if an error happened */
10493 if (ret != 0)
10494 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10495 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010496 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010497
10498 kfree(res);
10499 return ret;
10500}
10501
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010502static int io_register_enable_rings(struct io_ring_ctx *ctx)
10503{
10504 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10505 return -EBADFD;
10506
10507 if (ctx->restrictions.registered)
10508 ctx->restricted = 1;
10509
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010510 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10511 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10512 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010513 return 0;
10514}
10515
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010516static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010517 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010518 unsigned nr_args)
10519{
10520 __u32 tmp;
10521 int err;
10522
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010523 if (up->resv)
10524 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010525 if (check_add_overflow(up->offset, nr_args, &tmp))
10526 return -EOVERFLOW;
10527 err = io_rsrc_node_switch_start(ctx);
10528 if (err)
10529 return err;
10530
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010531 switch (type) {
10532 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010533 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010534 case IORING_RSRC_BUFFER:
10535 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010536 }
10537 return -EINVAL;
10538}
10539
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010540static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10541 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010542{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010543 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010544
10545 if (!nr_args)
10546 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010547 memset(&up, 0, sizeof(up));
10548 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10549 return -EFAULT;
10550 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10551}
10552
10553static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010554 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010555{
10556 struct io_uring_rsrc_update2 up;
10557
10558 if (size != sizeof(up))
10559 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010560 if (copy_from_user(&up, arg, sizeof(up)))
10561 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010562 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010563 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010564 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010565}
10566
Pavel Begunkovc0724812021-10-04 20:02:54 +010010567static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010568 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010569{
10570 struct io_uring_rsrc_register rr;
10571
10572 /* keep it extendible */
10573 if (size != sizeof(rr))
10574 return -EINVAL;
10575
10576 memset(&rr, 0, sizeof(rr));
10577 if (copy_from_user(&rr, arg, size))
10578 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010579 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010580 return -EINVAL;
10581
Pavel Begunkov992da012021-06-10 16:37:37 +010010582 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010583 case IORING_RSRC_FILE:
10584 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10585 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010586 case IORING_RSRC_BUFFER:
10587 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10588 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010589 }
10590 return -EINVAL;
10591}
10592
Pavel Begunkovc0724812021-10-04 20:02:54 +010010593static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
10594 void __user *arg, unsigned len)
Jens Axboefe764212021-06-17 10:19:54 -060010595{
10596 struct io_uring_task *tctx = current->io_uring;
10597 cpumask_var_t new_mask;
10598 int ret;
10599
10600 if (!tctx || !tctx->io_wq)
10601 return -EINVAL;
10602
10603 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10604 return -ENOMEM;
10605
10606 cpumask_clear(new_mask);
10607 if (len > cpumask_size())
10608 len = cpumask_size();
10609
10610 if (copy_from_user(new_mask, arg, len)) {
10611 free_cpumask_var(new_mask);
10612 return -EFAULT;
10613 }
10614
10615 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10616 free_cpumask_var(new_mask);
10617 return ret;
10618}
10619
Pavel Begunkovc0724812021-10-04 20:02:54 +010010620static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
Jens Axboefe764212021-06-17 10:19:54 -060010621{
10622 struct io_uring_task *tctx = current->io_uring;
10623
10624 if (!tctx || !tctx->io_wq)
10625 return -EINVAL;
10626
10627 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10628}
10629
Pavel Begunkovc0724812021-10-04 20:02:54 +010010630static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10631 void __user *arg)
Jens Axboe2e480052021-08-27 11:33:19 -060010632{
Jens Axboefa846932021-09-01 14:15:59 -060010633 struct io_uring_task *tctx = NULL;
10634 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010635 __u32 new_count[2];
10636 int i, ret;
10637
Jens Axboe2e480052021-08-27 11:33:19 -060010638 if (copy_from_user(new_count, arg, sizeof(new_count)))
10639 return -EFAULT;
10640 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10641 if (new_count[i] > INT_MAX)
10642 return -EINVAL;
10643
Jens Axboefa846932021-09-01 14:15:59 -060010644 if (ctx->flags & IORING_SETUP_SQPOLL) {
10645 sqd = ctx->sq_data;
10646 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010647 /*
10648 * Observe the correct sqd->lock -> ctx->uring_lock
10649 * ordering. Fine to drop uring_lock here, we hold
10650 * a ref to the ctx.
10651 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010652 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010653 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010654 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010655 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010656 if (sqd->thread)
10657 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010658 }
10659 } else {
10660 tctx = current->io_uring;
10661 }
10662
10663 ret = -EINVAL;
10664 if (!tctx || !tctx->io_wq)
10665 goto err;
10666
Jens Axboe2e480052021-08-27 11:33:19 -060010667 ret = io_wq_max_workers(tctx->io_wq, new_count);
10668 if (ret)
Jens Axboefa846932021-09-01 14:15:59 -060010669 goto err;
10670
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010671 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010672 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010673 io_put_sq_data(sqd);
10674 }
Jens Axboe2e480052021-08-27 11:33:19 -060010675
10676 if (copy_to_user(arg, new_count, sizeof(new_count)))
10677 return -EFAULT;
10678
10679 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010680err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010681 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010682 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010683 io_put_sq_data(sqd);
10684 }
Jens Axboefa846932021-09-01 14:15:59 -060010685 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010686}
10687
Jens Axboe071698e2020-01-28 10:04:42 -070010688static bool io_register_op_must_quiesce(int op)
10689{
10690 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010691 case IORING_REGISTER_BUFFERS:
10692 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010693 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010694 case IORING_UNREGISTER_FILES:
10695 case IORING_REGISTER_FILES_UPDATE:
10696 case IORING_REGISTER_PROBE:
10697 case IORING_REGISTER_PERSONALITY:
10698 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010699 case IORING_REGISTER_FILES2:
10700 case IORING_REGISTER_FILES_UPDATE2:
10701 case IORING_REGISTER_BUFFERS2:
10702 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010703 case IORING_REGISTER_IOWQ_AFF:
10704 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010705 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010706 return false;
10707 default:
10708 return true;
10709 }
10710}
10711
Pavel Begunkovc0724812021-10-04 20:02:54 +010010712static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx)
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010713{
10714 long ret;
10715
10716 percpu_ref_kill(&ctx->refs);
10717
10718 /*
10719 * Drop uring mutex before waiting for references to exit. If another
10720 * thread is currently inside io_uring_enter() it might need to grab the
10721 * uring_lock to make progress. If we hold it here across the drain
10722 * wait, then we can deadlock. It's safe to drop the mutex here, since
10723 * no new references will come in after we've killed the percpu ref.
10724 */
10725 mutex_unlock(&ctx->uring_lock);
10726 do {
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010727 ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ);
10728 if (ret) {
10729 ret = min(0L, ret);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010730 break;
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010731 }
10732
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010733 ret = io_run_task_work_sig();
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010734 io_req_caches_free(ctx);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010735 } while (ret >= 0);
10736 mutex_lock(&ctx->uring_lock);
10737
10738 if (ret)
10739 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10740 return ret;
10741}
10742
Jens Axboeedafcce2019-01-09 09:16:05 -070010743static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10744 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010745 __releases(ctx->uring_lock)
10746 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010747{
10748 int ret;
10749
Jens Axboe35fa71a2019-04-22 10:23:23 -060010750 /*
10751 * We're inside the ring mutex, if the ref is already dying, then
10752 * someone else killed the ctx or is already going through
10753 * io_uring_register().
10754 */
10755 if (percpu_ref_is_dying(&ctx->refs))
10756 return -ENXIO;
10757
Pavel Begunkov75c40212021-04-15 13:07:40 +010010758 if (ctx->restricted) {
10759 if (opcode >= IORING_REGISTER_LAST)
10760 return -EINVAL;
10761 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10762 if (!test_bit(opcode, ctx->restrictions.register_op))
10763 return -EACCES;
10764 }
10765
Jens Axboe071698e2020-01-28 10:04:42 -070010766 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010767 ret = io_ctx_quiesce(ctx);
10768 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010769 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010770 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010771
10772 switch (opcode) {
10773 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010774 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010775 break;
10776 case IORING_UNREGISTER_BUFFERS:
10777 ret = -EINVAL;
10778 if (arg || nr_args)
10779 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010780 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010781 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010782 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010783 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010784 break;
10785 case IORING_UNREGISTER_FILES:
10786 ret = -EINVAL;
10787 if (arg || nr_args)
10788 break;
10789 ret = io_sqe_files_unregister(ctx);
10790 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010791 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010792 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010793 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010794 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010795 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010796 ret = -EINVAL;
10797 if (nr_args != 1)
10798 break;
10799 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010800 if (ret)
10801 break;
10802 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10803 ctx->eventfd_async = 1;
10804 else
10805 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010806 break;
10807 case IORING_UNREGISTER_EVENTFD:
10808 ret = -EINVAL;
10809 if (arg || nr_args)
10810 break;
10811 ret = io_eventfd_unregister(ctx);
10812 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010813 case IORING_REGISTER_PROBE:
10814 ret = -EINVAL;
10815 if (!arg || nr_args > 256)
10816 break;
10817 ret = io_probe(ctx, arg, nr_args);
10818 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010819 case IORING_REGISTER_PERSONALITY:
10820 ret = -EINVAL;
10821 if (arg || nr_args)
10822 break;
10823 ret = io_register_personality(ctx);
10824 break;
10825 case IORING_UNREGISTER_PERSONALITY:
10826 ret = -EINVAL;
10827 if (arg)
10828 break;
10829 ret = io_unregister_personality(ctx, nr_args);
10830 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010831 case IORING_REGISTER_ENABLE_RINGS:
10832 ret = -EINVAL;
10833 if (arg || nr_args)
10834 break;
10835 ret = io_register_enable_rings(ctx);
10836 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010837 case IORING_REGISTER_RESTRICTIONS:
10838 ret = io_register_restrictions(ctx, arg, nr_args);
10839 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010840 case IORING_REGISTER_FILES2:
10841 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010842 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010843 case IORING_REGISTER_FILES_UPDATE2:
10844 ret = io_register_rsrc_update(ctx, arg, nr_args,
10845 IORING_RSRC_FILE);
10846 break;
10847 case IORING_REGISTER_BUFFERS2:
10848 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10849 break;
10850 case IORING_REGISTER_BUFFERS_UPDATE:
10851 ret = io_register_rsrc_update(ctx, arg, nr_args,
10852 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010853 break;
Jens Axboefe764212021-06-17 10:19:54 -060010854 case IORING_REGISTER_IOWQ_AFF:
10855 ret = -EINVAL;
10856 if (!arg || !nr_args)
10857 break;
10858 ret = io_register_iowq_aff(ctx, arg, nr_args);
10859 break;
10860 case IORING_UNREGISTER_IOWQ_AFF:
10861 ret = -EINVAL;
10862 if (arg || nr_args)
10863 break;
10864 ret = io_unregister_iowq_aff(ctx);
10865 break;
Jens Axboe2e480052021-08-27 11:33:19 -060010866 case IORING_REGISTER_IOWQ_MAX_WORKERS:
10867 ret = -EINVAL;
10868 if (!arg || nr_args != 2)
10869 break;
10870 ret = io_register_iowq_max_workers(ctx, arg);
10871 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010872 default:
10873 ret = -EINVAL;
10874 break;
10875 }
10876
Jens Axboe071698e2020-01-28 10:04:42 -070010877 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010878 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010879 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010880 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010881 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010882 return ret;
10883}
10884
10885SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10886 void __user *, arg, unsigned int, nr_args)
10887{
10888 struct io_ring_ctx *ctx;
10889 long ret = -EBADF;
10890 struct fd f;
10891
10892 f = fdget(fd);
10893 if (!f.file)
10894 return -EBADF;
10895
10896 ret = -EOPNOTSUPP;
10897 if (f.file->f_op != &io_uring_fops)
10898 goto out_fput;
10899
10900 ctx = f.file->private_data;
10901
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010902 io_run_task_work();
10903
Jens Axboeedafcce2019-01-09 09:16:05 -070010904 mutex_lock(&ctx->uring_lock);
10905 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10906 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010907 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10908 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010909out_fput:
10910 fdput(f);
10911 return ret;
10912}
10913
Jens Axboe2b188cc2019-01-07 10:46:33 -070010914static int __init io_uring_init(void)
10915{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010916#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10917 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10918 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10919} while (0)
10920
10921#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10922 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10923 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10924 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10925 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10926 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10927 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10928 BUILD_BUG_SQE_ELEM(8, __u64, off);
10929 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10930 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010931 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010932 BUILD_BUG_SQE_ELEM(24, __u32, len);
10933 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10934 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10935 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10936 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010937 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10938 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010939 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10940 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10941 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10942 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10943 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10944 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10945 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10946 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010947 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010948 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10949 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010950 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010951 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010952 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010010953 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010954
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010955 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10956 sizeof(struct io_uring_rsrc_update));
10957 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10958 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010010959
10960 /* ->buf_index is u16 */
10961 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
10962
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010963 /* should fit into one byte */
10964 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010010965 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
10966 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010967
Jens Axboed3656342019-12-18 09:50:26 -070010968 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080010969 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010970
Jens Axboe91f245d2021-02-09 13:48:50 -070010971 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10972 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010973 return 0;
10974};
10975__initcall(io_uring_init);