blob: e3bfbd3f47873b4b747caf32c2079cbdfd147b0f [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 | \
112 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000113
Pavel Begunkov09899b12021-06-14 02:36:22 +0100114#define IO_TCTX_REFS_CACHE_NR (1U << 10)
115
Jens Axboe2b188cc2019-01-07 10:46:33 -0700116struct io_uring {
117 u32 head ____cacheline_aligned_in_smp;
118 u32 tail ____cacheline_aligned_in_smp;
119};
120
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * This data is shared with the application through the mmap at offsets
123 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 *
125 * The offsets to the member fields are published through struct
126 * io_sqring_offsets when calling io_uring_setup.
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 /*
130 * Head and tail offsets into the ring; the offsets need to be
131 * masked to get valid indices.
132 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 * The kernel controls head of the sq ring and the tail of the cq ring,
134 * and the application controls tail of the sq ring and the head of the
135 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000139 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 * ring_entries - 1)
141 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000142 u32 sq_ring_mask, cq_ring_mask;
143 /* Ring sizes (constant, power of 2) */
144 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200145 /*
146 * Number of invalid entries dropped by the kernel due to
147 * invalid index stored in array
148 *
149 * Written by the kernel, shouldn't be modified by the
150 * application (i.e. get number of "new events" by comparing to
151 * cached value).
152 *
153 * After a new SQ head value was read by the application this
154 * counter includes all submissions that were dropped reaching
155 * the new SQ head (and possibly more).
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200159 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200160 *
161 * Written by the kernel, shouldn't be modified by the
162 * application.
163 *
164 * The application needs a full memory barrier before checking
165 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
166 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000167 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200169 * Runtime CQ flags
170 *
171 * Written by the application, shouldn't be modified by the
172 * kernel.
173 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100174 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200175 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200176 * Number of completion events lost because the queue was full;
177 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800178 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200179 * the completion queue.
180 *
181 * Written by the kernel, shouldn't be modified by the
182 * application (i.e. get number of "new events" by comparing to
183 * cached value).
184 *
185 * As completion events come in out of order this counter is not
186 * ordered with any other data.
187 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000188 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200189 /*
190 * Ring buffer of completion events.
191 *
192 * The kernel writes completion events fresh every time they are
193 * produced, so the application is allowed to modify pending
194 * entries.
195 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000196 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700197};
198
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000199enum io_uring_cmd_flags {
200 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000201 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000202};
203
Jens Axboeedafcce2019-01-09 09:16:05 -0700204struct io_mapped_ubuf {
205 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100206 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700207 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600208 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100209 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700210};
211
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000212struct io_ring_ctx;
213
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000214struct io_overflow_cqe {
215 struct io_uring_cqe cqe;
216 struct list_head list;
217};
218
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100219struct io_fixed_file {
220 /* file * with additional FFS_* flags */
221 unsigned long file_ptr;
222};
223
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000224struct io_rsrc_put {
225 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100226 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000227 union {
228 void *rsrc;
229 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100230 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000231 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000232};
233
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100234struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100235 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700236};
237
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100238struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800239 struct percpu_ref refs;
240 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000241 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100242 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600243 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000244 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800245};
246
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100247typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
248
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100249struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700250 struct io_ring_ctx *ctx;
251
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100252 u64 **tags;
253 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100254 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100255 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700256 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800257 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700258};
259
Jens Axboe5a2e7452020-02-23 16:23:11 -0700260struct io_buffer {
261 struct list_head list;
262 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300263 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700264 __u16 bid;
265};
266
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200267struct io_restriction {
268 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
269 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
270 u8 sqe_flags_allowed;
271 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200272 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200273};
274
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700275enum {
276 IO_SQ_THREAD_SHOULD_STOP = 0,
277 IO_SQ_THREAD_SHOULD_PARK,
278};
279
Jens Axboe534ca6d2020-09-02 13:52:19 -0600280struct io_sq_data {
281 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000282 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000283 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600284
285 /* ctx's that are using this sqd */
286 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600287
Jens Axboe534ca6d2020-09-02 13:52:19 -0600288 struct task_struct *thread;
289 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800290
291 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700292 int sq_cpu;
293 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700294 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700295
296 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700297 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600298};
299
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000300#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000301#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000302#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000303
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000304struct io_submit_link {
305 struct io_kiocb *head;
306 struct io_kiocb *last;
307};
308
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000309struct io_submit_state {
310 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000311 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000312
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000313 bool plug_started;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +0100314 bool need_plug;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000315
316 /*
317 * Batch completion logic
318 */
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +0100319 struct io_wq_work_list compl_reqs;
320
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +0100321 /* inline/task_work completion list, under ->uring_lock */
322 struct list_head free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000323};
324
Jens Axboe2b188cc2019-01-07 10:46:33 -0700325struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100326 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700327 struct {
328 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100330 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800332 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800333 unsigned int drain_next: 1;
334 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200335 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100336 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100337 unsigned int drain_active: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100338 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100340 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100341 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100342 struct mutex uring_lock;
343
Hristo Venev75b28af2019-08-26 17:23:46 +0000344 /*
345 * Ring buffer of indices into array of io_uring_sqe, which is
346 * mmapped by the application using the IORING_OFF_SQES offset.
347 *
348 * This indirection could e.g. be used to assign fixed
349 * io_uring_sqe entries to operations and only submit them to
350 * the queue when needed.
351 *
352 * The kernel modifies neither the indices array nor the entries
353 * array.
354 */
355 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100356 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700357 unsigned cached_sq_head;
358 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600359 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100360
361 /*
362 * Fixed resources fast path, should be accessed only under
363 * uring_lock, and updated through io_uring_register(2)
364 */
365 struct io_rsrc_node *rsrc_node;
366 struct io_file_table file_table;
367 unsigned nr_user_files;
368 unsigned nr_user_bufs;
369 struct io_mapped_ubuf **user_bufs;
370
371 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600372 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600373 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700374 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100375 struct xarray io_buffers;
376 struct xarray personalities;
377 u32 pers_next;
378 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700379 } ____cacheline_aligned_in_smp;
380
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100381 /* IRQ completion list, under ->completion_lock */
382 struct list_head locked_free_list;
383 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700384
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100385 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600386 struct io_sq_data *sq_data; /* if using sq thread polling */
387
Jens Axboe90554202020-09-03 12:12:41 -0600388 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600389 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000390
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100391 unsigned long check_cq_overflow;
392
Jens Axboe206aefd2019-11-07 18:27:42 -0700393 struct {
394 unsigned cached_cq_tail;
395 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700396 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100397 struct wait_queue_head poll_wait;
398 struct wait_queue_head cq_wait;
399 unsigned cq_extra;
400 atomic_t cq_timeouts;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100401 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700402 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700403
404 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700405 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700406
Jens Axboe89850fc2021-08-10 15:11:51 -0600407 spinlock_t timeout_lock;
408
Jens Axboedef596e2019-01-09 08:59:42 -0700409 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300410 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700411 * io_uring instances that don't use IORING_SETUP_SQPOLL.
412 * For SQPOLL, only the single threaded io_sq_thread() will
413 * manipulate the list, hence no extra locking is needed there.
414 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300415 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700416 struct hlist_head *cancel_hash;
417 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800418 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700419 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600420
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200421 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700422
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100423 /* slow path rsrc auxilary data, used by update/register */
424 struct {
425 struct io_rsrc_node *rsrc_backup_node;
426 struct io_mapped_ubuf *dummy_ubuf;
427 struct io_rsrc_data *file_data;
428 struct io_rsrc_data *buf_data;
429
430 struct delayed_work rsrc_put_work;
431 struct llist_head rsrc_put_llist;
432 struct list_head rsrc_ref_list;
433 spinlock_t rsrc_ref_lock;
434 };
435
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700436 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100437 struct {
438 #if defined(CONFIG_UNIX)
439 struct socket *ring_sock;
440 #endif
441 /* hashed buffered write serialization */
442 struct io_wq_hash *hash_map;
443
444 /* Only used for accounting purposes */
445 struct user_struct *user;
446 struct mm_struct *mm_account;
447
448 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100449 struct llist_head fallback_llist;
450 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100451 struct work_struct exit_work;
452 struct list_head tctx_list;
453 struct completion ref_comp;
454 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700455};
456
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100457struct io_uring_task {
458 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100459 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100460 struct xarray xa;
461 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100462 const struct io_ring_ctx *last;
463 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100464 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100465 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100466 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100467
468 spinlock_t task_lock;
469 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100470 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100471 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100472};
473
Jens Axboe09bb8392019-03-13 12:39:28 -0600474/*
475 * First field must be the file pointer in all the
476 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
477 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700478struct io_poll_iocb {
479 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000480 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700481 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600482 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700483 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700484 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700485};
486
Pavel Begunkov9d805892021-04-13 02:58:40 +0100487struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000488 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100489 u64 old_user_data;
490 u64 new_user_data;
491 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600492 bool update_events;
493 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000494};
495
Jens Axboeb5dba592019-12-11 14:02:38 -0700496struct io_close {
497 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700498 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100499 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700500};
501
Jens Axboead8a48a2019-11-15 08:49:11 -0700502struct io_timeout_data {
503 struct io_kiocb *req;
504 struct hrtimer timer;
505 struct timespec64 ts;
506 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600507 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700508};
509
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700510struct io_accept {
511 struct file *file;
512 struct sockaddr __user *addr;
513 int __user *addr_len;
514 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100515 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600516 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700517};
518
519struct io_sync {
520 struct file *file;
521 loff_t len;
522 loff_t off;
523 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700524 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700525};
526
Jens Axboefbf23842019-12-17 18:45:56 -0700527struct io_cancel {
528 struct file *file;
529 u64 addr;
530};
531
Jens Axboeb29472e2019-12-17 18:50:29 -0700532struct io_timeout {
533 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300534 u32 off;
535 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300536 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000537 /* head of the link, used by linked timeouts only */
538 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600539 /* for linked completions */
540 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700541};
542
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100543struct io_timeout_rem {
544 struct file *file;
545 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000546
547 /* timeout update */
548 struct timespec64 ts;
549 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600550 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100551};
552
Jens Axboe9adbd452019-12-20 08:45:55 -0700553struct io_rw {
554 /* NOTE: kiocb has the file as the first member, so don't do it here */
555 struct kiocb kiocb;
556 u64 addr;
557 u64 len;
558};
559
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700560struct io_connect {
561 struct file *file;
562 struct sockaddr __user *addr;
563 int addr_len;
564};
565
Jens Axboee47293f2019-12-20 08:58:21 -0700566struct io_sr_msg {
567 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700568 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100569 struct compat_msghdr __user *umsg_compat;
570 struct user_msghdr __user *umsg;
571 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700572 };
Jens Axboee47293f2019-12-20 08:58:21 -0700573 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700574 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700575 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700576 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700577};
578
Jens Axboe15b71ab2019-12-11 11:20:36 -0700579struct io_open {
580 struct file *file;
581 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100582 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700583 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700584 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600585 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700586};
587
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000588struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700589 struct file *file;
590 u64 arg;
591 u32 nr_args;
592 u32 offset;
593};
594
Jens Axboe4840e412019-12-25 22:03:45 -0700595struct io_fadvise {
596 struct file *file;
597 u64 offset;
598 u32 len;
599 u32 advice;
600};
601
Jens Axboec1ca7572019-12-25 22:18:28 -0700602struct io_madvise {
603 struct file *file;
604 u64 addr;
605 u32 len;
606 u32 advice;
607};
608
Jens Axboe3e4827b2020-01-08 15:18:09 -0700609struct io_epoll {
610 struct file *file;
611 int epfd;
612 int op;
613 int fd;
614 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700615};
616
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300617struct io_splice {
618 struct file *file_out;
619 struct file *file_in;
620 loff_t off_out;
621 loff_t off_in;
622 u64 len;
623 unsigned int flags;
624};
625
Jens Axboeddf0322d2020-02-23 16:41:33 -0700626struct io_provide_buf {
627 struct file *file;
628 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100629 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700630 __u32 bgid;
631 __u16 nbufs;
632 __u16 bid;
633};
634
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700635struct io_statx {
636 struct file *file;
637 int dfd;
638 unsigned int mask;
639 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700640 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700641 struct statx __user *buffer;
642};
643
Jens Axboe36f4fa62020-09-05 11:14:22 -0600644struct io_shutdown {
645 struct file *file;
646 int how;
647};
648
Jens Axboe80a261f2020-09-28 14:23:58 -0600649struct io_rename {
650 struct file *file;
651 int old_dfd;
652 int new_dfd;
653 struct filename *oldpath;
654 struct filename *newpath;
655 int flags;
656};
657
Jens Axboe14a11432020-09-28 14:27:37 -0600658struct io_unlink {
659 struct file *file;
660 int dfd;
661 int flags;
662 struct filename *filename;
663};
664
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700665struct io_mkdir {
666 struct file *file;
667 int dfd;
668 umode_t mode;
669 struct filename *filename;
670};
671
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700672struct io_symlink {
673 struct file *file;
674 int new_dfd;
675 struct filename *oldpath;
676 struct filename *newpath;
677};
678
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700679struct io_hardlink {
680 struct file *file;
681 int old_dfd;
682 int new_dfd;
683 struct filename *oldpath;
684 struct filename *newpath;
685 int flags;
686};
687
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300688struct io_completion {
689 struct file *file;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000690 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300691};
692
Jens Axboef499a022019-12-02 16:28:46 -0700693struct io_async_connect {
694 struct sockaddr_storage address;
695};
696
Jens Axboe03b12302019-12-02 18:50:25 -0700697struct io_async_msghdr {
698 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000699 /* points to an allocated iov, if NULL we use fast_iov instead */
700 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700701 struct sockaddr __user *uaddr;
702 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700703 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700704};
705
Jens Axboef67676d2019-12-02 11:03:47 -0700706struct io_async_rw {
707 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600708 const struct iovec *free_iovec;
709 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600710 struct iov_iter_state iter_state;
Jens Axboe227c0c92020-08-13 11:51:40 -0600711 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600712 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700713};
714
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300715enum {
716 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
717 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
718 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
719 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
720 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700721 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300722
Pavel Begunkovdddca222021-04-27 16:13:52 +0100723 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100724 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300725 REQ_F_INFLIGHT_BIT,
726 REQ_F_CUR_POS_BIT,
727 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300728 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300729 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700730 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700731 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000732 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600733 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100734 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100735 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100736 REQ_F_ARM_LTIMEOUT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700737 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100738 REQ_F_NOWAIT_READ_BIT,
739 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700740 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700741
742 /* not a real bit, just to check we're not overflowing the space */
743 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300744};
745
746enum {
747 /* ctx owns file */
748 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
749 /* drain existing IO first */
750 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
751 /* linked sqes */
752 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
753 /* doesn't sever on completion < 0 */
754 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
755 /* IOSQE_ASYNC */
756 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700757 /* IOSQE_BUFFER_SELECT */
758 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300759
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300760 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100761 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000762 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300763 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
764 /* read/write uses file position */
765 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
766 /* must not punt to workers */
767 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100768 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300769 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300770 /* needs cleanup */
771 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700772 /* already went through poll handler */
773 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700774 /* buffer already selected */
775 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000776 /* completion is deferred through io_comp_state */
777 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600778 /* caller should reissue async */
779 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700780 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100781 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700782 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100783 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700784 /* regular file */
785 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100786 /* has creds assigned */
787 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100788 /* skip refcounting if not set */
789 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100790 /* there is a linked timeout that has to be armed */
791 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700792};
793
794struct async_poll {
795 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600796 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300797};
798
Pavel Begunkovf237c302021-08-18 12:42:46 +0100799typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100800
Jens Axboe7cbf1722021-02-10 00:03:20 +0000801struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100802 union {
803 struct io_wq_work_node node;
804 struct llist_node fallback_node;
805 };
806 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000807};
808
Pavel Begunkov992da012021-06-10 16:37:37 +0100809enum {
810 IORING_RSRC_FILE = 0,
811 IORING_RSRC_BUFFER = 1,
812};
813
Jens Axboe09bb8392019-03-13 12:39:28 -0600814/*
815 * NOTE! Each of the iocb union members has the file pointer
816 * as the first entry in their struct definition. So you can
817 * access the file pointer through any of the sub-structs,
818 * or directly as just 'ki_filp' in this struct.
819 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700820struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700821 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600822 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700823 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700824 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100825 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700826 struct io_accept accept;
827 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700828 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700829 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100830 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700831 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700832 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700833 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700834 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000835 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700836 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700837 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700838 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300839 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700840 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700841 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600842 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600843 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600844 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700845 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700846 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700847 struct io_hardlink hardlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300848 /* use only after cleaning per-op data, see io_clean_op() */
849 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700850 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700851
Jens Axboee8c2bc12020-08-15 18:44:09 -0700852 /* opcode allocated if it needs to store data for async defer */
853 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700854 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800855 /* polled IO has completed */
856 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700857
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700858 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300859 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700860
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300861 struct io_ring_ctx *ctx;
862 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700863 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300864 struct task_struct *task;
865 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000867 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000868 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700869
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100870 /* used with ctx->iopoll_list with reads/writes */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300871 struct list_head inflight_entry;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100872 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300873 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
874 struct hlist_node hash_node;
875 struct async_poll *apoll;
876 struct io_wq_work work;
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100877 const struct cred *creds;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +0100878
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +0100879 struct io_wq_work_node comp_list;
880
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100881 /* store used ubuf, so we can prevent reloading */
882 struct io_mapped_ubuf *imu;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700883};
884
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000885struct io_tctx_node {
886 struct list_head ctx_node;
887 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000888 struct io_ring_ctx *ctx;
889};
890
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300891struct io_defer_entry {
892 struct list_head list;
893 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300894 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300895};
896
Jens Axboed3656342019-12-18 09:50:26 -0700897struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700898 /* needs req->file assigned */
899 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700900 /* hash wq insertion if file is a regular file */
901 unsigned hash_reg_file : 1;
902 /* unbound wq insertion if file is a non-regular file */
903 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700904 /* opcode is not supported by this kernel */
905 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700906 /* set if opcode supports polled "wait" */
907 unsigned pollin : 1;
908 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700909 /* op supports buffer selection */
910 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000911 /* do prep async if is going to be punted */
912 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600913 /* should block plug */
914 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700915 /* size of async data needed, if any */
916 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700917};
918
Jens Axboe09186822020-10-13 15:01:40 -0600919static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300920 [IORING_OP_NOP] = {},
921 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700922 .needs_file = 1,
923 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700924 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700925 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000926 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600927 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700928 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700929 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300930 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700931 .needs_file = 1,
932 .hash_reg_file = 1,
933 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700934 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000935 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600936 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700937 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700938 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300939 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700940 .needs_file = 1,
941 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300942 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700943 .needs_file = 1,
944 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700945 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600946 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700947 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700948 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300949 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700950 .needs_file = 1,
951 .hash_reg_file = 1,
952 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700953 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600954 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700955 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700958 .needs_file = 1,
959 .unbound_nonreg_file = 1,
960 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300961 [IORING_OP_POLL_REMOVE] = {},
962 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700963 .needs_file = 1,
964 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300965 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700966 .needs_file = 1,
967 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700968 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000969 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700970 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700971 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300972 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700973 .needs_file = 1,
974 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700975 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700976 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000977 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700978 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700979 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300980 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700981 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700982 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000983 [IORING_OP_TIMEOUT_REMOVE] = {
984 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000985 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300986 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700987 .needs_file = 1,
988 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700989 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700990 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300991 [IORING_OP_ASYNC_CANCEL] = {},
992 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700993 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700994 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300995 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700996 .needs_file = 1,
997 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700998 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000999 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001000 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001001 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001002 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001003 .needs_file = 1,
1004 },
Jens Axboe44526be2021-02-15 13:32:18 -07001005 [IORING_OP_OPENAT] = {},
1006 [IORING_OP_CLOSE] = {},
1007 [IORING_OP_FILES_UPDATE] = {},
1008 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001009 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001010 .needs_file = 1,
1011 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001012 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001013 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001014 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001015 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001016 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001017 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001018 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001019 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001020 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001021 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001022 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001023 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001024 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001025 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001026 .needs_file = 1,
1027 },
Jens Axboe44526be2021-02-15 13:32:18 -07001028 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001029 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001030 .needs_file = 1,
1031 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001032 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001033 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001034 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001035 .needs_file = 1,
1036 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001037 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001038 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001039 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001040 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001041 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001042 [IORING_OP_EPOLL_CTL] = {
1043 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001044 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001045 [IORING_OP_SPLICE] = {
1046 .needs_file = 1,
1047 .hash_reg_file = 1,
1048 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001049 },
1050 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001051 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001052 [IORING_OP_TEE] = {
1053 .needs_file = 1,
1054 .hash_reg_file = 1,
1055 .unbound_nonreg_file = 1,
1056 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001057 [IORING_OP_SHUTDOWN] = {
1058 .needs_file = 1,
1059 },
Jens Axboe44526be2021-02-15 13:32:18 -07001060 [IORING_OP_RENAMEAT] = {},
1061 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001062 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001063 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001064 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001065};
1066
Pavel Begunkov0756a862021-08-15 10:40:25 +01001067/* requests with any of those set should undergo io_disarm_next() */
1068#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1069
Pavel Begunkov7a612352021-03-09 00:37:59 +00001070static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001071static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001072static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1073 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001074 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001075static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001076
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001077static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1078 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001079static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001080static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001081static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001082static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001083static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001084 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001085 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001086static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001087static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001088 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001089static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001090static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001091
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001092static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkovc4501782021-09-08 16:40:52 +01001093static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001094static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001095
Pavel Begunkovb9445592021-08-25 12:25:45 +01001096static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1097 unsigned int issue_flags, u32 slot_index);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01001098static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1099
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001100static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001101
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102static struct kmem_cache *req_cachep;
1103
Jens Axboe09186822020-10-13 15:01:40 -06001104static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105
1106struct sock *io_uring_get_socket(struct file *file)
1107{
1108#if defined(CONFIG_UNIX)
1109 if (file->f_op == &io_uring_fops) {
1110 struct io_ring_ctx *ctx = file->private_data;
1111
1112 return ctx->ring_sock->sk;
1113 }
1114#endif
1115 return NULL;
1116}
1117EXPORT_SYMBOL(io_uring_get_socket);
1118
Pavel Begunkovf237c302021-08-18 12:42:46 +01001119static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1120{
1121 if (!*locked) {
1122 mutex_lock(&ctx->uring_lock);
1123 *locked = true;
1124 }
1125}
1126
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001127#define io_for_each_link(pos, head) \
1128 for (pos = (head); pos; pos = pos->link)
1129
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001130/*
1131 * Shamelessly stolen from the mm implementation of page reference checking,
1132 * see commit f958d7b528b1 for details.
1133 */
1134#define req_ref_zero_or_close_to_overflow(req) \
1135 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1136
1137static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1138{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001139 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001140 return atomic_inc_not_zero(&req->refs);
1141}
1142
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001143static inline bool req_ref_put_and_test(struct io_kiocb *req)
1144{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001145 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1146 return true;
1147
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001148 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1149 return atomic_dec_and_test(&req->refs);
1150}
1151
1152static inline void req_ref_put(struct io_kiocb *req)
1153{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001154 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001155 WARN_ON_ONCE(req_ref_put_and_test(req));
1156}
1157
1158static inline void req_ref_get(struct io_kiocb *req)
1159{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001160 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001161 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1162 atomic_inc(&req->refs);
1163}
1164
Pavel Begunkovc4501782021-09-08 16:40:52 +01001165static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1166{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001167 if (!wq_list_empty(&ctx->submit_state.compl_reqs))
Pavel Begunkovc4501782021-09-08 16:40:52 +01001168 __io_submit_flush_completions(ctx);
1169}
1170
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001171static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001172{
1173 if (!(req->flags & REQ_F_REFCOUNT)) {
1174 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001175 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001176 }
1177}
1178
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001179static inline void io_req_set_refcount(struct io_kiocb *req)
1180{
1181 __io_req_set_refcount(req, 1);
1182}
1183
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001184static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001185{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001186 struct io_ring_ctx *ctx = req->ctx;
1187
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001188 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001189 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001190 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001191 }
1192}
1193
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001194static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1195{
1196 bool got = percpu_ref_tryget(ref);
1197
1198 /* already at zero, wait for ->release() */
1199 if (!got)
1200 wait_for_completion(compl);
1201 percpu_ref_resurrect(ref);
1202 if (got)
1203 percpu_ref_put(ref);
1204}
1205
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001206static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1207 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001208{
1209 struct io_kiocb *req;
1210
Pavel Begunkov68207682021-03-22 01:58:25 +00001211 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001212 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001213 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001214 return true;
1215
1216 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001217 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001218 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001219 }
1220 return false;
1221}
1222
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001223static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001224{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001225 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001226}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001227
Hao Xua8295b92021-08-27 17:46:09 +08001228static inline void req_fail_link_node(struct io_kiocb *req, int res)
1229{
1230 req_set_fail(req);
1231 req->result = res;
1232}
1233
Jens Axboe2b188cc2019-01-07 10:46:33 -07001234static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1235{
1236 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1237
Jens Axboe0f158b42020-05-14 17:18:39 -06001238 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001239}
1240
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001241static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1242{
1243 return !req->timeout.off;
1244}
1245
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001246static void io_fallback_req_func(struct work_struct *work)
1247{
1248 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1249 fallback_work.work);
1250 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1251 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001252 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001253
1254 percpu_ref_get(&ctx->refs);
1255 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001256 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001257
Pavel Begunkovf237c302021-08-18 12:42:46 +01001258 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001259 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001260 mutex_unlock(&ctx->uring_lock);
1261 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001262 percpu_ref_put(&ctx->refs);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001263
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001264}
1265
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1267{
1268 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001269 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270
1271 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1272 if (!ctx)
1273 return NULL;
1274
Jens Axboe78076bb2019-12-04 19:56:40 -07001275 /*
1276 * Use 5 bits less than the max cq entries, that should give us around
1277 * 32 entries per hash list if totally full and uniformly spread.
1278 */
1279 hash_bits = ilog2(p->cq_entries);
1280 hash_bits -= 5;
1281 if (hash_bits <= 0)
1282 hash_bits = 1;
1283 ctx->cancel_hash_bits = hash_bits;
1284 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1285 GFP_KERNEL);
1286 if (!ctx->cancel_hash)
1287 goto err;
1288 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1289
Pavel Begunkov62248432021-04-28 13:11:29 +01001290 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1291 if (!ctx->dummy_ubuf)
1292 goto err;
1293 /* set invalid range, so io_import_fixed() fails meeting it */
1294 ctx->dummy_ubuf->ubuf = -1UL;
1295
Roman Gushchin21482892019-05-07 10:01:48 -07001296 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001297 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1298 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001299
1300 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001301 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001302 INIT_LIST_HEAD(&ctx->sqd_list);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001303 init_waitqueue_head(&ctx->poll_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001304 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001305 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001306 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001307 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001309 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001310 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001311 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001312 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001313 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001314 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001315 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001316 spin_lock_init(&ctx->rsrc_ref_lock);
1317 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001318 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1319 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001320 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001321 INIT_LIST_HEAD(&ctx->submit_state.free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001322 INIT_LIST_HEAD(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001323 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001324 INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001326err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001327 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001328 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001329 kfree(ctx);
1330 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001331}
1332
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001333static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1334{
1335 struct io_rings *r = ctx->rings;
1336
1337 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1338 ctx->cq_extra--;
1339}
1340
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001341static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001342{
Jens Axboe2bc99302020-07-09 09:43:27 -06001343 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1344 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001345
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001346 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001347 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001348
Bob Liu9d858b22019-11-13 18:06:25 +08001349 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001350}
1351
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001352#define FFS_ASYNC_READ 0x1UL
1353#define FFS_ASYNC_WRITE 0x2UL
1354#ifdef CONFIG_64BIT
1355#define FFS_ISREG 0x4UL
1356#else
1357#define FFS_ISREG 0x0UL
1358#endif
1359#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1360
1361static inline bool io_req_ffs_set(struct io_kiocb *req)
1362{
1363 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1364}
1365
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001366static void io_req_track_inflight(struct io_kiocb *req)
1367{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001368 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001369 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001370 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001371 }
1372}
1373
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001374static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1375{
1376 req->flags &= ~REQ_F_LINK_TIMEOUT;
1377}
1378
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001379static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1380{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001381 if (WARN_ON_ONCE(!req->link))
1382 return NULL;
1383
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001384 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1385 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001386
1387 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001388 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001389 __io_req_set_refcount(req->link, 2);
1390 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001391}
1392
1393static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1394{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001395 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001396 return NULL;
1397 return __io_prep_linked_timeout(req);
1398}
1399
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001400static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001401{
Jens Axboed3656342019-12-18 09:50:26 -07001402 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001403 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001404
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001405 if (!(req->flags & REQ_F_CREDS)) {
1406 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001407 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001408 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001409
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001410 req->work.list.next = NULL;
1411 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001412 if (req->flags & REQ_F_FORCE_ASYNC)
1413 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1414
Jens Axboed3656342019-12-18 09:50:26 -07001415 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001416 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001417 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001418 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001419 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001420 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001421 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001422
1423 switch (req->opcode) {
1424 case IORING_OP_SPLICE:
1425 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001426 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1427 req->work.flags |= IO_WQ_WORK_UNBOUND;
1428 break;
1429 }
Jens Axboe561fb042019-10-24 07:25:42 -06001430}
1431
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001432static void io_prep_async_link(struct io_kiocb *req)
1433{
1434 struct io_kiocb *cur;
1435
Pavel Begunkov44eff402021-07-26 14:14:31 +01001436 if (req->flags & REQ_F_LINK_TIMEOUT) {
1437 struct io_ring_ctx *ctx = req->ctx;
1438
Jens Axboe79ebeae2021-08-10 15:18:27 -06001439 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001440 io_for_each_link(cur, req)
1441 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001442 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001443 } else {
1444 io_for_each_link(cur, req)
1445 io_prep_async_work(cur);
1446 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001447}
1448
Pavel Begunkovf237c302021-08-18 12:42:46 +01001449static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001450{
Jackie Liua197f662019-11-08 08:09:12 -07001451 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001452 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001453 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001454
Pavel Begunkovf237c302021-08-18 12:42:46 +01001455 /* must not take the lock, NULL it as a precaution */
1456 locked = NULL;
1457
Jens Axboe3bfe6102021-02-16 14:15:30 -07001458 BUG_ON(!tctx);
1459 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001460
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001461 /* init ->work of the whole link before punting */
1462 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001463
1464 /*
1465 * Not expected to happen, but if we do have a bug where this _can_
1466 * happen, catch it here and ensure the request is marked as
1467 * canceled. That will make io-wq go through the usual work cancel
1468 * procedure rather than attempt to run this request (or create a new
1469 * worker for it).
1470 */
1471 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1472 req->work.flags |= IO_WQ_WORK_CANCEL;
1473
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001474 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1475 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001476 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001477 if (link)
1478 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001479}
1480
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001481static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001482 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001483 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001484{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001485 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001486
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001487 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001488 if (status)
1489 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001490 atomic_set(&req->ctx->cq_timeouts,
1491 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001492 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001493 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001494 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001495 }
1496}
1497
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001498static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001499{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001500 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001501 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1502 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001503
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001504 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001505 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001506 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001507 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001508 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001509 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001510}
1511
Pavel Begunkov360428f2020-05-30 14:54:17 +03001512static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001513 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001514{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001515 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001516
Jens Axboe79ebeae2021-08-10 15:18:27 -06001517 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001518 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001519 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001520 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001521 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001522
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001523 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001524 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001525
1526 /*
1527 * Since seq can easily wrap around over time, subtract
1528 * the last seq at which timeouts were flushed before comparing.
1529 * Assuming not more than 2^31-1 events have happened since,
1530 * these subtractions won't have wrapped, so we can check if
1531 * target is in [last_seq, current_seq] by comparing the two.
1532 */
1533 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1534 events_got = seq - ctx->cq_last_tm_flush;
1535 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001536 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001537
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001538 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001539 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001540 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001541 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001542 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001543}
1544
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001545static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001546{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001547 if (ctx->off_timeout_used)
1548 io_flush_timeouts(ctx);
1549 if (ctx->drain_active)
1550 io_queue_deferred(ctx);
1551}
1552
1553static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1554{
1555 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1556 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001557 /* order cqe stores with ring update */
1558 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001559}
1560
Jens Axboe90554202020-09-03 12:12:41 -06001561static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1562{
1563 struct io_rings *r = ctx->rings;
1564
Pavel Begunkova566c552021-05-16 22:58:08 +01001565 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001566}
1567
Pavel Begunkov888aae22021-01-19 13:32:39 +00001568static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1569{
1570 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1571}
1572
Pavel Begunkovd068b502021-05-16 22:58:11 +01001573static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001574{
Hristo Venev75b28af2019-08-26 17:23:46 +00001575 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001576 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577
Stefan Bühler115e12e2019-04-24 23:54:18 +02001578 /*
1579 * writes to the cq entry need to come after reading head; the
1580 * control dependency is enough as we're using WRITE_ONCE to
1581 * fill the cq entry
1582 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001583 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001584 return NULL;
1585
Pavel Begunkov888aae22021-01-19 13:32:39 +00001586 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001587 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001588}
1589
Jens Axboef2842ab2020-01-08 11:04:00 -07001590static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1591{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001592 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001593 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001594 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1595 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001596 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001597}
1598
Jens Axboe2c5d7632021-08-21 07:21:19 -06001599/*
1600 * This should only get called when at least one event has been posted.
1601 * Some applications rely on the eventfd notification count only changing
1602 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1603 * 1:1 relationship between how many times this function is called (and
1604 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1605 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001606static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001607{
Jens Axboe5fd46172021-08-06 14:04:31 -06001608 /*
1609 * wake_up_all() may seem excessive, but io_wake_function() and
1610 * io_should_wake() handle the termination of the loop and only
1611 * wake as many waiters as we need to.
1612 */
1613 if (wq_has_sleeper(&ctx->cq_wait))
1614 wake_up_all(&ctx->cq_wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001615 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1616 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001617 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001618 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov3f008382021-10-01 10:39:33 +01001619 if (waitqueue_active(&ctx->poll_wait))
Pavel Begunkov311997b2021-06-14 23:37:28 +01001620 wake_up_interruptible(&ctx->poll_wait);
Jens Axboe8c838782019-03-12 15:48:16 -06001621}
1622
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001623static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1624{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001625 /* see waitqueue_active() comment */
1626 smp_mb();
1627
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001628 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001629 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001630 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001631 }
1632 if (io_should_trigger_evfd(ctx))
1633 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov3f008382021-10-01 10:39:33 +01001634 if (waitqueue_active(&ctx->poll_wait))
Pavel Begunkov311997b2021-06-14 23:37:28 +01001635 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001636}
1637
Jens Axboec4a2ed72019-11-21 21:01:26 -07001638/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001639static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001640{
Jens Axboeb18032b2021-01-24 16:58:56 -07001641 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001642
Pavel Begunkova566c552021-05-16 22:58:08 +01001643 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001644 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001645
Jens Axboeb18032b2021-01-24 16:58:56 -07001646 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001647 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001648 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001649 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001650 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001651
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001652 if (!cqe && !force)
1653 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001654 ocqe = list_first_entry(&ctx->cq_overflow_list,
1655 struct io_overflow_cqe, list);
1656 if (cqe)
1657 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1658 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001659 io_account_cq_overflow(ctx);
1660
Jens Axboeb18032b2021-01-24 16:58:56 -07001661 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001662 list_del(&ocqe->list);
1663 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001664 }
1665
Pavel Begunkov09e88402020-12-17 00:24:38 +00001666 all_flushed = list_empty(&ctx->cq_overflow_list);
1667 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001668 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001669 WRITE_ONCE(ctx->rings->sq_flags,
1670 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001671 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001672
Jens Axboeb18032b2021-01-24 16:58:56 -07001673 if (posted)
1674 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001675 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001676 if (posted)
1677 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001678 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001679}
1680
Pavel Begunkov90f67362021-08-09 20:18:12 +01001681static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001682{
Jens Axboeca0a2652021-03-04 17:15:48 -07001683 bool ret = true;
1684
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001685 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001686 /* iopoll syncs against uring_lock, not completion_lock */
1687 if (ctx->flags & IORING_SETUP_IOPOLL)
1688 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001689 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001690 if (ctx->flags & IORING_SETUP_IOPOLL)
1691 mutex_unlock(&ctx->uring_lock);
1692 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001693
1694 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001695}
1696
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001697/* must to be called somewhat shortly after putting a request */
1698static inline void io_put_task(struct task_struct *task, int nr)
1699{
1700 struct io_uring_task *tctx = task->io_uring;
1701
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001702 if (likely(task == current)) {
1703 tctx->cached_refs += nr;
1704 } else {
1705 percpu_counter_sub(&tctx->inflight, nr);
1706 if (unlikely(atomic_read(&tctx->in_idle)))
1707 wake_up(&tctx->wait);
1708 put_task_struct_many(task, nr);
1709 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001710}
1711
Pavel Begunkov9a108672021-08-27 11:55:01 +01001712static void io_task_refs_refill(struct io_uring_task *tctx)
1713{
1714 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1715
1716 percpu_counter_add(&tctx->inflight, refill);
1717 refcount_add(refill, &current->usage);
1718 tctx->cached_refs += refill;
1719}
1720
1721static inline void io_get_task_refs(int nr)
1722{
1723 struct io_uring_task *tctx = current->io_uring;
1724
1725 tctx->cached_refs -= nr;
1726 if (unlikely(tctx->cached_refs < 0))
1727 io_task_refs_refill(tctx);
1728}
1729
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001730static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1731 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001733 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001735 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1736 if (!ocqe) {
1737 /*
1738 * If we're in ring overflow flush mode, or in task cancel mode,
1739 * or cannot allocate an overflow entry, then we need to drop it
1740 * on the floor.
1741 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001742 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001743 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001745 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001746 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001747 WRITE_ONCE(ctx->rings->sq_flags,
1748 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1749
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001750 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001751 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001752 ocqe->cqe.res = res;
1753 ocqe->cqe.flags = cflags;
1754 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1755 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756}
1757
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001758static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1759 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001760{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001761 struct io_uring_cqe *cqe;
1762
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001763 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764
1765 /*
1766 * If we can't get a cq entry, userspace overflowed the
1767 * submission (by quite a lot). Increment the overflow count in
1768 * the ring.
1769 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001770 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001771 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001772 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001773 WRITE_ONCE(cqe->res, res);
1774 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001775 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001777 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001778}
1779
Pavel Begunkov8d133262021-04-11 01:46:33 +01001780/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001781static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1782 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001783{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001784 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001785}
1786
Pavel Begunkov7a612352021-03-09 00:37:59 +00001787static void io_req_complete_post(struct io_kiocb *req, long res,
1788 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001789{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001790 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791
Jens Axboe79ebeae2021-08-10 15:18:27 -06001792 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001793 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001794 /*
1795 * If we're the last reference to this request, add to our locked
1796 * free_list cache.
1797 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001798 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001799 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001800 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001801 io_disarm_next(req);
1802 if (req->link) {
1803 io_req_task_queue(req->link);
1804 req->link = NULL;
1805 }
1806 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001807 io_dismantle_req(req);
1808 io_put_task(req->task, 1);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001809 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001810 ctx->locked_free_nr++;
Pavel Begunkova3f349072021-09-15 12:04:20 +01001811 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001812 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001813 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001814 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001815 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001816}
1817
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001818static inline bool io_req_needs_clean(struct io_kiocb *req)
1819{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001820 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001821}
1822
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001823static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001824 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001825{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001826 struct io_submit_state *state;
1827
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001828 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001829 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001830 req->result = res;
1831 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001832 req->flags |= REQ_F_COMPLETE_INLINE;
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001833
1834 state = &req->ctx->submit_state;
1835 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
Jens Axboee1e16092020-06-22 09:17:17 -06001836}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001837
Pavel Begunkov889fca72021-02-10 00:03:09 +00001838static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1839 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001840{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001841 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1842 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001843 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001844 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001845}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001846
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001847static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001848{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001849 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001850}
1851
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001852static void io_req_complete_failed(struct io_kiocb *req, long res)
1853{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001854 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001855 io_req_complete_post(req, res, 0);
1856}
1857
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001858static void io_req_complete_fail_submit(struct io_kiocb *req)
1859{
1860 /*
1861 * We don't submit, fail them all, for that replace hardlinks with
1862 * normal links. Extra REQ_F_LINK is tolerated.
1863 */
1864 req->flags &= ~REQ_F_HARDLINK;
1865 req->flags |= REQ_F_LINK;
1866 io_req_complete_failed(req, req->result);
1867}
1868
Pavel Begunkov864ea922021-08-09 13:04:08 +01001869/*
1870 * Don't initialise the fields below on every allocation, but do that in
1871 * advance and keep them valid across allocations.
1872 */
1873static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1874{
1875 req->ctx = ctx;
1876 req->link = NULL;
1877 req->async_data = NULL;
1878 /* not necessary, but safer to zero */
1879 req->result = 0;
1880}
1881
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001882static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001883 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001884{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001885 spin_lock(&ctx->completion_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001886 list_splice_init(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001887 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001888 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001889}
1890
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001891/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001892static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001893{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001894 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001895
Jens Axboec7dae4b2021-02-09 19:53:37 -07001896 /*
1897 * If we have more than a batch's worth of requests in our IRQ side
1898 * locked cache, grab the lock and move them over to our submission
1899 * side cache.
1900 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001901 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001902 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001903 return !list_empty(&state->free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001904}
1905
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001906/*
1907 * A request might get retired back into the request caches even before opcode
1908 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1909 * Because of that, io_alloc_req() should be called only under ->uring_lock
1910 * and with extra caution to not get a request that is still worked on.
1911 */
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001912static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001913 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001915 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001916 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001917 void *reqs[IO_REQ_ALLOC_BATCH];
1918 struct io_kiocb *req;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001919 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001920
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001921 if (likely(!list_empty(&state->free_list) || io_flush_cached_reqs(ctx)))
Pavel Begunkov864ea922021-08-09 13:04:08 +01001922 goto got_req;
Jens Axboe2579f912019-01-09 09:10:43 -07001923
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001924 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001925
Pavel Begunkov864ea922021-08-09 13:04:08 +01001926 /*
1927 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1928 * retry single alloc to be on the safe side.
1929 */
1930 if (unlikely(ret <= 0)) {
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001931 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1932 if (!reqs[0])
Pavel Begunkov864ea922021-08-09 13:04:08 +01001933 return NULL;
1934 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001935 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001936
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001937 for (i = 0; i < ret; i++) {
1938 req = reqs[i];
1939
1940 io_preinit_req(req, ctx);
1941 list_add(&req->inflight_entry, &state->free_list);
1942 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001943got_req:
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001944 req = list_first_entry(&state->free_list, struct io_kiocb, inflight_entry);
1945 list_del(&req->inflight_entry);
1946 return req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001947}
1948
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001949static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001950{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001951 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001952 fput(file);
1953}
1954
Pavel Begunkov6b639522021-09-08 16:40:50 +01001955static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001957 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001958
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001959 if (io_req_needs_clean(req))
1960 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001961 if (!(flags & REQ_F_FIXED_FILE))
1962 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001963 if (req->fixed_rsrc_refs)
1964 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001965 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00001966 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001967 req->async_data = NULL;
1968 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001969}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001970
Pavel Begunkov216578e2020-10-13 09:44:00 +01001971static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001972{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001973 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001974
Pavel Begunkov216578e2020-10-13 09:44:00 +01001975 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001976 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001977
Jens Axboe79ebeae2021-08-10 15:18:27 -06001978 spin_lock(&ctx->completion_lock);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001979 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001980 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001981 spin_unlock(&ctx->completion_lock);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001982
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001983 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001984}
1985
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001986static inline void io_remove_next_linked(struct io_kiocb *req)
1987{
1988 struct io_kiocb *nxt = req->link;
1989
1990 req->link = nxt->link;
1991 nxt->link = NULL;
1992}
1993
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001994static bool io_kill_linked_timeout(struct io_kiocb *req)
1995 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06001996 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001997{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001998 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001999
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002000 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002001 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002002
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002003 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002004 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002005 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002006 list_del(&link->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002007 io_cqring_fill_event(link->ctx, link->user_data,
2008 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002009 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002010 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002011 }
2012 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002013 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002014}
2015
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002016static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002017 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002018{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002019 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06002020
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002021 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002022 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002023 long res = -ECANCELED;
2024
2025 if (link->flags & REQ_F_FAIL)
2026 res = link->result;
2027
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002028 nxt = link->link;
2029 link->link = NULL;
2030
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002031 trace_io_uring_fail_link(req, link);
Hao Xua8295b92021-08-27 17:46:09 +08002032 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002033 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002034 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002035 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002036}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002037
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002038static bool io_disarm_next(struct io_kiocb *req)
2039 __must_hold(&req->ctx->completion_lock)
2040{
2041 bool posted = false;
2042
Pavel Begunkov0756a862021-08-15 10:40:25 +01002043 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2044 struct io_kiocb *link = req->link;
2045
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002046 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002047 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2048 io_remove_next_linked(req);
2049 io_cqring_fill_event(link->ctx, link->user_data,
2050 -ECANCELED, 0);
2051 io_put_req_deferred(link);
2052 posted = true;
2053 }
2054 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002055 struct io_ring_ctx *ctx = req->ctx;
2056
2057 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002058 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002059 spin_unlock_irq(&ctx->timeout_lock);
2060 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002061 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002062 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002063 posted |= (req->link != NULL);
2064 io_fail_links(req);
2065 }
2066 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002067}
2068
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002069static void __io_req_find_next_prep(struct io_kiocb *req)
2070{
2071 struct io_ring_ctx *ctx = req->ctx;
2072 bool posted;
2073
2074 spin_lock(&ctx->completion_lock);
2075 posted = io_disarm_next(req);
2076 if (posted)
2077 io_commit_cqring(req->ctx);
2078 spin_unlock(&ctx->completion_lock);
2079 if (posted)
2080 io_cqring_ev_posted(ctx);
2081}
2082
2083static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002084{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002085 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002086
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002087 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2088 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002089 /*
2090 * If LINK is set, we have dependent requests in this chain. If we
2091 * didn't fail this request, queue the first one up, moving any other
2092 * dependencies to the next request. In case of failure, fail the rest
2093 * of the chain.
2094 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002095 if (unlikely(req->flags & IO_DISARM_MASK))
2096 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002097 nxt = req->link;
2098 req->link = NULL;
2099 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002100}
Jens Axboe2665abf2019-11-05 12:40:47 -07002101
Pavel Begunkovf237c302021-08-18 12:42:46 +01002102static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002103{
2104 if (!ctx)
2105 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002106 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002107 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002108 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002109 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002110 }
2111 percpu_ref_put(&ctx->refs);
2112}
2113
Jens Axboe7cbf1722021-02-10 00:03:20 +00002114static void tctx_task_work(struct callback_head *cb)
2115{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002116 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002117 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002118 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2119 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002120
Pavel Begunkov16f72072021-06-17 18:14:09 +01002121 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002122 struct io_wq_work_node *node;
2123
Pavel Begunkovc4501782021-09-08 16:40:52 +01002124 if (!tctx->task_list.first && locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002125 io_submit_flush_completions(ctx);
2126
Pavel Begunkov3f184072021-06-17 18:14:06 +01002127 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002128 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002129 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002130 if (!node)
2131 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002132 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002133 if (!node)
2134 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002135
Pavel Begunkov6294f362021-08-10 17:53:55 +01002136 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002137 struct io_wq_work_node *next = node->next;
2138 struct io_kiocb *req = container_of(node, struct io_kiocb,
2139 io_task_work.node);
2140
2141 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002142 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002143 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002144 /* if not contended, grab and improve batching */
2145 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002146 percpu_ref_get(&ctx->refs);
2147 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002148 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002149 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002150 } while (node);
2151
Jens Axboe7cbf1722021-02-10 00:03:20 +00002152 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002153 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002154
Pavel Begunkovf237c302021-08-18 12:42:46 +01002155 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002156}
2157
Pavel Begunkove09ee512021-07-01 13:26:05 +01002158static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002159{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002160 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002161 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002162 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002163 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002164 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002165 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002166
2167 WARN_ON_ONCE(!tctx);
2168
Jens Axboe0b81e802021-02-16 10:33:53 -07002169 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002170 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002171 running = tctx->task_running;
2172 if (!running)
2173 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002174 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002175
2176 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002177 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002178 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002179
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002180 /*
2181 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2182 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2183 * processing task_work. There's no reliable way to tell if TWA_RESUME
2184 * will do the job.
2185 */
2186 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002187 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2188 if (notify == TWA_NONE)
2189 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002190 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002191 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002192
Pavel Begunkove09ee512021-07-01 13:26:05 +01002193 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002194 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002195 node = tctx->task_list.first;
2196 INIT_WQ_LIST(&tctx->task_list);
2197 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002198
Pavel Begunkove09ee512021-07-01 13:26:05 +01002199 while (node) {
2200 req = container_of(node, struct io_kiocb, io_task_work.node);
2201 node = node->next;
2202 if (llist_add(&req->io_task_work.fallback_node,
2203 &req->ctx->fallback_llist))
2204 schedule_delayed_work(&req->ctx->fallback_work, 1);
2205 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002206}
2207
Pavel Begunkovf237c302021-08-18 12:42:46 +01002208static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002209{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002210 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002211
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002212 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002213 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002214 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002215}
2216
Pavel Begunkovf237c302021-08-18 12:42:46 +01002217static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002218{
2219 struct io_ring_ctx *ctx = req->ctx;
2220
Pavel Begunkovf237c302021-08-18 12:42:46 +01002221 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002222 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002223 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002224 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002225 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002226 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002227}
2228
Pavel Begunkova3df76982021-02-18 22:32:52 +00002229static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2230{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002231 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002232 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002233 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002234}
2235
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002236static void io_req_task_queue(struct io_kiocb *req)
2237{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002238 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002239 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002240}
2241
Jens Axboe773af692021-07-27 10:25:55 -06002242static void io_req_task_queue_reissue(struct io_kiocb *req)
2243{
2244 req->io_task_work.func = io_queue_async_work;
2245 io_req_task_work_add(req);
2246}
2247
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002248static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002249{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002250 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002251
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002252 if (nxt)
2253 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002254}
2255
Jens Axboe9e645e112019-05-10 16:07:28 -06002256static void io_free_req(struct io_kiocb *req)
2257{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002258 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002259 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002260}
2261
Pavel Begunkovf237c302021-08-18 12:42:46 +01002262static void io_free_req_work(struct io_kiocb *req, bool *locked)
2263{
2264 io_free_req(req);
2265}
2266
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002267struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002268 struct task_struct *task;
2269 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002270 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002271};
2272
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002273static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002274{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002275 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002276 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002277 rb->task = NULL;
2278}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002279
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002280static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2281 struct req_batch *rb)
2282{
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002283 if (rb->ctx_refs)
2284 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkove98e49b2021-08-18 17:01:43 +01002285 if (rb->task)
Pavel Begunkove9dbe222021-08-09 13:04:20 +01002286 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002287}
2288
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002289static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2290 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002291{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002292 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002293 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002294
Jens Axboee3bc8e92020-09-24 08:45:57 -06002295 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002296 if (rb->task)
2297 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002298 rb->task = req->task;
2299 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002300 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002301 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002302 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002303
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002304 list_add(&req->inflight_entry, &state->free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002305}
2306
Pavel Begunkovc4501782021-09-08 16:40:52 +01002307static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002308 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002309{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002310 struct io_wq_work_node *node, *prev;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002311 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002312 struct req_batch rb;
2313
Jens Axboe79ebeae2021-08-10 15:18:27 -06002314 spin_lock(&ctx->completion_lock);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002315 wq_list_for_each(node, prev, &state->compl_reqs) {
2316 struct io_kiocb *req = container_of(node, struct io_kiocb,
2317 comp_list);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002318
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002319 __io_cqring_fill_event(ctx, req->user_data, req->result,
2320 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002321 }
2322 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002323 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002324 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002325
2326 io_init_req_batch(&rb);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002327 node = state->compl_reqs.first;
2328 do {
2329 struct io_kiocb *req = container_of(node, struct io_kiocb,
2330 comp_list);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002331
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002332 node = req->comp_list.next;
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002333 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002334 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002335 } while (node);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002336
2337 io_req_free_batch_finish(ctx, &rb);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002338 INIT_WQ_LIST(&state->compl_reqs);
Jens Axboee65ef562019-03-12 10:16:44 -06002339}
2340
Jens Axboeba816ad2019-09-28 11:36:45 -06002341/*
2342 * Drop reference to request, return next in chain (if there is one) if this
2343 * was the last reference to this request.
2344 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002345static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002346{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002347 struct io_kiocb *nxt = NULL;
2348
Jens Axboede9b4cc2021-02-24 13:28:27 -07002349 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002350 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002351 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002352 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002353 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002354}
2355
Pavel Begunkov0d850352021-03-19 17:22:37 +00002356static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002357{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002358 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002359 io_free_req(req);
2360}
2361
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002362static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002363{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002364 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002365 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002366 io_req_task_work_add(req);
2367 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002368}
2369
Pavel Begunkov6c503152021-01-04 20:36:36 +00002370static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002371{
2372 /* See comment at the top of this file */
2373 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002374 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002375}
2376
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002377static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2378{
2379 struct io_rings *rings = ctx->rings;
2380
2381 /* make sure SQ entry isn't read before tail */
2382 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2383}
2384
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002385static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002386{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002387 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002388
Jens Axboebcda7ba2020-02-23 16:42:51 -07002389 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2390 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002391 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002392 kfree(kbuf);
2393 return cflags;
2394}
2395
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002396static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2397{
2398 struct io_buffer *kbuf;
2399
Pavel Begunkovae421d92021-08-17 20:28:08 +01002400 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2401 return 0;
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002402 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2403 return io_put_kbuf(req, kbuf);
2404}
2405
Jens Axboe4c6e2772020-07-01 11:29:10 -06002406static inline bool io_run_task_work(void)
2407{
Nadav Amitef98eb02021-08-07 17:13:41 -07002408 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002409 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002410 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002411 return true;
2412 }
2413
2414 return false;
2415}
2416
Jens Axboedef596e2019-01-09 08:59:42 -07002417/*
2418 * Find and free completed poll iocbs
2419 */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002420static void io_iopoll_complete(struct io_ring_ctx *ctx, struct list_head *done)
Jens Axboedef596e2019-01-09 08:59:42 -07002421{
Jens Axboe8237e042019-12-28 10:48:22 -07002422 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002423 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002424
2425 /* order with ->result store in io_complete_rw_iopoll() */
2426 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002427
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002428 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002429 while (!list_empty(done)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002430 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002431 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002432
Pavel Begunkovae421d92021-08-17 20:28:08 +01002433 __io_cqring_fill_event(ctx, req->user_data, req->result,
2434 io_put_rw_kbuf(req));
Jens Axboedef596e2019-01-09 08:59:42 -07002435
Jens Axboede9b4cc2021-02-24 13:28:27 -07002436 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002437 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002438 }
Jens Axboedef596e2019-01-09 08:59:42 -07002439
Jens Axboe09bb8392019-03-13 12:39:28 -06002440 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002441 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002442 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002443}
2444
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002445static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
Jens Axboedef596e2019-01-09 08:59:42 -07002446{
2447 struct io_kiocb *req, *tmp;
Christoph Hellwigd729cf92021-10-12 13:12:20 +02002448 unsigned int poll_flags = BLK_POLL_NOSLEEP;
Jens Axboeb688f112021-10-12 09:28:46 -06002449 DEFINE_IO_COMP_BATCH(iob);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002450 int nr_events = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002451 LIST_HEAD(done);
Jens Axboedef596e2019-01-09 08:59:42 -07002452
2453 /*
2454 * Only spin for completions if we don't have multiple devices hanging
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002455 * off our complete list.
Jens Axboedef596e2019-01-09 08:59:42 -07002456 */
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002457 if (ctx->poll_multi_queue || force_nonspin)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002458 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002459
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002460 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002461 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002462 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002463
2464 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002465 * Move completed and retryable entries to our local lists.
2466 * If we find a request that requires polling, break out
2467 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002468 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002469 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002470 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002471 nr_events++;
Jens Axboedef596e2019-01-09 08:59:42 -07002472 continue;
2473 }
2474 if (!list_empty(&done))
2475 break;
2476
Jens Axboeb688f112021-10-12 09:28:46 -06002477 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002478 if (unlikely(ret < 0))
2479 return ret;
2480 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002481 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002482
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002483 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002484 if (!rq_list_empty(iob.req_list) ||
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002485 READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002486 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002487 nr_events++;
2488 }
Jens Axboedef596e2019-01-09 08:59:42 -07002489 }
2490
Jens Axboeb688f112021-10-12 09:28:46 -06002491 if (!rq_list_empty(iob.req_list))
2492 iob.complete(&iob);
Jens Axboedef596e2019-01-09 08:59:42 -07002493 if (!list_empty(&done))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002494 io_iopoll_complete(ctx, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002495
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002496 return nr_events;
Jens Axboedef596e2019-01-09 08:59:42 -07002497}
2498
2499/*
Jens Axboedef596e2019-01-09 08:59:42 -07002500 * We can't just wait for polled events to come to us, we have to actively
2501 * find and complete them.
2502 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002503static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002504{
2505 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2506 return;
2507
2508 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002509 while (!list_empty(&ctx->iopoll_list)) {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002510 /* let it sleep and repeat later if can't complete a request */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002511 if (io_do_iopoll(ctx, true) == 0)
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002512 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002513 /*
2514 * Ensure we allow local-to-the-cpu processing to take place,
2515 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002516 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002517 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002518 if (need_resched()) {
2519 mutex_unlock(&ctx->uring_lock);
2520 cond_resched();
2521 mutex_lock(&ctx->uring_lock);
2522 }
Jens Axboedef596e2019-01-09 08:59:42 -07002523 }
2524 mutex_unlock(&ctx->uring_lock);
2525}
2526
Pavel Begunkov7668b922020-07-07 16:36:21 +03002527static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002528{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002529 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002530 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002531
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002532 /*
2533 * We disallow the app entering submit/complete with polling, but we
2534 * still need to lock the ring to prevent racing with polled issue
2535 * that got punted to a workqueue.
2536 */
2537 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002538 /*
2539 * Don't enter poll loop if we already have events pending.
2540 * If we do, we can potentially be spinning for commands that
2541 * already triggered a CQE (eg in error).
2542 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002543 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002544 __io_cqring_overflow_flush(ctx, false);
2545 if (io_cqring_events(ctx))
2546 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002547 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002548 /*
2549 * If a submit got punted to a workqueue, we can have the
2550 * application entering polling for a command before it gets
2551 * issued. That app will hold the uring_lock for the duration
2552 * of the poll right here, so we need to take a breather every
2553 * now and then to ensure that the issue has a chance to add
2554 * the poll to the issued list. Otherwise we can spin here
2555 * forever, while the workqueue is stuck trying to acquire the
2556 * very same mutex.
2557 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002558 if (list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002559 u32 tail = ctx->cached_cq_tail;
2560
Jens Axboe500f9fb2019-08-19 12:15:59 -06002561 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002562 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002563 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002564
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002565 /* some requests don't go through iopoll_list */
2566 if (tail != ctx->cached_cq_tail ||
2567 list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002568 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002569 }
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002570 ret = io_do_iopoll(ctx, !min);
2571 if (ret < 0)
2572 break;
2573 nr_events += ret;
2574 ret = 0;
2575 } while (nr_events < min && !need_resched());
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002576out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002577 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002578 return ret;
2579}
2580
Jens Axboe491381ce2019-10-17 09:20:46 -06002581static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002582{
Jens Axboe491381ce2019-10-17 09:20:46 -06002583 /*
2584 * Tell lockdep we inherited freeze protection from submission
2585 * thread.
2586 */
2587 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002588 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002589
Pavel Begunkov1c986792021-03-22 01:58:31 +00002590 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2591 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002592 }
2593}
2594
Jens Axboeb63534c2020-06-04 11:28:00 -06002595#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002596static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002597{
Pavel Begunkovab454432021-03-22 01:58:33 +00002598 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002599
Pavel Begunkovab454432021-03-22 01:58:33 +00002600 if (!rw)
2601 return !io_req_prep_async(req);
Jens Axboecd658692021-09-10 11:19:14 -06002602 iov_iter_restore(&rw->iter, &rw->iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002603 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002604}
Jens Axboeb63534c2020-06-04 11:28:00 -06002605
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002606static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002607{
Jens Axboe355afae2020-09-02 09:30:31 -06002608 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002609 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002610
Jens Axboe355afae2020-09-02 09:30:31 -06002611 if (!S_ISBLK(mode) && !S_ISREG(mode))
2612 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002613 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2614 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002615 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002616 /*
2617 * If ref is dying, we might be running poll reap from the exit work.
2618 * Don't attempt to reissue from that path, just let it fail with
2619 * -EAGAIN.
2620 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002621 if (percpu_ref_is_dying(&ctx->refs))
2622 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002623 /*
2624 * Play it safe and assume not safe to re-import and reissue if we're
2625 * not in the original thread group (or in task context).
2626 */
2627 if (!same_thread_group(req->task, current) || !in_task())
2628 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002629 return true;
2630}
Jens Axboee82ad482021-04-02 19:45:34 -06002631#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002632static bool io_resubmit_prep(struct io_kiocb *req)
2633{
2634 return false;
2635}
Jens Axboee82ad482021-04-02 19:45:34 -06002636static bool io_rw_should_reissue(struct io_kiocb *req)
2637{
2638 return false;
2639}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002640#endif
2641
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002642static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002643{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002644 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2645 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002646 if (res != req->result) {
2647 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2648 io_rw_should_reissue(req)) {
2649 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002650 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002651 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002652 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002653 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002654 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002655 return false;
2656}
2657
Pavel Begunkovf237c302021-08-18 12:42:46 +01002658static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002659{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002660 unsigned int cflags = io_put_rw_kbuf(req);
2661 long res = req->result;
2662
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002663 if (*locked)
Pavel Begunkov126180b2021-08-18 12:42:47 +01002664 io_req_complete_state(req, res, cflags);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002665 else
Pavel Begunkov126180b2021-08-18 12:42:47 +01002666 io_req_complete_post(req, res, cflags);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002667}
2668
2669static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2670 unsigned int issue_flags)
2671{
2672 if (__io_complete_rw_common(req, res))
2673 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002674 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002675}
2676
2677static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2678{
Jens Axboe9adbd452019-12-20 08:45:55 -07002679 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002680
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002681 if (__io_complete_rw_common(req, res))
2682 return;
2683 req->result = res;
2684 req->io_task_work.func = io_req_task_complete;
2685 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002686}
2687
Jens Axboedef596e2019-01-09 08:59:42 -07002688static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2689{
Jens Axboe9adbd452019-12-20 08:45:55 -07002690 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002691
Jens Axboe491381ce2019-10-17 09:20:46 -06002692 if (kiocb->ki_flags & IOCB_WRITE)
2693 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002694 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002695 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2696 req->flags |= REQ_F_REISSUE;
2697 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002698 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002699 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002700
2701 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002702 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002703 smp_wmb();
2704 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002705}
2706
2707/*
2708 * After the iocb has been issued, it's safe to be found on the poll list.
2709 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002710 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002711 * accessing the kiocb cookie.
2712 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002713static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002714{
2715 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002716 const bool in_async = io_wq_current_is_worker();
2717
2718 /* workqueue context doesn't hold uring_lock, grab it now */
2719 if (unlikely(in_async))
2720 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002721
2722 /*
2723 * Track whether we have multiple files in our lists. This will impact
2724 * how we do polling eventually, not spinning if we're on potentially
2725 * different devices.
2726 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002727 if (list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002728 ctx->poll_multi_queue = false;
2729 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002730 struct io_kiocb *list_req;
2731
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002732 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002733 inflight_entry);
Hao Xu915b3dd2021-06-28 05:37:30 +08002734
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002735 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002736 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002737 }
2738
2739 /*
2740 * For fast devices, IO may have already completed. If it has, add
2741 * it to the front so we find it first.
2742 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002743 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002744 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002745 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002746 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002747
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002748 if (unlikely(in_async)) {
2749 /*
2750 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2751 * in sq thread task context or in io worker task context. If
2752 * current task context is sq thread, we don't need to check
2753 * whether should wake up sq thread.
2754 */
2755 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2756 wq_has_sleeper(&ctx->sq_data->wait))
2757 wake_up(&ctx->sq_data->wait);
2758
2759 mutex_unlock(&ctx->uring_lock);
2760 }
Jens Axboedef596e2019-01-09 08:59:42 -07002761}
2762
Jens Axboe4503b762020-06-01 10:00:27 -06002763static bool io_bdev_nowait(struct block_device *bdev)
2764{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002765 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002766}
2767
Jens Axboe2b188cc2019-01-07 10:46:33 -07002768/*
2769 * If we tracked the file through the SCM inflight mechanism, we could support
2770 * any file. For now, just ensure that anything potentially problematic is done
2771 * inline.
2772 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002773static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002774{
2775 umode_t mode = file_inode(file)->i_mode;
2776
Jens Axboe4503b762020-06-01 10:00:27 -06002777 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002778 if (IS_ENABLED(CONFIG_BLOCK) &&
2779 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002780 return true;
2781 return false;
2782 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002783 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002784 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002785 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002786 if (IS_ENABLED(CONFIG_BLOCK) &&
2787 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002788 file->f_op != &io_uring_fops)
2789 return true;
2790 return false;
2791 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002792
Jens Axboec5b85622020-06-09 19:23:05 -06002793 /* any ->read/write should understand O_NONBLOCK */
2794 if (file->f_flags & O_NONBLOCK)
2795 return true;
2796
Jens Axboeaf197f52020-04-28 13:15:06 -06002797 if (!(file->f_mode & FMODE_NOWAIT))
2798 return false;
2799
2800 if (rw == READ)
2801 return file->f_op->read_iter != NULL;
2802
2803 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002804}
2805
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002806static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002807{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002808 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002809 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002810 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002811 return true;
2812
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002813 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002814}
2815
Jens Axboe5d329e12021-09-14 11:08:37 -06002816static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2817 int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002818{
Jens Axboedef596e2019-01-09 08:59:42 -07002819 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002820 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002821 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002822 unsigned ioprio;
2823 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002824
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002825 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002826 req->flags |= REQ_F_ISREG;
2827
Jens Axboe2b188cc2019-01-07 10:46:33 -07002828 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002829 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002830 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002831 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002832 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002833 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002834 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2835 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2836 if (unlikely(ret))
2837 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002838
Jens Axboe5d329e12021-09-14 11:08:37 -06002839 /*
2840 * If the file is marked O_NONBLOCK, still allow retry for it if it
2841 * supports async. Otherwise it's impossible to use O_NONBLOCK files
2842 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2843 */
2844 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2845 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002846 req->flags |= REQ_F_NOWAIT;
2847
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848 ioprio = READ_ONCE(sqe->ioprio);
2849 if (ioprio) {
2850 ret = ioprio_check_cap(ioprio);
2851 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002852 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002853
2854 kiocb->ki_ioprio = ioprio;
2855 } else
2856 kiocb->ki_ioprio = get_current_ioprio();
2857
Jens Axboedef596e2019-01-09 08:59:42 -07002858 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002859 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2860 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002861 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002862
Jens Axboe394918e2021-03-08 11:40:23 -07002863 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002864 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002865 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002866 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002867 if (kiocb->ki_flags & IOCB_HIPRI)
2868 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002869 kiocb->ki_complete = io_complete_rw;
2870 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002871
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002872 if (req->opcode == IORING_OP_READ_FIXED ||
2873 req->opcode == IORING_OP_WRITE_FIXED) {
2874 req->imu = NULL;
2875 io_req_set_rsrc_node(req);
2876 }
2877
Jens Axboe3529d8c2019-12-19 18:24:38 -07002878 req->rw.addr = READ_ONCE(sqe->addr);
2879 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002880 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002881 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882}
2883
2884static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2885{
2886 switch (ret) {
2887 case -EIOCBQUEUED:
2888 break;
2889 case -ERESTARTSYS:
2890 case -ERESTARTNOINTR:
2891 case -ERESTARTNOHAND:
2892 case -ERESTART_RESTARTBLOCK:
2893 /*
2894 * We can't just restart the syscall, since previously
2895 * submitted sqes may already be in progress. Just fail this
2896 * IO with EINTR.
2897 */
2898 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002899 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002900 default:
2901 kiocb->ki_complete(kiocb, ret, 0);
2902 }
2903}
2904
Jens Axboea1d7c392020-06-22 11:09:46 -06002905static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002906 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002907{
Jens Axboeba042912019-12-25 16:33:42 -07002908 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002909 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002910
Jens Axboe227c0c92020-08-13 11:51:40 -06002911 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002912 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002913 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002914 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002915 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002916 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002917 }
2918
Jens Axboeba042912019-12-25 16:33:42 -07002919 if (req->flags & REQ_F_CUR_POS)
2920 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002921 if (ret >= 0 && (kiocb->ki_complete == io_complete_rw))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002922 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002923 else
2924 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002925
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002926 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002927 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002928 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002929 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002930 } else {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002931 unsigned int cflags = io_put_rw_kbuf(req);
2932 struct io_ring_ctx *ctx = req->ctx;
2933
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002934 req_set_fail(req);
Hao Xu14cfbb72021-10-14 22:04:00 +08002935 if (!(issue_flags & IO_URING_F_NONBLOCK)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002936 mutex_lock(&ctx->uring_lock);
2937 __io_req_complete(req, issue_flags, ret, cflags);
2938 mutex_unlock(&ctx->uring_lock);
2939 } else {
2940 __io_req_complete(req, issue_flags, ret, cflags);
2941 }
Pavel Begunkov97284632021-04-08 19:28:03 +01002942 }
2943 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002944}
2945
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002946static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2947 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002948{
Jens Axboe9adbd452019-12-20 08:45:55 -07002949 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002950 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002951 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002952
Pavel Begunkov75769e32021-04-01 15:43:54 +01002953 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002954 return -EFAULT;
2955 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002956 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002957 return -EFAULT;
2958
2959 /*
2960 * May not be a start of buffer, set size appropriately
2961 * and advance us to the beginning.
2962 */
2963 offset = buf_addr - imu->ubuf;
2964 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002965
2966 if (offset) {
2967 /*
2968 * Don't use iov_iter_advance() here, as it's really slow for
2969 * using the latter parts of a big fixed buffer - it iterates
2970 * over each segment manually. We can cheat a bit here, because
2971 * we know that:
2972 *
2973 * 1) it's a BVEC iter, we set it up
2974 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2975 * first and last bvec
2976 *
2977 * So just find our index, and adjust the iterator afterwards.
2978 * If the offset is within the first bvec (or the whole first
2979 * bvec, just use iov_iter_advance(). This makes it easier
2980 * since we can just skip the first segment, which may not
2981 * be PAGE_SIZE aligned.
2982 */
2983 const struct bio_vec *bvec = imu->bvec;
2984
2985 if (offset <= bvec->bv_len) {
2986 iov_iter_advance(iter, offset);
2987 } else {
2988 unsigned long seg_skip;
2989
2990 /* skip first vec */
2991 offset -= bvec->bv_len;
2992 seg_skip = 1 + (offset >> PAGE_SHIFT);
2993
2994 iter->bvec = bvec + seg_skip;
2995 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002996 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002997 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002998 }
2999 }
3000
Pavel Begunkov847595d2021-02-04 13:52:06 +00003001 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003002}
3003
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003004static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3005{
3006 struct io_ring_ctx *ctx = req->ctx;
3007 struct io_mapped_ubuf *imu = req->imu;
3008 u16 index, buf_index = req->buf_index;
3009
3010 if (likely(!imu)) {
3011 if (unlikely(buf_index >= ctx->nr_user_bufs))
3012 return -EFAULT;
3013 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3014 imu = READ_ONCE(ctx->user_bufs[index]);
3015 req->imu = imu;
3016 }
3017 return __io_import_fixed(req, rw, iter, imu);
3018}
3019
Jens Axboebcda7ba2020-02-23 16:42:51 -07003020static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3021{
3022 if (needs_lock)
3023 mutex_unlock(&ctx->uring_lock);
3024}
3025
3026static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3027{
3028 /*
3029 * "Normal" inline submissions always hold the uring_lock, since we
3030 * grab it from the system call. Same is true for the SQPOLL offload.
3031 * The only exception is when we've detached the request and issue it
3032 * from an async worker thread, grab the lock for that case.
3033 */
3034 if (needs_lock)
3035 mutex_lock(&ctx->uring_lock);
3036}
3037
3038static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3039 int bgid, struct io_buffer *kbuf,
3040 bool needs_lock)
3041{
3042 struct io_buffer *head;
3043
3044 if (req->flags & REQ_F_BUFFER_SELECTED)
3045 return kbuf;
3046
3047 io_ring_submit_lock(req->ctx, needs_lock);
3048
3049 lockdep_assert_held(&req->ctx->uring_lock);
3050
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003051 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003052 if (head) {
3053 if (!list_empty(&head->list)) {
3054 kbuf = list_last_entry(&head->list, struct io_buffer,
3055 list);
3056 list_del(&kbuf->list);
3057 } else {
3058 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003059 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003060 }
3061 if (*len > kbuf->len)
3062 *len = kbuf->len;
3063 } else {
3064 kbuf = ERR_PTR(-ENOBUFS);
3065 }
3066
3067 io_ring_submit_unlock(req->ctx, needs_lock);
3068
3069 return kbuf;
3070}
3071
Jens Axboe4d954c22020-02-27 07:31:19 -07003072static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3073 bool needs_lock)
3074{
3075 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003076 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003077
3078 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003079 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003080 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3081 if (IS_ERR(kbuf))
3082 return kbuf;
3083 req->rw.addr = (u64) (unsigned long) kbuf;
3084 req->flags |= REQ_F_BUFFER_SELECTED;
3085 return u64_to_user_ptr(kbuf->addr);
3086}
3087
3088#ifdef CONFIG_COMPAT
3089static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3090 bool needs_lock)
3091{
3092 struct compat_iovec __user *uiov;
3093 compat_ssize_t clen;
3094 void __user *buf;
3095 ssize_t len;
3096
3097 uiov = u64_to_user_ptr(req->rw.addr);
3098 if (!access_ok(uiov, sizeof(*uiov)))
3099 return -EFAULT;
3100 if (__get_user(clen, &uiov->iov_len))
3101 return -EFAULT;
3102 if (clen < 0)
3103 return -EINVAL;
3104
3105 len = clen;
3106 buf = io_rw_buffer_select(req, &len, needs_lock);
3107 if (IS_ERR(buf))
3108 return PTR_ERR(buf);
3109 iov[0].iov_base = buf;
3110 iov[0].iov_len = (compat_size_t) len;
3111 return 0;
3112}
3113#endif
3114
3115static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3116 bool needs_lock)
3117{
3118 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3119 void __user *buf;
3120 ssize_t len;
3121
3122 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3123 return -EFAULT;
3124
3125 len = iov[0].iov_len;
3126 if (len < 0)
3127 return -EINVAL;
3128 buf = io_rw_buffer_select(req, &len, needs_lock);
3129 if (IS_ERR(buf))
3130 return PTR_ERR(buf);
3131 iov[0].iov_base = buf;
3132 iov[0].iov_len = len;
3133 return 0;
3134}
3135
3136static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3137 bool needs_lock)
3138{
Jens Axboedddb3e22020-06-04 11:27:01 -06003139 if (req->flags & REQ_F_BUFFER_SELECTED) {
3140 struct io_buffer *kbuf;
3141
3142 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3143 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3144 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003145 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003146 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003147 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003148 return -EINVAL;
3149
3150#ifdef CONFIG_COMPAT
3151 if (req->ctx->compat)
3152 return io_compat_import(req, iov, needs_lock);
3153#endif
3154
3155 return __io_iov_buffer_select(req, iov, needs_lock);
3156}
3157
Pavel Begunkov847595d2021-02-04 13:52:06 +00003158static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3159 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003160{
Jens Axboe9adbd452019-12-20 08:45:55 -07003161 void __user *buf = u64_to_user_ptr(req->rw.addr);
3162 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003163 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003164 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003165
Pavel Begunkov7d009162019-11-25 23:14:40 +03003166 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003167 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003168 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003169 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003170
Jens Axboebcda7ba2020-02-23 16:42:51 -07003171 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003172 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003173 return -EINVAL;
3174
Jens Axboe3a6820f2019-12-22 15:19:35 -07003175 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003176 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003177 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003178 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003179 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003180 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003181 }
3182
Jens Axboe3a6820f2019-12-22 15:19:35 -07003183 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3184 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003185 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003186 }
3187
Jens Axboe4d954c22020-02-27 07:31:19 -07003188 if (req->flags & REQ_F_BUFFER_SELECT) {
3189 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003190 if (!ret)
3191 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003192 *iovec = NULL;
3193 return ret;
3194 }
3195
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003196 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3197 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003198}
3199
Jens Axboe0fef9482020-08-26 10:36:20 -06003200static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3201{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003202 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003203}
3204
Jens Axboe32960612019-09-23 11:05:34 -06003205/*
3206 * For files that don't have ->read_iter() and ->write_iter(), handle them
3207 * by looping over ->read() or ->write() manually.
3208 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003209static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003210{
Jens Axboe4017eb92020-10-22 14:14:12 -06003211 struct kiocb *kiocb = &req->rw.kiocb;
3212 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003213 ssize_t ret = 0;
3214
3215 /*
3216 * Don't support polled IO through this interface, and we can't
3217 * support non-blocking either. For the latter, this just causes
3218 * the kiocb to be handled from an async context.
3219 */
3220 if (kiocb->ki_flags & IOCB_HIPRI)
3221 return -EOPNOTSUPP;
3222 if (kiocb->ki_flags & IOCB_NOWAIT)
3223 return -EAGAIN;
3224
3225 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003226 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003227 ssize_t nr;
3228
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003229 if (!iov_iter_is_bvec(iter)) {
3230 iovec = iov_iter_iovec(iter);
3231 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003232 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3233 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003234 }
3235
Jens Axboe32960612019-09-23 11:05:34 -06003236 if (rw == READ) {
3237 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003238 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003239 } else {
3240 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003241 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003242 }
3243
3244 if (nr < 0) {
3245 if (!ret)
3246 ret = nr;
3247 break;
3248 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003249 if (!iov_iter_is_bvec(iter)) {
3250 iov_iter_advance(iter, nr);
3251 } else {
3252 req->rw.len -= nr;
3253 req->rw.addr += nr;
3254 }
Jens Axboe32960612019-09-23 11:05:34 -06003255 ret += nr;
3256 if (nr != iovec.iov_len)
3257 break;
Jens Axboe32960612019-09-23 11:05:34 -06003258 }
3259
3260 return ret;
3261}
3262
Jens Axboeff6165b2020-08-13 09:47:43 -06003263static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3264 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003265{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003266 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003267
Jens Axboeff6165b2020-08-13 09:47:43 -06003268 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003269 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003270 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003271 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003272 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003273 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003274 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003275 unsigned iov_off = 0;
3276
3277 rw->iter.iov = rw->fast_iov;
3278 if (iter->iov != fast_iov) {
3279 iov_off = iter->iov - fast_iov;
3280 rw->iter.iov += iov_off;
3281 }
3282 if (rw->fast_iov != fast_iov)
3283 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003284 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003285 } else {
3286 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003287 }
3288}
3289
Hao Xu8d4af682021-09-22 18:15:22 +08003290static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003291{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003292 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3293 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3294 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003295}
3296
Jens Axboeff6165b2020-08-13 09:47:43 -06003297static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3298 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003299 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003300{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003301 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003302 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003303 if (!req->async_data) {
Jens Axboecd658692021-09-10 11:19:14 -06003304 struct io_async_rw *iorw;
3305
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003306 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003307 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003308 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003309 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003310
Jens Axboeff6165b2020-08-13 09:47:43 -06003311 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboecd658692021-09-10 11:19:14 -06003312 iorw = req->async_data;
3313 /* we've copied and mapped the iter, ensure state is saved */
3314 iov_iter_save_state(&iorw->iter, &iorw->iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003315 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003316 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003317}
3318
Pavel Begunkov73debe62020-09-30 22:57:54 +03003319static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003320{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003321 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003322 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003323 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003324
Pavel Begunkov2846c482020-11-07 13:16:27 +00003325 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003326 if (unlikely(ret < 0))
3327 return ret;
3328
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003329 iorw->bytes_done = 0;
3330 iorw->free_iovec = iov;
3331 if (iov)
3332 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecd658692021-09-10 11:19:14 -06003333 iov_iter_save_state(&iorw->iter, &iorw->iter_state);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003334 return 0;
3335}
3336
Pavel Begunkov73debe62020-09-30 22:57:54 +03003337static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003338{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003339 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3340 return -EBADF;
Jens Axboe5d329e12021-09-14 11:08:37 -06003341 return io_prep_rw(req, sqe, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003342}
3343
Jens Axboec1dd91d2020-08-03 16:43:59 -06003344/*
3345 * This is our waitqueue callback handler, registered through lock_page_async()
3346 * when we initially tried to do the IO with the iocb armed our waitqueue.
3347 * This gets called when the page is unlocked, and we generally expect that to
3348 * happen when the page IO is completed and the page is now uptodate. This will
3349 * queue a task_work based retry of the operation, attempting to copy the data
3350 * again. If the latter fails because the page was NOT uptodate, then we will
3351 * do a thread based blocking retry of the operation. That's the unexpected
3352 * slow path.
3353 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003354static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3355 int sync, void *arg)
3356{
3357 struct wait_page_queue *wpq;
3358 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003359 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003360
3361 wpq = container_of(wait, struct wait_page_queue, wait);
3362
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003363 if (!wake_page_match(wpq, key))
3364 return 0;
3365
Hao Xuc8d317a2020-09-29 20:00:45 +08003366 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003367 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003368 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003369 return 1;
3370}
3371
Jens Axboec1dd91d2020-08-03 16:43:59 -06003372/*
3373 * This controls whether a given IO request should be armed for async page
3374 * based retry. If we return false here, the request is handed to the async
3375 * worker threads for retry. If we're doing buffered reads on a regular file,
3376 * we prepare a private wait_page_queue entry and retry the operation. This
3377 * will either succeed because the page is now uptodate and unlocked, or it
3378 * will register a callback when the page is unlocked at IO completion. Through
3379 * that callback, io_uring uses task_work to setup a retry of the operation.
3380 * That retry will attempt the buffered read again. The retry will generally
3381 * succeed, or in rare cases where it fails, we then fall back to using the
3382 * async worker threads for a blocking retry.
3383 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003384static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003385{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003386 struct io_async_rw *rw = req->async_data;
3387 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003388 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003389
3390 /* never retry for NOWAIT, we just complete with -EAGAIN */
3391 if (req->flags & REQ_F_NOWAIT)
3392 return false;
3393
Jens Axboe227c0c92020-08-13 11:51:40 -06003394 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003395 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003396 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003397
Jens Axboebcf5a062020-05-22 09:24:42 -06003398 /*
3399 * just use poll if we can, and don't attempt if the fs doesn't
3400 * support callback based unlocks
3401 */
3402 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3403 return false;
3404
Jens Axboe3b2a4432020-08-16 10:58:43 -07003405 wait->wait.func = io_async_buf_func;
3406 wait->wait.private = req;
3407 wait->wait.flags = 0;
3408 INIT_LIST_HEAD(&wait->wait.entry);
3409 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003410 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003411 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003412 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003413}
3414
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003415static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003416{
3417 if (req->file->f_op->read_iter)
3418 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003419 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003420 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003421 else
3422 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003423}
3424
Ming Lei7db30432021-08-21 23:07:51 +08003425static bool need_read_all(struct io_kiocb *req)
3426{
3427 return req->flags & REQ_F_ISREG ||
3428 S_ISBLK(file_inode(req->file)->i_mode);
3429}
3430
Pavel Begunkov889fca72021-02-10 00:03:09 +00003431static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003432{
3433 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003434 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003435 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003436 struct io_async_rw *rw = req->async_data;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003437 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003438 struct iov_iter_state __state, *state;
3439 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003440
Pavel Begunkov2846c482020-11-07 13:16:27 +00003441 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003442 iter = &rw->iter;
Jens Axboecd658692021-09-10 11:19:14 -06003443 state = &rw->iter_state;
3444 /*
3445 * We come here from an earlier attempt, restore our state to
3446 * match in case it doesn't. It's cheap enough that we don't
3447 * need to make this conditional.
3448 */
3449 iov_iter_restore(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003450 iovec = NULL;
3451 } else {
3452 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3453 if (ret < 0)
3454 return ret;
Jens Axboecd658692021-09-10 11:19:14 -06003455 state = &__state;
3456 iov_iter_save_state(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003457 }
Jens Axboecd658692021-09-10 11:19:14 -06003458 req->result = iov_iter_count(iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003459
Jens Axboefd6c2e42019-12-18 12:19:41 -07003460 /* Ensure we clear previously set non-block flag */
3461 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003462 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003463 else
3464 kiocb->ki_flags |= IOCB_NOWAIT;
3465
Pavel Begunkov24c74672020-06-21 13:09:51 +03003466 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003467 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003468 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003469 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003470 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003471
Jens Axboecd658692021-09-10 11:19:14 -06003472 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003473 if (unlikely(ret)) {
3474 kfree(iovec);
3475 return ret;
3476 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003477
Jens Axboe227c0c92020-08-13 11:51:40 -06003478 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003479
Jens Axboe230d50d2021-04-01 20:41:15 -06003480 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003481 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003482 /* IOPOLL retry should happen for io-wq threads */
3483 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003484 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003485 /* no retry on NONBLOCK nor RWF_NOWAIT */
3486 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003487 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003488 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003489 } else if (ret == -EIOCBQUEUED) {
3490 goto out_free;
Jens Axboecd658692021-09-10 11:19:14 -06003491 } else if (ret <= 0 || ret == req->result || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003492 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003493 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003494 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003495 }
3496
Jens Axboecd658692021-09-10 11:19:14 -06003497 /*
3498 * Don't depend on the iter state matching what was consumed, or being
3499 * untouched in case of error. Restore it and we'll advance it
3500 * manually if we need to.
3501 */
3502 iov_iter_restore(iter, state);
3503
Jens Axboe227c0c92020-08-13 11:51:40 -06003504 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003505 if (ret2)
3506 return ret2;
3507
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003508 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003509 rw = req->async_data;
Jens Axboecd658692021-09-10 11:19:14 -06003510 /*
3511 * Now use our persistent iterator and state, if we aren't already.
3512 * We've restored and mapped the iter to match.
3513 */
3514 if (iter != &rw->iter) {
3515 iter = &rw->iter;
3516 state = &rw->iter_state;
3517 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003518
Pavel Begunkovb23df912021-02-04 13:52:04 +00003519 do {
Jens Axboecd658692021-09-10 11:19:14 -06003520 /*
3521 * We end up here because of a partial read, either from
3522 * above or inside this loop. Advance the iter by the bytes
3523 * that were consumed.
3524 */
3525 iov_iter_advance(iter, ret);
3526 if (!iov_iter_count(iter))
3527 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003528 rw->bytes_done += ret;
Jens Axboecd658692021-09-10 11:19:14 -06003529 iov_iter_save_state(iter, state);
3530
Pavel Begunkovb23df912021-02-04 13:52:04 +00003531 /* if we can retry, do so with the callbacks armed */
3532 if (!io_rw_should_retry(req)) {
3533 kiocb->ki_flags &= ~IOCB_WAITQ;
3534 return -EAGAIN;
3535 }
3536
3537 /*
3538 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3539 * we get -EIOCBQUEUED, then we'll get a notification when the
3540 * desired page gets unlocked. We can also get a partial read
3541 * here, and if we do, then just retry at the new offset.
3542 */
3543 ret = io_iter_do_read(req, iter);
3544 if (ret == -EIOCBQUEUED)
3545 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003546 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003547 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboecd658692021-09-10 11:19:14 -06003548 iov_iter_restore(iter, state);
3549 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003550done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003551 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003552out_free:
3553 /* it's faster to check here then delegate to kfree */
3554 if (iovec)
3555 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003556 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003557}
3558
Pavel Begunkov73debe62020-09-30 22:57:54 +03003559static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003560{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003561 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3562 return -EBADF;
Jens Axboe5d329e12021-09-14 11:08:37 -06003563 return io_prep_rw(req, sqe, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003564}
3565
Pavel Begunkov889fca72021-02-10 00:03:09 +00003566static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003567{
3568 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003569 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003570 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003571 struct io_async_rw *rw = req->async_data;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003572 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003573 struct iov_iter_state __state, *state;
3574 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003575
Pavel Begunkov2846c482020-11-07 13:16:27 +00003576 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003577 iter = &rw->iter;
Jens Axboecd658692021-09-10 11:19:14 -06003578 state = &rw->iter_state;
3579 iov_iter_restore(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003580 iovec = NULL;
3581 } else {
3582 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3583 if (ret < 0)
3584 return ret;
Jens Axboecd658692021-09-10 11:19:14 -06003585 state = &__state;
3586 iov_iter_save_state(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003587 }
Jens Axboecd658692021-09-10 11:19:14 -06003588 req->result = iov_iter_count(iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003589
Jens Axboefd6c2e42019-12-18 12:19:41 -07003590 /* Ensure we clear previously set non-block flag */
3591 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003592 kiocb->ki_flags &= ~IOCB_NOWAIT;
3593 else
3594 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003595
Pavel Begunkov24c74672020-06-21 13:09:51 +03003596 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003597 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003598 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003599
Jens Axboe10d59342019-12-09 20:16:22 -07003600 /* file path doesn't support NOWAIT for non-direct_IO */
3601 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3602 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003603 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003604
Jens Axboecd658692021-09-10 11:19:14 -06003605 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003606 if (unlikely(ret))
3607 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003608
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003609 /*
3610 * Open-code file_start_write here to grab freeze protection,
3611 * which will be released by another thread in
3612 * io_complete_rw(). Fool lockdep by telling it the lock got
3613 * released so that it doesn't complain about the held lock when
3614 * we return to userspace.
3615 */
3616 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003617 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003618 __sb_writers_release(file_inode(req->file)->i_sb,
3619 SB_FREEZE_WRITE);
3620 }
3621 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003622
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003623 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003624 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003625 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003626 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003627 else
3628 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003629
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003630 if (req->flags & REQ_F_REISSUE) {
3631 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003632 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003633 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003634
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003635 /*
3636 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3637 * retry them without IOCB_NOWAIT.
3638 */
3639 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3640 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003641 /* no retry on NONBLOCK nor RWF_NOWAIT */
3642 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003643 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003644 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003645 /* IOPOLL retry should happen for io-wq threads */
3646 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3647 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003648done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003649 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003650 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003651copy_iov:
Jens Axboecd658692021-09-10 11:19:14 -06003652 iov_iter_restore(iter, state);
Jens Axboe227c0c92020-08-13 11:51:40 -06003653 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003654 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003655 }
Jens Axboe31b51512019-01-18 22:56:34 -07003656out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003657 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003658 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003659 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003660 return ret;
3661}
3662
Jens Axboe80a261f2020-09-28 14:23:58 -06003663static int io_renameat_prep(struct io_kiocb *req,
3664 const struct io_uring_sqe *sqe)
3665{
3666 struct io_rename *ren = &req->rename;
3667 const char __user *oldf, *newf;
3668
Jens Axboeed7eb252021-06-23 09:04:13 -06003669 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3670 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003671 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003672 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003673 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3674 return -EBADF;
3675
3676 ren->old_dfd = READ_ONCE(sqe->fd);
3677 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3678 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3679 ren->new_dfd = READ_ONCE(sqe->len);
3680 ren->flags = READ_ONCE(sqe->rename_flags);
3681
3682 ren->oldpath = getname(oldf);
3683 if (IS_ERR(ren->oldpath))
3684 return PTR_ERR(ren->oldpath);
3685
3686 ren->newpath = getname(newf);
3687 if (IS_ERR(ren->newpath)) {
3688 putname(ren->oldpath);
3689 return PTR_ERR(ren->newpath);
3690 }
3691
3692 req->flags |= REQ_F_NEED_CLEANUP;
3693 return 0;
3694}
3695
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003696static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003697{
3698 struct io_rename *ren = &req->rename;
3699 int ret;
3700
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003701 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003702 return -EAGAIN;
3703
3704 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3705 ren->newpath, ren->flags);
3706
3707 req->flags &= ~REQ_F_NEED_CLEANUP;
3708 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003709 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003710 io_req_complete(req, ret);
3711 return 0;
3712}
3713
Jens Axboe14a11432020-09-28 14:27:37 -06003714static int io_unlinkat_prep(struct io_kiocb *req,
3715 const struct io_uring_sqe *sqe)
3716{
3717 struct io_unlink *un = &req->unlink;
3718 const char __user *fname;
3719
Jens Axboe22634bc2021-06-23 09:07:45 -06003720 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3721 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003722 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3723 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003724 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003725 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3726 return -EBADF;
3727
3728 un->dfd = READ_ONCE(sqe->fd);
3729
3730 un->flags = READ_ONCE(sqe->unlink_flags);
3731 if (un->flags & ~AT_REMOVEDIR)
3732 return -EINVAL;
3733
3734 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3735 un->filename = getname(fname);
3736 if (IS_ERR(un->filename))
3737 return PTR_ERR(un->filename);
3738
3739 req->flags |= REQ_F_NEED_CLEANUP;
3740 return 0;
3741}
3742
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003743static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003744{
3745 struct io_unlink *un = &req->unlink;
3746 int ret;
3747
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003748 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003749 return -EAGAIN;
3750
3751 if (un->flags & AT_REMOVEDIR)
3752 ret = do_rmdir(un->dfd, un->filename);
3753 else
3754 ret = do_unlinkat(un->dfd, un->filename);
3755
3756 req->flags &= ~REQ_F_NEED_CLEANUP;
3757 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003758 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003759 io_req_complete(req, ret);
3760 return 0;
3761}
3762
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003763static int io_mkdirat_prep(struct io_kiocb *req,
3764 const struct io_uring_sqe *sqe)
3765{
3766 struct io_mkdir *mkd = &req->mkdir;
3767 const char __user *fname;
3768
3769 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3770 return -EINVAL;
3771 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3772 sqe->splice_fd_in)
3773 return -EINVAL;
3774 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3775 return -EBADF;
3776
3777 mkd->dfd = READ_ONCE(sqe->fd);
3778 mkd->mode = READ_ONCE(sqe->len);
3779
3780 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3781 mkd->filename = getname(fname);
3782 if (IS_ERR(mkd->filename))
3783 return PTR_ERR(mkd->filename);
3784
3785 req->flags |= REQ_F_NEED_CLEANUP;
3786 return 0;
3787}
3788
3789static int io_mkdirat(struct io_kiocb *req, int issue_flags)
3790{
3791 struct io_mkdir *mkd = &req->mkdir;
3792 int ret;
3793
3794 if (issue_flags & IO_URING_F_NONBLOCK)
3795 return -EAGAIN;
3796
3797 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3798
3799 req->flags &= ~REQ_F_NEED_CLEANUP;
3800 if (ret < 0)
3801 req_set_fail(req);
3802 io_req_complete(req, ret);
3803 return 0;
3804}
3805
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003806static int io_symlinkat_prep(struct io_kiocb *req,
3807 const struct io_uring_sqe *sqe)
3808{
3809 struct io_symlink *sl = &req->symlink;
3810 const char __user *oldpath, *newpath;
3811
3812 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3813 return -EINVAL;
3814 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3815 sqe->splice_fd_in)
3816 return -EINVAL;
3817 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3818 return -EBADF;
3819
3820 sl->new_dfd = READ_ONCE(sqe->fd);
3821 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3822 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3823
3824 sl->oldpath = getname(oldpath);
3825 if (IS_ERR(sl->oldpath))
3826 return PTR_ERR(sl->oldpath);
3827
3828 sl->newpath = getname(newpath);
3829 if (IS_ERR(sl->newpath)) {
3830 putname(sl->oldpath);
3831 return PTR_ERR(sl->newpath);
3832 }
3833
3834 req->flags |= REQ_F_NEED_CLEANUP;
3835 return 0;
3836}
3837
3838static int io_symlinkat(struct io_kiocb *req, int issue_flags)
3839{
3840 struct io_symlink *sl = &req->symlink;
3841 int ret;
3842
3843 if (issue_flags & IO_URING_F_NONBLOCK)
3844 return -EAGAIN;
3845
3846 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3847
3848 req->flags &= ~REQ_F_NEED_CLEANUP;
3849 if (ret < 0)
3850 req_set_fail(req);
3851 io_req_complete(req, ret);
3852 return 0;
3853}
3854
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003855static int io_linkat_prep(struct io_kiocb *req,
3856 const struct io_uring_sqe *sqe)
3857{
3858 struct io_hardlink *lnk = &req->hardlink;
3859 const char __user *oldf, *newf;
3860
3861 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3862 return -EINVAL;
3863 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3864 return -EINVAL;
3865 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3866 return -EBADF;
3867
3868 lnk->old_dfd = READ_ONCE(sqe->fd);
3869 lnk->new_dfd = READ_ONCE(sqe->len);
3870 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3871 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3872 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3873
3874 lnk->oldpath = getname(oldf);
3875 if (IS_ERR(lnk->oldpath))
3876 return PTR_ERR(lnk->oldpath);
3877
3878 lnk->newpath = getname(newf);
3879 if (IS_ERR(lnk->newpath)) {
3880 putname(lnk->oldpath);
3881 return PTR_ERR(lnk->newpath);
3882 }
3883
3884 req->flags |= REQ_F_NEED_CLEANUP;
3885 return 0;
3886}
3887
3888static int io_linkat(struct io_kiocb *req, int issue_flags)
3889{
3890 struct io_hardlink *lnk = &req->hardlink;
3891 int ret;
3892
3893 if (issue_flags & IO_URING_F_NONBLOCK)
3894 return -EAGAIN;
3895
3896 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3897 lnk->newpath, lnk->flags);
3898
3899 req->flags &= ~REQ_F_NEED_CLEANUP;
3900 if (ret < 0)
3901 req_set_fail(req);
3902 io_req_complete(req, ret);
3903 return 0;
3904}
3905
Jens Axboe36f4fa62020-09-05 11:14:22 -06003906static int io_shutdown_prep(struct io_kiocb *req,
3907 const struct io_uring_sqe *sqe)
3908{
3909#if defined(CONFIG_NET)
3910 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3911 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003912 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3913 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003914 return -EINVAL;
3915
3916 req->shutdown.how = READ_ONCE(sqe->len);
3917 return 0;
3918#else
3919 return -EOPNOTSUPP;
3920#endif
3921}
3922
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003923static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003924{
3925#if defined(CONFIG_NET)
3926 struct socket *sock;
3927 int ret;
3928
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003929 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003930 return -EAGAIN;
3931
Linus Torvalds48aba792020-12-16 12:44:05 -08003932 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003933 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003934 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003935
3936 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003937 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003938 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003939 io_req_complete(req, ret);
3940 return 0;
3941#else
3942 return -EOPNOTSUPP;
3943#endif
3944}
3945
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003946static int __io_splice_prep(struct io_kiocb *req,
3947 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003948{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003949 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003950 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003951
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003952 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3953 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003954
3955 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003956 sp->len = READ_ONCE(sqe->len);
3957 sp->flags = READ_ONCE(sqe->splice_flags);
3958
3959 if (unlikely(sp->flags & ~valid_flags))
3960 return -EINVAL;
3961
Pavel Begunkov62906e82021-08-10 14:52:47 +01003962 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003963 (sp->flags & SPLICE_F_FD_IN_FIXED));
3964 if (!sp->file_in)
3965 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003966 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003967 return 0;
3968}
3969
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003970static int io_tee_prep(struct io_kiocb *req,
3971 const struct io_uring_sqe *sqe)
3972{
3973 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3974 return -EINVAL;
3975 return __io_splice_prep(req, sqe);
3976}
3977
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003978static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003979{
3980 struct io_splice *sp = &req->splice;
3981 struct file *in = sp->file_in;
3982 struct file *out = sp->file_out;
3983 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3984 long ret = 0;
3985
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003986 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003987 return -EAGAIN;
3988 if (sp->len)
3989 ret = do_tee(in, out, sp->len, flags);
3990
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003991 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3992 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003993 req->flags &= ~REQ_F_NEED_CLEANUP;
3994
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003995 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003996 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003997 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003998 return 0;
3999}
4000
4001static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4002{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004003 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004004
4005 sp->off_in = READ_ONCE(sqe->splice_off_in);
4006 sp->off_out = READ_ONCE(sqe->off);
4007 return __io_splice_prep(req, sqe);
4008}
4009
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004010static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004011{
4012 struct io_splice *sp = &req->splice;
4013 struct file *in = sp->file_in;
4014 struct file *out = sp->file_out;
4015 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4016 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004017 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004018
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004019 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004020 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004021
4022 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4023 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004024
Jens Axboe948a7742020-05-17 14:21:38 -06004025 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004026 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004027
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004028 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4029 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004030 req->flags &= ~REQ_F_NEED_CLEANUP;
4031
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004032 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004033 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004034 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004035 return 0;
4036}
4037
Jens Axboe2b188cc2019-01-07 10:46:33 -07004038/*
4039 * IORING_OP_NOP just posts a completion event, nothing else.
4040 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004041static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004042{
4043 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004044
Jens Axboedef596e2019-01-09 08:59:42 -07004045 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4046 return -EINVAL;
4047
Pavel Begunkov889fca72021-02-10 00:03:09 +00004048 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004049 return 0;
4050}
4051
Pavel Begunkov1155c762021-02-18 18:29:38 +00004052static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004053{
Jens Axboe6b063142019-01-10 22:13:58 -07004054 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004055
Jens Axboe09bb8392019-03-13 12:39:28 -06004056 if (!req->file)
4057 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004058
Jens Axboe6b063142019-01-10 22:13:58 -07004059 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004060 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004061 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4062 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004063 return -EINVAL;
4064
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004065 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4066 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4067 return -EINVAL;
4068
4069 req->sync.off = READ_ONCE(sqe->off);
4070 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004071 return 0;
4072}
4073
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004074static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004075{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004076 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004077 int ret;
4078
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004079 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004080 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004081 return -EAGAIN;
4082
Jens Axboe9adbd452019-12-20 08:45:55 -07004083 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004084 end > 0 ? end : LLONG_MAX,
4085 req->sync.flags & IORING_FSYNC_DATASYNC);
4086 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004087 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004088 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004089 return 0;
4090}
4091
Jens Axboed63d1b52019-12-10 10:38:56 -07004092static int io_fallocate_prep(struct io_kiocb *req,
4093 const struct io_uring_sqe *sqe)
4094{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004095 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4096 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004097 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004098 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4099 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004100
4101 req->sync.off = READ_ONCE(sqe->off);
4102 req->sync.len = READ_ONCE(sqe->addr);
4103 req->sync.mode = READ_ONCE(sqe->len);
4104 return 0;
4105}
4106
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004107static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004108{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004109 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004110
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004111 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004112 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004113 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004114 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4115 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004116 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004117 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004118 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004119 return 0;
4120}
4121
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004122static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004123{
Jens Axboef8748882020-01-08 17:47:02 -07004124 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004125 int ret;
4126
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004127 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4128 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004129 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004130 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004131 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004132 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004133
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004134 /* open.how should be already initialised */
4135 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004136 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004137
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004138 req->open.dfd = READ_ONCE(sqe->fd);
4139 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004140 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004141 if (IS_ERR(req->open.filename)) {
4142 ret = PTR_ERR(req->open.filename);
4143 req->open.filename = NULL;
4144 return ret;
4145 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004146
4147 req->open.file_slot = READ_ONCE(sqe->file_index);
4148 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4149 return -EINVAL;
4150
Jens Axboe4022e7a2020-03-19 19:23:18 -06004151 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004152 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004153 return 0;
4154}
4155
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004156static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4157{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004158 u64 mode = READ_ONCE(sqe->len);
4159 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004160
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004161 req->open.how = build_open_how(flags, mode);
4162 return __io_openat_prep(req, sqe);
4163}
4164
Jens Axboecebdb982020-01-08 17:59:24 -07004165static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4166{
4167 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004168 size_t len;
4169 int ret;
4170
Jens Axboecebdb982020-01-08 17:59:24 -07004171 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4172 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004173 if (len < OPEN_HOW_SIZE_VER0)
4174 return -EINVAL;
4175
4176 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4177 len);
4178 if (ret)
4179 return ret;
4180
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004181 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004182}
4183
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004184static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004185{
4186 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004187 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004188 bool resolve_nonblock, nonblock_set;
4189 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004190 int ret;
4191
Jens Axboecebdb982020-01-08 17:59:24 -07004192 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004193 if (ret)
4194 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004195 nonblock_set = op.open_flag & O_NONBLOCK;
4196 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004197 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004198 /*
4199 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4200 * it'll always -EAGAIN
4201 */
4202 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4203 return -EAGAIN;
4204 op.lookup_flags |= LOOKUP_CACHED;
4205 op.open_flag |= O_NONBLOCK;
4206 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004207
Pavel Begunkovb9445592021-08-25 12:25:45 +01004208 if (!fixed) {
4209 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4210 if (ret < 0)
4211 goto err;
4212 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004213
4214 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004215 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004216 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004217 * We could hang on to this 'fd' on retrying, but seems like
4218 * marginal gain for something that is now known to be a slower
4219 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004220 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004221 if (!fixed)
4222 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004223
4224 ret = PTR_ERR(file);
4225 /* only retry if RESOLVE_CACHED wasn't already set by application */
4226 if (ret == -EAGAIN &&
4227 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4228 return -EAGAIN;
4229 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004230 }
4231
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004232 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4233 file->f_flags &= ~O_NONBLOCK;
4234 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004235
4236 if (!fixed)
4237 fd_install(ret, file);
4238 else
4239 ret = io_install_fixed_file(req, file, issue_flags,
4240 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004241err:
4242 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004243 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004244 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004245 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004246 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004247 return 0;
4248}
4249
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004250static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004251{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004252 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004253}
4254
Jens Axboe067524e2020-03-02 16:32:28 -07004255static int io_remove_buffers_prep(struct io_kiocb *req,
4256 const struct io_uring_sqe *sqe)
4257{
4258 struct io_provide_buf *p = &req->pbuf;
4259 u64 tmp;
4260
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004261 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4262 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004263 return -EINVAL;
4264
4265 tmp = READ_ONCE(sqe->fd);
4266 if (!tmp || tmp > USHRT_MAX)
4267 return -EINVAL;
4268
4269 memset(p, 0, sizeof(*p));
4270 p->nbufs = tmp;
4271 p->bgid = READ_ONCE(sqe->buf_group);
4272 return 0;
4273}
4274
4275static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4276 int bgid, unsigned nbufs)
4277{
4278 unsigned i = 0;
4279
4280 /* shouldn't happen */
4281 if (!nbufs)
4282 return 0;
4283
4284 /* the head kbuf is the list itself */
4285 while (!list_empty(&buf->list)) {
4286 struct io_buffer *nxt;
4287
4288 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4289 list_del(&nxt->list);
4290 kfree(nxt);
4291 if (++i == nbufs)
4292 return i;
4293 }
4294 i++;
4295 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004296 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004297
4298 return i;
4299}
4300
Pavel Begunkov889fca72021-02-10 00:03:09 +00004301static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004302{
4303 struct io_provide_buf *p = &req->pbuf;
4304 struct io_ring_ctx *ctx = req->ctx;
4305 struct io_buffer *head;
4306 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004307 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004308
4309 io_ring_submit_lock(ctx, !force_nonblock);
4310
4311 lockdep_assert_held(&ctx->uring_lock);
4312
4313 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004314 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004315 if (head)
4316 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004317 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004318 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004319
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004320 /* complete before unlock, IOPOLL may need the lock */
4321 __io_req_complete(req, issue_flags, ret, 0);
4322 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004323 return 0;
4324}
4325
Jens Axboeddf0322d2020-02-23 16:41:33 -07004326static int io_provide_buffers_prep(struct io_kiocb *req,
4327 const struct io_uring_sqe *sqe)
4328{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004329 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004330 struct io_provide_buf *p = &req->pbuf;
4331 u64 tmp;
4332
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004333 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004334 return -EINVAL;
4335
4336 tmp = READ_ONCE(sqe->fd);
4337 if (!tmp || tmp > USHRT_MAX)
4338 return -E2BIG;
4339 p->nbufs = tmp;
4340 p->addr = READ_ONCE(sqe->addr);
4341 p->len = READ_ONCE(sqe->len);
4342
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004343 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4344 &size))
4345 return -EOVERFLOW;
4346 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4347 return -EOVERFLOW;
4348
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004349 size = (unsigned long)p->len * p->nbufs;
4350 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004351 return -EFAULT;
4352
4353 p->bgid = READ_ONCE(sqe->buf_group);
4354 tmp = READ_ONCE(sqe->off);
4355 if (tmp > USHRT_MAX)
4356 return -E2BIG;
4357 p->bid = tmp;
4358 return 0;
4359}
4360
4361static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4362{
4363 struct io_buffer *buf;
4364 u64 addr = pbuf->addr;
4365 int i, bid = pbuf->bid;
4366
4367 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004368 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004369 if (!buf)
4370 break;
4371
4372 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004373 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004374 buf->bid = bid;
4375 addr += pbuf->len;
4376 bid++;
4377 if (!*head) {
4378 INIT_LIST_HEAD(&buf->list);
4379 *head = buf;
4380 } else {
4381 list_add_tail(&buf->list, &(*head)->list);
4382 }
4383 }
4384
4385 return i ? i : -ENOMEM;
4386}
4387
Pavel Begunkov889fca72021-02-10 00:03:09 +00004388static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004389{
4390 struct io_provide_buf *p = &req->pbuf;
4391 struct io_ring_ctx *ctx = req->ctx;
4392 struct io_buffer *head, *list;
4393 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004394 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004395
4396 io_ring_submit_lock(ctx, !force_nonblock);
4397
4398 lockdep_assert_held(&ctx->uring_lock);
4399
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004400 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004401
4402 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004403 if (ret >= 0 && !list) {
4404 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4405 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004406 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004407 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004408 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004409 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004410 /* complete before unlock, IOPOLL may need the lock */
4411 __io_req_complete(req, issue_flags, ret, 0);
4412 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004413 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004414}
4415
Jens Axboe3e4827b2020-01-08 15:18:09 -07004416static int io_epoll_ctl_prep(struct io_kiocb *req,
4417 const struct io_uring_sqe *sqe)
4418{
4419#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004420 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004421 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004422 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004423 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004424
4425 req->epoll.epfd = READ_ONCE(sqe->fd);
4426 req->epoll.op = READ_ONCE(sqe->len);
4427 req->epoll.fd = READ_ONCE(sqe->off);
4428
4429 if (ep_op_has_event(req->epoll.op)) {
4430 struct epoll_event __user *ev;
4431
4432 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4433 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4434 return -EFAULT;
4435 }
4436
4437 return 0;
4438#else
4439 return -EOPNOTSUPP;
4440#endif
4441}
4442
Pavel Begunkov889fca72021-02-10 00:03:09 +00004443static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004444{
4445#if defined(CONFIG_EPOLL)
4446 struct io_epoll *ie = &req->epoll;
4447 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004448 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004449
4450 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4451 if (force_nonblock && ret == -EAGAIN)
4452 return -EAGAIN;
4453
4454 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004455 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004456 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004457 return 0;
4458#else
4459 return -EOPNOTSUPP;
4460#endif
4461}
4462
Jens Axboec1ca7572019-12-25 22:18:28 -07004463static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4464{
4465#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004466 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004467 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004468 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4469 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004470
4471 req->madvise.addr = READ_ONCE(sqe->addr);
4472 req->madvise.len = READ_ONCE(sqe->len);
4473 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4474 return 0;
4475#else
4476 return -EOPNOTSUPP;
4477#endif
4478}
4479
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004480static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004481{
4482#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4483 struct io_madvise *ma = &req->madvise;
4484 int ret;
4485
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004486 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004487 return -EAGAIN;
4488
Minchan Kim0726b012020-10-17 16:14:50 -07004489 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004490 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004491 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004492 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004493 return 0;
4494#else
4495 return -EOPNOTSUPP;
4496#endif
4497}
4498
Jens Axboe4840e412019-12-25 22:03:45 -07004499static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4500{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004501 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004502 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004503 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4504 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004505
4506 req->fadvise.offset = READ_ONCE(sqe->off);
4507 req->fadvise.len = READ_ONCE(sqe->len);
4508 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4509 return 0;
4510}
4511
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004512static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004513{
4514 struct io_fadvise *fa = &req->fadvise;
4515 int ret;
4516
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004517 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004518 switch (fa->advice) {
4519 case POSIX_FADV_NORMAL:
4520 case POSIX_FADV_RANDOM:
4521 case POSIX_FADV_SEQUENTIAL:
4522 break;
4523 default:
4524 return -EAGAIN;
4525 }
4526 }
Jens Axboe4840e412019-12-25 22:03:45 -07004527
4528 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4529 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004530 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004531 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004532 return 0;
4533}
4534
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004535static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4536{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004537 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004538 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004539 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004540 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004541 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004542 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004543
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004544 req->statx.dfd = READ_ONCE(sqe->fd);
4545 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004546 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004547 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4548 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004549
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004550 return 0;
4551}
4552
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004553static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004554{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004555 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004556 int ret;
4557
Pavel Begunkov59d70012021-03-22 01:58:30 +00004558 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004559 return -EAGAIN;
4560
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004561 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4562 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004563
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004564 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004565 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004566 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004567 return 0;
4568}
4569
Jens Axboeb5dba592019-12-11 14:02:38 -07004570static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4571{
Jens Axboe14587a462020-09-05 11:36:08 -06004572 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004573 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004574 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004575 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -07004576 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004577 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004578 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004579
4580 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004581 req->close.file_slot = READ_ONCE(sqe->file_index);
4582 if (req->close.file_slot && req->close.fd)
4583 return -EINVAL;
4584
Jens Axboeb5dba592019-12-11 14:02:38 -07004585 return 0;
4586}
4587
Pavel Begunkov889fca72021-02-10 00:03:09 +00004588static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004589{
Jens Axboe9eac1902021-01-19 15:50:37 -07004590 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004591 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004592 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004593 struct file *file = NULL;
4594 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004595
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004596 if (req->close.file_slot) {
4597 ret = io_close_fixed(req, issue_flags);
4598 goto err;
4599 }
4600
Jens Axboe9eac1902021-01-19 15:50:37 -07004601 spin_lock(&files->file_lock);
4602 fdt = files_fdtable(files);
4603 if (close->fd >= fdt->max_fds) {
4604 spin_unlock(&files->file_lock);
4605 goto err;
4606 }
4607 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004608 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004609 spin_unlock(&files->file_lock);
4610 file = NULL;
4611 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004612 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004613
4614 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004615 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004616 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004617 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004618 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004619
Jens Axboe9eac1902021-01-19 15:50:37 -07004620 ret = __close_fd_get_file(close->fd, &file);
4621 spin_unlock(&files->file_lock);
4622 if (ret < 0) {
4623 if (ret == -ENOENT)
4624 ret = -EBADF;
4625 goto err;
4626 }
4627
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004628 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004629 ret = filp_close(file, current->files);
4630err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004631 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004632 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004633 if (file)
4634 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004635 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004636 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004637}
4638
Pavel Begunkov1155c762021-02-18 18:29:38 +00004639static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004640{
4641 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004642
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004643 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4644 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004645 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4646 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004647 return -EINVAL;
4648
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004649 req->sync.off = READ_ONCE(sqe->off);
4650 req->sync.len = READ_ONCE(sqe->len);
4651 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004652 return 0;
4653}
4654
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004655static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004656{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004657 int ret;
4658
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004659 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004660 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004661 return -EAGAIN;
4662
Jens Axboe9adbd452019-12-20 08:45:55 -07004663 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004664 req->sync.flags);
4665 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004666 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004667 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004668 return 0;
4669}
4670
YueHaibing469956e2020-03-04 15:53:52 +08004671#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004672static int io_setup_async_msg(struct io_kiocb *req,
4673 struct io_async_msghdr *kmsg)
4674{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004675 struct io_async_msghdr *async_msg = req->async_data;
4676
4677 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004678 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004679 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004680 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004681 return -ENOMEM;
4682 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004683 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004684 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004685 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004686 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004687 /* if were using fast_iov, set it to the new one */
4688 if (!async_msg->free_iov)
4689 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4690
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004691 return -EAGAIN;
4692}
4693
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004694static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4695 struct io_async_msghdr *iomsg)
4696{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004697 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004698 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004699 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004700 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004701}
4702
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004703static int io_sendmsg_prep_async(struct io_kiocb *req)
4704{
4705 int ret;
4706
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004707 ret = io_sendmsg_copy_hdr(req, req->async_data);
4708 if (!ret)
4709 req->flags |= REQ_F_NEED_CLEANUP;
4710 return ret;
4711}
4712
Jens Axboe3529d8c2019-12-19 18:24:38 -07004713static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004714{
Jens Axboee47293f2019-12-20 08:58:21 -07004715 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004716
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004717 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4718 return -EINVAL;
4719
Pavel Begunkov270a5942020-07-12 20:41:04 +03004720 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004721 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004722 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4723 if (sr->msg_flags & MSG_DONTWAIT)
4724 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004725
Jens Axboed8768362020-02-27 14:17:49 -07004726#ifdef CONFIG_COMPAT
4727 if (req->ctx->compat)
4728 sr->msg_flags |= MSG_CMSG_COMPAT;
4729#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004730 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004731}
4732
Pavel Begunkov889fca72021-02-10 00:03:09 +00004733static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004734{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004735 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004736 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004737 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004738 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004739 int ret;
4740
Florent Revestdba4a922020-12-04 12:36:04 +01004741 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004742 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004743 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004744
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004745 kmsg = req->async_data;
4746 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004747 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004748 if (ret)
4749 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004750 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004751 }
4752
Pavel Begunkov04411802021-04-01 15:44:00 +01004753 flags = req->sr_msg.msg_flags;
4754 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004755 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004756 if (flags & MSG_WAITALL)
4757 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4758
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004759 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004760 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004761 return io_setup_async_msg(req, kmsg);
4762 if (ret == -ERESTARTSYS)
4763 ret = -EINTR;
4764
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004765 /* fast path, check for non-NULL to avoid function call */
4766 if (kmsg->free_iov)
4767 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004768 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004769 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004770 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004771 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004772 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004773}
4774
Pavel Begunkov889fca72021-02-10 00:03:09 +00004775static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004776{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004777 struct io_sr_msg *sr = &req->sr_msg;
4778 struct msghdr msg;
4779 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004780 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004781 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004782 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004783 int ret;
4784
Florent Revestdba4a922020-12-04 12:36:04 +01004785 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004786 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004787 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004788
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004789 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4790 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004791 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004792
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004793 msg.msg_name = NULL;
4794 msg.msg_control = NULL;
4795 msg.msg_controllen = 0;
4796 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004797
Pavel Begunkov04411802021-04-01 15:44:00 +01004798 flags = req->sr_msg.msg_flags;
4799 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004800 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004801 if (flags & MSG_WAITALL)
4802 min_ret = iov_iter_count(&msg.msg_iter);
4803
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004804 msg.msg_flags = flags;
4805 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004806 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004807 return -EAGAIN;
4808 if (ret == -ERESTARTSYS)
4809 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004810
Stefan Metzmacher00312752021-03-20 20:33:36 +01004811 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004812 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004813 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004814 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004815}
4816
Pavel Begunkov1400e692020-07-12 20:41:05 +03004817static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4818 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004819{
4820 struct io_sr_msg *sr = &req->sr_msg;
4821 struct iovec __user *uiov;
4822 size_t iov_len;
4823 int ret;
4824
Pavel Begunkov1400e692020-07-12 20:41:05 +03004825 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4826 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004827 if (ret)
4828 return ret;
4829
4830 if (req->flags & REQ_F_BUFFER_SELECT) {
4831 if (iov_len > 1)
4832 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004833 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004834 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004835 sr->len = iomsg->fast_iov[0].iov_len;
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, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004840 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004841 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004842 if (ret > 0)
4843 ret = 0;
4844 }
4845
4846 return ret;
4847}
4848
4849#ifdef CONFIG_COMPAT
4850static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004851 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004852{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004853 struct io_sr_msg *sr = &req->sr_msg;
4854 struct compat_iovec __user *uiov;
4855 compat_uptr_t ptr;
4856 compat_size_t len;
4857 int ret;
4858
Pavel Begunkov4af34172021-04-11 01:46:30 +01004859 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4860 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004861 if (ret)
4862 return ret;
4863
4864 uiov = compat_ptr(ptr);
4865 if (req->flags & REQ_F_BUFFER_SELECT) {
4866 compat_ssize_t clen;
4867
4868 if (len > 1)
4869 return -EINVAL;
4870 if (!access_ok(uiov, sizeof(*uiov)))
4871 return -EFAULT;
4872 if (__get_user(clen, &uiov->iov_len))
4873 return -EFAULT;
4874 if (clen < 0)
4875 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004876 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004877 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004878 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004879 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004880 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004881 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004882 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004883 if (ret < 0)
4884 return ret;
4885 }
4886
4887 return 0;
4888}
Jens Axboe03b12302019-12-02 18:50:25 -07004889#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004890
Pavel Begunkov1400e692020-07-12 20:41:05 +03004891static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4892 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004893{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004894 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004895
4896#ifdef CONFIG_COMPAT
4897 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004898 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004899#endif
4900
Pavel Begunkov1400e692020-07-12 20:41:05 +03004901 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004902}
4903
Jens Axboebcda7ba2020-02-23 16:42:51 -07004904static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004905 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004906{
4907 struct io_sr_msg *sr = &req->sr_msg;
4908 struct io_buffer *kbuf;
4909
Jens Axboebcda7ba2020-02-23 16:42:51 -07004910 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4911 if (IS_ERR(kbuf))
4912 return kbuf;
4913
4914 sr->kbuf = kbuf;
4915 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004916 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004917}
4918
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004919static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4920{
4921 return io_put_kbuf(req, req->sr_msg.kbuf);
4922}
4923
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004924static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004925{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004926 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004927
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004928 ret = io_recvmsg_copy_hdr(req, req->async_data);
4929 if (!ret)
4930 req->flags |= REQ_F_NEED_CLEANUP;
4931 return ret;
4932}
4933
4934static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4935{
4936 struct io_sr_msg *sr = &req->sr_msg;
4937
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004938 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4939 return -EINVAL;
4940
Pavel Begunkov270a5942020-07-12 20:41:04 +03004941 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004942 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004943 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004944 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4945 if (sr->msg_flags & MSG_DONTWAIT)
4946 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004947
Jens Axboed8768362020-02-27 14:17:49 -07004948#ifdef CONFIG_COMPAT
4949 if (req->ctx->compat)
4950 sr->msg_flags |= MSG_CMSG_COMPAT;
4951#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004952 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004953}
4954
Pavel Begunkov889fca72021-02-10 00:03:09 +00004955static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004956{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004957 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004958 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004959 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004960 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004961 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004962 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004963 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004964
Florent Revestdba4a922020-12-04 12:36:04 +01004965 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004966 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004967 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004968
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004969 kmsg = req->async_data;
4970 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004971 ret = io_recvmsg_copy_hdr(req, &iomsg);
4972 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004973 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004974 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004975 }
4976
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004977 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004978 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004979 if (IS_ERR(kbuf))
4980 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004981 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004982 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4983 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004984 1, req->sr_msg.len);
4985 }
4986
Pavel Begunkov04411802021-04-01 15:44:00 +01004987 flags = req->sr_msg.msg_flags;
4988 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004989 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004990 if (flags & MSG_WAITALL)
4991 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4992
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004993 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4994 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004995 if (force_nonblock && ret == -EAGAIN)
4996 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004997 if (ret == -ERESTARTSYS)
4998 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004999
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005000 if (req->flags & REQ_F_BUFFER_SELECTED)
5001 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005002 /* fast path, check for non-NULL to avoid function call */
5003 if (kmsg->free_iov)
5004 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005005 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005006 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005007 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005008 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005009 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005010}
5011
Pavel Begunkov889fca72021-02-10 00:03:09 +00005012static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005013{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005014 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005015 struct io_sr_msg *sr = &req->sr_msg;
5016 struct msghdr msg;
5017 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005018 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005019 struct iovec iov;
5020 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005021 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005022 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005023 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005024
Florent Revestdba4a922020-12-04 12:36:04 +01005025 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005026 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005027 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005028
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005029 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005030 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005031 if (IS_ERR(kbuf))
5032 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005033 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005034 }
5035
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005036 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005037 if (unlikely(ret))
5038 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005039
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005040 msg.msg_name = NULL;
5041 msg.msg_control = NULL;
5042 msg.msg_controllen = 0;
5043 msg.msg_namelen = 0;
5044 msg.msg_iocb = NULL;
5045 msg.msg_flags = 0;
5046
Pavel Begunkov04411802021-04-01 15:44:00 +01005047 flags = req->sr_msg.msg_flags;
5048 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005049 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005050 if (flags & MSG_WAITALL)
5051 min_ret = iov_iter_count(&msg.msg_iter);
5052
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005053 ret = sock_recvmsg(sock, &msg, flags);
5054 if (force_nonblock && ret == -EAGAIN)
5055 return -EAGAIN;
5056 if (ret == -ERESTARTSYS)
5057 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005058out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005059 if (req->flags & REQ_F_BUFFER_SELECTED)
5060 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01005061 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005062 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005063 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005064 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005065}
5066
Jens Axboe3529d8c2019-12-19 18:24:38 -07005067static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005068{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005069 struct io_accept *accept = &req->accept;
5070
Jens Axboe14587a462020-09-05 11:36:08 -06005071 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005072 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005073 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005074 return -EINVAL;
5075
Jens Axboed55e5f52019-12-11 16:12:15 -07005076 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5077 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005078 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005079 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005080
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005081 accept->file_slot = READ_ONCE(sqe->file_index);
5082 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5083 (accept->flags & SOCK_CLOEXEC)))
5084 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005085 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5086 return -EINVAL;
5087 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5088 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005089 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005090}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005091
Pavel Begunkov889fca72021-02-10 00:03:09 +00005092static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005093{
5094 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005095 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005096 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005097 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005098 struct file *file;
5099 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005100
Jiufei Xuee697dee2020-06-10 13:41:59 +08005101 if (req->file->f_flags & O_NONBLOCK)
5102 req->flags |= REQ_F_NOWAIT;
5103
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005104 if (!fixed) {
5105 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5106 if (unlikely(fd < 0))
5107 return fd;
5108 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005109 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5110 accept->flags);
5111 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005112 if (!fixed)
5113 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005114 ret = PTR_ERR(file);
5115 if (ret == -EAGAIN && force_nonblock)
5116 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005117 if (ret == -ERESTARTSYS)
5118 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005119 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005120 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005121 fd_install(fd, file);
5122 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005123 } else {
5124 ret = io_install_fixed_file(req, file, issue_flags,
5125 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005126 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005127 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005128 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005129}
5130
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005131static int io_connect_prep_async(struct io_kiocb *req)
5132{
5133 struct io_async_connect *io = req->async_data;
5134 struct io_connect *conn = &req->connect;
5135
5136 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5137}
5138
Jens Axboe3529d8c2019-12-19 18:24:38 -07005139static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005140{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005141 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005142
Jens Axboe14587a462020-09-05 11:36:08 -06005143 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005144 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005145 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5146 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005147 return -EINVAL;
5148
Jens Axboe3529d8c2019-12-19 18:24:38 -07005149 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5150 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005151 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005152}
5153
Pavel Begunkov889fca72021-02-10 00:03:09 +00005154static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005155{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005156 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005157 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005158 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005159 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005160
Jens Axboee8c2bc12020-08-15 18:44:09 -07005161 if (req->async_data) {
5162 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005163 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005164 ret = move_addr_to_kernel(req->connect.addr,
5165 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005166 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005167 if (ret)
5168 goto out;
5169 io = &__io;
5170 }
5171
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005172 file_flags = force_nonblock ? O_NONBLOCK : 0;
5173
Jens Axboee8c2bc12020-08-15 18:44:09 -07005174 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005175 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005176 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005177 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005178 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005179 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005180 ret = -ENOMEM;
5181 goto out;
5182 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005183 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005184 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005185 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005186 if (ret == -ERESTARTSYS)
5187 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005188out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005189 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005190 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005191 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005192 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005193}
YueHaibing469956e2020-03-04 15:53:52 +08005194#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005195#define IO_NETOP_FN(op) \
5196static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5197{ \
5198 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005199}
5200
Jens Axboe99a10082021-02-19 09:35:19 -07005201#define IO_NETOP_PREP(op) \
5202IO_NETOP_FN(op) \
5203static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5204{ \
5205 return -EOPNOTSUPP; \
5206} \
5207
5208#define IO_NETOP_PREP_ASYNC(op) \
5209IO_NETOP_PREP(op) \
5210static int io_##op##_prep_async(struct io_kiocb *req) \
5211{ \
5212 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005213}
5214
Jens Axboe99a10082021-02-19 09:35:19 -07005215IO_NETOP_PREP_ASYNC(sendmsg);
5216IO_NETOP_PREP_ASYNC(recvmsg);
5217IO_NETOP_PREP_ASYNC(connect);
5218IO_NETOP_PREP(accept);
5219IO_NETOP_FN(send);
5220IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005221#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005222
Jens Axboed7718a92020-02-14 22:23:12 -07005223struct io_poll_table {
5224 struct poll_table_struct pt;
5225 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005226 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005227 int error;
5228};
5229
Jens Axboed7718a92020-02-14 22:23:12 -07005230static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005231 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005232{
Jens Axboed7718a92020-02-14 22:23:12 -07005233 /* for instances that support it check for an event match first: */
5234 if (mask && !(mask & poll->events))
5235 return 0;
5236
5237 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5238
5239 list_del_init(&poll->wait.entry);
5240
Jens Axboed7718a92020-02-14 22:23:12 -07005241 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005242 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005243
Jens Axboed7718a92020-02-14 22:23:12 -07005244 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005245 * If this fails, then the task is exiting. When a task exits, the
5246 * work gets canceled, so just cancel this request as well instead
5247 * of executing it. We can't safely execute it anyway, as we may not
5248 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005249 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005250 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005251 return 1;
5252}
5253
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005254static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5255 __acquires(&req->ctx->completion_lock)
5256{
5257 struct io_ring_ctx *ctx = req->ctx;
5258
Jens Axboe316319e2021-08-19 09:41:42 -06005259 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005260 if (unlikely(req->task->flags & PF_EXITING))
5261 WRITE_ONCE(poll->canceled, true);
5262
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005263 if (!req->result && !READ_ONCE(poll->canceled)) {
5264 struct poll_table_struct pt = { ._key = poll->events };
5265
5266 req->result = vfs_poll(req->file, &pt) & poll->events;
5267 }
5268
Jens Axboe79ebeae2021-08-10 15:18:27 -06005269 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005270 if (!req->result && !READ_ONCE(poll->canceled)) {
5271 add_wait_queue(poll->head, &poll->wait);
5272 return true;
5273 }
5274
5275 return false;
5276}
5277
Jens Axboed4e7cd32020-08-15 11:44:50 -07005278static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005279{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005280 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005281 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005282 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005283 return req->apoll->double_poll;
5284}
5285
5286static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5287{
5288 if (req->opcode == IORING_OP_POLL_ADD)
5289 return &req->poll;
5290 return &req->apoll->poll;
5291}
5292
5293static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005294 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005295{
5296 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005297
5298 lockdep_assert_held(&req->ctx->completion_lock);
5299
5300 if (poll && poll->head) {
5301 struct wait_queue_head *head = poll->head;
5302
Jens Axboe79ebeae2021-08-10 15:18:27 -06005303 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005304 list_del_init(&poll->wait.entry);
5305 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005306 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005307 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005308 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005309 }
5310}
5311
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005312static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005313 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005314{
5315 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005316 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005317 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005318
Pavel Begunkove27414b2021-04-09 09:13:20 +01005319 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005320 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005321 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005322 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005323 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005324 }
Jens Axboeb69de282021-03-17 08:37:41 -06005325 if (req->poll.events & EPOLLONESHOT)
5326 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005327 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
5328 req->poll.events |= EPOLLONESHOT;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005329 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005330 }
Hao Xu7b289c32021-04-13 15:20:39 +08005331 if (flags & IORING_CQE_F_MORE)
5332 ctx->cq_extra++;
5333
Jens Axboe88e41cf2021-02-22 22:08:01 -07005334 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005335}
5336
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005337static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
5338 __must_hold(&req->ctx->completion_lock)
5339{
5340 bool done;
5341
5342 done = __io_poll_complete(req, mask);
5343 io_commit_cqring(req->ctx);
5344 return done;
5345}
5346
Pavel Begunkovf237c302021-08-18 12:42:46 +01005347static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005348{
Jens Axboe6d816e02020-08-11 08:04:14 -06005349 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005350 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005351
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005352 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005353 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005354 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005355 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005356
Hao Xu5b7aa382021-09-22 18:12:38 +08005357 if (req->poll.done) {
5358 spin_unlock(&ctx->completion_lock);
5359 return;
5360 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005361 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005362 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005363 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005364 hash_del(&req->hash_node);
Hao Xubd99c712021-09-22 18:12:36 +08005365 req->poll.done = true;
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005366 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005367 req->result = 0;
5368 add_wait_queue(req->poll.head, &req->poll.wait);
5369 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005370 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005371 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005372 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005373
Jens Axboe88e41cf2021-02-22 22:08:01 -07005374 if (done) {
5375 nxt = io_put_req_find_next(req);
5376 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005377 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005378 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005379 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005380}
5381
5382static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5383 int sync, void *key)
5384{
5385 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005386 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005387 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005388 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005389
5390 /* for instances that support it check for an event match first: */
5391 if (mask && !(mask & poll->events))
5392 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005393 if (!(poll->events & EPOLLONESHOT))
5394 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005395
Jens Axboe8706e042020-09-28 08:38:54 -06005396 list_del_init(&wait->entry);
5397
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005398 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005399 bool done;
5400
Jens Axboe79ebeae2021-08-10 15:18:27 -06005401 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005402 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005403 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005404 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005405 /* make sure double remove sees this as being gone */
5406 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005407 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005408 if (!done) {
5409 /* use wait func handler, so it matches the rq type */
5410 poll->wait.func(&poll->wait, mode, sync, key);
5411 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005412 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005413 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005414 return 1;
5415}
5416
5417static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5418 wait_queue_func_t wake_func)
5419{
5420 poll->head = NULL;
5421 poll->done = false;
5422 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005423#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5424 /* mask in events that we always want/need */
5425 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005426 INIT_LIST_HEAD(&poll->wait.entry);
5427 init_waitqueue_func_entry(&poll->wait, wake_func);
5428}
5429
5430static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005431 struct wait_queue_head *head,
5432 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005433{
5434 struct io_kiocb *req = pt->req;
5435
5436 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005437 * The file being polled uses multiple waitqueues for poll handling
5438 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5439 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005440 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005441 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005442 struct io_poll_iocb *poll_one = poll;
5443
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005444 /* double add on the same waitqueue head, ignore */
5445 if (poll_one->head == head)
5446 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005447 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005448 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005449 if ((*poll_ptr)->head == head)
5450 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005451 pt->error = -EINVAL;
5452 return;
5453 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005454 /*
5455 * Can't handle multishot for double wait for now, turn it
5456 * into one-shot mode.
5457 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005458 if (!(poll_one->events & EPOLLONESHOT))
5459 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005460 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5461 if (!poll) {
5462 pt->error = -ENOMEM;
5463 return;
5464 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005465 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005466 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005467 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005468 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005469 }
5470
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005471 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005472 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005473
5474 if (poll->events & EPOLLEXCLUSIVE)
5475 add_wait_queue_exclusive(head, &poll->wait);
5476 else
5477 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005478}
5479
5480static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5481 struct poll_table_struct *p)
5482{
5483 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005484 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005485
Jens Axboe807abcb2020-07-17 17:09:27 -06005486 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005487}
5488
Pavel Begunkovf237c302021-08-18 12:42:46 +01005489static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005490{
Jens Axboed7718a92020-02-14 22:23:12 -07005491 struct async_poll *apoll = req->apoll;
5492 struct io_ring_ctx *ctx = req->ctx;
5493
Olivier Langlois236daeae2021-05-31 02:36:37 -04005494 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005495
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005496 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005497 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005498 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005499 }
5500
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005501 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005502 io_poll_remove_double(req);
Hao Xubd99c712021-09-22 18:12:36 +08005503 apoll->poll.done = true;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005504 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005505
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005506 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005507 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005508 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005509 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005510}
5511
5512static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5513 void *key)
5514{
5515 struct io_kiocb *req = wait->private;
5516 struct io_poll_iocb *poll = &req->apoll->poll;
5517
5518 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5519 key_to_poll(key));
5520
5521 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5522}
5523
5524static void io_poll_req_insert(struct io_kiocb *req)
5525{
5526 struct io_ring_ctx *ctx = req->ctx;
5527 struct hlist_head *list;
5528
5529 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5530 hlist_add_head(&req->hash_node, list);
5531}
5532
5533static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5534 struct io_poll_iocb *poll,
5535 struct io_poll_table *ipt, __poll_t mask,
5536 wait_queue_func_t wake_func)
5537 __acquires(&ctx->completion_lock)
5538{
5539 struct io_ring_ctx *ctx = req->ctx;
5540 bool cancel = false;
5541
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005542 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005543 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005544 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005545 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005546
5547 ipt->pt._key = mask;
5548 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005549 ipt->error = 0;
5550 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005551
Jens Axboed7718a92020-02-14 22:23:12 -07005552 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005553 if (unlikely(!ipt->nr_entries) && !ipt->error)
5554 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005555
Jens Axboe79ebeae2021-08-10 15:18:27 -06005556 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005557 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005558 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005559 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005560 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005561 if (unlikely(list_empty(&poll->wait.entry))) {
5562 if (ipt->error)
5563 cancel = true;
5564 ipt->error = 0;
5565 mask = 0;
5566 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005567 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005568 list_del_init(&poll->wait.entry);
5569 else if (cancel)
5570 WRITE_ONCE(poll->canceled, true);
5571 else if (!poll->done) /* actually waiting for an event */
5572 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005573 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005574 }
5575
5576 return mask;
5577}
5578
Olivier Langlois59b735a2021-06-22 05:17:39 -07005579enum {
5580 IO_APOLL_OK,
5581 IO_APOLL_ABORTED,
5582 IO_APOLL_READY
5583};
5584
5585static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005586{
5587 const struct io_op_def *def = &io_op_defs[req->opcode];
5588 struct io_ring_ctx *ctx = req->ctx;
5589 struct async_poll *apoll;
5590 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005591 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005592 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005593
5594 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005595 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005596 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005597 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005598 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005599 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005600
5601 if (def->pollin) {
5602 rw = READ;
5603 mask |= POLLIN | POLLRDNORM;
5604
5605 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5606 if ((req->opcode == IORING_OP_RECVMSG) &&
5607 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5608 mask &= ~POLLIN;
5609 } else {
5610 rw = WRITE;
5611 mask |= POLLOUT | POLLWRNORM;
5612 }
5613
Jens Axboe9dab14b2020-08-25 12:27:50 -06005614 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005615 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005616 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005617
5618 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5619 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005620 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005621 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005622 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005623 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005624 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005625 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005626
5627 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5628 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005629 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005630 if (ret || ipt.error)
5631 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5632
Olivier Langlois236daeae2021-05-31 02:36:37 -04005633 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5634 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005635 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005636}
5637
5638static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005639 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005640 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005641{
Jens Axboeb41e9852020-02-17 09:52:41 -07005642 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005643
Jens Axboe50826202021-02-23 09:02:26 -07005644 if (!poll->head)
5645 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005646 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005647 if (do_cancel)
5648 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005649 if (!list_empty(&poll->wait.entry)) {
5650 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005651 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005652 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005653 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005654 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005655 return do_complete;
5656}
5657
Pavel Begunkov5d709042021-08-09 20:18:13 +01005658static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005659 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005660{
5661 bool do_complete;
5662
Jens Axboed4e7cd32020-08-15 11:44:50 -07005663 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005664 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005665
Jens Axboeb41e9852020-02-17 09:52:41 -07005666 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005667 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005668 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005669 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005670 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005671 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005672 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005673}
5674
Jens Axboe76e1b642020-09-26 15:05:03 -06005675/*
5676 * Returns true if we found and killed one or more poll requests
5677 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005678static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005679 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005680{
Jens Axboe78076bb2019-12-04 19:56:40 -07005681 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005682 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005683 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005684
Jens Axboe79ebeae2021-08-10 15:18:27 -06005685 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005686 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5687 struct hlist_head *list;
5688
5689 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005690 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005691 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005692 posted += io_poll_remove_one(req);
5693 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005694 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005695 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005696
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005697 if (posted)
5698 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005699
5700 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005701}
5702
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005703static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5704 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005705 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005706{
Jens Axboe78076bb2019-12-04 19:56:40 -07005707 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005708 struct io_kiocb *req;
5709
Jens Axboe78076bb2019-12-04 19:56:40 -07005710 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5711 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005712 if (sqe_addr != req->user_data)
5713 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005714 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5715 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005716 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005717 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005718 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005719}
5720
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005721static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5722 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005723 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005724{
5725 struct io_kiocb *req;
5726
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005727 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005728 if (!req)
5729 return -ENOENT;
5730 if (io_poll_remove_one(req))
5731 return 0;
5732
5733 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005734}
5735
Pavel Begunkov9096af32021-04-14 13:38:36 +01005736static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5737 unsigned int flags)
5738{
5739 u32 events;
5740
5741 events = READ_ONCE(sqe->poll32_events);
5742#ifdef __BIG_ENDIAN
5743 events = swahw32(events);
5744#endif
5745 if (!(flags & IORING_POLL_ADD_MULTI))
5746 events |= EPOLLONESHOT;
5747 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5748}
5749
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005750static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005751 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005752{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005753 struct io_poll_update *upd = &req->poll_update;
5754 u32 flags;
5755
Jens Axboe221c5eb2019-01-17 09:41:58 -07005756 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5757 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005758 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005759 return -EINVAL;
5760 flags = READ_ONCE(sqe->len);
5761 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5762 IORING_POLL_ADD_MULTI))
5763 return -EINVAL;
5764 /* meaningless without update */
5765 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005766 return -EINVAL;
5767
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005768 upd->old_user_data = READ_ONCE(sqe->addr);
5769 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5770 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005771
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005772 upd->new_user_data = READ_ONCE(sqe->off);
5773 if (!upd->update_user_data && upd->new_user_data)
5774 return -EINVAL;
5775 if (upd->update_events)
5776 upd->events = io_poll_parse_events(sqe, flags);
5777 else if (sqe->poll32_events)
5778 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005779
Jens Axboe221c5eb2019-01-17 09:41:58 -07005780 return 0;
5781}
5782
Jens Axboe221c5eb2019-01-17 09:41:58 -07005783static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5784 void *key)
5785{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005786 struct io_kiocb *req = wait->private;
5787 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005788
Jens Axboed7718a92020-02-14 22:23:12 -07005789 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005790}
5791
Jens Axboe221c5eb2019-01-17 09:41:58 -07005792static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5793 struct poll_table_struct *p)
5794{
5795 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5796
Jens Axboee8c2bc12020-08-15 18:44:09 -07005797 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005798}
5799
Jens Axboe3529d8c2019-12-19 18:24:38 -07005800static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005801{
5802 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005803 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005804
5805 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5806 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005807 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005808 return -EINVAL;
5809 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005810 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005811 return -EINVAL;
5812
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005813 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005814 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005815 return 0;
5816}
5817
Pavel Begunkov61e98202021-02-10 00:03:08 +00005818static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005819{
5820 struct io_poll_iocb *poll = &req->poll;
5821 struct io_ring_ctx *ctx = req->ctx;
5822 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005823 __poll_t mask;
Hao Xu5b7aa382021-09-22 18:12:38 +08005824 bool done;
Jens Axboe0969e782019-12-17 18:40:57 -07005825
Jens Axboed7718a92020-02-14 22:23:12 -07005826 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005827
Jens Axboed7718a92020-02-14 22:23:12 -07005828 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5829 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005830
Jens Axboe8c838782019-03-12 15:48:16 -06005831 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005832 ipt.error = 0;
Hao Xu5b7aa382021-09-22 18:12:38 +08005833 done = io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005834 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005835 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005836
Jens Axboe8c838782019-03-12 15:48:16 -06005837 if (mask) {
5838 io_cqring_ev_posted(ctx);
Hao Xu5b7aa382021-09-22 18:12:38 +08005839 if (done)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005840 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005841 }
Jens Axboe8c838782019-03-12 15:48:16 -06005842 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005843}
5844
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005845static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005846{
5847 struct io_ring_ctx *ctx = req->ctx;
5848 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005849 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005850 int ret;
5851
Jens Axboe79ebeae2021-08-10 15:18:27 -06005852 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005853 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005854 if (!preq) {
5855 ret = -ENOENT;
5856 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005857 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005858
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005859 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5860 completing = true;
5861 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5862 goto err;
5863 }
5864
Jens Axboecb3b200e2021-04-06 09:49:31 -06005865 /*
5866 * Don't allow racy completion with singleshot, as we cannot safely
5867 * update those. For multishot, if we're racing with completion, just
5868 * let completion re-add it.
5869 */
5870 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5871 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5872 ret = -EALREADY;
5873 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005874 }
5875 /* we now have a detached poll request. reissue. */
5876 ret = 0;
5877err:
Jens Axboeb69de282021-03-17 08:37:41 -06005878 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005879 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005880 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005881 io_req_complete(req, ret);
5882 return 0;
5883 }
5884 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005885 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005886 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005887 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005888 preq->poll.events |= IO_POLL_UNMASK;
5889 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005890 if (req->poll_update.update_user_data)
5891 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005892 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005893
Jens Axboeb69de282021-03-17 08:37:41 -06005894 /* complete update request, we're done with it */
5895 io_req_complete(req, ret);
5896
Jens Axboecb3b200e2021-04-06 09:49:31 -06005897 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005898 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005899 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005900 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005901 io_req_complete(preq, ret);
5902 }
Jens Axboeb69de282021-03-17 08:37:41 -06005903 }
5904 return 0;
5905}
5906
Pavel Begunkovf237c302021-08-18 12:42:46 +01005907static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005908{
Jens Axboe89850fc2021-08-10 15:11:51 -06005909 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005910 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005911}
5912
Jens Axboe5262f562019-09-17 12:26:57 -06005913static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5914{
Jens Axboead8a48a2019-11-15 08:49:11 -07005915 struct io_timeout_data *data = container_of(timer,
5916 struct io_timeout_data, timer);
5917 struct io_kiocb *req = data->req;
5918 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005919 unsigned long flags;
5920
Jens Axboe89850fc2021-08-10 15:11:51 -06005921 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005922 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005923 atomic_set(&req->ctx->cq_timeouts,
5924 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005925 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005926
Jens Axboe89850fc2021-08-10 15:11:51 -06005927 req->io_task_work.func = io_req_task_timeout;
5928 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005929 return HRTIMER_NORESTART;
5930}
5931
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005932static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5933 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005934 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005935{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005936 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005937 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005938 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005939
5940 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005941 found = user_data == req->user_data;
5942 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005943 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005944 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005945 if (!found)
5946 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005947
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005948 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005949 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005950 return ERR_PTR(-EALREADY);
5951 list_del_init(&req->timeout.list);
5952 return req;
5953}
5954
5955static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005956 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005957 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005958{
5959 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5960
5961 if (IS_ERR(req))
5962 return PTR_ERR(req);
5963
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005964 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005965 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005966 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005967 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005968}
5969
Jens Axboe50c1df22021-08-27 17:11:06 -06005970static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
5971{
5972 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
5973 case IORING_TIMEOUT_BOOTTIME:
5974 return CLOCK_BOOTTIME;
5975 case IORING_TIMEOUT_REALTIME:
5976 return CLOCK_REALTIME;
5977 default:
5978 /* can't happen, vetted at prep time */
5979 WARN_ON_ONCE(1);
5980 fallthrough;
5981 case 0:
5982 return CLOCK_MONOTONIC;
5983 }
5984}
5985
Pavel Begunkovf1042b62021-08-28 19:54:39 -06005986static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5987 struct timespec64 *ts, enum hrtimer_mode mode)
5988 __must_hold(&ctx->timeout_lock)
5989{
5990 struct io_timeout_data *io;
5991 struct io_kiocb *req;
5992 bool found = false;
5993
5994 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
5995 found = user_data == req->user_data;
5996 if (found)
5997 break;
5998 }
5999 if (!found)
6000 return -ENOENT;
6001
6002 io = req->async_data;
6003 if (hrtimer_try_to_cancel(&io->timer) == -1)
6004 return -EALREADY;
6005 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6006 io->timer.function = io_link_timeout_fn;
6007 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6008 return 0;
6009}
6010
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006011static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6012 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06006013 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006014{
6015 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6016 struct io_timeout_data *data;
6017
6018 if (IS_ERR(req))
6019 return PTR_ERR(req);
6020
6021 req->timeout.off = 0; /* noseq */
6022 data = req->async_data;
6023 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06006024 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006025 data->timer.function = io_timeout_fn;
6026 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6027 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07006028}
6029
Jens Axboe3529d8c2019-12-19 18:24:38 -07006030static int io_timeout_remove_prep(struct io_kiocb *req,
6031 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07006032{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006033 struct io_timeout_rem *tr = &req->timeout_rem;
6034
Jens Axboeb29472e2019-12-17 18:50:29 -07006035 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6036 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006037 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6038 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006039 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006040 return -EINVAL;
6041
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006042 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006043 tr->addr = READ_ONCE(sqe->addr);
6044 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006045 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6046 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6047 return -EINVAL;
6048 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6049 tr->ltimeout = true;
6050 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006051 return -EINVAL;
6052 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6053 return -EFAULT;
6054 } else if (tr->flags) {
6055 /* timeout removal doesn't support flags */
6056 return -EINVAL;
6057 }
6058
Jens Axboeb29472e2019-12-17 18:50:29 -07006059 return 0;
6060}
6061
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006062static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6063{
6064 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6065 : HRTIMER_MODE_REL;
6066}
6067
Jens Axboe11365042019-10-16 09:08:32 -06006068/*
6069 * Remove or update an existing timeout command
6070 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006071static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006072{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006073 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006074 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006075 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006076
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006077 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6078 spin_lock(&ctx->completion_lock);
6079 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006080 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006081 spin_unlock_irq(&ctx->timeout_lock);
6082 spin_unlock(&ctx->completion_lock);
6083 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006084 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6085
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006086 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006087 if (tr->ltimeout)
6088 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6089 else
6090 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006091 spin_unlock_irq(&ctx->timeout_lock);
6092 }
Jens Axboe11365042019-10-16 09:08:32 -06006093
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006094 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006095 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006096 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006097 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006098}
6099
Jens Axboe3529d8c2019-12-19 18:24:38 -07006100static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006101 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006102{
Jens Axboead8a48a2019-11-15 08:49:11 -07006103 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006104 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006105 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006106
Jens Axboead8a48a2019-11-15 08:49:11 -07006107 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006108 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006109 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6110 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006111 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006112 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006113 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006114 flags = READ_ONCE(sqe->timeout_flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006115 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK))
6116 return -EINVAL;
6117 /* more than one clock specified is invalid, obviously */
6118 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006119 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006120
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006121 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006122 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006123 if (unlikely(off && !req->ctx->off_timeout_used))
6124 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006125
Jens Axboee8c2bc12020-08-15 18:44:09 -07006126 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006127 return -ENOMEM;
6128
Jens Axboee8c2bc12020-08-15 18:44:09 -07006129 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006130 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006131 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006132
6133 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006134 return -EFAULT;
6135
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006136 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006137 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006138
6139 if (is_timeout_link) {
6140 struct io_submit_link *link = &req->ctx->submit_state.link;
6141
6142 if (!link->head)
6143 return -EINVAL;
6144 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6145 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006146 req->timeout.head = link->last;
6147 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006148 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006149 return 0;
6150}
6151
Pavel Begunkov61e98202021-02-10 00:03:08 +00006152static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006153{
Jens Axboead8a48a2019-11-15 08:49:11 -07006154 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006155 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006156 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006157 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006158
Jens Axboe89850fc2021-08-10 15:11:51 -06006159 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006160
Jens Axboe5262f562019-09-17 12:26:57 -06006161 /*
6162 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006163 * timeout event to be satisfied. If it isn't set, then this is
6164 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006165 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006166 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006167 entry = ctx->timeout_list.prev;
6168 goto add;
6169 }
Jens Axboe5262f562019-09-17 12:26:57 -06006170
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006171 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6172 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006173
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006174 /* Update the last seq here in case io_flush_timeouts() hasn't.
6175 * This is safe because ->completion_lock is held, and submissions
6176 * and completions are never mixed in the same ->completion_lock section.
6177 */
6178 ctx->cq_last_tm_flush = tail;
6179
Jens Axboe5262f562019-09-17 12:26:57 -06006180 /*
6181 * Insertion sort, ensuring the first entry in the list is always
6182 * the one we need first.
6183 */
Jens Axboe5262f562019-09-17 12:26:57 -06006184 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006185 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6186 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006187
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006188 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006189 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006190 /* nxt.seq is behind @tail, otherwise would've been completed */
6191 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006192 break;
6193 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006194add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006195 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006196 data->timer.function = io_timeout_fn;
6197 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006198 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006199 return 0;
6200}
6201
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006202struct io_cancel_data {
6203 struct io_ring_ctx *ctx;
6204 u64 user_data;
6205};
6206
Jens Axboe62755e32019-10-28 21:49:21 -06006207static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006208{
Jens Axboe62755e32019-10-28 21:49:21 -06006209 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006210 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006211
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006212 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006213}
6214
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006215static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6216 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006217{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006218 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006219 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006220 int ret = 0;
6221
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006222 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006223 return -ENOENT;
6224
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006225 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006226 switch (cancel_ret) {
6227 case IO_WQ_CANCEL_OK:
6228 ret = 0;
6229 break;
6230 case IO_WQ_CANCEL_RUNNING:
6231 ret = -EALREADY;
6232 break;
6233 case IO_WQ_CANCEL_NOTFOUND:
6234 ret = -ENOENT;
6235 break;
6236 }
6237
Jens Axboee977d6d2019-11-05 12:39:45 -07006238 return ret;
6239}
6240
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006241static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006242{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006243 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006244 int ret;
6245
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006246 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006247
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006248 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006249 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006250 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006251
6252 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006253 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006254 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006255 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006256 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006257 goto out;
6258 ret = io_poll_cancel(ctx, sqe_addr, false);
6259out:
6260 spin_unlock(&ctx->completion_lock);
6261 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006262}
6263
Jens Axboe3529d8c2019-12-19 18:24:38 -07006264static int io_async_cancel_prep(struct io_kiocb *req,
6265 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006266{
Jens Axboefbf23842019-12-17 18:45:56 -07006267 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006268 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006269 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6270 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006271 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6272 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006273 return -EINVAL;
6274
Jens Axboefbf23842019-12-17 18:45:56 -07006275 req->cancel.addr = READ_ONCE(sqe->addr);
6276 return 0;
6277}
6278
Pavel Begunkov61e98202021-02-10 00:03:08 +00006279static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006280{
6281 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006282 u64 sqe_addr = req->cancel.addr;
6283 struct io_tctx_node *node;
6284 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006285
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006286 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006287 if (ret != -ENOENT)
6288 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006289
6290 /* slow path, try all io-wq's */
6291 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
6292 ret = -ENOENT;
6293 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6294 struct io_uring_task *tctx = node->task->io_uring;
6295
Pavel Begunkov58f99372021-03-12 16:25:55 +00006296 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6297 if (ret != -ENOENT)
6298 break;
6299 }
6300 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00006301done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006302 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006303 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006304 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006305 return 0;
6306}
6307
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006308static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006309 const struct io_uring_sqe *sqe)
6310{
Daniele Albano61710e42020-07-18 14:15:16 -06006311 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6312 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006313 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006314 return -EINVAL;
6315
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006316 req->rsrc_update.offset = READ_ONCE(sqe->off);
6317 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6318 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006319 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006320 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006321 return 0;
6322}
6323
Pavel Begunkov889fca72021-02-10 00:03:09 +00006324static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006325{
6326 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006327 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006328 int ret;
6329
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006330 up.offset = req->rsrc_update.offset;
6331 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006332 up.nr = 0;
6333 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006334 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006335
Jens Axboecdb31c22021-09-24 08:43:54 -06006336 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006337 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006338 &up, req->rsrc_update.nr_args);
Jens Axboecdb31c22021-09-24 08:43:54 -06006339 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Jens Axboe05f3fb32019-12-09 11:22:50 -07006340
6341 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006342 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006343 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006344 return 0;
6345}
6346
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006347static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006348{
Jens Axboed625c6e2019-12-17 19:53:05 -07006349 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006350 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006351 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006352 case IORING_OP_READV:
6353 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006354 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006355 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006356 case IORING_OP_WRITEV:
6357 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006358 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006359 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006360 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006361 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006362 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006363 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006364 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006365 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006366 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006367 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006368 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006369 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006370 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006371 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006372 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006373 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006374 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006375 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006376 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006377 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006378 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006379 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006380 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006381 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006382 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006383 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006384 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006385 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006386 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006387 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006388 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006389 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006390 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006391 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006392 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006393 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006394 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006395 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006396 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006397 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006398 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006399 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006400 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006401 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006402 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006403 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006404 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006405 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006406 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006407 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006408 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006409 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006410 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006411 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006412 case IORING_OP_SHUTDOWN:
6413 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006414 case IORING_OP_RENAMEAT:
6415 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006416 case IORING_OP_UNLINKAT:
6417 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006418 case IORING_OP_MKDIRAT:
6419 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006420 case IORING_OP_SYMLINKAT:
6421 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006422 case IORING_OP_LINKAT:
6423 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006424 }
6425
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006426 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6427 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006428 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006429}
6430
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006431static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006432{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006433 if (!io_op_defs[req->opcode].needs_async_setup)
6434 return 0;
6435 if (WARN_ON_ONCE(req->async_data))
6436 return -EFAULT;
6437 if (io_alloc_async_data(req))
6438 return -EAGAIN;
6439
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006440 switch (req->opcode) {
6441 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006442 return io_rw_prep_async(req, READ);
6443 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006444 return io_rw_prep_async(req, WRITE);
6445 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006446 return io_sendmsg_prep_async(req);
6447 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006448 return io_recvmsg_prep_async(req);
6449 case IORING_OP_CONNECT:
6450 return io_connect_prep_async(req);
6451 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006452 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6453 req->opcode);
6454 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006455}
6456
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006457static u32 io_get_sequence(struct io_kiocb *req)
6458{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006459 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006460
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006461 /* need original cached_sq_head, but it was increased for each req */
6462 io_for_each_link(req, req)
6463 seq--;
6464 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006465}
6466
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006467static bool io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006468{
Pavel Begunkov3c199662021-06-15 16:47:57 +01006469 struct io_kiocb *pos;
Jens Axboedef596e2019-01-09 08:59:42 -07006470 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006471 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006472 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006473 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07006474
Pavel Begunkovb8ce1b92021-08-31 14:13:11 +01006475 if (req->flags & REQ_F_FAIL) {
6476 io_req_complete_fail_submit(req);
6477 return true;
6478 }
6479
Pavel Begunkov3c199662021-06-15 16:47:57 +01006480 /*
6481 * If we need to drain a request in the middle of a link, drain the
6482 * head request and the next request/link after the current link.
6483 * Considering sequential execution of links, IOSQE_IO_DRAIN will be
6484 * maintained for every request of our link.
6485 */
6486 if (ctx->drain_next) {
6487 req->flags |= REQ_F_IO_DRAIN;
6488 ctx->drain_next = false;
6489 }
6490 /* not interested in head, start from the first linked */
6491 io_for_each_link(pos, req->link) {
6492 if (pos->flags & REQ_F_IO_DRAIN) {
6493 ctx->drain_next = true;
6494 req->flags |= REQ_F_IO_DRAIN;
6495 break;
6496 }
6497 }
6498
Jens Axboedef596e2019-01-09 08:59:42 -07006499 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006500 if (likely(list_empty_careful(&ctx->defer_list) &&
Pavel Begunkov10c66902021-06-15 16:47:56 +01006501 !(req->flags & REQ_F_IO_DRAIN))) {
6502 ctx->drain_active = false;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006503 return false;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006504 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006505
6506 seq = io_get_sequence(req);
6507 /* Still a chance to pass the sequence check */
6508 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006509 return false;
Jens Axboedef596e2019-01-09 08:59:42 -07006510
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006511 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006512 if (ret)
Pavel Begunkov1b487732021-07-11 22:41:13 +01006513 goto fail;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006514 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006515 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006516 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006517 ret = -ENOMEM;
6518fail:
6519 io_req_complete_failed(req, ret);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006520 return true;
6521 }
Jens Axboe31b51512019-01-18 22:56:34 -07006522
Jens Axboe79ebeae2021-08-10 15:18:27 -06006523 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006524 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006525 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006526 kfree(de);
Pavel Begunkovf237c302021-08-18 12:42:46 +01006527 io_queue_async_work(req, NULL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006528 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006529 }
6530
6531 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006532 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006533 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006534 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006535 spin_unlock(&ctx->completion_lock);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006536 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006537}
6538
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006539static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006540{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006541 if (req->flags & REQ_F_BUFFER_SELECTED) {
6542 switch (req->opcode) {
6543 case IORING_OP_READV:
6544 case IORING_OP_READ_FIXED:
6545 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006546 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006547 break;
6548 case IORING_OP_RECVMSG:
6549 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006550 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006551 break;
6552 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006553 }
6554
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006555 if (req->flags & REQ_F_NEED_CLEANUP) {
6556 switch (req->opcode) {
6557 case IORING_OP_READV:
6558 case IORING_OP_READ_FIXED:
6559 case IORING_OP_READ:
6560 case IORING_OP_WRITEV:
6561 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006562 case IORING_OP_WRITE: {
6563 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006564
6565 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006566 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006567 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006568 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006569 case IORING_OP_SENDMSG: {
6570 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006571
6572 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006573 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006574 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006575 case IORING_OP_SPLICE:
6576 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006577 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6578 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006579 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006580 case IORING_OP_OPENAT:
6581 case IORING_OP_OPENAT2:
6582 if (req->open.filename)
6583 putname(req->open.filename);
6584 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006585 case IORING_OP_RENAMEAT:
6586 putname(req->rename.oldpath);
6587 putname(req->rename.newpath);
6588 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006589 case IORING_OP_UNLINKAT:
6590 putname(req->unlink.filename);
6591 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006592 case IORING_OP_MKDIRAT:
6593 putname(req->mkdir.filename);
6594 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006595 case IORING_OP_SYMLINKAT:
6596 putname(req->symlink.oldpath);
6597 putname(req->symlink.newpath);
6598 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006599 case IORING_OP_LINKAT:
6600 putname(req->hardlink.oldpath);
6601 putname(req->hardlink.newpath);
6602 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006603 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006604 }
Jens Axboe75652a302021-04-15 09:52:40 -06006605 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6606 kfree(req->apoll->double_poll);
6607 kfree(req->apoll);
6608 req->apoll = NULL;
6609 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006610 if (req->flags & REQ_F_INFLIGHT) {
6611 struct io_uring_task *tctx = req->task->io_uring;
6612
6613 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006614 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006615 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006616 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006617
6618 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006619}
6620
Pavel Begunkov889fca72021-02-10 00:03:09 +00006621static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006622{
Jens Axboeedafcce2019-01-09 09:16:05 -07006623 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006624 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006625 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006626
Pavel Begunkov6878b402021-09-24 21:59:41 +01006627 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006628 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006629
Jens Axboed625c6e2019-12-17 19:53:05 -07006630 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006631 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006632 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006633 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006634 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006635 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006636 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006637 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006638 break;
6639 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006640 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006641 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006642 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006643 break;
6644 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006645 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006646 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006648 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006649 break;
6650 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006651 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006652 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006653 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006654 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006655 break;
6656 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006657 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006658 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006659 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006660 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006661 break;
6662 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006663 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006664 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006665 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006666 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006667 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006668 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006669 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006670 break;
6671 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006672 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006673 break;
6674 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006675 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006676 break;
6677 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006678 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006679 break;
6680 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006681 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006682 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006683 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006684 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006685 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006686 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006687 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006688 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006689 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006690 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006691 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006692 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006693 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006694 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006695 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006696 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006697 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006698 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006699 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006700 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006701 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006702 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006703 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006704 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006705 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006706 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006707 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006708 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006709 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006710 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006711 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006712 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006713 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006714 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006715 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006716 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006717 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006718 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006719 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006720 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006721 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006722 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006723 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006724 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006725 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006726 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006727 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006728 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006729 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006730 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006731 case IORING_OP_MKDIRAT:
6732 ret = io_mkdirat(req, issue_flags);
6733 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006734 case IORING_OP_SYMLINKAT:
6735 ret = io_symlinkat(req, issue_flags);
6736 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006737 case IORING_OP_LINKAT:
6738 ret = io_linkat(req, issue_flags);
6739 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740 default:
6741 ret = -EINVAL;
6742 break;
6743 }
Jens Axboe31b51512019-01-18 22:56:34 -07006744
Jens Axboe5730b272021-02-27 15:57:30 -07006745 if (creds)
6746 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006747 if (ret)
6748 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006749 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006750 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6751 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006752
6753 return 0;
6754}
6755
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006756static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6757{
6758 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6759
6760 req = io_put_req_find_next(req);
6761 return req ? &req->work : NULL;
6762}
6763
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006764static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006765{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006766 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006767 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006768 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006769
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006770 /* one will be dropped by ->io_free_work() after returning to io-wq */
6771 if (!(req->flags & REQ_F_REFCOUNT))
6772 __io_req_set_refcount(req, 2);
6773 else
6774 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006775
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006776 timeout = io_prep_linked_timeout(req);
6777 if (timeout)
6778 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006779
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006780 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006781 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006782 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006783
Jens Axboe561fb042019-10-24 07:25:42 -06006784 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006785 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006786 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006787 /*
6788 * We can get EAGAIN for polled IO even though we're
6789 * forcing a sync submission from here, since we can't
6790 * wait for request slots on the block side.
6791 */
6792 if (ret != -EAGAIN)
6793 break;
6794 cond_resched();
6795 } while (1);
6796 }
Jens Axboe31b51512019-01-18 22:56:34 -07006797
Pavel Begunkova3df76982021-02-18 22:32:52 +00006798 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006799 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006800 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006801}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006802
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006803static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006804 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006805{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006806 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006807}
6808
Jens Axboe09bb8392019-03-13 12:39:28 -06006809static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6810 int index)
6811{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006812 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006813
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006814 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006815}
6816
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006817static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006818{
6819 unsigned long file_ptr = (unsigned long) file;
6820
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006821 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006822 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006823 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006824 file_ptr |= FFS_ASYNC_WRITE;
6825 if (S_ISREG(file_inode(file)->i_mode))
6826 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006827 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006828}
6829
Pavel Begunkovac177052021-08-09 13:04:02 +01006830static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6831 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006832{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006833 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006834 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006835
Pavel Begunkovac177052021-08-09 13:04:02 +01006836 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6837 return NULL;
6838 fd = array_index_nospec(fd, ctx->nr_user_files);
6839 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6840 file = (struct file *) (file_ptr & FFS_MASK);
6841 file_ptr &= ~FFS_MASK;
6842 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006843 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006844 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006845 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006846}
6847
Pavel Begunkovac177052021-08-09 13:04:02 +01006848static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006849 struct io_kiocb *req, int fd)
6850{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006851 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006852
6853 trace_io_uring_file_get(ctx, fd);
6854
6855 /* we don't allow fixed io_uring files */
6856 if (file && unlikely(file->f_op == &io_uring_fops))
6857 io_req_track_inflight(req);
6858 return file;
6859}
6860
6861static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006862 struct io_kiocb *req, int fd, bool fixed)
6863{
6864 if (fixed)
6865 return io_file_get_fixed(ctx, req, fd);
6866 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006867 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006868}
6869
Pavel Begunkovf237c302021-08-18 12:42:46 +01006870static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006871{
6872 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006873 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006874
6875 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006876 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006877 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006878 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006879 } else {
6880 io_req_complete_post(req, -ETIME, 0);
6881 }
6882}
6883
Jens Axboe2665abf2019-11-05 12:40:47 -07006884static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6885{
Jens Axboead8a48a2019-11-15 08:49:11 -07006886 struct io_timeout_data *data = container_of(timer,
6887 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006888 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006889 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006890 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006891
Jens Axboe89b263f2021-08-10 15:14:18 -06006892 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006893 prev = req->timeout.head;
6894 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006895
6896 /*
6897 * We don't expect the list to be empty, that will only happen if we
6898 * race with the completion of the linked work.
6899 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006900 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006901 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006902 if (!req_ref_inc_not_zero(prev))
6903 prev = NULL;
6904 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006905 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006906 req->timeout.prev = prev;
6907 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006908
Jens Axboe89b263f2021-08-10 15:14:18 -06006909 req->io_task_work.func = io_req_task_link_timeout;
6910 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006911 return HRTIMER_NORESTART;
6912}
6913
Pavel Begunkovde968c12021-03-19 17:22:33 +00006914static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006915{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006916 struct io_ring_ctx *ctx = req->ctx;
6917
Jens Axboe89b263f2021-08-10 15:14:18 -06006918 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006919 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006920 * If the back reference is NULL, then our linked request finished
6921 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006922 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006923 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006924 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006925
Jens Axboead8a48a2019-11-15 08:49:11 -07006926 data->timer.function = io_link_timeout_fn;
6927 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6928 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006929 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006930 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006931 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006932 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006933 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006934}
6935
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006936static void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006937 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006938{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006939 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006940 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006941
Olivier Langlois59b735a2021-06-22 05:17:39 -07006942issue_sqe:
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006943 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006944
6945 /*
6946 * We async punt it if the file wasn't marked NOWAIT, or if the file
6947 * doesn't support non-blocking read/write attempts
6948 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006949 if (likely(!ret)) {
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01006950 if (req->flags & REQ_F_COMPLETE_INLINE)
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006951 return;
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006952
6953 linked_timeout = io_prep_linked_timeout(req);
6954 if (linked_timeout)
6955 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006956 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006957 linked_timeout = io_prep_linked_timeout(req);
6958
Olivier Langlois59b735a2021-06-22 05:17:39 -07006959 switch (io_arm_poll_handler(req)) {
6960 case IO_APOLL_READY:
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006961 if (linked_timeout)
6962 io_unprep_linked_timeout(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006963 goto issue_sqe;
6964 case IO_APOLL_ABORTED:
Pavel Begunkov18400382021-03-19 17:22:34 +00006965 /*
6966 * Queued up for async execution, worker will release
6967 * submit reference when the iocb is actually submitted.
6968 */
Pavel Begunkovf237c302021-08-18 12:42:46 +01006969 io_queue_async_work(req, NULL);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006970 break;
Pavel Begunkov18400382021-03-19 17:22:34 +00006971 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006972
6973 if (linked_timeout)
6974 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006975 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006976 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006977 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006978}
6979
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006980static inline void io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006981 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006982{
Pavel Begunkov10c66902021-06-15 16:47:56 +01006983 if (unlikely(req->ctx->drain_active) && io_drain_req(req))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006984 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08006985
Hao Xua8295b92021-08-27 17:46:09 +08006986 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006987 __io_queue_sqe(req);
Hao Xua8295b92021-08-27 17:46:09 +08006988 } else if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01006989 io_req_complete_fail_submit(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006990 } else {
6991 int ret = io_req_prep_async(req);
6992
6993 if (unlikely(ret))
6994 io_req_complete_failed(req, ret);
6995 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01006996 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07006997 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006998}
6999
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007000/*
7001 * Check SQE restrictions (opcode and flags).
7002 *
7003 * Returns 'true' if SQE is allowed, 'false' otherwise.
7004 */
7005static inline bool io_check_restriction(struct io_ring_ctx *ctx,
7006 struct io_kiocb *req,
7007 unsigned int sqe_flags)
7008{
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01007009 if (likely(!ctx->restricted))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007010 return true;
7011
7012 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7013 return false;
7014
7015 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7016 ctx->restrictions.sqe_flags_required)
7017 return false;
7018
7019 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7020 ctx->restrictions.sqe_flags_required))
7021 return false;
7022
7023 return true;
7024}
7025
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007026static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007027 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007028 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007029{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007030 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007031 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007032 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007033
Pavel Begunkov864ea922021-08-09 13:04:08 +01007034 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007035 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007036 /* same numerical values with corresponding REQ_F_*, safe to copy */
7037 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007038 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007039 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007040 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007041 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007042
7043 if (unlikely(req->opcode >= IORING_OP_LAST))
7044 return -EINVAL;
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007045 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
7046 /* enforce forwards compatibility on users */
7047 if (sqe_flags & ~SQE_VALID_FLAGS)
7048 return -EINVAL;
7049 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
7050 !io_op_defs[req->opcode].buffer_select)
7051 return -EOPNOTSUPP;
7052 if (sqe_flags & IOSQE_IO_DRAIN)
7053 ctx->drain_active = true;
7054 }
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01007055 if (!io_check_restriction(ctx, req, sqe_flags))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007056 return -EACCES;
7057
Jens Axboe003e8dc2021-03-06 09:22:27 -07007058 personality = READ_ONCE(sqe->personality);
7059 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007060 req->creds = xa_load(&ctx->personalities, personality);
7061 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07007062 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007063 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01007064 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007065 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007066 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007067
Jens Axboe27926b62020-10-28 09:33:23 -06007068 /*
7069 * Plug now if we have more than 1 IO left after this, and the target
7070 * is potentially a read/write to block based storage.
7071 */
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007072 if (state->need_plug && io_op_defs[req->opcode].plug) {
Jens Axboe27926b62020-10-28 09:33:23 -06007073 blk_start_plug(&state->plug);
7074 state->plug_started = true;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007075 state->need_plug = false;
Jens Axboe27926b62020-10-28 09:33:23 -06007076 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007077
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007078 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01007079 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007080 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007081 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007082 ret = -EBADF;
7083 }
Pavel Begunkov71b547c2020-10-10 18:34:09 +01007084 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007085}
7086
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007087static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007088 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007089 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007090{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007091 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007092 int ret;
7093
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007094 ret = io_init_req(ctx, req, sqe);
7095 if (unlikely(ret)) {
7096fail_req:
Jens Axboea87acfd2021-09-11 16:04:50 -06007097 trace_io_uring_req_failed(sqe, ret);
7098
Hao Xua8295b92021-08-27 17:46:09 +08007099 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007100 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007101 /*
7102 * we can judge a link req is failed or cancelled by if
7103 * REQ_F_FAIL is set, but the head is an exception since
7104 * it may be set REQ_F_FAIL because of other req's failure
7105 * so let's leverage req->result to distinguish if a head
7106 * is set REQ_F_FAIL because of its failure or other req's
7107 * failure so that we can set the correct ret code for it.
7108 * init result here to avoid affecting the normal path.
7109 */
7110 if (!(link->head->flags & REQ_F_FAIL))
7111 req_fail_link_node(link->head, -ECANCELED);
7112 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7113 /*
7114 * the current req is a normal req, we should return
7115 * error and thus break the submittion loop.
7116 */
7117 io_req_complete_failed(req, ret);
7118 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007119 }
Hao Xua8295b92021-08-27 17:46:09 +08007120 req_fail_link_node(req, ret);
7121 } else {
7122 ret = io_req_prep(req, sqe);
7123 if (unlikely(ret))
7124 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007125 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007126
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007127 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007128 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7129 req->flags, true,
7130 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007131
Jens Axboe6c271ce2019-01-10 11:22:30 -07007132 /*
7133 * If we already have a head request, queue this one for async
7134 * submittal once the head completes. If we don't have a head but
7135 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7136 * submitted sync once the chain is complete. If none of those
7137 * conditions are true (normal request), then just queue it.
7138 */
7139 if (link->head) {
7140 struct io_kiocb *head = link->head;
7141
Hao Xua8295b92021-08-27 17:46:09 +08007142 if (!(req->flags & REQ_F_FAIL)) {
7143 ret = io_req_prep_async(req);
7144 if (unlikely(ret)) {
7145 req_fail_link_node(req, ret);
7146 if (!(head->flags & REQ_F_FAIL))
7147 req_fail_link_node(head, -ECANCELED);
7148 }
7149 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007150 trace_io_uring_link(ctx, req, head);
7151 link->last->link = req;
7152 link->last = req;
7153
7154 /* last request of a link, enqueue the link */
7155 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7156 link->head = NULL;
Pavel Begunkov5e159202021-06-14 23:37:26 +01007157 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007158 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007159 } else {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007160 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08007161 link->head = req;
7162 link->last = req;
7163 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007164 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007165 }
7166 }
7167
7168 return 0;
7169}
7170
7171/*
7172 * Batched submission is done, ensure local IO is flushed out.
7173 */
7174static void io_submit_state_end(struct io_submit_state *state,
7175 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03007176{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007177 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007178 io_queue_sqe(state->link.head);
Pavel Begunkovc4501782021-09-08 16:40:52 +01007179 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007180 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007181 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007182}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007183
Jens Axboe9e645e112019-05-10 16:07:28 -06007184/*
7185 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007186 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007187static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007188 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007189{
7190 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007191 state->need_plug = max_ios > 2;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007192 /* set only head, no need to init link_last in advance */
7193 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007194}
7195
Jens Axboe193155c2020-02-22 23:22:19 -07007196static void io_commit_sqring(struct io_ring_ctx *ctx)
7197{
Jens Axboe75c6a032020-01-28 10:15:23 -07007198 struct io_rings *rings = ctx->rings;
7199
7200 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007201 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007202 * since once we write the new head, the application could
7203 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007204 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007205 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007206}
7207
Jens Axboe9e645e112019-05-10 16:07:28 -06007208/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007209 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007210 * that is mapped by userspace. This means that care needs to be taken to
7211 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007212 * being a good citizen. If members of the sqe are validated and then later
7213 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007214 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007215 */
7216static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007217{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007218 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007219 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007220
7221 /*
7222 * The cached sq head (or cq tail) serves two purposes:
7223 *
7224 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007225 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007226 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007227 * though the application is the one updating it.
7228 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007229 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007230 if (likely(head < ctx->sq_entries))
7231 return &ctx->sq_sqes[head];
7232
7233 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007234 ctx->cq_extra--;
7235 WRITE_ONCE(ctx->rings->sq_dropped,
7236 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007237 return NULL;
7238}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007239
Jens Axboe0f212202020-09-13 13:09:39 -06007240static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007241 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007242{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007243 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007244
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007245 /* make sure SQ entry isn't read before tail */
7246 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03007247 if (!percpu_ref_tryget_many(&ctx->refs, nr))
7248 return -EAGAIN;
Pavel Begunkov9a108672021-08-27 11:55:01 +01007249 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007250
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007251 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007252 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007253 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007254 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007255
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007256 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03007257 if (unlikely(!req)) {
7258 if (!submitted)
7259 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007260 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007261 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007262 sqe = io_get_sqe(ctx);
7263 if (unlikely(!sqe)) {
Hao Xu0c6e1d72021-08-26 01:58:56 +08007264 list_add(&req->inflight_entry, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007265 break;
7266 }
Jens Axboed3656342019-12-18 09:50:26 -07007267 /* will complete beyond this point, count as submitted */
7268 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007269 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007270 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007271 }
7272
Pavel Begunkov9466f432020-01-25 22:34:01 +03007273 if (unlikely(submitted != nr)) {
7274 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007275 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007276
Pavel Begunkov09899b12021-06-14 02:36:22 +01007277 current->io_uring->cached_refs += unused;
Jens Axboed8a6df12020-10-15 16:24:45 -06007278 percpu_ref_put_many(&ctx->refs, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03007279 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007280
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007281 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007282 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7283 io_commit_sqring(ctx);
7284
Jens Axboe6c271ce2019-01-10 11:22:30 -07007285 return submitted;
7286}
7287
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007288static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7289{
7290 return READ_ONCE(sqd->state);
7291}
7292
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007293static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7294{
7295 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007296 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007297 WRITE_ONCE(ctx->rings->sq_flags,
7298 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007299 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007300}
7301
7302static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7303{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007304 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007305 WRITE_ONCE(ctx->rings->sq_flags,
7306 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007307 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007308}
7309
Xiaoguang Wang08369242020-11-03 14:15:59 +08007310static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007311{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007312 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007313 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007314
Jens Axboec8d1ba52020-09-14 11:07:26 -06007315 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007316 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007317 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7318 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007319
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007320 if (!list_empty(&ctx->iopoll_list) || to_submit) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007321 const struct cred *creds = NULL;
7322
7323 if (ctx->sq_creds != current_cred())
7324 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007325
Xiaoguang Wang08369242020-11-03 14:15:59 +08007326 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007327 if (!list_empty(&ctx->iopoll_list))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01007328 io_do_iopoll(ctx, true);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007329
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007330 /*
7331 * Don't submit if refs are dying, good for io_uring_register(),
7332 * but also it is relied upon by io_ring_exit_work()
7333 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007334 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7335 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007336 ret = io_submit_sqes(ctx, to_submit);
7337 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007338
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007339 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7340 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007341 if (creds)
7342 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007343 }
Jens Axboe90554202020-09-03 12:12:41 -06007344
Xiaoguang Wang08369242020-11-03 14:15:59 +08007345 return ret;
7346}
7347
7348static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7349{
7350 struct io_ring_ctx *ctx;
7351 unsigned sq_thread_idle = 0;
7352
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007353 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7354 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007355 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007356}
7357
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007358static bool io_sqd_handle_event(struct io_sq_data *sqd)
7359{
7360 bool did_sig = false;
7361 struct ksignal ksig;
7362
7363 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7364 signal_pending(current)) {
7365 mutex_unlock(&sqd->lock);
7366 if (signal_pending(current))
7367 did_sig = get_signal(&ksig);
7368 cond_resched();
7369 mutex_lock(&sqd->lock);
7370 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007371 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7372}
7373
Jens Axboe6c271ce2019-01-10 11:22:30 -07007374static int io_sq_thread(void *data)
7375{
Jens Axboe69fb2132020-09-14 11:16:23 -06007376 struct io_sq_data *sqd = data;
7377 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007378 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007379 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007380 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007381
Pavel Begunkov696ee882021-04-01 09:55:04 +01007382 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007383 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007384
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007385 if (sqd->sq_cpu != -1)
7386 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7387 else
7388 set_cpus_allowed_ptr(current, cpu_online_mask);
7389 current->flags |= PF_NO_SETAFFINITY;
7390
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007391 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007392 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007393 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007394
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007395 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7396 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007397 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007398 timeout = jiffies + sqd->sq_thread_idle;
7399 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007400
Jens Axboee95eee22020-09-08 09:11:32 -06007401 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007402 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007403 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007404
Xiaoguang Wang08369242020-11-03 14:15:59 +08007405 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7406 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007407 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007408 if (io_run_task_work())
7409 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007410
Xiaoguang Wang08369242020-11-03 14:15:59 +08007411 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007412 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007413 if (sqt_spin)
7414 timeout = jiffies + sqd->sq_thread_idle;
7415 continue;
7416 }
7417
Xiaoguang Wang08369242020-11-03 14:15:59 +08007418 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007419 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007420 bool needs_sched = true;
7421
Hao Xu724cb4f2021-04-21 23:19:11 +08007422 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007423 io_ring_set_wakeup_flag(ctx);
7424
Hao Xu724cb4f2021-04-21 23:19:11 +08007425 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7426 !list_empty_careful(&ctx->iopoll_list)) {
7427 needs_sched = false;
7428 break;
7429 }
7430 if (io_sqring_entries(ctx)) {
7431 needs_sched = false;
7432 break;
7433 }
7434 }
7435
7436 if (needs_sched) {
7437 mutex_unlock(&sqd->lock);
7438 schedule();
7439 mutex_lock(&sqd->lock);
7440 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007441 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7442 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007443 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007444
7445 finish_wait(&sqd->wait, &wait);
7446 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007447 }
7448
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007449 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007450 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007451 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007452 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007453 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007454 mutex_unlock(&sqd->lock);
7455
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007456 complete(&sqd->exited);
7457 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007458}
7459
Jens Axboebda52162019-09-24 13:47:15 -06007460struct io_wait_queue {
7461 struct wait_queue_entry wq;
7462 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007463 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007464 unsigned nr_timeouts;
7465};
7466
Pavel Begunkov6c503152021-01-04 20:36:36 +00007467static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007468{
7469 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007470 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007471
7472 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007473 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007474 * started waiting. For timeouts, we always want to return to userspace,
7475 * regardless of event count.
7476 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007477 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007478}
7479
7480static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7481 int wake_flags, void *key)
7482{
7483 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7484 wq);
7485
Pavel Begunkov6c503152021-01-04 20:36:36 +00007486 /*
7487 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7488 * the task, and the next invocation will do it.
7489 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007490 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007491 return autoremove_wake_function(curr, mode, wake_flags, key);
7492 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007493}
7494
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007495static int io_run_task_work_sig(void)
7496{
7497 if (io_run_task_work())
7498 return 1;
7499 if (!signal_pending(current))
7500 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007501 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007502 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007503 return -EINTR;
7504}
7505
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007506/* when returns >0, the caller should retry */
7507static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7508 struct io_wait_queue *iowq,
7509 signed long *timeout)
7510{
7511 int ret;
7512
7513 /* make sure we run task_work before checking for signals */
7514 ret = io_run_task_work_sig();
7515 if (ret || io_should_wake(iowq))
7516 return ret;
7517 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007518 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007519 return 1;
7520
7521 *timeout = schedule_timeout(*timeout);
7522 return !*timeout ? -ETIME : 1;
7523}
7524
Jens Axboe2b188cc2019-01-07 10:46:33 -07007525/*
7526 * Wait until events become available, if we don't already have some. The
7527 * application must reap them itself, as they reside on the shared cq ring.
7528 */
7529static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007530 const sigset_t __user *sig, size_t sigsz,
7531 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007532{
Pavel Begunkov902910992021-08-09 09:07:32 -06007533 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007534 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007535 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7536 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007537
Jens Axboeb41e9852020-02-17 09:52:41 -07007538 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007539 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007540 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007541 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007542 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007543 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007544 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007545
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007546 if (uts) {
7547 struct timespec64 ts;
7548
7549 if (get_timespec64(&ts, uts))
7550 return -EFAULT;
7551 timeout = timespec64_to_jiffies(&ts);
7552 }
7553
Jens Axboe2b188cc2019-01-07 10:46:33 -07007554 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007555#ifdef CONFIG_COMPAT
7556 if (in_compat_syscall())
7557 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007558 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007559 else
7560#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007561 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007562
Jens Axboe2b188cc2019-01-07 10:46:33 -07007563 if (ret)
7564 return ret;
7565 }
7566
Pavel Begunkov902910992021-08-09 09:07:32 -06007567 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7568 iowq.wq.private = current;
7569 INIT_LIST_HEAD(&iowq.wq.entry);
7570 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007571 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007572 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007573
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007574 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007575 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007576 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007577 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007578 ret = -EBUSY;
7579 break;
7580 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007581 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007582 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007583 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007584 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007585 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007586 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007587
Jens Axboeb7db41c2020-07-04 08:55:50 -06007588 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007589
Hristo Venev75b28af2019-08-26 17:23:46 +00007590 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007591}
7592
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007593static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007594{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007595 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007596
7597 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007598 kfree(table[i]);
7599 kfree(table);
7600}
7601
7602static void **io_alloc_page_table(size_t size)
7603{
7604 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7605 size_t init_size = size;
7606 void **table;
7607
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007608 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007609 if (!table)
7610 return NULL;
7611
7612 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007613 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007614
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007615 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007616 if (!table[i]) {
7617 io_free_page_table(table, init_size);
7618 return NULL;
7619 }
7620 size -= this_size;
7621 }
7622 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007623}
7624
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007625static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7626{
7627 percpu_ref_exit(&ref_node->refs);
7628 kfree(ref_node);
7629}
7630
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007631static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7632{
7633 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7634 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7635 unsigned long flags;
7636 bool first_add = false;
7637
7638 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7639 node->done = true;
7640
7641 while (!list_empty(&ctx->rsrc_ref_list)) {
7642 node = list_first_entry(&ctx->rsrc_ref_list,
7643 struct io_rsrc_node, node);
7644 /* recycle ref nodes in order */
7645 if (!node->done)
7646 break;
7647 list_del(&node->node);
7648 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7649 }
7650 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7651
7652 if (first_add)
7653 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7654}
7655
7656static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7657{
7658 struct io_rsrc_node *ref_node;
7659
7660 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7661 if (!ref_node)
7662 return NULL;
7663
7664 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7665 0, GFP_KERNEL)) {
7666 kfree(ref_node);
7667 return NULL;
7668 }
7669 INIT_LIST_HEAD(&ref_node->node);
7670 INIT_LIST_HEAD(&ref_node->rsrc_list);
7671 ref_node->done = false;
7672 return ref_node;
7673}
7674
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007675static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7676 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007677{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007678 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7679 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007680
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007681 if (data_to_kill) {
7682 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007683
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007684 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007685 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007686 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007687 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007688
Pavel Begunkov3e942492021-04-11 01:46:34 +01007689 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007690 percpu_ref_kill(&rsrc_node->refs);
7691 ctx->rsrc_node = NULL;
7692 }
7693
7694 if (!ctx->rsrc_node) {
7695 ctx->rsrc_node = ctx->rsrc_backup_node;
7696 ctx->rsrc_backup_node = NULL;
7697 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007698}
7699
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007700static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007701{
7702 if (ctx->rsrc_backup_node)
7703 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007704 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007705 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7706}
7707
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007708static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007709{
7710 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007711
Pavel Begunkov215c3902021-04-01 15:43:48 +01007712 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007713 if (data->quiesce)
7714 return -ENXIO;
7715
7716 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007717 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007718 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007719 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007720 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007721 io_rsrc_node_switch(ctx, data);
7722
Pavel Begunkov3e942492021-04-11 01:46:34 +01007723 /* kill initial ref, already quiesced if zero */
7724 if (atomic_dec_and_test(&data->refs))
7725 break;
Jens Axboec018db42021-08-09 08:15:50 -06007726 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007727 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007728 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007729 if (!ret) {
7730 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007731 break;
Jens Axboec018db42021-08-09 08:15:50 -06007732 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007733
Pavel Begunkov3e942492021-04-11 01:46:34 +01007734 atomic_inc(&data->refs);
7735 /* wait for all works potentially completing data->done */
7736 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007737 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007738
Hao Xu8bad28d2021-02-19 17:19:36 +08007739 ret = io_run_task_work_sig();
7740 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007741 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007742 data->quiesce = false;
7743
Hao Xu8bad28d2021-02-19 17:19:36 +08007744 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007745}
7746
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007747static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7748{
7749 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7750 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7751
7752 return &data->tags[table_idx][off];
7753}
7754
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007755static void io_rsrc_data_free(struct io_rsrc_data *data)
7756{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007757 size_t size = data->nr * sizeof(data->tags[0][0]);
7758
7759 if (data->tags)
7760 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007761 kfree(data);
7762}
7763
Pavel Begunkovd878c812021-06-14 02:36:18 +01007764static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7765 u64 __user *utags, unsigned nr,
7766 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007767{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007768 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007769 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007770 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007771
7772 data = kzalloc(sizeof(*data), GFP_KERNEL);
7773 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007774 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007775 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007776 if (!data->tags) {
7777 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007778 return -ENOMEM;
7779 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007780
7781 data->nr = nr;
7782 data->ctx = ctx;
7783 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007784 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007785 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007786 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007787 u64 *tag_slot = io_get_tag_slot(data, i);
7788
7789 if (copy_from_user(tag_slot, &utags[i],
7790 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007791 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007792 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007793 }
7794
Pavel Begunkov3e942492021-04-11 01:46:34 +01007795 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007796 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007797 *pdata = data;
7798 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007799fail:
7800 io_rsrc_data_free(data);
7801 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007802}
7803
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007804static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7805{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007806 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7807 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007808 return !!table->files;
7809}
7810
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007811static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007812{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007813 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007814 table->files = NULL;
7815}
7816
Jens Axboe2b188cc2019-01-07 10:46:33 -07007817static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7818{
7819#if defined(CONFIG_UNIX)
7820 if (ctx->ring_sock) {
7821 struct sock *sock = ctx->ring_sock->sk;
7822 struct sk_buff *skb;
7823
7824 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7825 kfree_skb(skb);
7826 }
7827#else
7828 int i;
7829
7830 for (i = 0; i < ctx->nr_user_files; i++) {
7831 struct file *file;
7832
7833 file = io_file_from_index(ctx, i);
7834 if (file)
7835 fput(file);
7836 }
7837#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007838 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007839 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007840 ctx->file_data = NULL;
7841 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007842}
7843
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007844static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7845{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007846 int ret;
7847
Pavel Begunkov08480402021-04-13 02:58:38 +01007848 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007849 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007850 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7851 if (!ret)
7852 __io_sqe_files_unregister(ctx);
7853 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007854}
7855
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007856static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007857 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007858{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007859 WARN_ON_ONCE(sqd->thread == current);
7860
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007861 /*
7862 * Do the dance but not conditional clear_bit() because it'd race with
7863 * other threads incrementing park_pending and setting the bit.
7864 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007865 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007866 if (atomic_dec_return(&sqd->park_pending))
7867 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007868 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007869}
7870
Jens Axboe86e0d672021-03-05 08:44:39 -07007871static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007872 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007873{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007874 WARN_ON_ONCE(sqd->thread == current);
7875
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007876 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007877 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007878 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007879 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007880 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007881}
7882
7883static void io_sq_thread_stop(struct io_sq_data *sqd)
7884{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007885 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007886 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007887
Jens Axboe05962f92021-03-06 13:58:48 -07007888 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007889 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007890 if (sqd->thread)
7891 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007892 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007893 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007894}
7895
Jens Axboe534ca6d2020-09-02 13:52:19 -06007896static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007897{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007898 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007899 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7900
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007901 io_sq_thread_stop(sqd);
7902 kfree(sqd);
7903 }
7904}
7905
7906static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7907{
7908 struct io_sq_data *sqd = ctx->sq_data;
7909
7910 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007911 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007912 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007913 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007914 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007915
7916 io_put_sq_data(sqd);
7917 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007918 }
7919}
7920
Jens Axboeaa061652020-09-02 14:50:27 -06007921static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7922{
7923 struct io_ring_ctx *ctx_attach;
7924 struct io_sq_data *sqd;
7925 struct fd f;
7926
7927 f = fdget(p->wq_fd);
7928 if (!f.file)
7929 return ERR_PTR(-ENXIO);
7930 if (f.file->f_op != &io_uring_fops) {
7931 fdput(f);
7932 return ERR_PTR(-EINVAL);
7933 }
7934
7935 ctx_attach = f.file->private_data;
7936 sqd = ctx_attach->sq_data;
7937 if (!sqd) {
7938 fdput(f);
7939 return ERR_PTR(-EINVAL);
7940 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007941 if (sqd->task_tgid != current->tgid) {
7942 fdput(f);
7943 return ERR_PTR(-EPERM);
7944 }
Jens Axboeaa061652020-09-02 14:50:27 -06007945
7946 refcount_inc(&sqd->refs);
7947 fdput(f);
7948 return sqd;
7949}
7950
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007951static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7952 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007953{
7954 struct io_sq_data *sqd;
7955
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007956 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007957 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7958 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007959 if (!IS_ERR(sqd)) {
7960 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007961 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007962 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007963 /* fall through for EPERM case, setup new sqd/task */
7964 if (PTR_ERR(sqd) != -EPERM)
7965 return sqd;
7966 }
Jens Axboeaa061652020-09-02 14:50:27 -06007967
Jens Axboe534ca6d2020-09-02 13:52:19 -06007968 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7969 if (!sqd)
7970 return ERR_PTR(-ENOMEM);
7971
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007972 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007973 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007974 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007975 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007976 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007977 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007978 return sqd;
7979}
7980
Jens Axboe6b063142019-01-10 22:13:58 -07007981#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007982/*
7983 * Ensure the UNIX gc is aware of our file set, so we are certain that
7984 * the io_uring can be safely unregistered on process exit, even if we have
7985 * loops in the file referencing.
7986 */
7987static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7988{
7989 struct sock *sk = ctx->ring_sock->sk;
7990 struct scm_fp_list *fpl;
7991 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007992 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007993
Jens Axboe6b063142019-01-10 22:13:58 -07007994 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7995 if (!fpl)
7996 return -ENOMEM;
7997
7998 skb = alloc_skb(0, GFP_KERNEL);
7999 if (!skb) {
8000 kfree(fpl);
8001 return -ENOMEM;
8002 }
8003
8004 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07008005
Jens Axboe08a45172019-10-03 08:11:03 -06008006 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07008007 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07008008 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008009 struct file *file = io_file_from_index(ctx, i + offset);
8010
8011 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06008012 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06008013 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06008014 unix_inflight(fpl->user, fpl->fp[nr_files]);
8015 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07008016 }
8017
Jens Axboe08a45172019-10-03 08:11:03 -06008018 if (nr_files) {
8019 fpl->max = SCM_MAX_FD;
8020 fpl->count = nr_files;
8021 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008022 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06008023 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8024 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008025
Jens Axboe08a45172019-10-03 08:11:03 -06008026 for (i = 0; i < nr_files; i++)
8027 fput(fpl->fp[i]);
8028 } else {
8029 kfree_skb(skb);
8030 kfree(fpl);
8031 }
Jens Axboe6b063142019-01-10 22:13:58 -07008032
8033 return 0;
8034}
8035
8036/*
8037 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8038 * causes regular reference counting to break down. We rely on the UNIX
8039 * garbage collection to take care of this problem for us.
8040 */
8041static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8042{
8043 unsigned left, total;
8044 int ret = 0;
8045
8046 total = 0;
8047 left = ctx->nr_user_files;
8048 while (left) {
8049 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008050
8051 ret = __io_sqe_files_scm(ctx, this_files, total);
8052 if (ret)
8053 break;
8054 left -= this_files;
8055 total += this_files;
8056 }
8057
8058 if (!ret)
8059 return 0;
8060
8061 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008062 struct file *file = io_file_from_index(ctx, total);
8063
8064 if (file)
8065 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008066 total++;
8067 }
8068
8069 return ret;
8070}
8071#else
8072static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8073{
8074 return 0;
8075}
8076#endif
8077
Pavel Begunkov47e90392021-04-01 15:43:56 +01008078static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008079{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008080 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008081#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008082 struct sock *sock = ctx->ring_sock->sk;
8083 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8084 struct sk_buff *skb;
8085 int i;
8086
8087 __skb_queue_head_init(&list);
8088
8089 /*
8090 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8091 * remove this entry and rearrange the file array.
8092 */
8093 skb = skb_dequeue(head);
8094 while (skb) {
8095 struct scm_fp_list *fp;
8096
8097 fp = UNIXCB(skb).fp;
8098 for (i = 0; i < fp->count; i++) {
8099 int left;
8100
8101 if (fp->fp[i] != file)
8102 continue;
8103
8104 unix_notinflight(fp->user, fp->fp[i]);
8105 left = fp->count - 1 - i;
8106 if (left) {
8107 memmove(&fp->fp[i], &fp->fp[i + 1],
8108 left * sizeof(struct file *));
8109 }
8110 fp->count--;
8111 if (!fp->count) {
8112 kfree_skb(skb);
8113 skb = NULL;
8114 } else {
8115 __skb_queue_tail(&list, skb);
8116 }
8117 fput(file);
8118 file = NULL;
8119 break;
8120 }
8121
8122 if (!file)
8123 break;
8124
8125 __skb_queue_tail(&list, skb);
8126
8127 skb = skb_dequeue(head);
8128 }
8129
8130 if (skb_peek(&list)) {
8131 spin_lock_irq(&head->lock);
8132 while ((skb = __skb_dequeue(&list)) != NULL)
8133 __skb_queue_tail(head, skb);
8134 spin_unlock_irq(&head->lock);
8135 }
8136#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008137 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008138#endif
8139}
8140
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008141static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008142{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008143 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008144 struct io_ring_ctx *ctx = rsrc_data->ctx;
8145 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008146
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008147 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8148 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008149
8150 if (prsrc->tag) {
8151 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008152
8153 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008154 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008155 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01008156 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008157 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008158 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008159 io_cqring_ev_posted(ctx);
8160 io_ring_submit_unlock(ctx, lock_ring);
8161 }
8162
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008163 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008164 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008165 }
8166
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008167 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008168 if (atomic_dec_and_test(&rsrc_data->refs))
8169 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008170}
8171
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008172static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008173{
8174 struct io_ring_ctx *ctx;
8175 struct llist_node *node;
8176
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008177 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8178 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008179
8180 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008181 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008182 struct llist_node *next = node->next;
8183
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008184 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008185 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008186 node = next;
8187 }
8188}
8189
Jens Axboe05f3fb32019-12-09 11:22:50 -07008190static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008191 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008192{
8193 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008194 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008195 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008196 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008197
8198 if (ctx->file_data)
8199 return -EBUSY;
8200 if (!nr_args)
8201 return -EINVAL;
8202 if (nr_args > IORING_MAX_FIXED_FILES)
8203 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008204 if (nr_args > rlimit(RLIMIT_NOFILE))
8205 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008206 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008207 if (ret)
8208 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008209 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8210 &ctx->file_data);
8211 if (ret)
8212 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008213
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008214 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008215 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008216 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008217
Jens Axboe05f3fb32019-12-09 11:22:50 -07008218 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008219 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008220 ret = -EFAULT;
8221 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008222 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008223 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008224 if (fd == -1) {
8225 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008226 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008227 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008228 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008229 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008230
Jens Axboe05f3fb32019-12-09 11:22:50 -07008231 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008232 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008233 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008234 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008235
8236 /*
8237 * Don't allow io_uring instances to be registered. If UNIX
8238 * isn't enabled, then this causes a reference cycle and this
8239 * instance can never get freed. If UNIX is enabled we'll
8240 * handle it just fine, but there's still no point in allowing
8241 * a ring fd as it doesn't support regular read/write anyway.
8242 */
8243 if (file->f_op == &io_uring_fops) {
8244 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008245 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008246 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008247 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008248 }
8249
Jens Axboe05f3fb32019-12-09 11:22:50 -07008250 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008251 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008252 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008253 return ret;
8254 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008255
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008256 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008257 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008258out_fput:
8259 for (i = 0; i < ctx->nr_user_files; i++) {
8260 file = io_file_from_index(ctx, i);
8261 if (file)
8262 fput(file);
8263 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008264 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008265 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008266out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008267 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008268 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008269 return ret;
8270}
8271
Jens Axboec3a31e62019-10-03 13:59:56 -06008272static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8273 int index)
8274{
8275#if defined(CONFIG_UNIX)
8276 struct sock *sock = ctx->ring_sock->sk;
8277 struct sk_buff_head *head = &sock->sk_receive_queue;
8278 struct sk_buff *skb;
8279
8280 /*
8281 * See if we can merge this file into an existing skb SCM_RIGHTS
8282 * file set. If there's no room, fall back to allocating a new skb
8283 * and filling it in.
8284 */
8285 spin_lock_irq(&head->lock);
8286 skb = skb_peek(head);
8287 if (skb) {
8288 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8289
8290 if (fpl->count < SCM_MAX_FD) {
8291 __skb_unlink(skb, head);
8292 spin_unlock_irq(&head->lock);
8293 fpl->fp[fpl->count] = get_file(file);
8294 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8295 fpl->count++;
8296 spin_lock_irq(&head->lock);
8297 __skb_queue_head(head, skb);
8298 } else {
8299 skb = NULL;
8300 }
8301 }
8302 spin_unlock_irq(&head->lock);
8303
8304 if (skb) {
8305 fput(file);
8306 return 0;
8307 }
8308
8309 return __io_sqe_files_scm(ctx, 1, index);
8310#else
8311 return 0;
8312#endif
8313}
8314
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008315static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8316 struct io_rsrc_node *node, void *rsrc)
8317{
8318 struct io_rsrc_put *prsrc;
8319
8320 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8321 if (!prsrc)
8322 return -ENOMEM;
8323
8324 prsrc->tag = *io_get_tag_slot(data, idx);
8325 prsrc->rsrc = rsrc;
8326 list_add(&prsrc->list, &node->rsrc_list);
8327 return 0;
8328}
8329
Pavel Begunkovb9445592021-08-25 12:25:45 +01008330static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8331 unsigned int issue_flags, u32 slot_index)
8332{
8333 struct io_ring_ctx *ctx = req->ctx;
8334 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008335 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008336 struct io_fixed_file *file_slot;
8337 int ret = -EBADF;
8338
8339 io_ring_submit_lock(ctx, !force_nonblock);
8340 if (file->f_op == &io_uring_fops)
8341 goto err;
8342 ret = -ENXIO;
8343 if (!ctx->file_data)
8344 goto err;
8345 ret = -EINVAL;
8346 if (slot_index >= ctx->nr_user_files)
8347 goto err;
8348
8349 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8350 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008351
8352 if (file_slot->file_ptr) {
8353 struct file *old_file;
8354
8355 ret = io_rsrc_node_switch_start(ctx);
8356 if (ret)
8357 goto err;
8358
8359 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8360 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8361 ctx->rsrc_node, old_file);
8362 if (ret)
8363 goto err;
8364 file_slot->file_ptr = 0;
8365 needs_switch = true;
8366 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008367
8368 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8369 io_fixed_file_set(file_slot, file);
8370 ret = io_sqe_file_register(ctx, file, slot_index);
8371 if (ret) {
8372 file_slot->file_ptr = 0;
8373 goto err;
8374 }
8375
8376 ret = 0;
8377err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008378 if (needs_switch)
8379 io_rsrc_node_switch(ctx, ctx->file_data);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008380 io_ring_submit_unlock(ctx, !force_nonblock);
8381 if (ret)
8382 fput(file);
8383 return ret;
8384}
8385
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008386static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8387{
8388 unsigned int offset = req->close.file_slot - 1;
8389 struct io_ring_ctx *ctx = req->ctx;
8390 struct io_fixed_file *file_slot;
8391 struct file *file;
8392 int ret, i;
8393
8394 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
8395 ret = -ENXIO;
8396 if (unlikely(!ctx->file_data))
8397 goto out;
8398 ret = -EINVAL;
8399 if (offset >= ctx->nr_user_files)
8400 goto out;
8401 ret = io_rsrc_node_switch_start(ctx);
8402 if (ret)
8403 goto out;
8404
8405 i = array_index_nospec(offset, ctx->nr_user_files);
8406 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8407 ret = -EBADF;
8408 if (!file_slot->file_ptr)
8409 goto out;
8410
8411 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8412 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8413 if (ret)
8414 goto out;
8415
8416 file_slot->file_ptr = 0;
8417 io_rsrc_node_switch(ctx, ctx->file_data);
8418 ret = 0;
8419out:
8420 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
8421 return ret;
8422}
8423
Jens Axboe05f3fb32019-12-09 11:22:50 -07008424static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008425 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008426 unsigned nr_args)
8427{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008428 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008429 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008430 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008431 struct io_fixed_file *file_slot;
8432 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008433 int fd, i, err = 0;
8434 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008435 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008436
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008437 if (!ctx->file_data)
8438 return -ENXIO;
8439 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008440 return -EINVAL;
8441
Pavel Begunkov67973b92021-01-26 13:51:09 +00008442 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008443 u64 tag = 0;
8444
8445 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8446 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008447 err = -EFAULT;
8448 break;
8449 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008450 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8451 err = -EINVAL;
8452 break;
8453 }
noah4e0377a2021-01-26 15:23:28 -05008454 if (fd == IORING_REGISTER_FILES_SKIP)
8455 continue;
8456
Pavel Begunkov67973b92021-01-26 13:51:09 +00008457 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008458 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008459
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008460 if (file_slot->file_ptr) {
8461 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008462 err = io_queue_rsrc_removal(data, up->offset + done,
8463 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008464 if (err)
8465 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008466 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008467 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008468 }
8469 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008470 file = fget(fd);
8471 if (!file) {
8472 err = -EBADF;
8473 break;
8474 }
8475 /*
8476 * Don't allow io_uring instances to be registered. If
8477 * UNIX isn't enabled, then this causes a reference
8478 * cycle and this instance can never get freed. If UNIX
8479 * is enabled we'll handle it just fine, but there's
8480 * still no point in allowing a ring fd as it doesn't
8481 * support regular read/write anyway.
8482 */
8483 if (file->f_op == &io_uring_fops) {
8484 fput(file);
8485 err = -EBADF;
8486 break;
8487 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008488 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008489 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008490 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008491 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008492 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008493 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008494 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008495 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008496 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008497 }
8498
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008499 if (needs_switch)
8500 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008501 return done ? done : err;
8502}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008503
Jens Axboe685fe7f2021-03-08 09:37:51 -07008504static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8505 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008506{
Jens Axboee9418942021-02-19 12:33:30 -07008507 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008508 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008509 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008510
Yang Yingliang362a9e62021-07-20 16:38:05 +08008511 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008512 hash = ctx->hash_map;
8513 if (!hash) {
8514 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008515 if (!hash) {
8516 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008517 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008518 }
Jens Axboee9418942021-02-19 12:33:30 -07008519 refcount_set(&hash->refs, 1);
8520 init_waitqueue_head(&hash->wait);
8521 ctx->hash_map = hash;
8522 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008523 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008524
8525 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008526 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008527 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008528 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008529
Jens Axboed25e3a32021-02-16 11:41:41 -07008530 /* Do QD, or 4 * CPUS, whatever is smallest */
8531 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008532
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008533 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008534}
8535
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008536static int io_uring_alloc_task_context(struct task_struct *task,
8537 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008538{
8539 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008540 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008541
Pavel Begunkov09899b12021-06-14 02:36:22 +01008542 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008543 if (unlikely(!tctx))
8544 return -ENOMEM;
8545
Jens Axboed8a6df12020-10-15 16:24:45 -06008546 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8547 if (unlikely(ret)) {
8548 kfree(tctx);
8549 return ret;
8550 }
8551
Jens Axboe685fe7f2021-03-08 09:37:51 -07008552 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008553 if (IS_ERR(tctx->io_wq)) {
8554 ret = PTR_ERR(tctx->io_wq);
8555 percpu_counter_destroy(&tctx->inflight);
8556 kfree(tctx);
8557 return ret;
8558 }
8559
Jens Axboe0f212202020-09-13 13:09:39 -06008560 xa_init(&tctx->xa);
8561 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008562 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008563 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008564 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008565 spin_lock_init(&tctx->task_lock);
8566 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008567 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008568 return 0;
8569}
8570
8571void __io_uring_free(struct task_struct *tsk)
8572{
8573 struct io_uring_task *tctx = tsk->io_uring;
8574
8575 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008576 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008577 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008578
Jens Axboed8a6df12020-10-15 16:24:45 -06008579 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008580 kfree(tctx);
8581 tsk->io_uring = NULL;
8582}
8583
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008584static int io_sq_offload_create(struct io_ring_ctx *ctx,
8585 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008586{
8587 int ret;
8588
Jens Axboed25e3a32021-02-16 11:41:41 -07008589 /* Retain compatibility with failing for an invalid attach attempt */
8590 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8591 IORING_SETUP_ATTACH_WQ) {
8592 struct fd f;
8593
8594 f = fdget(p->wq_fd);
8595 if (!f.file)
8596 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008597 if (f.file->f_op != &io_uring_fops) {
8598 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008599 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008600 }
8601 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008602 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008603 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008604 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008605 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008606 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008607
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008608 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008609 if (IS_ERR(sqd)) {
8610 ret = PTR_ERR(sqd);
8611 goto err;
8612 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008613
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008614 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008615 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008616 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8617 if (!ctx->sq_thread_idle)
8618 ctx->sq_thread_idle = HZ;
8619
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008620 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008621 list_add(&ctx->sqd_list, &sqd->ctx_list);
8622 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008623 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008624 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008625 io_sq_thread_unpark(sqd);
8626
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008627 if (ret < 0)
8628 goto err;
8629 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008630 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008631
Jens Axboe6c271ce2019-01-10 11:22:30 -07008632 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008633 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008634
Jens Axboe917257d2019-04-13 09:28:55 -06008635 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008636 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008637 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008638 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008639 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008640 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008641 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008642
8643 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008644 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008645 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8646 if (IS_ERR(tsk)) {
8647 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008648 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008649 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008650
Jens Axboe46fe18b2021-03-04 12:39:36 -07008651 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008652 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008653 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008654 if (ret)
8655 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008656 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8657 /* Can't have SQ_AFF without SQPOLL */
8658 ret = -EINVAL;
8659 goto err;
8660 }
8661
Jens Axboe2b188cc2019-01-07 10:46:33 -07008662 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008663err_sqpoll:
8664 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008665err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008666 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008667 return ret;
8668}
8669
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008670static inline void __io_unaccount_mem(struct user_struct *user,
8671 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008672{
8673 atomic_long_sub(nr_pages, &user->locked_vm);
8674}
8675
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008676static inline int __io_account_mem(struct user_struct *user,
8677 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008678{
8679 unsigned long page_limit, cur_pages, new_pages;
8680
8681 /* Don't allow more pages than we can safely lock */
8682 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8683
8684 do {
8685 cur_pages = atomic_long_read(&user->locked_vm);
8686 new_pages = cur_pages + nr_pages;
8687 if (new_pages > page_limit)
8688 return -ENOMEM;
8689 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8690 new_pages) != cur_pages);
8691
8692 return 0;
8693}
8694
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008695static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008696{
Jens Axboe62e398b2021-02-21 16:19:37 -07008697 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008698 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008699
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008700 if (ctx->mm_account)
8701 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008702}
8703
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008704static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008705{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008706 int ret;
8707
Jens Axboe62e398b2021-02-21 16:19:37 -07008708 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008709 ret = __io_account_mem(ctx->user, nr_pages);
8710 if (ret)
8711 return ret;
8712 }
8713
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008714 if (ctx->mm_account)
8715 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008716
8717 return 0;
8718}
8719
Jens Axboe2b188cc2019-01-07 10:46:33 -07008720static void io_mem_free(void *ptr)
8721{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008722 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008723
Mark Rutland52e04ef2019-04-30 17:30:21 +01008724 if (!ptr)
8725 return;
8726
8727 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008728 if (put_page_testzero(page))
8729 free_compound_page(page);
8730}
8731
8732static void *io_mem_alloc(size_t size)
8733{
8734 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008735 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008736
8737 return (void *) __get_free_pages(gfp_flags, get_order(size));
8738}
8739
Hristo Venev75b28af2019-08-26 17:23:46 +00008740static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8741 size_t *sq_offset)
8742{
8743 struct io_rings *rings;
8744 size_t off, sq_array_size;
8745
8746 off = struct_size(rings, cqes, cq_entries);
8747 if (off == SIZE_MAX)
8748 return SIZE_MAX;
8749
8750#ifdef CONFIG_SMP
8751 off = ALIGN(off, SMP_CACHE_BYTES);
8752 if (off == 0)
8753 return SIZE_MAX;
8754#endif
8755
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008756 if (sq_offset)
8757 *sq_offset = off;
8758
Hristo Venev75b28af2019-08-26 17:23:46 +00008759 sq_array_size = array_size(sizeof(u32), sq_entries);
8760 if (sq_array_size == SIZE_MAX)
8761 return SIZE_MAX;
8762
8763 if (check_add_overflow(off, sq_array_size, &off))
8764 return SIZE_MAX;
8765
Hristo Venev75b28af2019-08-26 17:23:46 +00008766 return off;
8767}
8768
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008769static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008770{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008771 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008772 unsigned int i;
8773
Pavel Begunkov62248432021-04-28 13:11:29 +01008774 if (imu != ctx->dummy_ubuf) {
8775 for (i = 0; i < imu->nr_bvecs; i++)
8776 unpin_user_page(imu->bvec[i].bv_page);
8777 if (imu->acct_pages)
8778 io_unaccount_mem(ctx, imu->acct_pages);
8779 kvfree(imu);
8780 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008781 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008782}
8783
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008784static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8785{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008786 io_buffer_unmap(ctx, &prsrc->buf);
8787 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008788}
8789
8790static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008791{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008792 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008793
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008794 for (i = 0; i < ctx->nr_user_bufs; i++)
8795 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008796 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008797 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008798 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008799 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008800 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008801}
8802
Jens Axboeedafcce2019-01-09 09:16:05 -07008803static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8804{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008805 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008806
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008807 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008808 return -ENXIO;
8809
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008810 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8811 if (!ret)
8812 __io_sqe_buffers_unregister(ctx);
8813 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008814}
8815
8816static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8817 void __user *arg, unsigned index)
8818{
8819 struct iovec __user *src;
8820
8821#ifdef CONFIG_COMPAT
8822 if (ctx->compat) {
8823 struct compat_iovec __user *ciovs;
8824 struct compat_iovec ciov;
8825
8826 ciovs = (struct compat_iovec __user *) arg;
8827 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8828 return -EFAULT;
8829
Jens Axboed55e5f52019-12-11 16:12:15 -07008830 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008831 dst->iov_len = ciov.iov_len;
8832 return 0;
8833 }
8834#endif
8835 src = (struct iovec __user *) arg;
8836 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8837 return -EFAULT;
8838 return 0;
8839}
8840
Jens Axboede293932020-09-17 16:19:16 -06008841/*
8842 * Not super efficient, but this is just a registration time. And we do cache
8843 * the last compound head, so generally we'll only do a full search if we don't
8844 * match that one.
8845 *
8846 * We check if the given compound head page has already been accounted, to
8847 * avoid double accounting it. This allows us to account the full size of the
8848 * page, not just the constituent pages of a huge page.
8849 */
8850static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8851 int nr_pages, struct page *hpage)
8852{
8853 int i, j;
8854
8855 /* check current page array */
8856 for (i = 0; i < nr_pages; i++) {
8857 if (!PageCompound(pages[i]))
8858 continue;
8859 if (compound_head(pages[i]) == hpage)
8860 return true;
8861 }
8862
8863 /* check previously registered pages */
8864 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008865 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008866
8867 for (j = 0; j < imu->nr_bvecs; j++) {
8868 if (!PageCompound(imu->bvec[j].bv_page))
8869 continue;
8870 if (compound_head(imu->bvec[j].bv_page) == hpage)
8871 return true;
8872 }
8873 }
8874
8875 return false;
8876}
8877
8878static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8879 int nr_pages, struct io_mapped_ubuf *imu,
8880 struct page **last_hpage)
8881{
8882 int i, ret;
8883
Pavel Begunkov216e5832021-05-29 12:01:02 +01008884 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008885 for (i = 0; i < nr_pages; i++) {
8886 if (!PageCompound(pages[i])) {
8887 imu->acct_pages++;
8888 } else {
8889 struct page *hpage;
8890
8891 hpage = compound_head(pages[i]);
8892 if (hpage == *last_hpage)
8893 continue;
8894 *last_hpage = hpage;
8895 if (headpage_already_acct(ctx, pages, i, hpage))
8896 continue;
8897 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8898 }
8899 }
8900
8901 if (!imu->acct_pages)
8902 return 0;
8903
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008904 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008905 if (ret)
8906 imu->acct_pages = 0;
8907 return ret;
8908}
8909
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008910static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008911 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008912 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008913{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008914 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008915 struct vm_area_struct **vmas = NULL;
8916 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008917 unsigned long off, start, end, ubuf;
8918 size_t size;
8919 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008920
Pavel Begunkov62248432021-04-28 13:11:29 +01008921 if (!iov->iov_base) {
8922 *pimu = ctx->dummy_ubuf;
8923 return 0;
8924 }
8925
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008926 ubuf = (unsigned long) iov->iov_base;
8927 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8928 start = ubuf >> PAGE_SHIFT;
8929 nr_pages = end - start;
8930
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008931 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008932 ret = -ENOMEM;
8933
8934 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8935 if (!pages)
8936 goto done;
8937
8938 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8939 GFP_KERNEL);
8940 if (!vmas)
8941 goto done;
8942
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008943 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008944 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008945 goto done;
8946
8947 ret = 0;
8948 mmap_read_lock(current->mm);
8949 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8950 pages, vmas);
8951 if (pret == nr_pages) {
8952 /* don't support file backed memory */
8953 for (i = 0; i < nr_pages; i++) {
8954 struct vm_area_struct *vma = vmas[i];
8955
Pavel Begunkov40dad762021-06-09 15:26:54 +01008956 if (vma_is_shmem(vma))
8957 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008958 if (vma->vm_file &&
8959 !is_file_hugepages(vma->vm_file)) {
8960 ret = -EOPNOTSUPP;
8961 break;
8962 }
8963 }
8964 } else {
8965 ret = pret < 0 ? pret : -EFAULT;
8966 }
8967 mmap_read_unlock(current->mm);
8968 if (ret) {
8969 /*
8970 * if we did partial map, or found file backed vmas,
8971 * release any pages we did get
8972 */
8973 if (pret > 0)
8974 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008975 goto done;
8976 }
8977
8978 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8979 if (ret) {
8980 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008981 goto done;
8982 }
8983
8984 off = ubuf & ~PAGE_MASK;
8985 size = iov->iov_len;
8986 for (i = 0; i < nr_pages; i++) {
8987 size_t vec_len;
8988
8989 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8990 imu->bvec[i].bv_page = pages[i];
8991 imu->bvec[i].bv_len = vec_len;
8992 imu->bvec[i].bv_offset = off;
8993 off = 0;
8994 size -= vec_len;
8995 }
8996 /* store original address for later verification */
8997 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008998 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008999 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009000 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009001 ret = 0;
9002done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009003 if (ret)
9004 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009005 kvfree(pages);
9006 kvfree(vmas);
9007 return ret;
9008}
9009
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009010static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009011{
Pavel Begunkov87094462021-04-11 01:46:36 +01009012 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
9013 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009014}
9015
9016static int io_buffer_validate(struct iovec *iov)
9017{
Pavel Begunkov50e96982021-03-24 22:59:01 +00009018 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
9019
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009020 /*
9021 * Don't impose further limits on the size and buffer
9022 * constraints here, we'll -EINVAL later when IO is
9023 * submitted if they are wrong.
9024 */
Pavel Begunkov62248432021-04-28 13:11:29 +01009025 if (!iov->iov_base)
9026 return iov->iov_len ? -EFAULT : 0;
9027 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009028 return -EFAULT;
9029
9030 /* arbitrary limit, but we need something */
9031 if (iov->iov_len > SZ_1G)
9032 return -EFAULT;
9033
Pavel Begunkov50e96982021-03-24 22:59:01 +00009034 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
9035 return -EOVERFLOW;
9036
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009037 return 0;
9038}
9039
9040static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009041 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009042{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009043 struct page *last_hpage = NULL;
9044 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009045 int i, ret;
9046 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009047
Pavel Begunkov87094462021-04-11 01:46:36 +01009048 if (ctx->user_bufs)
9049 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009050 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009051 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009052 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009053 if (ret)
9054 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009055 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9056 if (ret)
9057 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009058 ret = io_buffers_map_alloc(ctx, nr_args);
9059 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009060 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009061 return ret;
9062 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009063
Pavel Begunkov87094462021-04-11 01:46:36 +01009064 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009065 ret = io_copy_iov(ctx, &iov, arg, i);
9066 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009067 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009068 ret = io_buffer_validate(&iov);
9069 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009070 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009071 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009072 ret = -EINVAL;
9073 break;
9074 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009075
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009076 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9077 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009078 if (ret)
9079 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009080 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009081
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009082 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009083
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009084 ctx->buf_data = data;
9085 if (ret)
9086 __io_sqe_buffers_unregister(ctx);
9087 else
9088 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009089 return ret;
9090}
9091
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009092static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9093 struct io_uring_rsrc_update2 *up,
9094 unsigned int nr_args)
9095{
9096 u64 __user *tags = u64_to_user_ptr(up->tags);
9097 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009098 struct page *last_hpage = NULL;
9099 bool needs_switch = false;
9100 __u32 done;
9101 int i, err;
9102
9103 if (!ctx->buf_data)
9104 return -ENXIO;
9105 if (up->offset + nr_args > ctx->nr_user_bufs)
9106 return -EINVAL;
9107
9108 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009109 struct io_mapped_ubuf *imu;
9110 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009111 u64 tag = 0;
9112
9113 err = io_copy_iov(ctx, &iov, iovs, done);
9114 if (err)
9115 break;
9116 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9117 err = -EFAULT;
9118 break;
9119 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009120 err = io_buffer_validate(&iov);
9121 if (err)
9122 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009123 if (!iov.iov_base && tag) {
9124 err = -EINVAL;
9125 break;
9126 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009127 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9128 if (err)
9129 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009130
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009131 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009132 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009133 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9134 ctx->rsrc_node, ctx->user_bufs[i]);
9135 if (unlikely(err)) {
9136 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009137 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009138 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009139 ctx->user_bufs[i] = NULL;
9140 needs_switch = true;
9141 }
9142
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009143 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009144 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009145 }
9146
9147 if (needs_switch)
9148 io_rsrc_node_switch(ctx, ctx->buf_data);
9149 return done ? done : err;
9150}
9151
Jens Axboe9b402842019-04-11 11:45:41 -06009152static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9153{
9154 __s32 __user *fds = arg;
9155 int fd;
9156
9157 if (ctx->cq_ev_fd)
9158 return -EBUSY;
9159
9160 if (copy_from_user(&fd, fds, sizeof(*fds)))
9161 return -EFAULT;
9162
9163 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9164 if (IS_ERR(ctx->cq_ev_fd)) {
9165 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009166
Jens Axboe9b402842019-04-11 11:45:41 -06009167 ctx->cq_ev_fd = NULL;
9168 return ret;
9169 }
9170
9171 return 0;
9172}
9173
9174static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9175{
9176 if (ctx->cq_ev_fd) {
9177 eventfd_ctx_put(ctx->cq_ev_fd);
9178 ctx->cq_ev_fd = NULL;
9179 return 0;
9180 }
9181
9182 return -ENXIO;
9183}
9184
Jens Axboe5a2e7452020-02-23 16:23:11 -07009185static void io_destroy_buffers(struct io_ring_ctx *ctx)
9186{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009187 struct io_buffer *buf;
9188 unsigned long index;
9189
Jens Axboe8bab4c02021-09-24 07:12:27 -06009190 xa_for_each(&ctx->io_buffers, index, buf) {
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009191 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009192 cond_resched();
9193 }
Jens Axboe5a2e7452020-02-23 16:23:11 -07009194}
9195
Pavel Begunkov72558342021-08-09 20:18:09 +01009196static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00009197{
Jens Axboe68e68ee2021-02-13 09:00:02 -07009198 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00009199
Pavel Begunkovbb943b82021-08-09 20:18:10 +01009200 list_for_each_entry_safe(req, nxt, list, inflight_entry) {
9201 list_del(&req->inflight_entry);
Jens Axboe1b4c3512021-02-10 00:03:19 +00009202 kmem_cache_free(req_cachep, req);
9203 }
9204}
9205
Jens Axboe4010fec2021-02-27 15:04:18 -07009206static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009207{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009208 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009209
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009210 mutex_lock(&ctx->uring_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009211 io_flush_cached_locked_reqs(ctx, state);
9212 io_req_cache_free(&state->free_list);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009213 mutex_unlock(&ctx->uring_lock);
9214}
9215
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009216static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009217{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009218 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009219 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009220}
9221
Jens Axboe2b188cc2019-01-07 10:46:33 -07009222static void io_ring_ctx_free(struct io_ring_ctx *ctx)
9223{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009224 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009225
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009226 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009227 mmdrop(ctx->mm_account);
9228 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009229 }
Jens Axboedef596e2019-01-09 08:59:42 -07009230
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009231 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9232 io_wait_rsrc_data(ctx->buf_data);
9233 io_wait_rsrc_data(ctx->file_data);
9234
Hao Xu8bad28d2021-02-19 17:19:36 +08009235 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009236 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009237 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009238 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009239 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009240 if (ctx->rings)
9241 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009242 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009243 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009244 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009245 if (ctx->sq_creds)
9246 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009247
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009248 /* there are no registered resources left, nobody uses it */
9249 if (ctx->rsrc_node)
9250 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009251 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009252 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009253 flush_delayed_work(&ctx->rsrc_put_work);
9254
9255 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9256 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009257
9258#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009259 if (ctx->ring_sock) {
9260 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009261 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009262 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009263#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009264 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009265
Hristo Venev75b28af2019-08-26 17:23:46 +00009266 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009267 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009268
9269 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009270 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009271 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009272 if (ctx->hash_map)
9273 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009274 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009275 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009276 kfree(ctx);
9277}
9278
9279static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9280{
9281 struct io_ring_ctx *ctx = file->private_data;
9282 __poll_t mask = 0;
9283
Pavel Begunkov311997b2021-06-14 23:37:28 +01009284 poll_wait(file, &ctx->poll_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009285 /*
9286 * synchronizes with barrier from wq_has_sleeper call in
9287 * io_commit_cqring
9288 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009289 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009290 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009291 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009292
9293 /*
9294 * Don't flush cqring overflow list here, just do a simple check.
9295 * Otherwise there could possible be ABBA deadlock:
9296 * CPU0 CPU1
9297 * ---- ----
9298 * lock(&ctx->uring_lock);
9299 * lock(&ep->mtx);
9300 * lock(&ctx->uring_lock);
9301 * lock(&ep->mtx);
9302 *
9303 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9304 * pushs them to do the flush.
9305 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009306 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009307 mask |= EPOLLIN | EPOLLRDNORM;
9308
9309 return mask;
9310}
9311
Yejune Deng0bead8c2020-12-24 11:02:20 +08009312static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009313{
Jens Axboe4379bf82021-02-15 13:40:22 -07009314 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009315
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009316 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009317 if (creds) {
9318 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009319 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009320 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009321
9322 return -EINVAL;
9323}
9324
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009325struct io_tctx_exit {
9326 struct callback_head task_work;
9327 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009328 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009329};
9330
9331static void io_tctx_exit_cb(struct callback_head *cb)
9332{
9333 struct io_uring_task *tctx = current->io_uring;
9334 struct io_tctx_exit *work;
9335
9336 work = container_of(cb, struct io_tctx_exit, task_work);
9337 /*
9338 * When @in_idle, we're in cancellation and it's racy to remove the
9339 * node. It'll be removed by the end of cancellation, just ignore it.
9340 */
9341 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009342 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009343 complete(&work->completion);
9344}
9345
Pavel Begunkov28090c12021-04-25 23:34:45 +01009346static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
9347{
9348 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9349
9350 return req->ctx == data;
9351}
9352
Jens Axboe85faa7b2020-04-09 18:14:00 -06009353static void io_ring_exit_work(struct work_struct *work)
9354{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009355 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009356 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009357 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009358 struct io_tctx_exit exit;
9359 struct io_tctx_node *node;
9360 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009361
Jens Axboe56952e92020-06-17 15:00:04 -06009362 /*
9363 * If we're doing polled IO and end up having requests being
9364 * submitted async (out-of-line), then completions can come in while
9365 * we're waiting for refs to drop. We need to reap these manually,
9366 * as nobody else will be looking for them.
9367 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009368 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009369 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009370 if (ctx->sq_data) {
9371 struct io_sq_data *sqd = ctx->sq_data;
9372 struct task_struct *tsk;
9373
9374 io_sq_thread_park(sqd);
9375 tsk = sqd->thread;
9376 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9377 io_wq_cancel_cb(tsk->io_uring->io_wq,
9378 io_cancel_ctx_cb, ctx, true);
9379 io_sq_thread_unpark(sqd);
9380 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009381
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009382 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9383 /* there is little hope left, don't run it too often */
9384 interval = HZ * 60;
9385 }
9386 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009387
Pavel Begunkov7f006512021-04-14 13:38:34 +01009388 init_completion(&exit.completion);
9389 init_task_work(&exit.task_work, io_tctx_exit_cb);
9390 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009391 /*
9392 * Some may use context even when all refs and requests have been put,
9393 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009394 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009395 * this lock/unlock section also waits them to finish.
9396 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009397 mutex_lock(&ctx->uring_lock);
9398 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009399 WARN_ON_ONCE(time_after(jiffies, timeout));
9400
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009401 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9402 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009403 /* don't spin on a single task if cancellation failed */
9404 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009405 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9406 if (WARN_ON_ONCE(ret))
9407 continue;
9408 wake_up_process(node->task);
9409
9410 mutex_unlock(&ctx->uring_lock);
9411 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009412 mutex_lock(&ctx->uring_lock);
9413 }
9414 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009415 spin_lock(&ctx->completion_lock);
9416 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009417
Jens Axboe85faa7b2020-04-09 18:14:00 -06009418 io_ring_ctx_free(ctx);
9419}
9420
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009421/* Returns true if we found and killed one or more timeouts */
9422static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009423 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009424{
9425 struct io_kiocb *req, *tmp;
9426 int canceled = 0;
9427
Jens Axboe79ebeae2021-08-10 15:18:27 -06009428 spin_lock(&ctx->completion_lock);
9429 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009430 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009431 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009432 io_kill_timeout(req, -ECANCELED);
9433 canceled++;
9434 }
9435 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009436 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009437 if (canceled != 0)
9438 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009439 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009440 if (canceled != 0)
9441 io_cqring_ev_posted(ctx);
9442 return canceled != 0;
9443}
9444
Jens Axboe2b188cc2019-01-07 10:46:33 -07009445static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9446{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009447 unsigned long index;
9448 struct creds *creds;
9449
Jens Axboe2b188cc2019-01-07 10:46:33 -07009450 mutex_lock(&ctx->uring_lock);
9451 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009452 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009453 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009454 xa_for_each(&ctx->personalities, index, creds)
9455 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009456 mutex_unlock(&ctx->uring_lock);
9457
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009458 io_kill_timeouts(ctx, NULL, true);
9459 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009460
Jens Axboe15dff282019-11-13 09:09:23 -07009461 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009462 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009463
Jens Axboe85faa7b2020-04-09 18:14:00 -06009464 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009465 /*
9466 * Use system_unbound_wq to avoid spawning tons of event kworkers
9467 * if we're exiting a ton of rings at the same time. It just adds
9468 * noise and overhead, there's no discernable change in runtime
9469 * over using system_wq.
9470 */
9471 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009472}
9473
9474static int io_uring_release(struct inode *inode, struct file *file)
9475{
9476 struct io_ring_ctx *ctx = file->private_data;
9477
9478 file->private_data = NULL;
9479 io_ring_ctx_wait_and_kill(ctx);
9480 return 0;
9481}
9482
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009483struct io_task_cancel {
9484 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009485 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009486};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009487
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009488static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009489{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009490 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009491 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009492 bool ret;
9493
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009494 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009495 struct io_ring_ctx *ctx = req->ctx;
9496
9497 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009498 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009499 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009500 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009501 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009502 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009503 }
9504 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009505}
9506
Pavel Begunkove1915f72021-03-11 23:29:35 +00009507static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009508 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009509{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009510 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009511 LIST_HEAD(list);
9512
Jens Axboe79ebeae2021-08-10 15:18:27 -06009513 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009514 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009515 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009516 list_cut_position(&list, &ctx->defer_list, &de->list);
9517 break;
9518 }
9519 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009520 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009521 if (list_empty(&list))
9522 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009523
9524 while (!list_empty(&list)) {
9525 de = list_first_entry(&list, struct io_defer_entry, list);
9526 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009527 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009528 kfree(de);
9529 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009530 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009531}
9532
Pavel Begunkov1b007642021-03-06 11:02:17 +00009533static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9534{
9535 struct io_tctx_node *node;
9536 enum io_wq_cancel cret;
9537 bool ret = false;
9538
9539 mutex_lock(&ctx->uring_lock);
9540 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9541 struct io_uring_task *tctx = node->task->io_uring;
9542
9543 /*
9544 * io_wq will stay alive while we hold uring_lock, because it's
9545 * killed after ctx nodes, which requires to take the lock.
9546 */
9547 if (!tctx || !tctx->io_wq)
9548 continue;
9549 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9550 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9551 }
9552 mutex_unlock(&ctx->uring_lock);
9553
9554 return ret;
9555}
9556
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009557static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9558 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009559 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009560{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009561 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009562 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009563
9564 while (1) {
9565 enum io_wq_cancel cret;
9566 bool ret = false;
9567
Pavel Begunkov1b007642021-03-06 11:02:17 +00009568 if (!task) {
9569 ret |= io_uring_try_cancel_iowq(ctx);
9570 } else if (tctx && tctx->io_wq) {
9571 /*
9572 * Cancels requests of all rings, not only @ctx, but
9573 * it's fine as the task is in exit/exec.
9574 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009575 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009576 &cancel, true);
9577 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9578 }
9579
9580 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009581 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009582 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009583 while (!list_empty_careful(&ctx->iopoll_list)) {
9584 io_iopoll_try_reap_events(ctx);
9585 ret = true;
9586 }
9587 }
9588
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009589 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9590 ret |= io_poll_remove_all(ctx, task, cancel_all);
9591 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009592 if (task)
9593 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009594 if (!ret)
9595 break;
9596 cond_resched();
9597 }
9598}
9599
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009600static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009601{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009602 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009603 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009604 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009605
9606 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009607 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009608 if (unlikely(ret))
9609 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009610 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009611 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009612 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9613 node = kmalloc(sizeof(*node), GFP_KERNEL);
9614 if (!node)
9615 return -ENOMEM;
9616 node->ctx = ctx;
9617 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009618
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009619 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9620 node, GFP_KERNEL));
9621 if (ret) {
9622 kfree(node);
9623 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009624 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009625
9626 mutex_lock(&ctx->uring_lock);
9627 list_add(&node->ctx_node, &ctx->tctx_list);
9628 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009629 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009630 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009631 return 0;
9632}
9633
9634/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009635 * Note that this task has used io_uring. We use it for cancelation purposes.
9636 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009637static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009638{
9639 struct io_uring_task *tctx = current->io_uring;
9640
9641 if (likely(tctx && tctx->last == ctx))
9642 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009643 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009644}
9645
9646/*
Jens Axboe0f212202020-09-13 13:09:39 -06009647 * Remove this io_uring_file -> task mapping.
9648 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009649static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009650{
9651 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009652 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009653
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009654 if (!tctx)
9655 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009656 node = xa_erase(&tctx->xa, index);
9657 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009658 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009659
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009660 WARN_ON_ONCE(current != node->task);
9661 WARN_ON_ONCE(list_empty(&node->ctx_node));
9662
9663 mutex_lock(&node->ctx->uring_lock);
9664 list_del(&node->ctx_node);
9665 mutex_unlock(&node->ctx->uring_lock);
9666
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009667 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009668 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009669 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009670}
9671
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009672static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009673{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009674 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009675 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009676 unsigned long index;
9677
Jens Axboe8bab4c02021-09-24 07:12:27 -06009678 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009679 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009680 cond_resched();
9681 }
Marco Elverb16ef422021-05-27 11:25:48 +02009682 if (wq) {
9683 /*
9684 * Must be after io_uring_del_task_file() (removes nodes under
9685 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9686 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009687 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009688 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009689 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009690}
9691
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009692static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009693{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009694 if (tracked)
9695 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009696 return percpu_counter_sum(&tctx->inflight);
9697}
9698
Pavel Begunkov09899b12021-06-14 02:36:22 +01009699static void io_uring_drop_tctx_refs(struct task_struct *task)
9700{
9701 struct io_uring_task *tctx = task->io_uring;
9702 unsigned int refs = tctx->cached_refs;
9703
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009704 if (refs) {
9705 tctx->cached_refs = 0;
9706 percpu_counter_sub(&tctx->inflight, refs);
9707 put_task_struct_many(task, refs);
9708 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009709}
9710
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009711/*
9712 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9713 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9714 */
9715static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009716{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009717 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009718 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009719 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009720 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009721
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009722 WARN_ON_ONCE(sqd && sqd->thread != current);
9723
Palash Oswal6d042ff2021-04-27 18:21:49 +05309724 if (!current->io_uring)
9725 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009726 if (tctx->io_wq)
9727 io_wq_exit_start(tctx->io_wq);
9728
Jens Axboefdaf0832020-10-30 09:37:30 -06009729 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009730 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009731 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009732 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009733 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009734 if (!inflight)
9735 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009736
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009737 if (!sqd) {
9738 struct io_tctx_node *node;
9739 unsigned long index;
9740
9741 xa_for_each(&tctx->xa, index, node) {
9742 /* sqpoll task will cancel all its requests */
9743 if (node->ctx->sq_data)
9744 continue;
9745 io_uring_try_cancel_requests(node->ctx, current,
9746 cancel_all);
9747 }
9748 } else {
9749 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9750 io_uring_try_cancel_requests(ctx, current,
9751 cancel_all);
9752 }
9753
9754 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009755 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009756 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009757 * If we've seen completions, retry without waiting. This
9758 * avoids a race where a completion comes in before we did
9759 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009760 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009761 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009762 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009763 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009764 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009765 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009766
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009767 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009768 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009769 /* for exec all current's requests should be gone, kill tctx */
9770 __io_uring_free(current);
9771 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009772}
9773
Hao Xuf552a272021-08-12 12:14:35 +08009774void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009775{
Hao Xuf552a272021-08-12 12:14:35 +08009776 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009777}
9778
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009779static void *io_uring_validate_mmap_request(struct file *file,
9780 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009781{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009782 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009783 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009784 struct page *page;
9785 void *ptr;
9786
9787 switch (offset) {
9788 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009789 case IORING_OFF_CQ_RING:
9790 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009791 break;
9792 case IORING_OFF_SQES:
9793 ptr = ctx->sq_sqes;
9794 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009795 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009796 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009797 }
9798
9799 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009800 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009801 return ERR_PTR(-EINVAL);
9802
9803 return ptr;
9804}
9805
9806#ifdef CONFIG_MMU
9807
9808static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9809{
9810 size_t sz = vma->vm_end - vma->vm_start;
9811 unsigned long pfn;
9812 void *ptr;
9813
9814 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9815 if (IS_ERR(ptr))
9816 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009817
9818 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9819 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9820}
9821
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009822#else /* !CONFIG_MMU */
9823
9824static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9825{
9826 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9827}
9828
9829static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9830{
9831 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9832}
9833
9834static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9835 unsigned long addr, unsigned long len,
9836 unsigned long pgoff, unsigned long flags)
9837{
9838 void *ptr;
9839
9840 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9841 if (IS_ERR(ptr))
9842 return PTR_ERR(ptr);
9843
9844 return (unsigned long) ptr;
9845}
9846
9847#endif /* !CONFIG_MMU */
9848
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009849static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009850{
9851 DEFINE_WAIT(wait);
9852
9853 do {
9854 if (!io_sqring_full(ctx))
9855 break;
Jens Axboe90554202020-09-03 12:12:41 -06009856 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9857
9858 if (!io_sqring_full(ctx))
9859 break;
Jens Axboe90554202020-09-03 12:12:41 -06009860 schedule();
9861 } while (!signal_pending(current));
9862
9863 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009864 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009865}
9866
Hao Xuc73ebb62020-11-03 10:54:37 +08009867static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9868 struct __kernel_timespec __user **ts,
9869 const sigset_t __user **sig)
9870{
9871 struct io_uring_getevents_arg arg;
9872
9873 /*
9874 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9875 * is just a pointer to the sigset_t.
9876 */
9877 if (!(flags & IORING_ENTER_EXT_ARG)) {
9878 *sig = (const sigset_t __user *) argp;
9879 *ts = NULL;
9880 return 0;
9881 }
9882
9883 /*
9884 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9885 * timespec and sigset_t pointers if good.
9886 */
9887 if (*argsz != sizeof(arg))
9888 return -EINVAL;
9889 if (copy_from_user(&arg, argp, sizeof(arg)))
9890 return -EFAULT;
9891 *sig = u64_to_user_ptr(arg.sigmask);
9892 *argsz = arg.sigmask_sz;
9893 *ts = u64_to_user_ptr(arg.ts);
9894 return 0;
9895}
9896
Jens Axboe2b188cc2019-01-07 10:46:33 -07009897SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009898 u32, min_complete, u32, flags, const void __user *, argp,
9899 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009900{
9901 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009902 int submitted = 0;
9903 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009904 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009905
Jens Axboe4c6e2772020-07-01 11:29:10 -06009906 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009907
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009908 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9909 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009910 return -EINVAL;
9911
9912 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009913 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009914 return -EBADF;
9915
9916 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009917 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009918 goto out_fput;
9919
9920 ret = -ENXIO;
9921 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009922 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009923 goto out_fput;
9924
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009925 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009926 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009927 goto out;
9928
Jens Axboe6c271ce2019-01-10 11:22:30 -07009929 /*
9930 * For SQ polling, the thread will do all submissions and completions.
9931 * Just return the requested submit count, and wake the thread if
9932 * we were asked to.
9933 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009934 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009935 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009936 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009937
Jens Axboe21f96522021-08-14 09:04:40 -06009938 if (unlikely(ctx->sq_data->thread == NULL)) {
9939 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009940 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009941 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009942 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009943 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009944 if (flags & IORING_ENTER_SQ_WAIT) {
9945 ret = io_sqpoll_wait_sq(ctx);
9946 if (ret)
9947 goto out;
9948 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009949 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009950 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009951 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009952 if (unlikely(ret))
9953 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009954 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009955 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009956 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009957
9958 if (submitted != to_submit)
9959 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009960 }
9961 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009962 const sigset_t __user *sig;
9963 struct __kernel_timespec __user *ts;
9964
9965 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9966 if (unlikely(ret))
9967 goto out;
9968
Jens Axboe2b188cc2019-01-07 10:46:33 -07009969 min_complete = min(min_complete, ctx->cq_entries);
9970
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009971 /*
9972 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9973 * space applications don't need to do io completion events
9974 * polling again, they can rely on io_sq_thread to do polling
9975 * work, which can reduce cpu usage and uring_lock contention.
9976 */
9977 if (ctx->flags & IORING_SETUP_IOPOLL &&
9978 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009979 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009980 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009981 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009982 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009983 }
9984
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009985out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009986 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009987out_fput:
9988 fdput(f);
9989 return submitted ? submitted : ret;
9990}
9991
Tobias Klauserbebdb652020-02-26 18:38:32 +01009992#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009993static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9994 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009995{
Jens Axboe87ce9552020-01-30 08:25:34 -07009996 struct user_namespace *uns = seq_user_ns(m);
9997 struct group_info *gi;
9998 kernel_cap_t cap;
9999 unsigned __capi;
10000 int g;
10001
10002 seq_printf(m, "%5d\n", id);
10003 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
10004 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
10005 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
10006 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
10007 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
10008 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
10009 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
10010 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
10011 seq_puts(m, "\n\tGroups:\t");
10012 gi = cred->group_info;
10013 for (g = 0; g < gi->ngroups; g++) {
10014 seq_put_decimal_ull(m, g ? " " : "",
10015 from_kgid_munged(uns, gi->gid[g]));
10016 }
10017 seq_puts(m, "\n\tCapEff:\t");
10018 cap = cred->cap_effective;
10019 CAP_FOR_EACH_U32(__capi)
10020 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
10021 seq_putc(m, '\n');
10022 return 0;
10023}
10024
10025static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
10026{
Joseph Qidbbe9c62020-09-29 09:01:22 -060010027 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +080010028 struct io_overflow_cqe *ocqe;
10029 struct io_rings *r = ctx->rings;
10030 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
10031 unsigned int cached_sq_head = ctx->cached_sq_head;
10032 unsigned int cached_cq_tail = ctx->cached_cq_tail;
10033 unsigned int sq_head = READ_ONCE(r->sq.head);
10034 unsigned int sq_tail = READ_ONCE(r->sq.tail);
10035 unsigned int cq_head = READ_ONCE(r->cq.head);
10036 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010037 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010038 unsigned int i;
10039
10040 /*
10041 * we may get imprecise sqe and cqe info if uring is actively running
10042 * since we get cached_sq_head and cached_cq_tail without uring_lock
10043 * and sq_tail and cq_head are changed by userspace. But it's ok since
10044 * we usually use these info when it is stuck.
10045 */
10046 seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask);
10047 seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask);
10048 seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask);
10049 seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask);
10050 seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask);
10051 seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask);
10052 seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head);
10053 for (i = cached_sq_head; i < sq_tail; i++) {
10054 unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]);
10055
10056 if (likely(sq_idx <= sq_mask)) {
10057 struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx];
10058
10059 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10060 sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data);
10061 }
10062 }
10063 seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head);
10064 for (i = cq_head; i < cached_cq_tail; i++) {
10065 struct io_uring_cqe *cqe = &r->cqes[i & cq_mask];
10066
10067 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
10068 i & cq_mask, cqe->user_data, cqe->res, cqe->flags);
10069 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010070
Jens Axboefad8e0d2020-09-28 08:57:48 -060010071 /*
10072 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10073 * since fdinfo case grabs it in the opposite direction of normal use
10074 * cases. If we fail to get the lock, we just don't iterate any
10075 * structures that could be going away outside the io_uring mutex.
10076 */
10077 has_lock = mutex_trylock(&ctx->uring_lock);
10078
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010079 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010080 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010081 if (!sq->thread)
10082 sq = NULL;
10083 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010084
10085 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10086 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010087 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010088 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010089 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010090
Jens Axboe87ce9552020-01-30 08:25:34 -070010091 if (f)
10092 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10093 else
10094 seq_printf(m, "%5u: <none>\n", i);
10095 }
10096 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010097 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010098 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010099 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010100
Pavel Begunkov4751f532021-04-01 15:43:55 +010010101 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010102 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010103 if (has_lock && !xa_empty(&ctx->personalities)) {
10104 unsigned long index;
10105 const struct cred *cred;
10106
Jens Axboe87ce9552020-01-30 08:25:34 -070010107 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010108 xa_for_each(&ctx->personalities, index, cred)
10109 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010110 }
Hao Xu83f84352021-09-13 21:08:54 +080010111 if (has_lock)
10112 mutex_unlock(&ctx->uring_lock);
10113
10114 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010115 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010116 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10117 struct hlist_head *list = &ctx->cancel_hash[i];
10118 struct io_kiocb *req;
10119
10120 hlist_for_each_entry(req, list, hash_node)
10121 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10122 req->task->task_works != NULL);
10123 }
Hao Xu83f84352021-09-13 21:08:54 +080010124
10125 seq_puts(m, "CqOverflowList:\n");
10126 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10127 struct io_uring_cqe *cqe = &ocqe->cqe;
10128
10129 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10130 cqe->user_data, cqe->res, cqe->flags);
10131
10132 }
10133
Jens Axboe79ebeae2021-08-10 15:18:27 -060010134 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010135}
10136
10137static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
10138{
10139 struct io_ring_ctx *ctx = f->private_data;
10140
10141 if (percpu_ref_tryget(&ctx->refs)) {
10142 __io_uring_show_fdinfo(ctx, m);
10143 percpu_ref_put(&ctx->refs);
10144 }
10145}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010146#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010147
Jens Axboe2b188cc2019-01-07 10:46:33 -070010148static const struct file_operations io_uring_fops = {
10149 .release = io_uring_release,
10150 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010151#ifndef CONFIG_MMU
10152 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10153 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10154#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010155 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010156#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010157 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010158#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010159};
10160
10161static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10162 struct io_uring_params *p)
10163{
Hristo Venev75b28af2019-08-26 17:23:46 +000010164 struct io_rings *rings;
10165 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010166
Jens Axboebd740482020-08-05 12:58:23 -060010167 /* make sure these are sane, as we already accounted them */
10168 ctx->sq_entries = p->sq_entries;
10169 ctx->cq_entries = p->cq_entries;
10170
Hristo Venev75b28af2019-08-26 17:23:46 +000010171 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10172 if (size == SIZE_MAX)
10173 return -EOVERFLOW;
10174
10175 rings = io_mem_alloc(size);
10176 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010177 return -ENOMEM;
10178
Hristo Venev75b28af2019-08-26 17:23:46 +000010179 ctx->rings = rings;
10180 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10181 rings->sq_ring_mask = p->sq_entries - 1;
10182 rings->cq_ring_mask = p->cq_entries - 1;
10183 rings->sq_ring_entries = p->sq_entries;
10184 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010185
10186 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010187 if (size == SIZE_MAX) {
10188 io_mem_free(ctx->rings);
10189 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010190 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010191 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010192
10193 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010194 if (!ctx->sq_sqes) {
10195 io_mem_free(ctx->rings);
10196 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010197 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010198 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010199
Jens Axboe2b188cc2019-01-07 10:46:33 -070010200 return 0;
10201}
10202
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010203static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10204{
10205 int ret, fd;
10206
10207 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10208 if (fd < 0)
10209 return fd;
10210
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010211 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010212 if (ret) {
10213 put_unused_fd(fd);
10214 return ret;
10215 }
10216 fd_install(fd, file);
10217 return fd;
10218}
10219
Jens Axboe2b188cc2019-01-07 10:46:33 -070010220/*
10221 * Allocate an anonymous fd, this is what constitutes the application
10222 * visible backing of an io_uring instance. The application mmaps this
10223 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10224 * we have to tie this fd to a socket for file garbage collection purposes.
10225 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010226static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010227{
10228 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010229#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010230 int ret;
10231
Jens Axboe2b188cc2019-01-07 10:46:33 -070010232 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10233 &ctx->ring_sock);
10234 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010235 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010236#endif
10237
Jens Axboe2b188cc2019-01-07 10:46:33 -070010238 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
10239 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010240#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010241 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010242 sock_release(ctx->ring_sock);
10243 ctx->ring_sock = NULL;
10244 } else {
10245 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010246 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010247#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010248 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010249}
10250
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010251static int io_uring_create(unsigned entries, struct io_uring_params *p,
10252 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010253{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010254 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010255 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010256 int ret;
10257
Jens Axboe8110c1a2019-12-28 15:39:54 -070010258 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010259 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010260 if (entries > IORING_MAX_ENTRIES) {
10261 if (!(p->flags & IORING_SETUP_CLAMP))
10262 return -EINVAL;
10263 entries = IORING_MAX_ENTRIES;
10264 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010265
10266 /*
10267 * Use twice as many entries for the CQ ring. It's possible for the
10268 * application to drive a higher depth than the size of the SQ ring,
10269 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010270 * some flexibility in overcommitting a bit. If the application has
10271 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10272 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010273 */
10274 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010275 if (p->flags & IORING_SETUP_CQSIZE) {
10276 /*
10277 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10278 * to a power-of-two, if it isn't already. We do NOT impose
10279 * any cq vs sq ring sizing.
10280 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010281 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010282 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010283 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10284 if (!(p->flags & IORING_SETUP_CLAMP))
10285 return -EINVAL;
10286 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10287 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010288 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10289 if (p->cq_entries < p->sq_entries)
10290 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010291 } else {
10292 p->cq_entries = 2 * p->sq_entries;
10293 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010294
Jens Axboe2b188cc2019-01-07 10:46:33 -070010295 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010296 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010297 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010298 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010299 if (!capable(CAP_IPC_LOCK))
10300 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010301
10302 /*
10303 * This is just grabbed for accounting purposes. When a process exits,
10304 * the mm is exited and dropped before the files, hence we need to hang
10305 * on to this mm purely for the purposes of being able to unaccount
10306 * memory (locked/pinned vm). It's not used for anything else.
10307 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010308 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010309 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010310
Jens Axboe2b188cc2019-01-07 10:46:33 -070010311 ret = io_allocate_scq_urings(ctx, p);
10312 if (ret)
10313 goto err;
10314
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010315 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010316 if (ret)
10317 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010318 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010319 ret = io_rsrc_node_switch_start(ctx);
10320 if (ret)
10321 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010322 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010323
Jens Axboe2b188cc2019-01-07 10:46:33 -070010324 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010325 p->sq_off.head = offsetof(struct io_rings, sq.head);
10326 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10327 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10328 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10329 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10330 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10331 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010332
10333 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010334 p->cq_off.head = offsetof(struct io_rings, cq.head);
10335 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10336 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10337 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10338 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10339 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010340 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010341
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010342 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10343 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010344 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010345 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010346 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
10347 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010348
10349 if (copy_to_user(params, p, sizeof(*p))) {
10350 ret = -EFAULT;
10351 goto err;
10352 }
Jens Axboed1719f72020-07-30 13:43:53 -060010353
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010354 file = io_uring_get_file(ctx);
10355 if (IS_ERR(file)) {
10356 ret = PTR_ERR(file);
10357 goto err;
10358 }
10359
Jens Axboed1719f72020-07-30 13:43:53 -060010360 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010361 * Install ring fd as the very last thing, so we don't risk someone
10362 * having closed it before we finish setup
10363 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010364 ret = io_uring_install_fd(ctx, file);
10365 if (ret < 0) {
10366 /* fput will clean it up */
10367 fput(file);
10368 return ret;
10369 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010370
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010371 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010372 return ret;
10373err:
10374 io_ring_ctx_wait_and_kill(ctx);
10375 return ret;
10376}
10377
10378/*
10379 * Sets up an aio uring context, and returns the fd. Applications asks for a
10380 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10381 * params structure passed in.
10382 */
10383static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10384{
10385 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010386 int i;
10387
10388 if (copy_from_user(&p, params, sizeof(p)))
10389 return -EFAULT;
10390 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10391 if (p.resv[i])
10392 return -EINVAL;
10393 }
10394
Jens Axboe6c271ce2019-01-10 11:22:30 -070010395 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010396 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010397 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10398 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010399 return -EINVAL;
10400
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010401 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010402}
10403
10404SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10405 struct io_uring_params __user *, params)
10406{
10407 return io_uring_setup(entries, params);
10408}
10409
Jens Axboe66f4af92020-01-16 15:36:52 -070010410static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
10411{
10412 struct io_uring_probe *p;
10413 size_t size;
10414 int i, ret;
10415
10416 size = struct_size(p, ops, nr_args);
10417 if (size == SIZE_MAX)
10418 return -EOVERFLOW;
10419 p = kzalloc(size, GFP_KERNEL);
10420 if (!p)
10421 return -ENOMEM;
10422
10423 ret = -EFAULT;
10424 if (copy_from_user(p, arg, size))
10425 goto out;
10426 ret = -EINVAL;
10427 if (memchr_inv(p, 0, size))
10428 goto out;
10429
10430 p->last_op = IORING_OP_LAST - 1;
10431 if (nr_args > IORING_OP_LAST)
10432 nr_args = IORING_OP_LAST;
10433
10434 for (i = 0; i < nr_args; i++) {
10435 p->ops[i].op = i;
10436 if (!io_op_defs[i].not_supported)
10437 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10438 }
10439 p->ops_len = i;
10440
10441 ret = 0;
10442 if (copy_to_user(arg, p, size))
10443 ret = -EFAULT;
10444out:
10445 kfree(p);
10446 return ret;
10447}
10448
Jens Axboe071698e2020-01-28 10:04:42 -070010449static int io_register_personality(struct io_ring_ctx *ctx)
10450{
Jens Axboe4379bf82021-02-15 13:40:22 -070010451 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010452 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010453 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010454
Jens Axboe4379bf82021-02-15 13:40:22 -070010455 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010456
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010457 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10458 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010459 if (ret < 0) {
10460 put_cred(creds);
10461 return ret;
10462 }
10463 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010464}
10465
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010466static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
10467 unsigned int nr_args)
10468{
10469 struct io_uring_restriction *res;
10470 size_t size;
10471 int i, ret;
10472
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010473 /* Restrictions allowed only if rings started disabled */
10474 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10475 return -EBADFD;
10476
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010477 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010478 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010479 return -EBUSY;
10480
10481 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10482 return -EINVAL;
10483
10484 size = array_size(nr_args, sizeof(*res));
10485 if (size == SIZE_MAX)
10486 return -EOVERFLOW;
10487
10488 res = memdup_user(arg, size);
10489 if (IS_ERR(res))
10490 return PTR_ERR(res);
10491
10492 ret = 0;
10493
10494 for (i = 0; i < nr_args; i++) {
10495 switch (res[i].opcode) {
10496 case IORING_RESTRICTION_REGISTER_OP:
10497 if (res[i].register_op >= IORING_REGISTER_LAST) {
10498 ret = -EINVAL;
10499 goto out;
10500 }
10501
10502 __set_bit(res[i].register_op,
10503 ctx->restrictions.register_op);
10504 break;
10505 case IORING_RESTRICTION_SQE_OP:
10506 if (res[i].sqe_op >= IORING_OP_LAST) {
10507 ret = -EINVAL;
10508 goto out;
10509 }
10510
10511 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10512 break;
10513 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10514 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10515 break;
10516 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10517 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10518 break;
10519 default:
10520 ret = -EINVAL;
10521 goto out;
10522 }
10523 }
10524
10525out:
10526 /* Reset all restrictions if an error happened */
10527 if (ret != 0)
10528 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10529 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010530 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010531
10532 kfree(res);
10533 return ret;
10534}
10535
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010536static int io_register_enable_rings(struct io_ring_ctx *ctx)
10537{
10538 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10539 return -EBADFD;
10540
10541 if (ctx->restrictions.registered)
10542 ctx->restricted = 1;
10543
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010544 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10545 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10546 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010547 return 0;
10548}
10549
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010550static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010551 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010552 unsigned nr_args)
10553{
10554 __u32 tmp;
10555 int err;
10556
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010557 if (up->resv)
10558 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010559 if (check_add_overflow(up->offset, nr_args, &tmp))
10560 return -EOVERFLOW;
10561 err = io_rsrc_node_switch_start(ctx);
10562 if (err)
10563 return err;
10564
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010565 switch (type) {
10566 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010567 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010568 case IORING_RSRC_BUFFER:
10569 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010570 }
10571 return -EINVAL;
10572}
10573
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010574static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10575 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010576{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010577 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010578
10579 if (!nr_args)
10580 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010581 memset(&up, 0, sizeof(up));
10582 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10583 return -EFAULT;
10584 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10585}
10586
10587static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010588 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010589{
10590 struct io_uring_rsrc_update2 up;
10591
10592 if (size != sizeof(up))
10593 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010594 if (copy_from_user(&up, arg, sizeof(up)))
10595 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010596 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010597 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010598 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010599}
10600
Pavel Begunkov792e3582021-04-25 14:32:21 +010010601static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010602 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010603{
10604 struct io_uring_rsrc_register rr;
10605
10606 /* keep it extendible */
10607 if (size != sizeof(rr))
10608 return -EINVAL;
10609
10610 memset(&rr, 0, sizeof(rr));
10611 if (copy_from_user(&rr, arg, size))
10612 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010613 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010614 return -EINVAL;
10615
Pavel Begunkov992da012021-06-10 16:37:37 +010010616 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010617 case IORING_RSRC_FILE:
10618 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10619 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010620 case IORING_RSRC_BUFFER:
10621 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10622 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010623 }
10624 return -EINVAL;
10625}
10626
Jens Axboefe764212021-06-17 10:19:54 -060010627static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10628 unsigned len)
10629{
10630 struct io_uring_task *tctx = current->io_uring;
10631 cpumask_var_t new_mask;
10632 int ret;
10633
10634 if (!tctx || !tctx->io_wq)
10635 return -EINVAL;
10636
10637 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10638 return -ENOMEM;
10639
10640 cpumask_clear(new_mask);
10641 if (len > cpumask_size())
10642 len = cpumask_size();
10643
10644 if (copy_from_user(new_mask, arg, len)) {
10645 free_cpumask_var(new_mask);
10646 return -EFAULT;
10647 }
10648
10649 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10650 free_cpumask_var(new_mask);
10651 return ret;
10652}
10653
10654static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10655{
10656 struct io_uring_task *tctx = current->io_uring;
10657
10658 if (!tctx || !tctx->io_wq)
10659 return -EINVAL;
10660
10661 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10662}
10663
Jens Axboe2e480052021-08-27 11:33:19 -060010664static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10665 void __user *arg)
10666{
Jens Axboefa846932021-09-01 14:15:59 -060010667 struct io_uring_task *tctx = NULL;
10668 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010669 __u32 new_count[2];
10670 int i, ret;
10671
Jens Axboe2e480052021-08-27 11:33:19 -060010672 if (copy_from_user(new_count, arg, sizeof(new_count)))
10673 return -EFAULT;
10674 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10675 if (new_count[i] > INT_MAX)
10676 return -EINVAL;
10677
Jens Axboefa846932021-09-01 14:15:59 -060010678 if (ctx->flags & IORING_SETUP_SQPOLL) {
10679 sqd = ctx->sq_data;
10680 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010681 /*
10682 * Observe the correct sqd->lock -> ctx->uring_lock
10683 * ordering. Fine to drop uring_lock here, we hold
10684 * a ref to the ctx.
10685 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010686 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010687 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010688 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010689 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010690 if (sqd->thread)
10691 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010692 }
10693 } else {
10694 tctx = current->io_uring;
10695 }
10696
10697 ret = -EINVAL;
10698 if (!tctx || !tctx->io_wq)
10699 goto err;
10700
Jens Axboe2e480052021-08-27 11:33:19 -060010701 ret = io_wq_max_workers(tctx->io_wq, new_count);
10702 if (ret)
Jens Axboefa846932021-09-01 14:15:59 -060010703 goto err;
10704
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010705 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010706 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010707 io_put_sq_data(sqd);
10708 }
Jens Axboe2e480052021-08-27 11:33:19 -060010709
10710 if (copy_to_user(arg, new_count, sizeof(new_count)))
10711 return -EFAULT;
10712
10713 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010714err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010715 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010716 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010717 io_put_sq_data(sqd);
10718 }
Jens Axboefa846932021-09-01 14:15:59 -060010719 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010720}
10721
Jens Axboe071698e2020-01-28 10:04:42 -070010722static bool io_register_op_must_quiesce(int op)
10723{
10724 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010725 case IORING_REGISTER_BUFFERS:
10726 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010727 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010728 case IORING_UNREGISTER_FILES:
10729 case IORING_REGISTER_FILES_UPDATE:
10730 case IORING_REGISTER_PROBE:
10731 case IORING_REGISTER_PERSONALITY:
10732 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010733 case IORING_REGISTER_FILES2:
10734 case IORING_REGISTER_FILES_UPDATE2:
10735 case IORING_REGISTER_BUFFERS2:
10736 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010737 case IORING_REGISTER_IOWQ_AFF:
10738 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010739 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010740 return false;
10741 default:
10742 return true;
10743 }
10744}
10745
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010746static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10747{
10748 long ret;
10749
10750 percpu_ref_kill(&ctx->refs);
10751
10752 /*
10753 * Drop uring mutex before waiting for references to exit. If another
10754 * thread is currently inside io_uring_enter() it might need to grab the
10755 * uring_lock to make progress. If we hold it here across the drain
10756 * wait, then we can deadlock. It's safe to drop the mutex here, since
10757 * no new references will come in after we've killed the percpu ref.
10758 */
10759 mutex_unlock(&ctx->uring_lock);
10760 do {
10761 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10762 if (!ret)
10763 break;
10764 ret = io_run_task_work_sig();
10765 } while (ret >= 0);
10766 mutex_lock(&ctx->uring_lock);
10767
10768 if (ret)
10769 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10770 return ret;
10771}
10772
Jens Axboeedafcce2019-01-09 09:16:05 -070010773static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10774 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010775 __releases(ctx->uring_lock)
10776 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010777{
10778 int ret;
10779
Jens Axboe35fa71a2019-04-22 10:23:23 -060010780 /*
10781 * We're inside the ring mutex, if the ref is already dying, then
10782 * someone else killed the ctx or is already going through
10783 * io_uring_register().
10784 */
10785 if (percpu_ref_is_dying(&ctx->refs))
10786 return -ENXIO;
10787
Pavel Begunkov75c40212021-04-15 13:07:40 +010010788 if (ctx->restricted) {
10789 if (opcode >= IORING_REGISTER_LAST)
10790 return -EINVAL;
10791 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10792 if (!test_bit(opcode, ctx->restrictions.register_op))
10793 return -EACCES;
10794 }
10795
Jens Axboe071698e2020-01-28 10:04:42 -070010796 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010797 ret = io_ctx_quiesce(ctx);
10798 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010799 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010800 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010801
10802 switch (opcode) {
10803 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010804 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010805 break;
10806 case IORING_UNREGISTER_BUFFERS:
10807 ret = -EINVAL;
10808 if (arg || nr_args)
10809 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010810 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010811 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010812 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010813 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010814 break;
10815 case IORING_UNREGISTER_FILES:
10816 ret = -EINVAL;
10817 if (arg || nr_args)
10818 break;
10819 ret = io_sqe_files_unregister(ctx);
10820 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010821 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010822 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010823 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010824 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010825 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010826 ret = -EINVAL;
10827 if (nr_args != 1)
10828 break;
10829 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010830 if (ret)
10831 break;
10832 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10833 ctx->eventfd_async = 1;
10834 else
10835 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010836 break;
10837 case IORING_UNREGISTER_EVENTFD:
10838 ret = -EINVAL;
10839 if (arg || nr_args)
10840 break;
10841 ret = io_eventfd_unregister(ctx);
10842 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010843 case IORING_REGISTER_PROBE:
10844 ret = -EINVAL;
10845 if (!arg || nr_args > 256)
10846 break;
10847 ret = io_probe(ctx, arg, nr_args);
10848 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010849 case IORING_REGISTER_PERSONALITY:
10850 ret = -EINVAL;
10851 if (arg || nr_args)
10852 break;
10853 ret = io_register_personality(ctx);
10854 break;
10855 case IORING_UNREGISTER_PERSONALITY:
10856 ret = -EINVAL;
10857 if (arg)
10858 break;
10859 ret = io_unregister_personality(ctx, nr_args);
10860 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010861 case IORING_REGISTER_ENABLE_RINGS:
10862 ret = -EINVAL;
10863 if (arg || nr_args)
10864 break;
10865 ret = io_register_enable_rings(ctx);
10866 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010867 case IORING_REGISTER_RESTRICTIONS:
10868 ret = io_register_restrictions(ctx, arg, nr_args);
10869 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010870 case IORING_REGISTER_FILES2:
10871 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010872 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010873 case IORING_REGISTER_FILES_UPDATE2:
10874 ret = io_register_rsrc_update(ctx, arg, nr_args,
10875 IORING_RSRC_FILE);
10876 break;
10877 case IORING_REGISTER_BUFFERS2:
10878 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10879 break;
10880 case IORING_REGISTER_BUFFERS_UPDATE:
10881 ret = io_register_rsrc_update(ctx, arg, nr_args,
10882 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010883 break;
Jens Axboefe764212021-06-17 10:19:54 -060010884 case IORING_REGISTER_IOWQ_AFF:
10885 ret = -EINVAL;
10886 if (!arg || !nr_args)
10887 break;
10888 ret = io_register_iowq_aff(ctx, arg, nr_args);
10889 break;
10890 case IORING_UNREGISTER_IOWQ_AFF:
10891 ret = -EINVAL;
10892 if (arg || nr_args)
10893 break;
10894 ret = io_unregister_iowq_aff(ctx);
10895 break;
Jens Axboe2e480052021-08-27 11:33:19 -060010896 case IORING_REGISTER_IOWQ_MAX_WORKERS:
10897 ret = -EINVAL;
10898 if (!arg || nr_args != 2)
10899 break;
10900 ret = io_register_iowq_max_workers(ctx, arg);
10901 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010902 default:
10903 ret = -EINVAL;
10904 break;
10905 }
10906
Jens Axboe071698e2020-01-28 10:04:42 -070010907 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010908 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010909 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010910 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010911 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010912 return ret;
10913}
10914
10915SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10916 void __user *, arg, unsigned int, nr_args)
10917{
10918 struct io_ring_ctx *ctx;
10919 long ret = -EBADF;
10920 struct fd f;
10921
10922 f = fdget(fd);
10923 if (!f.file)
10924 return -EBADF;
10925
10926 ret = -EOPNOTSUPP;
10927 if (f.file->f_op != &io_uring_fops)
10928 goto out_fput;
10929
10930 ctx = f.file->private_data;
10931
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010932 io_run_task_work();
10933
Jens Axboeedafcce2019-01-09 09:16:05 -070010934 mutex_lock(&ctx->uring_lock);
10935 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10936 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010937 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10938 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010939out_fput:
10940 fdput(f);
10941 return ret;
10942}
10943
Jens Axboe2b188cc2019-01-07 10:46:33 -070010944static int __init io_uring_init(void)
10945{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010946#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10947 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10948 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10949} while (0)
10950
10951#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10952 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10953 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10954 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10955 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10956 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10957 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10958 BUILD_BUG_SQE_ELEM(8, __u64, off);
10959 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10960 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010961 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010962 BUILD_BUG_SQE_ELEM(24, __u32, len);
10963 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10964 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10965 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10966 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010967 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10968 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010969 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10970 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10971 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10972 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10973 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10974 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10975 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10976 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010977 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010978 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10979 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010980 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010981 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010982 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010010983 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010984
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010985 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10986 sizeof(struct io_uring_rsrc_update));
10987 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10988 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010010989
10990 /* ->buf_index is u16 */
10991 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
10992
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010993 /* should fit into one byte */
10994 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010010995 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
10996 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010997
Jens Axboed3656342019-12-18 09:50:26 -070010998 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080010999 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011000
Jens Axboe91f245d2021-02-09 13:48:50 -070011001 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
11002 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070011003 return 0;
11004};
11005__initcall(io_uring_init);